Repository navigation
[Bugfix][Model] GLM-5.x: load ModelOpt NVFP4 MLA projections into BF16 fused_qkv_a_proj #59833
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
Closed
avifenesh
wants to merge
4
commits into
vllm-project:main
from
avifenesh:glm5next-nvfp4-mla-proj-load
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a7e091
[Bugfix][Model] GLM-5.x: load ModelOpt NVFP4 MLA projections into BF1…
avifenesh be8e956
Ignore unused NVFP4 input scales for BF16 MLA projections
avifenesh 78169eb
Merge upstream main to refresh NVFP4 MLA loader fix
avifenesh 0f2bb87
[Bugfix][Model] Preserve FP8 and native quantized scale routing
avifenesh 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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Loading ModelOpt NVFP4 MLA projections into GLM-5.x's BF16 fused_qkv_a_proj.""" | ||
|
|
||
| import torch | ||
|
|
||
| from vllm.models.glm5next.common.model import _try_load_nvfp4_attn_proj | ||
|
|
||
| E2M1 = torch.tensor( | ||
| [0, 0.5, 1, 1.5, 2, 3, 4, 6, -0.0, -0.5, -1, -1.5, -2, -3, -4, -6], | ||
| dtype=torch.float32, | ||
| ) | ||
| PREFIX = "model.layers.3.self_attn" | ||
|
|
||
|
|
||
| class _RecordingParam: | ||
| def __init__(self): | ||
| self.calls: list = [] | ||
|
|
||
| def weight_loader(self, param, weight, shard_id=None): | ||
| self.calls.append((weight, shard_id)) | ||
|
|
||
|
|
||
| def _nvfp4(out_dim: int, in_dim: int, seed: int): | ||
| g = torch.Generator().manual_seed(seed) | ||
| packed = torch.randint( | ||
| 0, 256, (out_dim, in_dim // 2), dtype=torch.uint8, generator=g | ||
| ) | ||
| scale = (torch.rand(out_dim, in_dim // 16, generator=g) * 4 + 0.25).to( | ||
| torch.float8_e4m3fn | ||
| ) | ||
| gscale = torch.tensor(0.0123, dtype=torch.float32) | ||
| codes = torch.stack([packed & 0xF, packed >> 4], dim=-1).reshape(out_dim, -1).long() | ||
| vals = E2M1[codes].view(out_dim, -1, 16) | ||
| ref = (vals * (scale.float() * gscale).unsqueeze(-1)).reshape(out_dim, -1) | ||
| return packed, scale, gscale, ref.to(torch.bfloat16) | ||
|
|
||
|
|
||
| def _feed(proj, packed, scale, gscale, params, buf, loaded, pad=0): | ||
| names = [ | ||
| (f"{PREFIX}.{proj}.weight_scale", scale), | ||
| (f"{PREFIX}.{proj}.weight", packed), | ||
| (f"{PREFIX}.{proj}.weight_scale_2", gscale), | ||
| ] | ||
| return [_try_load_nvfp4_attn_proj(n, t, buf, params, loaded, pad) for n, t in names] | ||
|
|
||
|
|
||
| def test_nvfp4_q_a_and_kv_a_dequantize_into_bf16_fused_shards(): | ||
| fused = _RecordingParam() | ||
| params = {f"{PREFIX}.fused_qkv_a_proj.weight": fused} | ||
| buf: dict = {} | ||
| loaded: set[str] = set() | ||
|
|
||
| q_packed, q_scale, q_gs, q_ref = _nvfp4(64, 128, seed=0) | ||
| kv_packed, kv_scale, kv_gs, kv_ref = _nvfp4(32, 128, seed=1) | ||
| assert all(_feed("q_a_proj", q_packed, q_scale, q_gs, params, buf, loaded, 8)) | ||
| assert all( | ||
| _feed("kv_a_proj_with_mqa", kv_packed, kv_scale, kv_gs, params, buf, loaded, 8) | ||
| ) | ||
|
|
||
| (q_w, q_shard), (kv_w, kv_shard) = fused.calls | ||
| assert (q_shard, kv_shard) == (0, 1) | ||
| assert q_w.dtype == torch.bfloat16 | ||
| torch.testing.assert_close(q_w, q_ref) | ||
| # NoPE models pad the kv_a rope rows with zeros; q_a is not padded. | ||
| torch.testing.assert_close(kv_w[:32], kv_ref) | ||
| assert kv_w.shape == (40, 128) and not kv_w[32:].any() | ||
| assert loaded == {f"{PREFIX}.fused_qkv_a_proj.weight"} | ||
|
|
||
|
|
||
| def test_nvfp4_projection_with_quantized_target_uses_normal_path(): | ||
| params = { | ||
| f"{PREFIX}.q_b_proj.weight": _RecordingParam(), | ||
| f"{PREFIX}.q_b_proj.weight_scale": _RecordingParam(), | ||
| } | ||
| packed, scale, gscale, _ = _nvfp4(64, 128, seed=2) | ||
| assert not any(_feed("q_b_proj", packed, scale, gscale, params, {}, set())) | ||
|
|
||
|
|
||
| def test_fp8_and_bf16_tensors_are_left_alone(): | ||
| params = {f"{PREFIX}.fused_qkv_a_proj.weight": _RecordingParam()} | ||
| fp8 = torch.zeros(64, 128, dtype=torch.float8_e4m3fn) | ||
| bf16 = torch.zeros(64, 128, dtype=torch.bfloat16) | ||
| for t in (fp8, bf16): | ||
| assert not _try_load_nvfp4_attn_proj( | ||
| f"{PREFIX}.q_a_proj.weight", t, {}, params, set(), 0 | ||
| ) |
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.
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.
Could we also ignore
input_scalewhen the target projection is kept in BF16?vroomfondel/GLM-5.3-Flash-NVFP4-W4A4stores it forq_b_projando_proj. Both are built withquant_config=None, so their weights are dequantized here, butinput_scalefalls through toparams_dict[name]and raises aKeyError.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.
Fixed in be8e956. The shared main-model/MTP helper now consumes
input_scalewhen the target has no activation-scale parameter. Quantized targets and registered scales keep the normal loader path.The regression suite covers
q_b_proj,o_projand the fused projections, with activation scales before and after weight loading: 6 passed. The added BF16 cases failed before the fix. A separate CPU reproducer through both public loaders raised the reportedKeyErrorin all 8 baseline cases; all 8 patched cases loaded and matched the BF16 weight and projection-output references exactly. Pre-commit and mypy-3.12 passed.I have not rerun full serving of
vroomfondel/GLM-5.3-Flash-NVFP4-W4A4.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.
Confirmed in
be8e956a69; theinput_scalecase is covered now.