Repository navigation
Conversation
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>
sayakpaul
left a comment
There was a problem hiding this comment.
Thanks! I left some comments. LMK if they make sense.
| # 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} |
There was a problem hiding this comment.
But this does have the risk of pressurizing the memory a little bit right?
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
This is fine as long as buffers don't contribute too much.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
Should also do an assert on the outputs with and without offloading.
There was a problem hiding this comment.
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.
|
/diffusers-bot pytest tests/models/ -k "group_offloading" |
|
✅ |
… buffer test Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
|
/diffusers-bot pytest tests/models/ -k "group_offloading" |
|
✅ |
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_varin 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 tocpu_param_dict) and restores them on disk offload; parameters still gettorch.empty_like. Buffers stay in host memory as a result; the largest I found is SD3'spos_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):
TestDirectBufferAccessGroupOffload, 4 memory cases (stream / no stream x leaf / block)TestDirectBufferAccessGroupOffload, 4 disk casesassert torch.equal(model.bn.running_mean.cpu(), running_mean))tests/hooks/test_group_offloading.pyThe 2 failures in the rest of the file are the MPS
test_error_raised_if_streams_used_and_backend_has_no_streamscases.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_likeloop; whichever merges later needs a small rebase.A maintainer has not acknowledged #15332 yet.
Before submitting
self-reviewskill on the diff?documentation guidelines, and
here are tips on formatting docstrings.