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

Merged
merged 27 commits into from
Jan 10, 2025

Conversation

danhipke
Copy link
Contributor

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 <[email protected]>
@danhipke
Copy link
Contributor 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
Copy link
Contributor Author

Applied suggestions.

@danhipke
Copy link
Contributor Author

danhipke commented Jan 2, 2025

Any other changes needed before this can be merged?

@bghira
Copy link
Contributor

bghira commented Jan 3, 2025

fwiw ultralytics overrides torch.load behaviour to always use mmap so if that's used in combination here, they have to be loaded in the correct order or else the ultralytics one takes priority and removes the capability to disable mmap. this happens when using some face fix or photomaker pipeline etc.

@sayakpaul sayakpaul requested a review from yiyixuxu January 9, 2025 13:27
@DN6
Copy link
Collaborator

DN6 commented Jan 9, 2025

Hi @danhipke I think you just have to run make style && make quality so that the quality checks pass.

@DN6 DN6 added the roadmap Add to current release roadmap label Jan 10, 2025
@DN6 DN6 merged commit 52c05bd into huggingface:main Jan 10, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
close-to-merge roadmap Add to current release roadmap
Projects
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