Repository navigation
Studio: diffusion auto-optimizer core shared by the image and video load paths - #13520
Conversation
…oad paths Adds core/inference/diffusion_autoopt (targets, profile, patches, plan, apply, runtime): one orchestrator over the existing speed primitives for image and video loads, a PatchSpec registry with a single install / uninstall lifecycle and one fingerprint util for the per-architecture patches, a static profile of each denoiser class, a per-load audit surfaced as resolved.speed_mode.steps, and a runner-aware auto step cache check for from_pipe workflows and I2V swaps. Every registered family resolves the same optimizations as before: the parity fixture was captured from 5f38afb and is asserted for every image and video family across speed tiers, dtypes, offload and GGUF.
|
@codex review |
|
You have reached your Codex usage limits for security reviews. Please try again later. |
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 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. Another round soon, please! 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". |
Summary
First step toward one generalized optimization layer for every Studio diffusion and video model, so a new model plugs in with data and at most a small hook instead of edits across many call sites. The design follows unsloth-zoo's
compiler.pywhere it fits: per-model behaviour lives in tables, source rewrites are fingerprint gated and reversible, and every lever reports what it did.This PR is behaviour preserving. Every registered family resolves exactly the optimization set it resolved before (624-case parity test below), and real-weight renders are byte-identical to main.
New package
core/inference/diffusion_autoopt/:patches.py: aPatchSpecregistry with one install / uninstall lifecycle and one fingerprint helper (the duplicated_digest/_body_hascopies now alias it, same algorithm, so pinned fingerprints still match). The per-arch patches (eager, arch, Qwen-Image-2.1 fast step / RoPE / fused, Qwen RoPE, FLUX.2 RoPE, ROCm fused, Z-Image fused, Wan fused) register as specs and install in the original order. Every existingUNSLOTH_DIFFUSION_*kill switch keeps working, and each spec also answersUNSLOTH_DIFFUSION_PATCH_<NAME>=0. The hand-kept teardown list becomes teardown groups.splice_sourceadds the in-memory source rewrite helper (exactly-once needles, unresolved-globals check, closures refused, fingerprint gate, reversible); nothing uses it yet.targets.py: the denoiser attribute lists that were hard-coded in several places, with the old semantics, plus a by-type resolver that only feeds the profile, the audit and families with no registry data.profile.py: per denoiser class, the repeated blocks (declared, verified or discovered) and a static graph-break screen of each block's forward, cached by class, source digest and diffusers version.plan.py: the ordered steps with their reasons, from the same checks the primitives use, and one builder for the compile-cache kwargs (it replaces two inline dicts that had to stay in sync).apply.py:run_speed_layer, the single entry for the image load, the deferred third-image profile, video and MiniMax-H3. The existingapply_speed_optimscall is passed in unchanged. LTX static compile, the LTX STG adapter and the cuDNN-benchmark opt-out move from inline branches into family hooks. A family with no registry data gets a guarded trial compile of its discovered blocks; a block whose forward shows graph-break suspects is declined with the reason (UNSLOTH_DIFFUSION_AUTOOPT_DISCOVER=0turns this off).runtime.py: the auto step cache is judged against the pipeline that actually runs the request, reusingdiffusion_multistagefor multi-call runners.Audit in status. Each load records every step as engaged, declined with a reason, or failed with the error, under
resolved.speed_mode.steps(an optionalstepsfield onDiffusionResolvedControl). A Z-Image load shows 23 entries, for example "tf32: max tier only" and "compiled_dequant: not a GGUF transformer".One behaviour change. In diffusers 0.41,
FluxImg2ImgPipeline,FluxInpaintPipelineand the Z-Image img2img / inpaint pipelines never opencache_context, so an auto first-block cache left on by the base load would raise "No cache context is set" there. The auto cache now stays off for those calls, with the reason in status. This is read from the diffusers source, not reproduced on a live run; an explicitly requested fbcache on those workflows is unchanged.Not touched: the quant, ConvRot and policy tables, prequant validation, and video memory planning.
Testing
tests/test_diffusion_autoopt_parity.pywithtests/fixtures/diffusion_autoopt_parity.json, captured from main by replaying the old load sequence over stub pipes (real class names, modules and_repeated_blocks). 24 families (15 image, 9 video) x 4 speed tiers x bf16 / fp16 x offload on / off x GGUF on / off = 624 cases, comparing the installed patches, applied flags, compile kwargs per DiT, LTX static compile, auto-dynamic, int8 GEMM and the CUDA-graph decision and reason. Passes with the GPU hidden and visible. It also asserts the old sequence still reproduces the fixture and that every registry family is covered.tests/test_diffusion_autoopt_core.py: 27 unit tests.test_diffusion_single_file_registry.py::test_a_packed_norm_weight_comes_out_as_real_values[Q8_0], a NaN-flakytorch.equalin a file this PR does not touch that passed 4 of 4 reruns on both base and head.speed_optims(identical on base and head)[][]Left for follow-ups
TORCHINDUCTOR_CACHE_DIR, which an image backend in the same server shares, and nothing was measured, so they stay off; the audit says so on every video load.