Repository navigation
Expand file tree
/
Copy pathllama.mapal
More file actions
412 lines (379 loc) · 16.7 KB
/
Copy pathllama.mapal
File metadata and controls
412 lines (379 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// llama.mapal — Llama-3.2-1B-Instruct: read the weights, run the prompt, generate greedily.
//
// The whole model is the functions below; the weights are data, read from one file (`model.bin`, written
// by `benches/model/export_weights.py` from the Hugging Face checkpoint) and the prompt from another.
// Run with `MAPAL_DATA_DIR` set to the directory holding them:
//
// MAPAL_DATA_DIR=~/.cache/mapal/models/llama-3.2-1b-instruct/mapal mapal run examples/llama.mapal
//
// It prints one line per generated token: the token id, its logit, and the milliseconds since the prompt
// was submitted (when the token is on the host). Every activation is f32; the weights stay bf16; the key/value
// cache is stored in the format `main` chooses (f32 here; `to<bf16>` keeps it in bf16, as the competitors do).
//
// One function serves the prompt and every generated token: `forward` runs a chunk of `tokens` new tokens
// (the whole prompt at once, then one token at a time) through every layer, appending their keys and values
// to the cache, and returns the logits after the chunk's last token.
// ---------------------------------------------------------------------------------------------------------
// The model's sizes (the checkpoint's config.json).
128256 -> vocabulary_size;
2048 -> hidden_size;
16 -> layer_count;
32 -> query_heads;
8 -> key_value_heads;
64 -> head_size;
8192 -> feed_forward_size;
0.00001 -> rms_epsilon;
// Rotary position embedding: theta 500000 with llama3 frequency scaling.
13.122363377404328 -> log_rope_theta; // ln(500000)
32.0 -> rope_factor;
1.0 -> rope_low_frequency_factor;
4.0 -> rope_high_frequency_factor;
8192.0 -> rope_original_context;
// The run: the prompt's length (its first ids from `prompt.bin`) and how many tokens to generate.
512 -> prompt_length;
128 -> generated_tokens;
2048 -> prompt_file_length;
// Derived sizes.
query_heads * head_size -> query_size;
key_value_heads * head_size -> key_value_size;
query_heads / key_value_heads -> heads_per_key_value_head;
head_size / 2 -> rotation_pairs;
prompt_length + generated_tokens -> context_length;
layer_count * context_length * key_value_size -> cache_size;
// ---------------------------------------------------------------------------------------------------------
// Types.
// A linear layer's weights as the checkpoint stores them: `rows` outputs of `columns` inputs, row by row.
type Matrix<rows, columns> {
_: [bf16; rows * columns]
}
type DecoderLayerWeights {
attention_norm: [bf16; hidden_size],
query: Matrix<query_size, hidden_size>,
key: Matrix<key_value_size, hidden_size>,
value: Matrix<key_value_size, hidden_size>,
output: Matrix<hidden_size, query_size>,
feed_forward_norm: [bf16; hidden_size],
gate: Matrix<feed_forward_size, hidden_size>,
up: Matrix<feed_forward_size, hidden_size>,
down: Matrix<hidden_size, feed_forward_size>,
}
// The token embedding is also the output head (tied weights).
type Model {
embedding: Matrix<vocabulary_size, hidden_size>,
layers: [DecoderLayerWeights; layer_count],
final_norm: [bf16; hidden_size],
}
// Every layer's keys and values for every position of the context: cell
// `((layer * context_length + position) * key_value_heads + key_value_head) * head_size + dimension`,
// stored as `element`.
type KeyValueCache<element: float> {
keys: [element; cache_size],
values: [element; cache_size],
}
// ---------------------------------------------------------------------------------------------------------
// Rotary position embedding.
// The rotation frequency of pair `pair` of a head: theta^(-2 pair / head_size). Llama 3 stretches the slow
// ones: wavelengths longer than the original context / low factor are divided by the factor, those shorter
// than the original context / high factor are kept, and those between are blended smoothly.
fn inverse_frequency(pair: i32) -> f32 {
pair * 2 -> to<f32> -> twice_pair;
head_size -> widen<f32> -> width;
-(twice_pair / width) * log_rope_theta -> exp -> frequency;
6.2831855 / frequency -> wavelength;
(wavelength > rope_original_context / rope_low_frequency_factor) -> {
-true-> frequency / rope_factor;
-false-> {
(wavelength < rope_original_context / rope_high_frequency_factor) -> {
-true-> frequency;
-false-> {
rope_original_context / wavelength - rope_low_frequency_factor -> above_low;
above_low / (rope_high_frequency_factor - rope_low_frequency_factor) -> smooth;
(1.0 - smooth) * frequency / rope_factor + smooth * frequency
}
}
}
} -> ret;
}
// Computed before the program runs: it depends on nothing the run reads.
rotation_pairs -> iota -> map {
pair -> pair -> inverse_frequency
} -> inverse_frequencies: [f32; rotation_pairs];
// Rotate each head of each token by its position's angles: in a head, element i < half and its partner
// i + half are a pair (x, y) that becomes (x cos - y sin, y cos + x sin), at angle position * frequency.
fn rotate<tokens, width>(x: [f32; tokens * width], positions: [i32; tokens]) -> [f32; tokens * width] {
tokens * width -> iota -> map {
cell ->
cell / width -> t;
cell % head_size -> within_head;
within_head % rotation_pairs -> pair;
positions[t] -> to<f32> -> position;
position * inverse_frequencies[pair] -> angle;
angle -> cos -> cosine;
angle -> sin -> sine;
(within_head < rotation_pairs) -> {
-true-> x[cell] * cosine - x[cell + rotation_pairs] * sine;
-false-> x[cell] * cosine + x[cell - rotation_pairs] * sine;
}
} -> ret;
}
// ---------------------------------------------------------------------------------------------------------
// Building blocks. Activations are `tokens` vectors stored one after another.
fn embed<tokens>(embedding: Matrix<vocabulary_size, hidden_size>, ids: [i32; tokens]) -> [f32; tokens * hidden_size] {
tokens * hidden_size -> iota -> map {
cell ->
ids[cell / hidden_size] * hidden_size + cell % hidden_size -> row_cell;
embedding[row_cell] -> widen<f32>
} -> ret;
}
// RMSNorm of each vector: x / sqrt(mean(x^2) + epsilon), times a learned gain per feature.
fn rms_norm<tokens>(x: [f32; tokens * hidden_size], gain: [bf16; hidden_size]) -> [f32; tokens * hidden_size] {
hidden_size -> iota -> features;
hidden_size -> widen<f32> -> width;
tokens -> iota -> map {
t -> (0.0, features) -> fold {
sum, i -> sum + x[t * hidden_size + i] * x[t * hidden_size + i]
}
} -> sum_of_squares;
tokens * hidden_size -> iota -> map {
cell ->
sum_of_squares[cell / hidden_size] / width + rms_epsilon -> sqrt -> root;
gain[cell % hidden_size] -> widen<f32> -> g;
x[cell] * (1.0 / root) * g
} -> ret;
}
// A linear layer without bias: out[t][row] = sum over columns of weight[row][column] * x[t][column].
fn project<tokens, rows, columns>(weight: Matrix<rows, columns>, x: [f32; tokens * columns]) -> [f32; tokens * rows] {
columns -> iota -> along;
tokens * rows -> iota -> map {
cell ->
cell / rows -> t;
cell % rows -> row;
(0.0, along) -> fold {
sum, column ->
weight[row * columns + column] -> widen<f32> -> w;
sum + w * x[t * columns + column]
}
} -> ret;
}
fn add<n>(a: [f32; n], b: [f32; n]) -> [f32; n] {
n -> iota -> map {
i -> a[i] + b[i]
} -> ret;
}
fn larger(a: f32, b: f32) -> f32 {
(a > b) -> {
-true-> a;
-false-> b;
} -> ret;
}
// SiLU, the gate activation of SwiGLU: x * sigmoid(x).
fn silu(x: f32) -> f32 {
-x -> exp -> decay;
x / (1.0 + decay) -> ret;
}
// ---------------------------------------------------------------------------------------------------------
// Attention.
// The cache with the chunk's rows (keys or values, one row per token) written at positions
// first_position, first_position + 1, ... of layer `layer`, each new element rounded once to the cache's format.
fn write_rows<tokens, element: float>(cache: [element; cache_size], rows: [f32; tokens * key_value_size], layer: i32, first_position: i32) -> [element; cache_size] {
cache_size -> iota -> map {
cell ->
cell / key_value_size - layer * context_length - first_position -> t;
(t >= 0 && t < tokens) -> {
-true-> rows[t * key_value_size + cell % key_value_size] -> to<element>;
-false-> cache[cell];
}
} -> ret;
}
// Causal grouped-query attention over layer `layer`'s cached keys and values: each token's query head h reads
// key/value head h / 4 of every cached position up to its own, scores are scaled by 1 / sqrt(head_size),
// softmax over each head's scores, and the result is the weighted sum of the cached values.
fn attend<tokens, element: float>(
query: [f32; tokens * query_size],
positions: [i32; tokens],
cache: KeyValueCache<element>,
layer: i32,
) -> [f32; tokens * query_size] {
head_size -> iota -> dimensions;
context_length -> iota -> past_positions;
head_size -> widen<f32> -> sqrt -> scale_root;
tokens * query_heads * context_length -> iota -> map {
cell ->
cell / context_length -> token_head;
cell % context_length -> past;
token_head / query_heads -> t;
token_head % query_heads / heads_per_key_value_head -> key_value_head;
(past <= positions[t]) -> {
-true-> {
(layer * context_length + past) * key_value_heads + key_value_head -> key_row;
(0.0, dimensions) -> fold {
sum, d ->
query[token_head * head_size + d] -> q;
cache.keys[key_row * head_size + d] -> widen<f32> -> key;
sum + q * key
} -> dot;
dot / scale_root
}
-false-> -1000000000.0;
}
} -> scores;
tokens * query_heads -> iota -> token_heads;
token_heads -> map {
token_head -> (-1000000000.0, past_positions) -> fold {
best, past -> (best, scores[token_head * context_length + past]) -> larger
}
} -> row_maximum;
tokens * query_heads * context_length -> iota -> map {
cell -> scores[cell] - row_maximum[cell / context_length] -> exp
} -> exponentials;
token_heads -> map {
token_head -> (0.0, past_positions) -> fold {
sum, past -> sum + exponentials[token_head * context_length + past]
}
} -> row_total;
tokens * query_size -> iota -> map {
cell ->
cell / head_size -> token_head;
cell % head_size -> d;
token_head % query_heads / heads_per_key_value_head -> key_value_head;
(0.0, past_positions) -> fold {
sum, past ->
(layer * context_length + past) * key_value_heads + key_value_head -> value_row;
exponentials[token_head * context_length + past] -> weight;
cache.values[value_row * head_size + d] -> widen<f32> -> value;
sum + weight * value
} -> weighted;
weighted / row_total[token_head]
} -> ret;
}
// ---------------------------------------------------------------------------------------------------------
// The decoder layer and the whole model.
// SwiGLU feed-forward: down(silu(gate(x)) * up(x)).
fn feed_forward<tokens>(x: [f32; tokens * hidden_size], weights: DecoderLayerWeights) -> [f32; tokens * hidden_size] {
(weights.gate, x) -> project -> gate: [f32; tokens * feed_forward_size];
(weights.up, x) -> project -> up: [f32; tokens * feed_forward_size];
tokens * feed_forward_size -> iota -> map {
i ->
gate[i] -> silu -> activated;
activated * up[i]
} -> mixed;
(weights.down, mixed) -> project -> ret;
}
// One decoder layer over the chunk: attention with a residual connection, then the feed-forward block with
// a residual connection. The chunk's keys and values enter the cache before its queries attend.
fn decoder_layer<tokens, element: float>(
hidden: [f32; tokens * hidden_size],
weights: DecoderLayerWeights,
layer: i32,
cache: KeyValueCache<element>,
positions: [i32; tokens],
) -> ([f32; tokens * hidden_size], KeyValueCache<element>) {
(hidden, weights.attention_norm) -> rms_norm -> normed;
(weights.query, normed) -> project -> query: [f32; tokens * query_size];
(weights.key, normed) -> project -> key: [f32; tokens * key_value_size];
(weights.value, normed) -> project -> value: [f32; tokens * key_value_size];
(query, positions) -> rotate -> rotated_query;
(key, positions) -> rotate -> rotated_key;
(cache.keys, rotated_key, layer, positions[0]) -> write_rows -> keys;
(cache.values, value, layer, positions[0]) -> write_rows -> values;
KeyValueCache {
keys: keys,
values: values,
} -> updated_cache;
(rotated_query, positions, updated_cache, layer) -> attend -> attended;
(weights.output, attended) -> project -> attention_output: [f32; tokens * hidden_size];
(hidden, attention_output) -> add -> after_attention;
(after_attention, weights.feed_forward_norm) -> rms_norm -> normed_again;
(normed_again, weights) -> feed_forward -> feed_forward_output;
(after_attention, feed_forward_output) -> add -> ret.0;
updated_cache -> ret.1;
}
// Run a chunk of tokens, the first at `first_position`, through every layer; return the logits after the
// chunk's last token and the cache holding the chunk's keys and values.
fn forward<tokens, element: float>(ids: [i32; tokens], first_position: i32, model: Model, cache: KeyValueCache<element>) -> ([f32; vocabulary_size], KeyValueCache<element>) {
tokens -> iota -> map {
t -> first_position + t
} -> positions;
(model.embedding, ids) -> embed -> embedded;
mut hidden: [f32; tokens * hidden_size] <- embedded;
mut updated: KeyValueCache<element> <- cache;
mut layer: i32 <- 0;
loop {
(layer < layer_count) -> {
-true-> {
(hidden, model.layers[layer], layer, updated, positions) -> decoder_layer -> result;
result.0 -> hidden;
result.1 -> updated;
layer + 1 -> layer;
-> loop;
}
-false-> {
hidden_size -> iota -> map {
i -> hidden[(tokens - 1) * hidden_size + i]
} -> last;
(last, model.final_norm) -> rms_norm -> normed;
(model.embedding, normed) -> project -> logits: [f32; vocabulary_size];
(logits, updated) -> ret;
}
}
}
}
// Greedy decoding: the token with the largest logit (the first one on a tie).
fn most_likely_token(logits: [f32; vocabulary_size]) -> i32 {
vocabulary_size -> iota -> tokens;
((logits[0], 0), tokens) -> fold {
best, token -> (logits[token] > best.0) -> {
-true-> (logits[token], token);
-false-> best;
}
} -> winner;
winner.1 -> ret;
}
// ---------------------------------------------------------------------------------------------------------
fn main() {
"model.bin" -> read -> model: Model;
// The cache's format, chosen here once: f32 (`0.0 -> to<bf16>` stores it in bf16).
0.0 -> to<f32> -> zero;
(zero, cache_size) -> fill -> nothing_cached;
KeyValueCache {
keys: nothing_cached,
values: nothing_cached,
} -> empty_cache;
// The prompt arrives (the clock starts), and all of it at once, from position 0, gives the first token.
() -> time -> submitted;
"prompt.bin" -> read -> prompt_file: [i32; prompt_file_length];
prompt_length -> iota -> map {
t -> prompt_file[t]
} -> prompt;
(prompt, 0, model, empty_cache) -> forward -> prefilled;
prefilled.0 -> most_likely_token -> first_token;
() -> time -> first_token_time;
first_token -> print;
" " -> print;
prefilled.0[first_token] -> print;
" " -> print;
first_token_time - submitted -> println;
// Each later token: feed the previous one at the next position.
mut token: i32 <- first_token;
mut cache <- prefilled.1;
mut position: i32 <- prompt_length;
loop {
(position < prompt_length + generated_tokens - 1) -> {
-true-> {
[token] -> ids;
(ids, position, model, cache) -> forward -> stepped;
stepped.0 -> most_likely_token -> token;
stepped.1 -> cache;
() -> time -> now;
token -> print;
" " -> print;
stepped.0[token] -> print;
" " -> print;
now - submitted -> println;
position + 1 -> position;
-> loop;
}
-false-> position -> last_position;
}
}
}