Repository navigation
Studio: run Z-Image and Wan2.2-5B / HunyuanVideo-1.5 in fp16 on fp16-only GPUs instead of fp32 - #12476
Conversation
…of promoting to fp32 Z-Image overflows float16 only in the attention and FFN branches that feed the post-norm RMSNorm (FFN output up to 1.1e6 at 1024 px). Rescale those branches by a power of two and divide the norm eps by its square, which is the same function, so the model stays in float16 on T4 instead of being promoted to float32 and streamed from host RAM. Wan2.2-TI2V-5B is finite in plain float16 (DiT activations peak below 800), so it opts out of the blanket video promotion. The promotion is now decided per family by fp16_guard; families without a recipe keep it. UNSLOTH_STUDIO_FP16_GUARD=0 restores the float32 promotion. bf16 and fp32 loads are unchanged.
Both the 480p and 720p checkpoints stay finite in plain float16 (DiT peak about 3e2, Qwen2.5-VL 7.4e3, VAE 2.2e2) and land closer to the float32 render than bf16 does, so they opt out of the float32 promotion like Wan2.2-TI2V-5B. Wan2.2-T2V-A14B stays promoted: its float16 render drifts from float32 about as far as bf16 does. Log the kept float16 on the video path, keep the Wan VAE promotion test on the kill switch, and add LoRA and plan-size coverage.
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. |
|
This is not this PR's bug. peft 0.18.1 runs |
…jection peft's LoRA dispatch imports torchao symbols that the CI torchao no longer exports, so the test failed on an unrelated dependency. A hand-written base + B @ A wrapper exercises the same property: Linear children replaced after the guard is installed.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 43d18ab1ce
ℹ️ 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".
| import inspect | ||
| import sys | ||
|
|
||
| cls = getattr(importlib.import_module("diffusers"), cls_name) |
There was a problem hiding this comment.
Close the Dynamo import window before probing Diffusers
When the first Z-Image load runs before background warmup has completed or with warmup disabled, _target_for_ordinal() reaches this import from diffusion.py:5114, before close_dynamo_import_window() at line 5131. Importing Diffusers here therefore bypasses the synchronization that the load path explicitly requires before any Diffusers import and can reintroduce the concurrent torch._dynamo import race that leaves the process partially initialized; move this probe after that guard or make dtype resolution avoid importing Diffusers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 24742f5: the probe now calls close_dynamo_import_window before importing diffusers, and dtype resolution checks for float16 first so bf16 / fp32 loads never probe.
| logger.warning( | ||
| "diffusion.fp16_guard: recipe=%s matched no block; float16 output may overflow " | ||
| "(set %s=0 to promote to float32)", |
There was a problem hiding this comment.
Reject fp16 when no transformer block accepts the guard
If an allowed Diffusers release retains the source tokens checked by _recipe_supported() but changes the actual block topology—for example by adding a projection bias or moving one of the modules—dtype resolution has already selected fp16, while _block_sites() can reject every block here. Merely warning then runs Z-Image entirely unguarded in fp16, the exact path known to overflow and produce black images; the structural preflight must enforce the same conditions as installation, or a zero-site result must abort rather than continue.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not reachable: the Z-Image block (bias-free to_q/k/v/out and w1/w2/w3, diffusers RMSNorm with float eps, same norm/ffn names) is identical in every diffusers release that ships it (0.36.0 through 0.40.0) and the pinned main commit, and a real Studio load on 0.40.0 guards all 34 blocks. A zero-site install also logs a warning naming the kill switch.
…users The recipe probe imports diffusers from dtype resolution, which runs before load_pipeline's own close_dynamo_import_window call. Close the window in the probe itself, as the other early diffusers probes do, and resolve the dtype first so bf16 and fp32 loads never probe at all.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 24742f5017
ℹ️ 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".
| inpaint_pipeline_class = "ZImageInpaintPipeline", | ||
| fp16_incompatible = True, | ||
| # The attention / FFN branches overflow float16 before their post-norm. | ||
| fp16_guard = "rescale_post_norm", |
There was a problem hiding this comment.
Honor fp16 guards in compile eligibility
On an fp16-only CUDA target with the Default or Max speed tier selected, this family retains fp16_incompatible=True, so diffusion_speed.compile_eligible() returns False at lines 338–339 even though this recipe now keeps the effective target in float16. Regional compilation is therefore silently disabled for Z-Image, as it is for the newly native video families, leaving requested speed optimizations—and potentially torchao transformers that rely on compilation—on the eager path. The compile gate should test whether the family actually promotes to fp32 rather than the legacy incompatibility flag alone.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Intentional: compile_eligible keeping guarded fp16 families eager is what keeps them correct. The compile path installs the int8 fused MLP, which replaces the FeedForward instance forward the guard relies on (diffusion_int8_fused.install sets module.forward), so compiling would bring the fp16 overflow back. Speed is not the goal on these cards; the measured T4 numbers in the description are eager. Noted the reason at the gate in 1b393ca.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
# Conflicts: # studio/backend/core/inference/diffusion.py
# Conflicts: # studio/backend/tests/test_diffusion_backend.py
Problem
On GPUs without bf16 (T4 and other sm75 cards), Studio promotes Z-Image and every video family from fp16 to fp32:
fp16_incompatible, and everyVideoFamilydefaults tofp16_incompatible = True.The promotion exists for a reason. Plain fp16 Z-Image overflows and renders black: LPIPS 0.87 to 0.95 against fp32. The fix has to keep fp16 accurate, not just switch it on.
Change
New
diffusion_fp16_guard.py. Each family declares how it runs in fp16 (fp16_guard), and the old promotion stays the fallback:rescale_post_normnativerescale_post_norm(Z-Image):rms_norm(y / s, eps / s^2) == rms_norm(y, eps). So the guard divides the branch by a power of two before the overflow point and divides that norm's eps by s^2. The result is the same function, with no clamping of real values:w3half of the gate is scaled before the product (siluapplies tow1only), so the product andw2stay in range.to_q/w1/ ... at run time, so a LoRA wrapper or quantized Linear installed later sees the same reparameterisation. A block with a bias on those projections is not guarded.nan_to_numto the fp16 range is a backstop only.diffusion.py, the load paths indiffusion.pyandvideo.py, and the video memory planner. The planner now sizes the guarded families at fp16, so on a T4 they can stay resident instead of being planned at fp32 size.UNSLOTH_STUDIO_FP16_GUARD=0restores the fp32 promotion for every family.Results
Kaggle T4 (2x T4, 31 GB host RAM; Studio uses one card), Studio defaults, seconds per render:
Accuracy
LPIPS against an fp32 reference with the same seeds and the same initial noise (generated on CPU):
Tests
test_diffusion_fp16_guard.pycovers:native;test_diffusion_backend.pyandtest_video_backend.pyare updated for the new resolver and planner sizes. Four of those tests fail on main as expected: Z-Image and Wan2.2-5B are no longer promoted, and the 5B is planned at fp16 size.test_diffusion_memory.py: 1202 passed, 1 skipped.Not covered