Skip to content

refactor(models): share one MLA core between the bailing families - #3789

Open
inureyes wants to merge 3 commits into
mainfrom
refactor/3782-shared-bailing-mla
Open

inureyes wants to merge 3 commits into
mainfrom
refactor/3782-shared-bailing-mla

Conversation

@inureyes

@inureyes inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Summary

bailing_hybrid::MultiLatentAttention and bailing_moe_v3::GatedMlaAttention now wrap one pub(crate) core, BailingMla in src/models/bailing_mla.rs, so a fix to the MLA forward (like #3736's embed_q layout fix) lands once. The core takes the three things that differ between the families: MlaSpec::rope_traditional, a FoldedMask argument (hybrid passes the caller mask or causal mode, v3 always causal), and MlaSpec::output_gate (v3's per-head sigmoid(g_proj(x)) before dense). Each family still loads its own absorbed planes through a closure: hybrid still splits a raw kv_b_proj, and v3 still reads only the pre-split planes. Weight keys, supported checkpoints and type names are unchanged. mla_rope.rs and rope_folded_attention are untouched.

Parity evidence

  • The first commit adds a parity test per family, recorded against main's implementation. A test-only frozen oracle (bailing_mla_parity_oracle.rs) reproduces the pre-refactor forward op for op. Each test runs the real module and the oracle on fixed-seed weights for a 3-token prefill and two cached decode steps, and requires max_abs_diff == 0 with identical f32 bits. The tests cover both q_lora_rank forms. For hybrid they also cover both RoPE layouts and both mask sources, and for v3 the gate. I compared against the oracle rather than a stored fixture because a fixture recorded on one backend would not stay bit-exact on the other CI backends.
  • After the refactor both tests still pass. Mutation check: flipping the RoPE layout in the core, or dropping the caller mask, makes them fail (max_abs_diff 0.02 to 0.07). Two test settings were needed to make the mutations visible: a RoPE width of 4 (at width 2 the two layouts are the same rotation) and a caller mask that is not plain causal.
  • A one-off, uncommitted logit dump of both tiny models (prefill plus two decode steps; v3 with q_lora_rank set and null, hybrid with two kv_lora_rank values) was bit-identical between main and this branch.

Test plan

  • cargo test --profile test-fast --features cuda -p mlxcel --lib -- bailing mla (200 passed, after merging current main)
  • cargo clippy --all-targets -- -D warnings on default features and --features cuda, cargo fmt --check, check_env_var_docs.py, mlx_test_guard_coverage

Not run: a real-checkpoint greedy generate comparison. No bailing_hybrid or bailing_moe_v3 checkpoint is available on the test hosts.

Closes #3782

Before folding bailing_hybrid's MultiLatentAttention and bailing_moe_v3's GatedMlaAttention into one shared core (#3782), record their current behavior. A test-only oracle reproduces the pre-refactor MLA forward op for op on the same layer types, and each family's parity test runs its real attention module and the oracle on fixed-seed weights for a three-token prefill and two cached decode steps, requiring bit-identical outputs (max_abs_diff == 0 and equal f32 bits).

Coverage: both query forms (q_lora_rank set and unset) for both families, both RoPE pair layouts and both mask sources (caller mask and the layer's causal mode) for bailing_hybrid, and the per-head output gate for bailing_moe_v3. The rotary width is 4 so the two RoPE pair layouts actually differ, and the caller mask is not plain causal so a layer that dropped it would diverge. Comparing against the oracle rather than a stored fixture keeps the check exact on every CI backend.

Passes on main's implementation (CUDA, GB10).

Refs #3782
bailing_moe_v3's GatedMlaAttention repeated about 80 lines of bailing_hybrid's MultiLatentAttention forward, so a fix such as #3736's embed_q layout change had to be made twice. Both now wrap a pub(crate) BailingMla in src/models/bailing_mla.rs that owns the shared fields, the loading and the forward. It is parameterized by the three places the families differ: MlaSpec::rope_traditional (hybrid reads rope_interleave, v3 is always interleaved), the FoldedMask argument (the hybrid caller mask or causal mode, v3's fixed causal mode), and MlaSpec::output_gate, the per-head sigmoid(g_proj(x)) gate applied before dense. The absorbed-plane loading stays per family through a closure: hybrid still splits a raw kv_b_proj, v3 still reads only the pre-split planes.

Weight keys, supported checkpoints and public types are unchanged. sigmoid_f32_as moves into the core, and v3's KDA gate imports it from there. The hybrid prefill planner reads the layer shape through BailingMla::prefill_attention.

Validation (CUDA, GB10): the parity tests from the previous commit, recorded against main's implementation, still pass bit for bit, and flipping the RoPE layout or dropping the caller mask in the core makes them fail. A one-off logit dump of both tiny models (prefill plus two decode steps, both q_lora forms for v3, two kv_lora_rank values for hybrid) was bit-identical before and after. All 146 bailing lib tests pass, and clippy passes on default features and with --features cuda.

Closes #3782
@inureyes inureyes added type:refactor Code restructuring without changing functionality priority:low Low priority area:models Model architectures, weights, loading, metadata status:review Under review labels Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:low Low priority status:review Under review type:refactor Code restructuring without changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(models): share one MLA attention core between bailing_hybrid and bailing_moe_v3

1 participant