BitLora class represents a custom linear layer with LoRa (Low Rank) regularization. Args: rank (int): The rank of the LoRa regularization. Default is 4. lora_alpha (int): The scaling factor for LoRa regularization. Default is 1. *args: Variable length argument list.
| 26 | |
| 27 | |
| 28 | class BitLora(BitLinear): |
| 29 | """ |
| 30 | BitLora class represents a custom linear layer with LoRa (Low Rank) regularization. |
| 31 | |
| 32 | Args: |
| 33 | rank (int): The rank of the LoRa regularization. Default is 4. |
| 34 | lora_alpha (int): The scaling factor for LoRa regularization. Default is 1. |
| 35 | *args: Variable length argument list. |
| 36 | **kwargs: Arbitrary keyword arguments. |
| 37 | |
| 38 | Attributes: |
| 39 | rank (int): The rank of the LoRa regularization. |
| 40 | lora_alpha (int): The scaling factor for LoRa regularization. |
| 41 | scaling (float): The scaling factor for LoRa regularization. |
| 42 | merged (bool): Indicates whether the LoRa regularization has been merged with the weight matrix. |
| 43 | lora_a (nn.Parameter): The learnable parameter matrix of shape (in_features, rank). |
| 44 | lora_b (nn.Parameter): The learnable parameter matrix of shape (rank, out_features). |
| 45 | |
| 46 | Examples: |
| 47 | |
| 48 | """ |
| 49 | |
| 50 | def __init__(self, rank: int = 4, lora_alpha: int = 1, *args, **kwargs): |
| 51 | super(BitLora, self).__init__(*args, **kwargs) |
| 52 | self.rank = rank |
| 53 | self.lora_alpha = lora_alpha |
| 54 | self.scaling = self.lora_alpha / self.rank |
| 55 | self.merged = False |
| 56 | |
| 57 | self.lora_a = nn.Parameter(torch.zeros(self.in_features, rank)) |
| 58 | self.lora_b = nn.Parameter(torch.zeros(rank, self.out_features)) |
| 59 | |
| 60 | # Rmsnorm |
| 61 | self.rms_norm = SimpleRMSNorm(self.in_features) |
| 62 | |
| 63 | def forward(self, x: Tensor) -> Tensor: |
| 64 | """ |
| 65 | Forward pass of the BitLora layer. |
| 66 | |
| 67 | Args: |
| 68 | x (Tensor): The input tensor. |
| 69 | |
| 70 | Returns: |
| 71 | Tensor: The output tensor. |
| 72 | |
| 73 | """ |
| 74 | w = self.weight |
| 75 | |
| 76 | # Normalize the input tensor |
| 77 | x_norm = self.rms_norm(x) |
| 78 | |
| 79 | # Activation Quant |
| 80 | x_quant = activation_quant(x_norm) |
| 81 | |
| 82 | if not self.merged and self.rank > 0: |
| 83 | lora = self.lora_a @ self.lora_b |
| 84 | w = w + lora * self.scaling |
| 85 |
nothing calls this directly
no outgoing calls
no test coverage detected