Skip to content

Images: /v1/images/generations can run a replacement model with the previous model's steps and guidance #9448

Closed

Description

@oobabooga

Summary

POST /v1/images/generations picks the per-model step count and guidance from a status() read taken before the generation starts. If the loaded model is replaced in between, the request runs on the new model with the previous model's parameters, and the edit-only refusal was decided against the previous model too.

Where

studio/backend/routes/inference.py on main (20c015f):

status = backend.status()                                   # line 26384
...
workflows = status.get("workflows") or []                   # line 26390
if workflows and "txt2img" not in workflows:                # line 26391
    raise HTTPException(status_code = 400, ...)
...
steps, guidance = default_generation_params(                # line 26403
    status.get("repo_id"), status.get("base_repo")
)
result = await asyncio.to_thread(backend.generate, ..., steps = steps, guidance = guidance, ...)

DiffusionBackend.generate then binds state = self._state once it holds _generate_lock, which can be a different model than the one status() described.

Why the window is wide

begin_load raises its teardown fence, frees the old pipeline, and releases the fence immediately after _unload_locked, then holds _generate_lock alone for the whole construction of the new model. So _teardown_waiters is already 0 for the large majority of a replacement, and the refusal at diffusion.py:5463 does not cover it. A generate arriving in that window simply blocks on _generate_lock and proceeds against whatever committed.

Reproduce

Against studio/backend/tests/test_diffusion_backend.py on main, with two model directories:

  1. load_pipeline(old_dir, ...), record backend.status()["repo_id"].
  2. Start a replacement load_pipeline(new_dir, ...) on a thread, blocked inside _FakeTransformer.from_single_file. Assert backend._teardown_waiters == 0 here, so the fence is genuinely down.
  3. Call backend.generate(prompt = "x", steps = 2) from another thread; it blocks on _generate_lock.
  4. Release the replacement.

The generate returns with result["repo_id"] == new_dir. It ran on a model the route never validated, with old_dir's steps and guidance.

Impact

For a concrete pair, replacing a turbo model with a non-turbo one can run the latter at the turbo's step count and guidance, producing a bad image rather than an error. The edit-only 400 can also be answered from the wrong model in either direction.

The Studio /images/generate route takes steps and guidance from the request body, so it is the user's own input there. This endpoint derives them server-side, which is what makes it wrong rather than merely stale.

Fix direction

Resolve the per-model defaults and the edit-only check from the state actually admitted for the generation, rather than from a status() read taken before the call. That means generate() filling in the defaults when the caller does not supply them, instead of default_generation_params being called in the route.

Found while reviewing #8866, in #8866 (comment). It is not caused by that PR: the behaviour above reproduces on the merge base, because the fence was already down for the construction window. Filing separately so the route change is not folded into that fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions