rasbt commited on
Commit
b0bdc19
·
verified ·
1 Parent(s): 11fb34b

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. .DS_Store +0 -0
  2. .ipynb_checkpoints/model-checkpoint.py +334 -0
  3. README.md +2 -5
  4. model.py +7 -26
  5. tokenizer.py +70 -43
.DS_Store ADDED
Binary file (6.15 kB). View file
 
.ipynb_checkpoints/model-checkpoint.py ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
2
+ # Source for "Build a Large Language Model From Scratch"
3
+ # https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/07_gpt_to_llama/standalone-llama32.ipynb
4
+
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+
9
+
10
+ LLAMA32_CONFIG_1B = {
11
+ "vocab_size": 128_256, # Vocabulary size
12
+ "context_length": 8192, # Maximum context length to use (reduced to save memory)
13
+ "orig_context_length": 131_072, # Context length that was used to train the model
14
+ "emb_dim": 2048, # Embedding dimension
15
+ "n_heads": 32, # Number of attention heads
16
+ "n_layers": 16, # Number of layers
17
+ "hidden_dim": 8192, # Size of the intermediate dimension in FeedForward
18
+ "n_kv_groups": 8, # Key-Value groups for grouped-query attention
19
+ "rope_base": 500_000.0, # The base in RoPE's "theta"
20
+ "dtype": torch.bfloat16, # Lower-precision dtype to reduce memory usage
21
+ "rope_freq": { # RoPE frequency scaling
22
+ "factor": 32.0,
23
+ "low_freq_factor": 1.0,
24
+ "high_freq_factor": 4.0,
25
+ "original_context_length": 8192,
26
+ }
27
+ }
28
+
29
+ LLAMA32_CONFIG_3B = {
30
+ "vocab_size": 128_256, # Vocabulary size
31
+ "context_length": 8192, # Maximum context length to use (reduced to save memory)
32
+ "orig_context_length": 131_072, # Context length that was used to train the model
33
+ "emb_dim": 3072, # Embedding dimension
34
+ "n_heads": 24, # Number of attention heads
35
+ "n_layers": 28, # Number of layers
36
+ "hidden_dim": 8192, # Size of the intermediate dimension in FeedForward
37
+ "n_kv_groups": 8, # Key-Value groups for grouped-query attention
38
+ "rope_base": 500_000.0, # The base in RoPE's "theta"
39
+ "dtype": torch.bfloat16, # Lower-precision dtype to reduce memory usage
40
+ "rope_freq": { # RoPE frequency scaling
41
+ "factor": 32.0,
42
+ "low_freq_factor": 1.0,
43
+ "high_freq_factor": 4.0,
44
+ "original_context_length": 8192,
45
+ }
46
+ }
47
+
48
+
49
+ class Llama3Model(nn.Module):
50
+ def __init__(self, cfg):
51
+ super().__init__()
52
+
53
+ # Main model parameters
54
+ self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"], dtype=cfg["dtype"])
55
+
56
+ self.trf_blocks = nn.ModuleList( # ModuleList since Sequential can only accept one input, and we need `x, mask, cos, sin`
57
+ [TransformerBlock(cfg) for _ in range(cfg["n_layers"])]
58
+ )
59
+
60
+ self.final_norm = nn.RMSNorm(cfg["emb_dim"], eps=1e-5, dtype=cfg["dtype"])
61
+ self.out_head = nn.Linear(cfg["emb_dim"], cfg["vocab_size"], bias=False, dtype=cfg["dtype"])
62
+
63
+ # Reusuable utilities
64
+ self.register_buffer(
65
+ "mask", torch.triu(torch.ones(cfg["context_length"], cfg["context_length"]), diagonal=1).bool(),
66
+ persistent=False
67
+ )
68
+
69
+ if cfg["orig_context_length"] != cfg["context_length"]:
70
+ cfg["rope_base"] = rescale_theta(
71
+ cfg["rope_base"],
72
+ cfg["orig_context_length"],
73
+ cfg["context_length"]
74
+ )
75
+ cos, sin = compute_rope_params(
76
+ head_dim=cfg["emb_dim"] // cfg["n_heads"],
77
+ theta_base=cfg["rope_base"],
78
+ context_length=cfg["context_length"],
79
+ freq_config=cfg["rope_freq"]
80
+ )
81
+ self.register_buffer("cos", cos, persistent=False)
82
+ self.register_buffer("sin", sin, persistent=False)
83
+ self.cfg = cfg
84
+
85
+ def forward(self, in_idx):
86
+ # Forward pass
87
+ tok_embeds = self.tok_emb(in_idx)
88
+ x = tok_embeds
89
+
90
+ for block in self.trf_blocks:
91
+ x = block(x, self.mask, self.cos, self.sin)
92
+ x = self.final_norm(x)
93
+ logits = self.out_head(x.to(self.cfg["dtype"]))
94
+ return logits
95
+
96
+
97
+ class TransformerBlock(nn.Module):
98
+ def __init__(self, cfg):
99
+ super().__init__()
100
+ self.att = GroupedQueryAttention(
101
+ d_in=cfg["emb_dim"],
102
+ d_out=cfg["emb_dim"],
103
+ num_heads=cfg["n_heads"],
104
+ num_kv_groups=cfg["n_kv_groups"],
105
+ dtype=cfg["dtype"]
106
+ )
107
+ self.ff = FeedForward(cfg)
108
+ self.norm1 = nn.RMSNorm(cfg["emb_dim"], eps=1e-5, dtype=cfg["dtype"])
109
+ self.norm2 = nn.RMSNorm(cfg["emb_dim"], eps=1e-5, dtype=cfg["dtype"])
110
+
111
+ def forward(self, x, mask, cos, sin):
112
+ # Shortcut connection for attention block
113
+ shortcut = x
114
+ x = self.norm1(x)
115
+ x = self.att(x, mask, cos, sin) # Shape [batch_size, num_tokens, emb_size]
116
+ x = x + shortcut # Add the original input back
117
+
118
+ # Shortcut connection for feed-forward block
119
+ shortcut = x
120
+ x = self.norm2(x)
121
+ x = self.ff(x)
122
+ x = x + shortcut # Add the original input back
123
+
124
+ return x
125
+
126
+
127
+ class FeedForward(nn.Module):
128
+ def __init__(self, cfg):
129
+ super().__init__()
130
+ self.fc1 = nn.Linear(cfg["emb_dim"], cfg["hidden_dim"], dtype=cfg["dtype"], bias=False)
131
+ self.fc2 = nn.Linear(cfg["emb_dim"], cfg["hidden_dim"], dtype=cfg["dtype"], bias=False)
132
+ self.fc3 = nn.Linear(cfg["hidden_dim"], cfg["emb_dim"], dtype=cfg["dtype"], bias=False)
133
+
134
+ def forward(self, x):
135
+ x_fc1 = self.fc1(x)
136
+ x_fc2 = self.fc2(x)
137
+ x = nn.functional.silu(x_fc1) * x_fc2
138
+ return self.fc3(x)
139
+
140
+
141
+ class GroupedQueryAttention(nn.Module):
142
+ def __init__(
143
+ self, d_in, d_out, num_heads,
144
+ num_kv_groups,
145
+ dtype=None
146
+ ):
147
+ super().__init__()
148
+ assert d_out % num_heads == 0, "d_out must be divisible by num_heads"
149
+ assert num_heads % num_kv_groups == 0, "num_heads must be divisible by num_kv_groups"
150
+
151
+ self.d_out = d_out
152
+ self.num_heads = num_heads
153
+ self.head_dim = d_out // num_heads
154
+
155
+ self.W_key = nn.Linear(d_in, num_kv_groups * self.head_dim, bias=False, dtype=dtype)
156
+ self.W_value = nn.Linear(d_in, num_kv_groups * self.head_dim, bias=False, dtype=dtype)
157
+ self.num_kv_groups = num_kv_groups
158
+ self.group_size = num_heads // num_kv_groups
159
+
160
+ self.W_query = nn.Linear(d_in, d_out, bias=False, dtype=dtype)
161
+ self.out_proj = nn.Linear(d_out, d_out, bias=False, dtype=dtype)
162
+
163
+ def forward(self, x, mask, cos, sin):
164
+ b, num_tokens, d_in = x.shape
165
+
166
+ queries = self.W_query(x) # Shape: (b, num_tokens, d_out)
167
+ keys = self.W_key(x) # Shape: (b, num_tokens, num_kv_groups * head_dim)
168
+ values = self.W_value(x) # Shape: (b, num_tokens, num_kv_groups * head_dim)
169
+
170
+ # Reshape queries, keys, and values
171
+ queries = queries.view(b, num_tokens, self.num_heads, self.head_dim)
172
+ keys = keys.view(b, num_tokens, self.num_kv_groups, self.head_dim)
173
+ values = values.view(b, num_tokens, self.num_kv_groups, self.head_dim)
174
+
175
+ # Transpose keys, values, and queries
176
+ keys = keys.transpose(1, 2) # Shape: (b, num_heads, num_tokens, head_dim)
177
+ values = values.transpose(1, 2) # Shape: (b, num_heads, num_tokens, head_dim)
178
+ queries = queries.transpose(1, 2) # Shape: (b, num_query_groups, num_tokens, head_dim)
179
+
180
+ # Apply RoPE
181
+ keys = apply_rope(keys, cos, sin)
182
+ queries = apply_rope(queries, cos, sin)
183
+
184
+ # Expand keys and values to match the number of heads
185
+ # Shape: (b, num_heads, num_tokens, head_dim)
186
+ keys = keys.repeat_interleave(self.group_size, dim=1) # Shape: (b, num_heads, num_tokens, head_dim)
187
+ values = values.repeat_interleave(self.group_size, dim=1) # Shape: (b, num_heads, num_tokens, head_dim)
188
+ # For example, before repeat_interleave along dim=1 (query groups):
189
+ # [K1, K2]
190
+ # After repeat_interleave (each query group is repeated group_size times):
191
+ # [K1, K1, K2, K2]
192
+ # If we used regular repeat instead of repeat_interleave, we'd get:
193
+ # [K1, K2, K1, K2]
194
+
195
+ # Compute scaled dot-product attention (aka self-attention) with a causal mask
196
+ # Shape: (b, num_heads, num_tokens, num_tokens)
197
+ attn_scores = queries @ keys.transpose(2, 3) # Dot product for each head
198
+
199
+ # Use the mask to fill attention scores
200
+ attn_scores = attn_scores.masked_fill(mask[:num_tokens, :num_tokens], -torch.inf)
201
+
202
+ attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
203
+ assert keys.shape[-1] == self.head_dim
204
+
205
+ # Shape: (b, num_tokens, num_heads, head_dim)
206
+ context_vec = (attn_weights @ values).transpose(1, 2)
207
+
208
+ # Combine heads, where self.d_out = self.num_heads * self.head_dim
209
+ context_vec = context_vec.reshape(b, num_tokens, self.d_out)
210
+ context_vec = self.out_proj(context_vec) # optional projection
211
+
212
+ return context_vec
213
+
214
+
215
+ def compute_rope_params(head_dim, theta_base=10_000, context_length=4096, freq_config=None, dtype=torch.float32):
216
+ assert head_dim % 2 == 0, "Embedding dimension must be even"
217
+
218
+ # Compute the inverse frequencies
219
+ inv_freq = 1.0 / (theta_base ** (torch.arange(0, head_dim, 2, dtype=dtype)[: (head_dim // 2)].float() / head_dim))
220
+
221
+ # Frequency adjustments
222
+ if freq_config is not None:
223
+ low_freq_wavelen = freq_config["original_context_length"] / freq_config["low_freq_factor"]
224
+ high_freq_wavelen = freq_config["original_context_length"] / freq_config["high_freq_factor"]
225
+
226
+ wavelen = 2 * torch.pi / inv_freq
227
+
228
+ inv_freq_llama = torch.where(
229
+ wavelen > low_freq_wavelen, inv_freq / freq_config["factor"], inv_freq
230
+ )
231
+
232
+ smooth_factor = (freq_config["original_context_length"] / wavelen - freq_config["low_freq_factor"]) / (
233
+ freq_config["high_freq_factor"] - freq_config["low_freq_factor"]
234
+ )
235
+
236
+ smoothed_inv_freq = (
237
+ (1 - smooth_factor) * (inv_freq / freq_config["factor"]) + smooth_factor * inv_freq
238
+ )
239
+
240
+ is_medium_freq = (wavelen <= low_freq_wavelen) & (wavelen >= high_freq_wavelen)
241
+ inv_freq_llama = torch.where(is_medium_freq, smoothed_inv_freq, inv_freq_llama)
242
+ inv_freq = inv_freq_llama
243
+
244
+ # Generate position indices
245
+ positions = torch.arange(context_length, dtype=dtype)
246
+
247
+ # Compute the angles
248
+ angles = positions[:, None] * inv_freq[None, :] # Shape: (context_length, head_dim // 2)
249
+
250
+ # Expand angles to match the head_dim
251
+ angles = torch.cat([angles, angles], dim=1) # Shape: (context_length, head_dim)
252
+
253
+ # Precompute sine and cosine
254
+ cos = torch.cos(angles)
255
+ sin = torch.sin(angles)
256
+
257
+ return cos, sin
258
+
259
+
260
+ def apply_rope(x, cos, sin):
261
+ # x: (batch_size, num_heads, seq_len, head_dim)
262
+ batch_size, num_heads, seq_len, head_dim = x.shape
263
+ assert head_dim % 2 == 0, "Head dimension must be even"
264
+
265
+ # Split x into first half and second half
266
+ x1 = x[..., : head_dim // 2] # First half
267
+ x2 = x[..., head_dim // 2:] # Second half
268
+
269
+ # Adjust sin and cos shapes
270
+ cos = cos[:seq_len, :].unsqueeze(0).unsqueeze(0) # Shape: (1, 1, seq_len, head_dim)
271
+ sin = sin[:seq_len, :].unsqueeze(0).unsqueeze(0)
272
+
273
+ # Apply the rotary transformation
274
+ rotated = torch.cat((-x2, x1), dim=-1)
275
+ x_rotated = (x * cos) + (rotated * sin)
276
+
277
+ # It's ok to use lower-precision after applying cos and sin rotation
278
+ return x_rotated.to(dtype=x.dtype)
279
+
280
+
281
+ def rescale_theta(theta_old, context_length_old, context_length_new):
282
+ scaling_factor = context_length_new / context_length_old
283
+ theta_new = theta_old * scaling_factor
284
+ return theta_new
285
+
286
+
287
+ def text_to_token_ids(text, tokenizer):
288
+ encoded = tokenizer.encode(text)
289
+ encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension
290
+ return encoded_tensor
291
+
292
+
293
+ def token_ids_to_text(token_ids, tokenizer):
294
+ flat = token_ids.squeeze(0) # remove batch dimension
295
+ return tokenizer.decode(flat.tolist())
296
+
297
+
298
+ def generate(model, idx, max_new_tokens, context_size, temperature=0.0, top_k=None, eos_id=None):
299
+
300
+ # For-loop is the same as before: Get logits, and only focus on last time step
301
+ for _ in range(max_new_tokens):
302
+ idx_cond = idx[:, -context_size:]
303
+ with torch.no_grad():
304
+ logits = model(idx_cond)
305
+ logits = logits[:, -1, :]
306
+
307
+ # Filter logits with top_k sampling
308
+ if top_k is not None:
309
+ # Keep only top_k values
310
+ top_logits, _ = torch.topk(logits, top_k)
311
+ min_val = top_logits[:, -1]
312
+ logits = torch.where(logits < min_val, torch.tensor(float('-inf')).to(logits.device), logits)
313
+
314
+ # Apply temperature scaling
315
+ if temperature > 0.0:
316
+ logits = logits / temperature
317
+
318
+ # Apply softmax to get probabilities
319
+ probs = torch.softmax(logits, dim=-1) # (batch_size, context_len)
320
+
321
+ # Sample from the distribution
322
+ idx_next = torch.multinomial(probs, num_samples=1) # (batch_size, 1)
323
+
324
+ # Otherwise same as before: get idx of the vocab entry with the highest logits value
325
+ else:
326
+ idx_next = torch.argmax(logits, dim=-1, keepdim=True) # (batch_size, 1)
327
+
328
+ if idx_next == eos_id: # Stop generating early if end-of-sequence token is encountered and eos_id is specified
329
+ break
330
+
331
+ # Same as before: append sampled index to the running sequence
332
+ idx = torch.cat((idx, idx_next), dim=1) # (batch_size, num_tokens+1)
333
+
334
+ return idx
README.md CHANGED
@@ -87,12 +87,9 @@ MODEL_FILE = "llama3.2-1B-instruct.pth"
87
  # MODEL_FILE = "llama3.2-3B-base.pth"
88
  ```
89
 
90
- Basic text generation settings that can be defined by the user. Note that the recommended 8192-token context size requires approximately 3 GB of VRAM for the text generation example.
91
 
92
  ```
93
- MODEL_CONTEXT_LENGTH = 8192 # Supports up to 131_072
94
-
95
- # Text generation settings
96
  if "instruct" in MODEL_FILE:
97
  PROMPT = "What do llamas eat?"
98
  else:
@@ -139,7 +136,7 @@ else:
139
  LLAMA32_CONFIG["context_length"] = MODEL_CONTEXT_LENGTH
140
 
141
  model = Llama3Model(LLAMA32_CONFIG)
142
- model.load_state_dict(torch.load(MODEL_FILE, weights_only=True, map_location="cpu"))
143
 
144
  device = (
145
  torch.device("cuda") if torch.cuda.is_available() else
 
87
  # MODEL_FILE = "llama3.2-3B-base.pth"
88
  ```
89
 
90
+ Basic text generation settings that can be defined by the user.
91
 
92
  ```
 
 
 
93
  if "instruct" in MODEL_FILE:
94
  PROMPT = "What do llamas eat?"
95
  else:
 
136
  LLAMA32_CONFIG["context_length"] = MODEL_CONTEXT_LENGTH
137
 
138
  model = Llama3Model(LLAMA32_CONFIG)
139
+ model.load_state_dict(torch.load(MODEL_FILE, weights_only=True))
140
 
141
  device = (
142
  torch.device("cuda") if torch.cuda.is_available() else
model.py CHANGED
@@ -9,8 +9,7 @@ import torch.nn as nn
9
 
10
  LLAMA32_CONFIG_1B = {
11
  "vocab_size": 128_256, # Vocabulary size
12
- "context_length": 8192, # Maximum context length to use (reduced to save memory)
13
- "orig_context_length": 131_072, # Context length that was used to train the model
14
  "emb_dim": 2048, # Embedding dimension
15
  "n_heads": 32, # Number of attention heads
16
  "n_layers": 16, # Number of layers
@@ -28,8 +27,7 @@ LLAMA32_CONFIG_1B = {
28
 
29
  LLAMA32_CONFIG_3B = {
30
  "vocab_size": 128_256, # Vocabulary size
31
- "context_length": 8192, # Maximum context length to use (reduced to save memory)
32
- "orig_context_length": 131_072, # Context length that was used to train the model
33
  "emb_dim": 3072, # Embedding dimension
34
  "n_heads": 24, # Number of attention heads
35
  "n_layers": 28, # Number of layers
@@ -61,17 +59,6 @@ class Llama3Model(nn.Module):
61
  self.out_head = nn.Linear(cfg["emb_dim"], cfg["vocab_size"], bias=False, dtype=cfg["dtype"])
62
 
63
  # Reusuable utilities
64
- self.register_buffer(
65
- "mask", torch.triu(torch.ones(cfg["context_length"], cfg["context_length"]), diagonal=1).bool(),
66
- persistent=False
67
- )
68
-
69
- if cfg["orig_context_length"] != cfg["context_length"]:
70
- cfg["rope_base"] = rescale_theta(
71
- cfg["rope_base"],
72
- cfg["orig_context_length"],
73
- cfg["context_length"]
74
- )
75
  cos, sin = compute_rope_params(
76
  head_dim=cfg["emb_dim"] // cfg["n_heads"],
77
  theta_base=cfg["rope_base"],
@@ -83,12 +70,14 @@ class Llama3Model(nn.Module):
83
  self.cfg = cfg
84
 
85
  def forward(self, in_idx):
86
- # Forward pass
87
  tok_embeds = self.tok_emb(in_idx)
88
  x = tok_embeds
89
 
 
 
 
90
  for block in self.trf_blocks:
91
- x = block(x, self.mask, self.cos, self.sin)
92
  x = self.final_norm(x)
93
  logits = self.out_head(x.to(self.cfg["dtype"]))
94
  return logits
@@ -140,9 +129,7 @@ class FeedForward(nn.Module):
140
 
141
  class GroupedQueryAttention(nn.Module):
142
  def __init__(
143
- self, d_in, d_out, num_heads,
144
- num_kv_groups,
145
- dtype=None
146
  ):
147
  super().__init__()
148
  assert d_out % num_heads == 0, "d_out must be divisible by num_heads"
@@ -278,12 +265,6 @@ def apply_rope(x, cos, sin):
278
  return x_rotated.to(dtype=x.dtype)
279
 
280
 
281
- def rescale_theta(theta_old, context_length_old, context_length_new):
282
- scaling_factor = context_length_new / context_length_old
283
- theta_new = theta_old * scaling_factor
284
- return theta_new
285
-
286
-
287
  def text_to_token_ids(text, tokenizer):
288
  encoded = tokenizer.encode(text)
289
  encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension
 
9
 
10
  LLAMA32_CONFIG_1B = {
11
  "vocab_size": 128_256, # Vocabulary size
12
+ "context_length": 131_072, # Context length that was used to train the model
 
13
  "emb_dim": 2048, # Embedding dimension
14
  "n_heads": 32, # Number of attention heads
15
  "n_layers": 16, # Number of layers
 
27
 
28
  LLAMA32_CONFIG_3B = {
29
  "vocab_size": 128_256, # Vocabulary size
30
+ "context_length": 131_072, # Context length that was used to train the model
 
31
  "emb_dim": 3072, # Embedding dimension
32
  "n_heads": 24, # Number of attention heads
33
  "n_layers": 28, # Number of layers
 
59
  self.out_head = nn.Linear(cfg["emb_dim"], cfg["vocab_size"], bias=False, dtype=cfg["dtype"])
60
 
61
  # Reusuable utilities
 
 
 
 
 
 
 
 
 
 
 
62
  cos, sin = compute_rope_params(
63
  head_dim=cfg["emb_dim"] // cfg["n_heads"],
64
  theta_base=cfg["rope_base"],
 
70
  self.cfg = cfg
71
 
72
  def forward(self, in_idx):
 
73
  tok_embeds = self.tok_emb(in_idx)
74
  x = tok_embeds
75
 
76
+ num_tokens = x.shape[1]
77
+ mask = torch.triu(torch.ones(num_tokens, num_tokens, device=x.device, dtype=torch.bool), diagonal=1)
78
+
79
  for block in self.trf_blocks:
80
+ x = block(x, mask, self.cos, self.sin)
81
  x = self.final_norm(x)
82
  logits = self.out_head(x.to(self.cfg["dtype"]))
83
  return logits
 
129
 
130
  class GroupedQueryAttention(nn.Module):
131
  def __init__(
132
+ self, d_in, d_out, num_heads, num_kv_groups, dtype=None
 
 
133
  ):
134
  super().__init__()
135
  assert d_out % num_heads == 0, "d_out must be divisible by num_heads"
 
265
  return x_rotated.to(dtype=x.dtype)
266
 
267
 
 
 
 
 
 
 
268
  def text_to_token_ids(text, tokenizer):
269
  encoded = tokenizer.encode(text)
270
  encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension
tokenizer.py CHANGED
@@ -11,71 +11,98 @@ from tiktoken.load import load_tiktoken_bpe
11
 
12
 
13
  class Llama3Tokenizer:
 
14
  def __init__(self, model_path):
15
- assert os.path.isfile(model_path), f"Model file {model_path} not found"
16
- mergeable_ranks = load_tiktoken_bpe(model_path)
17
 
18
- self.special_tokens = {
 
 
 
19
  "<|begin_of_text|>": 128000,
20
  "<|end_of_text|>": 128001,
21
  "<|start_header_id|>": 128006,
22
  "<|end_header_id|>": 128007,
23
  "<|eot_id|>": 128009,
24
  }
25
- self.special_tokens.update({
26
- f"<|reserved_{i}|>": 128002 + i for i in range(256) if (128002 + i) not in self.special_tokens.values()
27
- })
28
 
29
  self.model = tiktoken.Encoding(
30
  name=Path(model_path).name,
31
- pat_str=r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+",
32
- mergeable_ranks=mergeable_ranks,
33
- special_tokens=self.special_tokens
 
 
 
 
 
 
34
  )
35
 
36
- def encode(self, text, bos=False, eos=False, allowed_special=set(), disallowed_special=()):
37
- if bos:
38
- tokens = [self.special_tokens["<|begin_of_text|>"]]
39
- else:
40
- tokens = []
41
-
42
- tokens += self.model.encode(text, allowed_special=allowed_special, disallowed_special=disallowed_special)
43
 
 
 
 
 
 
 
 
 
 
 
44
  if eos:
45
- tokens.append(self.special_tokens["<|end_of_text|>"])
46
- return tokens
 
47
 
48
- def decode(self, tokens):
49
- return self.model.decode(tokens)
50
 
51
 
52
  class ChatFormat:
53
- def __init__(self, tokenizer):
54
- self.tokenizer = tokenizer
55
-
56
- def encode_header(self, message):
57
- tokens = []
58
- tokens.append(self.tokenizer.special_tokens["<|start_header_id|>"])
59
- tokens.extend(self.tokenizer.encode(message["role"], bos=False, eos=False))
60
- tokens.append(self.tokenizer.special_tokens["<|end_header_id|>"])
61
- tokens.extend(self.tokenizer.encode("\n\n", bos=False, eos=False))
62
- return tokens
63
-
64
- def encode(self, text):
65
- message = {
66
- "role": "user",
67
- "content": text
68
- }
69
 
70
- tokens = self.encode_header(message)
71
- tokens.extend(
72
- self.tokenizer.encode(message["content"].strip(), bos=False, eos=False)
 
 
 
 
 
 
 
 
 
73
  )
74
- tokens.append(self.tokenizer.special_tokens["<|eot_id|>"])
75
- return tokens
76
 
77
- def decode(self, token_ids):
78
- return self.tokenizer.decode(token_ids)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
 
81
  def clean_text(text, header_end="assistant<|end_header_id|>\n\n"):
 
11
 
12
 
13
  class Llama3Tokenizer:
14
+ """Thin wrapper around tiktoken that keeps track of Llama-3 special IDs."""
15
  def __init__(self, model_path):
16
+ if not os.path.isfile(model_path):
17
+ raise FileNotFoundError(model_path)
18
 
19
+ mergeable = load_tiktoken_bpe(model_path)
20
+
21
+ # hard-coded from Meta's tokenizer.json
22
+ self.special = {
23
  "<|begin_of_text|>": 128000,
24
  "<|end_of_text|>": 128001,
25
  "<|start_header_id|>": 128006,
26
  "<|end_header_id|>": 128007,
27
  "<|eot_id|>": 128009,
28
  }
29
+ self.special.update({f"<|reserved_{i}|>": 128002 + i
30
+ for i in range(256)
31
+ if 128002 + i not in self.special.values()})
32
 
33
  self.model = tiktoken.Encoding(
34
  name=Path(model_path).name,
35
+ pat_str=r"(?i:'s|'t|'re|'ve|'m|'ll|'d)"
36
+ r"|[^\r\n\p{L}\p{N}]?\p{L}+"
37
+ r"|\p{N}{1,3}"
38
+ r"| ?[^\s\p{L}\p{N}]+[\r\n]*"
39
+ r"|\s*[\r\n]+"
40
+ r"|\s+(?!\S)"
41
+ r"|\s+",
42
+ mergeable_ranks=mergeable,
43
+ special_tokens=self.special,
44
  )
45
 
46
+ def encode(self, text, bos=False, eos=False, allowed_special=set()):
47
+ ids: list[int] = []
 
 
 
 
 
48
 
49
+ if bos:
50
+ ids.append(self.special_tokens["<|begin_of_text|>"])
51
+
52
+ # delegate to underlying tiktoken.Encoding.encode
53
+ ids.extend(
54
+ self.model.encode(
55
+ text,
56
+ allowed_special=allowed_special,
57
+ )
58
+ )
59
  if eos:
60
+ ids.append(self.special_tokens["<|end_of_text|>"])
61
+
62
+ return ids
63
 
64
+ def decode(self, ids):
65
+ return self.model.decode(ids)
66
 
67
 
68
  class ChatFormat:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ def __init__(self, tokenizer: Llama3Tokenizer, *,
71
+ default_system="You are a helpful assistant."):
72
+ self.tok = tokenizer
73
+ self.default_system = default_system
74
+
75
+ def _header(self, role):
76
+ """Encode <|start_header_id|>role<|end_header_id|>\n\n"""
77
+ return (
78
+ [self.tok.special["<|start_header_id|>"]]
79
+ + self.tok.encode(role)
80
+ + [self.tok.special["<|end_header_id|>"]]
81
+ + self.tok.encode("\n\n")
82
  )
 
 
83
 
84
+ def encode(self, user_message, system_message=None, allowed_special=None):
85
+ sys_msg = system_message if system_message is not None else self.default_system
86
+
87
+ ids = [self.tok.special["<|begin_of_text|>"]]
88
+
89
+ # system
90
+ ids += self._header("system")
91
+ ids += self.tok.encode(sys_msg, allowed_special=allowed_special)
92
+ ids += [self.tok.special["<|eot_id|>"]]
93
+
94
+ # user
95
+ ids += self._header("user")
96
+ ids += self.tok.encode(user_message)
97
+ ids += [self.tok.special["<|eot_id|>"]]
98
+
99
+ # assistant header (no content yet)
100
+ ids += self._header("assistant")
101
+
102
+ return ids
103
+
104
+ def decode(self, ids):
105
+ return self.tok.decode(ids)
106
 
107
 
108
  def clean_text(text, header_end="assistant<|end_header_id|>\n\n"):