-
Notifications
You must be signed in to change notification settings - Fork 792
[PyTorch] Fix shape and size() for columnwise-only quantized tensors #3266
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
Merged
vthumbe1503
merged 4 commits into
NVIDIA:main
from
pggPL:quantized_tensor_columnwise_shape
Jul 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
199e437
[PyTorch] Fix Float8BlockwiseQTensor.shape for columnwise-only tensors
pggPL 0001d1b
Skip row-scaled NVFP4 columnwise-only case and speed up the 2D shape …
pggPL 0efc6c5
[PyTorch] Fix size() on the columnwise-only path for FP8 and blockwise
pggPL a6dfc49
Merge branch 'main' into quantized_tensor_columnwise_shape
vthumbe1503 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -757,6 +757,58 @@ def test_shape_with_none_data( | |
| f"after setting data to None on {type(x_test).__name__}" | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize("quantization", _quantization_list) | ||
| @pytest.mark.parametrize( | ||
| "rowwise, columnwise", | ||
| [(True, True), (True, False), (False, True)], | ||
| ids=["rowwise_columnwise", "rowwise_only", "columnwise_only"], | ||
| ) | ||
| @pytest.mark.parametrize("shape", [(128, 256), (4, 128, 256)], ids=["2d", "3d"]) | ||
| def test_shape_matches_size( | ||
| self, | ||
| *, | ||
| quantization: str, | ||
| rowwise: bool, | ||
| columnwise: bool, | ||
| shape: Iterable[int], | ||
| dtype: torch.dtype = torch.bfloat16, | ||
| device: torch.device = "cuda", | ||
| ) -> None: | ||
| """shape, size() and size(dim) stay consistent for every usage combination. | ||
|
|
||
| Both shape and size() are derived from whichever data buffer is present, | ||
| and classes that store columnwise data transposed have to undo that. A | ||
| columnwise-only tensor is where they can drift apart -- from each other, | ||
| and from the shape the tensor was allocated with. | ||
| """ | ||
| quantizer = make_quantizer(quantization, device=device) | ||
| # Row-scaled NVFP4 accepts set_usage(rowwise=False) but rejects the | ||
| # allocation itself, so it has to be filtered out up front. | ||
| if getattr(quantizer, "row_scaled_nvfp4", False) and not rowwise: | ||
| pytest.skip(f"{quantization} requires rowwise usage") | ||
| quantizer.set_usage(rowwise=rowwise, columnwise=columnwise) | ||
| if (quantizer.rowwise_usage, quantizer.columnwise_usage) != (rowwise, columnwise): | ||
| pytest.skip(f"{quantization} does not support this usage combination") | ||
|
|
||
| x = quantizer.make_empty(shape, dtype=dtype, device=device) | ||
| name = type(x).__name__ | ||
|
|
||
| # shape and size() must describe the same tensor, whichever buffer they | ||
| # end up reading. | ||
| assert tuple(x.shape) == tuple(x.size()), f"{name}: {tuple(x.shape)} vs {tuple(x.size())}" | ||
|
|
||
| # size(dim) must agree with the full shape, including negative indices. | ||
| # It cannot be served by forwarding dim to a transposed buffer. | ||
| for dim in range(len(x.shape)): | ||
| assert x.size(dim) == x.shape[dim], f"{name}.size({dim}) is {x.size(dim)}" | ||
| neg = dim - len(x.shape) | ||
| assert x.size(neg) == x.shape[neg], f"{name}.size({neg}) is {x.size(neg)}" | ||
|
|
||
| # NVFP4 deliberately reports columnwise-only tensors flattened to 2D and | ||
| # warns about it, so only the ranks it preserves are checked here. | ||
| if not (isinstance(quantizer, NVFP4Quantizer) and not rowwise and len(shape) > 2): | ||
| assert tuple(x.shape) == tuple(shape), f"{name}.shape is {tuple(x.shape)}" | ||
|
Collaborator
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. We might want to dig deep into why we dont need to apply the same shape preservation for NVFP4 tensors. But given it already exists in the code base, makes sense to defer it to a different PR. |
||
|
|
||
| @pytest.mark.parametrize( | ||
| "quantization", | ||
| _quantization_list + (["nvfp4_2d"] if nvfp4_available else []), | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.