Skip to content

fix(models): read bailing_hybrid embed_q as [H, kv_lora_rank, qk_nope_head_dim] - #3736

Merged
inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/bailing-hybrid-embed-q-shape
Dec 4, 2026
Merged

inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/bailing-hybrid-embed-q-shape

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #3735.

bailing_hybrid::validate_weights checked the absorbed MLA embed_q plane as [H, qk_nope_head_dim, kv_lora_rank]. The plane is [H, kv_lora_rank, qk_nope_head_dim]: split_kv_b_proj builds it that way, mlx-lm's MultiLinear(qk_nope_head_dim, kv_lora_rank, H) stores it that way, and the MLA forward consumes it that way. Any config whose two widths differ was refused at load, which includes Ling-2.6-flash (kv_lora_rank 512, qk_nope_head_dim 128), the checkpoint this family was written for. The check now passes (kv_lora_rank, qk_nope_head_dim) as (out_features, in_features).

What changed

  • src/models/bailing_hybrid.rs, validate_weights: the embed_q call to validate_multi_linear swaps its out_features / in_features arguments, with a comment naming each plane's layout. The unembed_out check ([H, v_head_dim, kv_lora_rank]) was already right and is unchanged.
  • src/models/bailing_hybrid_tests.rs:
    • validate_weights_reads_embed_q_as_kv_lora_by_nope walks (kv_lora_rank, qk_nope_head_dim) = (2, 2), (4, 2), (2, 4), (8, 2) and (8, 1), the last two being Ling-2.6-flash's 4:1 ratio and a more extreme one. For each it checks the pre-split layout the mlx-community conversion ships, that the transposed plane is still refused whenever the widths differ, and a raw kv_b_proj that sanitize_weights splits itself (asserting both split shapes).
    • synthetic_forward_runs_with_kv_lora_rank_unequal_to_nope loads the tiny model with kv_lora_rank 8 and qk_nope_head_dim 2 through from_weights and runs a 3-token prefill (materializing MLA path) and a decode step (absorbed path), checking shapes and finiteness.
  • docs/supported-models.md (Ling-2.6-flash entry) and the sanitize_weights doc comment: the published mlx-community/Ling-2.6-flash-mlx-4bit (revision bd458afd) ships embed_q / unembed_out 4-bit affine-quantized (embed_q.weight is U32 [32, 512, 16] with scales / biases [32, 512, 2]), not unquantized as both said; the entry now records the real-checkpoint run below instead of "unit-tested only". The existing sentences about current mlx-lm's quant_predicate are kept.

Other callers: validate_multi_linear is private to bailing_hybrid.rs and has exactly these two call sites. No other module validates a bailing_hybrid embed_q.

Verification

NVIDIA GB10 (sm_121), driver 580.178.04, CUDA 13.0.88, Linux 7.0.0-1019-nvidia aarch64, rustc 1.97.1, --features cuda, --profile test-fast. Base 9402cf893. Commit tree cef9852d; the gate and checks below ran on tree 2afacb93, which differs from it only in CHANGELOG.md (an entry since removed).

Tests

cargo test --locked --profile test-fast --features cuda --lib -- --test-threads=1 bailing_hybrid::

main + the new tests fix
validate_weights_reads_embed_q_as_kv_lora_by_nope FAIL: kv_lora_rank 4, qk_nope_head_dim 2: unexpected model.layers.1.attention.embed_q.weight shape [2, 4, 2]: expected [2, 2, 4] (per-head absorbed MLA plane) pass
synthetic_forward_runs_with_kv_lora_rank_unequal_to_nope FAIL: unexpected model.layers.1.attention.embed_q.weight shape [2, 8, 2]: expected [2, 2, 8] (per-head absorbed MLA plane) pass
bailing_hybrid:: total 24 passed, 2 failed 26 passed, 0 failed

The fix column ran on tree e6e1a719, which differs from 2afacb93 only in docs/supported-models.md, the sanitize_weights doc comment and CHANGELOG.md; the full gate below ran on 2afacb93.

Real checkpoint

mlx-community/Ling-2.6-flash-mlx-4bit, revision bd458afdc97da0f60046f76f5ec96f204b4fb27f (58.6 GB; kv_lora_rank 512, qk_nope_head_dim 128). mlxcel generate -m <dir> -p "The capital of France is" -n 32 --temp 0 --no-chat-template, binaries built with --profile test-fast --features cuda from main and from the fix (different sha256):

  • main: exits 1 after 1.2 s, before any weight is materialized:
    Error: unexpected model.layers.7.attention.embed_q.weight shape [32, 512, 16]: expected [32, 128, 512] (per-head absorbed MLA plane)
  • fix: loads (1,178 tensors, 58.6 GB materialized, 79 s) and generates 32 tokens at 13.2 tok/s:
    \nA.Paris\nB.London\nC.Berlin\nD.Madrid\nA\nThe problem is asking for the identification of the capital city
  • mlx-lm 0.32.2 (mlx 0.32.5, CUDA) on the same directory, stream_generate(..., max_tokens=32, sampler=make_sampler(temp=0.0)), prompt ids [678, 7706, 300, 11406, 341]: the same 32-token text, byte for byte.

Checks

  • cargo fmt --all -- --check
  • cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings
  • python3 scripts/ci/check_used_by.py (used-by: OK, 78 top-level pub fn in 2 shared files, 57 annotated, 715 caller-file references resolved, 3498 Rust files scanned), bash scripts/ci/check_used_by_test.sh (all cases passed)
  • make verify-test-cuda with a private 0700 TMPDIR, tree 2afacb93 before and after: 18,893 passed, 2 failed, 578 ignored over 207 test result lines. Both failures were pre-existing on the base 9402cf893 and are unrelated to this change; current main fixes both (feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262) #3686, test: restore MLX guard scan and no-GPU catalog test gates #3765):
    • tests/mlx_test_guard_coverage.rs mlx_evaluating_lib_tests_take_mlx_test_guard: src/vision/encoders/nemotron_h_nano_omni_tests.rs:276 pos_embed_resample_runs_in_f32 evaluates MLX without the guard. The two new tests here are not listed.
    • tests/step3p7_parity.rs text_decode_delegates_sequence_local_native_batching: assertion failed: !wrapper.supports_padded_prefill().
      On unmodified main (9402cf893), cargo test --locked --profile test-fast --features cuda --test mlx_test_guard_coverage --test step3p7_parity -- --test-threads=1 fails both the same way (5 passed / 1 failed and 7 passed / 1 failed, same messages).

git merge-tree merges the commit onto current origin/main (5bf7b8628) without conflict. On the merged tree, the Python tooling tests (python3 -m unittest discover -s tests -p 'test_*.py'), the scripts/ci checks CI runs and cargo fmt --all -- --check pass.

Limits

  • One real prompt, 5 tokens in and 32 out, compared with mlx-lm as text. Longer prompts (the chunked GLA prefill path), batched decode and the server were not run on the real checkpoint.
  • While loading the real checkpoint, mlxcel::quant warns once per MoE layer that model.layers.N.mlp.gate.gate_proj is declared 4-bit but packed as 8-bit, and uses the shape-derived 8 bits (config.json declares those routers 8-bit per layer). The output still matches mlx-lm. The warning is independent of this change and was not investigated.
  • Not run on Metal.

@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) labels Dec 4, 2026
…bed-q-shape

# Conflicts:
#	docs/supported-models.md
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3735. The fix is correct and complete; no code changes were needed beyond merging main.

  • Layout: mlx-lm's MultiLinear(input_dims, output_dims, num_heads) stores [num_heads, output_dims, input_dims], and embed_q = MultiLinear(qk_nope_head_dim, kv_lora_rank, H), so the plane is [H, kv_lora_rank, qk_nope_head_dim]. Every consumer here agrees: split_kv_b_proj produces that shape, the decode path's embed_q.forward(q_nope) maps nope to kv_lora, and the prefill path's embed_q.forward_no_transpose(kv_latent) maps kv_lora to nope. Only validate_weights had the axes swapped. bailing_moe_v3::validate_weights (feat(models): support Ling-3.0 (BailingMoeV3ForCausalLM) #3728) already passes (kv_lora_rank, qk_nope_head_dim), so the two families now agree.
  • Tests: with src/models/bailing_hybrid.rs reverted to main, both new tests fail (validate_weights_reads_embed_q_as_kv_lora_by_nope, synthetic_forward_runs_with_kv_lora_rank_unequal_to_nope; 24 passed, 2 failed). With the fix, all bailing_ lib tests pass (142, CUDA, GB10). The forward test uses kv_lora_rank 8 against qk_nope_head_dim 2, so a transposition in any consumer would surface as a shape error.

Pushed a merge of current main (resolving the docs/supported-models.md conflict with the #3728 entry: kept main's V3 dispatch wording and this PR's quantized-planes and real-checkpoint sentences).

Validation on the merged tree: cargo fmt --check, cargo clippy --all-targets --features cuda -D warnings, tests/mlx_test_guard_coverage.rs, check_env_var_docs.py, check_used_by.py. No bailing_hybrid checkpoint is present on the maintainer hosts, so the real-checkpoint run in the PR body was not repeated.

Follow-up note, not done here: bailing_moe_v3::GatedMlaAttention::attend duplicates roughly 80 lines of bailing_hybrid's MLA forward (query projection, latent split, RoPE, cache update, absorbed vs materialized branch). The differences are the RoPE traditional flag, the mask source and the trailing per-head gate, so a shared MLA core parameterized on those three would remove the duplicate.

@inureyes
inureyes merged commit d179bb6 into lablup:main Dec 4, 2026
36 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): bailing_hybrid validates embed_q with its last two axes swapped, blocking Ling-2.6-flash

2 participants