已合并
修复API入参校验和用例适配 #36711
bellatan创建于 5月26日
修复API入参校验和用例适配 #36711
已合并
共 3 个文件变更+58-15
| @@ -1457,10 +1457,6 @@ class TestModuleHookNN(NNTestCase): | |||
| 1457 | out, _ = mod(True, inp) | 1457 | out, _ = mod(True, inp) |
| 1458 | out.sum().backward() | 1458 | out.sum().backward() |
| 1459 | 1459 | ||
| 1460 | - | ||
| 1461 | - "Skip: pre-existing error message mismatch " | ||
| 1462 | - "(BackwardHookFunctionBackward vs BackwardHookFunction) on ARM CI" | ||
| 1463 | - ) | ||
| 1464 | def test_hook_inplace(self): | 1460 | def test_hook_inplace(self): |
| 1465 | class MyModule(nn.Module): | 1461 | class MyModule(nn.Module): |
| 1466 | def forward(self, inp, do_inplace): | 1462 | def forward(self, inp, do_inplace): |
| @@ -1488,7 +1484,7 @@ class TestModuleHookNN(NNTestCase): | |||
| 1488 | self.assertEqual(hook_called[0], 1) | 1484 | self.assertEqual(hook_called[0], 1) |
| 1489 | 1485 | ||
| 1490 | # Input inplace error should throw an error | 1486 | # Input inplace error should throw an error |
| 1491 | - with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunctionBackward is " | 1487 | + with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunction(Backward)? is " |
| 1492 | "a view and is being modified inplace."): | 1488 | "a view and is being modified inplace."): |
| 1493 | mod(inp.clone(), True) | 1489 | mod(inp.clone(), True) |
| 1494 | 1490 | ||
| @@ -1497,14 +1493,14 @@ class TestModuleHookNN(NNTestCase): | |||
| 1497 | local_inp = inp.clone() | 1493 | local_inp = inp.clone() |
| 1498 | out = mod(local_inp, False) | 1494 | out = mod(local_inp, False) |
| 1499 | local_inp[0] *= 1 | 1495 | local_inp[0] *= 1 |
| 1500 | - with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunctionBackward is " | 1496 | + with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunction(Backward)? is " |
| 1501 | "a view and its base or another view"): | 1497 | "a view and its base or another view"): |
| 1502 | # Any operation involving the view will fail here | 1498 | # Any operation involving the view will fail here |
| 1503 | mod.inp + 2 | 1499 | mod.inp + 2 |
| 1504 | 1500 | ||
| 1505 | # Output inplace error should throw an error | 1501 | # Output inplace error should throw an error |
| 1506 | out = mod(inp, False) | 1502 | out = mod(inp, False) |
| 1507 | - with self.assertRaisesRegex(RuntimeError, "BackwardHookFunctionBackward is a view " | 1503 | + with self.assertRaisesRegex(RuntimeError, "BackwardHookFunction(Backward)? is a view " |
| 1508 | "and is being modified inplace."): | 1504 | "and is being modified inplace."): |
| 1509 | out += 1 | 1505 | out += 1 |
| 1510 | 1506 | ||
| @@ -140,6 +140,31 @@ class TorchNPUDeviceTestCase(TestCase): | |||
| 140 | torch.npu.reset_stream_limit(stream1) | 140 | torch.npu.reset_stream_limit(stream1) |
| 141 | self.assertEqual(ans_dict_3, torch.npu.get_stream_limit(stream1)) | 141 | self.assertEqual(ans_dict_3, torch.npu.get_stream_limit(stream1)) |
| 142 | 142 | ||
| 143 | + def test_set_device_limit_device_type_check(self): | ||
| 144 | + invalid_devices = ["0", 0.0, None, True, False] | ||
| 145 | + origin_called = getattr(torch_npu.npu.set_device_limit, "called", None) | ||
| 146 | + | ||
| 147 | + try: | ||
| 148 | + for device in invalid_devices: | ||
| 149 | + with self.subTest(device=device): | ||
| 150 | + if hasattr(torch_npu.npu.set_device_limit, "called"): | ||
| 151 | + torch_npu.npu.set_device_limit.called = False | ||
| 152 | + | ||
| 153 | + with self.assertRaisesRegex(TypeError, "device must be an int"): | ||
| 154 | + torch_npu.npu.set_device_limit(device) | ||
| 155 | + finally: | ||
| 156 | + if origin_called is not None: | ||
| 157 | + torch_npu.npu.set_device_limit.called = origin_called | ||
| 158 | + | ||
| 159 | + def test_get_device_limit_device_type_check(self): | ||
| 160 | + invalid_devices = ["0", 0.0, None, True, False] | ||
| 161 | + | ||
| 162 | + for device in invalid_devices: | ||
| 163 | + with self.subTest(device=device): | ||
| 164 | + with self.assertRaisesRegex(TypeError, "device must be an int"): | ||
| 165 | + torch_npu.npu.get_device_limit(device) | ||
| 166 | + | ||
| 167 | + | ||
| 143 | class TorchNPUMemoryApiTestCase(TestCase): | 168 | class TorchNPUMemoryApiTestCase(TestCase): |
| 144 | def test_npu_memory_stats(self): | 169 | def test_npu_memory_stats(self): |
| 145 | res = torch_npu.npu.memory_stats() | 170 | res = torch_npu.npu.memory_stats() |
| @@ -161,6 +161,12 @@ class _allowHF32Matmul: | |||
| 161 | 161 | ||
| 162 | def __setattr__(cls, name, value): | 162 | def __setattr__(cls, name, value): |
| 163 | if name == "allow_hf32": | 163 | if name == "allow_hf32": |
| 164 | + if not isinstance(value, bool): | ||
| 165 | + raise TypeError( | ||
| 166 | + "allow_hf32 must be a bool, but got {}{}".format( | ||
| 167 | + type(value).__name__, pta_error(ErrCode.TYPE) | ||
| 168 | + ) | ||
| 169 | + ) | ||
| 164 | option = {"ALLOW_MATMUL_HF32": "enable" if value else "disable"} | 170 | option = {"ALLOW_MATMUL_HF32": "enable" if value else "disable"} |
| 165 | torch_npu._C._npu_setOption(option) | 171 | torch_npu._C._npu_setOption(option) |
| 166 | elif name == "cube_math_type": | 172 | elif name == "cube_math_type": |
| @@ -185,6 +191,12 @@ class _allowHF32Conv: | |||
| 185 | 191 | ||
| 186 | def __setattr__(cls, name, value): | 192 | def __setattr__(cls, name, value): |
| 187 | if name == "allow_hf32": | 193 | if name == "allow_hf32": |
| 194 | + if not isinstance(value, bool): | ||
| 195 | + raise TypeError( | ||
| 196 | + "allow_hf32 must be a bool, but got {}{}".format( | ||
| 197 | + type(value).__name__, pta_error(ErrCode.TYPE) | ||
| 198 | + ) | ||
| 199 | + ) | ||
| 188 | option = {"ALLOW_CONV_HF32": "enable" if value else "disable"} | 200 | option = {"ALLOW_CONV_HF32": "enable" if value else "disable"} |
| 189 | torch_npu._C._npu_setOption(option) | 201 | torch_npu._C._npu_setOption(option) |
| 190 | 202 | ||
| @@ -214,24 +226,34 @@ class _call_once_class: | |||
| 214 | 226 | ||
| 215 | def set_device_limit(device, cube_num=-1, vector_num=-1): | 227 | def set_device_limit(device, cube_num=-1, vector_num=-1): |
| 216 | from torch_npu.npu import device_count | 228 | from torch_npu.npu import device_count |
| 217 | - device_id = _get_device_index(device, optional=True) | 229 | + if isinstance(device, bool) or not isinstance(device, int): |
| 218 | - if device_id < 0 or device_id >= device_count(): | 230 | + raise TypeError( |
| 231 | + "device must be an int, but got {}{}".format( | ||
| 232 | + type(device).__name__, pta_error(ErrCode.TYPE) | ||
| 233 | + ) | ||
| 234 | + ) | ||
| 235 | + if device < 0 or device >= device_count(): | ||
| 219 | raise AssertionError("Invalid device id" + pta_error(ErrCode.VALUE)) | 236 | raise AssertionError("Invalid device id" + pta_error(ErrCode.VALUE)) |
| 220 | torch_npu.npu._lazy_init() | 237 | torch_npu.npu._lazy_init() |
| 221 | if cube_num != -1: | 238 | if cube_num != -1: |
| 222 | - torch_npu._C._npu_set_device_res_limit(device_id, 0, cube_num) | 239 | + torch_npu._C._npu_set_device_res_limit(device, 0, cube_num) |
| 223 | if vector_num != -1: | 240 | if vector_num != -1: |
| 224 | - torch_npu._C._npu_set_device_res_limit(device_id, 1, vector_num) | 241 | + torch_npu._C._npu_set_device_res_limit(device, 1, vector_num) |
| 225 | 242 | ||
| 226 | 243 | ||
| 227 | def get_device_limit(device): | 244 | def get_device_limit(device): |
| 228 | from torch_npu.npu import device_count | 245 | from torch_npu.npu import device_count |
| 229 | - device_id = _get_device_index(device, optional=True) | 246 | + if isinstance(device, bool) or not isinstance(device, int): |
| 230 | - if device_id < 0 or device_id >= device_count(): | 247 | + raise TypeError( |
| 248 | + "device must be an int, but got {}{}".format( | ||
| 249 | + type(device).__name__, pta_error(ErrCode.TYPE) | ||
| 250 | + ) | ||
| 251 | + ) | ||
| 252 | + if device < 0 or device >= device_count(): | ||
| 231 | raise AssertionError("Invalid device id" + pta_error(ErrCode.VALUE)) | 253 | raise AssertionError("Invalid device id" + pta_error(ErrCode.VALUE)) |
| 232 | torch_npu.npu._lazy_init() | 254 | torch_npu.npu._lazy_init() |
| 233 | - return {"cube_core_num": torch_npu._C._npu_get_device_res_limit(device_id, 0), \ | 255 | + return {"cube_core_num": torch_npu._C._npu_get_device_res_limit(device, 0), \ |
| 234 | - "vector_core_num": torch_npu._C._npu_get_device_res_limit(device_id, 1)} | 256 | + "vector_core_num": torch_npu._C._npu_get_device_res_limit(device, 1)} |
| 235 | 257 | ||
| 236 | 258 | ||
| 237 | def set_stream_limit(stream, cube_num=-1, vector_num=-1): | 259 | def set_stream_limit(stream, cube_num=-1, vector_num=-1): |