-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathinfer.cpp
More file actions
372 lines (337 loc) · 11.4 KB
/
infer.cpp
File metadata and controls
372 lines (337 loc) · 11.4 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
#include "model.h"
#include <assert.h>
#include <cfloat>
#include <math.h>
#include "immintrin.h"
#include "f16cintrin.h"
static void matmul(float* xout, float* x, float* w, int n, int d) {
// W (d,n) @ x (n,) -> xout (d,)
int i;
#pragma omp parallel for private(i)
for (i = 0; i < d; i++) {
float val = 0.0f;
for (int j = 0; j < n; j++) {
val += w[i * n + j] * x[j];
}
xout[i] = val;
}
}
// matmul supporting float16 weights via the F16C extension, which allows
// conversion into float32 values before calculations.
static void matmul(float* xout, float* x, f16_t* w, int n, int d) {
#if defined(__AVX2__) && defined(__F16C__)
// W (d,n) @ x (n,) -> xout (d,)
assert(n % 16 == 0);
int i;
#pragma omp parallel for private(i)
for (i = 0; i < d; i++) {
// Vectorized dot product of w[i][:] and x[:] where w is a packed float16 array.
__m256 sumlo = _mm256_setzero_ps();
__m256 sumhi = _mm256_setzero_ps();
for (int j = 0; j < n; j+=16) {
// Extract the next set of 16 float16 weights from `w` and store them
// to two separate float32 vectors of width 8 (`wveclo_ps`, `wvechi_ps`)
__m256i wvec = _mm256_loadu_si256((__m256i*)&w[i * n + j]);
__m128i wveclo = _mm256_extractf128_si256(wvec, 0);
__m128i wvechi = _mm256_extractf128_si256(wvec, 1);
__m256 wveclo_ps = _mm256_cvtph_ps(wveclo);
__m256 wvechi_ps = _mm256_cvtph_ps(wvechi);
// Extract the next two float32 vectors of width 8 `xveclo`, `xvechi` from `x`
__m256 xveclo = _mm256_loadu_ps(&x[j]);
__m256 xvechi = _mm256_loadu_ps(&x[j + 8]);
// Compute vectorized FMAs: sumlo += wveclo * xveclo, sumhi += wvechi * xvechi
sumlo = _mm256_fmadd_ps(wveclo_ps, xveclo, sumlo);
sumhi = _mm256_fmadd_ps(wvechi_ps, xvechi, sumhi);
}
// Horizontally reduce width-8 float32 vectors sumlo, sumhi to a scalar.
__m256 sum8 = _mm256_add_ps(sumlo, sumhi); // sum8[0:8] = sumlo[0:8] + sumhi[0:8]
__m128 sum4 = _mm_add_ps( // sum4[0:4] = sum8[0:4] + sum8[4:8]
_mm256_extractf128_ps(sum8, 0),
_mm256_extractf128_ps(sum8, 1)
);
__m128 sum1 = _mm_dp_ps(sum4, _mm_set1_ps(1.0f), 0xf1); // sum1[0] = dot(sum4, [1,1,1,1])
xout[i] = _mm_cvtss_f32(sum1);
}
#else
assert(false && "float16 not supported on this platform");
#endif
}
static void rmsnorm(float* o, float* x, float* weight, int size, float eps) {
float rms = 0.0f;
for (int i = 0; i < size; ++i) {
rms += x[i] * x[i];
}
rms = sqrtf(rms / size + eps);
float scale = 1.0f / rms;
for (int i = 0; i < size; ++i) {
o[i] = x[i] * scale * weight[i];
}
}
[[maybe_unused]] static void layernorm(float* o, float* x, float* weight, float* bias, int size, float eps) {
float mean = 0.0f;
for (int i = 0; i < size; ++i) {
mean += x[i];
}
mean /= size;
float var = 0.0f;
for (int i = 0; i < size; ++i) {
var += (x[i] - mean) * (x[i] - mean);
}
var /= size;
float scale = 1.0f / sqrtf(var + eps);
if (bias) {
for (int i = 0; i < size; ++i) {
o[i] = (x[i] - mean) * scale * weight[i] + bias[i];
}
} else {
for (int i = 0; i < size; ++i) {
o[i] = (x[i] - mean) * scale * weight[i];
}
}
}
// Compute the softmax of an input vector `x` of length `size` and store it in `o`.
static void softmax(float* o, float* x, int size) {
float score_max = -FLT_MAX;
for (int i = 0; i < size; ++i) {
if (x[i] > score_max) {
score_max = x[i];
}
}
float score_sum = 0.0f;
for (int i = 0; i < size; ++i) {
o[i] = expf(x[i] - score_max);
score_sum += o[i];
}
for (int i = 0; i < size; ++i) {
o[i] /= score_sum;
}
}
inline float gelu(float x) {
return 0.5f * x * (1.0f + tanhf(0.797885f * (x + 0.044715f * x * x * x)));
}
inline float silu(float x) {
return x / (1.0f + expf(-x));
}
inline float clip(float x, float v) {
return x < -v ? -v : (x > v ? v : x);
}
// TODO annotate me
static void rope(float* vec, int d, int head_dim, int pos, float theta, int rotary_dim) {
for (int i = 0; i < d; i += 2) {
int j_head = i % head_dim;
float freq = j_head >= rotary_dim ? 0.f : 1.0f / powf(theta, (float)j_head / (float)rotary_dim);
float val = pos * freq;
float fcr = cosf(val);
float fci = sinf(val);
float v0 = vec[i];
float v1 = vec[i + 1];
vec[i] = v0 * fcr - v1 * fci;
vec[i + 1] = v0 * fci + v1 * fcr;
}
}
// Compute next value in a sequence for a single causal self-attention head.
void attn(
float* xout, // (dim,) - output vector
float* atth, // (kv_len,) - scratch space to hold attention scores of the sequence
float* qh, // (head_dim,) - query vector for this head
float* kh, // (kv_len, n_kv_heads, head_dim) - buffer containing key vectors of the sequence for all KV heads
float* vh, // (kv_len, n_kv_heads, head_dim) - buffer containing value vectors of the sequence for all KV heads
int head_dim, // size of the "key-space"
int n_kv_heads, // number of kv heads, can be < n_heads (1 is MultiQueryAttention, >1 is GroupedQueryAttention)
int kv_len // number of tokens of the sequence we will attend over
) {
int kv_stride = n_kv_heads * head_dim; // stride per token in this kv head
// calculate attention scores as dot products of q and k
for (int t = 0; t < kv_len; ++t) {
float score = 0.0f;
for (int i = 0; i < head_dim; ++i) {
score += qh[i] * kh[t * kv_stride + i];
}
score /= sqrtf(head_dim);
atth[t] = score;
}
// softmax the scores to get attention weights over [0..kv_len)
softmax(atth, atth, kv_len);
// mix values with attention weights
for (int i = 0; i < head_dim; ++i) {
float vi = 0.0f;
for (int t = 0; t < kv_len; ++t) {
vi += atth[t] * vh[t * kv_stride + i];
}
xout[i] = vi;
}
}
void Block::block(
InferenceState& s, // inference state
const Config& c, // model configuration
int pos, // index of the current token in the sequence
int kv_pos, // index of the current token in the kv cache, must be in [0..kv_len) since kv cache is a ring buffer
int kv_len // number of tokens in the kv cache that we will attend over
) const {
switch (_weight_dtype) {
case DType::dt_f32: {
_block<float>(s, c, pos, kv_pos, kv_len);
break;
}
case DType::dt_f16: {
#if defined(__AVX2__) && defined(__F16C__)
_block<f16_t>(s, c, pos, kv_pos, kv_len);
#else
assert(false && "float16 not supported on this platform");
#endif
break;
}
default: {
assert(false && "unsupported weight dtype");
}
}
}
// Compute forward pass for a single block and update the inference state accordingly.
// PRECONDITIONS:
// - `s.x()` contains the input to the block. Output will also go here.
// - Block KV cache is hydrated.
template <typename T>
void Block::_block(
InferenceState& s, // inference state
const Config& c, // model configuration
int pos, // index of the current token in the sequence
int kv_pos, // index of the current token in the kv cache, must be in [0..kv_len) since kv cache is a ring buffer
int kv_len // number of tokens in the kv cache that we will attend over
) const {
// attention pre-norm
switch (c.norm_type) {
case LayerNormType::RMSNorm: {
rmsnorm(s.xb(), s.x(), rms_att_weight(), c.dim, c.norm_eps);
break;
}
}
int q_dim = c.n_heads * c.head_dim;
int kv_dim = c.n_kv_heads * c.head_dim;
// qkv matmuls for this position
matmul(s.q(), s.xb(), wq<T>(), c.dim, q_dim);
matmul(s.k(), s.xb(), wk<T>(), c.dim, kv_dim);
matmul(s.v(), s.xb(), wv<T>(), c.dim, kv_dim);
// some models require clipping qkv values
for (int i = 0; i < q_dim; ++i) {
s.q()[i] = clip(s.q()[i], c.qkv_clip);
}
for (int i = 0; i < kv_dim; ++i) {
s.k()[i] = clip(s.k()[i], c.qkv_clip);
s.v()[i] = clip(s.v()[i], c.qkv_clip);
}
// RoPE relative positional encoding: complex-valued rotate q and k in each head
rope(s.q(), q_dim, c.head_dim, pos, c.rope_theta, c.rotary_dim);
rope(s.k(), kv_dim, c.head_dim, pos, c.rope_theta, c.rotary_dim);
// key and value point to the kv cache
float* kb = key_cache();
float* vb = value_cache();
// update kv cache
for (int i = 0; i < kv_dim; ++i) {
kb[kv_pos * kv_dim + i] = s.k()[i];
vb[kv_pos * kv_dim + i] = s.v()[i];
}
// Multihead attention. Iterate over all heads.
int q_per_kv_head = c.n_heads / c.n_kv_heads; // query heads per kv head (for MultiQueryAttention/GroupedQueryAttention)
int h;
#pragma omp parallel for private(h)
for (h = 0; h < c.n_heads; h++) {
int kv_head_offset = (h / q_per_kv_head) * c.head_dim;
float* kh = kb + kv_head_offset;
float* vh = vb + kv_head_offset;
attn(s.xb2(h), s.att(h), s.q(h), kh, vh, c.head_dim, c.n_kv_heads, kv_len);
}
// final matmul to get output of the attention, using `hb` as temp storage
matmul(s.hb(), s.xb2(), wo<T>(), q_dim, c.dim);
// residual connection back into x
for (int i = 0; i < c.dim; ++i) {
s.x()[i] += s.hb()[i];
}
// ffn pre-norm
switch (c.norm_type) {
case LayerNormType::RMSNorm: {
rmsnorm(s.xb(), s.x(), rms_ffn_weight(), c.dim, c.norm_eps);
break;
}
}
// mix self.w2(F.silu(self.w1(x)) * self.w3(x))
// Note this is a feedforward with a GLU, not a simple MLP.
matmul(s.hb(), s.xb(), w1<T>(), c.dim, c.hidden_dim);
matmul(s.hb2(), s.xb(), w3<T>(), c.dim, c.hidden_dim);
switch (c.act) {
case ActivationType::GELU: {
for (int i = 0; i < c.hidden_dim; ++i) {
s.hb()[i] = gelu(s.hb()[i]) * s.hb2()[i];
}
break;
}
case ActivationType::SILU: {
for (int i = 0; i < c.hidden_dim; ++i) {
s.hb()[i] = silu(s.hb()[i]) * s.hb2()[i];
}
break;
}
}
matmul(s.xb2(), s.hb(), w2<T>(), c.hidden_dim, c.dim);
// residual connection back into x
for (int i = 0; i < c.dim; ++i) {
s.x()[i] += s.xb2()[i];
}
}
void Model::copy_embedding(InferenceState& s, int token) {
switch (config.weight_dtype) {
case DType::dt_f32: {
float* emb = static_cast<float*>(token_embedding_table);
for (int i = 0; i < config.dim; ++i) {
s.x()[i] = emb[token * config.dim + i];
}
break;
}
case DType::dt_f16: {
#if defined(__AVX2__) && defined(__F16C__)
f16_t* emb = static_cast<f16_t*>(token_embedding_table);
for (int i = 0; i < config.dim; i+=1) {
s.x()[i] = _cvtsh_ss(emb[token * config.dim + i]);
}
#else
assert(false && "float16 not supported on this platform");
#endif
break;
}
default: {
assert(false && "unsupported weight dtype");
}
}
}
void Model::forward(InferenceState& s, int token, int pos) {
const Config& c = config;
// copy the token embedding into `x`
copy_embedding(s, token);
// TODO: attention sinks
int kv_pos = pos % c.max_seq_len;
int kv_len = pos >= c.max_seq_len ? c.max_seq_len : pos + 1;
// forward all layers in order
for (auto& b : blocks) {
b.block(s, c, pos, kv_pos, kv_len);
}
// final layer norm
switch (c.norm_type) {
case LayerNormType::RMSNorm: {
rmsnorm(s.x(), s.x(), rms_final_weight, c.dim, c.norm_eps);
break;
}
}
// classifier into logits
switch (config.weight_dtype) {
case DType::dt_f32: {
matmul(s.logits(), s.x(), static_cast<float*>(wcls), c.dim, c.vocab_size);
break;
}
case DType::dt_f16: {
matmul(s.logits(), s.x(), static_cast<f16_t*>(wcls), c.dim, c.vocab_size);
break;
}
default: {
assert(false && "unsupported weight dtype");
}
}
}