Skip to content

Close the generator after the server ends the response - #1

Open
cassieliang6709 wants to merge 1 commit into
turingH:fix/spec-prompt-cache-key-kv-misalignmentfrom
cassieliang6709:early-done-signal
Open

cassieliang6709 wants to merge 1 commit into
turingH:fix/spec-prompt-cache-key-kv-misalignmentfrom
cassieliang6709:early-done-signal

Conversation

@cassieliang6709

@cassieliang6709 cassieliang6709 commented Nov 25, 2026 •

Copy link
Copy Markdown

This goes on top of ml-explore#2083.

The finally block in speculative_generate_step feeds the tokens that were yielded but not processed into both caches. In the server that block runs when the stream_generate loop breaks, which is before rqueue.put(None), so the client waits for a target and a draft forward pass before the response ends.

This keeps a reference to the generator, ends the response first, then closes the generator. The caches are still filled before insert_cache.

Time from the last content chunk to the end of the stream, M1 Pro, Qwen3-4B-Instruct-2507-4bit with Qwen3-0.6B-4bit as the draft, 22 streamed requests per run:

median
a4b972f 30 ms, 33 ms (two runs)
this change 3 ms

tests/test_server.py, tests/test_generate.py and tests/test_prompt_cache.py pass (110 tests).

Closing a speculative generator feeds the tokens it yielded but did not
process into the caches. The server closed it before telling the client
the response was done, so every request waited for that extra forward
pass. Close it after the response ends; the cache is still complete
before it is saved.
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.

1 participant