-
Notifications
You must be signed in to change notification settings - Fork 6.5k
[docs] Models #12248
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
Merged
Merged
[docs] Models #12248
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| <!--Copyright 2025 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| --> | ||
|
|
||
| [[open-in-colab]] | ||
|
|
||
| # Models | ||
|
|
||
| A diffusion model relies on a few individual models working together to generate an output. These models are responsible for denoising, encoding inputs, and decoding latents into the actual outputs. | ||
|
|
||
| This guide will show you how to load models. | ||
|
|
||
| ## Loading a model | ||
|
|
||
| All models are loaded with the [`~ModelMixin.from_pretrained`] method, which downloads and caches the latest model version. If the latest files are available in the local cache, [`~ModelMixin.from_pretrained`] reuses files in the cache. | ||
|
|
||
| Pass the `subfolder` argument to [`~ModelMixin.from_pretrained`] to specify where to load the model weights from. Omit the `subfolder` argument if the repository doesn't have a subfolder structure or if you're loading a standalone model. | ||
|
|
||
| ```py | ||
| from diffusers import QwenImageTransformer2DModel | ||
|
|
||
| model = QwenImageTransformer2DModel.from_pretrained("Qwen/Qwen-Image", subfolder="transformer") | ||
| ``` | ||
|
|
||
| ## AutoModel | ||
|
|
||
| [`AutoModel`] detects the model class from a `model_index.json` file or a model's `config.json` file. It fetches the correct model class from these files and delegates the actual loading to the model class. [`AutoModel`] is useful for automatic model type detection without needing to know the exact model class beforehand. | ||
|
|
||
| ```py | ||
| from diffusers import AutoModel | ||
|
|
||
| model = AutoModel.from_pretrained( | ||
| "Qwen/Qwen-Image", subfolder="transformer" | ||
| ) | ||
| ``` | ||
|
|
||
| ## Model data types | ||
|
|
||
| Use the `torch_dtype` argument in [`~ModelMixin.from_pretrained`] to load a model with a specific data type. This allows you to load a model in a lower precision to reduce memory usage. | ||
|
|
||
| ```py | ||
| import torch | ||
| from diffusers import QwenImageTransformer2DModel | ||
|
|
||
| model = QwenImageTransformer2DModel.from_pretrained( | ||
| "Qwen/Qwen-Image", | ||
| subfolder="transformer", | ||
| torch_dtype=torch.bfloat16 | ||
| ) | ||
| ``` | ||
|
|
||
| [nn.Module.to](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.to) can also convert to a specific data type on the fly. However, it converts *all* weights to the requested data type unlike `torch_dtype` which respects `_keep_in_fp32_modules`. This argument preserves layers in `torch.float32` for numerical stability and best generation quality (see example [_keep_in_fp32_modules](https://github.com/huggingface/diffusers/blob/f864a9a352fa4a220d860bfdd1782e3e5af96382/src/diffusers/models/transformers/transformer_wan.py#L374)) | ||
|
|
||
| ```py | ||
| from diffusers import QwenImageTransformer2DModel | ||
|
|
||
| model = QwenImageTransformer2DModel.from_pretrained( | ||
| "Qwen/Qwen-Image", subfolder="transformer" | ||
| ) | ||
| model = model.to(dtype=torch.float16) | ||
| ``` | ||
|
|
||
| ## Device placement | ||
|
|
||
| Use the `device_map` argument in [`~ModelMixin.from_pretrained`] to place a model on an accelerator like a GPU. It is especially helpful where there are multiple GPUs. | ||
|
|
||
| Diffusers currently provides three options to `device_map` for individual models, `"cuda"`, `"balanced"` and `"auto"`. Refer to the table below to compare the three placement strategies. | ||
|
|
||
| | parameter | description | | ||
| |---|---| | ||
| | `"cuda"` | places pipeline on a supported accelerator (CUDA) | | ||
| | `"balanced"` | evenly distributes pipeline on all GPUs | | ||
| | `"auto"` | distribute model from fastest device first to slowest | | ||
|
|
||
| Use the `max_memory` argument in [`~ModelMixin.from_pretrained`] to allocate a maximum amount of memory to use on each device. By default, Diffusers uses the maximum amount available. | ||
|
|
||
| ```py | ||
| import torch | ||
| from diffusers import QwenImagePipeline | ||
|
|
||
| max_memory = {0: "16GB", 1: "16GB"} | ||
| pipeline = QwenImagePipeline.from_pretrained( | ||
| "Qwen/Qwen-Image", | ||
| torch_dtype=torch.bfloat16, | ||
| device_map="cuda", | ||
| max_memory=max_memory | ||
| ) | ||
| ``` | ||
|
|
||
| The `hf_device_map` attribute allows you to access and view the `device_map`. | ||
|
|
||
| ```py | ||
| print(transformer.hf_device_map) | ||
| # {'': device(type='cuda')} | ||
| ``` | ||
|
|
||
| ## Saving models | ||
|
|
||
| Save a model with the [`~ModelMixin.save_pretrained`] method. | ||
|
|
||
| ```py | ||
| from diffusers import QwenImageTransformer2DModel | ||
|
|
||
| model = QwenImageTransformer2DModel.from_pretrained("Qwen/Qwen-Image", subfolder="transformer") | ||
| model.save_pretrained("./local/model") | ||
| ``` | ||
|
|
||
| For large models, it is helpful to use `max_shard_size` to save a model as multiple shards. A shard can be loaded faster and save memory (refer to the [parallel loading](./loading#parallel-loading) docs for more details), especially if there is more than one GPU. | ||
|
|
||
| ```py | ||
| model.save_pretrained("./local/model", max_shard_size="5GB") | ||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.