Skip to content

[group offloading] Keep buffer values when offloading to disk - #15333

Merged
sayakpaul merged 4 commits into
huggingface:mainfrom
klin-amd:dev/klin/group-offload-disk-keep-buffers
Dec 4, 2026
Merged

sayakpaul merged 4 commits into
huggingface:mainfrom
klin-amd:dev/klin/group-offload-disk-keep-buffers

Conversation

@klin-amd

@klin-amd klin-amd commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15332

Disk offloading replaced every offloaded tensor with torch.empty_like(...). Buffers read without calling their module's forward are never onloaded, so callers got garbage, e.g. vae.bn.running_mean / running_var in the FLUX.2 pipelines, which gives an all-black image with the VAE offloaded to disk. Memory offloading keeps the values.

This keeps each group's buffers on the offload device (cpu_buffer_dict, built in __init__ next to cpu_param_dict) and restores them on disk offload; parameters still get torch.empty_like. Buffers stay in host memory as a result; the largest I found is SD3's pos_embed (108 MiB bf16 for SD3 Medium, 171 MiB for SD3.5 Large).

Tests (AMD Radeon AI PRO R9700, torch 2.16.0a0+rocm10.3.0a20261115):

Test main this PR
New TestDirectBufferAccessGroupOffload, 4 memory cases (stream / no stream x leaf / block) passed passed
New TestDirectBufferAccessGroupOffload, 4 disk cases failed (assert torch.equal(model.bn.running_mean.cpu(), running_mean)) passed
Rest of tests/hooks/test_group_offloading.py 38 passed, 2 failed 38 passed, 2 failed
FLUX.2 Klein repro from the issue all-black image identical to no offload

The 2 failures in the rest of the file are the MPS test_error_raised_if_streams_used_and_backend_has_no_streams cases.

Self-review notes: buffers are stored once, so an in-place change after the group is created is reset on offload (on main it became uninitialized). #13721, #13751 and #13875 also edit the empty_like loop; whichever merges later needs a small rebase.

A maintainer has not acknowledged #15332 yet.

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Disk offloading replaced every offloaded tensor with an uninitialized
torch.empty_like tensor. Buffers read without calling the module's
forward (e.g. a VAE's batch norm running statistics read by the FLUX.2
pipelines) are never onloaded, so they held garbage. Keep buffer values
on the offload device, as memory offloading does, and add a regression
test.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
…the test

Build cpu_buffer_dict in __init__ next to cpu_param_dict instead of
lazily, fix the comments (buffers are not always small), and parametrize
the regression test over use_stream.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
@klin-amd
klin-amd marked this pull request as ready for review November 18, 2026 06:39
@yiyixuxu
yiyixuxu requested a review from sayakpaul November 30, 2026 23:41

@yiyixuxu yiyixuxu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left some comments. LMK if they make sense.

Comment on lines +167 to +171
# Buffers can be read without calling the forward of the module that owns them (e.g. a pipeline reading a
# VAE's batch norm running statistics), so they may never be onloaded. Keep their values on the offload
# device instead of the uninitialized tensors that offloaded parameters get.
buffers = [buffer for module in self.modules for buffer in module.buffers()] + self.buffers
self.cpu_buffer_dict = {buffer: buffer.data.to(self.offload_device) for buffer in buffers}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this does have the risk of pressurizing the memory a little bit right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, by the buffers' size and nothing else; parameters still get empty_like. With a 256 MiB parameter + 256 MiB buffer model, RSS after disk offload is 1138 MiB on main vs 1394 MiB here. Buffer bytes of the models I checked (fp32 as built from config; a bf16 load halves SD3's pos_embed, which is in the checkpoint, while the Wan RoPE tables are built at init and stay fp32): FLUX.2 Klein VAE 1 KiB (the three bn buffers); FLUX.2 Klein, FLUX.1 and Qwen-Image transformers, FLUX.1 and Wan VAEs 0; Wan2.2 transformer 1 MiB (RoPE); SD3 Medium pos_embed 216 MiB (2.8% of its parameters), SD3.5 Large 342 MiB (1.1%).

self._is_offloaded_to_disk = True

# We do this to free up the RAM which is still holding the up tensor data.
# Free the RAM held by the parameters; they are reloaded from disk on onload. Buffers keep their values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as long as buffers don't contribute too much.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. SD3's pos_embed is the largest I know of; the other models I checked are at most 1 MiB.

@pytest.mark.parametrize("offload_type", ["leaf_level", "block_level"])
@pytest.mark.parametrize("use_stream", [False, True], ids=["no_stream", "stream"])
@pytest.mark.parametrize("offload_to_disk", [False, True], ids=["memory", "disk"])
def test_buffers_read_without_forward_keep_their_values(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also do an assert on the outputs with and without offloading.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. The dummy forward now reads bn.running_mean / running_var directly, as the FLUX.2 pipelines do, and the test compares the output with a model without offloading. On main the output assert alone fails for the leaf_level disk cases; with block_level, bn is in the unmatched group that is onloaded for the forward, so the output is right and only the direct read after the forward sees the uninitialized buffers, which the buffer asserts cover.

@sayakpaul

Copy link
Copy Markdown
Member

/diffusers-bot pytest tests/models/ -k "group_offloading"

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

✅ pytest tests/models/ -k "group_offloading" passed on GPU — view logs.

… buffer test

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sayakpaul

Copy link
Copy Markdown
Member

/diffusers-bot pytest tests/models/ -k "group_offloading"

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

✅ pytest tests/models/ -k "group_offloading" passed on GPU — view logs.

@sayakpaul
sayakpaul merged commit 2232c16 into huggingface:main Dec 4, 2026
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Group offloading to disk leaves buffers read without a forward call uninitialized (FLUX.2 Klein all-black image)

3 participants