Skip to content

fix(glm cuda): a stopped request ends on every rank within a round - #301

Closed
MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:fix/glm-serial-stop
Closed

MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:fix/glm-serial-stop

Conversation

@MiaAI-Lab

Copy link
Copy Markdown
Contributor

What this changes

A stopped GLM-5.3-Flash request on CUDA now ends on every rank within a round. Today it keeps both ranks decoding to max_tokens (or an end token), and every later request waits behind it.

This was found through a user report on the GLM-5.3-Flash DGX Spark recipe (issue 38 on MiaAI-Lab/GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold): a client that left a long streamed request kept the server busy until the reply would have reached max_tokens.

The bug

  • In families/glm5_next/cuda/decode.py, the serial, MTP and DFlash2 loops (and auto_decode in drafter_choice.py) call on_tokens(...) but never read its result.
  • GlmEngine.follow runs rank 1 with lambda new: None, so rank 1 has no way to learn that rank 0 wants to stop.
  • So when the client goes, a stop string matches or a grammar cuts, the server's on_tokens returns True, but neither rank stops. Both decode to the end.
  • A single rank has the same problem, since the loops ignore the result there too.

The fix

stop.StopVote (a new module of about 50 lines) wraps on_tokens:

  • A true result from rank 0's callback is that rank's wish to stop. Rank 1's callback never asks.
  • The wish rides as one extra float32 word on the next verify sample's all-gather. That is the exchange every round already makes: the top-k gather in sample_rows, or the nucleus rule's first gather.
  • The word is removed before sampling. Every rank then reads the same gathered flags and ends the run after the same round.
  • No new collective is added. Draft samples (draft=True, the MTP chain) and the DFlash2 drafter's own gathers are unchanged.
  • GlmEngine._run_once sets the vote on every rank after prefill, and _run clears it in its finally.
  • The stats of a stopped run say "stopped": true.
  • The server's on_tokens is unchanged apart from its comment.

Why replies are unchanged

  • The vote word is appended after the candidates and stripped before choose_rows / nucleus_rows see them, so every sampled token is the same as before.
  • Every rank adds the word to the same gathers, so the gather sizes are the same on every rank. Rank 1 always sends 0.
  • Only the point where a stopped run ends changes. An unstopped run decodes exactly as before, with the same tokens and rounds (tested).

Receipt

  • Environment: this branch on upstream main (9356df5, v0.6.3). CPU tests ran in the NVIDIA PyTorch container (PyTorch 2.13.0a0 nv26.07, Python 3.12, aarch64), with no GPU used.
  • Exactness: tests/test_glm_serial_stop.py runs two ranks as threads over a real CPU all-gather, through the real decode loops (serial_decode, mtp_decode, dflash_decode, auto_decode). It covers greedy, top-k and nucleus. Each rank holds half of a fake model's vocabulary. Checked:
    • With the vote and no stop, every loop gives the same tokens and rounds as without it.
    • Each verify gather carries exactly one word more, and the sizes are the same on both ranks.
    • A stop asked at rank 0's 1st or 3rd callback ends both ranks after the next round, with identical tokens and positions. The tokens sent before the stop are a prefix of the unstopped reply.
    • GlmEngine._run on two ranks: both end after the same round and both report stopped. The vote is cleared, and the unstopped run is unchanged.
  • Decode / prompt speed: the hot path changes by one float32 word in the per-round gather that already happens, and no new collective. No benchmark was rerun on this branch.
  • Measured in the recipe: the same change ran in the recipe's build (TensorFold with the recipe's patches) on two DGX Sparks. There, a 32k-token streamed request with MTP drafts (--parallel 1) whose client left after 5 s freed the server in 0.12 s, and the next request started 0.26 s later. Before the change, both ranks decoded to max_tokens.
  • Tests run:
    • pytest tests/test_glm*.py tests/test_cuda*.py tests/test_family_streams.py tests/test_server*.py tests/test_lane_server.py tests/test_exact_sampling.py: 1,263 passed, 22 skipped and 12 failed on this branch, against 1,230 passed, 22 skipped and the same 12 failed on unmodified main.
    • The 12 failures need mlx, which isn't in the container: test_lane_server.py (11) and test_exact_sampling.py::test_nucleus_without_top_k_matches_the_whole_vocabulary_draw.
    • The new tests/test_glm_serial_stop.py has 33 tests: 31 fail on main and all 33 pass here. It imports the CUDA modules under the fake-triton fixture from test_cuda_geometry.py, like the other GLM CPU tests.
  • Not run, and why:
    • The GPU case added to tests/cuda/test_glm_engine.py (test_a_stop_ends_the_run_after_the_next_round: serial, MTP and auto policies; top-k, nucleus and greedy). It was run in the recipe's build, not on upstream main, because no GPU was free for this branch.
    • Metal is untouched. This is the CUDA GLM family only.

Checklist

  • Every token still goes through the same rounds. No new serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Sampling is untouched, and the vote word is removed before it.
  • No precision traded for speed.
  • Prompt processing is unchanged. Prefill doesn't vote: the vote is set after the first token.
  • Platforms: CUDA GLM only, CPU-tested here. The GPU test was run in the recipe's build. Metal is not touched.
  • New tests fail before the change, pass after it, and skip without torch.
  • Comments and docstrings are one line.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

The GLM decode loops (serial, MTP, DFlash2, auto) call on_tokens but never read its result, and rank 1 runs
with a no-op callback, so rank 1 cannot know that rank 0 wants to stop. After a client left, a stop string
matched or a grammar cut, both ranks decoded on to max_tokens or an end token, and every later request
waited behind it.

stop.StopVote wraps on_tokens. Rank 0's wish to stop rides as one extra float32 word on the next verify
sample's all-gather (the exchange each round already makes; the nucleus rule's first gather likewise) and
is removed before sampling, so every rank ends the run after the same round. Sampled tokens are unchanged;
only when a stopped run ends changes. Every rank adds the word to the same gathers, so gather sizes stay
equal across ranks. GlmEngine._run_once sets the vote on every rank after prefill, _run clears it, and the
stats of a stopped run say "stopped". One rank stops the same way.

Tests: tests/test_glm_serial_stop.py (two CPU ranks as threads over a real all-gather: every loop and
sampling rule, replies unchanged, equal gather sizes; GlmEngine._run on two ranks) and a GPU case in
tests/cuda/test_glm_engine.py.
@MiaAI-Lab

Copy link
Copy Markdown
Contributor Author

@ashhart @pulseandthread: #320 and this PR fix the same bug. GLM's two-rank decode loops never read on_tokens' result, so a stopped reply decodes on to max_tokens on both ranks. Both use a sticky stop vote from rank 0. The differences:

  • this PR sends the vote as one extra word on the round's existing sample all-gather, so no exchange is added;
  • it covers the serial, MTP and DFlash2 loops and auto_decode;
  • it has CPU tests (tests/test_glm_serial_stop.py) and a GPU test, and it merges cleanly;
  • fix(glm5_next cuda): both ranks stop a reply when its caller asks #320 adds a one-int all-gather each round, and it currently conflicts with main.

Either is fine with us. If you prefer #320, I'll close this one. If you take this one, please credit @pulseandthread as well, who found and fixed the same bug in #194 and #320. The GLM recipe ships this version as patch 0070 (v1.5).

@MiaAI-Lab

Copy link
Copy Markdown
Contributor Author

Still needed on 0.6.5: in glm5_next/cuda/decode.py and drafter_choice.py the decode loops still ignore what on_tokens returns, and rank 1 still follows with lambda new: None. This branch still merges cleanly on 609ca41 (the same diff, cherry-picked without changes). Here is a fresh receipt on 0.6.5, and the related work.

Related

#320 (@pulseandthread) fixes the same bug with a one-int all-gather of its own each round, and #231 (@plotarmordev)
ports that vote to Flash Next; @pulseandthread found and fixed it first, in #194. This pull request sends the vote on
the round's existing sample all-gather instead, and covers the serial, MTP, DFlash2 and auto loops. Either fix is
fine with us; if you take this one, please credit @pulseandthread too.

Receipt

Rerun on TensorFold 0.6.5. The commit applies to 609ca41 unchanged (a clean cherry-pick of this branch).

Environment. TensorFold 0.6.5 (609ca41) against this change on it. NGC PyTorch 26.07 container (PyTorch
2.13.0a0+9186a08, CUDA 13.3, Python 3.12), pip install "tensorfold[test] @ git+...@609ca41", each tree's
src/tensorfold mounted over the installed package. Two DGX Sparks (GB10, 128 GB each), --tp 2 over their CX7
cable (NCCL on both RoCE rails, NCCL_IB_HCA as the RUNBOOK says). Checkpoint Mia-AiLab/GLM-5.3-Flash-EXL3-4bpw-TensorFold
at 078455ff; serve arguments --tp 2 --context 131072, otherwise the defaults (--drafter auto picked the MTP head
and incoai/GLM-5.3-Flash-DFlash2 at bf582e4 from the cache). Before and after ran on the same two machines in the
same session.

What it fixes, on the two Sparks (two runs each, both nearly identical):

0.6.5 this change
greedy completion with "stop": [" 40,"], max_tokens 3,000, ignore_eos: reply after its 96 tokens 45.7 s, 45.5 s 1.60 s, 1.57 s
streamed 4,000-token ignore_eos request, client closes its socket after 3 s: until /health says busy: false 57.7 s, 57.7 s 0.21 s, 0.21 s
the same greedy 64-token request before and after those, token_sha equal (ba2f337c82d8) equal (ba2f337c82d8)

On 0.6.5 the stopped reply's text was complete after 96 tokens, but the response came only after both ranks had
decoded all 3,000; /health counted 6 tokens for the abandoned stream while the pair stayed busy for a minute. The
stop-string replies are the same 96 tokens on both trees (sha=e8af119abe3c on the server's done line).

Exactness (python3 tools/bench_concurrent.py http://127.0.0.1:8877 glm-flash --alone --serial, 4 streams,
256 tokens, 3 reps): every concurrent reply equal to its solo run, and every solo run equal to "draft": false.

cell 0.6.5 alone / serial this change alone / serial
code, T=1.0 12/12, 4/4 12/12, 4/4
chat, T=1.0 12/12, 4/4 12/12, 4/4
code, T=0 12/12, 1/1 12/12, 1/1
chat, T=0 12/12, 1/1 12/12, 1/1

Decode (python3 tools/bench_openai.py http://127.0.0.1:8877 glm-flash, 64 tokens, 5 reps, median tok/s).
Boots in this order: 0.6.5, a tree that does not touch decode (a /metrics change), this change (run twice on one
boot), 0.6.5 again.

prompt 0.6.5 (1st) other tree this change this change 0.6.5 (2nd)
code, T=1.0 34.90 35.31 35.34 35.19 35.23
chat, T=1.0 26.08 26.17 26.24 26.16 26.13
code, T=0 41.41 41.56 41.28 41.40 41.35
chat, T=0 23.86 23.84 23.79 23.72 23.77

The vote is one float32 word more on a gather each round already makes; the medians are within 0.4% of 0.6.5's
second run in every cell. The first 0.6.5 run had CPU tests running beside rank 1.

Prompt speed (tools/prefill_cold.py, cold prompts, median of 3, tok/s). Prefill does not vote: the vote is set
after the first token.

prompt 0.6.5 this change
2k 490.7 489.1
8k 461.8 467.1
16k 472.6 466.5
32k 476.9 469.8
64k 491.4 491.0

Tests.

  • tests/test_glm_serial_stop.py (CPU, the NGC container): 33 passed here; on 0.6.5's source 31 of them fail and 2
    pass (the two that check an unstopped run decodes as before).
  • tests/cuda/test_glm_engine.py on one GB10: 42 passed here (33 before, plus the new
    test_a_stop_ends_the_run_after_the_next_round for serial, MTP and auto policies, top-k, nucleus and greedy);
    33 passed on 0.6.5.
  • the full suite, python -m pytest -q -p no:randomly --timeout 120 tests in the NGC 26.07 container on an aarch64 CPU (no GPU, no MLX): 4,080 passed, 135 failed, 11 errors, 172 skipped on this branch, against 4,047 passed and the same 135 failed, 11 errors and 172 skipped on 0.6.5. The failures and errors are the same tests on both trees: they need MLX or macOS (test_lane_server.py, test_request_reasoning.py, test_prompt_fill.py and the like), and one, test_cancellation.py::test_streaming_socket_disconnect_during_decode_releases_the_active_row, loops on recv there until pytest-timeout's 120 s stops it (without the timeout the suite hangs at that test). The 33 more passing are test_glm_serial_stop.py.

Not run. Metal: this is the CUDA GLM family only, and nothing outside families/glm5_next/cuda changes but a
comment in cuda/server.py. A grammar cut (response_format) and a thinking-budget cut stop through the same
on_tokens result; we timed only the stop string and the client leaving.

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @MiaAI-Lab. The Python engine is frozen (#286) and GLM isn't on the Zig engine yet, so this won't land as written, but the bug has no Zig counterpart to fix.

The Zig engine's cluster is leader-driven. Rank 0 plans every round and broadcasts it, and the follower loop in zig/src/cluster/backend.zig only applies what rank 0 sends, with no decode loop of its own. A reply that rank 0 ends, by a stop string, a cancel or the cap, drops out of the next round on every rank, and one release command frees its slot everywhere before that round runs.

Thanks for chasing the recipe report down to the callback result nobody read, and for testing it on two ranks over a real all-gather.

@ashhart ashhart closed this Oct 6, 2026
grearjake-star added a commit to grearjake-star/TensorFold that referenced this pull request Oct 8, 2026
The GLM serial, MTP and DFlash2 decode loops (and auto_decode) called on_tokens but never read its
result, and rank 1 followed with a callback that could not stop. So when a client left, a stop string
matched or a grammar cut the reply, both ranks decoded on to max_tokens and every later request
waited behind it (a 343 s busy pair in the report). A sticky stop vote (stop.StopVote) now rides one
extra word on the round's existing sample all-gather, so both ranks end after the same round with no
added exchange. Replies up to the stop are unchanged.

Upstream: ashhart#301 (the same bug as ashhart#320 and ashhart#194, found on the GLM-5.3-Flash DGX Spark
recipe), closed unmerged with the Python engine frozen. Host tests in tests/test_glm_serial_stop.py;
the CUDA test needs a GLM checkpoint on two ranks, which this fork's gate does not run.

Co-authored-by: MiaAI-Lab <[email protected]>
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.

2 participants