Set the currently active adapters for use in the UNet. Args: adapter_names (`List[str]` or `str`): The names of the adapters to use. adapter_weights (`Union[List[float], float]`, *optional*): The adapter(s) weights to use with
(
self,
adapter_names: Union[List[str], str],
weights: Optional[Union[float, Dict, List[float], List[Dict], List[None]]] = None,
)
| 54 | _hf_peft_config_loaded = False |
| 55 | |
| 56 | def set_adapters( |
| 57 | self, |
| 58 | adapter_names: Union[List[str], str], |
| 59 | weights: Optional[Union[float, Dict, List[float], List[Dict], List[None]]] = None, |
| 60 | ): |
| 61 | """ |
| 62 | Set the currently active adapters for use in the UNet. |
| 63 | |
| 64 | Args: |
| 65 | adapter_names (`List[str]` or `str`): |
| 66 | The names of the adapters to use. |
| 67 | adapter_weights (`Union[List[float], float]`, *optional*): |
| 68 | The adapter(s) weights to use with the UNet. If `None`, the weights are set to `1.0` for all the |
| 69 | adapters. |
| 70 | |
| 71 | Example: |
| 72 | |
| 73 | ```py |
| 74 | from diffusers import AutoPipelineForText2Image |
| 75 | import torch |
| 76 | |
| 77 | pipeline = AutoPipelineForText2Image.from_pretrained( |
| 78 | "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16 |
| 79 | ).to("cuda") |
| 80 | pipeline.load_lora_weights( |
| 81 | "jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors", adapter_name="cinematic" |
| 82 | ) |
| 83 | pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel") |
| 84 | pipeline.set_adapters(["cinematic", "pixel"], adapter_weights=[0.5, 0.5]) |
| 85 | ``` |
| 86 | """ |
| 87 | if not USE_PEFT_BACKEND: |
| 88 | raise ValueError("PEFT backend is required for `set_adapters()`.") |
| 89 | |
| 90 | adapter_names = [adapter_names] if isinstance(adapter_names, str) else adapter_names |
| 91 | |
| 92 | # Expand weights into a list, one entry per adapter |
| 93 | # examples for e.g. 2 adapters: [{...}, 7] -> [7,7] ; None -> [None, None] |
| 94 | if not isinstance(weights, list): |
| 95 | weights = [weights] * len(adapter_names) |
| 96 | |
| 97 | if len(adapter_names) != len(weights): |
| 98 | raise ValueError( |
| 99 | f"Length of adapter names {len(adapter_names)} is not equal to the length of their weights {len(weights)}." |
| 100 | ) |
| 101 | |
| 102 | # Set None values to default of 1.0 |
| 103 | # e.g. [{...}, 7] -> [{...}, 7] ; [None, None] -> [1.0, 1.0] |
| 104 | weights = [w if w is not None else 1.0 for w in weights] |
| 105 | |
| 106 | # e.g. [{...}, 7] -> [{expanded dict...}, 7] |
| 107 | scale_expansion_fn = _SET_ADAPTER_SCALE_FN_MAPPING[self.__class__.__name__] |
| 108 | weights = scale_expansion_fn(self, weights) |
| 109 | |
| 110 | set_weights_and_activate_adapters(self, adapter_names, weights) |
| 111 | |
| 112 | def add_adapter(self, adapter_config, adapter_name: str = "default") -> None: |
| 113 | r""" |