Skip to content

Studio: Qwen-Image-Edit streamed-tier activation seed keeps more of the DiT resident on tight cards - #15645

Merged
danielhanchen merged 4 commits into
mainfrom
studio-qedit-streamed-seed
Oct 28, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-qedit-streamed-seed

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Qwen-Image-Edit-2511 on a tight VRAM budget runs the streamed group-offload tier. Before this change, that tier was placed from the whole-render measured peak (6046 MiB, 7168 MiB reserved), and that peak already pads for the block prefetch window. The streamed room then subtracts the window a second time, so too few transformer groups stayed resident.

This PR adds a streamed-tier seed, _MEASURED_STREAMED_IMAGE_PEAK_MIB["qwen-image-edit"] = 3824 (4608 MiB reserved). It was measured with every DiT group streamed, with the window excluded, and it is read only where the window is subtracted on its own. The request guard prices requests on the same scale, through a 13th entry in the reserve tuple.

Two follow-ups make the wider residency safe:

  • Prompt encode: on the streamed-seed path the encode streams every resident group back out (encode_resident_transformer_mib = 0, the state the 12 GiB plan already uses). The flat encode room no longer stacks on top of the wider residency. Without this, a 16 GiB load overran during the warm-up encode: the allocator hit OOM, then retried.
  • VAE kernel prebuild child: the child's free-memory gate now subtracts the streamed seed plus the prefetch window that the first render still allocates. When the child would not fit, it is skipped and the parent compiles the fused VAE kernels as before. Without this, the child raised torch.OutOfMemoryError at 16 GiB.

The kill switch is UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_STREAMED=0. It restores the previous placement, the encode room and the prebuild gate. UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_QWEN_EDIT=0 still drops the measured seed entirely.

Results

RTX PRO 6000, budget capped at 12 / 16 GiB. Base is the merge base (71dd6d2). Arms alternate in ABBA order, 3 repeats per arm, a fresh Studio home per cell, and 3 timed renders after a warm-up. Times are the median s/image over 3 new prompts.

budget base s/image (3 reps) head s/image (3 reps) change base A/A spread resident groups base / head
12 GiB 48.388 / 48.380 / 48.397 45.932 / 45.879 / 45.883 -5.1% 0.04% 2 / 7 of 61
16 GiB 45.295 / 45.286 / 45.324 44.088 / 44.074 / 44.106 -2.7% 0.08% 9 / 14 of 61
12 GiB, 1328x1328 source plus one reference image (2 pairs) 77.896 / 77.921 77.966 / 77.986 +0.08% 0.03% 2 / 7 of 61
  • Output: all 48 PNGs are byte identical between base and head (md5 per image, every arm).
  • OOM: head logged no allocator OOM or OutOfMemoryError lines in any process. The prebuild child exited 0 in every base arm and was skipped in every head arm.
  • First image: load plus cold render is unchanged within noise.
  • Larger request: this shape gains nothing and is 0.08% slower. The request guard streams the extra groups for the larger request.

Tests

studio/backend/tests/test_diffusion_qwen_edit_streamed_seed.py (CPU only, sizes taken from the 12 / 16 GiB load logs):

  • the seed value and its pixel scaling;
  • wider residency at both budgets, never above free memory;
  • both kill switches;
  • the request guard on the streamed scale;
  • the encode on the streamed-seed path streams every resident group and never holds more than the resident-seed placement does (fails without the change: 4180 MiB kept at 16 GiB);
  • the prebuild gate skips the child when the pending streamed runtime leaves too little room, and is unchanged for resident-seed placements.

test_diffusion_vae_prebuild.py still passes.

Place the streamed group-offload tier of Qwen-Image-Edit-2511 from a
streamed-tier measured peak (3824 MiB, 4608 reserved) that excludes the
prefetch window, instead of the resident seed that already pads for it.
At a 12 / 16 GiB budget this keeps 7 / 14 of 61 transformer groups resident
(was 2 / 9).

The prompt encode on that path streams every resident group back out, so the
wider residency never stacks on top of the encoder. The VAE kernel prebuild
child is priced against the streamed runtime the first render still
allocates, and is skipped when it would not fit.

UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_STREAMED=0 restores the previous
placement.
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T03:11:54.827462Z 6b3da8a 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.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 6b3da8ad0e

ℹ️ 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
danielhanchen merged commit 602eda4 into main Oct 28, 2026
2 of 35 checks passed
@danielhanchen
danielhanchen deleted the studio-qedit-streamed-seed branch October 28, 2026 04:19
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 29, 2026
… of the int8 DiT resident under a VRAM budget (unslothai#15985)

## Summary

When Qwen-Image-2512 text-to-image runs under a VRAM budget, Studio
streamed every offload group of the int8 DiT. Nothing stayed resident.
At 16 GiB the Studio process used 5.4 GiB of the budget, and each
denoising step re-uploaded the whole 31.7 GiB transformer.

The cause is that the `qwen-image` family had no entry in the shipped
activation seed tables. With no seed and no learned peak, which is the
case on a fresh home, `_refine_plan_from_loaded_weights` has no headroom
figure. It returns the flat all-streamed plan. Qwen-Image-Edit had the
same problem, and unslothai#15542 / unslothai#15645 fixed it for that family only.

## Change

- `diffusion_memory.py`:
- `_MEASURED_IMAGE_PEAK_MIB["qwen-image"] = 6032`. This is the
whole-render peak Studio itself recorded for Qwen-Image-2512 on sm_120
(1024x1024, 20 steps, true CFG 4, int8 DiT and int8 encoder). A
returning home learns the same figure.
- `_MEASURED_STREAMED_IMAGE_PEAK_MIB["qwen-image"] = 3824`. This is the
Edit streamed figure. The model uses the same DiT class and the same
VAE, and text-to-image has half the image tokens (no source latents), so
this figure bounds the text-to-image peak.
- Kill switch: `UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_QWEN_IMAGE=0`
drops both seeds for this family only and restores the previous
all-streamed placement.
`UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_STREAMED=0` still drops only the
streamed figure.
- Scope:
- Only torchao int8 / fp8 denoisers on the default and max tiers read
these seeds.
- Dense bf16 (the eager table), GGUF, packed and fp8-stored denoisers
keep their current placement, as does the off tier.
  - A learned peak still raises the seed as before.
- The seed changes which groups stay on the card, not the math.

## Measurements

The base is origin/main `2a7e3a553e`, which already includes unslothai#15930.
Setup:

- Qwen-Image-2512, Studio auto (int8 DiT, int8 encoder), 1024x1024, 20
steps, true CFG 4, empty negative.
- One RTX PRO 6000 Blackwell (sm_120) VM. The budget is held by a VRAM
ballast plus a capped device total.
- Base and head arms are interleaved, each a fresh process with a fresh
Studio home.
- Each arm runs a warm-up, then reports the median s/image over 3 new
prompts.

| budget | base (r1 / r2 / r3) | head (r1 / r2 / r5) | change | head,
kill switch |
|---|---|---|---|---|
| 16 GiB | 12.913 / 12.889 / 12.927 s | **11.466 / 11.381 / 11.339 s** |
-11.7% | 12.984 s (same as base) |
| 12 GiB | 12.891 s | **11.991 s** | -7.0% | |

- A/A spread: base 0.3% (3 repeats), head 1.1% (3 repeats). Both deltas
are far outside the spread.
- Placement:
- 16 GiB: 7101 MiB resident (14 of 61 offload groups), against 0 on
base.
  - 12 GiB: 3319 MiB resident (7 of 61).
- Log line: `measured-activation placement group -> group (7343 MiB of
the 32496 MiB transformer ... stay resident (measured peak 7168 MiB)`.
- Torch profiler, one 16 GiB render:
- host-to-device traffic during denoise falls from 666.4 GiB to 520.8
GiB (33.3 to 24.8 GiB per step);
  - copy time not hidden behind compute falls from 5.75 s to 4.18 s;
  - kernel busy time is unchanged (6.985 vs 6.972 s).
- Peak VRAM of the Studio process at 16 GiB rises from 5.4 to 12.4 GiB,
inside the budget. At 12 GiB it rises from 5.4 to 8.7 GiB.
- No out-of-memory line in any log. At both budgets the VAE prebuild
child is skipped, as on Qwen-Image-Edit, because the streamed placement
still allocates 6229 MiB.
- Both guidance branches still share each streamed block's upload. Every
render on base and head logs `diffusion.cfg_pair: 19 guided step(s) ran
both branches through each streamed block`. With
`UNSLOTH_DIFFUSION_CFG_PAIR=0` the times are 24.481 s on base and 19.702
s on head.
- Output: every PNG is md5-identical across all 13 Studio arms on this
VM: base, head, kill switch, pair off, 12 and 16 GiB, and profiler runs.
- No regression where the DiT already fits: on an A100 40 GB with no
budget, the DiT stays resident and the placement does not change. Base
12.114 / 12.122 s, head 12.138 / 12.113 s, kill switch 12.140 s. All
PNGs are md5-identical.

## Tests

- New `tests/test_diffusion_qwen_image_seed.py` (CPU only, sizes taken
from the 16 / 12 GiB load logs):
  - the seed values and their pixel scaling;
- partial residency at 16 and 12 GiB, never above free memory, with the
encode streaming the resident groups back out;
  - both kill switches;
  - the off / eager tiers and a dense bf16 denoiser keep the flat plan.
- 56 placement, offload, CFG and Qwen suites: head 1525 passed, base
1516 passed. The same 6 tests fail on both, all pre-existing in
`test_diffusion_qwenimage21_vision.py` and
`test_diffusion_qwenimage_rope.py`.

## Not covered

- Measured on sm_120 (16 / 12 GiB budgets) and sm_80 (resident) only. No
consumer 16 GB card was used, and the budget was simulated.
- The streamed figure is borrowed from Qwen-Image-Edit, not measured for
text-to-image. The peaks measured here stay inside the budget at 16 and
12 GiB.
- Requests larger than 1024x1024 were not run. The request guard
re-streams groups for them, as for the other seeded families.
- Qwen-Image (the original release) shares the family but was not run.
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