|
| 1 | +import torch |
| 2 | + |
| 3 | +from torch.testing._internal.common_utils import TestCase, run_tests |
| 4 | + |
| 5 | +class TestCustomFunction(TestCase): |
| 6 | + def test_autograd_function_with_matmul_folding_at_output(self): |
| 7 | + """ |
| 8 | + When tensor folding occurs during matmul operation returned tensor is a view. |
| 9 | + This can cause issues when matmul is used inside a custom function |
| 10 | + and such view is then returned as output. Then it cannot be modified inplace |
| 11 | + and causes errors. |
| 12 | + It can be especially problematic when after such function inplace allreduce |
| 13 | + is performed. This test recreates this behaviour. |
| 14 | + Issue is resolved when unsafe_view is returned from matmul instead. |
| 15 | + """ |
| 16 | + |
| 17 | + class CustomFunction(torch.autograd.Function): |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def forward(ctx, inp1, inp2) -> torch.Tensor: |
| 21 | + ctx.save_for_backward(inp2) |
| 22 | + ctx.output_shape = inp1.size() |
| 23 | + return torch.matmul(inp1, inp2) |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def backward(ctx, grad_output) -> tuple[torch.Tensor, None]: |
| 27 | + output_shape = ctx.output_shape |
| 28 | + inp2, = ctx.saved_tensors |
| 29 | + return torch.mm(grad_output.squeeze(), inp2.t()).view(output_shape), None |
| 30 | + |
| 31 | + |
| 32 | + def outer_function(inp1, inp2) -> torch.Tensor: |
| 33 | + res = CustomFunction.apply(inp1, inp2) |
| 34 | + res.add_(1.0) |
| 35 | + return res.sum() |
| 36 | + |
| 37 | + def usual_function(inp1, inp2) -> torch.Tensor: |
| 38 | + res = torch.matmul(inp1, inp2) |
| 39 | + res.add_(1.0) |
| 40 | + return res.sum() |
| 41 | + |
| 42 | + |
| 43 | + inp1_custom = torch.randn(4, 1, 2, requires_grad=True) |
| 44 | + inp1_usual = inp1_custom.detach().clone().requires_grad_(True) |
| 45 | + |
| 46 | + inp2 = torch.randn(2, 4) |
| 47 | + c_custom_func = torch.compile(outer_function) |
| 48 | + c_usual_func = torch.compile(usual_function) |
| 49 | + |
| 50 | + result_custom = c_custom_func(inp1_custom, inp2) |
| 51 | + result_custom.backward() |
| 52 | + result_usual = c_usual_func(inp1_usual, inp2) |
| 53 | + result_usual.backward() |
| 54 | + |
| 55 | + torch.allclose(inp1_custom.grad, inp1_usual.grad) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + run_tests() |
0 commit comments