Repository navigation
Studio diffusion: price FLUX.3 Klein edits by sequence tokens on Ampere / Ada - #18928
Conversation
…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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # Always charged (L4 runs used 80; the encoder caps at 512). | ||
| _FLUX3_TEXT_TOKENS = 512 |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
…ect ROCm wheels by tag [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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.
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.condition_pixel_weight0.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_oversizedthe 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_verdictandestimate_tiled_image_runtime_mibtake 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._flux3_guard_denoise_mib(diffusion.py).The previous estimate is kept for:
UNSLOTH_DIFFUSION_FLUX3_TOKEN_PRICED=0restores 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 ownallow_oversizedrender.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.
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:
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):