Skip to content

glm metal: shortest-remaining-first prompt order by default on one Mac - #3903

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:glm-metal-srpt-default
Nov 24, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:glm-metal-srpt-default

Conversation

@gilby

@gilby gilby commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

GLM-5.3-Flash on one Apple Silicon Mac now uses shortest-remaining-first (srpt) prompt prefill order by default whenever neither --decode-share nor a TENSORFOLD_PREFILL_SHARE below 1 nor a non-srpt TENSORFOLD_PREFILL_ORDER is configured; the two-Mac expert-parallel pair and every explicit flag/env override keep the prior FIFO order.

  • Issue: Refs GLM-5.3-Flash on Metal: decode and prompt speed on M5 Ultra, token-exact (baseline, items and gate) #3152 (PR 2 of 3 M5 Ultra scheduling/pool/profiling pull requests; GLM_ROUND_PROF is in glm metal: GLM_ROUND_PROF per-round host and GPU summary #3894 and GLM_POOL_TOKENS stacks on this branch).
  • Stacked on: main (2a674b552498739e783a31c2573fc9ccf4d8cc0d), 1 commit (813ebef6f6459d9f7ae08624dae11f2bd1d2aef1; CHANGELOG.md left untouched per CONTRIBUTING.md so post-merge changelog: commits do not conflict). git merge-tree --write-tree origin/main 813ebef6f6459d9f7ae08624dae11f2bd1d2aef1 against current origin/main (e3fb56cb09061a979cb02080601fc6f7870d3362) succeeds cleanly (2bdcf43320d105fe5b02246171a5fecd0f274b04).
  • atc overlap --json: Run on 813ebef6f6459d9f7ae08624dae11f2bd1d2aef1 against origin/main; complete: false (myPullRequestsLimit, otherPullRequestsLimit, pullFilesUnavailable, timeLimit). Zero workingTree PR overlaps (0 conflicting PRs); zero overlap with #3894 (GLM_ROUND_PROF) or active #3448. (worktreeOverlaps: 1 local stack sibling worktree sharing glm_host.zig/glm_schedule_tests.zig/glm_expiry.zig, stacked in PR 3).

Checks on this head

  • Head commit: 813ebef6f6459d9f7ae08624dae11f2bd1d2aef1 (identical Zig/docs sources to measured 5c2f513ca, CHANGELOG.md dropped)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 221 vs 221, 0 new (tools/zig/lean_new.py clean vs origin/main e3fb56cb0; tools/pr_check.py --base origin/main exits 0 with 0 error(s), 1 warning(s): the expected advisory CHANGELOG.md Unreleased warning since CHANGELOG.md is omitted to avoid merge conflicts).
  • Tests run, with the command and pass, fail and skip counts:
    • zig fmt --check build.zig zig: passed (0 exit) on 813ebef6f.
    • git diff --check origin/main: passed (0 exit) on 813ebef6f.
    • Fail-before / pass-after: with srptByDefault returning false, zig build test-glm-scheduling-host failed (64 pass, 1 fail, failed glm order: one Mac with no share starts with shortest-remaining-first); after fixing:
      • zig build test-glm-scheduling-host --summary all: passed (3/3 steps succeeded; 65/65 tests passed) on 813ebef6f.
      • zig build test test-golden dev-tools --summary all -Dcpu=apple_m1: passed (1103/1103 steps succeeded; 468/483 tests passed, 15 skipped on 813ebef6f incremental pass after 15101/15225 full pass on 5c2f513ca, identical Zig sources).
      • zig build native -Dcpu=apple_m1 -Dversion=1.0.0: passed (5c2f513ca, identical Zig/docs/build sources).
  • CI: all checks green on 813ebef6f6459d9f7ae08624dae11f2bd1d2aef1 (Zig format/fmt, No GPU/zig, zig-b, zig-windows, zig-macos-dev-tools, pytest, benchmark-clients (ubuntu-latest), benchmark-clients (windows-latest), release-paths (ubuntu-24.04) all COMPLETED SUCCESS; nightly jobs SKIPPED).
  • Not run, and why: CUDA, HIP, Windows, desktop app, two-Mac pair end-to-end hardware check (two-Mac pair fallback checked by glm_order_tests.zig host tests), and M1–M4 hardware checks (only Metal GLM native host scheduling changes; no GPU kernels changed).

Receipt

  • Main against branch: 7-arm symmetric A-B-C-C-B-A (+ discard) on M5 Ultra with TensorFold/GLM-5.3-Flash-MLX-4bit-MTP, --parallel 3 --context 1048576 --prompt-cache-gib 0, Nominal thermal (notifyutil -g com.apple.system.thermalpressurelevel == 0) and idle background (mediaanalysisd CPU 0.0) at start and end of every arm. Raw scrubbed artifacts + script: https://gist.github.com/e2ab481ecaa851eb40e8127279b41f97
    • Mixed long fill + short reply (tools/bench_fill.py --long 262144 --reply 21 --lead 5.0), 2-arm median per condition:
      • long_first 21-token reply_s: main 6.11s (6.05s, 6.16s) vs pr2 FIFO (TENSORFOLD_PREFILL_ORDER=fifo) 6.08s (6.04s, 6.12s) vs pr2 default SRPT 3.56s (3.57s, 3.56s) (41.7% faster, 1.72x).
      • long_first long_ttft_s: main 141.19s (140.59s, 141.79s) vs pr2 FIFO 141.41s (141.01s, 141.80s) vs pr2 default SRPT 139.51s (139.61s, 139.41s) (-1.2%, well within 5%).
      • long_first both_done_s: main 141.57s vs pr2 FIFO 141.78s vs pr2 default SRPT 139.89s.
      • long_first reply_gap_max_ms: main 309.3ms vs pr2 FIFO 309.2ms vs pr2 default SRPT 297.5ms.
      • alone (reply_s / long_ttft_s): 0.38s / 136.24s (main), 0.38s / 136.16s (fifo), 0.38s / 136.19s (srpt).
    • Concurrent exactness & speed (tools/bench_concurrent.py --alone --serial --tokens 256 --reps 3 --temperatures 0 --levels 1,3), aggregate / per-stream tok/s (2-arm median):
      • code, T=0.0, L=1: main 99.15 vs fifo 99.00 vs srpt 99.10
      • code, T=0.0, L=3: main 170.75 / 57.95 vs fifo 170.65 / 57.90 vs srpt 170.80 / 57.90
      • chat, T=0.0, L=1: main 81.30 vs fifo 81.05 vs srpt 81.05
      • chat, T=0.0, L=3: main 146.30 / 49.90 vs fifo 146.35 / 49.90 vs srpt 146.20 / 49.90
  • Boxes it reaches: Apple Silicon Macs running native Metal GLM-5.3-Flash (tensorfold-native, one-Mac host); two-Mac expert-parallel pair keeps old order (glm_order_tests.zig); no CUDA/HIP/Windows/app changes.
  • New switch? Default, accepted values, profile row: No new environment variable. Uses existing TENSORFOLD_PREFILL_ORDER (srpt now default on one Mac for GLM-5.3-Flash when --decode-share and TENSORFOLD_PREFILL_SHARE < 1 are unset; any other value keeps FIFO), --decode-share, and TENSORFOLD_PREFILL_SHARE (per docs/api.md). No optimization-ledger profile row needed.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Base origin/main 2a674b552498739e783a31c2573fc9ccf4d8cc0d (v1.2.10), head 813ebef6f6459d9f7ae08624dae11f2bd1d2aef1 (identical Zig/docs/build sources to measured 5c2f513ca, CHANGELOG.md dropped); Zig 0.17.0; Apple Metal toolchain 32023.922 (metalfe-32023.922.4) (Metal 4); Apple M5 Ultra (80-core GPU, 512 GiB RAM), macOS 27.2.1 (26B131); checkpoint TensorFold/GLM-5.3-Flash-MLX-4bit-MTP (rev d2b17f5253440224422b150705c1109a9a3a0368).
  • Paths and platforms the change reaches, which I ran and which I could not: Reaches zig/src/native/{glm_order.zig,glm_host.zig} and host scheduler config tests (zig/src/{glm_order_tests.zig,glm_schedule_tests.zig}, zig/build/glm_expiry.zig). Ran on M5 Ultra (macOS 27.2.1); M1–M4 hardware and physical two-Mac pair not run on-device (host tests built -Dcpu=apple_m1).
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: Across all 7 arms (arm0..arm6), bench_fill reported exact = true, cold = true, bench_concurrent reported unequal_alone = 0, unequal_serial = 0, failed = 0, token_sha256_diffs_vs_main = 0, and 21-token greedy reply21_sha256 (f8348662fcdfb545b97562a6a6231266b35e1f192e49c552f5f97c8d4c47a86a) had 0 diffs across all arms.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): Posted above and in the public receipt gist (https://gist.github.com/e2ab481ecaa851eb40e8127279b41f97).
  • Decode speed before and after, from tools/bench_openai.py: Single-stream greedy decode from bench_concurrent.py --alone --serial (code: 99.15 main vs 99.10 tok/s SRPT; chat: 81.30 main vs 81.05 tok/s SRPT; 21-token bench_openai.stream exactness check identical SHA across all arms); standalone bench_openai.py suite n/a because this PR only changes queued prompt scheduling order (glm_order.zig) and cannot change solo decode kernels.
  • Prompt speed before and after: the cold first request's ttft_s from tools/bench_cache.py --require-cold on a long prompt, or "n/a" with the reason when the change cannot reach prompt processing: Cold 262,144-token prefill TTFT measured via tools/bench_fill.py (--prompt-cache-gib 0, cold=true): solo 136.24s (main) vs 136.19s (srpt), and concurrent long_first 141.19s (main) / 141.41s (fifo) vs 139.51s (srpt, -1.2%, within 5%).
  • Licence of any new weights or drafter, or "n/a": n/a (uses existing TensorFold/GLM-5.3-Flash-MLX-4bit-MTP checkpoint, no new weights or drafter).

Checklist

  • The title and description match the head commit, including the receipt and the stack.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.

  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.

  • No precision traded for speed. If the bits change, the description says which sums change and why.

  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled

@krunkosaurus

Copy link
Copy Markdown
Contributor

I ran this on a second M5 Ultra against an agent-shaped load, because the order it changes is what was hurting our production serving. It helps a great deal here.

Setup

  • Mac Studio M5 Ultra, 512 GB, macOS 27.2 (26B118). Zig 0.17.0, Metal toolchain 32023.922.4.
  • TensorFold/GLM-5.3-Flash-MLX-4bit-MTP @ d2b17f52.
  • serve --context 1048576 --parallel 4 --max-tokens 32768 --thinking --vision --prompt-cache-gib 64 --keep-warm 900.
  • "main" is v1.2.10 (2a674b5) built from source. "PR" is 62b362a, this commit plus glm metal: GLM_POOL_TOKENS sizes the shared latent pool apart from the per-stream window (stacked on #3903) #3906 with the pool left at its default, built the same way (zig build native -Dcpu=apple_m1).
  • The machine served nothing else during a run and each arm started a fresh server. Thermal pressure read 1 or 2 at every arm boundary, never 0 (the Mac had been serving for three days), so absolute numbers may read a little low. One run per arm, arms back to back, not interleaved.

Agent loops beside long prompts

Three agents start together. Each sends a 15.8K-token system prompt that all three share, its own 22K-token brief, then 12 tool steps of about 1,400 new prompt tokens and a 200-token reply (temperature 1.0, top_p 0.95, a seed, reasoning_effort high, ignore_eos). Two cold prompts of about 137K tokens arrive 45 s and 150 s in.

main PR every request alone
Agent loop, mean of the three 515.8 s 197.1 s 54.9 s
A step, send to last token, p50 / p90 41.5 / 52.5 s 7.8 / 17.7 s 3.3 s p50
A step's decode, tok/s, p10 / p50 4.5 / 5.5 57.8 / 70.3 83.0 p50
First long prompt, first token 239.6 s 222.6 s 74.9 s
Second long prompt, first token 339.3 s 186.2 s 76.9 s
All 41 requests done 522.9 s 337.0 s

The 1.2.8 release (with the #3545 patch, which is what we were serving) measured the same as main: 507.3 s a loop, 5.9 tok/s p50. The loops ran 2.6 times faster on the PR and neither long prompt waited longer for its first token.

The receipt's bench at half the length

tools/bench_fill.py --long 131072 --reply 21 --lead 5.0, served with --prompt-cache-gib 0:

main PR
long_first reply_s 6.88 s 4.50 s
long_first long_ttft_s 75.25 s 75.37 s
alone reply_s / long_ttft_s 0.39 s / 66.40 s 0.39 s / 66.69 s
exact, cold true, true true, true

Decode with no prompt filling

tools/bench_concurrent.py --alone --serial --tokens 256 --reps 3 --temperatures 1.0,0 --levels 1,4: the PR reads level with main (code, sampled, 4 streams: 123.7 against 123.8 tok/s; the widest gap is chat, greedy, 4 streams at 147.3 against 154.8, where the 1.2.8 build read 152.8). Every cell has 0 unequal against alone and against "draft": false, and the token hashes are the same on main and on the PR in all 8 cells.

Token hashes in the loop test

30 of the 41 loop replies have the same token_sha256 on main and on the PR. The other 11 are agent 1's replies from its 8th request on and agent 2's from its 9th. I don't think the order causes it:

So each long sampled conversation seems to have two possible outcomes, on main as well, and I haven't found what picks one. I can open a separate issue with the request bodies if that is useful.

In production

This has served our agent traffic since 2026-11-19 11:11 UTC, together with #3906 at 3M tokens and the #3418 to #3458 stack. On the 1.2.8 build, 70 hours of that traffic had a median decode of 48.7 tok/s but a 10th percentile of 8.2 (replies of 64 tokens or more), and 9% of requests waited over a minute for a first token. In the first 33 minutes on the new build (100 requests from the same agents, the three changes together, a backlog still clearing) the 10th percentile is 18.7 tok/s, requests that read 16K tokens or more reach a first token in 49 s at the median where it was 204 s, and 1,000 generated tokens cost 46 s of request time where they cost 78 s. That is a short window, so take it as a first reading.

The default looks right to me for one Mac serving agents.

My agent (Claude) did the builds, the runs and this write-up. The load script and the raw JSON are available if you want them.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby for the careful A/B and the second contributor's agent-loop receipt. This is ready: the default applies only on one Mac with no share setting, an explicit order still wins, and the pair keeps its old order. The new host tests cover those cases, and the head applies cleanly to today's main. We were unable to run the Mac checks here; your receipts carry that evidence. Nothing else is needed.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked 3fa080556 against main 210e3b0c5. The only conflict now is in docs/api.md, where main added the --thinking-reserve description. I kept that wording and gave GLM its own sentence describing the single-Mac default, the decode-share override and the effective prompt-share threshold. No source resolution was needed.

zig build test-glm-scheduling-host -Dcpu=apple_m1 -j5 --summary all passed 65/65 on that trial tree on an M5 Max with Zig 0.17.0. In the scratch tree, changing the default's boundary from >= 1 to > 1 failed exactly the one-Mac default test (64/65). These were host checks. The served workload and two-Mac behavior remain covered to the extent described in the existing receipts.

Igor Lessio - AIFLOW LABS

@krunkosaurus

Copy link
Copy Markdown
Contributor

A correction to the "In production" paragraph of my comment above.

The 70-hour baseline I quoted was not one workload. 50 hours into it we limited our support agent to two background runs at a time; on the day we set it, the agent had averaged 3.5 runs at once and peaked at 8. Split at that point, with nothing else changed on the 1.2.8 build:

1.2.8, no limit (50.3 h) 1.2.8, two runs at most (20.5 h) new build, two runs at most (30.0 h)
Requests 9,421 3,011 4,201
Request time per 1,000 generated tokens 110.2 s 34.6 s 21.8 s
First token after reading 16K tokens or more, p50 / p90 250 / 601 s 44 / 208 s 24 / 50 s
Decode in small steps (under 4K read, 64 to 512 tokens out), tok/s p10 / p50 8.1 / 51.0 21.9 / 58.1 35.0 / 72.4
Decode in the requests that read 16K or more, tok/s p10 / p50 6.7 / 10.4 9.6 / 38.9 27.2 / 61.4
Conversations alive at once, p50 / p90 / max 3 / 5 / 11 2 / 4 / 6 2 / 3 / 6

"New build" is this commit with #3906 at 3M tokens and the #3418 to #3458 stack, as before.

The like-for-like gain in production is between the last two columns: 34.6 s to 21.8 s per 1,000 generated tokens, and the slow tenth of first tokens after a long read from 208 s to 50 s. The 78 s and 204 s I quoted came mostly from the heavier overlap of the first 50 hours, which our own limit had removed before the new build went in. The controlled runs in the comment are not affected.

We removed that limit today (2026-11-20 17:27 UTC), so the new build now gets the load of the first column. I can add those numbers here once there is a day of them.

My agent (Claude) found the mistake and wrote this.

@gilby
gilby marked this pull request as ready for review November 21, 2026 20:44
@ashhart
ashhart force-pushed the glm-metal-srpt-default branch from 3fa0805 to 3d2f44d Compare November 21, 2026 22:23
@AIFlowML

Copy link
Copy Markdown
Contributor

The current 3d2f44dd head trial-merges cleanly into 3a02038db, including the API docs that previously overlapped. The GLM scheduling target passed all 70 tests on M5 Max.

The last hosted pytest failure is missing PyYAML during collection. The current merged tree retains main's exact workflow blob with pytest pyyaml. A fresh Python 3.13 environment reproduced the collection error with pytest alone, then passed the workflow condition test and all four subtests after installing PyYAML.

zig build test-glm-scheduling-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j1 --summary all
python -m pytest -q -p no:cacheprovider tools/test_ci_workflow_conditions.py

The current checks cover scheduling policy and the CI prerequisite. The served A/B and multi-Mac measurements remain in the existing receipts.

Igor Lessio - AIFLOW LABS

gilby and others added 2 commits November 24, 2026 16:29
A reply that would end well before a long prompt goes first, as for Qwen3.8-27B. A decode share, a prompt share below 1, the two-Mac pair or a TENSORFOLD_PREFILL_ORDER other than srpt keeps the old order. Refs ashhart#3152.
@ashhart
ashhart force-pushed the glm-metal-srpt-default branch from 04a454d to d2d3cff Compare November 24, 2026 16:29
@ashhart
ashhart merged commit 99df7d3 into ashhart:main Nov 24, 2026
1 of 11 checks passed
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants