Repository navigation
fix(server): isolate the seeded RNG state per request (#4014) - #4033
Merged
Merged
Conversation
The seed path called torch.manual_seed() before acquiring _generation_lock and never restored the previous state, so a seeded request moved every later unseeded caller onto its random stream, and a concurrent request could reseed the process between another request's seed and its generate() call. Capture the CPU and accelerator RNG state, seed, generate and restore inside _generation_lock. Regression tests cover the leak and the concurrent interleaving; both fail on the previous implementation.
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #4014 (already fixed by #4029): a request that sets
seedno longer leaks its RNG state into the rest of the process, and can no longer be reseeded by a concurrent request.Root cause
main@928d8dd3c,gptqmodel/utils/openai_server.py:421-424:generate()is called under_generation_lockfurther down, atgptqmodel/utils/openai_server.py:460. The seed write happens before that lock and is never undone, so:manual_seed()and itsgenerate(). The first request then samples from the second request's seed.Both are reproducible with a scripted model:
torch.get_rng_state()differs after a seeded request, and two concurrentseed=11/seed=22requests both observeinitial_seed() == 22.The fix
Capture the CPU (and, when the accelerator context already exists, device) RNG state, seed, generate and restore — all while
_generation_lockis held.torch.cuda.is_initialized()is used instead ofis_available()so a CPU-only request never creates a CUDA context just to read its state. The restore runs in afinally, so a generation error cannot leave the seed applied.flowchart TD A["OpenAiRequest with seed"] --> B["generate_kwargs built"] B --> C{"seed set and _runtime_generate is None"} C -- "no: runtime adapter" --> G["generate() with seed kwarg"] C -- "yes: native generate" --> D["acquire _generation_lock"] D --> E["_capture_rng_state()"] E --> F["torch.manual_seed(seed)"] F --> H["generate() under torch.inference_mode()"] H --> I["finally: _restore_rng_state(state)"] H -- "generate raises" --> I I --> J["response"]torch.manual_seed(request.seed)+torch.cuda.manual_seed_all(...)before the lock, never restoredgptqmodel/utils/openai_server.py:421-424(main @928d8dd3c)_capture_rng_state()/_restore_rng_state()aroundgenerate(), inside_generation_lockwith self._generation_lock, torch.inference_mode(): outputs = generate(...)gptqmodel/utils/openai_server.py:460-461Runtime adapters (vLLM/SGLang) are unchanged: they take
seedas ageneratekwarg and never touch the process RNG.Tests
tests/test_openai_server_contract.py:test_seed_is_applied_to_native_generation— the seed reachesgenerate()(torch.initial_seed()observed inside the call). Passes before and after; pins the behaviour that must not regress.test_seed_does_not_leak_into_the_global_rng—torch.get_rng_state()is unchanged after a seeded request.test_concurrent_seeded_requests_each_keep_their_own_seed— two overlapping seeded requests each observe their own seed.Fail-before, with the new tests run against
main@928d8dd3c:Pass-after, on this branch:
Real GPU check (RTX 4090
GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e,Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1, real uvicorn +openai3.51.0):_capture_rng_state/_restore_rng_stateround-trip on a live CUDA context: CPU restoredTrue, CUDA restoredTrue.seed=1234requests return identical text (True), and a following unseeded request returns a normal completion (finish_reason=stop).finish_reason=stop.Scope
Localized fix inside one endpoint path, so no new
docs/reference page or logic-flow page is added here; the protocol documentation for the wider #4014 changes is a separate concern. NoGPTQMODEL_*switches, no behaviour change for runtime adapters.