Repository navigation
test(mla): count dispatch paths per thread so tests survive parallelism - #3308
Merged
Merged
Conversation
The MLA dispatch counters are process-global, and model tests in the mlxcel crate asserted exact deltas from a snapshot() taken before and after a forward. Any MLA test running concurrently in the same test binary changed the delta, so these tests passed under --test-threads=1 and failed intermittently under the default parallel harness. record() now also bumps a thread-local tally, and the new mla::stats::scoped(f) returns f's result together with the calls recorded on the calling thread while f ran. Path selection happens while the graph is built, on the thread that calls forward, so a test's own forwards always land in its own scope. The global counters and take()/report_if_requested() are unchanged, so MLXCEL_TRACE_MLA_STATS=1 output from mlxcel generate, mlxcel-bench-decode and examples/logit_trace stays the same. Every snapshot() before/after pair in src/models/*_tests.rs (deepseek_v2, deepseek_v3, deepseek_v32, glm4_moe_lite, kimi_k3, kimi_linear, longcat_flash_ngram, longcat_flash_sparse) now uses scoped(). Two mlxcel-core unit tests cover two threads recording different paths concurrently and nested scopes across a global take(). Closes #3298
inureyes
force-pushed
the
fix/issue-3298-mla-stats-per-thread
branch
from
November 23, 2026 03:42
193f80f to
14df397
Compare
inureyes
marked this pull request as ready for review
November 23, 2026 03:46
inureyes
added a commit
that referenced
this pull request
Nov 23, 2026
…3306) Closes #3295 `DeepSeekV3Attention` and the LongCat ngram MLA rotated `q_pe` / `k_pe` with plain `base^(2i/d)` frequencies whatever `rope_scaling` declared, so every YaRN checkpoint (DeepSeek-V3 / Kimi-K2 `factor` 40 over 4096, LongCat-Flash-Lite `factor` 10 over 32768) rotated its low-frequency pairs `factor` times too fast past the original context. Only the softmax-scale correction read the block. ## Changes - New shared `MlaRope` (`src/models/mla_rope.rs`): resolves the block through `RopeScalingKind::resolve` (`deepseek_yarn` renamed to `yarn`, `--rope-*` / `--yarn-*` overrides honored), applies the table plus the YaRN magnitude to the rotated dims, and keeps the exact `fast_rope(.., base, 1.0, offset)` call when there is no block. - DeepSeek-V3 (`deepseek_v3`, `kimi_k2`, `joyai_llm_flash`, and through it Kimi-VL's text model and the V3 pipeline stage executor) and the LongCat ngram MLA resolve it once per model and duplicate it per layer. `original_max_position_embeddings` falls back to 4096 (no `train_ctx`), as upstream `YarnRoPE` does. - `DsaRope` (V3.2), the LongCat sparse rope and Xing4.0's opt-in table now use it, each keeping its own `train_ctx` (`None`, `max_position_embeddings`, `max_position_embeddings`). Xing4.0 still refuses `linear`. Youtu passes `MlaRope::plain` so its rotation stays plain. One plain-path change: the sparse rope's no-table rotation now follows `--rope-freq-base` like its table (it used `rope_theta` before). - Bilingual technical report in `TECHNICAL_REPORTS/3306-mla-yarn-rope-20261123.*.md`. - Docs: `supported-models.md` (V3, LongCat, Xing), the `--rope-*` coverage list in `llama-server-compat.md`, and the Used-by lists in `rope_utils.rs` / `rope_overrides.rs`, and the override error text. ## Validation Teacher-forced `logit_trace` against mlx-lm (`references/mlx-lm` 1bf8da0), with the pre-fix binary (main) as the negative arm: | Checkpoint | Region | Decided disagreements, this PR | Pre-fix | PPL this PR / ref | |---|---|---|---|---| | GigaChat3-10B-A1.8B (`deepseek_v3`, YaRN 64 over 4096), 8-bit | 8192 positions | 0 / 2774 | 989 / 2774 | 14.582 / 14.572 | | same | past 4096 | 0 / 1264 | 971 / 1264 | 16.688 / 16.652 | | longcat-flash-lite-4bit (YaRN 10 over 32768) | 36864 positions | 39 / 20486 (max gap 4.5) | 1709 / 20486 | 25.45 / 25.82 | | same | past 32768 | 6 / 2446 | 671 / 2446 | 17.92 / 17.49 | - GigaChat3 is quantized locally to affine 8-bit (`models/gigachat3-10b-a1.8b-mlx-8bit`) because the `deepseek_v3` loader does not load an unquantized MoE checkpoint; mlx-lm needed a shim to drop the MTP layer at index 26 (it hardcodes 61). - A single 36864-token mlx-lm forward on LongCat returns uniform logits past position 16384, so the LongCat reference is prefilled through mlx-lm's KV cache in 2048-token steps. That path agrees bit-for-bit with the single forward on the first 16384 positions, and mlxcel's one-piece and 2048-piece context prefill also agree bit-for-bit. The reference has no noise of its own (2048-step against 4096-step prefill: 0 disagreements), so the LongCat residual (gaps up to 4.5) sits above the reference's noise. It grows with position: 0 to 1 decided disagreements per 4096-position chunk below 12288, 4 to 10 from 12288 on, with no step at 32768. Its source is not identified, and the LongCat half of the first acceptance criterion is open on that point. - Byte-identical rows before and after: `moonlight-16b-a3b-instruct-4bit` (no block), `xing4.0-29b-a4b-mlx-8bit`, `longcat-flash-lite-sparse-4bit`. - Unit tests: tables against upstream `YarnRoPE` at the V3, GigaChat3 and LongCat geometries, the alias, byte-identity of the no-block path, the magnitude, a synthetic `deepseek_v3` YaRN config changing `q_pe` past the original context, YaRN reaching every layer and batched decode, and the ngram model carrying the table. I ran the narrow selectors `coverage_tests`, `mla_rope`, `deepseek_v3`, `deepseek_v32`, `xing4_0`, `longcat`, `kimi`, `youtu`, `rope_*` and the stage executor, plus clippy, fmt and the CI scripts. The broad workspace gate is left to CI. - `xing4_0_tests.rs` was edited only to follow the field rename (`rope_table` to `rope.freqs()`, `with_rope_table` to `with_rope`). The assertions are unchanged. - Three LongCat latent tests fail only when run in parallel, with or without this change, because they assert on deltas of the shared global MLA stats counters. #3298 (now merged as #3308) addresses that.
inureyes
added a commit
that referenced
this pull request
Nov 23, 2026
Brings in eleven commits from main. The only conflict was docs/supported-models.md, where main reworded the Xing4.0 entry next to the Hy4 one: main's Xing4.0 text and the Hy4 entry are both kept. The shared DeepSeek MLA RoPE change (#3306) and the per-thread MLA dispatch counters (#3308) merged cleanly under the Hy4 attention. Refs #2354
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The MLA dispatch counters in
mlxcel_core::mla::statsare process-global, and model tests in themlxcelcrate asserted exact deltas from asnapshot()taken before and after a forward. Any MLA test running concurrently in the same test binary changed the delta, so these tests passed under--test-threads=1and failed intermittently under the default parallel harness.Changes
src/lib/mlxcel-core/src/mla/stats.rs:record()also bumps a thread-local tally that is never reset. Newpub fn scoped<R>(f: impl FnOnce() -> R) -> (R, MlaDispatchCounts)returnsf's result and the calls recorded on the calling thread whilefran. Scopes nest, and a globaltake()inside a scope does not disturb it. Path selection runs while the graph is built, on the thread that callsforward, so a test's own forwards land in its own scope. The global counters,snapshot(),take()andreport_if_requested()are unchanged.snapshot()before/after pair insrc/models/*_tests.rsnow usesscoped():deepseek_v2_tests,deepseek_v2_latent_option_tests,deepseek_v3_latent_tests,deepseek_v32_latent_tests,deepseek_v32_latent_option_tests,glm4_moe_lite_latent_tests,glm4_moe_lite_latent_option_tests,kimi_k3_latent_tests,kimi_linear_latent_tests(itspath_deltahelper is gone),longcat_flash_ngram_latent_testsandlongcat_flash_sparse_tests. The assertions themselves (exact counts and lower bounds) are unchanged.scopedresult holds only its own thread's calls (a before/aftersnapshot()would see both), and nested scopes across a globaltake().docs/mla-absorbed-decode.md: one paragraph telling test authors to usescopedrather than asnapshot()pair.The crate-wide test mutex alternative was rejected in the issue, since every new MLA test would have to remember to take it.
Test plan
cargo test -p mlxcel-core --profile test-fast --features metal,accelerate --lib mla::stats(4 passed)cargo clippy --lib --tests --features metal,accelerate -p mlxcel-core -p mlxcel -- -D warningsmlxcellib test binary filtered bylatent sparse_decode glm4_moe_lite(161 tests), under the GPU lock: 10 of 10 passed (158 passed, 3 ignored each)models::deepseek_v2 models::deepseek_v3 models::kimi_linear models::kimi_k3 models::longcat_flash, 173 tests): 10 of 10 passedcheck_used_by,check_kernel_dtype_keys,check_env_var_docs,check_crate_versions,check_kernel_port_dispatch,check_binary_assets, Apache header check,--lib coverage_testsMLXCEL_TRACE_MLA_STATS=1output is unchanged by construction:report_if_requested()still reads and resets the same global atomics, andrecord()still increments them first.Closes #3298