|
| 1 | +# Copyright (c) 2024 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 | + |
| 17 | +import numpy as np |
| 18 | +from get_test_cover_info import ( |
| 19 | + XPUOpTestWrapper, |
| 20 | + create_test_class, |
| 21 | + get_xpu_op_support_types, |
| 22 | +) |
| 23 | +from op_test_xpu import XPUOpTest |
| 24 | + |
| 25 | +import paddle |
| 26 | +from paddle.base.framework import convert_np_dtype_to_dtype_ |
| 27 | + |
| 28 | +paddle.enable_static() |
| 29 | + |
| 30 | + |
| 31 | +class XPUTestEmptyLikeOp(XPUOpTestWrapper): |
| 32 | + def __init__(self): |
| 33 | + self.op_name = 'empty_like' |
| 34 | + self.use_dynamic_create_class = False |
| 35 | + |
| 36 | + # Situation 1: Attr(shape) is a list(without tensor) |
| 37 | + class TestEmptyLikeOp(XPUOpTest): |
| 38 | + def setUp(self): |
| 39 | + self.op_type = "empty_like" |
| 40 | + self.init_dtype() |
| 41 | + self.set_xpu() |
| 42 | + self.place = paddle.XPUPlace(0) |
| 43 | + self.set_inputs() |
| 44 | + self.init_config() |
| 45 | + |
| 46 | + def test_check_output(self): |
| 47 | + self.check_output_customized(self.verify_output) |
| 48 | + |
| 49 | + def verify_output(self, outs): |
| 50 | + data_type = outs[0].dtype |
| 51 | + if data_type in [ |
| 52 | + 'float32', |
| 53 | + 'float64', |
| 54 | + 'int32', |
| 55 | + 'int64', |
| 56 | + 'int8', |
| 57 | + 'uint8', |
| 58 | + 'float16', |
| 59 | + 'int16', |
| 60 | + 'uint16', |
| 61 | + ]: |
| 62 | + max_value = np.nanmax(outs[0]) |
| 63 | + min_value = np.nanmin(outs[0]) |
| 64 | + |
| 65 | + always_full_zero = max_value == 0.0 and min_value == 0.0 |
| 66 | + always_non_full_zero = max_value >= min_value |
| 67 | + self.assertTrue( |
| 68 | + always_full_zero or always_non_full_zero, |
| 69 | + 'always_full_zero or always_non_full_zero.', |
| 70 | + ) |
| 71 | + elif data_type in ['bool']: |
| 72 | + total_num = outs[0].size |
| 73 | + true_num = np.sum(outs[0]) |
| 74 | + false_num = np.sum(~outs[0]) |
| 75 | + self.assertTrue( |
| 76 | + total_num == true_num + false_num, |
| 77 | + 'The value should always be True or False.', |
| 78 | + ) |
| 79 | + else: |
| 80 | + # pass |
| 81 | + self.assertTrue(False, 'invalid data type') |
| 82 | + |
| 83 | + def set_inputs(self): |
| 84 | + self.inputs = {} |
| 85 | + |
| 86 | + def init_config(self): |
| 87 | + dtype_inner = convert_np_dtype_to_dtype_(self.dtype) |
| 88 | + self.attrs = {'shape': self.shape, 'dtype': dtype_inner} |
| 89 | + self.outputs = {'Out': np.zeros(self.shape).astype(self.dtype)} |
| 90 | + |
| 91 | + def init_dtype(self): |
| 92 | + self.dtype = self.in_type |
| 93 | + |
| 94 | + def set_xpu(self): |
| 95 | + self.__class__.use_xpu = True |
| 96 | + self.__class__.no_need_check_grad = True |
| 97 | + self.__class__.op_type = self.op_type |
| 98 | + |
| 99 | + class TestEmptyLikeOpCase1(TestEmptyLikeOp): |
| 100 | + def set_input(self): |
| 101 | + x = np.random.uniform(size=[50]).astype(self.dtype) |
| 102 | + self.inputs = {'x': x} |
| 103 | + |
| 104 | + class TestEmptyLikeOpCase2(TestEmptyLikeOp): |
| 105 | + def set_input(self): |
| 106 | + x = np.random.uniform(size=[1, 50, 3, 4]).astype(self.dtype) |
| 107 | + self.inputs = {'x': x} |
| 108 | + |
| 109 | + class TestEmptyLikeOpCase3(TestEmptyLikeOp): |
| 110 | + def set_input(self): |
| 111 | + x = np.random.uniform(size=[5, 5, 5]).astype(self.dtype) |
| 112 | + self.inputs = {'x': x} |
| 113 | + |
| 114 | + # Situation 2: x and output have different dtypes |
| 115 | + class TestEmptyLikeOp_DifferentDtype(TestEmptyLikeOp): |
| 116 | + def set_inputs(self): |
| 117 | + x = np.random.uniform(size=[500, 3]).astype("int32") |
| 118 | + self.inputs = {"x": x} |
| 119 | + |
| 120 | + |
| 121 | +support_types = get_xpu_op_support_types('empty_like') |
| 122 | +for stype in support_types: |
| 123 | + create_test_class(globals(), XPUTestEmptyLikeOp, stype) |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + unittest.main() |
0 commit comments