Skip to content

fix: stream tool-call arguments from the CUDA server - #114

Closed
olexale wants to merge 1 commit into
ashhart:mainfrom
olexale:cuda-stream-tool-call-arguments
Closed

olexale wants to merge 1 commit into
ashhart:mainfrom
olexale:cuda-stream-tool-call-arguments

Conversation

@olexale

@olexale olexale commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

The CUDA server sends nothing while a tool call is being written. A reply that writes a whole file in one write call streams no bytes until the reply ends, so clients with an idle timeout drop it: pi through LiteLLM, and Node's fetch (undici), both cut the body after ~300 s of silence and report terminated. The harness retries, the same prompt regenerates the same call, and it fails again at the same place.

This is the second half of #99, which its author closed. That PR sent empty keepalive deltas. This one streams the call's arguments as they are written, the way the Mac server already does, so the client sees progress and gets the call without waiting for the end parser. References are at main (7a00336).

Cause

App.run hides tool-call markup from the answer (src/tensorfold/cuda/server.py:304), on_tokens emits only visible text (:332), and the handler sends the calls only after run returns (:532). The Mac server feeds the answer into ToolCallStreamer and emits its deltas as they arrive (src/tensorfold/server/app.py:431). The CUDA server never got that step.

The change

  • run builds a ToolCallStreamer(tools) when the request has tools and allows parallel calls. Each round it feeds the raw answer (never the reasoning) and emits the streamer's deltas after the visible-text delta: a header with the call's id and name, then arguments pieces.
  • run returns calls_streamed, the number of calls already sent. The handler sends only the calls after those, i.e. the ones the streamer could not follow, from the end parser as before.
  • Requests with parallel_tool_calls: false keep the end parser, which picks their single call (parallel_tool_calls: false is ignored, so a reply can carry several tool calls #21).
  • The cancellation check still runs every round, with or without new text.

Tests

  • tests/test_cuda_tool_choice.py: the streamed required call now arrives as one header naming get_weather followed by argument deltas that join to {"city": "Oslo"}. A new test checks that parallel_tool_calls: false still sends the call in one piece.
  • tests/test_cuda_stop_strings.py: the streaming helper merges argument deltas by index. The stop-string test that puts a second call after STOP still gets only the first call, so a call past the cut does not stream.

On macOS the full suite gives 2956 passed, 701 skipped. On a DGX Spark (NVIDIA PyTorch 26.07), the 21 files that exercise the CUDA server and ToolCallStreamer give 649 passed, 15 skipped, against 648 on main; the extra one is the new test.

On the GPU

One DGX Spark (GB10), Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP, --parallel 3 --context 262144 --kv-dtype int8 --mtp-drafts 6 --mtp-confidence 0.60 --ple-on-ssd. One streamed request offering a write(path, content) tool, asking for a single-file Breakout game in one call:

Thinking off main (7a00336) this branch
reply 16,170 tokens, 220 s 16,170 tokens, 223 s
first tool-call bytes 220.1 s 0.9 s
longest gap between SSE events 219.4 s 0.7 s
tool-call chunks 1 4,123
the call index.html, 47,443 chars the same bytes

The two replies are identical: the change sends the same tokens, only earlier. With thinking on, this branch streamed 25k characters of reasoning, then the call from 156 s to the end at 517 s (35,035 tokens, a 71,617-character file) with no gap over 0.5 s. On main that call's 361 s would pass silent, over the ~300 s idle cut of Node's fetch and LiteLLM.

Not covered

  • A non-streamed reply still sends its headers only when done.
  • A very long prefill before the first token is still silent.
  • parallel_tool_calls: false requests still hold their call until the reply ends.

The CUDA server (tensorfold/cuda/server.py) held tool calls back while they
were generated and sent them only once the reply ended. A long call, such as
writing a whole index.html, left the SSE stream silent for minutes, and
clients behind LiteLLM dropped the connection after ~300 s idle.

The answer now feeds ToolCallStreamer, as server/app.py already does, and its
argument deltas go out each round. The handler sends only the calls the
streamer could not follow. Requests with parallel_tool_calls=false keep the
end parser, which picks their single call.

Tests: the streamed required-call test checks the header plus argument
deltas, a new test covers parallel_tool_calls=false sending the call whole,
and the stop-string helper merges streamed arguments by index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@olexale
olexale force-pushed the cuda-stream-tool-call-arguments branch from eb66778 to 77619a9 Compare September 29, 2026 22:01
ashhart added a commit that referenced this pull request Sep 30, 2026
…t resends resume (#124), streamed tool calls (#114)

- CUDA prompts take bf16 activations by default; --prefill-fp8 restores FP8 prompts; both ranks agree on the precision;
  block-scaled FP8 checkpoints load (#126)
- Flash Next keeps each prompt's state one token early, so identical resends and next thinking turns resume (#124)
- Tool-call arguments stream as they are written on the CUDA server (#114)
- Before M5, Flash Next hyper-connection projections of 8+ rows run in 8-row tiles with the per-row bits
- --checkpoint-slots on CUDA (#125)
@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

We measured it on a GB10 with Flash Next and a request that writes one file in a 3,236-token tool call: before, the whole call arrived at once when the reply ended, at 33.7 s; with your change its name arrives at 0.51 s and the first argument byte at 0.57 s, never more than half a second apart, and the joined call is byte-identical to the unstreamed one by token sha. The CUDA server now feeds the reply's answer text, never its reasoning, to the same streamer the Mac server uses, so every CUDA family gets the streaming, and a call cut at max_tokens streams a partial call with finish_reason length, as on the Mac. Decode and replies are unchanged, at one and four streams.

@ashhart ashhart closed this Sep 30, 2026
BlivionIaG referenced this pull request in BlivionIaG/TensorFold Oct 20, 2026
…t resends resume (ashhart#124), streamed tool calls (#114)

- CUDA prompts take bf16 activations by default; --prefill-fp8 restores FP8 prompts; both ranks agree on the precision;
  block-scaled FP8 checkpoints load (ashhart#126)
- Flash Next keeps each prompt's state one token early, so identical resends and next thinking turns resume (ashhart#124)
- Tool-call arguments stream as they are written on the CUDA server (#114)
- Before M5, Flash Next hyper-connection projections of 8+ rows run in 8-row tiles with the per-row bits
- --checkpoint-slots on CUDA (ashhart#125)
@ashhart ashhart added the landed Landed on main by the maintainers; the commits keep the contributor's authorship label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

landed Landed on main by the maintainers; the commits keep the contributor's authorship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants