Skip to content

Studio Qwen-Image-2.1: encode a condition image again after load_state_dict(assign=True) on its VAE - #17197

Merged
danielhanchen merged 2 commits into
mainfrom
dh/vae-latent-cache-param-identity
Nov 9, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/vae-latent-cache-param-identity

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #17092, which reuses a condition image's VAE latents across Qwen-Image-2.1 / Turbo renders.

What happened

The cache key tells a weight change apart by _weights_revision, the tensor count plus the sum of the tensors' version counters. An in-place load_state_dict bumps those counters, so it encodes again. load_state_dict(..., assign=True) does not write in place: it swaps in the incoming tensors, whose counters can equal the old ones. The VAE object, config, dtype and method overrides are also unchanged, so the next render with the same pixels hit the old entry and returned latents encoded with the previous weights.

With the tiny AutoencoderKLQwenImage21 and the real _encode_vae_image, assigning a differently seeded state dict left the revision unchanged and the cached result differed from a fresh encode.

No default Studio flow does this today: every assign=True load builds the VAE before the pipeline installs the cache. It is still a silent wrong-output path for any later reload, so it is closed here.

Fix

When the cache first sees a VAE it registers a load_state_dict post hook on each of the VAE's modules. Any load on the VAE or one of its submodules (in place or assign=True) clears the entries and bumps a generation number that is part of the key, so an encode that was in flight during the load cannot be served afterwards. Offload moves that swap .data do not run the hook and keep their hits, as before. A VAE that refuses the hook is never cached. The per-render cost is one attribute read; the hooks are registered once per VAE.

UNSLOTH_REFERENCE_VAE_CACHE=0 still turns the cache off.

Tests

studio/backend/tests/test_diffusion_reference_cache.py:

  • test_assigned_weights_encode_again: assign=True on the whole VAE and on vae.decoder both encode again and equal a fresh encode. Fails on main (stale latents), passes here.
  • test_a_vae_that_takes_no_hooks_is_never_cached: a VAE that refuses the hook bypasses the cache.

CPU, diffusers 0.42.1 / torch 2.10 and diffusers 0.41.0 / torch 2.10 (cpu): main 2 failed / 8 passed, this branch 10 passed on both.

@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T03:03:00.096706Z 18b8a84 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 18b8a84a7e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 9, 2026
…uff-unsloth wheel (#17211)

## Summary

The Backend CI step that installs the pinned `ruff-unsloth` wheel
retries 3 times with 5 s and 10 s gaps. Between 05:19 and 05:40 UTC
today, GitHub served intermittent 504s for the release asset, and every
attempt of an unlucky job landed inside one burst. That turned `Repo
tests (CPU, fsdp2 / gloo-cp / gloo-pp / gloo-tp / rest /
studio-install)` red on #17188, #17191, #17197, #17201 and #17202, while
sibling jobs in the same runs installed the wheel fine a minute later.

This raises the loop to 6 attempts with exponential backoff (10, 20, 40,
80, 160 s, about 5 minutes total). A healthy download still never
sleeps, and a lasting outage still fails the job with the same
`::error::` line, so the fixed-point formatter guard keeps failing
loudly in CI when the wheel is truly unavailable.

## Changes

- `.github/workflows/studio-backend-ci.yml`: 6 attempts, `sleep $((10 <<
(attempt - 1)))`.
- `tests/test_ruff_unsloth_install_retries.py`: runs the step's own
`run:` script under bash with `pip` and `sleep` faked on `PATH`, and
checks that
- 5 failed downloads in a row still install, after at least 300 s of
backoff,
- a download that never recovers still fails the job, in under 10
minutes,
  - a healthy download does not wait at all.

## Testing

- `pytest tests/test_ruff_unsloth_install_retries.py`: 3 passed.
- Against the old step from `main`, the burst test fails with
`ruff-unsloth install failed after 3 attempts`.
- The other tests that read `studio-backend-ci.yml` pass.
@danielhanchen
danielhanchen merged commit 8429c9a into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the dh/vae-latent-cache-param-identity branch November 9, 2026 12:25
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