Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix duplicate safetensors.load_file call in _onload_from_disk when st…#13851

Merged
sayakpaul merged 2 commits into
huggingface:mainfrom
gagandhakrey:fix/group-offloading-duplicate-disk-load
Jun 1, 2026
Merged

Fix duplicate safetensors.load_file call in _onload_from_disk when st…#13851
sayakpaul merged 2 commits into
huggingface:mainfrom
gagandhakrey:fix/group-offloading-duplicate-disk-load

Conversation

@gagandhakrey

Copy link
Copy Markdown
Contributor

Fix: Duplicate safetensors.load_file Call in _onload_from_disk

Problem

In src/diffusers/hooks/group_offloading.py, ModuleGroup._onload_from_disk called safetensors.torch.load_file() twice when stream is None (the default, non-streaming path):

# Called unconditionally — result discarded when stream is None
device = str(self.onload_device) if self.stream is None else "cpu"
loaded_tensors = safetensors.torch.load_file(
    self.safetensors_file_path,
    device=device,
)

if self.stream is not None:
    ...
else:
    onload_device = (
        self.onload_device.type
        if isinstance(self.onload_device, torch.device)
        else self.onload_device
    )

    # Second load — overwrites the first, which is silently discarded
    loaded_tensors = safetensors.torch.load_file(
        self.safetensors_file_path,
        device=onload_device,
    )

The first load result was immediately overwritten and garbage collected. As a result, every _onload_from_disk invocation in the default (use_stream=False) path performed two full tensor loads from disk while only using the second result.

Since offload_to_disk_path is typically used without streaming, this affected the documented and most common usage pattern.

Impact

  • 2× disk I/O on every group load, slowing inference when disk offloading is enabled.
  • Transient 2× memory usage during loading (CPU memory and potentially VRAM depending on the target device), increasing the risk of OOM on memory-constrained systems.
  • Triggered on every _onload_from_disk call, which can occur hundreds or thousands of times during an inference run depending on model size and denoising steps.

Solution

Move the load_file() call into the corresponding execution branch so that tensors are loaded exactly once using the correct device argument for that path.

This removes the redundant disk read and avoids allocating an unused copy of the loaded tensors while preserving existing behavior.

@github-actions github-actions Bot added size/S PR with diff < 50 LOC hooks labels Jun 1, 2026
@gagandhakrey

Copy link
Copy Markdown
Contributor Author

Hey @AryanV @sayakpaul, I found a bug in _onload_from_disk where safetensors.load_file() was called twice in the default (use_stream=False) path, causing unnecessary disk I/O and memory overhead. Would appreciate a review. Thanks!

@DN6 DN6 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 @gagandhakrey 👍🏽

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@gagandhakrey

Copy link
Copy Markdown
Contributor Author

Thanks @DN6 for the review, looks like CI is green as well , just checking if there is anything else needed from my side so this can be merged


if self.stream is not None:
# Load to CPU first, pin memory, then async copy to the target device
loaded_tensors = safetensors.torch.load_file(self.safetensors_file_path, device="cpu")

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.

Why should it have to be conditioned on if self.strea is not None?

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.

@sayakpaul these two branches handle tensors differently after loading , the stream is not None branch needs device="cpu" specifically because it calls .pin_memory() on the tensor before doing a non_blocking async copy to the device — pinned memory is required for async host->device transfers to work correctly, the else branch loads directly to the target device and assigns immediately with no intermediate step, so there's no need to go through cpu at all , making the load unconditional with "cpu" would add an unnecessary CPU->device transfer in the no-stream path

@sayakpaul sayakpaul merged commit 9aee939 into huggingface:main Jun 1, 2026
15 checks passed
DN6 pushed a commit that referenced this pull request Jul 1, 2026
#13851)

Fix duplicate safetensors.load_file call in _onload_from_disk when stream is None

Signed-off-by: Gagan Dhakrey <[email protected]>
Co-authored-by: Sayak Paul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hooks size/S PR with diff < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants