[Dynamo] Fix Tensor.T trace (#88642)

Summary:

Tensor.T considered T as a GetAttr and didn't progate "example_value"

Via https://pytorch.org/docs/stable/tensors.html#torch.Tensor.T
> If n is the number of dimensions in x, x.T is equivalent to
> x.permute(n-1, n-2, ..., 0).

Fixes pytorch/torchdynamo#1476

Test Plan:

pytest test/dynamo/test_functions.py::FunctionTests::test_T

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: [D41130306](https://our.internmc.facebook.com/intern/diff/D41130306)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/88642
Approved by: https://github.com/tugsbayasgalan, https://github.com/yanboliang, https://github.com/jansel
This commit is contained in:
Akshit Khurana 2022-11-08 10:29:39 -08:00 committed by PyTorch MergeBot
parent c7fc710459
commit 7006ac6ee5
2 changed files with 7 additions and 0 deletions

View File

@ -329,6 +329,10 @@ class FunctionTests(torch._dynamo.test_case.TestCase):
if x.ndim == 2 and x.ndimension() == 2 and x.dim() == 2:
return x + 1
@make_test
def test_T(x):
return torch.ones_like(x.T)
@make_test
def test_is_sparse(x):
if not x.is_sparse:

View File

@ -438,6 +438,9 @@ class TensorVariable(VariableTracker):
result = self.call_method(tx, "size", [], {})
elif name == "ndim" and self.ndim is None:
result = self.call_method(tx, "dim", [], {})
elif name == "T":
args = [variables.ConstantVariable(i) for i in range(self.ndim - 1, -1, -1)]
result = self.call_method(tx, "permute", args, {})
if name == "__class__":
return TorchVariable(self.python_type(), **options)