-
Notifications
You must be signed in to change notification settings - Fork 193
FP8 Block quantize onnx export support #324
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
jingyu-ml
wants to merge
21
commits into
main
Choose a base branch
from
jingyux/block-quant-onnx
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.
Open
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
071f167
Add Sage Attn ONNX & Fixed a bug in diffusers
jingyu-ml 831c32d
Lint
jingyu-ml d2c6e0f
Fixed test cases fail
jingyu-ml 0af26b2
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml 94ec97d
lint
jingyu-ml 35f3da2
update comment
jingyu-ml 25be640
Fix the test case
jingyu-ml 9e88a34
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml 0a0ad7a
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml f80b847
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml b9c4769
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml fb33cac
change the block size to 128
jingyu-ml d007815
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml df6bf80
Update the dim
jingyu-ml 09c491a
Update the K
jingyu-ml 18a6eee
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml b7cacd1
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml b01dcaa
Update the GraphContext
jingyu-ml f84f6e5
Update the args
jingyu-ml 3cebde6
update
jingyu-ml 1dc2221
Merge branch 'main' into jingyux/block-quant-onnx
jingyu-ml 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -646,6 +646,29 @@ def _real_quantize(self, inputs): | |
| self._dequantize = True | ||
| return outputs | ||
|
|
||
| def _get_block_sizes_list(self, shape): | ||
| """Convert block_sizes dict to list format based on tensor shape. | ||
|
|
||
| Args: | ||
| shape: The tensor shape to use for conversion (can be tuple or torch.Size) | ||
|
|
||
| Returns: | ||
| List of block sizes for each dimension, or None if block_sizes is None | ||
|
|
||
| Example: | ||
| block_sizes = {-2: 32} with shape [2, 24, 4608, 128] -> [1, 1, 32, 1] | ||
jingyu-ml marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| if self.block_sizes is None: | ||
| return None | ||
|
|
||
| block_sizes_list = [] | ||
| for dim in range(len(shape)): | ||
| # Check both positive and negative dimension indices | ||
| dim_negative = dim - len(shape) | ||
| block_size = self.block_sizes.get(dim, None) or self.block_sizes.get(dim_negative, None) | ||
| block_sizes_list.append(block_size if block_size is not None else 1) | ||
| return block_sizes_list | ||
|
|
||
jingyu-ml marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def _fake_quantize(self, inputs): | ||
| """Fake quantization.""" | ||
| amax = None | ||
|
|
@@ -654,7 +677,7 @@ def _fake_quantize(self, inputs): | |
| self._validate_amax(amax) | ||
|
|
||
| if self.block_sizes is not None and self.block_sizes.get("type", "static") == "dynamic": | ||
| # Block quantization, including dynamic and static block quantization | ||
| # Double scale Block quantization, including dynamic and static block quantization | ||
|
Contributor
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. Just to make sure, are we doing double quantization for fp8 block quantization or single? |
||
| block_size = self.block_sizes.get(-1, None) or self.block_sizes.get( | ||
| inputs.dim() - 1, None | ||
| ) | ||
|
|
@@ -675,6 +698,10 @@ def _fake_quantize(self, inputs): | |
| # Float-point quantization, e.g., FP8 | ||
| E, M = self._num_bits # noqa: N806 | ||
|
|
||
| # Convert block_sizes dict to list format | ||
| # Use original input shape if available (before reshaping), otherwise use current shape | ||
| shape_for_block_sizes = getattr(self, "_original_input_shape", inputs.shape) | ||
| block_sizes_list = self._get_block_sizes_list(shape_for_block_sizes) | ||
| outputs = scaled_e4m3( | ||
| inputs, | ||
| amax, | ||
|
|
@@ -683,6 +710,7 @@ def _fake_quantize(self, inputs): | |
| M, | ||
| self._trt_high_precision_dtype, | ||
| self._pass_through_bwd, | ||
| block_sizes_list, | ||
| ) | ||
|
|
||
| else: | ||
|
|
@@ -931,9 +959,10 @@ def forward(self, inputs): | |
| and self.block_sizes.get("type", None) != "dynamic" | ||
| and self._fake_quant | ||
| ): | ||
| # Tensor reshaping is required for static block quantization | ||
| # Tensor shapes are handled separately by the quantization kernels for dynamic block quantization | ||
| # Reshape is required if the logic is not handled in the simulation kernel | ||
| # Only MX format and NVFP4 reshape are currently supported by the kernel. | ||
| self._setup_for_blockquant(inputs) | ||
| setattr(self, "_original_input_shape", inputs.shape) | ||
| inputs = self._process_for_blockquant(inputs) | ||
jingyu-ml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| outputs = inputs | ||
|
|
@@ -974,6 +1003,8 @@ def forward(self, inputs): | |
| ): | ||
| outputs = self._reset_to_original_shape(outputs) | ||
|
|
||
| if hasattr(self, "_original_input_shape"): | ||
| delattr(self, "_original_input_shape") | ||
jingyu-ml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return outputs | ||
|
|
||
| def _short_amax(self, fmt=".4f"): | ||
|
|
||
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.