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 test/prototype/mx_formats/test_nvfp4_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,25 @@ def test_nvfp4_matmul_with_amax(
assert sqnr >= SQNR_THRESHOLD, (
f"SQNR {sqnr:.2f} < {SQNR_THRESHOLD}, use_gelu={use_gelu}, mm_config={mm_config}, compile={compile}, bias={bias}"
)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available")
@pytest.mark.skipif(
not TORCH_VERSION_AT_LEAST_2_8, reason="NVFP4 requires PyTorch 2.8+"
)
def test_nvfp4_to_copy():
Copy link
Contributor

@jerryzh168 jerryzh168 Aug 20, 2025

Choose a reason for hiding this comment

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

should we verify the effect for to_copy as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

from torchao.prototype.mx_formats.nvfp4_tensor import NVFP4Tensor

x = NVFP4Tensor.to_nvfp4(torch.randn((32, 128))).cuda()
y = torch.ops.aten._to_copy(x, dtype=torch.bfloat16)
assert torch.equal(x.qdata, y.qdata)
assert torch.equal(x._scale_e4m3, y._scale_e4m3)
assert x._per_tensor_scale is None
assert y._per_tensor_scale is None
assert x._act_per_tensor_scale is None
assert y._act_per_tensor_scale is None
assert x._block_size == y._block_size
assert x.use_triton_kernel == y.use_triton_kernel
assert x.act_quant_kwargs == y.act_quant_kwargs
assert x.dtype == torch.float32
assert y.dtype == torch.bfloat16
2 changes: 1 addition & 1 deletion torchao/prototype/mx_formats/nvfp4_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ def nvfp4_to_copy(func, types, args, kwargs):

if dtype is not None:
res = NVFP4Tensor(
tensor.qdata,
tensor._scale_e4m3,
tensor._per_tensor_scale,
tensor._act_per_tensor_scale,
tensor._data,
tensor._block_size,
dtype,
tensor._is_swizzled_scales,
Expand Down
Loading