Skip to content

[BUG] AMD: diffusion in studio falls back to whole-module offload and a 20 step image takes 48 minutes #8081

Closed

Description

@LeoBorcherding

unsloth/Z-Image-GGUF at 1024x1024, 20 steps, on a 16 GB Windows ROCm card. It completes, but the request takes 48m25s (process_time_ms: 2905289). Only 1m47s of that is sampling at 5.40 s/it. Everything after the sampler is spent paging submodules over PCIe and decoding the VAE in tiles. A later pass in the same session measured 106.51 s/it, about 20x the resident rate.

diffusion.loaded: repo=unsloth/Z-Image-GGUF base=Tongyi-MAI/Z-Image-Turbo
device=cuda offload=model tiling=True
reasons=companions exceed budget; whole-module offload of every component

The planner asks for 24511 MiB resident against a 13822 MiB budget:

term MiB
transformer (int8) 6451
dense companions (text encoder + VAE) 7820
runtime headroom 8192
base overhead 2048

Group offload is the right tier here (stream the transformer, keep the companions), but its floor is companion_dense + headroom + overhead = 18060, over budget, so _group_fits() is false and it falls through to whole-module.

Fix

1. Don't count the text encoder as resident during sampling. It runs once, before step 0. If the prompt is encoded and the encoder released before the transformer goes resident, the group floor becomes VAE + headroom + overhead, roughly 10.5 GB, which fits. That is the difference between 48 minutes and about 3. The conditioning cache already installed on this path is half the plumbing.

group_floor is built at studio/backend/core/inference/diffusion_memory.py:388 and gated at :400.

2. The runtime headroom is a flat 8192 MiB guess. It is the largest term in both sums, larger than the transformer itself, and studio/backend/core/inference/diffusion.py:2923 calls the estimator with width = None, height = None, so it always plans at default resolution regardless of the request. Reserving 8 of 13.8 GB for activations is what pushes everything else out.

3. The turbo discount never fires. estimate_image_runtime_mib takes a variant hint built from repo_id or base or "" (diffusion.py:2919), so a GGUF repo id shadows the base and Tongyi-MAI/Z-Image-Turbo is never seen. Join both instead of picking one. Worth about 1.2 GB. Not decisive on its own, the floor is over budget either way, but it is wrong regardless.

Repro

Load unsloth/Z-Image-GGUF on a card where the companions do not fit, generate 1024x1024 / 20 steps, watch the bar sit at 20/20 for 45 minutes.

Activity

  1. changed the title [-]Studio: diffusion falls back to whole-module offload and a 20 step image takes 48 minutes[/-] [+][Optimization] AMD: diffusion in studio falls back to whole-module offload and a 20 step image takes 48 minutes[/+] on Aug 7, 2026
  2. changed the title [-][Optimization] AMD: diffusion in studio falls back to whole-module offload and a 20 step image takes 48 minutes[/-] [+][BUG] AMD: diffusion in studio falls back to whole-module offload and a 20 step image takes 48 minutes[/+] on Aug 7, 2026
  3. danielhanchen commented on Aug 9, 2026

    @danielhanchen
    Member

    All three of your points check out, and the arithmetic reproduces exactly: transformer 6451 + companions 7820 + headroom 8192 + overhead 2048 = 24,511 against a 13,822 MiB budget, group floor 7820 + 8192 + 2048 = 18,060, so _group_fits() goes false and it falls to whole-module offload. That is the 48 minutes.

    Point 1, the text encoder counted as resident, is the decisive one and it is fixed. You are right that it runs once, before step 0, and that holding it resident for the whole denoise reserves its bytes for nothing.

    I did not release it, though. The conditioning cache is opt-in (UNSLOTH_DIFFUSION_COND_CACHE_DIR) and only helps on a repeated prompt, so it would not have carried a cold run, and releasing a component mid-pipeline is more lifecycle surgery than this needs. Instead the encoders are streamed: apply_group_offloading is applied to the text_encoder* components too, so they page in for that single encode instead of being placed resident. The planner prices a second floor for that tier, companions - text_encoders + headroom + overhead, and prefers it only where plain group offload does not fit and whole-module offload would otherwise be the answer. Your 16 GB card moves from model to that tier. So does a 12 GB card.

    One consequence you would want to know about, since it lands on machines like yours: diffusers pins every offloaded parameter in host RAM on the copy-stream path. That is fine when group offload was already the plan, but this tier is only ever reached as a rescue from whole-module offload, which pins nothing, on a card small enough that the companions did not fit. Turning a device-memory shortfall into ten-plus GB of unswappable host RAM would just move the problem, so the rescue tier passes low_cpu_mem_usage = True. The encoders it streams run once per call, so the slower unpinned copy is paid once rather than per step. Existing group plans are untouched.

    Point 2, the flat 8192 headroom. Correct that it is the largest term and that it is always planned at the default resolution. It is not fixed by shrinking it, because #8188 is the same term being too small above 1 MP and lowering it would make that worse. The constant turns out to be well calibrated; what was missing was the plumbing. So the load-time call keeps width = None (load time genuinely does not know the request) and a generate-time re-check compares the real resolution against the free budget. With the encoder fix above, your 1024x1024 case no longer needs the headroom lowered at all.

    Point 3, the turbo discount. Confirmed, and fixed. repo_id or base or "" drops the base whenever a repo id exists, which is every GGUF load, and the base is where the marker lives. unsloth/Z-Image-GGUF says nothing; Tongyi-MAI/Z-Image-Turbo says turbo. Both are joined now, deduplicated, and the hint is stored on the load state so the generate-time check uses the same multipliers. As you said, not decisive alone, but wrong regardless. Here it takes the load-time headroom from 8192 to 6963.

    Before and after through the shipped planner, at your resolution and sizes:

    card safe budget MiB before after
    8 GB 6144 model model
    12 GB 10240 model group + streamed encoders
    16 GB 14336 model group + streamed encoders
    24 GB 22119 group group
    32 GB and up none none

    And the cross-check you would want: at 1024x1024 nothing is refused at any card size, so this does not reintroduce #8188, and #8188's guard does not push your case back to whole-module offload.

    PR: #8224

  4. danielhanchen commented on Aug 9, 2026

    @danielhanchen
    Member

    One correction to my note above, worth recording since it affects the machines this fix targets.

    Streaming the text encoders originally put them in the same all-or-nothing loop as the transformer. That loop propagates any failure rather than falling back, because once a module carries group-offload hooks whole-module offload is no longer usable. Right for the DiTs; wrong once the encoders joined it. A text encoder is a much less well-trodden target for block-level group offloading than a DiT, and one whose structure diffusers does not recognise can refuse outright. Since the encoders are applied after the DiTs, that refusal would have arrived with hooks already installed and turned your slow-but-working 48-minute load into a hard failure.

    Each encoder is now applied on its own with the failure absorbed: a refusal places that encoder resident, which is exactly the behaviour you have today, so the worst case is the current one rather than a broken load. The transformer keeps its original semantics.

    Nothing in the decision table changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions