Skip to content

Studio diffusion: price FLUX.3 Klein edits by sequence tokens on Ampere / Ada - #18928

Merged
danielhanchen merged 4 commits into
mainfrom
studio-flux3-canvas-estimate
Nov 22, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-flux3-canvas-estimate

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On a 24 GB L4, FLUX.3 Klein edits at 2048x2048 with 4 to 6 input images were refused by the activation guard even though they run resident with room to spare. This PR prices the denoise of a FLUX.3 edit by its sequence tokens. The rate is measured on CUDA compute capability 8.x (Ampere, Ada), and the change applies only there.

Root cause

The refusal comes from the denoise term of estimate_tiled_image_runtime_mib. The message already says "even with tiled VAE decoding", so the decode term is not it, and the text encoder stays resident on this path, so it is not counted twice either.

  • The output canvas is charged at the flat 1024 MiB per megapixel: 4096 MiB at 2048x2048.
  • Every reference is charged at 8192 MiB per weighted megapixel (condition_pixel_weight 0.11): about 901 MiB per 1 MP reference.

That is about 220 MiB per 1k sequence tokens, which matches the B200 eager figures the FLUX.3 row was calibrated from. On L4 and A100-40 the same renders measure 63 to 76 MiB per 1k tokens (int8, compiled) and 106 to 116 (bf16). Both cards read the same, and the rate is flat from 0 to 10 references.

Example: a 4-image edit read 9.52 GB needed (including the 2 GB fixed overhead) against 7.07 GB usable. With allow_oversized the same request ran with 2.0 GiB of denoise activations and 2.5 GiB left free at its peak.

Change

  • flux3_conditioned_denoise_mib (diffusion_memory.py) prices a FLUX.3 edit's denoise from its token count: 256 px per output token, 4096 tokens per reference (the pipeline resizes each one to 1 MP), plus a 512-token prompt window. The rates per 1k tokens are 116 MiB for a bf16 / fp16 denoiser and 76 MiB for int8 / fp8 when compiled, each multiplied by a 1.25 margin.
  • image_activation_verdict and estimate_tiled_image_runtime_mib take it as an optional cap on the flat denoise term. It can only lower that term, and it is ignored when the request has no input images.
  • The generate guard computes it in _flux3_guard_denoise_mib (diffusion.py).

The previous estimate is kept for:

  • text-to-image, and the hires refine pass (which drops its condition pixels)
  • every other family, including FLUX.2 Klein, whose reference pricing is untouched
  • compute capability other than 8.x (T4, Hopper, Blackwell), and ROCm
  • GGUF and other quantization schemes
  • a quantized denoiser that is not compiled: Studio's speed "off" tier loads bf16, so eager int8 was never measured

UNSLOTH_DIFFUSION_FLUX3_TOKEN_PRICED=0 restores the previous estimate.

The untiled figure, the 2 GB fixed overhead and the budget reserve are unchanged.

Results

Colab L4 (24 GB) ran base (merge base f03997e160) and head on the same VM, in fresh processes, at Studio defaults, seed 1234, 4 steps. The "allowed" column is base's own allow_oversized render.

Need vs usable comes from the guard's own message (GB includes the 2 GB fixed overhead). Peaks are allocated memory above the pre-render baseline, read from the allowed render. s/step is the steady-state step time.

model case base default: need vs usable head default: need vs usable peak denoise / decode free at peak s/step base / head head vs base pixels
FLUX.3 klein int8 4 images refused, 9.52 vs 7.07 runs 1.99 / 3.17 GiB 2.46 GiB 6.6 / 6.7 identical
FLUX.3 klein int8 6 images refused, 11.28 vs 5.60 refused, 5.76 vs 5.60 2.46 / 0.93 GiB 3.10 GiB 9.4 / 9.5 identical (both allowed)
FLUX.3 klein bf16 4 images refused, 9.52 vs 9.23 runs 3.47 / 3.15 GiB 3.42 GiB 12.2 / 12.3 identical
FLUX.3 klein bf16 6 images refused, 11.28 vs 9.23 runs 4.33 / 3.14 GiB 3.08 GiB 17.5 / 17.6 identical
FLUX.3 klein base int8 4 images refused, 9.52 vs 7.08 runs 2.22 / 3.51 GiB 2.08 GiB 6.4 / 6.5 identical
FLUX.3 klein base int8 6 images refused, 11.28 vs 5.51 refused, 5.76 vs 5.51 2.79 / 1.42 GiB 2.99 GiB 9.0 / 9.1 identical (both allowed)
FLUX.3 klein int8 10 images refused, 14.80 vs 4.17 refused, 7.24 vs 4.17 3.43 GiB 16.7 / 16.7 identical (both allowed)
FLUX.3 klein bf16 10 images refused, 14.80 vs 9.23 refused, 10.00 vs 9.23 6.06 / 3.15 GiB 1.42 GiB 30.5 / 30.5 identical (both allowed)
all three text-to-image 2048 runs runs unchanged unchanged unchanged identical

No run on either side ran out of memory.

The int8 6-image case is still refused. The guard's free reading at the check (7.60 GB) leaves out about 3.7 GiB of CUDA graph pool that the previous 4-image render's captured graphs still hold, so the request misses by 0.16 GB. See the follow-up below.

Other cards:

  • A100-40, same arms, same VM for both trees: base already admits every case. Head makes the same decisions, with no OOM, and all 12 images are pixel-identical to base.
  • T4 (16 GB, compute capability 7.5): outside the measured class, so head keeps the base estimate.
    • Every verdict and need figure is identical to base.
    • All 10 rendered images are pixel-identical to base.
    • The 4 and 6 image int8 edits stay refused at the default request and stream when allowed.
    • The 10 image int8 edits (klein and base) stay refused. Forced with allow_oversized, they run out of memory on both trees alike, so that refusal is a real one, and this change leaves it in place.

Every priced figure covers its measured denoise peak with at least 1.2x to spare. With the unchanged decode term and fixed overhead, the whole tiled need covers the larger of the denoise and decode peaks with the same margin.

Follow-up (not in this PR)

When an earlier render's CUDA graphs are still captured, their pool is held, and the guard does not credit it as free. Admission could release the whole-step and per-block graphs whose keyed shapes the new request cannot replay, then credit the pool. That belongs in the graph layer, with its own eviction and test coverage.

Test plan

  • tests/test_diffusion_flux3_token_estimate.py (96 cases):
    • every measured L4 / A100-40 point is covered by its priced figure
    • L4 base and head verdicts at the recorded free readings
    • unmeasured device classes, ROCm, the kill switch, GGUF, nvfp4 and eager int8 keep the flat estimate
    • FLUX.2 Klein and other families are not priced by the new path
    • requests without input images produce identical verdicts for every family at 1024x1024, 2048x2048 and 1536x1024
  • Guard-related backend suite (17 files) on base and head: base 1535 passed, head 1631 passed (the extra 96 are the new file), no failures on either side.
  • GPU:
    • Colab L4 and A100-40: base vs head on one VM per card, as above.
    • Colab T4: base vs head on one VM, identical as above.

…re / Ada

The request guard priced a 2048x2048 FLUX.3 edit's denoise at the flat 1024 MiB per output
megapixel plus about 900 MiB per reference (B200 eager figures), so 4 and 6 image edits on a
24 GB L4 were refused although they run resident. On CUDA capability 8.x the tiled estimate's
denoise term is now capped by a per-token rate measured on L4 and A100-40 (bf16 116, int8
compiled 76 MiB per 1k tokens, margin 1.25). Text-to-image, other families, other device
classes, ROCm, GGUF and eager quantized denoisers keep the flat estimate.
UNSLOTH_DIFFUSION_FLUX3_TOKEN_PRICED=0 restores it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 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-22T18:42:27.111568Z b7a0081 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 Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-22T17:16:10.153561Z
We couldn’t complete this review. Comment @codex review to try again.
948cd63 PR opened
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 9d697df256

ℹ️ 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 +1763 to +1764
# Always charged (L4 runs used 80; the encoder caps at 512).
_FLUX3_TEXT_TOKENS = 512

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 FLUX.3's actual prompt window in token pricing

For edits with long prompts, this undercounts the denoiser's sequence: Flux3PipelineFacade.__call__ and encode_prompt in diffusion_flux3.py default to max_sequence_length=8192 and pass that value through to the text step, rather than capping it at 512. The generation-time 512-token limit in diffusion_text_length.py applies only to FLUX.1 families. Consequently, an admitted FLUX.3 edit can contain thousands of unpriced text tokens, exceeding even the 25% margin and undermining the activation guard on memory-constrained cards. Price the actual prompt length, conservatively reserve the supported window, or retain the flat estimate when the prompt exceeds the calibrated allowance.

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.

Fixed in b7a0081. The guard now charges the longest prompt in the request: flux3_prompt_tokens counts one token per 2 UTF-8 bytes plus 64 for the chat template, with 512 as the floor, so a long prompt raises the denoise estimate and a short one leaves it unchanged. The same commit swaps the raw torch.version.hip read for is_rocm_torch, so ROCm wheels that only carry the tag in __version__ keep the flat estimate. Tests cover the floor, the long-prompt price, the call site and the tag-only ROCm case.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: b7a0081d7b

ℹ️ 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 f6e9d17 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-flux3-canvas-estimate branch November 22, 2026 20:08
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…request from fitting (#18978)

## Summary

On a 24 GB L4, the int8 FLUX.3 Klein 6-image 2048x2048 edit is still
refused by the activation guard after #18928, by 0.16 GB, whenever an
earlier render in the same session captured CUDA graphs. Those graphs
hold about 3.3 GiB of graph pool that the guard, correctly, does not
count as free. This PR drops Studio's own idle graphs when, and only
when, that pool is the reason a request would be refused, then re-reads
the card. Every other request keeps its graphs.

## Root cause

- `cuda_reclaimable_bytes` (`diffusion_memory.py`) credits
reserved-but-unallocated allocator memory as free, minus the graph pools
(`_graph_held_cache_bytes`). Only the graphs that recorded into a pool
can reuse its blocks, so leaving them out is right while the graphs
live.
- The whole-step graphs share one pool (`_POOL_BOX` in
`diffusion_cuda_graph.py`), and each new shape's capture grows it. On L4
it held 1.85 GiB after a 2048 text-to-image render and 3.30 GiB after a
4-image edit. The graphs stay alive between renders, so the pool is
never handed back.
- At the 6-image check the card read 7.16 GiB free, plus 3.74 GiB
cached, 3.30 GiB of it graph pool. The guard needed 5.76 GB against 5.60
GB usable and refused. Freed, that pool alone would have covered the
request three times over.
- The 4-image graph cannot replay a 6-image request anyway (graphs key
on input shapes), so for this request the pool is dead weight.

## Change

- `graph_release_fits` (`diffusion_memory.py`) runs the guard verdict
without the oversized override. Only when that verdict refuses does it
read the idle graph-pool bytes (one allocator snapshot) and check
whether crediting them makes the request fit. A request that already
fits never pays for the snapshot.
- `_release_graphs_for_guard` (`diffusion.py`) runs just before the
generate guard's check. When `graph_release_fits` says yes, it drops
every graph handle (`cuda_graph.reset_all`, the same call the OOM path
already uses), which flushes the pool, and then rebuilds the guard
inputs from a fresh reading. The check that follows decides exactly as
before, on real free memory.
- It never runs when the request already fits, when releasing would
still not be enough, or when nothing has been captured. Any error leaves
the plain reading in place.
- `UNSLOTH_DIFFUSION_GUARD_RELEASE_GRAPHS=0` keeps the graphs and
restores the previous behaviour.

The cost is one re-capture the next time a dropped shape renders. It
only happens on a request that would otherwise have been refused.

Options considered and not taken:

- Not capturing graphs above a size: this slows every large render to
fix a guard reading.
- A single shared pool: one already exists. The pool grows with each new
shape because older graphs keep their blocks.

## Results

Colab L4 (24 GB), Studio defaults (int8 denoiser, compiled, CUDA
graphs), seed 1234, 4 steps. Each arm is a fresh process on one VM.
Every arm runs the same sequence: text-to-image 2048 twice, then 4, 6
and 10 reference edits at 2048x2048, each at the default request first
and with allow_oversized only when refused. Base is `b1caa38f6f`. The
kill-switch arm is head with `UNSLOTH_DIFFUSION_GUARD_RELEASE_GRAPHS=0`.
The reference arms run base with a single edit in a fresh process, so no
graphs are held when the guard checks.

| arm | 6 images default | 10 images default | graphs before / after 6
images | min free 6 / 10 images | OOM |
|---|---|---|---|---|---|
| base x3 | refused, 3.30 GiB graph pool held | refused, 4.70 GiB held |
2 / 2 | (oversized) 3.10 / 3.43 GiB | none |
| head x2 | runs | runs | 2 / 1 (dropped, re-recorded) | 1.79 / 3.59 GiB
| none |
| head, kill switch | refused | refused | 2 / 2 | (oversized) 3.10 /
3.43 GiB | none |
| base, fresh process, single edit | runs | runs | 0 / 1 | 2.99 / 3.70
GiB | none |

Steady-state s/step (median of steps 2 to 4):

| case | base (3 runs) | head (2 runs) | kill switch |
|---|---|---|---|
| text-to-image 2048, graph replay | 2.596 / 2.615 / 2.611 | 2.623 /
2.620 | 2.618 |
| 4-image edit | 6.909 / 6.962 / 6.958 | 6.941 / 6.956 | 6.957 |
| 6-image edit | (oversized) 9.755 / 9.739 / 9.735 | 9.721 / 9.715 |
(oversized) 9.726 |

Head is inside the base spread on every row.

Images:

- Text-to-image and 4-image renders are pixel-identical across every
arm.
- Head's admitted 6 and 10-image edits are pixel-identical to the same
request on base in a fresh process.
- They differ from base's allow_oversized renders of the same request.
That path tiles the VAE decode up front (decode peak 11.78 vs 13.99
GiB), and an admitted request does not.

## Test plan

- `tests/test_diffusion_guard_graph_release.py` (20 cases):
- the recorded L4 6-image reading refuses on its own and fits with the
held pool credited
- no release when the request already fits (the pool is not even read),
when the pool cannot close the gap, when free memory is unknown, or with
no graphs
  - the credit is capped at the card total
  - the kill switch, in every spelling, keeps the graphs and the refusal
  - a failed reset falls back to the plain reading
  - the generate guard calls it right before its check
- Guard and graph backend suites (24 files) on a Colab CPU VM: base 2803
passed, 2 failed; head 2822 passed, the same 2 failed. Both failures are
on base as well
(`test_every_third_party_bf16_pipeline_the_catalog_offers_is_mirrored`,
`test_cached_reference_kv_equals_recomputing_it_every_step`). That run
also caught a source pin in `test_diffusion_edit_load_placement.py`,
which this PR now satisfies (passes locally).
- Colab L4 base vs head vs kill switch, as above.
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