Skip to content

perf(models): keep quantized models bf16 to fix M1 Ultra decode regression (#289) - #290

Merged
inureyes merged 1 commit into
mainfrom
289-bf16-quant-scale-decode-perf
Jun 15, 2026
Merged

inureyes merged 1 commit into
mainfrom
289-bf16-quant-scale-decode-perf

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Fixes #289. The full M1 Ultra benchmark refresh (v0.1.4 → v0.2.1) surfaced a real, model-specific decode regression. Root cause: commit d80f78721 (#260 "add Apertus") added convert_quant_scales_bf16_to_f16 in load_text_weights, which promotes bf16 .scales/.biases → f16 for all quantized models. Promoting only the scales while activations stay bf16 creates a bf16-activation × f16-scale dtype mismatch in quantized_matmul/gather_qmm, regressing decode 33-41% on M1 Ultra for every checkpoint shipping bf16 scales.

Evidence (M1 Ultra, decode tok/s)

Control-gated, median-of-3 bisect (mixtral as a thermal control; M1 Ultra throttles under sustained load) — clean step at #260:

commit qwen3-30b-a3b verdict
926f9a45a (#259) 69.1 good
d80f78721 (#260) 44.4 first bad

Run-over-run regressions (all bf16-scale): qwen3-30b-a3b 70→46, qwen3-moe 70→46, qwen3-0.6b 290→247, nemotron-h 91→53, nemotron-nas 90→51, gpt-oss-120b, solar-100b, minimax-m2, hunyuan-large, jamba, dots.llm1. Controls (f16 scales) unchanged: mixtral 54→54, llama-3.1-8b 107→109, qwen2.5-7b 110→112, phi-4, starcoder2. The split matches the safetensors .scales dtype exactly. Not the fused-MoE kernel (MLXCEL_FUSED_MOE on/off identical) and not MLX (pin a6ec712 across the range).

Fix

Leave quantized models fully bf16 (the pre-#260 fast path). The promotion was not needed for correctness:

  • Apertus-2509 and Seed-OSS decode correctly with bf16 scales. The blank output once attributed to their bf16 scales was the separate xIELU read_scalar bf16 bug, fixed in the apertus loader (kept).
  • Promoting all tensors to f16 instead corrupts Apertus (xIELU x² overflows f16, like BitNet's relu²), so "convert everything to f16" is not viable either.

Validation (M1 Ultra, this branch)

  • Perf recovered: qwen3-30b-a3b 46 → 84, nemotron-h-30b 53 → 92 (≥ the v0.1.4 baseline).
  • Correctness preserved: tests/apertus_parity.rs (real-model greedy " Paris,") passes; Seed-OSS, dots.llm1, granite-4.0-h hybrid, and lfm2 decode correctly under the fix.
  • cargo fmt --check, cargo clippy --release --all-targets -- -D warnings, and cargo test --release --lib sanitize (83 passed) all green.

Notes

  • Also reflows one doc comment in granitemoehybrid_tests.rs so a wrapped + is not at a line start (pre-existing clippy::doc_lazy_continuation surfaced by --all-targets; unrelated to the fix, included to keep the lint gate green).
  • Likely affects other Apple Silicon (the promotion fires on all Apple Silicon); magnitude may differ by generation. The M1 Ultra benchmark docs will be refreshed on the fixed binary separately.

…ssion (#289)

#260 added a blanket bf16->f16 promotion of quant scales/biases for all
quantized models (convert_quant_scales_bf16_to_f16 in load_text_weights).
Promoting only the scales while activations stay bf16 creates a
bf16-activation x f16-scale dtype mismatch in quantized_matmul/gather_qmm
that regressed decode 33-41% on M1 Ultra for every bf16-scale checkpoint:
qwen3-30b-a3b 70->46, nemotron-h 91->53, plus qwen3-0.6b, gpt-oss-120b,
solar-100b, minimax, hunyuan-large, jamba, dots. f16-scale checkpoints
(mixtral, llama-3.1, qwen2.5, phi-4) were unaffected. Bisected to
d80f787 (#260); not the fused-MoE kernel and not the MLX pin.

The promotion was unnecessary for correctness. Apertus-2509 and Seed-OSS
decode correctly with bf16 scales; the blank output once attributed to
their bf16 scales was the separate xIELU read_scalar bf16 bug, fixed in
the apertus loader. Promoting every tensor to f16 instead corrupts
Apertus (xIELU x^2 overflows f16, like BitNet's relu^2).

Leave quantized models fully bf16 (the pre-#260 fast path). On M1 Ultra
this restores qwen3-30b-a3b to ~84 and nemotron-h to ~92 tok/s; the
apertus_parity real-model test still passes and Seed-OSS / dots.llm1 /
granite hybrid / lfm2 decode correctly.

Also reflow a doc comment in granitemoehybrid_tests.rs so a wrapped '+'
is not at a line start (pre-existing clippy::doc_lazy_continuation under
--all-targets; unrelated to the fix, needed for a green lint gate).
@inureyes inureyes added type:performance Performance improvements priority:high High priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific labels Jun 15, 2026
@inureyes
inureyes merged commit 58d1c58 into main Jun 15, 2026
4 checks passed
@inureyes
inureyes deleted the 289-bf16-quant-scale-decode-perf branch June 15, 2026 02:51
inureyes added a commit that referenced this pull request Jun 15, 2026
Full M1 Ultra + M5 Max text/VLM re-benchmark on mlxcel 0.2.1 (post #289 bf16-scale fix and #291 quantized-embedding fix). Regenerates model_tests_m1ultra.md, model_tests_m5max.md, and the shared cross-hardware index from the new CSVs; adds the 2026-06-15 M1 Ultra and M5 Max CSVs plus the pre-#290 regression-evidence sweep. Both Apple Silicon now on 0.2.1 + MLX pin a6ec712; M5 Max ~1.76x faster than M1 Ultra (avg) on the selected rows.
@inureyes inureyes added this to the 0.3 milestone Jun 21, 2026
inureyes added a commit that referenced this pull request Jun 22, 2026
Characterizes the per-model dense decode overhead for apertus and seed_oss versus mlx-lm and recovers the one cheap, bit-exact lever found, satisfying the acceptance criteria for #269.

Characterization (M1 Ultra, mlxcel-bench-decode warmup harness, temp-0, --no-chat-template, 100 decode tokens, mlx-lm 0.31.3 baselines from the issue). The bulk of the gap reported in #269 was the bf16-scale quantized decode regression (#289), which was fixed by #290 after this issue was filed. Both families ship bf16 quant scales and were in the regressed set, so current main already closes most of the gap: apertus is ~83.5 tok/s vs mlx-lm ~85 (~0.98x, was 0.83x at filing) and seed_oss is ~20.4 tok/s vs mlx-lm ~20.3 (~1.0x, was 0.84x at filing). The remaining apertus delta is diffuse per-op / kernel-launch overhead with no single high-value host-side lever: MLXCEL_PROFILE_FORWARD shows steady-state decode is GPU-eval-bound (~0.7ms host graph-build vs ~13ms GPU eval per token, host build is ~5% of the step), and MLXCEL_PROFILE_BLOCKS attributes the forced-serialized per-layer time as attn ~45% / xIELU MLP ~55% for apertus and attn ~38% / SwiGLU MLP ~62% for seed_oss. seed_oss is a vanilla Llama dense and carries no model-specific lever beyond what #290 already recovered.

Adds an env-gated, default-off forward profiling hook to apertus and seed_oss, mirroring the existing hooks in nemotron_h.rs and qwen3_moe.rs. MLXCEL_PROFILE_BLOCKS attributes single-token decode wall-clock to the attention vs MLP sub-blocks; MLXCEL_PROFILE_FORWARD splits the step into graph-build vs GPU-eval. Both flags are read once and cached in a OnceLock, and array_shape is only queried when a flag is set, so the steady-state decode path is unchanged (two atomic loads per token, no extra FFI calls or eval()).

Recovers one cheap, bit-exact op in apertus xIELU: both branches end in `+ beta * x`, so that shared term is now added once after the per-element select instead of inside each branch. This drops one elementwise add (and its kernel launch) per layer per token. The select only chooses which value beta*x is added to, so the arithmetic is unchanged. Verified greedy temp-0 output is byte-identical to the pre-change baseline for both apertus (500 bytes) and seed_oss (308 bytes); the decode delta is within run-to-run noise (~83.3 -> ~83.5 tok/s on apertus), consistent with the GPU-eval-bound profile. A fused/compiled xIELU activation (single kernel over the elementwise graph) is the only remaining real lever and is left as a documented follow-up, since it touches the C++ FFI bridge and risks f16 jitter.

Adds a CPU-only unit test (xielu_factored_beta_x_matches_branch_local) asserting the factored form equals the branch-local reference bit-for-bit in f32 across positive, negative, and zero inputs.
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…ssion (lablup#289) (lablup#290)

lablup#260 added a blanket bf16->f16 promotion of quant scales/biases for all
quantized models (convert_quant_scales_bf16_to_f16 in load_text_weights).
Promoting only the scales while activations stay bf16 creates a
bf16-activation x f16-scale dtype mismatch in quantized_matmul/gather_qmm
that regressed decode 33-41% on M1 Ultra for every bf16-scale checkpoint:
qwen3-30b-a3b 70->46, nemotron-h 91->53, plus qwen3-0.6b, gpt-oss-120b,
solar-100b, minimax, hunyuan-large, jamba, dots. f16-scale checkpoints
(mixtral, llama-3.1, qwen2.5, phi-4) were unaffected. Bisected to
d80f787 (lablup#260); not the fused-MoE kernel and not the MLX pin.

The promotion was unnecessary for correctness. Apertus-2509 and Seed-OSS
decode correctly with bf16 scales; the blank output once attributed to
their bf16 scales was the separate xIELU read_scalar bf16 bug, fixed in
the apertus loader. Promoting every tensor to f16 instead corrupts
Apertus (xIELU x^2 overflows f16, like BitNet's relu^2).

Leave quantized models fully bf16 (the pre-lablup#260 fast path). On M1 Ultra
this restores qwen3-30b-a3b to ~84 and nemotron-h to ~92 tok/s; the
apertus_parity real-model test still passes and Seed-OSS / dots.llm1 /
granite hybrid / lfm2 decode correctly.

Also reflow a doc comment in granitemoehybrid_tests.rs so a wrapped '+'
is not at a line start (pre-existing clippy::doc_lazy_continuation under
--all-targets; unrelated to the fix, needed for a green lint gate).
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
)

Full M1 Ultra + M5 Max text/VLM re-benchmark on mlxcel 0.2.1 (post lablup#289 bf16-scale fix and lablup#291 quantized-embedding fix). Regenerates model_tests_m1ultra.md, model_tests_m5max.md, and the shared cross-hardware index from the new CSVs; adds the 2026-06-15 M1 Ultra and M5 Max CSVs plus the pre-lablup#290 regression-evidence sweep. Both Apple Silicon now on 0.2.1 + MLX pin a6ec712; M5 Max ~1.76x faster than M1 Ultra (avg) on the selected rows.
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…up#399)

Characterizes the per-model dense decode overhead for apertus and seed_oss versus mlx-lm and recovers the one cheap, bit-exact lever found, satisfying the acceptance criteria for lablup#269.

Characterization (M1 Ultra, mlxcel-bench-decode warmup harness, temp-0, --no-chat-template, 100 decode tokens, mlx-lm 0.31.3 baselines from the issue). The bulk of the gap reported in lablup#269 was the bf16-scale quantized decode regression (lablup#289), which was fixed by lablup#290 after this issue was filed. Both families ship bf16 quant scales and were in the regressed set, so current main already closes most of the gap: apertus is ~83.5 tok/s vs mlx-lm ~85 (~0.98x, was 0.83x at filing) and seed_oss is ~20.4 tok/s vs mlx-lm ~20.3 (~1.0x, was 0.84x at filing). The remaining apertus delta is diffuse per-op / kernel-launch overhead with no single high-value host-side lever: MLXCEL_PROFILE_FORWARD shows steady-state decode is GPU-eval-bound (~0.7ms host graph-build vs ~13ms GPU eval per token, host build is ~5% of the step), and MLXCEL_PROFILE_BLOCKS attributes the forced-serialized per-layer time as attn ~45% / xIELU MLP ~55% for apertus and attn ~38% / SwiGLU MLP ~62% for seed_oss. seed_oss is a vanilla Llama dense and carries no model-specific lever beyond what lablup#290 already recovered.

Adds an env-gated, default-off forward profiling hook to apertus and seed_oss, mirroring the existing hooks in nemotron_h.rs and qwen3_moe.rs. MLXCEL_PROFILE_BLOCKS attributes single-token decode wall-clock to the attention vs MLP sub-blocks; MLXCEL_PROFILE_FORWARD splits the step into graph-build vs GPU-eval. Both flags are read once and cached in a OnceLock, and array_shape is only queried when a flag is set, so the steady-state decode path is unchanged (two atomic loads per token, no extra FFI calls or eval()).

Recovers one cheap, bit-exact op in apertus xIELU: both branches end in `+ beta * x`, so that shared term is now added once after the per-element select instead of inside each branch. This drops one elementwise add (and its kernel launch) per layer per token. The select only chooses which value beta*x is added to, so the arithmetic is unchanged. Verified greedy temp-0 output is byte-identical to the pre-change baseline for both apertus (500 bytes) and seed_oss (308 bytes); the decode delta is within run-to-run noise (~83.3 -> ~83.5 tok/s on apertus), consistent with the GPU-eval-bound profile. A fused/compiled xIELU activation (single kernel over the elementwise graph) is the only remaining real lever and is left as a documented follow-up, since it touches the C++ FFI bridge and risks f16 jitter.

Adds a CPU-only unit test (xielu_factored_beta_x_matches_branch_local) asserting the factored form equals the branch-local reference bit-for-bit in f32 across positive, negative, and zero inputs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: bf16->f16 quant-scale promotion (#260) regresses bf16-scale quantized model decode ~33-41% on M1 Ultra

1 participant