diffusers 0.29.2__py3-none-any.whl → 0.30.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffusers/__init__.py +94 -3
- diffusers/commands/env.py +1 -5
- diffusers/configuration_utils.py +4 -9
- diffusers/dependency_versions_table.py +2 -2
- diffusers/image_processor.py +1 -2
- diffusers/loaders/__init__.py +17 -2
- diffusers/loaders/ip_adapter.py +10 -7
- diffusers/loaders/lora_base.py +752 -0
- diffusers/loaders/lora_pipeline.py +2252 -0
- diffusers/loaders/peft.py +213 -5
- diffusers/loaders/single_file.py +3 -14
- diffusers/loaders/single_file_model.py +31 -10
- diffusers/loaders/single_file_utils.py +293 -8
- diffusers/loaders/textual_inversion.py +1 -6
- diffusers/loaders/unet.py +23 -208
- diffusers/models/__init__.py +20 -0
- diffusers/models/activations.py +22 -0
- diffusers/models/attention.py +386 -7
- diffusers/models/attention_processor.py +1937 -629
- diffusers/models/autoencoders/__init__.py +2 -0
- diffusers/models/autoencoders/autoencoder_kl.py +14 -3
- diffusers/models/autoencoders/autoencoder_kl_cogvideox.py +1271 -0
- diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +1 -1
- diffusers/models/autoencoders/autoencoder_oobleck.py +464 -0
- diffusers/models/autoencoders/autoencoder_tiny.py +1 -0
- diffusers/models/autoencoders/consistency_decoder_vae.py +1 -1
- diffusers/models/autoencoders/vq_model.py +4 -4
- diffusers/models/controlnet.py +2 -3
- diffusers/models/controlnet_hunyuan.py +401 -0
- diffusers/models/controlnet_sd3.py +11 -11
- diffusers/models/controlnet_sparsectrl.py +789 -0
- diffusers/models/controlnet_xs.py +40 -10
- diffusers/models/downsampling.py +68 -0
- diffusers/models/embeddings.py +403 -36
- diffusers/models/model_loading_utils.py +1 -3
- diffusers/models/modeling_flax_utils.py +1 -6
- diffusers/models/modeling_utils.py +4 -16
- diffusers/models/normalization.py +203 -12
- diffusers/models/transformers/__init__.py +6 -0
- diffusers/models/transformers/auraflow_transformer_2d.py +543 -0
- diffusers/models/transformers/cogvideox_transformer_3d.py +485 -0
- diffusers/models/transformers/hunyuan_transformer_2d.py +19 -15
- diffusers/models/transformers/latte_transformer_3d.py +327 -0
- diffusers/models/transformers/lumina_nextdit2d.py +340 -0
- diffusers/models/transformers/pixart_transformer_2d.py +102 -1
- diffusers/models/transformers/prior_transformer.py +1 -1
- diffusers/models/transformers/stable_audio_transformer.py +458 -0
- diffusers/models/transformers/transformer_flux.py +455 -0
- diffusers/models/transformers/transformer_sd3.py +18 -4
- diffusers/models/unets/unet_1d_blocks.py +1 -1
- diffusers/models/unets/unet_2d_condition.py +8 -1
- diffusers/models/unets/unet_3d_blocks.py +51 -920
- diffusers/models/unets/unet_3d_condition.py +4 -1
- diffusers/models/unets/unet_i2vgen_xl.py +4 -1
- diffusers/models/unets/unet_kandinsky3.py +1 -1
- diffusers/models/unets/unet_motion_model.py +1330 -84
- diffusers/models/unets/unet_spatio_temporal_condition.py +1 -1
- diffusers/models/unets/unet_stable_cascade.py +1 -3
- diffusers/models/unets/uvit_2d.py +1 -1
- diffusers/models/upsampling.py +64 -0
- diffusers/models/vq_model.py +8 -4
- diffusers/optimization.py +1 -1
- diffusers/pipelines/__init__.py +100 -3
- diffusers/pipelines/animatediff/__init__.py +4 -0
- diffusers/pipelines/animatediff/pipeline_animatediff.py +50 -40
- diffusers/pipelines/animatediff/pipeline_animatediff_controlnet.py +1076 -0
- diffusers/pipelines/animatediff/pipeline_animatediff_sdxl.py +17 -27
- diffusers/pipelines/animatediff/pipeline_animatediff_sparsectrl.py +1008 -0
- diffusers/pipelines/animatediff/pipeline_animatediff_video2video.py +51 -38
- diffusers/pipelines/audioldm2/modeling_audioldm2.py +1 -1
- diffusers/pipelines/audioldm2/pipeline_audioldm2.py +1 -0
- diffusers/pipelines/aura_flow/__init__.py +48 -0
- diffusers/pipelines/aura_flow/pipeline_aura_flow.py +591 -0
- diffusers/pipelines/auto_pipeline.py +97 -19
- diffusers/pipelines/cogvideo/__init__.py +48 -0
- diffusers/pipelines/cogvideo/pipeline_cogvideox.py +746 -0
- diffusers/pipelines/consistency_models/pipeline_consistency_models.py +1 -1
- diffusers/pipelines/controlnet/pipeline_controlnet.py +24 -30
- diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +31 -30
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint.py +24 -153
- diffusers/pipelines/controlnet/pipeline_controlnet_inpaint_sd_xl.py +19 -28
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +18 -28
- diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +29 -32
- diffusers/pipelines/controlnet/pipeline_flax_controlnet.py +2 -2
- diffusers/pipelines/controlnet_hunyuandit/__init__.py +48 -0
- diffusers/pipelines/controlnet_hunyuandit/pipeline_hunyuandit_controlnet.py +1042 -0
- diffusers/pipelines/controlnet_sd3/pipeline_stable_diffusion_3_controlnet.py +35 -0
- diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs.py +10 -6
- diffusers/pipelines/controlnet_xs/pipeline_controlnet_xs_sd_xl.py +0 -4
- diffusers/pipelines/deepfloyd_if/pipeline_if.py +2 -2
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img.py +2 -2
- diffusers/pipelines/deepfloyd_if/pipeline_if_img2img_superresolution.py +2 -2
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.py +2 -2
- diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting_superresolution.py +2 -2
- diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.py +2 -2
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion.py +11 -6
- diffusers/pipelines/deprecated/alt_diffusion/pipeline_alt_diffusion_img2img.py +11 -6
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_cycle_diffusion.py +6 -6
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_inpaint_legacy.py +6 -6
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_model_editing.py +10 -10
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_paradigms.py +10 -6
- diffusers/pipelines/deprecated/stable_diffusion_variants/pipeline_stable_diffusion_pix2pix_zero.py +3 -3
- diffusers/pipelines/deprecated/versatile_diffusion/modeling_text_unet.py +1 -1
- diffusers/pipelines/flux/__init__.py +47 -0
- diffusers/pipelines/flux/pipeline_flux.py +749 -0
- diffusers/pipelines/flux/pipeline_output.py +21 -0
- diffusers/pipelines/free_init_utils.py +2 -0
- diffusers/pipelines/free_noise_utils.py +236 -0
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3.py +2 -2
- diffusers/pipelines/kandinsky3/pipeline_kandinsky3_img2img.py +2 -2
- diffusers/pipelines/kolors/__init__.py +54 -0
- diffusers/pipelines/kolors/pipeline_kolors.py +1070 -0
- diffusers/pipelines/kolors/pipeline_kolors_img2img.py +1247 -0
- diffusers/pipelines/kolors/pipeline_output.py +21 -0
- diffusers/pipelines/kolors/text_encoder.py +889 -0
- diffusers/pipelines/kolors/tokenizer.py +334 -0
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_img2img.py +30 -29
- diffusers/pipelines/latent_consistency_models/pipeline_latent_consistency_text2img.py +23 -29
- diffusers/pipelines/latte/__init__.py +48 -0
- diffusers/pipelines/latte/pipeline_latte.py +881 -0
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion.py +4 -4
- diffusers/pipelines/ledits_pp/pipeline_leditspp_stable_diffusion_xl.py +0 -4
- diffusers/pipelines/lumina/__init__.py +48 -0
- diffusers/pipelines/lumina/pipeline_lumina.py +897 -0
- diffusers/pipelines/pag/__init__.py +67 -0
- diffusers/pipelines/pag/pag_utils.py +237 -0
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd.py +1329 -0
- diffusers/pipelines/pag/pipeline_pag_controlnet_sd_xl.py +1612 -0
- diffusers/pipelines/pag/pipeline_pag_hunyuandit.py +953 -0
- diffusers/pipelines/pag/pipeline_pag_kolors.py +1136 -0
- diffusers/pipelines/pag/pipeline_pag_pixart_sigma.py +872 -0
- diffusers/pipelines/pag/pipeline_pag_sd.py +1050 -0
- diffusers/pipelines/pag/pipeline_pag_sd_3.py +985 -0
- diffusers/pipelines/pag/pipeline_pag_sd_animatediff.py +862 -0
- diffusers/pipelines/pag/pipeline_pag_sd_xl.py +1333 -0
- diffusers/pipelines/pag/pipeline_pag_sd_xl_img2img.py +1529 -0
- diffusers/pipelines/pag/pipeline_pag_sd_xl_inpaint.py +1753 -0
- diffusers/pipelines/pia/pipeline_pia.py +30 -37
- diffusers/pipelines/pipeline_flax_utils.py +4 -9
- diffusers/pipelines/pipeline_loading_utils.py +0 -3
- diffusers/pipelines/pipeline_utils.py +2 -14
- diffusers/pipelines/semantic_stable_diffusion/pipeline_semantic_stable_diffusion.py +0 -1
- diffusers/pipelines/stable_audio/__init__.py +50 -0
- diffusers/pipelines/stable_audio/modeling_stable_audio.py +158 -0
- diffusers/pipelines/stable_audio/pipeline_stable_audio.py +745 -0
- diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +2 -0
- diffusers/pipelines/stable_diffusion/pipeline_flax_stable_diffusion.py +1 -1
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +23 -29
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_depth2img.py +15 -8
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +30 -29
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +23 -152
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_instruct_pix2pix.py +8 -4
- diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_upscale.py +11 -11
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip.py +8 -6
- diffusers/pipelines/stable_diffusion/pipeline_stable_unclip_img2img.py +6 -6
- diffusers/pipelines/stable_diffusion_3/__init__.py +2 -0
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +34 -3
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +33 -7
- diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +1201 -0
- diffusers/pipelines/stable_diffusion_attend_and_excite/pipeline_stable_diffusion_attend_and_excite.py +3 -3
- diffusers/pipelines/stable_diffusion_diffedit/pipeline_stable_diffusion_diffedit.py +6 -6
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen.py +5 -5
- diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py +5 -5
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_k_diffusion.py +6 -6
- diffusers/pipelines/stable_diffusion_k_diffusion/pipeline_stable_diffusion_xl_k_diffusion.py +0 -4
- diffusers/pipelines/stable_diffusion_ldm3d/pipeline_stable_diffusion_ldm3d.py +23 -29
- diffusers/pipelines/stable_diffusion_panorama/pipeline_stable_diffusion_panorama.py +27 -29
- diffusers/pipelines/stable_diffusion_sag/pipeline_stable_diffusion_sag.py +3 -3
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +17 -27
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +26 -29
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +17 -145
- diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_instruct_pix2pix.py +0 -4
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_adapter.py +6 -6
- diffusers/pipelines/t2i_adapter/pipeline_stable_diffusion_xl_adapter.py +18 -28
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth.py +8 -6
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_synth_img2img.py +8 -6
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero.py +6 -4
- diffusers/pipelines/text_to_video_synthesis/pipeline_text_to_video_zero_sdxl.py +0 -4
- diffusers/pipelines/unidiffuser/pipeline_unidiffuser.py +3 -3
- diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.py +1 -1
- diffusers/pipelines/wuerstchen/pipeline_wuerstchen_prior.py +5 -4
- diffusers/schedulers/__init__.py +8 -0
- diffusers/schedulers/scheduling_cosine_dpmsolver_multistep.py +572 -0
- diffusers/schedulers/scheduling_ddim.py +1 -1
- diffusers/schedulers/scheduling_ddim_cogvideox.py +449 -0
- diffusers/schedulers/scheduling_ddpm.py +1 -1
- diffusers/schedulers/scheduling_ddpm_parallel.py +1 -1
- diffusers/schedulers/scheduling_deis_multistep.py +2 -2
- diffusers/schedulers/scheduling_dpm_cogvideox.py +489 -0
- diffusers/schedulers/scheduling_dpmsolver_multistep.py +1 -1
- diffusers/schedulers/scheduling_dpmsolver_multistep_inverse.py +1 -1
- diffusers/schedulers/scheduling_dpmsolver_singlestep.py +64 -19
- diffusers/schedulers/scheduling_edm_dpmsolver_multistep.py +2 -2
- diffusers/schedulers/scheduling_flow_match_euler_discrete.py +63 -39
- diffusers/schedulers/scheduling_flow_match_heun_discrete.py +321 -0
- diffusers/schedulers/scheduling_ipndm.py +1 -1
- diffusers/schedulers/scheduling_unipc_multistep.py +1 -1
- diffusers/schedulers/scheduling_utils.py +1 -3
- diffusers/schedulers/scheduling_utils_flax.py +1 -3
- diffusers/training_utils.py +99 -14
- diffusers/utils/__init__.py +2 -2
- diffusers/utils/dummy_pt_objects.py +210 -0
- diffusers/utils/dummy_torch_and_torchsde_objects.py +15 -0
- diffusers/utils/dummy_torch_and_transformers_and_sentencepiece_objects.py +47 -0
- diffusers/utils/dummy_torch_and_transformers_objects.py +315 -0
- diffusers/utils/dynamic_modules_utils.py +1 -11
- diffusers/utils/export_utils.py +50 -6
- diffusers/utils/hub_utils.py +45 -42
- diffusers/utils/import_utils.py +37 -15
- diffusers/utils/loading_utils.py +80 -3
- diffusers/utils/testing_utils.py +11 -8
- {diffusers-0.29.2.dist-info → diffusers-0.30.1.dist-info}/METADATA +73 -83
- {diffusers-0.29.2.dist-info → diffusers-0.30.1.dist-info}/RECORD +217 -164
- {diffusers-0.29.2.dist-info → diffusers-0.30.1.dist-info}/WHEEL +1 -1
- diffusers/loaders/autoencoder.py +0 -146
- diffusers/loaders/controlnet.py +0 -136
- diffusers/loaders/lora.py +0 -1728
- {diffusers-0.29.2.dist-info → diffusers-0.30.1.dist-info}/LICENSE +0 -0
- {diffusers-0.29.2.dist-info → diffusers-0.30.1.dist-info}/entry_points.txt +0 -0
- {diffusers-0.29.2.dist-info → diffusers-0.30.1.dist-info}/top_level.txt +0 -0
diffusers/loaders/peft.py
CHANGED
@@ -12,15 +12,34 @@
|
|
12
12
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
13
|
# See the License for the specific language governing permissions and
|
14
14
|
# limitations under the License.
|
15
|
-
|
16
|
-
|
17
|
-
from
|
15
|
+
import inspect
|
16
|
+
from functools import partial
|
17
|
+
from typing import Dict, List, Optional, Union
|
18
|
+
|
19
|
+
from ..utils import (
|
20
|
+
MIN_PEFT_VERSION,
|
21
|
+
USE_PEFT_BACKEND,
|
22
|
+
check_peft_version,
|
23
|
+
delete_adapter_layers,
|
24
|
+
is_peft_available,
|
25
|
+
set_adapter_layers,
|
26
|
+
set_weights_and_activate_adapters,
|
27
|
+
)
|
28
|
+
from .unet_loader_utils import _maybe_expand_lora_scales
|
29
|
+
|
30
|
+
|
31
|
+
_SET_ADAPTER_SCALE_FN_MAPPING = {
|
32
|
+
"UNet2DConditionModel": _maybe_expand_lora_scales,
|
33
|
+
"UNetMotionModel": _maybe_expand_lora_scales,
|
34
|
+
"SD3Transformer2DModel": lambda model_cls, weights: weights,
|
35
|
+
"FluxTransformer2DModel": lambda model_cls, weights: weights,
|
36
|
+
}
|
18
37
|
|
19
38
|
|
20
39
|
class PeftAdapterMixin:
|
21
40
|
"""
|
22
41
|
A class containing all functions for loading and using adapters weights that are supported in PEFT library. For
|
23
|
-
more details about adapters and injecting them in a
|
42
|
+
more details about adapters and injecting them in a base model, check out the PEFT
|
24
43
|
[documentation](https://huggingface.co/docs/peft/index).
|
25
44
|
|
26
45
|
Install the latest version of PEFT, and use this mixin to:
|
@@ -33,6 +52,62 @@ class PeftAdapterMixin:
|
|
33
52
|
|
34
53
|
_hf_peft_config_loaded = False
|
35
54
|
|
55
|
+
def set_adapters(
|
56
|
+
self,
|
57
|
+
adapter_names: Union[List[str], str],
|
58
|
+
weights: Optional[Union[float, Dict, List[float], List[Dict], List[None]]] = None,
|
59
|
+
):
|
60
|
+
"""
|
61
|
+
Set the currently active adapters for use in the UNet.
|
62
|
+
|
63
|
+
Args:
|
64
|
+
adapter_names (`List[str]` or `str`):
|
65
|
+
The names of the adapters to use.
|
66
|
+
adapter_weights (`Union[List[float], float]`, *optional*):
|
67
|
+
The adapter(s) weights to use with the UNet. If `None`, the weights are set to `1.0` for all the
|
68
|
+
adapters.
|
69
|
+
|
70
|
+
Example:
|
71
|
+
|
72
|
+
```py
|
73
|
+
from diffusers import AutoPipelineForText2Image
|
74
|
+
import torch
|
75
|
+
|
76
|
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
77
|
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
|
78
|
+
).to("cuda")
|
79
|
+
pipeline.load_lora_weights(
|
80
|
+
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
|
81
|
+
)
|
82
|
+
pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
|
83
|
+
pipeline.set_adapters(["cinematic", "pixel"], adapter_weights=[0.5, 0.5])
|
84
|
+
```
|
85
|
+
"""
|
86
|
+
if not USE_PEFT_BACKEND:
|
87
|
+
raise ValueError("PEFT backend is required for `set_adapters()`.")
|
88
|
+
|
89
|
+
adapter_names = [adapter_names] if isinstance(adapter_names, str) else adapter_names
|
90
|
+
|
91
|
+
# Expand weights into a list, one entry per adapter
|
92
|
+
# examples for e.g. 2 adapters: [{...}, 7] -> [7,7] ; None -> [None, None]
|
93
|
+
if not isinstance(weights, list):
|
94
|
+
weights = [weights] * len(adapter_names)
|
95
|
+
|
96
|
+
if len(adapter_names) != len(weights):
|
97
|
+
raise ValueError(
|
98
|
+
f"Length of adapter names {len(adapter_names)} is not equal to the length of their weights {len(weights)}."
|
99
|
+
)
|
100
|
+
|
101
|
+
# Set None values to default of 1.0
|
102
|
+
# e.g. [{...}, 7] -> [{...}, 7] ; [None, None] -> [1.0, 1.0]
|
103
|
+
weights = [w if w is not None else 1.0 for w in weights]
|
104
|
+
|
105
|
+
# e.g. [{...}, 7] -> [{expanded dict...}, 7]
|
106
|
+
scale_expansion_fn = _SET_ADAPTER_SCALE_FN_MAPPING[self.__class__.__name__]
|
107
|
+
weights = scale_expansion_fn(self, weights)
|
108
|
+
|
109
|
+
set_weights_and_activate_adapters(self, adapter_names, weights)
|
110
|
+
|
36
111
|
def add_adapter(self, adapter_config, adapter_name: str = "default") -> None:
|
37
112
|
r"""
|
38
113
|
Adds a new adapter to the current model for training. If no adapter name is passed, a default name is assigned
|
@@ -66,7 +141,7 @@ class PeftAdapterMixin:
|
|
66
141
|
)
|
67
142
|
|
68
143
|
# Unlike transformers, here we don't need to retrieve the name_or_path of the unet as the loading logic is
|
69
|
-
# handled by the `load_lora_layers` or `
|
144
|
+
# handled by the `load_lora_layers` or `StableDiffusionLoraLoaderMixin`. Therefore we set it to `None` here.
|
70
145
|
adapter_config.base_model_name_or_path = None
|
71
146
|
inject_adapter_in_model(adapter_config, self, adapter_name)
|
72
147
|
self.set_adapter(adapter_name)
|
@@ -185,3 +260,136 @@ class PeftAdapterMixin:
|
|
185
260
|
for _, module in self.named_modules():
|
186
261
|
if isinstance(module, BaseTunerLayer):
|
187
262
|
return module.active_adapter
|
263
|
+
|
264
|
+
def fuse_lora(self, lora_scale=1.0, safe_fusing=False, adapter_names=None):
|
265
|
+
if not USE_PEFT_BACKEND:
|
266
|
+
raise ValueError("PEFT backend is required for `fuse_lora()`.")
|
267
|
+
|
268
|
+
self.lora_scale = lora_scale
|
269
|
+
self._safe_fusing = safe_fusing
|
270
|
+
self.apply(partial(self._fuse_lora_apply, adapter_names=adapter_names))
|
271
|
+
|
272
|
+
def _fuse_lora_apply(self, module, adapter_names=None):
|
273
|
+
from peft.tuners.tuners_utils import BaseTunerLayer
|
274
|
+
|
275
|
+
merge_kwargs = {"safe_merge": self._safe_fusing}
|
276
|
+
|
277
|
+
if isinstance(module, BaseTunerLayer):
|
278
|
+
if self.lora_scale != 1.0:
|
279
|
+
module.scale_layer(self.lora_scale)
|
280
|
+
|
281
|
+
# For BC with prevous PEFT versions, we need to check the signature
|
282
|
+
# of the `merge` method to see if it supports the `adapter_names` argument.
|
283
|
+
supported_merge_kwargs = list(inspect.signature(module.merge).parameters)
|
284
|
+
if "adapter_names" in supported_merge_kwargs:
|
285
|
+
merge_kwargs["adapter_names"] = adapter_names
|
286
|
+
elif "adapter_names" not in supported_merge_kwargs and adapter_names is not None:
|
287
|
+
raise ValueError(
|
288
|
+
"The `adapter_names` argument is not supported with your PEFT version. Please upgrade"
|
289
|
+
" to the latest version of PEFT. `pip install -U peft`"
|
290
|
+
)
|
291
|
+
|
292
|
+
module.merge(**merge_kwargs)
|
293
|
+
|
294
|
+
def unfuse_lora(self):
|
295
|
+
if not USE_PEFT_BACKEND:
|
296
|
+
raise ValueError("PEFT backend is required for `unfuse_lora()`.")
|
297
|
+
self.apply(self._unfuse_lora_apply)
|
298
|
+
|
299
|
+
def _unfuse_lora_apply(self, module):
|
300
|
+
from peft.tuners.tuners_utils import BaseTunerLayer
|
301
|
+
|
302
|
+
if isinstance(module, BaseTunerLayer):
|
303
|
+
module.unmerge()
|
304
|
+
|
305
|
+
def unload_lora(self):
|
306
|
+
if not USE_PEFT_BACKEND:
|
307
|
+
raise ValueError("PEFT backend is required for `unload_lora()`.")
|
308
|
+
|
309
|
+
from ..utils import recurse_remove_peft_layers
|
310
|
+
|
311
|
+
recurse_remove_peft_layers(self)
|
312
|
+
if hasattr(self, "peft_config"):
|
313
|
+
del self.peft_config
|
314
|
+
|
315
|
+
def disable_lora(self):
|
316
|
+
"""
|
317
|
+
Disables the active LoRA layers of the underlying model.
|
318
|
+
|
319
|
+
Example:
|
320
|
+
|
321
|
+
```py
|
322
|
+
from diffusers import AutoPipelineForText2Image
|
323
|
+
import torch
|
324
|
+
|
325
|
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
326
|
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
|
327
|
+
).to("cuda")
|
328
|
+
pipeline.load_lora_weights(
|
329
|
+
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
|
330
|
+
)
|
331
|
+
pipeline.disable_lora()
|
332
|
+
```
|
333
|
+
"""
|
334
|
+
if not USE_PEFT_BACKEND:
|
335
|
+
raise ValueError("PEFT backend is required for this method.")
|
336
|
+
set_adapter_layers(self, enabled=False)
|
337
|
+
|
338
|
+
def enable_lora(self):
|
339
|
+
"""
|
340
|
+
Enables the active LoRA layers of the underlying model.
|
341
|
+
|
342
|
+
Example:
|
343
|
+
|
344
|
+
```py
|
345
|
+
from diffusers import AutoPipelineForText2Image
|
346
|
+
import torch
|
347
|
+
|
348
|
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
349
|
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
|
350
|
+
).to("cuda")
|
351
|
+
pipeline.load_lora_weights(
|
352
|
+
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
|
353
|
+
)
|
354
|
+
pipeline.enable_lora()
|
355
|
+
```
|
356
|
+
"""
|
357
|
+
if not USE_PEFT_BACKEND:
|
358
|
+
raise ValueError("PEFT backend is required for this method.")
|
359
|
+
set_adapter_layers(self, enabled=True)
|
360
|
+
|
361
|
+
def delete_adapters(self, adapter_names: Union[List[str], str]):
|
362
|
+
"""
|
363
|
+
Delete an adapter's LoRA layers from the underlying model.
|
364
|
+
|
365
|
+
Args:
|
366
|
+
adapter_names (`Union[List[str], str]`):
|
367
|
+
The names (single string or list of strings) of the adapter to delete.
|
368
|
+
|
369
|
+
Example:
|
370
|
+
|
371
|
+
```py
|
372
|
+
from diffusers import AutoPipelineForText2Image
|
373
|
+
import torch
|
374
|
+
|
375
|
+
pipeline = AutoPipelineForText2Image.from_pretrained(
|
376
|
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
|
377
|
+
).to("cuda")
|
378
|
+
pipeline.load_lora_weights(
|
379
|
+
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_names="cinematic"
|
380
|
+
)
|
381
|
+
pipeline.delete_adapters("cinematic")
|
382
|
+
```
|
383
|
+
"""
|
384
|
+
if not USE_PEFT_BACKEND:
|
385
|
+
raise ValueError("PEFT backend is required for this method.")
|
386
|
+
|
387
|
+
if isinstance(adapter_names, str):
|
388
|
+
adapter_names = [adapter_names]
|
389
|
+
|
390
|
+
for adapter_name in adapter_names:
|
391
|
+
delete_adapter_layers(self, adapter_name)
|
392
|
+
|
393
|
+
# Pop also the corresponding adapter from the config
|
394
|
+
if hasattr(self, "peft_config"):
|
395
|
+
self.peft_config.pop(adapter_name, None)
|
diffusers/loaders/single_file.py
CHANGED
@@ -23,6 +23,7 @@ from packaging import version
|
|
23
23
|
from ..utils import deprecate, is_transformers_available, logging
|
24
24
|
from .single_file_utils import (
|
25
25
|
SingleFileComponentError,
|
26
|
+
_is_legacy_scheduler_kwargs,
|
26
27
|
_is_model_weights_in_cached_folder,
|
27
28
|
_legacy_load_clip_tokenizer,
|
28
29
|
_legacy_load_safety_checker,
|
@@ -42,7 +43,6 @@ logger = logging.get_logger(__name__)
|
|
42
43
|
# Legacy behaviour. `from_single_file` does not load the safety checker unless explicitly provided
|
43
44
|
SINGLE_FILE_OPTIONAL_COMPONENTS = ["safety_checker"]
|
44
45
|
|
45
|
-
|
46
46
|
if is_transformers_available():
|
47
47
|
import transformers
|
48
48
|
from transformers import PreTrainedModel, PreTrainedTokenizer
|
@@ -135,7 +135,7 @@ def load_single_file_sub_model(
|
|
135
135
|
class_obj, checkpoint=checkpoint, config=cached_model_config_path, local_files_only=local_files_only
|
136
136
|
)
|
137
137
|
|
138
|
-
elif is_diffusers_scheduler and is_legacy_loading:
|
138
|
+
elif is_diffusers_scheduler and (is_legacy_loading or _is_legacy_scheduler_kwargs(kwargs)):
|
139
139
|
loaded_sub_model = _legacy_load_scheduler(
|
140
140
|
class_obj, checkpoint=checkpoint, component_name=name, original_config=original_config, **kwargs
|
141
141
|
)
|
@@ -242,7 +242,6 @@ def _download_diffusers_model_config_from_hub(
|
|
242
242
|
revision,
|
243
243
|
proxies,
|
244
244
|
force_download=None,
|
245
|
-
resume_download=None,
|
246
245
|
local_files_only=None,
|
247
246
|
token=None,
|
248
247
|
):
|
@@ -253,7 +252,6 @@ def _download_diffusers_model_config_from_hub(
|
|
253
252
|
revision=revision,
|
254
253
|
proxies=proxies,
|
255
254
|
force_download=force_download,
|
256
|
-
resume_download=resume_download,
|
257
255
|
local_files_only=local_files_only,
|
258
256
|
token=token,
|
259
257
|
allow_patterns=allow_patterns,
|
@@ -288,9 +286,7 @@ class FromSingleFileMixin:
|
|
288
286
|
cache_dir (`Union[str, os.PathLike]`, *optional*):
|
289
287
|
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
|
290
288
|
is not used.
|
291
|
-
|
292
|
-
Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v1
|
293
|
-
of Diffusers.
|
289
|
+
|
294
290
|
proxies (`Dict[str, str]`, *optional*):
|
295
291
|
A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
|
296
292
|
'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
|
@@ -352,7 +348,6 @@ class FromSingleFileMixin:
|
|
352
348
|
deprecate("original_config_file", "1.0.0", deprecation_message)
|
353
349
|
original_config = original_config_file
|
354
350
|
|
355
|
-
resume_download = kwargs.pop("resume_download", None)
|
356
351
|
force_download = kwargs.pop("force_download", False)
|
357
352
|
proxies = kwargs.pop("proxies", None)
|
358
353
|
token = kwargs.pop("token", None)
|
@@ -382,7 +377,6 @@ class FromSingleFileMixin:
|
|
382
377
|
|
383
378
|
checkpoint = load_single_file_checkpoint(
|
384
379
|
pretrained_model_link_or_path,
|
385
|
-
resume_download=resume_download,
|
386
380
|
force_download=force_download,
|
387
381
|
proxies=proxies,
|
388
382
|
token=token,
|
@@ -412,7 +406,6 @@ class FromSingleFileMixin:
|
|
412
406
|
revision=revision,
|
413
407
|
proxies=proxies,
|
414
408
|
force_download=force_download,
|
415
|
-
resume_download=resume_download,
|
416
409
|
local_files_only=local_files_only,
|
417
410
|
token=token,
|
418
411
|
)
|
@@ -435,7 +428,6 @@ class FromSingleFileMixin:
|
|
435
428
|
revision=revision,
|
436
429
|
proxies=proxies,
|
437
430
|
force_download=force_download,
|
438
|
-
resume_download=resume_download,
|
439
431
|
local_files_only=False,
|
440
432
|
token=token,
|
441
433
|
)
|
@@ -555,7 +547,4 @@ class FromSingleFileMixin:
|
|
555
547
|
|
556
548
|
pipe = pipeline_class(**init_kwargs)
|
557
549
|
|
558
|
-
if torch_dtype is not None:
|
559
|
-
pipe.to(dtype=torch_dtype)
|
560
|
-
|
561
550
|
return pipe
|
@@ -11,6 +11,7 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
+
import importlib
|
14
15
|
import inspect
|
15
16
|
import re
|
16
17
|
from contextlib import nullcontext
|
@@ -21,7 +22,9 @@ from huggingface_hub.utils import validate_hf_hub_args
|
|
21
22
|
from ..utils import deprecate, is_accelerate_available, logging
|
22
23
|
from .single_file_utils import (
|
23
24
|
SingleFileComponentError,
|
25
|
+
convert_animatediff_checkpoint_to_diffusers,
|
24
26
|
convert_controlnet_checkpoint,
|
27
|
+
convert_flux_transformer_checkpoint_to_diffusers,
|
25
28
|
convert_ldm_unet_checkpoint,
|
26
29
|
convert_ldm_vae_checkpoint,
|
27
30
|
convert_sd3_transformer_checkpoint_to_diffusers,
|
@@ -69,9 +72,30 @@ SINGLE_FILE_LOADABLE_CLASSES = {
|
|
69
72
|
"checkpoint_mapping_fn": convert_sd3_transformer_checkpoint_to_diffusers,
|
70
73
|
"default_subfolder": "transformer",
|
71
74
|
},
|
75
|
+
"MotionAdapter": {
|
76
|
+
"checkpoint_mapping_fn": convert_animatediff_checkpoint_to_diffusers,
|
77
|
+
},
|
78
|
+
"SparseControlNetModel": {
|
79
|
+
"checkpoint_mapping_fn": convert_animatediff_checkpoint_to_diffusers,
|
80
|
+
},
|
81
|
+
"FluxTransformer2DModel": {
|
82
|
+
"checkpoint_mapping_fn": convert_flux_transformer_checkpoint_to_diffusers,
|
83
|
+
"default_subfolder": "transformer",
|
84
|
+
},
|
72
85
|
}
|
73
86
|
|
74
87
|
|
88
|
+
def _get_single_file_loadable_mapping_class(cls):
|
89
|
+
diffusers_module = importlib.import_module(__name__.split(".")[0])
|
90
|
+
for loadable_class_str in SINGLE_FILE_LOADABLE_CLASSES:
|
91
|
+
loadable_class = getattr(diffusers_module, loadable_class_str)
|
92
|
+
|
93
|
+
if issubclass(cls, loadable_class):
|
94
|
+
return loadable_class_str
|
95
|
+
|
96
|
+
return None
|
97
|
+
|
98
|
+
|
75
99
|
def _get_mapping_function_kwargs(mapping_fn, **kwargs):
|
76
100
|
parameters = inspect.signature(mapping_fn).parameters
|
77
101
|
|
@@ -121,9 +145,7 @@ class FromOriginalModelMixin:
|
|
121
145
|
cache_dir (`Union[str, os.PathLike]`, *optional*):
|
122
146
|
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
|
123
147
|
is not used.
|
124
|
-
|
125
|
-
Whether or not to resume downloading the model weights and configuration files. If set to `False`, any
|
126
|
-
incompletely downloaded files are deleted.
|
148
|
+
|
127
149
|
proxies (`Dict[str, str]`, *optional*):
|
128
150
|
A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
|
129
151
|
'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
|
@@ -149,8 +171,9 @@ class FromOriginalModelMixin:
|
|
149
171
|
```
|
150
172
|
"""
|
151
173
|
|
152
|
-
|
153
|
-
if class_name not in SINGLE_FILE_LOADABLE_CLASSES:
|
174
|
+
mapping_class_name = _get_single_file_loadable_mapping_class(cls)
|
175
|
+
# if class_name not in SINGLE_FILE_LOADABLE_CLASSES:
|
176
|
+
if mapping_class_name is None:
|
154
177
|
raise ValueError(
|
155
178
|
f"FromOriginalModelMixin is currently only compatible with {', '.join(SINGLE_FILE_LOADABLE_CLASSES.keys())}"
|
156
179
|
)
|
@@ -171,7 +194,6 @@ class FromOriginalModelMixin:
|
|
171
194
|
"`from_single_file` cannot accept both `config` and `original_config` arguments. Please provide only one of these arguments"
|
172
195
|
)
|
173
196
|
|
174
|
-
resume_download = kwargs.pop("resume_download", None)
|
175
197
|
force_download = kwargs.pop("force_download", False)
|
176
198
|
proxies = kwargs.pop("proxies", None)
|
177
199
|
token = kwargs.pop("token", None)
|
@@ -186,7 +208,6 @@ class FromOriginalModelMixin:
|
|
186
208
|
else:
|
187
209
|
checkpoint = load_single_file_checkpoint(
|
188
210
|
pretrained_model_link_or_path_or_dict,
|
189
|
-
resume_download=resume_download,
|
190
211
|
force_download=force_download,
|
191
212
|
proxies=proxies,
|
192
213
|
token=token,
|
@@ -195,7 +216,7 @@ class FromOriginalModelMixin:
|
|
195
216
|
revision=revision,
|
196
217
|
)
|
197
218
|
|
198
|
-
mapping_functions = SINGLE_FILE_LOADABLE_CLASSES[
|
219
|
+
mapping_functions = SINGLE_FILE_LOADABLE_CLASSES[mapping_class_name]
|
199
220
|
|
200
221
|
checkpoint_mapping_fn = mapping_functions["checkpoint_mapping_fn"]
|
201
222
|
if original_config:
|
@@ -207,7 +228,7 @@ class FromOriginalModelMixin:
|
|
207
228
|
if config_mapping_fn is None:
|
208
229
|
raise ValueError(
|
209
230
|
(
|
210
|
-
f"`original_config` has been provided for {
|
231
|
+
f"`original_config` has been provided for {mapping_class_name} but no mapping function"
|
211
232
|
"was found to convert the original config to a Diffusers config in"
|
212
233
|
"`diffusers.loaders.single_file_utils`"
|
213
234
|
)
|
@@ -267,7 +288,7 @@ class FromOriginalModelMixin:
|
|
267
288
|
)
|
268
289
|
if not diffusers_format_checkpoint:
|
269
290
|
raise SingleFileComponentError(
|
270
|
-
f"Failed to load {
|
291
|
+
f"Failed to load {mapping_class_name}. Weights for this component appear to be missing in the checkpoint."
|
271
292
|
)
|
272
293
|
|
273
294
|
ctx = init_empty_weights if is_accelerate_available() else nullcontext
|