Skip to content

Add GLM-5.3, and fix GLM-5.2/5.3 output past 2048 tokens - #2512

Merged
AlexCheema merged 1 commit into
mainfrom
models/glm-5.3
Nov 1, 2026
Merged

AlexCheema merged 1 commit into
mainfrom
models/glm-5.3

Conversation

@AlexCheema

Copy link
Copy Markdown
Contributor

Stacked on #2509 (upstream mlx-lm), which is stacked on #2508 (MLX 0.32.4) and #2507 (per-family model folders).

Summary

GLM-5.2 and GLM-5.3 have the same architecture (glm_moe_dsa). It is DeepSeek sparse attention with the indexer shared across layers:

  • Only 21 of the 78 layers have a DSA indexer.
  • The other 57 reuse the top-2048 key selection of the previous indexer layer.

exo's previous mlx-lm treated the model as DeepSeek V3.2, with an indexer in every layer. exo also loads checkpoints with strict=False. So the 57 indexers the checkpoint doesn't contain kept their random initial weights, and nothing reported it.

  • Below 2048 tokens of context every key is attended anyway, so nothing changes.
  • Past 2048 tokens, those 57 layers attend to an essentially random subset of the keys.

On the first 8 layers of the 4-bit model, next-token top-1 agreement with Hugging Face transformers falls from 0.95 to 0.50 at 3.9k tokens and to 0.36 at 20k tokens. That matches the needle failures above about 2.6k prompt tokens reported in #2270, on both tensor and pipeline sharding.

A header-only load of every mlx-community conversion of these models gives the same result. That covers GLM-5.2 4bit, mxfp4, DQ4plus-q8 (the model in #2270) and fp8, and GLM-5.3 4bit and mixed-4_5bit:

  • on origin/main, 285 parameters are left unset in each: 57 indexers × 5 tensors;
  • on this branch, none are.

Upstream mlx-lm implements the shared indexer (ml-explore/mlx-lm#1410), so the move to upstream mlx-lm (#2509) fixes the model itself. This PR adds what exo still needs:

  • Missing weights are an error.
    • load_model_checked records the tensor names mlx-lm loads. It raises MissingWeightsError when a model parameter was not in the checkpoint.
    • exo keeps strict=False for the other direction: checkpoint tensors the model doesn't use, such as MTP layers and vision towers.
    • A header-only check of all 123 existing cards finds no card that leaves a parameter unset.
  • Pipeline parallel forwards the shared top-k selection.
    • mlx-lm starts every pipeline rank with no previous selection. A rank whose first layer is a shared layer therefore attended densely there once the context passed 2048 tokens.
    • The selection made by a rank's last layer now goes to the next rank along with the hidden state (int32, [B, 1, L, 2048]). This only happens for models with shared layers, and only once the context exceeds index_topk.
    • 2x pipeline parallel is now bit-identical to a single device. Before, a split at layer 4 gave top-1 0.935 and mean KL 2.2e-4 against a single device past 2048 tokens. Three of every four layers past layer 2 are shared, so most split points start a rank on one. On the two Mac Studios here exo split at layer 38, an indexer layer; a split at 39, 40 or 41 would have started on a shared one.
  • GLM-5.x model classes in models/glm/. These are mlx-lm's model with two reference details restored:
    • The indexer's key LayerNorm uses eps 1e-6, as in the official reference implementation and transformers. mlx-lm uses MLX's default, 1e-5.
    • Router logits are computed in float32, as the config (moe_router_dtype: "float32") and transformers specify. mlx-lm runs the matmul in bf16 and only the sigmoid in float32. On the 8-layer check this moves top-1 from 0.959 to 0.968 and mean KL from 1.0e-4 to 6.2e-5.
  • reasoning_effort.
    • GLM-5.3's template understands only low and high, and GLM-5.2's only high. Both render every other value as "Reasoning Effort: Max".
    • exo turns enable_thinking: true into medium, so a plain thinking request ran at Max.
    • Mapping:
      • unset or xhigh → Max, the template default;
      • medium or high → High;
      • none, minimal or low → Low on GLM-5.3 (High on GLM-5.2, which has no Low).
    • GLM-5.3's template has no thinking-off switch: its generation prompt always opens <think>. enable_thinking: false now closes the block (<think></think>), as GLM-5.2's template does.
  • Card: mlx-community/GLM-5.3-4bit.
    • 418 GB, 1M-token context.
    • Sampling defaults 1.0 / 0.95, from zai-org's generation_config.json.
    • Capabilities: text and thinking.

Long context relies on MLX 0.32.4 (#2508) or mlx-lm's workaround.

Fixes #2270 in exo. The report also mentions corruption under a bare mlx_lm.server, which was not reproduced here. On a single Mac Studio, origin/main recalls a needle at 1k tokens and fails from about 4.5k on (0/9 prompts), the same threshold #2270 reports for tensor and pipeline sharding. This branch recalls all 15 prompts up to 16k.

Test plan

  • uv run basedpyright (0 errors), uv run ruff check, nix fmt (no changes) and pytest src (1945 passed).
  • New tests:
    • load_model_checked on a model with a missing parameter;
    • top-k forwarding between two pipeline ranks against a single device;
    • the GLM model classes (eps, float32 router);
    • every reasoning_effort / enable_thinking combination through the effort and generation-prompt lines of the GLM-5.2 and GLM-5.3 templates, copied verbatim. The full mlx-community GLM-5.3 template was also checked by hand: see "Chat template" below;
    • the card registry count.
  • Correctness: the report below. exo was run against Hugging Face transformers (PyTorch, float32) on real GLM-5.3 weights, on the full model's needle recall, and with tiny-random parity.
  • On a cluster: the 4-bit model on two Mac Studios (M3 Ultra, 512 GB, Thunderbolt 5):
    • placed by exo and chatted with from the dashboard;
    • needle recall through the API on 2x TP and 2x PP;
    • perf for single, 2x TP and 2x PP.

Correctness report

Verdict: PASS.

  • Before this branch: origin/main is wrong past 2048 tokens.
  • Upstream mlx-lm alone: matches the reference on a single device. Pipeline parallel still drifts when a rank starts on a shared layer.
  • This branch:
    • matches the reference;
    • 2x pipeline parallel is bit-identical to a single device;
    • the float32 router brings it closer to the reference than upstream mlx-lm is.

Headline: next-token agreement with transformers past 2048 tokens

Setup:

  • Weights: the first 8 layers of mlx-community/GLM-5.3-4bit: three indexer layers (0, 1, 2), three shared layers (3, 4, 5), one indexer layer (6) and one shared layer (7).
  • Reference: transformers runs the same 4-bit weights, dequantised to float32.
  • Scoring: a float32 log-softmax over the reference's top 64 tokens plus a tail bucket, at every position of each prompt.
  • Software:
    • transformers 5.22.0 with PyTorch 2.13 on CPU (Mac Studio, M3 Ultra);
    • exo in-process, through exo's own loading, patches and chunked prefill;
    • MLX is the PyPI build of v0.32.4. exo's uv.lock builds the same tag (36969c09) from source on macOS.

Columns:

Each cell is next-token top-1 agreement and mean KL.

prompt (tokens) origin/main upstream mlx-lm this PR transformers bf16
haystack, 3,875 0.496, 8.7e-3 0.960, 9.7e-5 0.964, 5.5e-5 0.979, 5.3e-5
raw text, 5,999 0.554, 1.2e-2 0.955, 1.2e-4 0.965, 7.4e-5 0.964, 6.8e-5
raw text, 19,916 0.355, 2.2e-2 0.959, 9.9e-5 0.968, 6.2e-5 not run (1)
below 2048 (all prompts) 0.949 to 0.959, 8e-5 to 1.2e-4 identical to origin/main 0.963 to 0.968, 4e-5 to 7e-5 0.965 to 0.974, 5.5e-5 to 6.7e-5

(1) transformers' bf16 matmuls on CPU fall back to a scalar kernel, too slow for a 20k-token prompt.

This PR is as close to the float32 reference as transformers' own bf16 forward is:

  • upstream mlx-lm sits at about 1.5x the noise floor's KL;

  • origin/main sits at 100x to 300x.

  • origin/main by layer: the hidden-state error against the reference jumps at layer 3, the first shared layer: from 5e-3 to 0.11-0.18, reaching 0.29-0.46 at layer 7.

  • Key selection: the shared layers' selections overlap the reference's by 0.54-0.57 on origin/main, against 0.986 with upstream mlx-lm and this PR.

  • Sparse prefill: past 16,384 tokens, where upstream mlx-lm switches to its sparse prefill, this PR scores top-1 0.968 (KL 6.5e-5), upstream mlx-lm 0.961 (1.0e-4) and origin/main 0.27 (2.9e-2).

  • Noise floor: transformers' own bf16 run, against its float32 run, has a last-layer median hidden error of 7.0e-3 to 7.3e-3 past 2048 tokens. The other runs, past 2048 tokens:

run last-layer median hidden error
transformers bf16 (noise floor) 7.0e-3 to 7.3e-3
this PR 7.4e-3 to 8.5e-3
upstream mlx-lm 1.4e-2 to 1.5e-2
origin/main 0.29 to 0.46

Pipeline parallel (2 ranks, split at layer 4, a shared layer; 5,999 tokens, positions ≥ 2048)

vs single device vs transformers
upstream mlx-lm, #2509 (each rank starts without a selection) identical through layer 3, then hidden error 1.7e-2 at layer 4 and 2.8e-2 at layer 7; top-1 0.935, KL 2.2e-4 top-1 0.930, KL 2.3e-4 (single device: 0.955, 1.2e-4)
this PR (selection forwarded) bit-identical (KL 0, hidden error 0) top-1 0.965, KL 7.4e-5, the same as a single device

Three of every four layers past layer 2 are shared layers, so most pipeline split points start a rank on one. On the two Mac Studios here, exo's placement split at layer 38, which is an indexer layer.

Full model, split at layer 39 (a shared layer). This is two Mac Studios, all 78 layers, teacher forcing on the 3.9k-token and 6k-token prompts with 512-token prefill chunks.

  • The second rank receives the selection from the first.
  • The result is bit-identical to a single device with the same chunks: KL 0 and hidden error 0 on all 78 layers, below and past 2048 tokens.

Tiny-random parity (tiny-random/glm-5.3, float32, 4,993 tokens, index_topk 128)

origin/main upstream mlx-lm this PR (eps 1e-6)
hidden error vs transformers, layer 3 (first shared) 0.62 1.1e-3 2.6e-5
hidden error, last layer 1.2 2.6e-3 6.0e-5
next-token top-1 past index_topk 0.0099 0.81 0.992
shared-layer selection overlap 0.09 to 0.11 0.996 0.9999

Full model: needle recall (single Mac Studio, 4-bit)

Setup:

  • Model and path: mlx-community/GLM-5.3-4bit, all 78 layers, through exo's own loading, chat template and mlx_generate, decoding greedily.
  • Prompt: a service inventory of N tokens, with one needle record at 25% depth (host=vault-07 service=secrets port=… rotation_code=KESTREL-…) and a question about it.
  • Thinking: off. The assistant turn opens with <think></think>.
  • Pass: the answer contains both the code and the port.
  • Branch: this PR on MLX 0.32.4. origin/main: its own mlx-lm fork and MLX pin.
prompt tokens origin/main this PR
1,049 3/3 3/3
2,541 2/3 (one wrong port, one answer with a stray </think>) 3/3
4,536 0/3 3/3
8,053 0/3 3/3
16,045 0/3 3/3
32,056 not run 1/1
131,055 not run 1/1 (17 min)

origin/main's answers past 4k tokens are not wrong answers but token salad. For example, at 8k:

1. The1658 1:1\n: \n . \n. 19. 1 . (.05 00:1 5.01.99 0 16..  the 0= 66. 500

That is the #2270 symptom, here on a single device: correct below about 2.6k prompt tokens, garbage from about 4k.

This PR answers every prompt in the same form, e.g. The secrets service on host vault-07 has rotation_code KESTREL-6880 and listens on port 22947.

On the full model, origin/main's next-token distributions differ from this PR's as follows (teacher forcing, the same four prompts as the 8-layer check):

below 2048 tokens past 2048 tokens
mean KL 2.8e-3 to 6.3e-3 (1) 1.1 to 4.5 nats
top-1 agreement 0.92 to 0.98 0.21 to 0.76

(1) Below 2048 tokens, only the float32 router, the epsilon and the MLX version differ.

Full model on two Mac Studios: needle recall through the API

Setup:

prompt tokens 2x TP (RDMA) 2x PP (ring)
2,541 3/3 3/3
4,536 3/3 3/3
8,053 3/3 3/3
16,045 3/3 3/3

Chat template

On the real GLM-5.3 template:

request origin/main this PR
enable_thinking: true (exo sends medium) Max High
reasoning_effort: "low" Low Low
enable_thinking: false Max, with <think> opened Low, with <think></think>

Deviations and caveats

  • Precision left upstream: mlx-lm scores the indexer in bf16. Float32 scores raise the shared layers' selection overlap with the reference from 0.986 to 0.995, but move next-token agreement by only 0.002 or less. That is left to mlx-lm and not patched here.
  • Truncated check: the 8-layer check uses the real weights of the first 8 layers. It covers both indexer and shared layers, the dense layers and the first five MoE layers, but not the full depth. The full model is covered by the needle runs, the main-vs-PR comparison and the split-39 pipeline check.
  • Needle seeds: the needle generator seeds its random numbers with seed * 1000 + length, so the 2,541-token seed-3 prompt and the 4,536-token seed-1 prompt share a code and port, though not a haystack. Each request is independent: there is no prefix cache.
  • Pipeline checks in-process: the in-process pipeline checks use 512-token chunks on the full model. With 4096-token chunks, the harness's teacher-forcing loop, which makes every rank wait for the logits, hit macOS's GPU command-buffer timeout on rank 0. exo's own pipelined prefill doesn't wait this way: the API runs above prefill 16k-token prompts over 2x PP without it.

Reproduce

The commands use the exo correctness harness and its GLM helper scripts.

# 8-layer checkpoint, byte-exact tensors from mlx-community/GLM-5.3-4bit @ 81ad9b73 (sha256-checked)
python truncate_mlx_checkpoint.py GLM-5.3-4bit GLM-5.3-4bit-L8 8

# reference: transformers GlmMoeDsaForCausalLM, float32, same 4-bit weights dequantised in memory
python run_reference_hf_mlx.py run --model GLM-5.3-4bit-L8 --dtype float32 --tasks tf,hidden \
  --prompts trunc_raw.jsonl --prompt-positions 100000 --hidden-max-positions 768 --topk 64 --out hf_float32.json

# exo in-process: single device, or 2 pipeline ranks split at layer 4 (MLX_RANK / MLX_HOSTFILE, ring)
python -m exo_harness.exo_inprocess run --model-id mlx-community/GLM-5.3-4bit-L8 --model-path GLM-5.3-4bit-L8 \
  --tasks tf,hidden --prompts trunc_raw.jsonl --prompt-positions 100000 --hidden-max-positions 768 --topk 64 \
  --continuations-from hf_float32.json --out exo.json  [--parallel pipeline --backend ring --pp-split 4]

# next-token agreement and hidden-state error, split at index_topk
python bucket_compare.py --reference hf_float32.json --candidate exo.json --threshold 2048

# indexer selections vs transformers (positions >= 2048)
python indexer_topk_capture.py hf --from-mlx --model-path GLM-5.3-4bit-L8 --ids ids.json --out topk_hf.npz --min-query 2048
python indexer_topk_capture.py mlx --model-path GLM-5.3-4bit-L8 --ids ids.json --out topk_exo.npz --min-query 2048
python indexer_topk_capture.py compare topk_exo.npz topk_hf.npz

# full model, needle recall through exo's own loading, rendering and mlx_generate
python needle_full.py --model-path ~/.exo/models/mlx-community--GLM-5.3-4bit --model-id mlx-community/GLM-5.3-4bit \
  --lengths 1000,2500,4500,8000,16000 --seeds 1,2,3 --out needle.json

Performance (4-bit, one and two Mac Studios)

Setup

  • Hardware: two Mac Studios (M3 Ultra, 512 GB) on macOS 27.0.1, joined by Thunderbolt 5.
    • Tensor parallel uses RDMA over Thunderbolt (MlxJaccl).
    • Pipeline parallel uses the TCP ring (MlxRing) over the same cable, split by exo at layer 38 of 78.
  • Weights: mlx-community/GLM-5.3-4bit (affine, group 64, 418 GB).
  • Software: this branch, MLX 0.32.4 (PyPI build of the tag uv.lock builds from source), mlx-lm 6b572ab.

Method: the exo correctness harness perf runner, batch 1.

  • Prompts: chat-templated prose, cut to the stated token count with the model's tokenizer.
  • Generation: 128 tokens, with EOS banned by /bench/chat/completions. The prefix cache is off.
  • Repetitions: 1 warm-up, then 3 measured requests per row. The table reports the median.
  • TTFT is measured client-side.
  • Prefill and decode tok/s are exo's server-side generation_stats.
  • Peak memory: MLX's peak on the reporting rank, and the largest RAM in use on either node, polled from /state.
config comm nodes prompt tok gen tok TTFT s (median [min-max]) prefill tok/s decode tok/s peak mem GB (MLX / max node RAM)
single - 1 512 128 8.49 [7.48-8.88] 62.4 18.7 450.3 / 439.2
single - 1 4096 128 26.68 [26.44-27.43] 155.4 17.3 456.3 / 447.9
single - 1 16384 128 129.35 [129.00-129.50] 127.0 16.9 471.8 / 467.1
2x TP RDMA 2 512 128 3.39 [3.39-3.44] 300.1 23.1 227.2 / 230.6
2x TP RDMA 2 4096 128 14.61 [14.58-14.64] 316.7 21.3 230.7 / 237.0
2x TP RDMA 2 16384 128 73.49 [73.38-73.64] 228.1 20.7 243.5 / 251.2
2x PP ring 2 512 128 4.39 [4.38-4.39] 125.2 17.8 205.7 / 249.3
2x PP ring 2 4096 128 26.61 [26.59-30.39] 155.9 16.4 211.6 / 257.4
2x PP ring 2 16384 128 81.61 [81.31-83.36] 214.3 16.0 218.4 / 307.2

Observations

  • 2x TP:
    • Decode is 1.23x single (23.1 vs 18.7 tok/s at 512 tokens, 20.7 vs 16.9 at 16k).
    • Prefill is 1.8x to 2x single at 4k and 16k tokens.
    • Each node holds half the weights.
  • 2x PP:
    • Decode is about 5 % below single. Every token crosses both machines.
    • At 16k tokens, prefill is 1.7x single (214 vs 127 tok/s), because exo overlaps the two stages during long prefills.
  • Single machine:
    • The 4-bit model takes 418 GB of the 498 GB GPU working set, and MLX peaks at 450 to 472 GB. The single-machine rows run close to that limit.
    • The 512-token row is out of line with the others: 8.5 s TTFT, where 2x PP takes 4.4 s. The time goes to that prefill. It was not investigated further.

The raw numbers are in perf.json.

Screenshots

Captured with headless Chromium from the dashboard of the two-Mac-Studio cluster, with the model running 2x tensor parallel over RDMA. Device names are generic.

cluster-home.png

cluster-chat.png

🤖 Generated with Claude Code

Landing commit: this PR's change on top of main, with conflicts with the PRs landed before it resolved as in the tested integration build.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@AlexCheema
AlexCheema merged commit bb1f029 into main Nov 1, 2026
6 checks passed
@AlexCheema
AlexCheema deleted the models/glm-5.3 branch November 1, 2026 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant