tahamajs's picture
download
raw
4.25 kB
import torch
import torch.nn.functional as F
from torch import Tensor
from torch import nn
from bitnet.bit_ffn import BitFeedForward
from bitnet.bit_attention import BitMGQA
from zeta import OutputHead
def l2norm(t, dim=-1):
return F.normalize(t, dim=dim)
class RMSNorm(nn.Module):
"""
Root Mean Square Normalization (RMSNorm) module.
Args:
dim (int): The input dimension.
affine (bool, optional): If True, apply an affine transformation to the normalized output.
Default is True.
Attributes:
scale (float): The scaling factor for the normalized output.
gamma (torch.Tensor or float): The learnable parameter for the affine transformation.
"""
def __init__(self, dim, affine=True):
super().__init__()
self.scale = dim**0.5
self.gamma = nn.Parameter(torch.ones(dim)) if affine else 1.0
def forward(self, x):
return l2norm(x) * self.gamma * self.scale
class Transformer(nn.Module):
"""
Transformer module that applies multi-head attention and feed-forward layers.
Args:
dim (int): The dimension of the input and output tensors.
heads (int): The number of attention heads.
depth (int): The number of transformer layers.
ff_mult (int, optional): The multiplier for the hidden dimension in the feed-forward layers.
Defaults to 2.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Attributes:
layers (nn.ModuleList): List of multi-head attention layers.
ffn_layers (nn.ModuleList): List of feed-forward layers.
"""
def __init__(
self, dim: int, heads: int, depth: int, ff_mult: int = 2, *args, **kwargs
):
super().__init__()
self.layers = nn.ModuleList([])
self.ffn_layers = nn.ModuleList([])
for _ in range(depth):
self.layers.append(BitMGQA(dim, heads, *args, **kwargs))
self.ffn_layers.append(
BitFeedForward(
dim,
dim,
ff_mult,
swish=True,
post_act_ln=True,
dropout=0.1,
),
)
# Norm
self.norm = nn.LayerNorm(dim)
def forward(self, x: Tensor, *args, **kwargs) -> Tensor:
skip = x
for attn, ffn in zip(self.layers, self.ffn_layers):
x, _ = attn(x, x, x, is_causal=True, *args, **kwargs)
x = self.norm(x + skip)
x = ffn(x) + x
return x
# [MAIN MODEL] BitNetTransformer
class BitNetTransformer(nn.Module):
"""
BitNetTransformer is a transformer-based model for BitNet.
Args:
dim (int): The dimension of the token embeddings.
depth (int): The number of transformer layers.
num_tokens (int): The number of tokens in the vocabulary.
heads (int, optional): The number of attention heads in the transformer. Defaults to 8.
ff_mult (int, optional): The multiplier for the feed-forward layer dimension. Defaults to 4.
Examples:
>>> import torch
>>> from bitnet import BitNetTransformer
>>> x = torch.randint(0, 20000, (1, 1024))
>>> bitnet = BitNetTransformer(
... num_tokens=20000,
... dim=1024,
... depth=6,
... heads=8,
... ff_mult=4,
... )
>>> logits = bitnet(x)
>>> print(logits)
"""
def __init__(
self,
dim: int,
depth: int,
num_tokens: int,
heads: int = 8,
ff_mult: int = 4,
):
super().__init__()
self.emb = nn.Embedding(num_tokens, dim)
self.transformer = Transformer(
dim=dim, depth=depth, heads=heads, ff_mult=ff_mult
)
# self.to_logits = nn.Sequential(RMSNorm(dim), nn.Linear(dim, num_tokens))
self.to_logits = OutputHead(
dim,
vocab_size=num_tokens,
)
# Norm
self.norm = nn.LayerNorm(dim)
def forward(self, x):
x = self.emb(x)
# Post emb norm
x = self.norm(x)
x = self.transformer(x)
return self.to_logits(x)

Xet Storage Details

Size:
4.25 kB
·
Xet hash:
a1bd75b0d30fc2e60dce78a395936030d0461974cc7d58f7fa91a516165e2511

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