Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
10 changes: 10 additions & 0 deletions vllm/model_executor/layers/quantization/fp8.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(
activation_scheme: str = "dynamic",
ignored_layers: list[str] | None = None,
weight_block_size: list[int] | None = None,
store_dtype: str | None = None,
) -> None:
super().__init__()

Expand All @@ -115,6 +116,7 @@ def __init__(
raise ValueError(f"Unsupported activation scheme {activation_scheme}")
self.activation_scheme = activation_scheme
self.ignored_layers = ignored_layers or []
self.store_dtype = store_dtype
if weight_block_size is not None:
if not is_checkpoint_fp8_serialized:
raise ValueError(
Expand Down Expand Up @@ -162,6 +164,7 @@ def from_config(cls, config: dict[str, Any]) -> "Fp8Config":
activation_scheme = cls.get_from_keys(config, ["activation_scheme"])
ignored_layers = cls.get_from_keys_or(config, ["ignored_layers"], None)
weight_block_size = cls.get_from_keys_or(config, ["weight_block_size"], None)
store_dtype = cls.get_from_keys_or(config, ["store_dtype"], None)
if not ignored_layers:
ignored_layers = cls.get_from_keys_or(
config, ["modules_to_not_convert"], None
Expand All @@ -171,6 +174,7 @@ def from_config(cls, config: dict[str, Any]) -> "Fp8Config":
activation_scheme=activation_scheme,
ignored_layers=ignored_layers,
weight_block_size=weight_block_size,
store_dtype=store_dtype,
)

def get_quant_method(
Expand Down Expand Up @@ -198,6 +202,12 @@ def get_quant_method(
fused_mapping=self.packed_modules_mapping,
):
return UnquantizedFusedMoEMethod(layer.moe_config)
if self.store_dtype == "mxfp4":
from vllm.model_executor.layers.quantization.mxfp4 import (
Mxfp4MoEMethod,
)

return Mxfp4MoEMethod(layer.moe_config)
if self.is_checkpoint_fp8_serialized:
moe_quant_method = Fp8MoEMethod(self, layer)
else:
Expand Down
165 changes: 160 additions & 5 deletions vllm/model_executor/models/mimo_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
)
from vllm.model_executor.layers.logits_processor import LogitsProcessor
from vllm.model_executor.layers.quantization import QuantizationConfig
from vllm.model_executor.layers.quantization.utils.quant_utils import (
GroupShape,
scaled_quantize,
)
from vllm.model_executor.layers.rotary_embedding import get_rope
from vllm.model_executor.layers.vocab_parallel_embedding import (
ParallelLMHead,
Expand Down Expand Up @@ -441,6 +445,85 @@ def is_compressed_softmax_layer(self) -> bool:
return self.config.hybrid_layer_pattern[self.layer_id] == 1


def _shard_fp8_qkv_proj(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

QQ: does this affect the accuracy of XiaomiMiMo/MiMo-V2.5-Pro?

@TheEpicDolphin TheEpicDolphin Jun 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It should only have an effect on accuracy if running with TP != 8. The reason is because we have to re-quantize the QKV weights in order to properly shard them on the TP ranks. In the special case of num_kv_heads = TP (TP=8), then we skip this re-quantization to avoid impacting accuracy. In the past, TP != 8 wasn't even supported (garbage outputs), so this change should strictly improve things.

I'll make sure to run evals to check that there are no regressions for XiaomiMiMo/MiMo-V2.5-Pro with TP=8

w_full: torch.Tensor,
s_full: torch.Tensor,
num_heads: int,
num_kv_heads: int,
head_dim: int,
v_head_dim: int,
tp_rank: int,
tp_size: int,
block: int = 128,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Shard the fp8 qkv_proj weights for ``tp_rank``.

The checkpoint stores the fused QKV as ``num_kv_heads`` contiguous groups
(one per KV head; ``n`` below), each ordered ``[Q | K | V]``:

[Q_1 | K_1 | V_1 | Q_2 | K_2 | V_2 | ... | Q_n | K_n | V_n]

Per group, Q has ``(num_heads / num_kv_heads) * head_dim`` rows, K has
``head_dim`` rows, and V has ``v_head_dim`` rows.

Each TP rank owns ``g = num_kv_heads / tp_size`` of these groups, and the
forward expects them de-interleaved into a single Q, K, and V block:

[Q_1 | Q_2 | ... | Q_g | K_1 | K_2 | ... | K_g | V_1 | V_2 | ... | V_g]

When ``g == 1`` the rank's slice is already ``[Q | K | V]``, so a plain
chunk suffices. When ``g > 1`` we cannot reach the de-interleaved layout by
re-permuting the fp8 block scales: each scale covers a 128-row block, and
since K is 192 rows (1.5 blocks) a block straddles the K/V boundary, so no
whole-block permutation produces it. Instead we dequantize this rank's
groups to float (dropping the block constraint), reorder the rows into the
layout above (Q, K, and V then each span a whole number of blocks), and
re-quantize to fp8.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this preserve the scales or are we recomputing new scales as well? If this makes new scales then we probably want to run some deeper accuracy evals to ensure there's no subtle accuracy degradation (possibly as a follow-up, given this fixes a major failure case?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It depends on the TP regime:

  • At TP = 8, the same logic as before is used to shard the qkv weights/scales. In this case the scales are preserved exactly.
  • At TP != 8, we now have to recompute the scales. This is because the attention forward pass expected the Q, K, and V to be laid out like: [all Q | all K | all V], but the checkpoint stores the weights interleaved by KV head like [Q_1 | K_1 | V_1 | Q_2 | K_2 | V_2 | ...]. We can easily permute the weights, but not the scales. And the reason we can't permute the scales is because each KV head has a scale value that straddles the K/V boundary.

So yeah, for TP != 8, this PR computes new scales. I ran some evals (scores are in the PR summary), and I don't see any regressions in accuracy compared to the TP=8 case that worked before. Of course, I'm happy to run deeper evals as a follow up.

"""
assert tp_size <= num_kv_heads and num_kv_heads % tp_size == 0, (
"TP size must evenly split the number of KV heads."
)

kv_heads_per_rank = num_kv_heads // tp_size
if kv_heads_per_rank == 1:
# One KV head per rank. The weights and scale can be trivially sharded
# without re-quantization.
w = w_full.chunk(tp_size, dim=0)[tp_rank]
s = s_full.chunk(tp_size, dim=0)[tp_rank]
return w, s

q_rows_per_group = (num_heads // num_kv_heads) * head_dim
k_rows_per_group = head_dim
v_rows_per_group = v_head_dim
rows_per_group = q_rows_per_group + k_rows_per_group + v_rows_per_group
scale_rows_per_group = s_full.shape[0] // num_kv_heads
qs, ks, vs = [], [], []
for g_idx in range(tp_rank * kv_heads_per_rank, (tp_rank + 1) * kv_heads_per_rank):
row_start = g_idx * rows_per_group
scale_row_start = g_idx * scale_rows_per_group
# Dequantize this group's weights.
w_g = w_full[row_start : row_start + rows_per_group].to(torch.float32)
s_g = s_full[scale_row_start : scale_row_start + scale_rows_per_group].to(
torch.float32
)
s_g_expanded = s_g.repeat_interleave(block, dim=0).repeat_interleave(
block, dim=1
)[:rows_per_group]
w_g_dequant = w_g * s_g_expanded
# Track the dequantized q, k, and v weights separately.
qs.append(w_g_dequant[:q_rows_per_group])
ks.append(w_g_dequant[q_rows_per_group : q_rows_per_group + k_rows_per_group])
vs.append(w_g_dequant[q_rows_per_group + k_rows_per_group :])

# Combine the q, k, and v weights into the following layout:
# [Q_1, Q_2, .., Q_g, K_1, K_2, ..., K_g, V_1, V_2, ..., V_g]
grouped = torch.cat([torch.cat(qs), torch.cat(ks), torch.cat(vs)], dim=0)
# Quantize back to fp8.
return scaled_quantize(
grouped, GroupShape(block, block), w_full.dtype, compute_dtype=torch.float32
)


@support_torch_compile
class MiMoV2Model(nn.Module):
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
Expand Down Expand Up @@ -547,6 +630,10 @@ def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
params_dict = dict(self.named_parameters(remove_duplicate=False))
loaded_params: set[str] = set()
expert_params_mapping = self.get_expert_mapping()
# Pro-format fused qkv_proj arrives as two tensors (weight and
# weight_scale_inv). Store them per-layer so that they can be
# sharded together.
pending_fp8_qkv_proj: dict[str, dict[str, torch.Tensor]] = {}
for name, loaded_weight in weights:
if "rotary_emb.inv_freq" in name:
continue
Expand Down Expand Up @@ -590,11 +677,15 @@ def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
if expert_matched:
continue
# Support fused qkv_proj checkpoint (Pro format)
if "qkv_proj" in name:
if name in params_dict:
param = params_dict[name]
loaded_weight = loaded_weight.chunk(tp_size, dim=0)[tp_rank]
default_weight_loader(param, loaded_weight)
if self._try_load_fp8_qkv_proj(
name,
loaded_weight,
pending_fp8_qkv_proj,
params_dict,
loaded_params,
tp_rank,
tp_size,
):
continue
stacked_matched = False
for param_name, weight_name, shard_id in stacked_params_mapping:
Expand Down Expand Up @@ -652,6 +743,70 @@ def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:

return loaded_params

def _try_load_fp8_qkv_proj(
self,
name: str,
tensor: torch.Tensor,
fp8_qkv_proj_dict: dict[str, dict[str, torch.Tensor]],
params_dict: dict[str, torch.nn.Parameter],
loaded_params: set[str],
tp_rank: int,
tp_size: int,
) -> bool:
"""
The fused fp8 QKV projection weights and scale are stored separately.
Special care must be taken while sharding these tensors across TP ranks.
See _shard_fp8_qkv_proj for more details.

Returns:
True if ``tensor`` was an fp8 qkv_proj weight/scale and was consumed
(caller should skip it); False otherwise, so the caller falls
through to its normal loading path.
"""
is_weight = (
name.endswith("qkv_proj.weight") and tensor.dtype == torch.float8_e4m3fn
)
is_scale = name.endswith("qkv_proj.weight_scale_inv")
if not is_weight and not is_scale:
# Weight is not in FP8 format. Ignore.
return False

if is_pp_missing_parameter(name, self):
# This qkv_proj is for a layer not on this PP rank.
return True

prefix, qkv_kind = name.rsplit(".", 1)
entry = fp8_qkv_proj_dict.setdefault(prefix, {})
entry[qkv_kind] = tensor
if "weight" not in entry or "weight_scale_inv" not in entry:
# Still waiting for the other param.
return True
del fp8_qkv_proj_dict[prefix]

# Get self_attn module, which is a parent of qkv_proj.
attn = self.get_submodule(prefix.rsplit(".", 1)[0])

# Shard the qkv_proj per-rank.
w_rank, s_rank = _shard_fp8_qkv_proj(
entry["weight"],
entry["weight_scale_inv"],
num_heads=attn.total_num_heads,
num_kv_heads=attn.total_num_kv_heads,
head_dim=attn.head_dim,
v_head_dim=attn.v_head_dim,
tp_rank=tp_rank,
tp_size=tp_size,
)
sharded = {"weight": w_rank, "weight_scale_inv": s_rank}
for kind, tensor in sharded.items():
param_name = f"{prefix}.{kind}"
param = params_dict[param_name]
if tensor.shape[0] > param.shape[0]:
tensor = tensor[: param.shape[0]]
default_weight_loader(param, tensor)
loaded_params.add(param_name)
return True


class MiMoV2FlashForCausalLM(nn.Module, SupportsPP, MixtureOfExperts):
packed_modules_mapping = {
Expand Down
Loading