Skip to content

Qwen FP8 ModelOPT support #20734

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

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6927b02
resolve conflict
Edwardf0t1 Jul 2, 2025
b45972e
bugfix
Edwardf0t1 Jun 20, 2025
bf96528
handle language_model. prefix
Edwardf0t1 Jun 20, 2025
cb20cd1
fix issue in fused_experts calling
Edwardf0t1 Jun 21, 2025
1f61802
minor
Edwardf0t1 Jun 21, 2025
0fb23e1
update ModelOptFp8Config, handle prefix in mllama4 weight loading, debug
Edwardf0t1 Jun 22, 2025
03d2b3b
debug, handle kv scales
Edwardf0t1 Jun 23, 2025
93d7185
fix kv scale name matching issue
Edwardf0t1 Jun 23, 2025
d154fe1
update, debug
Edwardf0t1 Jun 23, 2025
782c018
cleanup
Edwardf0t1 Jun 24, 2025
b78b191
fix format
Edwardf0t1 Jun 24, 2025
22745f3
resolve conflict
Edwardf0t1 Jul 9, 2025
1c5acec
debug
Edwardf0t1 Jul 2, 2025
b10782d
handle eplb in ModelOptFp8MoEMethod
Edwardf0t1 Jul 2, 2025
8265287
broadcasting BMM experts scales
Edwardf0t1 Jul 3, 2025
59190ea
cleanup
Edwardf0t1 Jul 3, 2025
7a6fc84
some refactor and cleanup
Edwardf0t1 Jul 3, 2025
47a47a9
refactor Llama4ForConditionalGeneration.load_weights
Edwardf0t1 Jul 3, 2025
eec1daf
resolve conflict
Edwardf0t1 Jul 9, 2025
770bc24
format and linter error fix
Edwardf0t1 Jul 3, 2025
770890a
simplify ModelOptFp8MoEMethod to avoid mypy error
Edwardf0t1 Jul 3, 2025
1beecbf
resolve conflict
Edwardf0t1 Jul 4, 2025
0b98a7f
format fix
Edwardf0t1 Jul 4, 2025
cc44385
fix mypy error
Edwardf0t1 Jul 9, 2025
1ece491
Merge remote-tracking branch 'vllm/main' into zhiyu/llama4-fp8-modelopt
Jul 10, 2025
767358e
Merge remote-tracking branch 'vllm/main' into jingyux/dev-qwen-fp8
Jul 10, 2025
1206f33
add qwen fp8 modelopt support
Jul 10, 2025
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
4 changes: 4 additions & 0 deletions vllm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,10 @@
if quant_cfg is None:
# compressed-tensors uses a "compression_config" key
quant_cfg = getattr(self.hf_config, "compression_config", None)
if quant_cfg is not None:
if quant_cfg["producer"]["name"].lower() == "modelopt":
if "quant_algo" in quant_cfg.keys() and quant_cfg["quant_algo"].lower() == "fp8":
quant_cfg = {"quant_method": "modelopt"}

Check failure on line 893 in vllm/config.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (SIM118)

vllm/config.py:892:20: SIM118 Use `key in dict` instead of `key in dict.keys()`

Check failure on line 893 in vllm/config.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (SIM102)

vllm/config.py:891:13: SIM102 Use a single `if` statement instead of nested `if` statements

Check failure on line 893 in vllm/config.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (SIM102)

vllm/config.py:890:9: SIM102 Use a single `if` statement instead of nested `if` statements
Comment on lines +891 to +893
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The direct access to quant_cfg["producer"]["name"] is unsafe and could raise a KeyError if the keys are not present in the quant_cfg dictionary. It's better to use .get() for safer access. Additionally, the nested if statements can be combined for improved readability.

Suggested change
if quant_cfg["producer"]["name"].lower() == "modelopt":
if "quant_algo" in quant_cfg.keys() and quant_cfg["quant_algo"].lower() == "fp8":
quant_cfg = {"quant_method": "modelopt"}
if (
quant_cfg is not None
and quant_cfg.get("producer", {}).get("name", "").lower() == "modelopt"
and quant_cfg.get("quant_algo", "").lower() == "fp8"
):
quant_cfg = {"quant_method": "modelopt"}

return quant_cfg

def _verify_quantization(self) -> None:
Expand Down
37 changes: 31 additions & 6 deletions vllm/model_executor/layers/fused_moe/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def create_weights(self, layer: torch.nn.Module, num_experts: int,
params_dtype: torch.dtype, **extra_weight_attrs):
raise NotImplementedError

def uses_weight_scale_2_pattern(self) -> bool:
"""
Returns True if this quantization method uses 'weight_scale_2' pattern
for per-tensor weight scales (e.g., FP4 variants), False otherwise.
This method should be overridden by subclasses that use the
'weight_scale_2' pattern instead of the standard 'weight_scale' pattern.
"""
return False

def init_prepare_finalize(self, moe: FusedMoEConfig,
quant_config: Optional[QuantizationConfig]):
all2all_manager = get_ep_group().device_communicator.all2all_manager
Expand Down Expand Up @@ -1049,12 +1059,23 @@ def weight_loader(self,

# TODO @dsikka: ModelOpt should follow the proper MoE loading pattern
if "ModelOpt" in quant_method_name:
if ('weight_scale_2' in weight_name
or 'input_scale' in weight_name):
self._load_per_tensor_weight_scale(shard_id=shard_id,
param=param,
loaded_weight=loaded_weight,
expert_id=expert_id)
# Determine per-tensor weight scale patterns based on variant
# Use the dedicated method instead of brittle string matching
uses_weight_scale_2 = self.quant_method.uses_weight_scale_2_pattern(
)

# For per-tensor, FP4 uses "weight_scale_2", FP8 uses "weight_scale"
per_tensor_conditions = (
"weight_scale_2" in weight_name if uses_weight_scale_2 else
"weight_scale" in weight_name) or "input_scale" in weight_name

if per_tensor_conditions:
self._load_per_tensor_weight_scale(
shard_id=shard_id,
param=param,
loaded_weight=loaded_weight,
expert_id=expert_id,
)
elif "weight" in weight_name:
self._load_model_weight_or_group_weight_scale(
shard_id=shard_id,
Expand Down Expand Up @@ -1526,3 +1547,7 @@ def moe_forward_fake(hidden_states: torch.Tensor, router_logits: torch.Tensor,
dispatch_key=current_platform.dispatch_key,
tags=(torch.Tag.needs_fixed_stride_order, ),
)

# Mark the FusedMoE weight_loader as supporting MoE-specific parameters
# to avoid expensive runtime reflection in model loading code
FusedMoE.weight_loader.supports_moe_loading = True # type: ignore[attr-defined]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider using torch.inference_mode() or torch.no_grad() to disable gradient calculation during inference, potentially reducing memory consumption and improving performance.

Suggested change
FusedMoE.weight_loader.supports_moe_loading = True # type: ignore[attr-defined]
with torch.inference_mode():
FusedMoE.weight_loader.supports_moe_loading = True # type: ignore[attr-defined]

2 changes: 2 additions & 0 deletions vllm/model_executor/layers/quantization/experts_int8.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def quantize_and_call_weight_loader(param: torch.nn.Parameter,
weight_loader(param, loaded_weight, weight_name, shard_id,
expert_id)

# Mark as supporting MoE-specific loading to avoid expensive reflection
quantize_and_call_weight_loader.supports_moe_loading = True # type: ignore[attr-defined]
return quantize_and_call_weight_loader


Expand Down
Loading
Loading