Skip to content

Studio diffusion: price the compiled tier's requests from its own learned peak (offloaded bf16 keeps its residency after compile) - #15198

Merged
danielhanchen merged 5 commits into
mainfrom
studio-diffusion-compiled-peak
Oct 25, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-diffusion-compiled-peak

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On an offloaded dense bf16 load, the deferred speed profile (compile on the 3rd image) made every compiled render keep less of the DiT resident than the eager renders did.

  • Colab L4, FLUX.1-schnell bf16: the load keeps 13498 MiB of the DiT resident. From image 3 on, every render logged streaming 3840 MiB of resident groups for an oversized request and kept 4321 MiB fewer. The image-3 denoise moved 53.3 GiB host to device, against ComfyUI's 17.8 GiB.
  • Cause: engaging the profile set _unsloth_peak_static. That sends every later request in measured_request_extra_mib to the flat estimate, 8192 MiB for FLUX.1 at 1024x1024, against the 4352 MiB the load reserved. The compiled tier also never recorded a learned peak of its own:
    • the identity kept graphs:off while the renders ran graphs:step;
    • once a graph was captured unrecorded, every later probe saw it as dirty.

Changes

  • The compiled tier records its own learned peak.
    • After the profile engages, the identity's graph part follows the mode the session runs (_tier_graph_mode). That mode stays the same through the first capture: the armed mode until the graphs are captured and then dropped, or settle eager, then off. So the render that captures the graph records, and the renders after a dropped graph record under graphs:off.
  • The request guard prices compiled renders from that peak.
    • measured_request_extra_mib takes tier_peak_mib, which is read only under the live compiled key, graph mode included (_tier_request_peak).
    • The price is the larger of that peak and the eager pricing the load reserved. A compiled tier that measures lower never lowers it.
    • The eager peak never stands in for the compiled tier here. Graph modes do not stand in for each other either. Measured on B200: the render that captured a graph recorded 1447 MiB, and the renders after the graph was dropped recorded 2546 MiB.
    • With no measurement yet (the first compiled render, a render waiting on the background compile, LoRA adapters, CFG not covered by the peak), the flat estimate stands in, as before.
  • No graph re-capture. A CUDA graph captured over a release keeps that release while the graph is live (_hold_captured_release), because graphs key on weight addresses. Once no graph is live, the measured peak places the renders.
  • Reuse of Studio diffusion: choose whole-module vs streamed offload per request from its step count #15163: its key list is now _learned_peak_keys / _learned_peak_for_keys, shared by the whole-module per-request path (unchanged: eager keys still stand in there) and the guard (eager_stand_in = False).
  • Kill switch: UNSLOTH_DIFFUSION_TIER_PEAKS=0 restores the static reserve and the flat estimate.

Results

Local B200 with an L4-sized cap: the planner sees 23034 MiB, of which 450 MiB is held by others, and the caching allocator is capped there. FLUX.1-schnell bf16 (transformer_quant=none), 1024x1024, 4 steps, a new prompt per render, 9 timed renders plus 1 profiled render per process, fresh learned-peak store per process. Timing arms had an exclusive lease. Base is the merge base 3bf5e85.

Session 1 is a first session (empty store). Session 2 reuses the store a session-1 process wrote.

arm DiT resident, images 3-9 (MiB) H2D, profiled render s / image, image 9: median (range) over 3 processes s / image, images 8-9 images
base, session 1 (3 processes) 9825 on every compiled render 63.17 GiB 1.393 (1.357-1.468) 1.415 (1.366-1.482) identical
head, session 1 (3) 9825 for images 3-7, 13768 from image 8 43.91 GiB 1.028 (1.028-1.046) 1.226 (1.060-1.234): image 8 re-pins 3.9 GiB identical
head, session 2 (3) 13768 from image 4 (image 3 waits on the background compile) 43.91 GiB 1.093 (1.051-1.112) 1.074 (1.040-1.097) identical
kill switch (1, shared lease) 9825, as base 63.17 GiB not timed identical
  • Residency: the load keeps 13768 MiB of the DiT resident (13498 on the L4). Base streams 3840 MiB of it again on every compiled render. Head does the same only until the compiled tier has its own measurement and no graph captured over the release is live:
    • in session 1, this B200 dropped the graph at image 6 (replay measured slower than the eager step), so renders from image 8 keep it all;
    • in session 2, the stored peak prices the first compiled render, so the graph is captured over the full placement.
  • H2D: the profiled render moves 30% less, which matches the expected third.
  • Speed: a steady compiled render takes about 25% less time (1.03-1.11 s against 1.36-1.48 s). The base A/A spread over 3 processes is 8%. Image 4 waits on the background compile in every arm: base 8.0-8.7 s, head session 1 7.8-11.2 s, head session 2 7.9-8.5 s. Its placement is the same in both session-1 arms, so the one 11.2 s head render is not explained by this change, but it was not re-run.
  • Images: all 10 renders of every process are md5-identical to base: 10 processes, 100 images (the base session-2 process included).
  • Compile and capture: 1 CUDA graph capture per process in every arm. Dynamo counted 4 frames and 627 captured calls in base, head session 1 and kill switch, and 3 frames and 492 calls in head session 2. No arm recompiles or re-captures after the first compiled render.
  • Device memory (NVML, this process): head never exceeds base. The bg-compile and capture renders peak at 23.93-24.02 GiB in both arms. Steady renders: base 19468 MiB; head session 2 17826-19468 MiB with 3.9 GiB more of the DiT resident.

Compiled peak above eager (still handled). Every compiled denoiser forward holds 6000 MiB more (a pre-hook allocation), graphs off:

arm release per compiled render DiT resident peak allocated images
base 3840 MiB (flat estimate) 9825 18235 MiB identical
head 3840 MiB on image 4, then 5120 MiB from the recorded 8129 MiB peak 8529 16938 MiB identical

The flat estimate under-prices this request, and head streams more to cover it. With graphs on, the graph captured over the 3840 MiB release stays live on that run, so head keeps that release. No arm hit an OOM.

Other family: Qwen-Image-2.1 bf16 at the same cap, 8 steps. The transformer stays resident in both arms. The renders, the H2D (14.1 GiB), the graph capture and the dynamo counts (3 frames, 488 calls) are identical to base. Head also records the compiled tier's 3061 MiB peak.

Tests

  • New tests/test_diffusion_tier_peaks.py (21 tests):
    • the flat estimate before the first measurement;
    • a compiled peak inside the reserve releasing nothing;
    • a larger compiled peak streaming exactly the difference;
    • a lower one never undercutting the eager pricing;
    • VAE-saver / LoRA invalidation still pricing flat;
    • the eager key never standing in, and each graph mode answering only for itself;
    • a pending background compile pricing flat;
    • the identity following the kept graph mode;
    • _tier_graph_mode across capture / drop / poison / decline;
    • the held release while a graph is live;
    • the kill switch.
  • Memory planner, learned peaks, offload, placement, CUDA graph, bg compile and seed suites (45 files): 1951 passed, 159 skipped. Base: 1935 passed, 159 skipped.

Limits

  • Real L4 not run. The cap emulates the planner's view and the caching allocator. NVML peaks include VMM ranges and the graph pool, which the cap does not bound. On this B200 the compile renders peak at about 24.0 GiB in both arms, above an L4's 23034 MiB. So the absence of an OOM is shown only for the allocator-capped part, and only relative to base.
  • First session, graph kept: on a card that keeps its CUDA graph (this B200 in some runs), the first session keeps the release the graph was captured over until a reload. The next session prices from the stored peak from the first compiled render. On the L4 the graph is dropped two renders after capture, so session 1 recovers there as on this B200.
  • Mixed request sizes: the held release follows the latest capture. A request that captures a second shape over a different release can make the first shape re-capture once.
  • Not measured: the image-3 / image-4 stall (R3 (a)), ROCm, Windows, and the speed effect on a real L4.

…rned peak

The deferred profile marked the load's reserve static, so every compiled render
sized itself from the flat estimate and streamed resident groups again (3840 MiB
on FLUX.1-schnell bf16 at an L4's 24 GiB). The compiled tier now records its own
learned peak, keyed by the CUDA graph mode it runs (stable across the first
capture), and the request guard prices compiled renders from it, never below the
eager pricing. Until that tier has a measurement the flat estimate stands in. A
graph captured over a release keeps that release while it is live, so no graph is
re-captured. UNSLOTH_DIFFUSION_TIER_PEAKS=0 restores the flat estimate.
@danielhanchen danielhanchen self-assigned this Oct 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T04:42:57.006595Z c8ee65b 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 73a8c15c2d

ℹ️ 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".

Comment on lines +3412 to +3416
totals = cuda_graph.stats(handles)
if totals.get("graphs"):
return armed
if totals.get("captures") or totals.get("poisoned") or _graphs_settled_eager(state):
return "off"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Account for per-block fallbacks in the tier graph identity

When a whole-step graph is dropped and its fallback_handle starts running per-block graphs, cuda_graph.stats(handles) only inspects the top-level handles. Consequently, this returns off despite live per-block graphs; if the original graph remains cached, it instead returns step. This fallback is installed by _attach_block_fallback and activated by GraphedForward._engage_fallback. Because _follow_live_graph_mode now writes this result into the peak identity and _tier_request_peak uses it for request pricing, per-block execution can consume or record a peak belonging to a different graph mode, potentially under-reserving memory. Inspect the fallback handles and distinguish their execution mode before selecting the learned-peak key.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. On the deferred profile's tier _attach_block_fallback arms a per-block fallback whenever the denoiser stays on the device (not hooked, or pinned resident), and on a streamed one with UNSLOTH_DIFFUSION_BLOCK_GRAPHS=1. Once the whole-step graph drops, _engage_fallback routes its calls through fallback_handle, which cuda_graph.stats() does not inspect, so the tier read off (or step while the old graph was still cached). _tier_graph_mode now reports its own step_blocks mode while a fallback is engaged, so those renders neither read nor record the step / off peaks; until that mode has its own learned peak they price from the flat estimate, as before. _live_graph_mode and non-deferred loads are unchanged. Added test_an_engaged_per_block_fallback_keys_its_own_graph_mode; the tier-peak, graph and memory suites pass (1957 passed, 159 skipped).

…tier graph mode

A whole-step graph that dropped hands its calls to per-block graphs held as
fallback_handle, which cuda_graph.stats() does not inspect, so the tier read
off or step for them. Report step_blocks while a fallback is engaged so it
neither reads nor records the step / off peaks.

[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. More of your lovely PRs please.

Reviewed commit: c8ee65b991

ℹ️ 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 c5922a0 into main Oct 25, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-compiled-peak branch October 25, 2026 07:55
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