MCPcopy Create free account
hub / github.com/kyegomez/BitNet / BitLinear

Class BitLinear

bitnet/bitlinear.py:27–60  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

25
26
27class 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

Callers 15

example.pyFile · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
replace_linears_in_hfFunction · 0.90

Calls

no outgoing calls