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
Copy file name to clipboardExpand all lines: docs/source/en/tutorials/using_peft_for_inference.md
+8-206Lines changed: 8 additions & 206 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,216 +12,18 @@ specific language governing permissions and limitations under the License.
12
12
13
13
[[open-in-colab]]
14
14
15
-
# Load LoRAs for inference
15
+
# LoRA
16
16
17
-
There are many adapter types (with [LoRAs](https://huggingface.co/docs/peft/conceptual_guides/adapter#low-rank-adaptation-lora) being the most popular) trained in different styles to achieve different effects. You can even combine multiple adapters to create new and unique images.
17
+
## Adjust weight scale
18
18
19
-
In this tutorial, you'll learn how to easily load and manage adapters for inference with the 🤗 [PEFT](https://huggingface.co/docs/peft/index) integration in 🤗 Diffusers. You'll use LoRA as the main adapter technique, so you'll see the terms LoRA and adapter used interchangeably.
Next, load a [CiroN2022/toy-face](https://huggingface.co/CiroN2022/toy-face) adapter with the [`~diffusers.loaders.StableDiffusionXLLoraLoaderMixin.load_lora_weights`] method. With the 🤗 PEFT integration, you can assign a specific `adapter_name` to the checkpoint, which lets you easily switch between different LoRA checkpoints. Let's call this adapter `"toy"`.
With the `adapter_name` parameter, it is really easy to use another adapter for inference! Load the [nerijs/pixel-art-xl](https://huggingface.co/nerijs/pixel-art-xl) adapter that has been fine-tuned to generate pixel art images and call it `"pixel"`.
58
-
59
-
The pipeline automatically sets the first loaded adapter (`"toy"`) as the active adapter, but you can activate the `"pixel"` adapter with the [`~loaders.peft.PeftAdapterMixin.set_adapters`] method:
By default, if the most up-to-date versions of PEFT and Transformers are detected, `low_cpu_mem_usage` is set to `True` to speed up the loading time of LoRA checkpoints.
81
-
82
-
</Tip>
83
-
84
-
## Merge adapters
85
-
86
-
You can also merge different adapter checkpoints for inference to blend their styles together.
87
-
88
-
Once again, use the [`~loaders.peft.PeftAdapterMixin.set_adapters`] method to activate the `pixel` and `toy` adapters and specify the weights for how they should be merged.
LoRA checkpoints in the diffusion community are almost always obtained with [DreamBooth](https://huggingface.co/docs/diffusers/main/en/training/dreambooth). DreamBooth training often relies on "trigger" words in the input text prompts in order for the generation results to look as expected. When you combine multiple LoRA checkpoints, it's important to ensure the trigger words for the corresponding LoRA checkpoints are present in the input text prompts.
97
-
98
-
</Tip>
99
-
100
-
Remember to use the trigger words for [CiroN2022/toy-face](https://hf.co/CiroN2022/toy-face) and [nerijs/pixel-art-xl](https://hf.co/nerijs/pixel-art-xl) (these are found in their repositories) in the prompt to generate an image.
101
-
102
-
```python
103
-
prompt ="toy_face of a hacker with a hoodie, pixel art"
Impressive! As you can see, the model generated an image that mixed the characteristics of both adapters.
113
-
114
-
> [!TIP]
115
-
> Through its PEFT integration, Diffusers also offers more efficient merging methods which you can learn about in the [Merge LoRAs](../using-diffusers/merge_loras) guide!
116
-
117
-
To return to only using one adapter, use the [`~loaders.peft.PeftAdapterMixin.set_adapters`] method to activate the `"toy"` adapter:
For even more customization, you can control how strongly the adapter affects each part of the pipeline. For this, pass a dictionary with the control strengths (called "scales") to [`~loaders.peft.PeftAdapterMixin.set_adapters`].
145
-
146
-
For example, here's how you can turn on the adapter for the `down` parts, but turn it off for the `mid` and `up` parts:
147
-
```python
148
-
pipe.enable_lora() # enable lora again, after we disabled it above
149
-
prompt ="toy_face of a hacker with a hoodie, pixel art"
This is a really powerful feature. You can use it to control the adapter strengths down to per-transformer level. And you can even use it for multiple adapters.
180
-
```python
181
-
adapter_weight_scales_toy =0.5
182
-
adapter_weight_scales_pixel = {
183
-
"unet": {
184
-
"down": 0.9, # all transformers in the down-part will use scale 0.9
185
-
# "mid" # because, in this example, "mid" is not given, all transformers in the mid part will use the default scale 1.0
186
-
"up": {
187
-
"block_0": 0.6, # all 3 transformers in the 0th block in the up-part will use scale 0.6
188
-
"block_1": [0.4, 0.8, 1.0], # the 3 transformers in the 1st block in the up-part will use scales 0.4, 0.8 and 1.0 respectively
You have attached multiple adapters in this tutorial, and if you're feeling a bit lost on what adapters have been attached to the pipeline's components, use the [`~diffusers.loaders.StableDiffusionLoraLoaderMixin.get_active_adapters`] method to check the list of active adapters:
202
-
203
-
```py
204
-
active_adapters = pipe.get_active_adapters()
205
-
active_adapters
206
-
["toy", "pixel"]
207
-
```
208
-
209
-
You can also get the active adapters of each pipeline component with [`~diffusers.loaders.StableDiffusionLoraLoaderMixin.get_list_adapters`]:
0 commit comments