Skip to content

feat(models): add Xing4.0 (xing4_0) support - #3276

Merged
inureyes merged 5 commits into
mainfrom
feature/2356-xing4-0-support
Nov 22, 2026
Merged

inureyes merged 5 commits into
mainfrom
feature/2356-xing4-0-support

Conversation

@inureyes

@inureyes inureyes commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds Xing4.0 (xing4_0, Xing4_0ForCausalLM), a 29B-total / 4B-active MoE on the DeepSeek-V3 lineage whose residual stream is a width-hc_mult manifold hyper connection. The port follows mlx-vlm's mlx_vlm/models/xing4_0 and reuses the DeepSeek-V3 MLA attention, noaux_tc MoE and weight sanitize by composition.

Closes #2356

What changed

  • src/models/xing4_0.rs (new): config with validation (hc_mult, Sinkhorn iterations, hc_eps, clamp range, hc * hidden overflow), the Xing hyper connection (f32 RMS norm over the flattened state, activation-dtype mix, sigmoid pre without eps, 2 * sigmoid post, clipped exp(x - rowmax) Sinkhorn with row-then-column rounds, collapse, post * out + comb @ x expand, mean head), decoder layer and model with native batched decode and last-row logits. Wrong-shaped hc_fn / hc_base / hc_scale are refused at load; the language_model. wrapper is stripped and the MTP trailer dropped by the reused DeepSeek-V3 sanitize.
  • src/models/deepseek_v3.rs: DeepSeekV3Attention gains an opt-in RoPE frequency table (with_rope_table), applied through fast_rope_with_freqs. Xing installs its YaRN table; every other family keeps the previous plain fast_rope call. The routed-expert SwitchGLU now stores one width per plane.
  • Review fixes: --rope-freq-base is carried onto the attention when Xing installs no RoPE table (rope_overrides::resolve_base), and a routed-expert plane whose shapes solve to 1 bit or to a non-integer width is a load error instead of a gather_qmm abort.
  • src/lib/mlxcel-core/src/layers.rs: QuantizedMultiLinear::from_weights solves an affine plane's width from its shapes when it differs from the declared width, with a load-time warning, matching what UnifiedLinear already does.
  • src/tokenizer/mod.rs: Xing4_0Tokenizer defaults add_bos_token to true when the config omits it, as the upstream class constructor does.
  • Wiring: detection, config-backed loader, LoadedModel, registry (MoE list, alias), memory estimate, tensor-parallel family name, batched-decode coverage list, MLA latent-pair cache families (fp16 only), docs/supported-models.md, CHANGELOG.md.
  • Bilingual technical report: TECHNICAL_REPORTS/3276-xing4-0-support-20261123.{en,ko}.md.
  • scripts/xing4_0_oracle_trace.py (new): mlx-vlm reference trace with three negative arms (--comb-transpose, --v4-sinkhorn, --no-yarn).

Validation

Teacher-forced logit trace over docs/architecture.md, plan 64 40 8 1536, BOS-anchored, scored with scripts/compare_logit_traces.py --decided 2:

Comparison Decided top-1 disagreements Perplexity
mlxcel 8-bit vs mlx-vlm 8-bit 0 / 1190 7.9571 vs 7.9506 (+0.08%)
mlxcel 8-bit vs mlx-vlm with comb.T expand 180 / 760 arm 45.10
mlxcel 8-bit vs mlx-vlm with V4 Sinkhorn 6 / 1156 arm 8.49
mlxcel 8-bit vs mlx-vlm without YaRN 6 / 1159 arm 8.31
mlxcel OptiQ 4-bit vs mlx-vlm 8-bit 0 / 1190 8.0360 (+1.07%)
mlxcel 8-bit vs mlx-vlm, single-token decode steps (48 positions) 0 / 11 +0.69%
  • Moonlight 16B-A3B 4-bit (shared DeepSeek-V3 code) before and after this branch: byte-identical trace (plan 64 20 8 512).
  • mlxcel generate greedy: 8-bit coherent at about 19 tok/s (29.2 GB resident), OptiQ 4-bit coherent at about 22 tok/s (19.2 GB); both answer 17 * 23 with 391.
  • mlxcel-server on the 8-bit checkpoint: non-streaming and streaming chat completions return coherent text (The capital of France is Paris.).
  • Unit tests: models::xing4_0 (16), models::deepseek_v3, models::deepseek_v32, models::glm4_moe_lite, models::longcat, coverage_tests, registry, detection, tokenizer, mlxcel-core mla::, one_bit, layers::. The GLM4 MoE Lite and LongCat latent-path counter tests fail only when run in parallel with each other (shared global MLA stats counters) and pass with --test-threads=1.
  • Contract checks all pass.

Findings outside this PR

  • DeepSeekV3Attention ignored YaRN for every family before this change: it always applied fast_rope at rope_theta. Only Xing opts into the frequency table here, so real DeepSeek-V3 and Kimi K2 checkpoints with a YaRN block still run plain RoPE. Worth a follow-up with its own long-context trace.
  • The OptiQ export logs one shape-reconciliation warning per per-path layer (about 360 lines at load), as the existing UnifiedLinear path already does.

Out of scope

MTP drafting, a fused Sinkhorn kernel, tensor-parallel sharding of the hyper connection, and a quantized latent cache for this family.

@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata modelsize:medium 4-bit checkpoint 10-50GB; comfortable on a 128GB dev box modeltype:text Text-only language model arch:moe Sparse mixture-of-experts decoder status:review Under review labels Nov 22, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Run Xing4.0 (xing4_0): the DeepSeek-V3 decoder inside a width-hc_mult hyper-connection residual stream, wired into CLI, server and registry.

Findings Addressed

  • With no RoPE table in force (--rope-scaling none, or a YaRN block that degrades to default), the table-less DeepSeek-V3 attention kept rope_theta while RopeScalingKind::resolve had already counted a --rope-freq-base override as applied, so the override was silently ignored. The base now goes through rope_overrides::resolve_base, like the other rope_utils families (HIGH, a6d439d)
  • Used by: notes on rope_utils, rope_overrides::resolve_base, strip_language_model_wrapper and mla_latent_decode_rows did not name Xing4.0 (MEDIUM, a6d439d)

Verified without change

  • Hyper connection matches mlx_vlm/models/xing4_0/language.py line by line: f32 RMS norm at rms_norm_eps, activation-dtype mix promoted to f32, eps-free pre, 2 * sigmoid post, clip, row-max exp, row-then-column Sinkhorn with + hc_eps, activation-dtype collapse, post * out + comb @ x, mean head, repeat widen.
  • rope_table is None for every other DeepSeekV3Attention builder, which keeps the exact previous fast_rope / fast_rope_batched calls. The YaRN mscale is the mscale / mscale_all_dim ratio, and mscale_all_dim still lands in the attention scale.
  • The per-plane width solve in QuantizedMultiLinear::from_weights and deepseek_v3::SwitchGLU::from_weights returns the declared width whenever the shapes agree. It changes only layouts that previously aborted inside quantized_matmul / gather_qmm, and nothing else reads SwitchGLU.bits.

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass (models::xing4_0, models::deepseek_v3::, models::rope_, clippy, fmt, used-by check)

@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

Fixed (HIGH): deepseek_v3::SwitchGLU::from_weights accepted an expert plane whose shapes solve to 1 bit. That plane would reach gather_qmm, which has no 1-bit kernel, and abort the process at the first routed forward pass. It is now a load error naming the plane, matching SwitchLinear (#1370). Fixed in 9eaaf21, with a test covering a 1-bit plane and a non-integer-width plane.

Checked with no change needed:

  • Xing40Config::validate bounds hc_mult (1..=64), the Sinkhorn iterations (at most 1000), hc_eps, the clamp range, and hc_mult * hidden_size against i32, so the (2 + hc) * hc and hc * hidden casts in XingHyperConnection::from_weights cannot overflow. hc_fn, hc_base and hc_scale are refused at load when their shapes are wrong.
  • QuantizedMultiLinear::from_weights: a solved width that differs from the declared one is checked again for range and for 1 bit. Shapes with no integer or loadable width are a load error.
  • The Xing4_0Tokenizer BOS default only applies when add_bos_token is absent and the class name matches exactly.
  • Decode hot path: no host syncs. All hyper-connection constants are built at load, and astype is a no-op when the dtypes already match.

Open, not fixed (below the auto-fix threshold):

  • MEDIUM (perf, depends on the checkpoint): if hc_fn is stored in a different dtype from the activations (for example f32 under bf16), astype(hc_fn) re-casts the full [(2+hc)*hc, hc*D] matrix in every sublayer for every token. Casting it once at load to the embedding dtype would remove this. I could not check the checkpoint dtype without loading one.
  • MEDIUM (perf, already listed as out of scope): Sinkhorn adds about 6 ops per iteration, which is about 240 small graph nodes per layer per token at 20 iterations. A fused kernel or a compiled closure would bring per-token CPU overhead down.
  • LOW: parse_eos truncates an out-of-range i64 with as i32. LOW: forward_rows runs the stream mean over every prefill position before select_row.

inureyes added a commit that referenced this pull request Nov 22, 2026
Add the bilingual technical report for PR #3276 covering the hyper-connection port, the opt-in YaRN table, the per-plane width solving, the tokenizer BOS default and the trace results.
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 22, 2026
Add the Xing4.0 causal LM: the DeepSeek-V3 MLA attention, noaux_tc MoE and sanitize reused by composition inside a width-hc_mult manifold hyper-connection residual stream, ported from mlx-vlm's xing4_0 module.

- New src/models/xing4_0.rs with the Xing-specific connection (f32 RMS norm, activation-dtype mix, no pre eps, clipped exp Sinkhorn with row-then-column rounds, comb @ x expand, mean head), config validation, native batched decode and last-row logits.
- DeepSeekV3Attention gains an opt-in RoPE frequency table so Xing applies its YaRN block; every other family keeps the plain-frequency path.
- QuantizedMultiLinear and the DeepSeek-V3 routed-expert SwitchGLU solve each affine plane's width from its shapes when a per-path override differs from the declared width (the OptiQ 4-bit export stores the MLA projections and some expert planes at 8 bits); uniform checkpoints keep the declared width.
- The Xing4_0Tokenizer class defaults add_bos_token to true, as its constructor does.
- Wired into detection, the config-backed loader, LoadedModel, the registry, the MLA latent-pair family list and docs/supported-models.md; scripts/xing4_0_oracle_trace.py scores the logit trace against mlx-vlm with three negative arms.
Xing4.0 resolves its RoPE through RopeScalingKind::resolve, which consumes an installed --rope-freq-base override and counts it as applied. When the scheme resolves to no table (--rope-scaling none, or a YaRN block that degrades to default), the DeepSeek-V3 attention kept rotating at the config's rope_theta, so the server accepted the override while the rotation ignored it, the silent-override failure rope_overrides exists to prevent. The table-less layers now take rope_theta through rope_overrides::resolve_base, as the other rope_utils families do. With no override installed the base is unchanged, so the released YaRN checkpoints and every other DeepSeek-V3 family are unaffected.

Also add Xing4.0 to the Used by notes of rope_utils, rope_overrides::resolve_base, strip_language_model_wrapper and mla_latent_decode_rows, which it now reaches.

The override is a process-wide OnceLock, so it is not exercised by a unit test; models::xing4_0 and models::deepseek_v3 tests, clippy, fmt and the used-by check pass.

Refs #2356
The per-plane width solve added for the OptiQ Xing4.0 export lets a plane's shapes pick its bit width. Shapes that solve to 1 bit passed the load checks and reached MLX's gather_qmm, which has no 1-bit kernel and aborts the process at the first routed forward pass. The shared SwitchLinear loader already refuses this layout (issue #1370); SwitchGLU now refuses it too, naming the plane.

A new test drives a hostile down_proj plane at a 1-bit and a non-integer width and asserts both are load errors. models::xing4_0 and models::deepseek_v3 unit tests, clippy and fmt pass.

Refs #2356
The xing4_0 family had a docs/supported-models.md entry but no CHANGELOG.md line, which the other recent model additions carry under Unreleased. The entry names the two checkpoints, the hyper-connection residual stream over the reused DeepSeek-V3 decoder, the mlx-vlm parity numbers, and the out-of-scope items.

Validation: cargo fmt --check, clippy on lib and tests, the repo CI check scripts, and the models::xing4_0, models::deepseek_v3, tokenizer::tests, coverage_tests and models::detection selectors all pass.

Refs #2356
Add the bilingual technical report for PR #3276 covering the hyper-connection port, the opt-in YaRN table, the per-plane width solving, the tokenizer BOS default and the trace results.
@inureyes
inureyes force-pushed the feature/2356-xing4-0-support branch from b2d0a27 to 897c772 Compare November 22, 2026 22:19
@inureyes
inureyes marked this pull request as ready for review November 22, 2026 22:24
@inureyes
inureyes merged commit f315dc2 into main Nov 22, 2026
32 of 33 checks passed
@inureyes
inureyes deleted the feature/2356-xing4-0-support branch November 22, 2026 22:24
inureyes added a commit that referenced this pull request Nov 22, 2026
Main gained `xing4_0` (#3276) in `MLA_LATENT_CACHE_FAMILIES` while this branch added `hy_v4`. Both edits set the pinned length to 17, so the textual merge kept 17 for a list of 18 and `only_the_fallback_less_families_are_latent_families` failed. Found by rerunning `-p mlxcel-core --lib mla::` after bringing the branch up to date with main.

Refs #2354
inureyes added a commit that referenced this pull request Nov 22, 2026
The branch was rebuilt on current main (f315dc2) to clear the CHANGELOG.md and docs/supported-models.md conflicts with #3270, #3275 and #3276. This merge records the previously pushed commit 2642118 as a parent so the update is a fast-forward of the remote branch; the tree is the rebuilt branch's, which already carries every change of that commit.

Refs #2354
inureyes added a commit that referenced this pull request Nov 22, 2026
The Kolibri-1 (#3275), Limite (#3249) and Xing4.0 (#3276) oracle trace scripts carry a shebang but landed with mode 100644. The executable-mode check in the Python tooling tests fails on main, and every open PR inherits the failure through its merge ref.

This sets the executable bit on the three files. Mode change only, no content changes. Verified locally that no other tracked `scripts/*.py` with a shebang is left at 100644.
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:moe Sparse mixture-of-experts decoder area:models Model architectures, weights, loading, metadata modelsize:medium 4-bit checkpoint 10-50GB; comfortable on a 128GB dev box modeltype:text Text-only language model 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 Xing4.0 (xing4_0) support

1 participant