|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from typing import Optional |
| 17 | +import numpy as np |
| 18 | +import paddle |
| 19 | +import paddle.fluid.core as core |
| 20 | + |
| 21 | +# from op_test import OpTest |
| 22 | + |
| 23 | + |
| 24 | +def np_nan_to_num( |
| 25 | + x: np.ndarray, |
| 26 | + nan: float = 0.0, |
| 27 | + posinf: Optional[float] = None, |
| 28 | + neginf: Optional[float] = None, |
| 29 | +) -> np.ndarray: |
| 30 | + return np.nan_to_num(x, True, nan=nan, posinf=posinf, neginf=neginf) |
| 31 | + |
| 32 | + |
| 33 | +def np_nan_to_num_op( |
| 34 | + x: np.ndarray, |
| 35 | + nan: float, |
| 36 | + replace_posinf_with_max: bool, |
| 37 | + posinf: float, |
| 38 | + replace_neginf_with_min: bool, |
| 39 | + neginf: float, |
| 40 | +) -> np.ndarray: |
| 41 | + if replace_posinf_with_max: |
| 42 | + posinf = None |
| 43 | + if replace_neginf_with_min: |
| 44 | + neginf = None |
| 45 | + return np.nan_to_num(x, True, nan=nan, posinf=posinf, neginf=neginf) |
| 46 | + |
| 47 | + |
| 48 | +def np_nan_to_num_grad(x: np.ndarray, dout: np.ndarray) -> np.ndarray: |
| 49 | + dx = np.copy(dout) |
| 50 | + dx[np.isnan(x) | (x == np.inf) | (x == -np.inf)] = 0 |
| 51 | + return dx |
| 52 | + |
| 53 | + |
| 54 | +class TestNanToNum(unittest.TestCase): |
| 55 | + def setUp(self): |
| 56 | + self.place = ( |
| 57 | + paddle.CUDAPlace(0) |
| 58 | + if core.is_compiled_with_cuda() |
| 59 | + else paddle.CPUPlace() |
| 60 | + ) |
| 61 | + |
| 62 | + def test_static(self): |
| 63 | + x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( |
| 64 | + np.float32 |
| 65 | + ) |
| 66 | + out1_np = np_nan_to_num(x_np) |
| 67 | + out2_np = np_nan_to_num(x_np, 1.0) |
| 68 | + out3_np = np_nan_to_num(x_np, 1.0, 9.0) |
| 69 | + out4_np = np_nan_to_num(x_np, 1.0, 9.0, -12.0) |
| 70 | + paddle.enable_static() |
| 71 | + with paddle.static.program_guard(paddle.static.Program()): |
| 72 | + x = paddle.fluid.data('X', x_np.shape) |
| 73 | + out1 = paddle.nan_to_num(x) |
| 74 | + out2 = paddle.nan_to_num(x, 1.0) |
| 75 | + out3 = paddle.nan_to_num(x, 1.0, 9.0) |
| 76 | + out4 = paddle.nan_to_num(x, 1.0, 9.0, -12.0) |
| 77 | + exe = paddle.static.Executor(self.place) |
| 78 | + res = exe.run(feed={'X': x_np}, fetch_list=[out1, out2, out3, out4]) |
| 79 | + |
| 80 | + self.assertTrue(np.allclose(out1_np, res[0])) |
| 81 | + self.assertTrue(np.allclose(out2_np, res[1])) |
| 82 | + self.assertTrue(np.allclose(out3_np, res[2])) |
| 83 | + self.assertTrue(np.allclose(out4_np, res[3])) |
| 84 | + |
| 85 | + def test_dygraph(self): |
| 86 | + |
| 87 | + paddle.disable_static(place=self.place) |
| 88 | + |
| 89 | + with paddle.fluid.dygraph.guard(): |
| 90 | + # NOTE(tiancaishaonvjituizi): float64 input fails the test |
| 91 | + x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( |
| 92 | + np.float32 |
| 93 | + # np.float64 |
| 94 | + ) |
| 95 | + x_tensor = paddle.to_tensor(x_np, stop_gradient=False) |
| 96 | + |
| 97 | + out_tensor = paddle.nan_to_num(x_tensor) |
| 98 | + out_np = np_nan_to_num(x_np) |
| 99 | + self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) |
| 100 | + |
| 101 | + out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, None) |
| 102 | + out_np = np_nan_to_num(x_np, 1, None, None) |
| 103 | + self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) |
| 104 | + |
| 105 | + out_tensor = paddle.nan_to_num(x_tensor, 1.0, 2.0, None) |
| 106 | + out_np = np_nan_to_num(x_np, 1, 2, None) |
| 107 | + self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) |
| 108 | + |
| 109 | + out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, -10.0) |
| 110 | + out_np = np_nan_to_num(x_np, 1, None, -10) |
| 111 | + self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) |
| 112 | + |
| 113 | + out_tensor = paddle.nan_to_num(x_tensor, 1.0, 100.0, -10.0) |
| 114 | + out_np = np_nan_to_num(x_np, 1, 100, -10) |
| 115 | + self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) |
| 116 | + |
| 117 | + paddle.enable_static() |
| 118 | + |
| 119 | + def test_check_grad(self): |
| 120 | + paddle.disable_static(place=self.place) |
| 121 | + x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( |
| 122 | + np.float32 |
| 123 | + ) |
| 124 | + x_tensor = paddle.to_tensor(x_np, stop_gradient=False) |
| 125 | + |
| 126 | + y = paddle.nan_to_num(x_tensor) |
| 127 | + dx = paddle.grad(y, x_tensor)[0].numpy() |
| 128 | + |
| 129 | + np_grad = np_nan_to_num_grad(x_np, np.ones_like(x_np)) |
| 130 | + self.assertTrue(np.allclose(np_grad, dx)) |
| 131 | + |
| 132 | + paddle.enable_static() |
| 133 | + |
| 134 | + |
| 135 | +# class BaseTestCases: |
| 136 | +# |
| 137 | +# class BaseOpTest(OpTest): |
| 138 | +# |
| 139 | +# def setUp(self): |
| 140 | +# self.op_type = "nan_to_num" |
| 141 | +# input = np.arange(100, dtype=np.float64) |
| 142 | +# input[5] = np.nan |
| 143 | +# input[29] = np.inf |
| 144 | +# input[97] = -np.inf |
| 145 | +# self.inputs = {'X': input} |
| 146 | +# self.attrs = self._attrs() |
| 147 | +# self.outputs = { |
| 148 | +# 'Out': np_nan_to_num_op(self.inputs['X'], **self.attrs) |
| 149 | +# } |
| 150 | +# paddle.enable_static() |
| 151 | +# |
| 152 | +# def test_check_output(self): |
| 153 | +# self.check_output() |
| 154 | +# |
| 155 | +# def test_check_grad(self): |
| 156 | +# input = self.inputs['X'] |
| 157 | +# dout = np.ones_like(input) / input.size |
| 158 | +# self.check_grad( |
| 159 | +# ['X'], |
| 160 | +# 'Out', |
| 161 | +# user_defined_grads=[np_nan_to_num_grad(self.inputs['X'], dout)]) |
| 162 | +# |
| 163 | +# def _attrs(self): |
| 164 | +# raise NotImplementedError() |
| 165 | +# |
| 166 | +# |
| 167 | +# class TestNanToNumOp1(BaseTestCases.BaseOpTest): |
| 168 | +# |
| 169 | +# def _attrs(self): |
| 170 | +# return { |
| 171 | +# 'nan': 0.0, |
| 172 | +# 'replace_posinf_with_max': True, |
| 173 | +# 'posinf': -1, |
| 174 | +# 'replace_neginf_with_min': True, |
| 175 | +# 'neginf': -10 |
| 176 | +# } |
| 177 | +# |
| 178 | +# |
| 179 | +# class TestNanToNumOp2(BaseTestCases.BaseOpTest): |
| 180 | +# |
| 181 | +# def _attrs(self): |
| 182 | +# return { |
| 183 | +# 'nan': 2.0, |
| 184 | +# 'replace_posinf_with_max': False, |
| 185 | +# 'posinf': -1, |
| 186 | +# 'replace_neginf_with_min': True, |
| 187 | +# 'neginf': -10 |
| 188 | +# } |
| 189 | +# |
| 190 | +# |
| 191 | +# class TestNanToNumOp3(BaseTestCases.BaseOpTest): |
| 192 | +# |
| 193 | +# def _attrs(self): |
| 194 | +# return { |
| 195 | +# 'nan': 0.0, |
| 196 | +# 'replace_posinf_with_max': False, |
| 197 | +# 'posinf': -1, |
| 198 | +# 'replace_neginf_with_min': False, |
| 199 | +# 'neginf': -10 |
| 200 | +# } |
| 201 | + |
| 202 | +if __name__ == "__main__": |
| 203 | + unittest.main() |
0 commit comments