tahamajs's picture
download
raw
1.53 kB
from torch import nn, Tensor
from zeta.nn.modules.simple_rmsnorm import SimpleRMSNorm
import torch.nn.functional as F
def activation_quant(x: Tensor):
"""Per token quantization to 8bits. No grouping is needed for quantization
Args:
x (Tensor): _description_
Returns:
_type_: _description_
"""
scale = 127.0 / x.abs().max(dim=-1, keepdim=True).values.clamp_(min=1e-5)
y = (x * scale).round().clamp_(-128, 127) / scale
return y
def weight_quant(w: Tensor):
scale = w.abs().mean()
e = w.mean()
u = (w - e).sign() * scale
return u
class BitLinearNew(nn.Linear):
"""
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 arguments.
Attributes:
dim (int): The input dimension of the layer.
"""
def forward(self, x: Tensor) -> Tensor:
"""
Forward pass of the BitLinear layer.
Args:
x (Tensor): The input tensor.
Returns:
Tensor: The output tensor.
"""
w = self.weight
x_norm = SimpleRMSNorm(self.in_features)(x)
# STE using detach
x_quant = x_norm + (activation_quant(x_norm) - x_norm).detach()
w_quant = w + (weight_quant(w) - w).detach()
y = F.linear(x_quant, w_quant)
return y

Xet Storage Details

Size:
1.53 kB
·
Xet hash:
8a50879be6b8bd79a51e13ec86e9b202a4985e26d46749b7a9f38eda76955499

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.