(self, model, state_dict_lora, lora_prefix="", alpha=1.0, model_resource="")
| 244 | |
| 245 | |
| 246 | def load(self, model, state_dict_lora, lora_prefix="", alpha=1.0, model_resource=""): |
| 247 | state_dict_model = model.state_dict() |
| 248 | device, dtype, computation_device, computation_dtype = self.fetch_device_and_dtype(state_dict_model) |
| 249 | lora_name_dict = self.get_name_dict(state_dict_lora) |
| 250 | for name in lora_name_dict: |
| 251 | weight_up = state_dict_lora[lora_name_dict[name][0]].to(device=computation_device, dtype=computation_dtype) |
| 252 | weight_down = state_dict_lora[lora_name_dict[name][1]].to(device=computation_device, dtype=computation_dtype) |
| 253 | if len(weight_up.shape) == 4: |
| 254 | weight_up = weight_up.squeeze(3).squeeze(2) |
| 255 | weight_down = weight_down.squeeze(3).squeeze(2) |
| 256 | weight_lora = alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3) |
| 257 | else: |
| 258 | weight_lora = alpha * torch.mm(weight_up, weight_down) |
| 259 | weight_model = state_dict_model[name].to(device=computation_device, dtype=computation_dtype) |
| 260 | weight_patched = weight_model + weight_lora |
| 261 | state_dict_model[name] = weight_patched.to(device=device, dtype=dtype) |
| 262 | print(f" {len(lora_name_dict)} tensors are updated.") |
| 263 | model.load_state_dict(state_dict_model) |
| 264 | |
| 265 | |
| 266 |
nothing calls this directly
no test coverage detected