Custom linear layer with bit quantization. Args: dim (int): The input dimension of the layer. training (bool, optional): Whether the layer is in training mode or not. Defaults to False. *args: Variable length argument list. **kwargs: Arbitrary keyword argume
| 25 | |
| 26 | |
| 27 | class BitLinear(nn.Linear): |
| 28 | """ |
| 29 | Custom linear layer with bit quantization. |
| 30 | |
| 31 | Args: |
| 32 | dim (int): The input dimension of the layer. |
| 33 | training (bool, optional): Whether the layer is in training mode or not. Defaults to False. |
| 34 | *args: Variable length argument list. |
| 35 | **kwargs: Arbitrary keyword arguments. |
| 36 | |
| 37 | Attributes: |
| 38 | dim (int): The input dimension of the layer. |
| 39 | |
| 40 | """ |
| 41 | |
| 42 | def forward(self, x: Tensor) -> Tensor: |
| 43 | """ |
| 44 | Forward pass of the BitLinear layer. |
| 45 | |
| 46 | Args: |
| 47 | x (Tensor): The input tensor. |
| 48 | |
| 49 | Returns: |
| 50 | Tensor: The output tensor. |
| 51 | |
| 52 | """ |
| 53 | w = self.weight |
| 54 | x_norm = SimpleRMSNorm(self.in_features)(x) |
| 55 | |
| 56 | # STE using detach |
| 57 | x_quant = x_norm + (activation_quant(x_norm) - x_norm).detach() |
| 58 | w_quant = w + (weight_quant(w) - w).detach() |
| 59 | y = F.linear(x_quant, w_quant) |
| 60 | return y |
no outgoing calls