Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a disable_mmap option to the from_single_file loader to improve load performance on network mounts #10305

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

danhipke
Copy link

What does this PR do?

This PR adds a no_mmap option to the from_single_file loader to disable the mmap loading behavior of safetensors.

This provides a huge performance benefit when loading from a file on a network mount (from 16 minutes -> <1 min for a 7.2GB model), which doesn't handle the seeky-ness of mmap based loading very well. Examples demonstrating this issue:

Fixes #10280

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@DN6 @yiyixuxu @asomoza

@danhipke danhipke changed the title No mmap Add a no_mmap option to the from_single_file loader to improve load performance on network mounts Dec 19, 2024
@sayakpaul
Copy link
Member

@DN6 I think the slow loading issue is affecting the CI quite a bit. So, maybe this could be prioritized.

@HuggingFaceDocBuilderDev

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.

@DN6
Copy link
Collaborator

DN6 commented Dec 20, 2024

Thanks @danhipke. The change looks good. But load_state_dict is also used in from_pretrained so we would need to add the option there as well.

And then pass it to the subsequent load_state_dict calls.

state_dict = load_state_dict(model_file, variant=variant)

and here

state_dict = load_state_dict(model_file, variant=variant)

And a small nit. I would prefer naming the flag disable_mmap

cc: @yiyixuxu for awareness.

@danhipke danhipke changed the title Add a no_mmap option to the from_single_file loader to improve load performance on network mounts Add a disable_mmap option to the from_single_file loader to improve load performance on network mounts Dec 20, 2024
sayakpaul and others added 6 commits December 20, 2024 21:25
…)` (huggingface#10316)

Update ltx_video.md to remove generator from `from_pretrained()`
Update pipeline_hunyuan_video.py

docs: fix a mistake
…peError in function prepare_latents caused by audio_vae_length (huggingface#10306)

[BUG FIX] [Stable Audio Pipeline] TypeError: new_zeros(): argument 'size' failed to unpack the object at pos 3 with error "type must be tuple of ints,but got float"

torch.Tensor.new_zeros() takes a single argument size (int...) – a list, tuple, or torch.Size of integers defining the shape of the output tensor.

in function prepare_latents:
audio_vae_length = self.transformer.config.sample_size * self.vae.hop_length
audio_shape = (batch_size // num_waveforms_per_prompt, audio_channels, audio_vae_length)
...
audio = initial_audio_waveforms.new_zeros(audio_shape)

audio_vae_length evaluates to float because self.transformer.config.sample_size returns a float

Co-authored-by: hlky <hlky@hlky.ac>
@danhipke
Copy link
Author

@DN6 Added it to from_pretrained and renamed.

src/diffusers/loaders/single_file.py Outdated Show resolved Hide resolved
src/diffusers/loaders/single_file_model.py Outdated Show resolved Hide resolved
@DN6
Copy link
Collaborator

DN6 commented Dec 23, 2024

danhipke and others added 2 commits December 22, 2024 22:59
Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com>
Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com>
@danhipke
Copy link
Author

Applied suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Safetensors loading uses mmap with multiple processes sharing the same fd cause slow gcsfuse performance
9 participants