-
-
Notifications
You must be signed in to change notification settings - Fork 16.4k
[Quantization] [Refactor] Create special "GptOssMxfp4MoeMethod" #39604
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a8b0639
[Quantization] Rename mxfp4 quant layer and oracle to gpt_oss_mxfp4
zyongye ae81a7c
[Quantization] Add GptOssMxfp4Config subclass and gpt_oss_mxfp4 quant…
zyongye f1c6e48
[Quantization] Rename make_mxfp4_moe_* to make_gpt_oss_mxfp4_moe_* in…
zyongye e431630
[Quantization] Rename mxfp4 oracle functions to gpt_oss_mxfp4 scope
zyongye 9169706
[Quantization] Rename GptOssMxfp4MoeBackend back to Mxfp4MoeBackend
zyongye 300dd5f
[Quantization] Rename oracle/gpt_oss_mxfp4.py back to oracle/mxfp4.py
zyongye 0d87550
[Quantization] Rename gpt_oss-specific oracle functions
zyongye aecc7bc
[Quantization] Improve error messages and document normalization paths
zyongye 1c46d1b
[Quantization] Fix Mxfp4Config base class and MXFP4 variant detection
zyongye 37cfb60
[Quantization] Fix type annotations and doc cross-reference
zyongye 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
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
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,11 +19,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
| from vllm.model_executor.layers.fused_moe.oracle.mxfp4 import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| TRITON_BACKENDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Mxfp4MoeBackend, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| convert_to_mxfp4_moe_kernel_format, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| convert_gpt_oss_weight_to_mxfp4_moe_kernel_format, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| make_mxfp4_moe_kernel, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| make_mxfp4_moe_quant_config, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mxfp4_round_up_hidden_size_and_intermediate_size, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| select_mxfp4_moe_backend, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| select_gpt_oss_mxfp4_moe_backend, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from vllm.model_executor.layers.quantization import QuantizationMethods | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,6 +38,12 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| class Mxfp4Config(QuantizationConfig): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Canonical base config for MXFP4 quantization. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| Subclasses override get_name() and override_quantization_method() to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| register themselves as the handler for a specific checkpoint format. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, ignored_layers: list[str] | None = None): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| super().__init__() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.ignored_layers = ignored_layers | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -62,6 +68,8 @@ def get_supported_act_dtypes(cls) -> list[torch.dtype]: | |||||||||||||||||||||||||||||||||||||||||||||||||
| def get_config_filenames(cls) -> list[str]: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # TODO (zyongye) This is only temporaty fallback. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # We should have `Mxfp4MoEMethod` after this migration is complete. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_quant_method( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self, layer: torch.nn.Module, prefix: str | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> "QuantizeMethodBase | None": | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -79,7 +87,7 @@ def get_quant_method( | |||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return UnquantizedLinearMethod() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(layer, FusedMoE): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return Mxfp4MoEMethod(layer.moe_config) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return GptOssMxfp4MoEMethod(layer.moe_config) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(layer, Attention): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.debug_once( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "MXFP4 attention layer is not implemented. " | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -93,13 +101,46 @@ def is_mxfp4_quant(self, prefix: str, layer: torch.nn.Module) -> bool: | |||||||||||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| class Mxfp4MoEMethod(FusedMoEMethodBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| class GptOssMxfp4Config(Mxfp4Config): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """MXFP4 config for GPT-OSS checkpoints. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| Checkpoints carry ``"quant_method": "mxfp4"`` in their JSON config. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| override_quantization_method() maps that to the canonical internal name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| so that the rest of the loading path uses "gpt_oss_mxfp4" consistently. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_name(cls) -> QuantizationMethods: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "gpt_oss_mxfp4" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def override_quantization_method( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cls, hf_quant_cfg, user_quant, hf_config=None | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> QuantizationMethods | None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Match both "mxfp4" (original checkpoint value) and "gpt_oss_mxfp4" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # (already normalized by verify_and_update_model_config) so that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # explicit --quantization mxfp4 from the user doesn't cause a mismatch. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if not ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| isinstance(hf_quant_cfg, dict) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| and hf_quant_cfg.get("quant_method") in ("mxfp4", "gpt_oss_mxfp4") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Require explicit confirmation that this is a GPT-OSS model. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Do NOT fall back to returning the override when hf_config is None, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # as that would silently claim all mxfp4 checkpoints. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| model_type = getattr(hf_config, "model_type", None) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if model_type != "gpt_oss": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "gpt_oss_mxfp4" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+134
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. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| class GptOssMxfp4MoEMethod(FusedMoEMethodBase): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """MXFP4 MoE quantization method.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, moe: FusedMoEConfig): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| super().__init__(moe) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.weight_dtype = "mxfp4" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.mxfp4_backend, self.experts_cls = select_mxfp4_moe_backend(moe) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.weight_dtype = "gpt_oss_mxfp4" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.mxfp4_backend, self.experts_cls = select_gpt_oss_mxfp4_moe_backend(moe) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| self.max_capture_size = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| get_current_vllm_config().compilation_config.max_cudagraph_capture_size | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -281,7 +322,7 @@ def _setup_kernel( | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Convert weights to kernel format | ||||||||||||||||||||||||||||||||||||||||||||||||||
| w13, w2, w13_scale, w2_scale, w13_bias, w2_bias = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| convert_to_mxfp4_moe_kernel_format( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| convert_gpt_oss_weight_to_mxfp4_moe_kernel_format( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mxfp4_backend=self.mxfp4_backend, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| layer=layer, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| w13_weight=w13, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
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.
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.
@zyongye IMO these are not gpt-oss specific. quark_moe.py is under refactor to adopt mxfp4 oracle which supports any model with mxfp4 moe. (might see dup comments from me, I sent in a wrong place earlier)
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.
We are temporarily route all mxfp4 to gpt_oss_mxfp4 for compatibility reason. We will later create another
mxfp4moethat will decouple with this one. After we added that method we can route AMD related change to the new moe class.