Skip to content

fix(server): isolate the seeded RNG state per request (#4014) - #4033

Merged
Qubitium merged 1 commit into
mainfrom
fix/openai-server-seed-rng
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/openai-server-seed-rng

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Follow-up to #4014 (already fixed by #4029): a request that sets seed no 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:

if request.seed is not None:
    torch.manual_seed(request.seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(request.seed)

generate() is called under _generation_lock further down, at gptqmodel/utils/openai_server.py:460. The seed write happens before that lock and is never undone, so:

  1. Leak — the process-global RNG (CPU and every accelerator device) stays on the request's seed. Every later unseeded request samples from that stream instead of the process default.
  2. Race — the seed is written before the lock is acquired, so a second request can reseed between the first request's manual_seed() and its generate(). 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 concurrent seed=11 / seed=22 requests both observe initial_seed() == 22.

The fix

Capture the CPU (and, when the accelerator context already exists, device) RNG state, seed, generate and restore — all while _generation_lock is held. torch.cuda.is_initialized() is used instead of is_available() so a CPU-only request never creates a CUDA context just to read its state. The restore runs in a finally, 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"]
Loading
Mechanism (before) Where it lived Replacement
torch.manual_seed(request.seed) + torch.cuda.manual_seed_all(...) before the lock, never restored gptqmodel/utils/openai_server.py:421-424 (main @ 928d8dd3c) _capture_rng_state() / _restore_rng_state() around generate(), inside _generation_lock
with self._generation_lock, torch.inference_mode(): outputs = generate(...) gptqmodel/utils/openai_server.py:460-461 same lock, with seed + restore nested inside it

Runtime adapters (vLLM/SGLang) are unchanged: they take seed as a generate kwarg and never touch the process RNG.

Tests

tests/test_openai_server_contract.py:

  • test_seed_is_applied_to_native_generation — the seed reaches generate() (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:

$ PYTHONPATH=<main tree>:/tmp/srv-deps /opt/venvs/cu130t/bin/python -m pytest -q tests/test_openai_server_contract.py -k seed
FAILED tests/test_openai_server_contract.py::test_seed_does_not_leak_into_the_global_rng
FAILED tests/test_openai_server_contract.py::test_concurrent_seeded_requests_each_keep_their_own_seed
2 failed, 2 passed, 57 deselected

Pass-after, on this branch:

$ PYTHONPATH=$PWD:/tmp/srv-deps /opt/venvs/cu130t/bin/python -m pytest -q tests/test_openai_server_contract.py -k seed
4 passed, 57 deselected

$ PYTHONPATH=$PWD:/tmp/srv-deps /opt/venvs/cu130t/bin/python -m pytest -q tests/test_openai_server_contract.py
61 passed

$ HIP_VISIBLE_DEVICES=1 PYTHONPATH=$PWD:/tmp/srv-deps /opt/venvs/rocm101t/bin/python -m pytest -q tests/test_openai_server_contract.py
61 passed

$ ruff check gptqmodel docs/eora tests setup.py
All checks passed!

Real GPU check (RTX 4090 GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e, Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1, real uvicorn + openai 3.51.0):

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. No GPTQMODEL_* switches, no behaviour change for runtime adapters.

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.
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