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

check scale.ndim before applying t/transpose #1339

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions test/float8/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ def test_copy_(self):
fp8_b.copy_(fp8_a)
torch.testing.assert_close(fp8_a._data, fp8_b._data)

def test_transpose(self):
a = torch.rand((16, 16), dtype=torch.bfloat16)
for axiswise_dim in (None, 0, -1):
scale_a = tensor_to_scale(a, e4m3_dtype)
fp8_a = hp_tensor_and_scale_to_float8(
a, scale_a, e4m3_dtype, axiswise_dim=axiswise_dim
)
fp8_b = hp_tensor_and_scale_to_float8(
a, scale_a, e4m3_dtype, axiswise_dim=axiswise_dim
)

fp8_a_transposed = fp8_a.transpose(0, 1)
fp8_b_t = fp8_b.t()

torch.testing.assert_close(
(fp8_a_transposed._data, fp8_a_transposed._scale),
(fp8_b_t._data, fp8_b_t._scale),
)

@pytest.mark.parametrize("shape", [(8, 16), (4, 8, 16), (2, 4, 8, 16)])
@pytest.mark.parametrize("axiswise_dim", [0, -1])
def test_axiswise_dynamic_cast(self, shape, axiswise_dim):
Expand Down
5 changes: 4 additions & 1 deletion torchao/float8/float8_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def float8_desugar_data_and_scale_op(aten_op, args, kwargs=None):
)
def float8_transpose(aten_op, args, kwargs=None):
new_data = aten_op(args[0]._data, *args[1:], **kwargs)
new_scale = aten_op(args[0]._scale, *args[1:], **kwargs)
if args[0]._scale.ndim > 1:
new_scale = aten_op(args[0]._scale, *args[1:], **kwargs)
else:
new_scale = args[0]._scale

if aten_op == aten.transpose.int:
_assert_tensorwise_scale(aten_op, args[0]._scale)
Expand Down
Loading