-
Notifications
You must be signed in to change notification settings - Fork 296
Add CUDA kernel for MXFP8 dim1 casting #2513
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
Open
danielvegamyhre
wants to merge
1
commit into
main
Choose a base branch
from
danielvegamyhre/stack/3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,804
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
triton_to_mxfp8_dim1_reference, | ||
unpack_uint4, | ||
) | ||
from torchao.prototype.mx_formats.mx_tensor import MXTensor | ||
from torchao.prototype.mx_formats.mx_tensor import MXTensor, ScaleCalculationMode, to_mx | ||
from torchao.prototype.mx_formats.utils import to_blocked | ||
from torchao.utils import ( | ||
TORCH_VERSION_AT_LEAST_2_8, | ||
|
@@ -56,6 +56,15 @@ | |
pytest.skip("Unsupported PyTorch version", allow_module_level=True) | ||
|
||
|
||
# TODO: shared utils file for benchmarking and testing | ||
def to_mx_dim1_reference(x_hp, block_size, scaling_mode): | ||
x_hp = x_hp.t().contiguous() | ||
scale_d1, data_d1 = to_mx( | ||
x_hp, torch.float8_e4m3fn, block_size, scaling_mode=scaling_mode | ||
) | ||
return data_d1.t(), scale_d1 | ||
|
||
|
||
@pytest.mark.skip( | ||
reason="TODO debug CI failure, low pri since this is not used in the MX code" # noqa: E501 | ||
) | ||
|
@@ -488,3 +497,99 @@ def test_rearrange(shape): | |
eager = to_blocked(scales, False) | ||
triton = to_blocked(scales, True) | ||
torch.testing.assert_close(eager, triton, atol=0, rtol=0) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not is_sm_at_least_100(), | ||
reason="MXFP8 requires CUDA capability 10.0 or greater", | ||
) | ||
@pytest.mark.parametrize("M", (32, 64, 2048)) | ||
@pytest.mark.parametrize("K", (32, 64, 2048)) | ||
@pytest.mark.parametrize("input_dtype", (torch.float32, torch.bfloat16)) | ||
@pytest.mark.parametrize( | ||
"scaling_mode", (ScaleCalculationMode.FLOOR, ScaleCalculationMode.RCEIL) | ||
) | ||
def test_cuda_mx_dim1_numerics(M, K, input_dtype, scaling_mode): | ||
from torchao.prototype import mxfp8_cuda | ||
|
||
scaling_mode_str = ( | ||
"floor" if scaling_mode == ScaleCalculationMode.FLOOR else "rceil" | ||
) | ||
block_size = 32 | ||
|
||
# Use disinct incrementing values from 0 to M*K-1 to make debugging easier. | ||
x = ( | ||
torch.arange(0, M * K, dtype=input_dtype, device="cuda") | ||
.reshape(M, K) | ||
.contiguous() | ||
) | ||
|
||
y_d1_ref, s_d1_ref = to_mx_dim1_reference( | ||
x, | ||
block_size=block_size, | ||
scaling_mode=scaling_mode, | ||
) | ||
|
||
_, y_d1, _, s_d1 = mxfp8_cuda.quantize( | ||
x, | ||
rowwise=False, | ||
colwise=True, | ||
scaling_mode=scaling_mode_str, | ||
scale_dim_x=1, | ||
scale_dim_y=block_size, | ||
) | ||
|
||
# check scales | ||
torch.testing.assert_close(s_d1, s_d1_ref, rtol=0, atol=0) | ||
|
||
# check quantized values | ||
torch.testing.assert_close(y_d1, y_d1_ref, rtol=0, atol=0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also test the memory layout of all the tensors vs reference |
||
assert y_d1.stride() == y_d1_ref.stride(), "quantized tensor strides do not match" | ||
|
||
|
||
@pytest.mark.skipif( | ||
not is_sm_at_least_100(), | ||
reason="MXFP8 requires CUDA capability 10.0 or greater", | ||
) | ||
def test_cuda_mx_dim0_not_supported(): | ||
from torchao.prototype import mxfp8_cuda | ||
|
||
M, K = 64, 64 | ||
block_size = 32 | ||
x = ( | ||
torch.arange(0, M * K, dtype=torch.bfloat16, device="cuda") | ||
.reshape(M, K) | ||
.contiguous() | ||
) | ||
with pytest.raises(RuntimeError): | ||
_, y_d1, _, s_d1 = mxfp8_cuda.quantize( | ||
x, | ||
rowwise=True, | ||
colwise=False, | ||
scale_dim_x=block_size, | ||
scale_dim_y=1, | ||
) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not is_sm_at_least_100(), | ||
reason="MXFP8 requires CUDA capability 10.0 or greater", | ||
) | ||
def test_cuda_mx_dim1_invalid_block_size(): | ||
from torchao.prototype import mxfp8_cuda | ||
|
||
M, K = 64, 64 | ||
x = ( | ||
torch.arange(0, M * K, dtype=torch.bfloat16, device="cuda") | ||
.reshape(M, K) | ||
.contiguous() | ||
) | ||
invalid_block_size = 4 | ||
with pytest.raises(RuntimeError): | ||
_, y_d1, _, s_d1 = mxfp8_cuda.quantize( | ||
x, | ||
rowwise=False, | ||
colwise=True, | ||
scale_dim_x=1, | ||
scale_dim_y=invalid_block_size, | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove "floor" to match all the others, or add "floor" to all the others
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added floor to others, I like the more explicit naming.