Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reshape only in row major layout #687

Merged
merged 3 commits into from
Sep 16, 2024

Conversation

mtopalovicTT
Copy link
Contributor

Reshape doesn't work for most output shapes in tile layout. As temporary workaround (until we figure out how to do a reshape in tile properly) and to unblock forge we will convert all input tensors to row before calling reshape, and then we will convert output tensor back to original layout.

#686

TTNN supports two kinds of reshapes - row and tile. row reshape works
as expected as long as we don't change last dim.
On the other hand tile reshape requires that output `W` and `H` dims are tile aligned.

For example this `reshape` in tile layout will fail:
1*12*32 -> 12*32

Error that we get is `Unable to reshape a tensor in TILE_LAYOUT
to non-tile height and width! Please convert the tensor to
ROW_MAJOR_LAYOUT first.`

In addition when we managed to do a `reshape` on tile tensor we
found that output tensor did not look the way we expected it to look.
For example:

If we have input row major tensor 4x4 which contains numbers from 0 .. 15
and we reshape it to 2x8 we will get [[0,1 .. 6,7], [8,9 .. 14,15]].
The output is correct.

If we take the same input tensor and convert it to tile layout
and call reshape to Shape((2, 8), (32, 32)) and convert output tensor to
row_major we will get tensor which looks like this:
[[0,1,2,3,0,0,0,0],[4,5,6,7,0,0,0,0]].This is different then row major
reshape.

In order to unblock forge for `reshape` op we will convert all input
tensors to row before calling reshape, and then we will convert output
tensor back to original layout.

Once we have understanding how reshape works for tile we will remove
this and implement proper fix.
@mtopalovicTT mtopalovicTT changed the title Milant/row major tensors for reshape Reshape only in row major layout Sep 13, 2024
%1 = "ttir.reshape"(%arg0, %0) <{shape = [4: i32, 32: i32, 2: i32, 32: i32] , operand_constraints = [#any_device_tile, #any_device_tile]}> : (tensor<4x2x32x32xbf16>, tensor<4x32x2x32xbf16>) -> tensor<4x32x2x32xbf16>
return %1 : tensor<4x32x2x32xbf16>
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these 2 tests belong in Silicon subdir? Otherwise they will not be exercising the runtime in CI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If thats the case we will have to move bunch of tests from dialect dir to silicon, since we are not testing bunch of ops.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the case :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay :)

@mtopalovicTT mtopalovicTT merged commit 8152560 into main Sep 16, 2024
9 of 11 checks passed
mtopalovicTT pushed a commit that referenced this pull request Sep 16, 2024
mtopalovicTT added a commit that referenced this pull request Sep 16, 2024
This reverts commit 8152560.

Co-authored-by: Milan Topalovic <mtopalovic994@gmail.com>
uazizTT pushed a commit that referenced this pull request Sep 16, 2024
* Convert to `row_major` layout before calling `reshape`

TTNN supports two kinds of reshapes - row and tile. row reshape works
as expected as long as we don't change last dim.
On the other hand tile reshape requires that output `W` and `H` dims are tile aligned.

For example this `reshape` in tile layout will fail:
1*12*32 -> 12*32

Error that we get is `Unable to reshape a tensor in TILE_LAYOUT
to non-tile height and width! Please convert the tensor to
ROW_MAJOR_LAYOUT first.`

In addition when we managed to do a `reshape` on tile tensor we
found that output tensor did not look the way we expected it to look.
For example:

If we have input row major tensor 4x4 which contains numbers from 0 .. 15
and we reshape it to 2x8 we will get [[0,1 .. 6,7], [8,9 .. 14,15]].
The output is correct.

If we take the same input tensor and convert it to tile layout
and call reshape to Shape((2, 8), (32, 32)) and convert output tensor to
row_major we will get tensor which looks like this:
[[0,1,2,3,0,0,0,0],[4,5,6,7,0,0,0,0]].This is different then row major
reshape.

In order to unblock forge for `reshape` op we will convert all input
tensors to row before calling reshape, and then we will convert output
tensor back to original layout.

Once we have understanding how reshape works for tile we will remove
this and implement proper fix.

* Adding comment

* Moving test to Silicon. Adding negative test for dialect.
uazizTT pushed a commit that referenced this pull request Sep 16, 2024
This reverts commit 8152560.

Co-authored-by: Milan Topalovic <mtopalovic994@gmail.com>
@mtopalovicTT mtopalovicTT deleted the milant/row_major_tensors_for_reshape branch December 31, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants