Skip to content

feat(cuda): stop strings on every CUDA engine, and ignore_eos on the 27B's CUDA engine - #63

Closed
nood-co1 wants to merge 2 commits into
ashhart:mainfrom
blockbrain-ai:contrib/cuda-stop-and-ignore-eos
Closed

nood-co1 wants to merge 2 commits into
ashhart:mainfrom
blockbrain-ai:contrib/cuda-stop-and-ignore-eos

Conversation

@nood-co1

@nood-co1 nood-co1 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Issue #46 was closed by eea15fd, which brought ignore_eos and stop strings to the Mac server and changed no file under cuda/. This PR does the same for CUDA, in two commits. The first can be taken without the second:

  1. Stop strings on every CUDA engine. The CUDA server reads stop, checks stop and ignore_eos before a stream opens, and cuts the reply token by token, so drafted and "draft": false replies return the same content, usage and token_sha.
  2. ignore_eos on the Qwen3.8-27B CUDA engine, on one GPU, on two ranks and with --parallel N. The Flash Next, Nemotron and Qwen3.6 CUDA engines still stop at end tokens.

Why

Q/ is src/tensorfold/families/qwen3_5/cuda/. References are at main (71377a5, 0.3.6.2).

  • docs/api.md:30 lists ignore_eos for "MLX and GLM CUDA", :31 lists stop for "MLX" only, and :36 says "Its Qwen engines also ignore ignore_eos."
  • The bench client sends ignore_eos: true (tools/bench_openai.py:26), and docs/recipes/README.md:49-50 warns that "the Qwen CUDA engines can stop at EOS before the requested limit". A 27B cell can therefore time replies shorter than --tokens.
  • cuda/server.py reads no stop: the only "stop" in it is the finish label (:312). A CUDA client that sends stop gets the whole reply. The server also accepts a non-boolean ignore_eos; the GLM app reads it with bool() (glm5_next/cuda/app.py:55). server/stopping.py:9-55 (stop_options, StopPolicy) is used only by the Mac server.
  • The 27B's decoders already take stop_eos: serial_decode and draft_decode (Q/decode.py:85, :183) and decode_tp (Q/decode_tp.py:159). The CUDA tests compare drafted with serial at stop_eos=False (tests/cuda/test_qwen27_prefill.py:104-105, tests/cuda/test_qwen27_forward.py:130, :153, tests/cuda/test_qwen27_exl3.py:108, :126). But Qwen27Engine.generate never passes it (Q/engine.py:175-176, :200-201), and MultiDecoder takes one decoder-wide flag (Q/multi.py:67, :73) that the engine never sets.

Change

Commit 1: stop strings on every CUDA engine

  • App._prepare calls stop_options, the Mac's validator. A non-boolean ignore_eos or a malformed stop gets HTTP 400 before any stream bytes, from prepare, check and GlmApp.check alike. PreparedRequest carries ignore_eos and stop.
  • App.run checks each new token in order. StopStrings.hit decodes only the tail that can hold a new match: the longest stop string's UTF-8 length plus 8 tokens. (The Mac keeps its length in characters plus 8, which can miss a match of multi-byte characters when a token is one byte.) StopStrings sits beside StreamDecoder in cuda/reply_text.py. At the first token whose text completes a stop string:
    • the reply's token list ends at that token;
    • on_tokens returns True; an engine that calls on after that gets True from generate_gated (0.3.6.2), which does not call on_tokens again for the reply;
    • finish_reason is stop;
    • usage, token_sha and the ids return_token_ids returns count the tokens through that token.
  • The cut runs inside on_tokens' error capture from fix: the CUDA server stops a request when its client has gone or its per-token work fails #57: a reply cut at a stop string finishes normally, and only a client that has gone raises RequestCancelled.
  • Streamed deltas hold back an end that may begin a stop string (StopPolicy.visible(partial=True), the Mac's rule). The match and everything after it are left out. Stop strings match the generated text, reasoning included, before split_thinking, as on the Mac (server/app.py:470-476, :495-497). Tool calls are parsed from the cut text.
  • Where the engine honours the stop (the 27B on one GPU and on two ranks; Flash Next, Nemotron and Qwen3.6 on one GPU), it ends in the round that holds the match. Concurrent streams end through the scheduler's cancel, after a later round. GLM, and Flash Next and Nemotron on two ranks, decode on to an end token or the reply limit (glm5_next/cuda/decode.py:453-454; qwen4_exp/cuda/engine.py:285-286; nemotron_h/cuda/app.py:230-231). The server returns the reply only up to the match.
  • ignore_eos where the engine reads it (GLM, GlmApp.reads_ignore_eos): end tokens inside the reply are decoded into its text, and finish_reason is length unless a stop string or a tool call ended the reply. This is the Mac's handling: with ignore_eos, StopPolicy.eos_ids is empty (server/stopping.py:26), so no token ends the stream (server/app.py:466) or is stripped from the end (:493).
  • docs/api.md: stop on both backends, and how CUDA applies it.

Commit 2: ignore_eos on the 27B's CUDA engine

  • Qwen27Engine.generate(..., draft=True, stop_eos=True) passes stop_eos to draft_decode on one GPU and to decode_tp on rank 0 of two. Rank 1 never reads it: rank 0 alone decides when the windows end (Q/decode_tp.py:186-188) and which rows are kept (:231). The request header is unchanged.
  • EXL3 packs of the 27B (one GPU) go through the same generate and decoders, so the flag reaches them too, <|im_end|> included: since 0.3.6.2 their end tokens include the ones generation_config.json lists (fix: Qwen3.8 Flash Next and 27B on EXL3 checkpoints stop at the end of the chat turn #69). Q/decode.py, Q/decode_tp.py and Q/multi.py have no EXL3 branch.
  • --parallel N: Scheduler.submit(..., stop_eos=True) stores the flag on the request's Stream. MultiDecoder._ends(s) gives the end tokens for that stream (none when it ignores them). The prefill step that takes a stream's first token, accept and take use it in place of the decoder-wide self.eos (Q/multi.py:145, :195, :201). Rank 1 builds its streams from the ADMIT message, prefills them in the steps rank 0 names and commits the paths rank 0 sends (Q/multi.py:342-356). The messages are unchanged.
  • The server passes stop_eos=not ignore_eos only to an engine whose generate takes it, with the same signature check it makes for draft (cuda/server.py:113). It goes through the generate that generate_gated calls, so each run of a reply that tool_choice: "required" restarts gets it too. GLM keeps its own handling. With ignore_eos on the 27B, end tokens inside the reply are text, and finish_reason is length unless a stop string or a tool call ended it, as in commit 1. Stop strings stay active.
  • Docs: ignore_eos on MLX, GLM CUDA and the 27B CUDA engine; Flash Next, Nemotron and Qwen3.6 CUDA still stop at end tokens (docs/api.md, docs/recipes/README.md). docs/recipes/adding-a-cuda-family.md describes stop_eos in generate and the reads_ignore_eos route GLM takes.

Not in this PR: ignore_eos for Flash Next, Nemotron and Qwen3.6 on CUDA. Each can follow the 27B's route (stop_eos in generate).

Behaviour changes

Request Before After
stop on any CUDA engine ignored; whole reply reply cut before the first match; finish_reason stop
ignore_eos not a boolean, or stop malformed (CUDA) accepted HTTP 400 before any stream bytes
ignore_eos: true on the 27B CUDA engine stopped at the first end token decodes to the reply limit; end tokens in the text, as on MLX; finish_reason length
ignore_eos: true on GLM CUDA end tokens left out of the text; finish_reason stop if the last token was one end tokens in the text, as on MLX; finish_reason length
ignore_eos: true on Flash Next, Nemotron or Qwen3.6 CUDA stops at an end token unchanged
Neither field unchanged

Exactness

  • Stop strings. The cut is decided token by token on the committed sequence, so equal token sequences are cut at the same token, however many tokens a round delivers. Drafted and serial decoding already give equal tokens, so they give equal content, usage and token_sha after the cut. No arithmetic changes. An engine that decodes on after the cut changes nothing the client receives.
  • ignore_eos. One flag reaches every 27B decode path, and each applies the same end rule with it: serial (Q/decode.py:94), drafted (Q/decode.py:205, :237), two ranks (Q/decode_tp.py:187, :231) and concurrent (cuda/streams.py:64 in accept, :43 in take). stop_eos=False is the setting the CUDA tests already compare drafted with serial at. Drafts, verify windows, sampling keys and commits are unchanged.
  • Caches. States are kept at message starts and at the prompt's end during prefill, before any reply token (Q/engine.py:167-171; Q/multi.py:159-164), so no token past an end token enters a cache.

On GPU, commit 2's tests pass on one Spark, and served ignore_eos replies give the same token_sha drafted and with "draft": false on one Spark and on two (GPU status, below). EXL3 packs and a served --parallel 2 have not run.

Tests

Host: macOS, 36 GiB, Python 3.13.5, PyTorch 2.11.0 CPU, MLX 0.31.1, an import-only stand-in for Triton (no kernel launches). The three new test files also pass without it.

  • tests/test_cuda_stop_strings.py (commit 1, 168 cases). A byte-level tokenizer (one token per byte, so a character can span tokens) and fake engines that deliver drafted rounds of 1 to 7 tokens, or one token a round for "draft": false. Chat and raw completions, streamed and not.
    • Twelve stop strings at different offsets: at the first byte, inside the first round, across round and stream boundaries, multi-byte, repeated. Drafted and serial give equal content, finish stop, completion_tokens and token_sha. The drafted engine ended in the round that holds the match, and the serial one at the matching token.
    • A list takes the earliest match. A held-back partial match that never completes is sent at the end.
    • A stop string longer in bytes than the Mac's character tail is still found.
    • Stop strings match reasoning and answer before the split. Tool calls are parsed from the cut text.
    • An engine that ignores the stop result gives the same reply.
    • With return_token_ids, drafted and serial replies, streamed and not, return the ids through the token that completes the match, with their token_sha.
    • Eleven malformed values for ignore_eos or stop, on the plain and the GLM app: HTTP 400, no stream bytes, no engine call, and check() agrees.
    • ignore_eos on an engine that reads it: end tokens are text, finish length (also when the reply limit falls on an end token), stop strings still apply, and tool calls are parsed from text that keeps the end tokens. On an engine that does not read it, replies are unchanged.
  • tests/test_cuda_27b_ignore_eos.py (commit 2, 27 cases). The server passes stop_eos only to an engine whose generate takes it, and to each run of a tool_choice: "required" reply that the call gate restarts. Stop strings stay active under ignore_eos. accept and take follow the end tokens they are given. The 27B's MultiDecoder runs over a scripted model on the CPU stand-ins of tests/test_cuda_failed_admission.py, with a stream that stops and one that ignores end tokens side by side, drafted and serial, including a first token that is an end token. The scheduler hands the flag to its stream. Qwen27Engine.generate passes stop_eos to draft_decode on one GPU, to decode_tp on rank 0 of two, and to its scheduler. The ADMIT message and the header rank 1 reads are unchanged.
  • tests/cuda/test_qwen27_ignore_eos.py (commit 2, GPU, 8 cases). Fixtures are built as in tests/cuda/test_qwen27_multi.py, with centred weights (a group's bias is -7.5 times its scale; with that file's biases a greedy path repeats one token). Each path's end token is a token it first produces after its first token and before its last, or its first token when it repeats one token. Each test asserts that at least one of its replies stops after its first token.
    • draft_decode with stop_eos=False and a drafter that proposes the true continuation (chains of 2 to 5, so the end token is accepted mid-round) equals serial_decode with stop_eos=False. With stop_eos=True both stop at the end token.
    • Qwen27Engine.generate with and without stop_eos equals a fresh prefill plus serial_decode: drafted from a fresh prefill, "draft": false, and a longer prompt resumed from the first prompt's kept end.
    • MultiDecoder: three streams (two drafted, one serial), one of them ignoring end tokens, each equal to its serial reference with its own flag.
    • Scheduler: three concurrent requests, one ignoring end tokens, each equal to its serial reference.
Run Result
test_cuda_stop_strings.py on main's sources (71377a5) 156 failed, 12 passed (requests whose replies commit 1 does not change, and the engine-ignores-the-stop check)
Same file, commit 1 168 passed
test_cuda_27b_ignore_eos.py on commit 1 21 failed, 6 passed (requests without ignore_eos: true, and the existing accept/take rule)
All three new files, commit 2 195 passed, 1 skipped (the GPU file)
Whole host suite (tests without tests/cuda), main (71377a5) 136 failed, 2,082 passed, 299 skipped, 18 errors
Same, commit 1 136 failed, 2,250 passed, 299 skipped, 18 errors
Same, commit 2 136 failed, 2,277 passed, 299 skipped, 18 errors

The same 154 ids fail or error in all three whole-suite runs, and none is in this PR's test files. Their causes on this host include transformers classes that are not installed (AutoTokenizer, GenerationMixin), no Triton backends, MLX 0.31.1 where GLM-5.3-Flash needs 0.32.2, a memory-window test that expects a Mac with 64 GiB or more, and files outside src and tests (tools/, CHANGELOG.md) that the test tree used here did not include. Twenty-two one-line breakages of the change, each applied alone, each fail between 1 and 68 cases of the two host files; they include a stop-string cut that raises RequestCancelled, a tool_choice: "required" re-run without stop_eos, and a concurrent stream's first token taken with the decoder's end tokens.

GPU status

Step 1, step 2 without its EXL3 file, and step 3 on one Spark and on two Sparks with --tp 2 passed. Surface: DGX Spark (GB10), one for steps 1 and 2 and the one-Spark check, two for the --tp 2 check; the tensorfold-bench image (PyTorch 2.13.0a0, NVIDIA 26.07; CUDA 13.3), TORCH_CUDA_ARCH_LIST=12.1; TensorFold 3f5e339 (commit 2), this branch on main 71377a5, imported from the branch's tree.

Step Command Result
1 python3 -m pytest tests/cuda/test_qwen27_ignore_eos.py (toy models) 8 passed
2 python3 -m pytest tests/cuda/test_qwen27_multi.py tests/cuda/test_qwen27_prefill.py tests/cuda/test_qwen27_forward.py tests/cuda/test_qwen27_client_gone.py (toy models) 35 passed (14, 14, 5 and 2); main (71377a5), in its tests/cuda run: the same 35 passed

Step 3: the 27B, Qwen3.8-27B-MLX-4bit (70ae7fac) with the DFlash2 drafter (50307d4c), under tensorfold serve <Qwen3.8-27B-MLX-4bit directory> --drafter <DFlash2 directory>, --tp 1 on one Spark and --tp 2 across two. Each request is the chat message "Reply with the single word: yes", greedy (temperature 0), thinking off, max_tokens 256.

Request One DGX Spark (GB10) Two DGX Sparks (GB10), --tp 2
No ignore_eos yes, 2 tokens, finish_reason stop yes, 2 tokens, stop
ignore_eos: true, drafted 256 tokens, length, token_sha a9d4bf8e3f47 256 tokens, length, 643191f611c4
ignore_eos: true, "draft": false 256 tokens, length, a9d4bf8e3f47 256 tokens, length, 643191f611c4
stop, a string first found after the model's first end token [" \"yes\".\n"] ["d with \""]
ignore_eos and stop, drafted 47 tokens, stop, 3b56ffac2404 88 tokens, stop, 60287c7e016a
ignore_eos and stop, "draft": false 47 tokens, stop, 3b56ffac2404 88 tokens, stop, 60287c7e016a
  • Every ignore_eos reply begins with yes, then <|im_end|> and <|endoftext|> as text.
  • Each reply cut at a stop string is the ignore_eos reply up to that string, which is left out.
  • The cut requests ended early: drafted, 18 decode rounds on one Spark (63 for the uncut reply) and 26 on two (48); with "draft": false, one round per token after the first (46 and 87).
  • token_sha is compared within a rank count. The one-Spark and two-Spark ignore_eos replies differ: each rank count has its own serial reference (docs/recipes/qwen3.8-27b.md:58-59).
  • No traceback in any rank's log.

Not run:

  • Step 2's tests/cuda/test_qwen27_exl3.py with TENSORFOLD_QWEN27_EXL3 and TENSORFOLD_QWEN27_DRAFTER set, so that its checkpoint tests run Qwen27Engine.generate: no EXL3 pack of the 27B was on the test machines.
  • Step 3's repeat with an EXL3 pack on one Spark, for the same reason.
  • Step 3's --parallel 2 repeat (two concurrent requests, one with ignore_eos).
  • Step 4, tools/bench_openai.py against the 27B. Expected: every cell's completion_tokens equals --tokens.
  • Step 5, which exercises commit 1's GLM ignore_eos change: GLM on two Sparks with ignore_eos: true and a prompt it ends early. Expected: the reply's text holds the end token's text where the model ended, finish_reason is length, and drafted and "draft": false give the same token_sha.

Evidence

From reading the code at main, the host tests above and the GPU runs under GPU status. No speed figure is claimed.

Overlaps


Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).

…ecked before a stream opens

- The CUDA server reads `stop` (a string or a list) and cuts the reply token by token: at the first token whose
  text completes a stop string, the reply's tokens end there, on_tokens returns True, and the match and everything
  after it are left out. Drafted rounds of many tokens and "draft": false rounds of one therefore give the same
  content, usage and token_sha. With return_token_ids, the ids listed end at the same token.
- The 27B (one GPU and two ranks), and Flash Next, Nemotron and Qwen3.6 on one GPU, stop in the round that holds
  the match; concurrent streams stop after a later round. GLM, and Flash Next and Nemotron on two ranks, decode
  on to an end token or the reply limit, and the server returns the reply only up to the match. A reply cut at
  a stop string finishes normally; only a client that has gone raises RequestCancelled.
- Streamed deltas hold back an end that may begin a stop string, as on the Mac (StopPolicy.visible); stop strings
  match the generated text, reasoning included, before it is split; tool calls are parsed from the cut text.
- A non-boolean ignore_eos or a malformed stop is refused with HTTP 400 before any stream bytes (stop_options, as
  on the Mac).
- Where the engine reads ignore_eos (GLM), end tokens inside the reply are text, as on the Mac, and finish_reason is
  "length" unless a stop string or a tool call ended the reply.
- StopStrings sits beside StreamDecoder in cuda/reply_text.py. docs/api.md: stop on both backends.
- Qwen27Engine.generate takes stop_eos (default True) and passes it to draft_decode on one GPU and to decode_tp on
  rank 0 of two. Rank 1 follows rank 0's windows and never reads it, so the request header is unchanged.
- With --parallel N, Scheduler.submit takes stop_eos and each Stream carries it; MultiDecoder accepts and ends a
  stream at end tokens only when its own request stops at them. Rank 1 commits the paths rank 0 sends, so the
  ADMIT message is unchanged.
- The server passes stop_eos=not ignore_eos to an engine whose generate takes it (the signature check it already
  makes for "draft"), in each run of a reply that tool_choice "required" restarts too. With ignore_eos, end
  tokens inside the reply are text, as on the Mac, and finish_reason is "length" unless a stop string or a tool
  call ended the reply. Stop strings stay active.
- Serial, drafted and concurrent decoding apply the same end rule, the first token of a concurrent stream's
  prefill step included; states kept at message starts and prompt ends are taken during prefill, so no token past
  an end token enters a cache.
- docs: ignore_eos on MLX, GLM CUDA and the 27B CUDA engine; Flash Next, Nemotron and Qwen3.6 CUDA still stop at
  end tokens.
@ashhart

ashhart commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Stop strings on every CUDA engine and ignore_eos on the 27B are in 0.3.6.3, under your name (88407d16). Stops
match token by token, so a wide drafted round can't move the cut, and drafted replies still equal "draft": false
ones with both options on. Thank you for a careful one.

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