You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -10,28 +10,239 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o
10
10
specific language governing permissions and limitations under the License.
11
11
-->
12
12
13
-
# Single files
13
+
# Loading Pipelines and Models via `from_single_file`
14
14
15
-
Diffusers supports loading pretrained pipeline (or model) weights stored in a single file, such as a `ckpt` or `safetensors` file. These single file types are typically produced from community trained models. There are three classes for loading single file weights:
15
+
The `from_single_file` method allows you to load supported pipelines using a single checkpoint fileas opposed to the folder format used by Diffusers. This is useful if you are working with many of the Stable Diffusion Web UI's (such as A1111) that extensively rely on a single file to distribute all the components of a diffusion model.
16
16
17
-
-[`FromSingleFileMixin`] supports loading pretrained pipeline weights stored in a single file, which can either be a `ckpt` or `safetensors` file.
18
-
-[`FromOriginalVAEMixin`] supports loading a pretrained [`AutoencoderKL`] from pretrained ControlNet weights stored in a single file, which can either be a `ckpt` or `safetensors` file.
19
-
-[`FromOriginalControlnetMixin`] supports loading pretrained ControlNet weights stored in a single file, which can either be a `ckpt` or `safetensors` file.
17
+
The `from_single_file` method also supports loading models in their originally distributed format. This means that supported models that have been finetuned with other services can be loaded directly into supported Diffusers model objects and pipelines.
18
+
19
+
## Pipelines that currently support `from_single_file` loading
20
+
21
+
-[`StableDiffusionPipeline`]
22
+
-[`StableDiffusionImg2ImgPipeline`]
23
+
-[`StableDiffusionInpaintPipeline`]
24
+
-[`StableDiffusionControlNetPipeline`]
25
+
-[`StableDiffusionControlNetImg2ImgPipeline`]
26
+
-[`StableDiffusionControlNetInpaintPipeline`]
27
+
-[`StableDiffusionUpscalePipeline`]
28
+
-[`StableDiffusionXLPipeline`]
29
+
-[`StableDiffusionXLImg2ImgPipeline`]
30
+
-[`StableDiffusionXLInpaintPipeline`]
31
+
-[`StableDiffusionXLInstructPix2PixPipeline`]
32
+
-[`StableDiffusionXLControlNetPipeline`]
33
+
-[`StableDiffusionXLKDiffusionPipeline`]
34
+
-[`LatentConsistencyModelPipeline`]
35
+
-[`LatentConsistencyModelImg2ImgPipeline`]
36
+
-[`StableDiffusionControlNetXSPipeline`]
37
+
-[`StableDiffusionXLControlNetXSPipeline`]
38
+
-[`LEditsPPPipelineStableDiffusion`]
39
+
-[`LEditsPPPipelineStableDiffusionXL`]
40
+
-[`PIAPipeline`]
41
+
42
+
## Models that currently support `from_single_file` loading
## Setting components in a Pipeline using `from_single_file`
61
+
62
+
Swap components of the pipeline by passing them directly to the `from_single_file` method. e.g If you would like use a different scheduler than the pipeline default.
63
+
64
+
```python
65
+
from diffusers import StableDiffusionXLPipeline, DDIMScheduler
model = StableCascadeUNet.from_single_file(ckpt_path)
91
+
92
+
```
93
+
94
+
## Using a Diffusers model repository to configure single file loading
95
+
96
+
Under the hood, `from_single_file` will try to determine a model repository to use to configure the components of the pipeline. You can also pass in a repository id to the `config` argument of the `from_single_file` method to explicitly set the repository to use.
## Override configuration options when using single file loading
109
+
110
+
Override the default model or pipeline configuration options when using `from_single_file` by passing in the relevant arguments directly to the `from_single_file` method. Any argument that is supported by the model or pipeline class can be configured in this way:
111
+
112
+
```python
113
+
from diffusers import StableDiffusionXLInstructPix2PixPipeline
model = UNet2DConditionModel.from_single_file(ckpt_path, upcast_attention=True)
125
+
126
+
```
127
+
128
+
In the example above, since we explicitly passed `repo_id="segmind/SSD-1B"`, it will use this [configuration file](https://huggingface.co/segmind/SSD-1B/blob/main/unet/config.json) from the "unet" subfolder in `"segmind/SSD-1B"` to configure the unet component included in the checkpoint; Similarly, it will use the `config.json` file from `"vae"` subfolder to configure the vae model, `config.json` file from text_encoder folder to configure text_encoder and so on.
129
+
130
+
Note that most of the time you do not need to explicitly a `config` argument, `from_single_file` will automatically map the checkpoint to a repo id (we will discuss this in more details in next section). However, this can be useful in cases where model components might have been changed from what was originally distributed or in cases where a checkpoint file might not have the necessary metadata to correctly determine the configuration to use for the pipeline.
20
131
21
132
<Tip>
22
133
23
134
To learn more about how to load single file weights, see the [Load different Stable Diffusion formats](../../using-diffusers/other-formats) loading guide.
As of `diffusers>=0.28.0` the `from_single_file` method will attempt to configure a pipeline or model by first inferring the model type from the checkpoint file and then using the model type to determine the appropriate model repo configuration to use from the Hugging Face Hub. For example, any single file checkpoint based on the Stable Diffusion XL base model will use the [`stabilityai/stable-diffusion-xl-base-1.0`](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) model repo to configure the pipeline.
30
141
31
-
## FromOriginalVAEMixin
142
+
If you are working in an environment with restricted internet access, it is recommended to download the config files and checkpoints for the model to your preferred directory and pass the local paths to the `pretrained_model_link_or_path` and `config` arguments of the `from_single_file` method.
By default this will download the checkpoints and config files to the [Hugging Face Hub cache directory](https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache). You can also specify a local directory to download the files to by passing the `local_dir` argument to the `hf_hub_download` and `snapshot_download` functions.
162
+
163
+
```python
164
+
from huggingface_hub import hf_hub_download, snapshot_download
## Working with local files on file systems that do not support symlinking
183
+
184
+
By default the `from_single_file` method relies on the `huggingface_hub` caching mechanism to fetch and store checkpoints and config files for models and pipelines. If you are working with a file system that does not support symlinking, it is recommended that you first download the checkpoint file to a local directory and disable symlinking by passing the `local_dir_use_symlink=False` argument to the `hf_hub_download` and `snapshot_download` functions.
185
+
186
+
```python
187
+
from huggingface_hub import hf_hub_download, snapshot_download
188
+
189
+
my_local_checkpoint_path = hf_hub_download(
190
+
repo_id="segmind/SSD-1B",
191
+
filename="SSD-1B.safetensors"
192
+
local_dir="my_local_checkpoints",
193
+
local_dir_use_symlinks=False
194
+
)
195
+
print("My local checkpoint: ", my_local_checkpoint_path)
Disabling symlinking means that the `huggingface_hub` caching mechanism has no way to determine whether a file has already been downloaded to the local directory. This means that the `hf_hub_download` and `snapshot_download` functions will download files to the local directory each time they are executed. If you are disabling symlinking, it is recommended that you separate the model download and loading steps to avoid downloading the same file multiple times.
215
+
216
+
</Tip>
217
+
218
+
## Using the original configuration file of a model
219
+
220
+
If you would like to configure the parameters of the model components in the pipeline using the orignal YAML configuration file, you can pass a local path or url to the original configuration file to the `original_config` argument of the `from_single_file` method.
In the example above, the `original_config` file is only used to configure the parameters of the individual model components of the pipeline. For example it will be used to configure parameters such as the `in_channels` of the `vae` model and `unet` model. It is not used to determine the type of component objects in the pipeline.
233
+
234
+
235
+
<Tip>
236
+
When using `original_config` with local_files_only=True`, Diffusers will attempt to infer the components based on the type signatures of pipeline class, rather than attempting to fetch the pipeline config from the Hugging Face Hub. This is to prevent backwards breaking changes in existing code that might not be able to connect to the internet to fetch the necessary pipeline config files.
237
+
238
+
This is not as reliable as providing a path to a local config repo and might lead to errors when configuring the pipeline. To avoid this, please run the pipeline with `local_files_only=False` once to download the appropriate pipeline config files to the local cache.
0 commit comments