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
14 changes: 14 additions & 0 deletions test/prototype/mx_formats/test_mx_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,20 @@ def test_view(elem_dtype):
x_mx_2 = x_mx.view(2, 4) # noqa: F841


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available")
def test_clone():
data = torch.randn(8, 8, device="cuda", dtype=torch.bfloat16)
block_size = 4
data_mx = MXTensor.to_mx(data, torch.float8_e4m3fn, block_size)
data_mx_c = data_mx.clone()
torch.testing.assert_close(
data_mx.to_dtype(torch.bfloat16),
data_mx_c.to_dtype(torch.bfloat16),
atol=0,
rtol=0,
)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available")
@pytest.mark.parametrize("elem_dtype", [DTYPE_FP6_E2M3, DTYPE_FP6_E3M2])
@pytest.mark.parametrize("pack_fp6", [False, True])
Expand Down
13 changes: 13 additions & 0 deletions torchao/prototype/mx_formats/mx_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,16 @@ def autocast_to_copy(func, types, args, kwargs):

# If only device was changed, return the device-changed tensor
return tensor


@implements([aten.clone.default])
def mx_clone(func, types, args, kwargs):
self = args[0]
memory_format = kwargs.get("memory_format", None)

if memory_format is not None:
clone_fn = lambda x: x.clone(memory_format=memory_format)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just avoid the if statement with clone_fn = lambda x: x.clone(**kwargs)? Do we need to explicitly exclude other kwargs and only include memory format if specified, for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is copy-pasta from

def nvfp4_clone(func, types, args, kwargs):
, IMO let's separate cleanup of this into a separate PR

else:
clone_fn = lambda x: x.clone()

return self._apply_fn_to_data(clone_fn)
Loading