Repository navigation
Studio diffusion: latch the CFG-batch verdict per load and across processes so a seed keeps one path - #14849
Merged
Conversation
…cesses The batched-or-plain choice for a request shape came from a live 2-sample timing gate, and a shape turned off by memory pressure was re-timed on the next render. A borderline shape could therefore take a different path (and give a slightly different image for the same seed) in another process, or on a later render after one transient OOM; a free-memory reading near the line also alternated batched and plain steps inside one render. - The timing verdict is latched on the shape: a transient memory off retries batching on the next render without re-timing. - Verdicts are stored next to the learned peaks, keyed on the load (class, config, layer classes, dtypes, offload policy, speed tier), card, compiled or eager, request shape and runtimes; first writer wins, so every later process takes the stored path without a trial. UNSLOTH_DIFFUSION_CFG_BATCH_VERDICTS=0 keeps it per load. - No room for a doubled call mid-render turns the shape plain for the rest of that render instead of skipping single steps.
Member
Author
|
@codex review |
Codex Review SummaryThis 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: 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". |
danielhanchen
added a commit
to Imagineer99/unsloth
that referenced
this pull request
Oct 24, 2026
…just the module layout (unslothai#15047) ## Summary The cross-process CFG-batch verdict store (unslothai#14849) keys the batched-vs-plain decision on a load identity that cannot tell a bf16 load from a torchao INT8 or FP8 dynamic load of the same model. A verdict timed on one is reused on the other without timing. This PR adds the weight tensor classes and the engaged `transformer_quant` to the key, and bumps the store version so old entries simply miss. Every other part of the verdict store is unchanged, so a shape still takes one path in every process. ## Details - After torchao `quantize_`, layers are still `nn.Linear` and the weights still report `torch.bfloat16`, so `_load_identity` hashed bf16, INT8-dynamic and FP8-dynamic (PerRow) to the same sha1. An INT8 or FP8 load could then be stuck on the path that was faster for bf16, or miss a gain. Pixels were within the batched tolerance either way, so this is speed only. - `_load_identity` now also hashes each parameter's tensor class and, for torchao subclasses, their non-tensor attributes (`mm_config`, `act_quant_kwargs`, `kernel_preference`, `dtype`; `block_size` is skipped since it only follows the layer shape). This separates fp8 with and without fast accumulate too. - `install_cfg_batch` takes `transformer_quant`. The image (`diffusion.py`) and video (`video.py`) loads pass `transformer_quant_engaged`, the scheme the load already records, so GGUF and other non-torchao quant paths are covered as well. - The key also carries the live attention backend (`_unsloth_attention_backend`), read when the verdict is looked up. `set_attention_backend()` changes neither the module classes nor the config, so the load identity alone missed a backend switch. - The store `version` goes from 1 to 2, and the reader ignores a file of another version. Old entries simply miss and are replaced on the next write. ## Tests New tests in `studio/backend/tests/test_diffusion_cfg_batch.py`, all 6 fail on main and pass here: - `test_the_verdict_key_tells_bf16_from_torchao_int8_and_fp8_loads`: bf16, INT8, FP8 fast accumulate on and off all give different keys. - `test_the_verdict_key_includes_the_engaged_transformer_quant` - `test_the_same_load_gives_a_stable_verdict_key`: a deep copy of each load gives the same key. - `test_install_keys_the_router_on_the_engaged_quant` - `test_the_verdict_key_follows_the_live_attention_backend` - `test_a_version_1_verdict_store_is_not_read` `pytest tests/test_diffusion_cfg_batch.py tests/test_diffusion_cfg_batch_sync.py`: 48 passed, 2 skipped. `tests/test_diffusion_memory_vram_budget.py -k cfg`: 3 passed. CPU only. The speed effect on a real GPU of reusing a bf16 verdict on an INT8 or FP8 load was not measured.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Single-card CFG batching (#13787, defaults from #14566, #14652, #14734) decides per request shape whether both guidance branches run as one doubled call. Batched is a tolerance mode (about LPIPS 0.01-0.02 versus plain on some models, since the doubled batch can pick other kernels), so the same seed and prompt only give the same image if every render takes the same path. Three ways that choice could change:
CfgBatchRouter.shapes, keyed on the argument tensor shapes, per load). Every new process or reload re-ran the 2-sample trial (min(batched) > 0.92 * min(plain), plus the single-sample_RESIDENT_MAX_PAIR_Sgate). A shape near the 0.92 line landed on either side depending on timing noise._OFF_TRANSIENT: no room before the trial, or an OOM on a doubled call) hadjudgedreset inend_render, so the next render re-timed it and could latch "measured no gain" for the rest of the load._batchedreturned plain for a single step when free memory read below 2x the activation peak and batched again on the next step, so a fluctuating free-memory reading mixed paths step by step.Probe with a controlled timer (per-call lognormal noise, 40 fresh loads per cell; P = plain pair, B = doubled call):
One transient OOM on render 2, ratio 0.91, sigma 0.03: render 1 and render 4 took different paths in 2/40 loads on main, 0/40 here. Free memory moving around the line each step on main:
PPPBPBBB,PPBBPPBP.Fix
_Shape.verdict). A transient memory off still makes that render plain, but the next render batches again from step 3 without a new trial.diffusion_cfg_batch_verdicts.jsonnext to the learned peaks. The key covers the load (denoiser class, config, layer classes, weight dtypes, offload policy, speed tier), the card name, compiled or eager, the request shape, and the runtime stamp (torch, diffusers, kernel packages). First writer wins under the same file lock as the learned peaks, so a later process takes the stored path with no trial. Only measured timing verdicts are stored. Nothing is stored off CUDA or for a load that cannot be identified.UNSLOTH_DIFFUSION_CFG_BATCH_VERDICTS=0keeps the verdict per load, andUNSLOTH_DIFFUSION_CFG_BATCH_VERDICTS_FILEsets the path.Tests
studio/backend/tests/test_diffusion_cfg_batch.py: a borderline shape keeps one path across fresh loads (both verdicts), memory pressure never re-times a judged shape, no room mid-render keeps the rest of that render plain, and the store is keyed on the card and skipped off CUDA. All 5 fail on main and pass here.test_diffusion_cfg_batch.py,_cfg_batch_sync,_cfg_parallel_tier,_zimage_cfg_split,_speed,test_diffusion_backend.py,test_video_backend.pywith-n 8: 1465 passed here; on main 1460 passed and the 5 new tests failed.batch. Process 2 skipped the trial ([1, 1, 1, 1, 2, 2, 2, 2]) and gave the same image hash as process 1.Not covered
UNSLOTH_DIFFUSION_CFG_BATCH_VERDICTS=0resets it.activation_mibis only known when a plain call raises the process's peak counter, so whether the memory gate applies still depends on process history. Unchanged here.test_diffusion_backend.py::test_begin_load_rejects_concurrenthit its 30 s thread timeout on a loaded host. It fails the same way on main and is unrelated.