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 a
| 90 | |
| 91 | # [MAIN MODEL] BitNetTransformer |
| 92 | class BitNetTransformer(nn.Module): |
| 93 | """ |
| 94 | BitNetTransformer is a transformer-based model for BitNet. |
| 95 | |
| 96 | Args: |
| 97 | dim (int): The dimension of the token embeddings. |
| 98 | depth (int): The number of transformer layers. |
| 99 | num_tokens (int): The number of tokens in the vocabulary. |
| 100 | heads (int, optional): The number of attention heads in the transformer. Defaults to 8. |
| 101 | ff_mult (int, optional): The multiplier for the feed-forward layer dimension. Defaults to 4. |
| 102 | |
| 103 | Examples: |
| 104 | >>> import torch |
| 105 | >>> from bitnet import BitNetTransformer |
| 106 | >>> x = torch.randint(0, 20000, (1, 1024)) |
| 107 | >>> bitnet = BitNetTransformer( |
| 108 | ... num_tokens=20000, |
| 109 | ... dim=1024, |
| 110 | ... depth=6, |
| 111 | ... heads=8, |
| 112 | ... ff_mult=4, |
| 113 | ... ) |
| 114 | >>> logits = bitnet(x) |
| 115 | >>> print(logits) |
| 116 | """ |
| 117 | |
| 118 | def __init__( |
| 119 | self, |
| 120 | dim: int, |
| 121 | depth: int, |
| 122 | num_tokens: int, |
| 123 | heads: int = 8, |
| 124 | ff_mult: int = 4, |
| 125 | ): |
| 126 | super().__init__() |
| 127 | self.emb = nn.Embedding(num_tokens, dim) |
| 128 | |
| 129 | self.transformer = Transformer( |
| 130 | dim=dim, depth=depth, heads=heads, ff_mult=ff_mult |
| 131 | ) |
| 132 | |
| 133 | # self.to_logits = nn.Sequential(RMSNorm(dim), nn.Linear(dim, num_tokens)) |
| 134 | self.to_logits = OutputHead( |
| 135 | dim, |
| 136 | vocab_size=num_tokens, |
| 137 | ) |
| 138 | |
| 139 | # Norm |
| 140 | self.norm = nn.LayerNorm(dim) |
| 141 | |
| 142 | def forward(self, x): |
| 143 | x = self.emb(x) |
| 144 | # Post emb norm |
| 145 | x = self.norm(x) |
| 146 | |
| 147 | x = self.transformer(x) |
| 148 | return self.to_logits(x) |
no outgoing calls