| 151 | return magictime_pipeline |
| 152 | |
| 153 | def load_diffusers_lora(pipeline, state_dict, alpha=1.0): |
| 154 | # directly update weight in diffusers model |
| 155 | for key in state_dict: |
| 156 | # only process lora down key |
| 157 | if "up." in key: continue |
| 158 | |
| 159 | up_key = key.replace(".down.", ".up.") |
| 160 | model_key = key.replace("processor.", "").replace("_lora", "").replace("down.", "").replace("up.", "") |
| 161 | model_key = model_key.replace("to_out.", "to_out.0.") |
| 162 | layer_infos = model_key.split(".")[:-1] |
| 163 | |
| 164 | curr_layer = pipeline.unet |
| 165 | while len(layer_infos) > 0: |
| 166 | temp_name = layer_infos.pop(0) |
| 167 | curr_layer = curr_layer.__getattr__(temp_name) |
| 168 | |
| 169 | weight_down = state_dict[key] * 2 |
| 170 | weight_up = state_dict[up_key] * 2 |
| 171 | curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down).to(curr_layer.weight.data.device) |
| 172 | |
| 173 | return pipeline |
| 174 | |
| 175 | def load_diffusers_lora_unet(unet, state_dict, alpha=1.0): |
| 176 | # directly update weight in diffusers model |