PEFT¶
Diffusers supports loading adapters such as LoRA with the PEFT library with the loaders.peft.PeftAdapterMixin
class. This allows modeling classes in Diffusers like UNet2DConditionModel
, SD3Transformer2DModel
to operate with an adapter.
Tip
Refer to the Inference with PEFT tutorial for an overview of how to use PEFT in Diffusers for inference.
mindone.diffusers.loaders.peft.PeftAdapterMixin
¶
A class containing all functions for loading and using adapters weights that are supported in PEFT library. For more details about adapters and injecting them in a base model, check out the PEFT documentation.
Install the latest version of PEFT, and use this mixin to:
- Attach new adapters in the model.
- Attach multiple adapters and iteratively activate/deactivate them.
- Activate/deactivate all adapters from the model.
- Get a list of the active adapters.
Source code in mindone/diffusers/loaders/peft.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.active_adapters()
¶
Gets the current list of active adapters of the model.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
Source code in mindone/diffusers/loaders/peft.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.add_adapter(adapter_config, adapter_name='default')
¶
Adds a new adapter to the current model for training. If no adapter name is passed, a default name is assigned to the adapter to follow the convention of the PEFT library.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them in the PEFT documentation.
PARAMETER | DESCRIPTION |
---|---|
adapter_config |
The configuration of the adapter to add; supported adapters are non-prefix tuning and adaption prompt methods.
TYPE:
|
adapter_name |
The name of the adapter to add. If no name is passed, a default name is assigned to the adapter.
TYPE:
|
Source code in mindone/diffusers/loaders/peft.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.delete_adapters(adapter_names)
¶
Delete an adapter's LoRA layers from the UNet.
PARAMETER | DESCRIPTION |
---|---|
adapter_names |
The names (single string or list of strings) of the adapter to delete.
TYPE:
|
from mindone.diffusers import AutoPipelineForText2Image
import mindspore
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", mindspore_dtype=mindspore.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_names="cinematic"
)
pipeline.delete_adapters("cinematic")
Source code in mindone/diffusers/loaders/peft.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.disable_adapters()
¶
Disable all adapters attached to the model and fallback to inference with the base model only.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
Source code in mindone/diffusers/loaders/peft.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.disable_lora()
¶
Disable the UNet's active LoRA layers.
Example:
from mindone.diffusers import AutoPipelineForText2Image
import mindspore
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", mindspore_dtype=mindspore.float16
)
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
)
pipeline.disable_lora()
Source code in mindone/diffusers/loaders/peft.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.enable_adapters()
¶
Enable adapters that are attached to the model. The model uses self.active_adapters()
to retrieve the list of
adapters to enable.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
Source code in mindone/diffusers/loaders/peft.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.enable_lora()
¶
Enable the UNet's active LoRA layers.
Example:
from mindone.diffusers import AutoPipelineForText2Image
import mindspore
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", mindspore_dtype=mindspore.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
)
pipeline.enable_lora()
Source code in mindone/diffusers/loaders/peft.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.set_adapter(adapter_name)
¶
Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT documentation.
PARAMETER | DESCRIPTION |
---|---|
adapter_name |
The list of adapters to set or the adapter name in the case of a single adapter.
TYPE:
|
Source code in mindone/diffusers/loaders/peft.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
mindone.diffusers.loaders.peft.PeftAdapterMixin.set_adapters(adapter_names, weights=None)
¶
Set the currently active adapters for use in the UNet.
PARAMETER | DESCRIPTION |
---|---|
adapter_names |
The names of the adapters to use.
TYPE:
|
adapter_weights |
The adapter(s) weights to use with the UNet. If
TYPE:
|
from mindone.diffusers import AutoPipelineForText2Image
import mindspore
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", mindspore_dtype=mindspore.float16
).to("cuda")
pipeline.load_lora_weights(
"jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic"
)
pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
pipeline.set_adapters(["cinematic", "pixel"], adapter_weights=[0.5, 0.5])
Source code in mindone/diffusers/loaders/peft.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|