|
17 | 17 | from tests.op_test import OpTest
|
18 | 18 |
|
19 | 19 | import numpy as np
|
20 |
| -import paddle.base as base |
21 | 20 | import paddle
|
22 | 21 |
|
23 | 22 | paddle.enable_static()
|
@@ -120,60 +119,119 @@ def test_zero_index(self):
|
120 | 119 |
|
121 | 120 |
|
122 | 121 | class TestGathertError(unittest.TestCase):
|
123 |
| - def test_error1(self): |
124 |
| - with paddle.static.program_guard( |
125 |
| - paddle.static.Program(), paddle.static.Program() |
126 |
| - ): |
127 |
| - paddle.set_device("mlu") |
128 |
| - |
129 |
| - shape = [8, 9, 6] |
130 |
| - x = paddle.static.data(shape=shape, dtype="int8", name="x") |
131 |
| - axis = paddle.static.data(shape=[1], dtype="float32", name="axis") |
132 |
| - index = paddle.static.data(shape=shape, dtype="int32", name="index") |
133 |
| - index_float = paddle.static.data( |
134 |
| - shape=shape, dtype="float32", name="index_float" |
135 |
| - ) |
136 |
| - |
137 |
| - def test_x_type(): |
138 |
| - paddle.gather(x, index) |
139 |
| - |
140 |
| - self.assertRaises(TypeError, test_x_type) |
141 |
| - |
142 |
| - def test_index_type(): |
143 |
| - paddle.gather(x, index_float) |
144 |
| - |
145 |
| - self.assertRaises(TypeError, test_index_type) |
146 |
| - |
147 |
| - def test_axis_dtype(): |
148 |
| - paddle.gather(x, index, axis=1.11) |
149 |
| - |
150 |
| - self.assertRaises(TypeError, test_axis_dtype) |
151 |
| - |
152 |
| - def test_axis_dtype1(): |
153 |
| - paddle.gather(x, index, axis=axis) |
154 |
| - |
155 |
| - self.assertRaises(TypeError, test_axis_dtype1) |
| 122 | + def setUp(self) -> None: |
| 123 | + self.place = paddle.CustomPlace("mlu", 0) |
| 124 | + paddle.set_device("mlu:0") |
156 | 125 |
|
157 |
| - def test_error2(self): |
158 |
| - with base.program_guard(base.Program(), base.Program()): |
| 126 | + def test_error1(self): |
| 127 | + paddle.enable_static() |
| 128 | + if not paddle.framework.use_pir_api(): |
| 129 | + with paddle.static.program_guard( |
| 130 | + paddle.static.Program(), paddle.static.Program() |
| 131 | + ): |
| 132 | + |
| 133 | + input_shape = [8, 9, 6] |
| 134 | + index_shape = [4] |
| 135 | + x_int8 = paddle.static.data( |
| 136 | + shape=input_shape, dtype="int8", name="x_int8" |
| 137 | + ) |
| 138 | + x_float32 = paddle.static.data( |
| 139 | + shape=input_shape, dtype="float32", name="x_float32" |
| 140 | + ) |
| 141 | + axis = paddle.static.data(shape=[1], dtype="float32", name="axis") |
| 142 | + index = paddle.static.data( |
| 143 | + shape=index_shape, dtype="int32", name="index" |
| 144 | + ) |
| 145 | + index_float = paddle.static.data( |
| 146 | + shape=index_shape, dtype="float32", name="index_float" |
| 147 | + ) |
| 148 | + |
| 149 | + def test_x_type(): |
| 150 | + paddle.gather(x_int8, index) |
| 151 | + |
| 152 | + self.assertRaises(TypeError, test_x_type) |
| 153 | + |
| 154 | + def test_index_type(): |
| 155 | + paddle.gather(x_float32, index_float) |
| 156 | + |
| 157 | + self.assertRaises(TypeError, test_index_type) |
| 158 | + |
| 159 | + def test_axis_dtype(): |
| 160 | + paddle.gather(x_float32, index, axis=1.11) |
| 161 | + |
| 162 | + self.assertRaises(TypeError, test_axis_dtype) |
| 163 | + |
| 164 | + def test_axis_dtype1(): |
| 165 | + paddle.gather(x_float32, index, axis=axis) |
| 166 | + |
| 167 | + self.assertRaises(TypeError, test_axis_dtype1) |
| 168 | + else: |
159 | 169 | paddle.set_device("mlu")
|
160 |
| - |
161 |
| - shape = [8, 9, 6] |
162 |
| - x = paddle.static.data(shape=shape, dtype="int8", name="x") |
163 |
| - index = paddle.static.data(shape=shape, dtype="int32", name="mask") |
164 |
| - index_float = paddle.static.data( |
165 |
| - shape=shape, dtype="float32", name="index_float" |
166 |
| - ) |
167 |
| - |
168 |
| - def test_x_type(): |
169 |
| - paddle.gather(x, index) |
170 |
| - |
171 |
| - self.assertRaises(TypeError, test_x_type) |
| 170 | + input_shape = [8, 9, 6] |
| 171 | + index_shape = [4] |
172 | 172 |
|
173 | 173 | def test_index_type():
|
174 |
| - paddle.gather(x, index_float) |
175 |
| - |
176 |
| - self.assertRaises(TypeError, test_index_type) |
| 174 | + with paddle.static.program_guard( |
| 175 | + paddle.static.Program(), paddle.static.Program() |
| 176 | + ): |
| 177 | + x = paddle.static.data(shape=input_shape, dtype="float32", name="x") |
| 178 | + index = paddle.static.data( |
| 179 | + shape=index_shape, dtype="float32", name="index_float" |
| 180 | + ) |
| 181 | + out = paddle.gather(x, index) |
| 182 | + exe = paddle.static.Executor(place=self.place) |
| 183 | + exe.run(paddle.static.default_startup_program()) |
| 184 | + self.assertRaises( |
| 185 | + ValueError, |
| 186 | + exe.run, |
| 187 | + paddle.static.default_main_program(), |
| 188 | + feed={ |
| 189 | + "x": np.random.random(input_shape).astype("float32"), |
| 190 | + "index_float": np.random.random(index_shape).astype( |
| 191 | + "float32" |
| 192 | + ), |
| 193 | + }, |
| 194 | + ) |
| 195 | + |
| 196 | + def test_axis_scalar_dtype(): |
| 197 | + with paddle.static.program_guard( |
| 198 | + paddle.static.Program(), paddle.static.Program() |
| 199 | + ): |
| 200 | + x = paddle.static.data(shape=input_shape, dtype="float32", name="x") |
| 201 | + index = paddle.static.data( |
| 202 | + shape=index_shape, dtype="int32", name="index" |
| 203 | + ) |
| 204 | + axis = paddle.static.data(shape=[1], dtype="int32", name="axis") |
| 205 | + self.assertRaises(TypeError, paddle.gather, x, index, axis=1.11) |
| 206 | + |
| 207 | + def test_axis_tensor_dtype(): |
| 208 | + with paddle.static.program_guard( |
| 209 | + paddle.static.Program(), paddle.static.Program() |
| 210 | + ): |
| 211 | + x = paddle.static.data(shape=input_shape, dtype="float32", name="x") |
| 212 | + index = paddle.static.data( |
| 213 | + shape=index_shape, dtype="int32", name="index" |
| 214 | + ) |
| 215 | + axis = paddle.static.data(shape=[1], dtype="float32", name="axis") |
| 216 | + y = paddle.gather(x, index, axis=axis) |
| 217 | + exe = paddle.static.Executor(place=self.place) |
| 218 | + exe.run(paddle.static.default_startup_program()) |
| 219 | + self.assertRaises( |
| 220 | + ValueError, |
| 221 | + exe.run, |
| 222 | + paddle.static.default_main_program(), |
| 223 | + feed={ |
| 224 | + "x": np.random.random(input_shape).astype("float32"), |
| 225 | + "index": np.random.randint(0, 8, index_shape).astype( |
| 226 | + "int32" |
| 227 | + ), |
| 228 | + "axis": np.array([1.11]).astype("float32"), |
| 229 | + }, |
| 230 | + ) |
| 231 | + |
| 232 | + test_index_type() |
| 233 | + test_axis_scalar_dtype() |
| 234 | + test_axis_tensor_dtype() |
177 | 235 |
|
178 | 236 |
|
179 | 237 | if __name__ == "__main__":
|
|
0 commit comments