| 349 | self._lora_scale = lora_scale |
| 350 | |
| 351 | def _unfuse_lora(self): |
| 352 | if not (getattr(self, "w_up", None) is not None and getattr(self, "w_down", None) is not None): |
| 353 | return |
| 354 | |
| 355 | fused_weight = self.weight.data |
| 356 | dtype, device = fused_weight.data.dtype, fused_weight.data.device |
| 357 | |
| 358 | self.w_up = self.w_up.to(device=device).float() |
| 359 | self.w_down = self.w_down.to(device).float() |
| 360 | |
| 361 | fusion = torch.mm(self.w_up.flatten(start_dim=1), self.w_down.flatten(start_dim=1)) |
| 362 | fusion = fusion.reshape((fused_weight.shape)) |
| 363 | unfused_weight = fused_weight.float() - (self._lora_scale * fusion) |
| 364 | self.weight.data = unfused_weight.to(device=device, dtype=dtype) |
| 365 | |
| 366 | self.w_up = None |
| 367 | self.w_down = None |
| 368 | |
| 369 | def forward(self, hidden_states: torch.Tensor, scale: float = 1.0) -> torch.Tensor: |
| 370 | if self.padding_mode != "zeros": |