Skip to content

Commit bb84ece

Browse files
Initial Commit
1 parent fd02c02 commit bb84ece

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎python/tvm/relax/frontend/torch/base_fx_graph_translator.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,26 @@ def _broadcast_to(self, node: fx.Node) -> relax.Var:
21072107
def _cat(self, node: fx.Node) -> relax.Var:
21082108
args = self.retrieve_args(node)
21092109
axis = args[1] if len(node.args) > 1 else node.kwargs.get("dim", 0)
2110-
return self.block_builder.emit(relax.op.concat(args[0], axis=axis))
2110+
tensors = args[0]
2111+
2112+
filtered = []
2113+
for t in tensors:
2114+
shape = self.shape_of(t)
2115+
if shape is not None and len(shape) == 1:
2116+
dim0 = self._static_dim(shape[0])
2117+
if dim0 == 0:
2118+
continue
2119+
filtered.append(t)
2120+
2121+
if not filtered:
2122+
# Passes args[0] to relax.op.concat
2123+
filtered = tensors
2124+
2125+
if len(filtered) == 1:
2126+
# Returns the only tensor that is not a 1D empty tensor
2127+
return filtered[0]
2128+
2129+
return self.block_builder.emit(relax.op.concat(filtered, axis=axis))
21112130

21122131
def _chunk(self, node: fx.Node) -> relax.Var:
21132132
x = self.env[node.args[0]]

‎tests/python/relax/test_frontend_from_exported_program.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4984,6 +4984,10 @@ class Cat1(Module):
49844984
def forward(self, x, y):
49854985
return torch.cat((x, y), dim=1)
49864986

4987+
class Cat2(Module):
4988+
def forward(self, x, y):
4989+
return torch.cat((x, y), dim=0)
4990+
49874991
@I.ir_module
49884992
class Expected1:
49894993
@R.function
@@ -5010,9 +5014,24 @@ def main(
50105014
R.output(gv)
50115015
return gv
50125016

5017+
# Expected IR: Concatenate a 1D empty tensor with any tensor
5018+
@I.ir_module
5019+
class Expected3:
5020+
@R.function
5021+
def main(
5022+
inp_0: R.Tensor((0,), dtype="float32"),
5023+
inp_1: R.Tensor((2, 3), dtype="float32"),
5024+
) -> R.Tuple(R.Tensor((2, 3), dtype="float32")):
5025+
with R.dataflow():
5026+
gv: R.Tuple(R.Tensor((2, 3), dtype="float32")) = (inp_1,)
5027+
R.output(gv)
5028+
return gv
5029+
50135030
example_args = (torch.randn(2, 3, dtype=torch.float32), torch.randn(2, 3, dtype=torch.float32))
5031+
example_args_empty = (torch.tensor([], dtype=torch.float32), torch.randn(2, 3, dtype=torch.float32))
50145032
verify_model(Cat0(), example_args, {}, Expected1)
50155033
verify_model(Cat1(), example_args, {}, Expected2)
5034+
verify_model(Cat2(), example_args_empty, {}, Expected3)
50165035

50175036

50185037
def test_cumsum():

‎tests/python/relax/test_frontend_from_fx.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,10 +5074,24 @@ def main(
50745074
R.output(gv)
50755075
return gv
50765076

5077+
# Expected IR: Concatenate a 1D empty tensor with any tensor
5078+
@I.ir_module
5079+
class Expected3:
5080+
@R.function
5081+
def main(
5082+
inp_0: R.Tensor((0,), dtype="float32"),
5083+
inp_1: R.Tensor((2, 3), dtype="float32"),
5084+
) -> R.Tensor((2, 3), dtype="float32"):
5085+
with R.dataflow():
5086+
gv: R.Tensor((2, 3), dtype="float32") = inp_1
5087+
R.output(gv)
5088+
return gv
5089+
50775090
verify_model(Cat0(), [([2, 3], "float32"), ([2, 3], "float32")], {}, Expected1)
50785091
verify_model(Cat1(), [([2, 3], "float32"), ([2, 3], "float32")], {}, Expected2)
50795092
verify_model(Cat2(), [([2, 3], "float32"), ([2, 3], "float32")], {}, Expected2)
50805093
verify_model(Cat3(), [([2, 3], "float32"), ([2, 3], "float32")], {}, Expected1)
5094+
verify_model(Cat3(), [([0], "float32"), ([2, 3], "float32")], {}, Expected3)
50815095

50825096

50835097
def test_max():

0 commit comments

Comments
 (0)