Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions python/tvm/relay/frontend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,27 @@ def flatten(self, inputs, input_types):
out = _op.squeeze(out, axis=squeeze_axes)
return out

def unflatten(self, inputs, input_types):
data = inputs[0]
dim = int(inputs[1])
unflattened_size = tuple(inputs[2])
dshape = get_const_tuple(self.infer_shape_with_prelude(data))

dim = dim if dim >= 0 else len(dshape) + dim
assert len(dshape) > dim >= 0

assert unflattened_size.count(-1) <= 1

mult = np.multiply.reduce(unflattened_size)
if mult < 0:
assert dshape[dim] % mult == 0
else:
assert dshape[dim] == mult

new_shape = dshape[:dim] + unflattened_size + dshape[dim + 1 :]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add check that dshape[dim] == multiplication of dimensions in unflattened_size

Copy link
Contributor Author

@mshr-h mshr-h Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vvchernov Thanks!
I don't think we have to check it because torch.jit.trace does it.
They provide something like the below RuntimeError when the shape is wrong.

RuntimeError: unflatten: Provided sizes [3, 5, 3, -1] don't multiply up to the size of dim 0 (60) in the input tensor

Should we add the check in TVM's PyTorch frontend?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @mshr-h! Ok torch.jit.trace do it, but in this case we do not need assert len(dshape) > dim.
It looks like TVM usually rechecks all corner cases.
About -1 in unflattened_size. In this case we can multiply together other dimension and check dshape[dim] % mult == 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vvchernov Thanks. Added the assertion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @mshr-h! There are two cases: unflattened_size has -1 and does not have. You check only the first one. Example: dshape[dim] = 8, unflattened_size = [2, 2, 1, 1] pass your assert, but it is failure case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked that _op.reshape does not check size correctness by it-self. Nevertheless I found that dim can be not only -1, but from list {0, -1, -2, -3, -4} See

def reshape(data, newshape, allowzero=False):

I suggest to check -1 case only. It looks like torch does not have other options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vvchernov
Thanks! Updated the shape check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, dim can be negative but I didn't check that. I'll add the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, dim can be negative but I didn't check that. I'll add the check.

Done.

out = _op.reshape(data, new_shape)
return out

def addmm(self, inputs, input_types):
input_mat = inputs[0]
mat1 = inputs[1]
Expand Down Expand Up @@ -3945,6 +3966,7 @@ def create_convert_map(self):
"aten::t": self.transpose,
"aten::numpy_T": self.numpy_T,
"aten::flatten": self.flatten,
"aten::unflatten": self.unflatten,
"aten::addmm": self.addmm,
"aten::size": self.size,
"aten::view": self.view,
Expand Down
36 changes: 35 additions & 1 deletion tests/python/frontend/pytorch/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,40 @@ def _test_flatten(start_dim, end_dim):
verify_model(_test_flatten(-3, -2), inp)


@tvm.testing.uses_gpu
def test_unflatten():
"""test_unflatten"""

def _test_unflatten(dim, unflattened_size):
return lambda inp: torch.unflatten(inp, dim, unflattened_size)

inp = torch.rand(60)

# [60] -> [3, 5, 2, 2]
verify_model(_test_unflatten(0, (3, 5, 2, 2)), inp)
verify_model(_test_unflatten(0, (-1, 5, 2, 2)), inp)
verify_model(_test_unflatten(0, (3, -1, 2, 2)), inp)
verify_model(_test_unflatten(0, (3, 5, -1, 2)), inp)
verify_model(_test_unflatten(0, (3, 5, 2, -1)), inp)
verify_model(_test_unflatten(-1, (3, 5, 2, 2)), inp)
verify_model(_test_unflatten(-1, (-1, 5, 2, 2)), inp)
verify_model(_test_unflatten(-1, (3, -1, 2, 2)), inp)
verify_model(_test_unflatten(-1, (3, 5, -1, 2)), inp)
verify_model(_test_unflatten(-1, (3, 5, 2, -1)), inp)

inp = torch.rand(3, 4, 1)

# [3, 4, 1] -> [3, 2, 2, 1]
verify_model(_test_unflatten(1, (2, 2)), inp)
verify_model(_test_unflatten(1, (-1, 2)), inp)

inp = torch.rand(5, 12, 3)

# [5, 12, 3] -> [5, 2, 2, 3, 1, 1, 3]
verify_model(_test_unflatten(1, (2, 2, 3, 1, 1)), inp)
verify_model(_test_unflatten(-2, (2, 2, 3, 1, 1)), inp)


@tvm.testing.uses_gpu
def test_forward_transpose():
"""test_forward_transpose"""
Expand Down Expand Up @@ -4744,7 +4778,7 @@ def test_fn(x, mask):
verify_model(test_fn, [inp.to(torch.float64), inp > 0.5])


@pytest.mark.skip(reason="unsupported op: 'aten::scaled_dot_product_attention', 'aten::unflatten'")
@pytest.mark.skip(reason="unsupported op: 'aten::scaled_dot_product_attention'")
def test_transformer():
"""test_transformer"""
model = torch.nn.Transformer(d_model=256, nhead=8, num_encoder_layers=6, num_decoder_layers=6)
Expand Down