|
| 1 | +# Copyright (c) 2025 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 | + |
| 19 | +import paddle |
| 20 | +from paddle import base |
| 21 | + |
| 22 | + |
| 23 | +def api_warpprt(x, y): |
| 24 | + return x.type_as(y) |
| 25 | + |
| 26 | + |
| 27 | +class TestTypeAsBase(unittest.TestCase): |
| 28 | + def setUp(self): |
| 29 | + self.input_dtype_1 = "float32" |
| 30 | + self.input_dtype_2 = "float16" |
| 31 | + self.input_shape = (2, 3) |
| 32 | + |
| 33 | + self.input_np_1 = self.generate_data( |
| 34 | + self.input_dtype_1, self.input_shape |
| 35 | + ) |
| 36 | + self.input_np_2 = self.generate_data( |
| 37 | + self.input_dtype_2, self.input_shape |
| 38 | + ) |
| 39 | + |
| 40 | + self.input_shape_1 = self.input_np_1.shape |
| 41 | + self.input_shape_2 = self.input_np_2.shape |
| 42 | + |
| 43 | + self.op_static = api_warpprt |
| 44 | + self.op_dygraph = api_warpprt |
| 45 | + self.places = [None, paddle.CPUPlace()] |
| 46 | + |
| 47 | + def generate_data(self, dtype, shape): |
| 48 | + if "int" in dtype: |
| 49 | + data = np.arange(1, np.prod(shape) + 1).reshape(shape) |
| 50 | + else: |
| 51 | + data = np.arange(1, np.prod(shape) + 1, dtype='float32').reshape( |
| 52 | + shape |
| 53 | + ) |
| 54 | + return data.astype(dtype) |
| 55 | + |
| 56 | + def check_static_result(self, place): |
| 57 | + paddle.enable_static() |
| 58 | + main_prog = paddle.static.Program() |
| 59 | + startup_prog = paddle.static.Program() |
| 60 | + with paddle.static.program_guard(main_prog, startup_prog): |
| 61 | + input_name_1 = 'input_1' |
| 62 | + input_name_2 = 'input_2' |
| 63 | + input_var_1 = paddle.static.data( |
| 64 | + name=input_name_1, |
| 65 | + shape=self.input_shape_1, |
| 66 | + dtype=self.input_dtype_1, |
| 67 | + ) |
| 68 | + input_var_2 = paddle.static.data( |
| 69 | + name=input_name_2, |
| 70 | + shape=self.input_shape_2, |
| 71 | + dtype=self.input_dtype_2, |
| 72 | + ) |
| 73 | + res = self.op_static(input_var_1, input_var_2) |
| 74 | + exe = base.Executor(place) |
| 75 | + fetches = exe.run( |
| 76 | + main_prog, |
| 77 | + feed={ |
| 78 | + input_name_1: self.input_np_1, |
| 79 | + input_name_2: self.input_np_2, |
| 80 | + }, |
| 81 | + fetch_list=[res], |
| 82 | + ) |
| 83 | + self.assertEqual(fetches[0].dtype, np.dtype(self.input_dtype_2)) |
| 84 | + |
| 85 | + def test_static(self): |
| 86 | + for place in self.places: |
| 87 | + self.check_static_result(place=place) |
| 88 | + |
| 89 | + def check_dygraph_result(self, place): |
| 90 | + with base.dygraph.guard(place): |
| 91 | + input_1 = paddle.to_tensor(self.input_np_1) |
| 92 | + input_2 = paddle.to_tensor(self.input_np_2) |
| 93 | + result = self.op_dygraph(input_1, input_2) |
| 94 | + self.assertEqual(result.dtype, input_2.dtype) |
| 95 | + |
| 96 | + def test_dygraph(self): |
| 97 | + for place in self.places: |
| 98 | + self.check_dygraph_result(place=place) |
| 99 | + |
| 100 | + |
| 101 | +class TestTypeAsFloat32ToFloat16(TestTypeAsBase): |
| 102 | + def setUp(self): |
| 103 | + self.input_dtype_1 = "float32" |
| 104 | + self.input_dtype_2 = "float16" |
| 105 | + super().setUp() |
| 106 | + |
| 107 | + |
| 108 | +class TestTypeAsFloat64ToFloat32(TestTypeAsBase): |
| 109 | + def setUp(self): |
| 110 | + self.input_dtype_1 = "float64" |
| 111 | + self.input_dtype_2 = "float32" |
| 112 | + super().setUp() |
| 113 | + |
| 114 | + |
| 115 | +class TestTypeAsInt32ToInt64(TestTypeAsBase): |
| 116 | + def setUp(self): |
| 117 | + self.input_dtype_1 = "int32" |
| 118 | + self.input_dtype_2 = "int64" |
| 119 | + super().setUp() |
| 120 | + |
| 121 | + |
| 122 | +class TestTypeAsInt32ToFloat32(TestTypeAsBase): |
| 123 | + def setUp(self): |
| 124 | + self.input_dtype_1 = "int32" |
| 125 | + self.input_dtype_2 = "float32" |
| 126 | + super().setUp() |
| 127 | + |
| 128 | + |
| 129 | +class TestTypeAsFloat32ToInt64(TestTypeAsBase): |
| 130 | + def setUp(self): |
| 131 | + self.input_dtype_1 = "float32" |
| 132 | + self.input_dtype_2 = "int64" |
| 133 | + super().setUp() |
| 134 | + |
| 135 | + |
| 136 | +class TestTypeAsInt8ToFloat64(TestTypeAsBase): |
| 137 | + def setUp(self): |
| 138 | + self.input_dtype_1 = "int8" |
| 139 | + self.input_dtype_2 = "float64" |
| 140 | + self.input_shape = (4, 2) |
| 141 | + super().setUp() |
| 142 | + |
| 143 | + |
| 144 | +class TestTypeAsUInt8ToInt32(TestTypeAsBase): |
| 145 | + def setUp(self): |
| 146 | + self.input_dtype_1 = "uint8" |
| 147 | + self.input_dtype_2 = "int32" |
| 148 | + self.input_shape = (3, 3) |
| 149 | + super().setUp() |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + unittest.main() |
0 commit comments