Skip to content

feat(models): add Apertus (xIELU, QK-norm, llama3 RoPE scaling) - #260

Merged
inureyes merged 2 commits into
mainfrom
feat/issue-248-apertus
Jun 13, 2026
Merged

inureyes merged 2 commits into
mainfrom
feat/issue-248-apertus

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Port Apertus (Swiss AI, apertus), a Llama-style dense transformer, through the standard config-backed loader. Apertus differs from a plain Llama block in three ways: an xIELU activation MLP with no gate, QK-norm on the per-head Q/K, and llama3 RoPE frequency scaling. Embeddings are untied (separate lm_head), with no logit multiplier. Mirrors the upstream reference at https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/apertus.py and the XieLU activation at https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/activations.py.

What changed

  • src/models/apertus.rs (new): ApertusModel with the config-backed load + from_weights + LanguageModel surface. xIELU activation built from square/expm1/minimum/where_cond; per-layer alpha_p/alpha_n are read to f32 at load (softplus applied, alpha_n offset by beta), and beta/eps default to 0.5 / -1e-6 when absent. QK-norm gated on qk_norm (RMSNorm after reshape-to-heads, before transpose + RoPE). llama3 scaled frequencies via fast_rope_with_freqs, computed once and shared per layer (same band formula as exaone4). Norms are attention_layernorm/feedforward_layernorm with standard residuals; post_norm is parsed but unused. Untied lm_head (tied would fall back to embed_tokens.as_linear).
  • src/models/apertus_tests.rs (new): checkpoint-free config-parse tests plus pure-scalar xIELU/softplus math checks.
  • Registration: detection.rs ("apertus"), model_metadata.rs (config-backed entry), loaded_model.rs (enum variant + dispatch arm), mod.rs (pub mod/pub use, ModelType::Apertus, ALL_MODEL_TYPES, metadata ("Apertus (dense)", "Specialized"), test module), distributed/tensor_parallel/inference.rs (architecture fallback), and docs/supported-models.md.

Test plan

  • cargo check --release --features metal,accelerate --lib
  • cargo clippy --release --features metal,accelerate --lib -- -D warnings
  • cargo fmt --check
  • End-to-end generation on mlx-community/Apertus-8B-Instruct-2509-4bit (real-model gate, run by the maintainer; the implementation environment has no Metal device)

Closes #248

Apertus (Swiss AI, model_type `apertus`) is a Llama-style dense transformer with three deltas over the standard block: an xIELU activation MLP with no gate (up_proj/down_proj only), QK-norm on the per-head Q/K, and llama3 RoPE frequency scaling. Embeddings are untied (separate lm_head), with no logit multiplier. Mirrors https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/apertus.py.

The xIELU activation carries per-layer learnable scalars alpha_p/alpha_n (stored pre-softplus) plus fixed beta/eps, read to f32 at load time and applied element-wise as where(x > 0, alpha_p*x^2 + beta*x, (expm1(min(x, eps)) - x)*alpha_n + beta*x) over square/expm1/minimum/where_cond ops. The llama3 scaled frequencies reuse the same band computation as exaone4 and feed fast_rope_with_freqs. QK-norm follows qwen3 (RMSNorm after reshape-to-heads, before transpose+RoPE), gated on the qk_norm flag. Per-layer norms are attention_layernorm/feedforward_layernorm with standard residuals; post_norm is parsed but unused.

Loads through the standard config-backed route. Registered in detection, model metadata, the loaded-model dispatch, ALL_MODEL_TYPES, the metadata family table, the tensor-parallel architecture fallback, and supported-models docs. Checkpoint-free unit tests cover config parsing and the xIELU/softplus math; end-to-end generation is validated against mlx-community/Apertus-8B-Instruct-2509-4bit.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata status:review Under review labels Jun 13, 2026
Two bf16 load bugs surfaced validating Apertus against mlx-community/Apertus-8B-Instruct-2509-4bit, each silently collapsing the forward toward zero rather than crashing.

load_text_weights now promotes bf16 quantization scales/biases to f16 for quantized models. Newer mlx-community exports (Apertus-2509, Seed-OSS) ship bf16 quant scales, and mlxcel's dequant path needs f16: bf16 scales dequantize to near-zero and generation emits blank output. f16-scale checkpoints (granite and the rest) are a no-op, so no existing model regresses; the packed uint32 weights and every other tensor are untouched.

Apertus read_scalar casts the bf16 act_fn scalars (xIELU alpha_p, alpha_n, beta, eps) to f32 before the item read. A raw item read on a bf16 array misreads the value, which zeroed the xIELU coefficients and vanished the MLP branch.

Validated end to end against the real checkpoint: per-layer logit magnitudes match the mlx-lm reference to ~1% and greedy decode reproduces " Paris," exactly (tests/apertus_parity.rs).
@inureyes inureyes added status:done Completed and removed status:review Under review labels Jun 13, 2026
@inureyes
inureyes merged commit d80f787 into main Jun 13, 2026
4 checks passed
@inureyes
inureyes deleted the feat/issue-248-apertus branch June 13, 2026 09:35
inureyes added a commit that referenced this pull request Jun 15, 2026
…ssion (#289) (#290)

#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 this to the 0.3 milestone Jun 21, 2026
sjswerdloff pushed a commit to sjswerdloff/mlxcel that referenced this pull request Jul 30, 2026
I read 'get rid of versioning of the cold storage' as the durable byte format's
magic/version identifier, and built a priority argument on it: the integrity
digest first because a freeze was coming. Alden disagreed, I escalated rather
than adjudicated, and Stuart says Alden is right.

He meant HOUSEKEEPING - versioning directories, environment variables, and dead
v2/v3 code and files - once things stabilise. Never the byte format.

The format is not freezing. Magic and version bytes stay, because without an
identifier stale bytes parse plausibly instead of failing closed.

The digest was still right to do first - it is Alden's P1 and the authority
files decide what may be deleted - but that is a DIFFERENT reason than the one I
gave, and the one I gave was false. Same shape Cora named on kindled-skills lablup#260
hours earlier: a true statement of effect with a wrong statement of cause.

Heading retracted in place rather than deleted; a design doc that silently
changes its premise is worse than one that shows the change. Real cleanup
targets recorded so the actual direction is not lost behind my misreading.

Co-Authored-By: clement-7074f29f <[email protected]>
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…up#260)

* feat(models): add Apertus (xIELU, QK-norm, llama3 RoPE scaling)

Apertus (Swiss AI, model_type `apertus`) is a Llama-style dense transformer with three deltas over the standard block: an xIELU activation MLP with no gate (up_proj/down_proj only), QK-norm on the per-head Q/K, and llama3 RoPE frequency scaling. Embeddings are untied (separate lm_head), with no logit multiplier. Mirrors https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/apertus.py.

The xIELU activation carries per-layer learnable scalars alpha_p/alpha_n (stored pre-softplus) plus fixed beta/eps, read to f32 at load time and applied element-wise as where(x > 0, alpha_p*x^2 + beta*x, (expm1(min(x, eps)) - x)*alpha_n + beta*x) over square/expm1/minimum/where_cond ops. The llama3 scaled frequencies reuse the same band computation as exaone4 and feed fast_rope_with_freqs. QK-norm follows qwen3 (RMSNorm after reshape-to-heads, before transpose+RoPE), gated on the qk_norm flag. Per-layer norms are attention_layernorm/feedforward_layernorm with standard residuals; post_norm is parsed but unused.

Loads through the standard config-backed route. Registered in detection, model metadata, the loaded-model dispatch, ALL_MODEL_TYPES, the metadata family table, the tensor-parallel architecture fallback, and supported-models docs. Checkpoint-free unit tests cover config parsing and the xIELU/softplus math; end-to-end generation is validated against mlx-community/Apertus-8B-Instruct-2509-4bit.

* fix(models): correct Apertus loading for bf16-scale checkpoints

Two bf16 load bugs surfaced validating Apertus against mlx-community/Apertus-8B-Instruct-2509-4bit, each silently collapsing the forward toward zero rather than crashing.

load_text_weights now promotes bf16 quantization scales/biases to f16 for quantized models. Newer mlx-community exports (Apertus-2509, Seed-OSS) ship bf16 quant scales, and mlxcel's dequant path needs f16: bf16 scales dequantize to near-zero and generation emits blank output. f16-scale checkpoints (granite and the rest) are a no-op, so no existing model regresses; the packed uint32 weights and every other tensor are untouched.

Apertus read_scalar casts the bf16 act_fn scalars (xIELU alpha_p, alpha_n, beta, eps) to f32 before the item read. A raw item read on a bf16 array misreads the value, which zeroed the xIELU coefficients and vanished the MLP branch.

Validated end to end against the real checkpoint: per-layer logit magnitudes match the mlx-lm reference to ~1% and greedy decode reproduces " Paris," exactly (tests/apertus_parity.rs).
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).
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:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): add Apertus

1 participant