This is an automated email from the ASF dual-hosted git repository.
junrushao 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 7d88352c3c [Relay][Pytorch] Add aten::view_as (#15370)
7d88352c3c is described below
commit 7d88352c3c2b47abab2abc14ebdc1543b1e5d096
Author: Huan Mei <[email protected]>
AuthorDate: Fri Jul 21 14:32:49 2023 +0800
[Relay][Pytorch] Add aten::view_as (#15370)
support aten::view_as op for the frontend of pytorch. The info of this op
is available at this
[link](https://pytorch.org/docs/stable/generated/torch.Tensor.view_as.html).
---
python/tvm/relay/frontend/pytorch.py | 13 +++++++++++++
tests/python/frontend/pytorch/test_forward.py | 15 +++++++++++++++
2 files changed, 28 insertions(+)
diff --git a/python/tvm/relay/frontend/pytorch.py
b/python/tvm/relay/frontend/pytorch.py
index 37f32e3c02..eadd0a3c46 100644
--- a/python/tvm/relay/frontend/pytorch.py
+++ b/python/tvm/relay/frontend/pytorch.py
@@ -1620,6 +1620,18 @@ class PyTorchOpConverter:
return _op.transform.reshape(data, new_shape)
+ def view_as(self, inputs, input_types):
+ data = inputs[0]
+ tensors = inputs[1]
+
+ if not isinstance(tensors, (_expr.Call, _expr.Constant, _expr.Var)):
+ msg = f"Data type {type(tensors)} could not be parsed in view_as
op"
+ raise AssertionError(msg)
+
+ shape = self.infer_shape(tensors)
+
+ return _op.transform.reshape(data, shape)
+
def reshape(self, inputs, input_types):
data = inputs[0]
new_shape = inputs[1]
@@ -3836,6 +3848,7 @@ class PyTorchOpConverter:
"aten::addmm": self.addmm,
"aten::size": self.size,
"aten::view": self.view,
+ "aten::view_as": self.view_as,
"aten::reshape": self.reshape,
"aten::reshape_as": self.reshape_as,
"aten::clone": self.clone,
diff --git a/tests/python/frontend/pytorch/test_forward.py
b/tests/python/frontend/pytorch/test_forward.py
index 5b02d26d5d..cb49e837fe 100644
--- a/tests/python/frontend/pytorch/test_forward.py
+++ b/tests/python/frontend/pytorch/test_forward.py
@@ -1660,6 +1660,21 @@ def test_forward_view():
verify_model(View3().float().eval(), input_data=input_data)
[email protected]_gpu
+def test_forward_view_as():
+ """test_forward_view_as"""
+ torch.set_grad_enabled(False)
+ input_shape = [1, 3, 10]
+
+ class ViewAs1(Module):
+ def forward(self, *args):
+ t1 = torch.ones((1 * 3 * 10))
+ return args[0].view_as(t1)
+
+ input_data = torch.rand(input_shape).float()
+ verify_model(ViewAs1().float().eval(), input_data=input_data)
+
+
@tvm.testing.uses_gpu
def test_forward_select():
"""test_forward_select"""