已合并
fix test_autograd to avoid calling policy_fn during recompute #44660
SCh_zx创建于 11 天前
fix test_autograd to avoid calling policy_fn during recompute #44660
已合并
共 2 个文件变更+29-22
| @@ -41,7 +41,7 @@ from torch.testing import make_tensor | |||
| 41 | from torch.testing._internal.common_utils import ( | 41 | from torch.testing._internal.common_utils import ( |
| 42 | TestCase, run_tests, skipIfNoLapack, slowTest, IS_WINDOWS, IS_MACOS, | 42 | TestCase, run_tests, skipIfNoLapack, slowTest, IS_WINDOWS, IS_MACOS, |
| 43 | disable_gc, gradcheck, gradgradcheck, parametrize, | 43 | disable_gc, gradcheck, gradgradcheck, parametrize, |
| 44 | - instantiate_parametrized_tests, skipIfMps, set_warn_always_context, TEST_PRIVATEUSE1, | 44 | + instantiate_parametrized_tests, skipIfMPS, set_warn_always_context, TEST_PRIVATEUSE1, |
| 45 | skipIfTorchDynamo, xfailIfTorchDynamo) | 45 | skipIfTorchDynamo, xfailIfTorchDynamo) |
| 46 | from torch.autograd import Variable, Function, detect_anomaly, kineto_available, _calculate_shape | 46 | from torch.autograd import Variable, Function, detect_anomaly, kineto_available, _calculate_shape |
| 47 | from torch.autograd.function import InplaceFunction | 47 | from torch.autograd.function import InplaceFunction |
| @@ -4321,11 +4321,18 @@ SinBackward0, MulBackward0, torch::autograd::AccumulateGrad | |||
| 4321 | # version counter doesn't change inside of the context manager | 4321 | # version counter doesn't change inside of the context manager |
| 4322 | self.assertEqual(2, x._version) | 4322 | self.assertEqual(2, x._version) |
| 4323 | 4323 | ||
| 4324 | - torch._C._autograd._unsafe_set_version_counter(x, 0) | 4324 | + torch._C._autograd._unsafe_set_version_counter((x,), (0,)) |
| 4325 | self.assertEqual(0, x._version) | 4325 | self.assertEqual(0, x._version) |
| 4326 | with self.assertRaisesRegex(RuntimeError, "Cannot set"): | 4326 | with self.assertRaisesRegex(RuntimeError, "Cannot set"): |
| 4327 | - torch._C._autograd._unsafe_set_version_counter(x, -1) | 4327 | + torch._C._autograd._unsafe_set_version_counter((x,), (-1,)) |
| 4328 | 4328 | ||
| 4329 | + y = torch.ones(2, requires_grad=True).clone() | ||
| 4330 | + with torch.autograd._unsafe_preserve_version_counter((x, y)): | ||
| 4331 | + x.mul_(2) | ||
| 4332 | + y.mul_(3) | ||
| 4333 | + # version counter doesn't change inside of the context manager | ||
| 4334 | + self.assertEqual(0, x._version) | ||
| 4335 | + self.assertEqual(0, y._version) | ||
| 4329 | 4336 | ||
| 4330 | def test_current_node(self): | 4337 | def test_current_node(self): |
| 4331 | pr = [] | 4338 | pr = [] |
| @@ -10119,7 +10126,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10119 | with self.assertRaisesRegex(RuntimeError, "Double backward is unsupported for"): | 10126 | with self.assertRaisesRegex(RuntimeError, "Double backward is unsupported for"): |
| 10120 | gradgradcheck(fn, (input_, 0, idx, src, 'prod')) | 10127 | gradgradcheck(fn, (input_, 0, idx, src, 'prod')) |
| 10121 | 10128 | ||
| 10122 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10129 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10123 | def test_parameter_resize(self, device): | 10130 | def test_parameter_resize(self, device): |
| 10124 | asd = torch.nn.Parameter(torch.ones(16, dtype=torch.double, device=device)) | 10131 | asd = torch.nn.Parameter(torch.ones(16, dtype=torch.double, device=device)) |
| 10125 | 10132 | ||
| @@ -10131,7 +10138,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10131 | m = torch.cat((asd, asd)) | 10138 | m = torch.cat((asd, asd)) |
| 10132 | m.sum().backward() | 10139 | m.sum().backward() |
| 10133 | 10140 | ||
| 10134 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10141 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10135 | 10142 | ||
| 10136 | def test_sparse_ctor_getter_backward(self, device, dtype): | 10143 | def test_sparse_ctor_getter_backward(self, device, dtype): |
| 10137 | # See NOTE [ Sparse: autograd and API ] on the expected behavior of this test | 10144 | # See NOTE [ Sparse: autograd and API ] on the expected behavior of this test |
| @@ -10166,7 +10173,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10166 | _test(sparse_size + dense_size, len(sparse_size), nnz, device) | 10173 | _test(sparse_size + dense_size, len(sparse_size), nnz, device) |
| 10167 | 10174 | ||
| 10168 | 10175 | ||
| 10169 | - @skipIfMps | 10176 | + @skipIfMPS |
| 10170 | 10177 | ||
| 10171 | def test_sparse_backward(self, device, dtype): | 10178 | def test_sparse_backward(self, device, dtype): |
| 10172 | class FixedGradientFunction(Function): | 10179 | class FixedGradientFunction(Function): |
| @@ -10209,7 +10216,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10209 | (fn.apply(x, sparse_grad1) + fn.apply(x, sparse_grad2)).sum().abs().backward() | 10216 | (fn.apply(x, sparse_grad1) + fn.apply(x, sparse_grad2)).sum().abs().backward() |
| 10210 | self.assertEqual(x.grad, sparse_grad1 + sparse_grad2) | 10217 | self.assertEqual(x.grad, sparse_grad1 + sparse_grad2) |
| 10211 | 10218 | ||
| 10212 | - @skipIfMps | 10219 | + @skipIfMPS |
| 10213 | def test_sparse_mask_autograd(self, device): | 10220 | def test_sparse_mask_autograd(self, device): |
| 10214 | tensor = torch.randn(3, requires_grad=True, device=device) | 10221 | tensor = torch.randn(3, requires_grad=True, device=device) |
| 10215 | mask = torch.ones(3, device=device) | 10222 | mask = torch.ones(3, device=device) |
| @@ -10219,7 +10226,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10219 | converted.sum().backward() | 10226 | converted.sum().backward() |
| 10220 | self.assertEqual(tensor.grad, mask.to_dense()) | 10227 | self.assertEqual(tensor.grad, mask.to_dense()) |
| 10221 | 10228 | ||
| 10222 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10229 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10223 | def test_pyscalar_conversions(self, device): | 10230 | def test_pyscalar_conversions(self, device): |
| 10224 | def _test_pyscalar_conversions(t, integral_conv): | 10231 | def _test_pyscalar_conversions(t, integral_conv): |
| 10225 | # integral -> integral | 10232 | # integral -> integral |
| @@ -10378,7 +10385,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10378 | 10385 | ||
| 10379 | self.assertEqual(before, after) | 10386 | self.assertEqual(before, after) |
| 10380 | 10387 | ||
| 10381 | - @skipIfMps # the test doesn't work on MPS | 10388 | + @skipIfMPS # the test doesn't work on MPS |
| 10382 | def test_where_functional(self, device): | 10389 | def test_where_functional(self, device): |
| 10383 | x = torch.randn(5, 5, dtype=torch.double, device=device, requires_grad=True) | 10390 | x = torch.randn(5, 5, dtype=torch.double, device=device, requires_grad=True) |
| 10384 | y = torch.randn(5, 5, dtype=torch.double, device=device, requires_grad=True) | 10391 | y = torch.randn(5, 5, dtype=torch.double, device=device, requires_grad=True) |
| @@ -10395,7 +10402,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10395 | gradcheck(where, [cond, x, y], raise_exception=True) | 10402 | gradcheck(where, [cond, x, y], raise_exception=True) |
| 10396 | gradgradcheck(where, [cond, x, y], [torch.randn(5, 5, 5, device=device)]) | 10403 | gradgradcheck(where, [cond, x, y], [torch.randn(5, 5, 5, device=device)]) |
| 10397 | 10404 | ||
| 10398 | - @skipIfMps # the test doesn't work on MPS | 10405 | + @skipIfMPS # the test doesn't work on MPS |
| 10399 | def test_where_scalar(self, device): | 10406 | def test_where_scalar(self, device): |
| 10400 | x = torch.randn(5, 5, dtype=torch.double, device=device, requires_grad=True) | 10407 | x = torch.randn(5, 5, dtype=torch.double, device=device, requires_grad=True) |
| 10401 | scalar = 4. | 10408 | scalar = 4. |
| @@ -10472,7 +10479,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10472 | with emit_itt(): | 10479 | with emit_itt(): |
| 10473 | a.add(1.0) | 10480 | a.add(1.0) |
| 10474 | 10481 | ||
| 10475 | - @skipIfMps # the test doesn't work as randn is not supported with type long | 10482 | + @skipIfMPS # the test doesn't work as randn is not supported with type long |
| 10476 | 10483 | ||
| 10477 | def test_grad_assignment(self, devices): | 10484 | def test_grad_assignment(self, devices): |
| 10478 | x = torch.randn(5, 5, device=devices[0]) | 10485 | x = torch.randn(5, 5, device=devices[0]) |
| @@ -10695,7 +10702,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10695 | x.sum().backward() | 10702 | x.sum().backward() |
| 10696 | self.assertEqual(root.grad.tolist(), [[1, 2], [1, 1]]) | 10703 | self.assertEqual(root.grad.tolist(), [[1, 2], [1, 1]]) |
| 10697 | 10704 | ||
| 10698 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10705 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10699 | def test_inplace_on_view_then_no_grad(self, device): | 10706 | def test_inplace_on_view_then_no_grad(self, device): |
| 10700 | # Perform an in-place operation on a view of a non-leaf variable. | 10707 | # Perform an in-place operation on a view of a non-leaf variable. |
| 10701 | a = torch.ones(3, 1, dtype=torch.double, device=device, requires_grad=True) | 10708 | a = torch.ones(3, 1, dtype=torch.double, device=device, requires_grad=True) |
| @@ -10709,7 +10716,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10709 | 10716 | ||
| 10710 | c.sum().backward() | 10717 | c.sum().backward() |
| 10711 | 10718 | ||
| 10712 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10719 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10713 | def test_inplace_on_view_gradcheck(self, device): | 10720 | def test_inplace_on_view_gradcheck(self, device): |
| 10714 | # gradcheck modifications to views | 10721 | # gradcheck modifications to views |
| 10715 | a = torch.randn(4, 4, dtype=torch.double, device=device, requires_grad=True) | 10722 | a = torch.randn(4, 4, dtype=torch.double, device=device, requires_grad=True) |
| @@ -10732,7 +10739,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10732 | with self.assertRaises(RuntimeError): | 10739 | with self.assertRaises(RuntimeError): |
| 10733 | v1[0].mul_(2) | 10740 | v1[0].mul_(2) |
| 10734 | 10741 | ||
| 10735 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10742 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10736 | def test_inplace_on_view_of_multiple_output_view(self, device): | 10743 | def test_inplace_on_view_of_multiple_output_view(self, device): |
| 10737 | a = torch.rand(10, dtype=torch.double, device=device, requires_grad=True).clone() | 10744 | a = torch.rand(10, dtype=torch.double, device=device, requires_grad=True).clone() |
| 10738 | b = a.unbind(0) | 10745 | b = a.unbind(0) |
| @@ -10740,7 +10747,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10740 | with self.assertRaises(RuntimeError): | 10747 | with self.assertRaises(RuntimeError): |
| 10741 | c.mul_(2) | 10748 | c.mul_(2) |
| 10742 | 10749 | ||
| 10743 | - @skipIfMps # MPS backend doesn't support double types | 10750 | + @skipIfMPS # MPS backend doesn't support double types |
| 10744 | def test_inplace_multiple_output_view_of_view(self, device): | 10751 | def test_inplace_multiple_output_view_of_view(self, device): |
| 10745 | a = torch.rand(10, dtype=torch.double, device=device, requires_grad=True).clone() | 10752 | a = torch.rand(10, dtype=torch.double, device=device, requires_grad=True).clone() |
| 10746 | b = a.view_as(a) | 10753 | b = a.view_as(a) |
| @@ -10748,7 +10755,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10748 | with self.assertRaises(RuntimeError): | 10755 | with self.assertRaises(RuntimeError): |
| 10749 | c[0].mul_(2) | 10756 | c[0].mul_(2) |
| 10750 | 10757 | ||
| 10751 | - @skipIfMps # MPS backend doesn't support double types | 10758 | + @skipIfMPS # MPS backend doesn't support double types |
| 10752 | def test_inplace_on_view_makes_base_require_grad(self, device): | 10759 | def test_inplace_on_view_makes_base_require_grad(self, device): |
| 10753 | # in-place modification to view makes base require grad | 10760 | # in-place modification to view makes base require grad |
| 10754 | a = torch.randn(4, 4, dtype=torch.double, device=device, requires_grad=False) | 10761 | a = torch.randn(4, 4, dtype=torch.double, device=device, requires_grad=False) |
| @@ -10774,7 +10781,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10774 | self.assertEqual(b.grad.tolist(), [5]) | 10781 | self.assertEqual(b.grad.tolist(), [5]) |
| 10775 | self.assertIsNone(a.grad) | 10782 | self.assertIsNone(a.grad) |
| 10776 | 10783 | ||
| 10777 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10784 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10778 | def test_inplace_on_view_modify_base(self, device): | 10785 | def test_inplace_on_view_modify_base(self, device): |
| 10779 | # Test that an in-place operation on a base that forced it to require | 10786 | # Test that an in-place operation on a base that forced it to require |
| 10780 | # grad also forces any previous views to require grad and backprop | 10787 | # grad also forces any previous views to require grad and backprop |
| @@ -10793,7 +10800,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10793 | gradcheck(fn, [r]) | 10800 | gradcheck(fn, [r]) |
| 10794 | gradgradcheck(fn, [r]) | 10801 | gradgradcheck(fn, [r]) |
| 10795 | 10802 | ||
| 10796 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10803 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10797 | def test_inplace_on_view_python(self, device): | 10804 | def test_inplace_on_view_python(self, device): |
| 10798 | # in-place modifications of Python-autograd created view | 10805 | # in-place modifications of Python-autograd created view |
| 10799 | a = torch.randn(4, 4, dtype=torch.double, device=device, requires_grad=True) | 10806 | a = torch.randn(4, 4, dtype=torch.double, device=device, requires_grad=True) |
| @@ -10871,7 +10878,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10871 | self.assertIsNone(b.grad) | 10878 | self.assertIsNone(b.grad) |
| 10872 | self.assertEqual(a.grad.item(), 2) | 10879 | self.assertEqual(a.grad.item(), 2) |
| 10873 | 10880 | ||
| 10874 | - @skipIfMps # the test doesn't work on MPS as double types are not supported | 10881 | + @skipIfMPS # the test doesn't work on MPS as double types are not supported |
| 10875 | def test_mv_grad_stride_0(self, device): | 10882 | def test_mv_grad_stride_0(self, device): |
| 10876 | mat = torch.randn(2, 2, dtype=torch.double, device=device) | 10883 | mat = torch.randn(2, 2, dtype=torch.double, device=device) |
| 10877 | vec = torch.randn(1, dtype=torch.double, device=device).requires_grad_(True) | 10884 | vec = torch.randn(1, dtype=torch.double, device=device).requires_grad_(True) |
| @@ -10925,7 +10932,7 @@ class TestAutogradDeviceType(TestCase): | |||
| 10925 | (c * d).sum().backward() | 10932 | (c * d).sum().backward() |
| 10926 | self.assertEqual(c.grad.stride(), (2, 1)) | 10933 | self.assertEqual(c.grad.stride(), (2, 1)) |
| 10927 | 10934 | ||
| 10928 | - @skipIfMps | 10935 | + @skipIfMPS |
| 10929 | def test_copy_r_to_c(self, device): | 10936 | def test_copy_r_to_c(self, device): |
| 10930 | out_c = torch.empty(3, 2, dtype=torch.cdouble, device=device) | 10937 | out_c = torch.empty(3, 2, dtype=torch.cdouble, device=device) |
| 10931 | inp_r = torch.randn(3, 2, dtype=torch.double, device=device, | 10938 | inp_r = torch.randn(3, 2, dtype=torch.double, device=device, |
| @@ -235,11 +235,11 @@ | |||
| 235 | "test_sparse_backward_npu_complex128 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 235 | "test_sparse_backward_npu_complex128 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 236 | "test_sparse_backward_npu_float64 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 236 | "test_sparse_backward_npu_float64 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 237 | "test_sparse_ctor_getter_backward_npu_complex128 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 237 | "test_sparse_ctor_getter_backward_npu_complex128 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 238 | - "test_sparse_ctor_getter_backward_privateuse1_float64 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 238 | + "test_sparse_ctor_getter_backward_npu_float64 (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 239 | "test_sparse_mask_autograd_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 239 | "test_sparse_mask_autograd_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 240 | "test_strided_leaf_grad_layout_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 240 | "test_strided_leaf_grad_layout_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 241 | "test_to_r_to_c_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 241 | "test_to_r_to_c_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 242 | - "test_view_copy_privateuse1 (__main__.TestAutogradMultipleDispatchPRIVATEUSE1)": ["", [""]], | 242 | + "test_view_copy_npu (__main__.TestAutogradMultipleDispatchPRIVATEUSE1)": ["", [""]], |
| 243 | "test_where_functional_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 243 | "test_where_functional_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 244 | "test_where_scalar_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], | 244 | "test_where_scalar_npu (__main__.TestAutogradDeviceTypePRIVATEUSE1)": ["", [""]], |
| 245 | "test_view_clone_view_inplace (__main__.TestCrossRefFunctionalization)": ["", [""]], | 245 | "test_view_clone_view_inplace (__main__.TestCrossRefFunctionalization)": ["", [""]], |