This is an automated email from the ASF dual-hosted git repository.

tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 40312853d7 [Relax][PyTorch] Support Tensor.t in the FX importer 
(#20927)
40312853d7 is described below

commit 40312853d7205037778e346732e9b0172e21e3c4
Author: Zhewen Tan <[email protected]>
AuthorDate: Wed Nov 11 13:25:36 2026 +0800

    [Relax][PyTorch] Support Tensor.t in the FX importer (#20927)
    
    This adds support for `Tensor.t()` in the PyTorch FX importer.
    
    For tensors with fewer than two dimensions, the importer returns the
    input unchanged. For 2-D tensors, it emits `relax.op.permute_dims` with
    reversed axes, matching PyTorch semantics. Inputs with more than two
    dimensions raise an error.
    
    A regression test covers the 2-D transpose path.
    
    Fixes #20922.
    
    Testing:
    - Targeted Relax PyTorch frontend tests: 7 passed
    - Pre-commit checks: all passed
    - `git diff --check`
---
 .../relax/frontend/torch/base_fx_graph_translator.py | 10 ++++++++++
 python/tvm/relax/frontend/torch/fx_translator.py     |  1 +
 tests/python/relax/test_frontend_from_fx.py          | 20 ++++++++++++++++++++
 3 files changed, 31 insertions(+)

diff --git a/python/tvm/relax/frontend/torch/base_fx_graph_translator.py 
b/python/tvm/relax/frontend/torch/base_fx_graph_translator.py
index 9bb3b77d73..21544ed20c 100644
--- a/python/tvm/relax/frontend/torch/base_fx_graph_translator.py
+++ b/python/tvm/relax/frontend/torch/base_fx_graph_translator.py
@@ -2675,6 +2675,16 @@ class BaseFXGraphImporter(metaclass=abc.ABCMeta):
             relax.op.topk(x, k=k, axis=dim, largest=largest, ret_type="both", 
dtype="int64")
         )
 
+    def _t(self, node: fx.Node) -> relax.Var:
+        args = self.retrieve_args(node)
+        x = args[0]
+        rank = len(self.shape_of(x))
+        if rank > 2:
+            raise ValueError(f"t() expects a tensor with <= 2 dimensions, but 
got {rank}D")
+        if rank < 2:
+            return x
+        return self.block_builder.emit(relax.op.permute_dims(x, [1, 0]))
+
     def _transpose(self, node: fx.Node) -> relax.Var:
         args = self.retrieve_args(node)
         full_idx = list(range(len(self.shape_of(args[0]))))
diff --git a/python/tvm/relax/frontend/torch/fx_translator.py 
b/python/tvm/relax/frontend/torch/fx_translator.py
index 7d31fa3468..2e7c2b46dd 100644
--- a/python/tvm/relax/frontend/torch/fx_translator.py
+++ b/python/tvm/relax/frontend/torch/fx_translator.py
@@ -1018,6 +1018,7 @@ class TorchFXImporter(BaseFXGraphImporter):
             "squeeze": self._squeeze,
             "stack": self._stack,
             "take": self._take,
+            "t": self._t,
             "tile": self._tile,
             "topk": self._topk,
             "transpose": self._transpose,
diff --git a/tests/python/relax/test_frontend_from_fx.py 
b/tests/python/relax/test_frontend_from_fx.py
index d3c6315c13..571541eebd 100644
--- a/tests/python/relax/test_frontend_from_fx.py
+++ b/tests/python/relax/test_frontend_from_fx.py
@@ -4638,6 +4638,26 @@ def test_transpose():
     verify_model(Transpose(), input_info, {}, expected1)
 
 
+def test_t():
+    input_info = [([2, 3], "float32")]
+
+    class T(Module):
+        def forward(self, x):
+            return x.t()
+
+    @tvm_tirx.script.ir_module
+    class expected:
+        @R.function
+        def main(x: R.Tensor((2, 3), dtype="float32")) -> R.Tensor((3, 2), 
dtype="float32"):
+            with R.dataflow():
+                lv: R.Tensor((3, 2), dtype="float32") = R.permute_dims(x, 
axes=[1, 0])
+                gv: R.Tensor((3, 2), dtype="float32") = lv
+                R.output(gv)
+            return gv
+
+    verify_model(T(), input_info, {}, expected)
+
+
 def test_repeat():
     class Tile1(Module):
         def forward(self, x: torch.Tensor):

Reply via email to