| 94 | self.lora_layer = lora_layer |
| 95 | |
| 96 | def _fuse_lora(self, lora_scale=1.0): |
| 97 | if self.lora_layer is None: |
| 98 | return |
| 99 | |
| 100 | dtype, device = self.weight.data.dtype, self.weight.data.device |
| 101 | |
| 102 | w_orig = self.weight.data.float() |
| 103 | w_up = self.lora_layer.up.weight.data.float() |
| 104 | w_down = self.lora_layer.down.weight.data.float() |
| 105 | |
| 106 | if self.lora_layer.network_alpha is not None: |
| 107 | w_up = w_up * self.lora_layer.network_alpha / self.lora_layer.rank |
| 108 | |
| 109 | fusion = torch.mm(w_up.flatten(start_dim=1), w_down.flatten(start_dim=1)) |
| 110 | fusion = fusion.reshape((w_orig.shape)) |
| 111 | fused_weight = w_orig + (lora_scale * fusion) |
| 112 | self.weight.data = fused_weight.to(device=device, dtype=dtype) |
| 113 | |
| 114 | # we can drop the lora layer now |
| 115 | self.lora_layer = None |
| 116 | |
| 117 | # offload the up and down matrices to CPU to not blow the memory |
| 118 | self.w_up = w_up.cpu() |
| 119 | self.w_down = w_down.cpu() |
| 120 | self._lora_scale = lora_scale |
| 121 | |
| 122 | def _unfuse_lora(self): |
| 123 | if not (hasattr(self, "w_up") and hasattr(self, "w_down")): |