Skip to content

serve: advance GLM and DSML tool calls from their cursor, scanning and streaming only the new text - #1010

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-opt-tool-stream-linear
Oct 30, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-opt-tool-stream-linear

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

GLM, Solar, DSML 도구 호출 스트림이 push마다 전체 버퍼를 다시 읽던 것을 새로 들어온 텍스트만 읽도록 바꿨습니다.

What and why

Streaming a tool call in the GLM key/value markup (also used for Solar) or in DSML re-parsed the whole buffer on every push and re-escaped and cloned all arguments so far, so one long string argument cost quadratic time and memory. Qwen's XML stream already reads from a cursor. This PR gives the GLM and DSML scans the same shape: each keeps its own cursor, reads only the new text and sends only the new arguments. The cursor, the sent length and the value reader live once in crates/serve/src/callstream.rs, and the Qwen scan uses them too.

Release notes: Changed: streaming a long tool-call argument in the GLM, Solar and DSML formats now costs time and memory linear in its length instead of quadratic.

Code shape

The review reads these first. A box left unticked needs one line of why.

  • No second copy. Logic two models (or two call sites) share has one common owner; I lifted it there instead of
    copying it (AGENTS.md "Common code first", docs/rust-quality.md R14). Where I found an existing copy, I
    named it below.
  • Model code holds only architecture facts. A model's own files carry only what its architecture forces
    (tensor shapes, the attention kind, the quant). A common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. Nothing I made unused is left behind (functions, flags, gate clauses, docs).
  • No silent failure. Undefined input is a named error or panic, never a defined-looking output.
  • Comments state what is true now. No dates, issue numbers or how a bug was found (tools/check-comments.sh).
  • No relaxed gate. No band, tolerance or lint moved to make a check pass; a re-pin carries PIN(YYYY-MM-DD):
    and its reason.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": removed. The call cursor, sent-length bookkeeping and value reader that qwenxml.rs carried now live in crates/serve/src/callstream.rs:70 (Reading) and :293 (read_value), and glmxml.rs, dsml.rs and qwenxml.rs all use them.

What I ran

Allocation and host time for one string argument of 294000 bytes streamed in 3000 lines, one push per line, from crates/serve/tests/callstream.rs (hw_long_string_stream_is_linear, debug build):

cargo test -p bloomery-serve --test callstream -- --include-ignored hw_long_string_stream_is_linear --nocapture
Metric Base Head Difference
GLM allocated bytes 3648169383 10529294 346x less
Solar allocated bytes 3647624663 9984574 365x less
DSML allocated bytes 3648106778 10466682 348x less
GLM host_us 11528918 34065 338x less
Solar host_us 11312658 32350 350x less
DSML host_us 11698738 33359 351x less

Output of that command on the base code:

glm: 3648169383 allocated for 294000 argument bytes
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 4 filtered out; finished in 11.37s

The test limits allocation to 128 times the argument bytes and covers both Qwen framings, GLM, Solar and DSML.

Host tests:

CARGO_BUILD_JOBS=4 cargo test -p bloomery-serve --lib --test callstream --test dsml --test glmxml --test qwenxml --test hermes -- --include-ignored --nocapture
test result: ok. 453 passed; 0 failed
test result: ok. 5 passed; 0 failed
test result: ok. 13 passed; 0 failed
test result: ok. 14 passed; 0 failed
test result: ok. 13 passed; 0 failed
test result: ok. 20 passed; 0 failed

The six results are lib, callstream, dsml, glmxml, hermes and qwenxml in that order.

Real binary on an RTX 5090, with an existing Qwen3.6-35B-A3B Q4_K_M model. bloomery-serve was started once per template (Qwen3-Coder, GLM, Solar, V4.1 DSML) with --model qwen3 --ctx 8192 --parallel 1 --no-mmproj and --chat-template-file for the three non-Qwen templates. A forced call with a 4864-byte string argument was sent once without and once with stream, and the streamed fragments were joined and compared byte for byte with the non-stream arguments:

cargo oxide build --arch sm_120a -- -p bloomery-gpu-gates --features glm5next,clef,mimo2,solar2,minimaxm3,flint,gemma-embedding2,hf,vision,checkpoint --release --bin bloomery-serve
bash tools/gpu-gate.sh --executable target/tool-stream/python3 -
NVIDIA GeForce RTX 5090, 595.99.02, 32607 MiB
qwen real-binary tool-stream PASS utf8_bytes=4864 argument_bytes=5390 fragments=1285 equal=true
glm real-binary tool-stream PASS utf8_bytes=4864 argument_bytes=5390 fragments=1412 equal=true
solar real-binary tool-stream PASS utf8_bytes=4864 argument_bytes=5390 fragments=1412 equal=true
dsml real-binary tool-stream PASS utf8_bytes=4864 argument_bytes=5390 fragments=1412 equal=true
gpu-gate.sh: python3 ran 46 s (exit 0)

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added the serve The HTTP server: crates/serve label Oct 30, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: No must-fix finding remains.

Non-blocking

⚠ Guard scanning independently of allocation: crates/serve/tests/callstream.rs:643. Replacing the cursor search at crates/serve/src/glmxml.rs:334 with self.buf.find(m.call_close) preserves the output and allocation assertions while restoring quadratic scanning.
For N equal b-byte argument pushes, the search ranges contain at least b × N × (N+1)/2 argument bytes [derived]; elapsed time is only printed. Add a deterministic bound on search work so this regression fails the test.

Checked

Duplicates: dryer reported zero candidate pairs. Qwen, GLM, and DSML share Reading, held, and read_value in callstream.rs; grammar transitions remain format-specific.
Complexity: diagnostics report new GLM CallRead::advance = 25 and DSML InvokeRead::advance = 19. Both use grammar-state matches. Complexity alone yields no finding.
Silent failure: typed GLM JSON failures reach KvError; malformed DSML returns content or Diverged. Name stages precede value stages.
Tests (static mutation analysis): omitting sent-length updates, accepting changed argument prefixes, ignoring divergence, or releasing held tag tails contradicts the changed unit assertions. Replaying cut arguments fails fragment equality; swallowing completed argument errors fails the integration assertions.
Gates: the Qwen allocation gate is extended to GLM, Solar, and DSML; its cap remains 128 times argument bytes. The PR body supplies FAIL-first allocation output.
Escape catalog: C1–C4 do not match the changed Rust files. No ledger lessons apply.

Notes

Validation was static; Rust tests were not rerun. The PR reports host-test results.
Add the measured base/head hashes, exact model filename, and just gate-* command output to complete the supplied performance/GPU evidence. The card line and application probe alone do not qualify.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 9540dc3 into midagedev:main Oct 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

serve The HTTP server: crates/serve

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant