Repository navigation
Conversation
…per-token work fails The CUDA server stopped a request only when a streamed write failed with a broken pipe or a reset. A non-streamed request always ran to its reply limit, a stream stopped only at a round that wrote text (not inside a hidden tool call), and a request queued behind the server lock was prefilled and decoded after its client had left. A write that timed out, or any other failure in the per-token callback, raised into the engine's decode loop; with two ranks that skips the stop that keeps rank 1 in step. - App.run takes cancelled=; the handler passes the Mac server's socket check (socket_cancellation) for streamed and non-streamed requests. The callback checks it every round, with or without new text, and asks the engine to stop. - A request whose client left while it waited for the lock raises RequestCancelled before generate: no prefill, no engine call, nothing written. - A request stopped for its client raises RequestCancelled once generate returns, as on the Mac server: no final chunk, no [DONE], no JSON reply. A client that has only shut its sending side does not get the stopped prefix as a whole reply with finish_reason "length". - The callback never raises into the engine. A failure is kept, the engine is asked to stop, and the failure is raised after generate returns. After a stop or a failure the callback returns True at once, for the engines that finish on both ranks and keep calling. - A streamed write treats any OSError (timed out, host unreachable) as a client that has gone; the JSON reply ignores a broken pipe or a reset. Engines stop through the path a failed streamed write already takes. No engine or arithmetic change; tokens sent before a stop are unchanged. Not covered: a request waiting inside the concurrent scheduler before admission is prefilled, then stops after its first round. Test: tests/test_cuda_server_disconnect.py (17 cases; 14 fail without the fix).
docs/api.md said the CUDA server serializes generation through its lock; with --parallel N the 27B (one or two ranks) and Flash Next (one rank) share rounds and skip it, as the CUDA recipe and the runbook say. It now says so, and what a disconnect does on CUDA. The recipe's note on engines that finish on both ranks names Nemotron too, which decodes to the end on two ranks whatever the callback returns.
Synthetic 27B weights, one stream and shared rounds: a request whose client leaves mid-reply sends a prefix of the exact reply and ends in RequestCancelled; a failing per-token callback raises after generate; the requests beside it and after it, and a resume from the stopped request's prompt end, keep their exact tokens. Not yet run on a GPU.
|
Thank you, @nood-co1. Before this, a CUDA request whose client had gone kept its turn on the GPU when We rebased it onto next-0.3.6.2 with your three commits unchanged, plus one small style commit that puts a |
Summary
Issue #26 (client disconnects do not cancel queued or running jobs) was fixed for the Mac server in 0.3.5. This PR does the same for the CUDA server, and keeps failures in the per-token callback out of the engine.
generatereturns.docs/api.mddescribes CUDA concurrency as the recipe and the runbook do, and says what a disconnect does.Why
In
src/tensorfold/cuda/server.pyatmain(34bae79):emit = lambda delta: True(:519). With the defaults (--max-tokens 4096,cli.py:40;--parallel auto, one request at a time on CUDA,cli.py:329), an abandoned non-streamed request holds the lock for its whole reply, and every request behind it waits.on_tokens(:376-389) stops only whenemitfails, andemitruns only on rounds that add visible text (:387). Inside a hidden tool call nothing is written, so nothing stops.generateis called (:393-395) whatever happened to the client while the request waited.emitcatches onlyBrokenPipeErrorandConnectionResetError(:482). A timed-out or unreachable client (ETIMEDOUT,EHOSTUNREACH) raises throughon_tokensinto the decode loop. So does any failure in the callback's own work (StreamDecoder.add, the reasoning split, tool-call hiding).An exception from
on_tokensdoes more than fail one request:decode_tpfrom its callback (families/qwen3_5/cuda/decode_tp.py:237-238) without broadcasting the empty window that stops rank 1 (:174-198). Rank 1 is still in its decode loop, so it reads rank 0's next request header (families/qwen3_5/cuda/engine.py:160-161) as a window. GLM's callbacks on rank 0 have the same shape (families/glm5_next/cuda/engine.py:298;decode.py:324,:387,:444).--parallel N).Scheduler.submitruns the callback on the request's thread (cuda/scheduler.py:33-34). The exception leavessubmit, and the stream stays in the decoder with nothing to stop it. On the host, with a stand-in decoder on the realScheduler, a stream whose callback failed at its third round decoded on to its count: 400 of 400 tokens.The Mac server already checks the socket for this (
server/cancellation.py:30-46, used atserver/http.py:185-186).Change
src/tensorfold/cuda/server.py:App.run(..., cancelled=None)takes a callable that is true once the client has gone.do_POSTpasses the Mac server's check,socket_cancellation(self.connection), for streamed and non-streamed requests.on_tokenscheckscancelled()every round, whether or not the round wrote text, and returns True to stop the engine.try/except Exception. The first failure is kept and the callback returns True. After a stop or a failure it returns True at once, without decoding or writing more. This matters for the engines that keep calling after a stop (two-rank Flash Next, Nemotron and GLM).generate, for a concurrent engine), a request whose client has gone raisesRequestCancelled. The handler closes the connection without writing more.generatereturns, a kept failure is raised and goes through the handler's existing error path.generatereturns, a request stopped for its client raisesRequestCancelled: no final chunk, no[DONE], no JSON reply. The Mac server's handler does the same (server/http.py:353-354,:435-436).emittreats anyOSErroras a client that has gone. The JSON reply (_json) ignoresBrokenPipeErrorandConnectionResetError.src/tensorfold/families/glm5_next/cuda/app.py:GlmApp.runpassescancelledthrough.Docs:
docs/api.md:45said "CUDA serializes generation through its server lock". With--parallel Nabove one, the 27B (one or two ranks) and Flash Next (one rank) share rounds and do not take that lock (cuda/server.py:393,docs/recipes/cuda.md:44-47,RUNBOOK.md:71-73). The paragraph now says so, and adds one sentence on disconnects.docs/recipes/cuda.md:55names Nemotron among the engines that finish on both ranks (families/nemotron_h/cuda/app.py:231).Behaviour notes:
[DONE]. Onmainit gets the whole reply.Schedulerbefore admission. It is admitted and prefilled, then stops after its first round. Skipping it needs a hook in the scheduler.Exactness
No engine file and no arithmetic change. Engines stop through the path a failed streamed write already takes:
engine.py:147-150,decode.py:190,:252-253.engine.py:171-173,decode_tp.py:175,:237-238.scheduler.py:33-34.Tokens sent before a stop are unchanged.
A skipped request makes no engine call. Every engine hands a request to rank 1 only inside
generate(27B:families/qwen3_5/cuda/engine.py:160-161; GLM:families/glm5_next/cuda/engine.py:348-349; Flash Next:families/qwen4_exp/cuda/engine.py:263; Nemotron:families/nemotron_h/cuda/app.py:227). A request skipped on rank 0 therefore never reaches rank 1, and the collective sequence stays aligned.Tests
Host: macOS, Python 3.13.5, PyTorch 2.11.0 CPU, an import-only stand-in for Triton (no kernel launches). No model ran.
tests/test_cuda_server_disconnect.py(new, 17 cases) serves the realServerand handler on127.0.0.1:0with a paced fake engine, a character tokenizer and a small template, astests/test_cuda_request_policy.pydoes. Every socket, join and wait has a timeout. The cases:generate, streamed or not, and the next request is served.App.runandGlmApp.runwithcancelledalready true make no engine call and release the lock, for serialized and concurrent engines.OSError(ETIMEDOUT)stops the engine without raising into it.generatereturns normally, for an engine that stops and for one that keeps calling to the end. The failure is raised afterwards, and nothing is sent after it.Schedulerwith a stand-in decoder ends the shared-round stream a round or two later.main's sources_jsonguard, which no case reachestestswithouttests/cuda),main(34bae79)main(34bae79)The same 151 ids fail or error on both, for host reasons: no
transformerstokenizer, no Triton backends, MLX 0.31.1 where GLM-5.3-Flash needs 0.32.2, and two tests that readtools/andCHANGELOG.md, which the test tree used here did not include. None is a CUDA server test.tests/cuda/test_qwen27_client_gone.py(new, GPU) drives the 27B decoders with the synthetic weights oftests/cuda/test_qwen27_multi.pythroughApp.run:RequestCancelled. A longer prompt then resumes from its kept prompt end with a fresh prefill's tokens. A request whose callback fails raises, and the next one is exact.Its harness (helpers,
App.run, the realScheduler) ran on the host with stand-in decoders. The 27B decoders did not.GPU status
One DGX Spark (GB10, sm_121); container: Python 3.12.3, PyTorch 2.13.0a0 (NVIDIA 26.07), CUDA 13.3, Triton 3.7.1.
TORCH_CUDA_ARCH_LIST=12.1was set: without it the 0.3.5 extensions do not build in this container (the failure #56 reports, fixed onmainin 0.3.6.1). Branch head 18d4f4d.tests/cuda/test_qwen27_client_gone.pytests/cudamain(beddbb7): 288 passed, the same 7 skippedStill pending:
--parallel auto): a non-streamed request withmax_tokens4096 whose client closes after 1 s. The next request should start within a round, and the log should show no traceback.token_shaequals a solo run's.Evidence
Surface: macOS, a stand-in engine at one token every 10 ms, the real CUDA
Appand handler, and the Qwen3.8-27B checkpoint's tokenizer and chat template; three trials per case. No model ran.main(beddbb7)generatecalled (3 of 3); stopped after 1 of 100 tokensgeneratenot called (0 of 3)generatecalled (3 of 3); ran 100 of 100generatenot called (0 of 3)[DONE](3 of 3)[DONE](3 of 3)The two-rank misalignment after a callback exception is from reading the code. It was not run on two ranks.
Overlaps
cuda/server.pyanddocs/api.md. Git merges the two branches in either order with no conflict, and both host test files pass on the merge (219 tests).on_tokens: 4 conflict blocks incuda/server.py. It is meant to land after this PR, rebased.main(34bae79) with no conflict; in the mergedApp.run, the stop and failure raises come before 0.3.6'sreturn_token_idsblock.Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).