Fix duplicate safetensors.load_file call in _onload_from_disk #3
Open
gagandhakrey wants to merge 1 commit into
Open
Fix duplicate safetensors.load_file call in _onload_from_disk #3gagandhakrey wants to merge 1 commit into
gagandhakrey wants to merge 1 commit into
Conversation
…ream is None Signed-off-by: Gagan Dhakrey <gagandhakrey@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
)
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.