已合并
test update ut #37775
huangyunlong创建于 6月6日
test update ut #37775
已合并
共 6 个文件变更+30-22
| @@ -1,3 +1,4 @@ | |||
| 1 | +import os | ||
| 1 | import unittest | 2 | import unittest |
| 2 | import torch | 3 | import torch |
| 3 | import torch_npu | 4 | import torch_npu |
| @@ -5,9 +6,13 @@ import torch_npu | |||
| 5 | from torch_npu.testing.testcase import TestCase, run_tests | 6 | from torch_npu.testing.testcase import TestCase, run_tests |
| 6 | 7 | ||
| 7 | 8 | ||
| 9 | +# for A2/A3 | ||
| 10 | +os.environ["INF_NAN_MODE_FORCE_DISABLE"] = "1" | ||
| 11 | +os.environ["INF_NAN_MODE_ENABLE"] = "1" | ||
| 12 | + | ||
| 13 | + | ||
| 8 | class TestFloatStatus(TestCase): | 14 | class TestFloatStatus(TestCase): |
| 9 | 15 | ||
| 10 | - | ||
| 11 | def test_float_status(self, device="npu"): | 16 | def test_float_status(self, device="npu"): |
| 12 | float_tensor = torch.tensor([40000.0], dtype=torch.float16).npu() | 17 | float_tensor = torch.tensor([40000.0], dtype=torch.float16).npu() |
| 13 | float_tensor = float_tensor + float_tensor | 18 | float_tensor = float_tensor + float_tensor |
| @@ -441,7 +441,11 @@ class TestNNInit(TestCase): | |||
| 441 | def fn(): | 441 | def fn(): |
| 442 | init.normal(x) | 442 | init.normal(x) |
| 443 | 443 | ||
| 444 | - with self.assertWarnsRegex(UserWarning, 'deprecated', msg='methods not suffixed with underscore should be deprecated'): | 444 | + with self.assertWarnsRegex( |
| 445 | + FutureWarning, | ||
| 446 | + "deprecated", | ||
| 447 | + msg="methods not suffixed with underscore should be deprecated", | ||
| 448 | + ): | ||
| 445 | fn() | 449 | fn() |
| 446 | 450 | ||
| 447 | 451 | ||
| @@ -10,7 +10,7 @@ import torch_npu | |||
| 10 | import torch_npu.testing | 10 | import torch_npu.testing |
| 11 | import torch.nn.functional as F | 11 | import torch.nn.functional as F |
| 12 | from torch.nn import Parameter | 12 | from torch.nn import Parameter |
| 13 | -from torch.optim import Adam, SGD | 13 | +from torch.optim import Adam, Rprop, SGD |
| 14 | from torch.optim.lr_scheduler import ( | 14 | from torch.optim.lr_scheduler import ( |
| 15 | LambdaLR, | 15 | LambdaLR, |
| 16 | MultiplicativeLR, | 16 | MultiplicativeLR, |
| @@ -1503,9 +1503,9 @@ class TestLRScheduler(TestCase): | |||
| 1503 | self.opt = old_opt # set optimizer back to SGD | 1503 | self.opt = old_opt # set optimizer back to SGD |
| 1504 | 1504 | ||
| 1505 | def test_cycle_lr_cycle_momentum_fail_with_momentumless_optimizer(self): | 1505 | def test_cycle_lr_cycle_momentum_fail_with_momentumless_optimizer(self): |
| 1506 | - with self.assertRaises(ValueError): | 1506 | + with self.assertRaises(ValueError): |
| 1507 | - adam_opt = Adam(self.net.parameters()) | 1507 | + rprop_opt = Rprop(self.net.parameters()) |
| 1508 | - scheduler = CyclicLR(adam_opt, base_lr=1, max_lr=5, cycle_momentum=True) | 1508 | + scheduler = CyclicLR(rprop_opt, base_lr=1, max_lr=5, cycle_momentum=True) |
| 1509 | 1509 | ||
| 1510 | def test_cycle_lr_removed_after_out_of_scope(self): | 1510 | def test_cycle_lr_removed_after_out_of_scope(self): |
| 1511 | import gc | 1511 | import gc |
| @@ -338,14 +338,13 @@ class TestFlopCounter(TestCase): | |||
| 338 | model = torch.nn.Linear(100, 100) | 338 | model = torch.nn.Linear(100, 100) |
| 339 | x = torch.randn(3, 100) | 339 | x = torch.randn(3, 100) |
| 340 | 340 | ||
| 341 | - flop_counter = FlopCounterMode(model) | 341 | + with FlopCounterMode() as mode: |
| 342 | - with flop_counter: | 342 | + self.assertEqual(len(torch.nn.modules.module._global_forward_pre_hooks), 1) |
| 343 | - self.assertEqual(len(model._forward_pre_hooks), 1) | 343 | + self.assertEqual(len(torch.nn.modules.module._global_forward_hooks), 1) |
| 344 | - self.assertEqual(len(model._forward_hooks), 1) | ||
| 345 | model(x).sum().backward() | 344 | model(x).sum().backward() |
| 346 | 345 | ||
| 347 | - self.assertEqual(len(model._forward_pre_hooks), 0) | 346 | + self.assertEqual(len(torch.nn.modules.module._global_forward_pre_hooks), 0) |
| 348 | - self.assertEqual(len(model._forward_hooks), 0) | 347 | + self.assertEqual(len(torch.nn.modules.module._global_forward_hooks), 0) |
| 349 | 348 | ||
| 350 | def test_pytrees(self): | 349 | def test_pytrees(self): |
| 351 | class Foo(torch.nn.Module): | 350 | class Foo(torch.nn.Module): |
| @@ -11,6 +11,12 @@ os.environ["COMBINED_ENABLE"] = "1" # Open combined-view cases optimization | |||
| 11 | 11 | ||
| 12 | # Optimized view Ops contains Transpose, permute, narrow, strideslice, select, unfold | 12 | # Optimized view Ops contains Transpose, permute, narrow, strideslice, select, unfold |
| 13 | 13 | ||
| 14 | +# The test case is a continuous optimization test case for aclop | ||
| 15 | +# By default, we will use aclnn in the following steps. To ensure the test case passes, we have added the path to aclnn | ||
| 16 | +# If the primary function is to maintain the original functionality, you can configure jit_compile and internal formats | ||
| 17 | +# torch_npu.npu.set_compile_mode(jit_compile=True) | ||
| 18 | +# torch.npu.config.allow_internal_format = True | ||
| 19 | + | ||
| 14 | 20 | ||
| 15 | class SingleViewCopyToContiguous(TestCase): | 21 | class SingleViewCopyToContiguous(TestCase): |
| 16 | def test_view_copy(self, device="npu"): | 22 | def test_view_copy(self, device="npu"): |
| @@ -173,7 +179,6 @@ class SingleViewCopyToContiguous(TestCase): | |||
| 173 | cpu_out2 = cpu_input[1:10, :, :].clone() | 179 | cpu_out2 = cpu_input[1:10, :, :].clone() |
| 174 | self.assertRtolEqual(npu_out2.to("cpu").numpy(), cpu_out2.numpy()) | 180 | self.assertRtolEqual(npu_out2.to("cpu").numpy(), cpu_out2.numpy()) |
| 175 | 181 | ||
| 176 | - | ||
| 177 | def test_select_at_first_axis_to_single_element_tensor_copy(self, device="npu"): | 182 | def test_select_at_first_axis_to_single_element_tensor_copy(self, device="npu"): |
| 178 | dtype_list5 = [torch.float32] | 183 | dtype_list5 = [torch.float32] |
| 179 | format_list5 = [2, 3, 29] | 184 | format_list5 = [2, 3, 29] |
| @@ -203,13 +208,13 @@ class SingleViewCopyToContiguous(TestCase): | |||
| 203 | npu_out2 = npu_input[0] + 1 | 208 | npu_out2 = npu_input[0] + 1 |
| 204 | if match_case: | 209 | if match_case: |
| 205 | self.assertEqual(check_operators_in_prof(['contiguous_h_memRepoint'], prof) or | 210 | self.assertEqual(check_operators_in_prof(['contiguous_h_memRepoint'], prof) or |
| 206 | - check_operators_in_prof(['aclnnInplaceCopy'], prof), | 211 | + check_operators_in_prof(['aclnnAdds'], prof), |
| 207 | - True, message="contiguous_h_memRepoint or aclnnInplaceCopy is not called!") | 212 | + True, message="contiguous_h_memRepoint or aclnnAdds is not called!") |
| 208 | else: | 213 | else: |
| 209 | # refresh storage desc after transdata | 214 | # refresh storage desc after transdata |
| 210 | self.assertEqual(check_operators_in_prof(['Identity'], prof) or | 215 | self.assertEqual(check_operators_in_prof(['Identity'], prof) or |
| 211 | - check_operators_in_prof(['aclnnInplaceCopy'], prof), | 216 | + check_operators_in_prof(['aclnnAdds'], prof), |
| 212 | - True, message="Identity or aclnnInplaceCopy is not called!") | 217 | + True, message="Identity or aclnnAdds is not called!") |
| 213 | cpu_out2 = cpu_input[0] + 1 | 218 | cpu_out2 = cpu_input[0] + 1 |
| 214 | self.assertRtolEqual(npu_out2.to("cpu").numpy(), cpu_out2.numpy()) | 219 | self.assertRtolEqual(npu_out2.to("cpu").numpy(), cpu_out2.numpy()) |
| 215 | 220 | ||
| @@ -31107,7 +31107,6 @@ | |||
| 31107 | "test_forward_mode_AD_abs_npu_float64 (__main__.TestFwdGradientsPRIVATEUSE1)": ["", ["A2"]], | 31107 | "test_forward_mode_AD_abs_npu_float64 (__main__.TestFwdGradientsPRIVATEUSE1)": ["", ["A2"]], |
| 31108 | "test_flatten_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], | 31108 | "test_flatten_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], |
| 31109 | "test_narrow_at_first_axis_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], | 31109 | "test_narrow_at_first_axis_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], |
| 31110 | - "test_select_at_first_axis_to_single_element_tensor_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], | ||
| 31111 | "test_unsqueeze_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], | 31110 | "test_unsqueeze_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], |
| 31112 | "test_view_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], | 31111 | "test_view_copy (__main__.SingleViewCopyToContiguous)": ["", ["A2"]], |
| 31113 | "test_view_narrow_permute_copy_contiguous (__main__.TestTriCombinedViewsCopyToContiguous)": ["", ["A2"]], | 31112 | "test_view_narrow_permute_copy_contiguous (__main__.TestTriCombinedViewsCopyToContiguous)": ["", ["A2"]], |
| @@ -31140,7 +31139,6 @@ | |||
| 31140 | "test_npu_deform_conv_2 (__main__.TestDeformConv)": ["", ["A2"]], | 31139 | "test_npu_deform_conv_2 (__main__.TestDeformConv)": ["", ["A2"]], |
| 31141 | "test_grad_scaling_unscale_float16 (__main__.TestAmpForeachNonFiniteCheckAndUnscale)": ["", ["A2"]], | 31140 | "test_grad_scaling_unscale_float16 (__main__.TestAmpForeachNonFiniteCheckAndUnscale)": ["", ["A2"]], |
| 31142 | "test_grad_scaling_unscale_float32 (__main__.TestAmpForeachNonFiniteCheckAndUnscale)": ["", ["A2"]], | 31141 | "test_grad_scaling_unscale_float32 (__main__.TestAmpForeachNonFiniteCheckAndUnscale)": ["", ["A2"]], |
| 31143 | - "test_float_status (__main__.TestFloatStatus)": ["", ["A2"]], | ||
| 31144 | "test_npu_fused_attention_score_fwd (__main__.TestFusedAttentionScoreFwd)": ["", ["A2"]], | 31142 | "test_npu_fused_attention_score_fwd (__main__.TestFusedAttentionScoreFwd)": ["", ["A2"]], |
| 31145 | "test_save_graph_repro (__main__.TestAfterAot)": ["", ["A2"]], | 31143 | "test_save_graph_repro (__main__.TestAfterAot)": ["", ["A2"]], |
| 31146 | "test_explain_with_backend (__main__.TestExplainWithBackend)": ["", ["A2"]], | 31144 | "test_explain_with_backend (__main__.TestExplainWithBackend)": ["", ["A2"]], |
| @@ -31985,7 +31983,6 @@ | |||
| 31985 | "test_sym_sqrt (__main__.TestPySymInt)": ["", [""]], | 31983 | "test_sym_sqrt (__main__.TestPySymInt)": ["", [""]], |
| 31986 | "test_unbacked_substitution (__main__.TestPySymInt)": ["", [""]], | 31984 | "test_unbacked_substitution (__main__.TestPySymInt)": ["", [""]], |
| 31987 | "test_symnode_hashing (__main__.TestSymNumberMagicMethods)": ["", [""]], | 31985 | "test_symnode_hashing (__main__.TestSymNumberMagicMethods)": ["", [""]], |
| 31988 | - "test_hook_registration (__main__.TestFlopCounter)": ["", [""]], | ||
| 31989 | "test_batch_norm (__main__.TestCrossRefFunctionalization)": ["", [""]], | 31986 | "test_batch_norm (__main__.TestCrossRefFunctionalization)": ["", [""]], |
| 31990 | "test_cat (__main__.TestCrossRefFunctionalization)": ["", [""]], | 31987 | "test_cat (__main__.TestCrossRefFunctionalization)": ["", [""]], |
| 31991 | "test_index_mutation_on_non_input (__main__.TestCrossRefFunctionalization)": ["", [""]], | 31988 | "test_index_mutation_on_non_input (__main__.TestCrossRefFunctionalization)": ["", [""]], |
| @@ -32005,7 +32002,6 @@ | |||
| 32005 | "test_multidevice_serialization_npu (__main__.TestDevicePrecisionPRIVATEUSE1)": ["", [""]], | 32002 | "test_multidevice_serialization_npu (__main__.TestDevicePrecisionPRIVATEUSE1)": ["", [""]], |
| 32006 | "test_cdist_grad_p_lt_1_no_nan_npu (__main__.TestTorchDeviceTypePRIVATEUSE1)": ["", [""]], | 32003 | "test_cdist_grad_p_lt_1_no_nan_npu (__main__.TestTorchDeviceTypePRIVATEUSE1)": ["", [""]], |
| 32007 | "test_serialization_npu (__main__.TestTorchDeviceTypePRIVATEUSE1)": ["", [""]], | 32004 | "test_serialization_npu (__main__.TestTorchDeviceTypePRIVATEUSE1)": ["", [""]], |
| 32008 | - "test_deprecation (__main__.TestNNInit)": ["", [""]], | ||
| 32009 | "test_fake_tensor_mode_simple_invalid_input (__main__.TestFxToOnnx)": ["", [""]], | 32005 | "test_fake_tensor_mode_simple_invalid_input (__main__.TestFxToOnnx)": ["", [""]], |
| 32010 | "test_missing_complex_onnx_variant_raises_errors_in_dispatcher (__main__.TestFxToOnnx)": ["", [""]], | 32006 | "test_missing_complex_onnx_variant_raises_errors_in_dispatcher (__main__.TestFxToOnnx)": ["", [""]], |
| 32011 | "test_no_graph_break_on_item (__main__.SubGraphTests)": ["", [""]], | 32007 | "test_no_graph_break_on_item (__main__.SubGraphTests)": ["", [""]], |
| @@ -32070,7 +32066,6 @@ | |||
| 32070 | "test_matmul_unshardable (__main__.TestRegisterSharding)": ["", [""]], | 32066 | "test_matmul_unshardable (__main__.TestRegisterSharding)": ["", [""]], |
| 32071 | "test_npu_fussion_attention_forward (__main__.TestRegisterSharding)": ["", [""]], | 32067 | "test_npu_fussion_attention_forward (__main__.TestRegisterSharding)": ["", [""]], |
| 32072 | "test_npu_fussion_attention_grad (__main__.TestRegisterSharding)": ["", [""]], | 32068 | "test_npu_fussion_attention_grad (__main__.TestRegisterSharding)": ["", [""]], |
| 32073 | - "test_cycle_lr_cycle_momentum_fail_with_momentumless_optimizer (optim.test_lrscheduler.TestLRScheduler)": ["", [""]], | ||
| 32074 | "test_as_strided (__main__.TestFunctionalization)": ["", [""]], | 32069 | "test_as_strided (__main__.TestFunctionalization)": ["", [""]], |
| 32075 | "test_copy_ (__main__.TestFunctionalization)": ["", [""]], | 32070 | "test_copy_ (__main__.TestFunctionalization)": ["", [""]], |
| 32076 | "test_diagonal (__main__.TestFunctionalization)": ["", [""]], | 32071 | "test_diagonal (__main__.TestFunctionalization)": ["", [""]], |