-
Notifications
You must be signed in to change notification settings - Fork 106
/
llama2-tasks.cpp
298 lines (248 loc) · 10.2 KB
/
llama2-tasks.cpp
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
#include <cmath>
#include <cassert>
#include <string.h>
#include "utils.hpp"
#include "funcs.hpp"
#include "socket.hpp"
#include "tasks.hpp"
#include "llama2-tasks.hpp"
void llamaRmsAtt(TASK_ARGS) {
TASK_VARIABLES;
if (threadIndex == 0) {
transformer->rms = rms(transformer->x, spec->dim);
}
}
void llamaRmsAttNorm(TASK_ARGS) {
TASK_VARIABLES;
float* xb = (float*)transformer->buffer->getUnit(TB_UNIT_XB);
rmsnorm(xb, transformer->x, transformer->rms, block->rmsAtt, spec->dim, nThreads, threadIndex);
}
void llamaQuantizeRmsAtt(TASK_ARGS) {
TASK_VARIABLES;
quantizeUnitBuffer(nThreads, threadIndex, ctx, TB_UNIT_XB, TB_UNIT_XB_QUANTIZED);
}
void llamaSyncRmsAtt(TASK_ARGS) {
TASK_VARIABLES;
syncUnitBuffer(nThreads, threadIndex, ctx, TB_UNIT_XB_QUANTIZED);
}
void llamaQkv(TASK_ARGS) {
TASK_VARIABLES;
assert(block->kvCacheSlice->kvDim0 == block->k0Slice->d0);
assert(block->kvCacheSlice->kvDim0 == block->v0Slice->d0);
float *xbq = (float*)transformer->buffer->getUnit(TB_UNIT_XB_QUANTIZED);
float *k0 = &block->keyCache[transformer->pos * block->kvCacheSlice->kvDim0];
float* v0 = &block->valueCache[transformer->pos * block->kvCacheSlice->kvDim0];
block->q0mm->forward(xbq, block->qo0, nThreads, threadIndex);
block->k0mm->forward(xbq, k0, nThreads, threadIndex);
block->v0mm->forward(xbq, v0, nThreads, threadIndex);
}
void llamaRope(TASK_ARGS) {
TASK_VARIABLES;
float* k0 = &block->keyCache[transformer->pos * block->kvCacheSlice->kvDim0];
transformer->rope->forward(true, block->qo0, transformer->pos, nThreads, threadIndex);
transformer->rope->forward(false, k0, transformer->pos, nThreads, threadIndex);
}
void llamaMultiheadAtt(TASK_ARGS) {
TASK_VARIABLES;
SPLIT_RANGE_TO_THREADS(h0Start, h0End, 0, block->multiHeadAttSlice->nHeads0, nThreads, threadIndex);
float* xb = (float*)transformer->buffer->getSliced(TB_UNIT_XB, transformer->sliceIndex);
int kvMul = spec->nHeads / spec->nKvHeads; // integer multiplier of the kv sharing in multiquery
for (int h0 = h0Start; h0 < h0End; h0++) {
// get the query vector for this head
float* _q = block->qo0 + h0 * spec->headSize;
// attention scores for this head
float* _att = block->att + h0 * spec->seqLen;
// iterate over all timesteps, including the current one
for (int t = 0; t <= transformer->pos; t++) {
// get the key vector for this head and at this timestep
float* k = block->keyCache + t * block->kvCacheSlice->kvDim0 + (h0 / kvMul) * spec->headSize;
// calculate the attention score as the dot product of q and k
float score = dotProduct(_q, k, spec->headSize) / sqrtf(spec->headSize);
_att[t] = score;
}
// softmax the scores to get attention weights, from 0..pos inclusively
softmax(_att, transformer->pos + 1);
// weighted sum of the values, store back into xb
float* hxb = xb + h0 * spec->headSize;
memset(hxb, 0, spec->headSize * sizeof(float));
for (int t = 0; t <= transformer->pos; t++) {
// get the value vector for this head and at this timestep
float* _v = block->valueCache + t * block->kvCacheSlice->kvDim0 + (h0 / kvMul) * spec->headSize;
// get the attention weight for this timestep
float a = _att[t];
// accumulate the weighted value into xb
for (int i = 0; i < spec->headSize; i++) {
hxb[i] += a * _v[i];
}
}
}
}
void llamaQuantizeMultiheadAtt(TASK_ARGS) {
TASK_VARIABLES;
quantizeSlicedBuffer(nThreads, threadIndex, ctx, true, TB_UNIT_XB, TB_UNIT_XB_QUANTIZED);
};
void llamaAtt(TASK_ARGS) {
TASK_VARIABLES;
void* xbq0 = transformer->buffer->getSliced(TB_UNIT_XB_QUANTIZED, transformer->sliceIndex);
float* xbv0 = (float*)transformer->buffer->getSliced(TB_SLICED_XBV, transformer->sliceIndex);
block->wo0mm->forward(xbq0, xbv0, nThreads, threadIndex);
}
void llamaQuantizeAtt(TASK_ARGS) {
TASK_VARIABLES;
quantizeSlicedBuffer(nThreads, threadIndex, ctx, false, TB_SLICED_XBV, TB_SLICED_XBV_QUANTIZED);
}
void llamaSyncAtt(TASK_ARGS) {
TASK_VARIABLES;
syncSliceOfSlicedBuffer(nThreads, threadIndex, ctx, TB_SLICED_XBV_QUANTIZED);
}
void llamaDequantizeAtt(TASK_ARGS) {
TASK_VARIABLES;
dequantizeSlicedBuffer(nThreads, threadIndex, ctx, false, TB_SLICED_XBV_QUANTIZED, TB_SLICED_XBV);
}
void llamaMergeAtt(TASK_ARGS) {
TASK_VARIABLES;
for (slice_index_t sliceIndex = 0; sliceIndex < spec->nSlices; sliceIndex++) {
float* xbv = (float*)transformer->buffer->getSliced(TB_SLICED_XBV, sliceIndex);
add(transformer->x, xbv, spec->dim, nThreads, threadIndex);
}
}
void llamaRmfFfn(TASK_ARGS) {
TASK_VARIABLES;
if (threadIndex == 0) {
transformer->rms = rms(transformer->x, spec->dim);
}
}
void llamaRmfFfnNorm(TASK_ARGS) {
TASK_VARIABLES;
float* xb = (float*)transformer->buffer->getUnit(TB_UNIT_XB);
float* x = (float*)transformer->x;
rmsnorm(xb, x, transformer->rms, block->rmsFfn, spec->dim, nThreads, threadIndex);
}
void llamaQuantizeRmfFfn(TASK_ARGS) {
TASK_VARIABLES;
quantizeUnitBuffer(nThreads, threadIndex, ctx, TB_UNIT_XB, TB_UNIT_XB_QUANTIZED);
}
void llamaSyncFfn(TASK_ARGS) {
TASK_VARIABLES;
syncUnitBuffer(nThreads, threadIndex, ctx, TB_UNIT_XB_QUANTIZED);
}
void llamaFfn0(TASK_ARGS) {
TASK_VARIABLES;
float* xb = (float*)transformer->buffer->getUnit(TB_UNIT_XB_QUANTIZED);
float* hb0 = (float*)transformer->buffer->getSliced(TB_SLICED_HB, transformer->sliceIndex);
block->w10mm->forward(xb, hb0, nThreads, threadIndex);
block->w30mm->forward(xb, block->hb20, nThreads, threadIndex);
if (spec->hiddenAct == SILU) {
silu(hb0, block->w10Slice->d0, nThreads, threadIndex);
} else if (spec->hiddenDim == GELU) {
gelu(hb0, block->w10Slice->d0, nThreads, threadIndex);
} else {
assert(false);
}
mul(hb0, block->hb20, block->w10Slice->d0, nThreads, threadIndex);
}
void llamaFfn1(TASK_ARGS) {
TASK_VARIABLES;
quantizeSlicedBuffer(nThreads, threadIndex, ctx, true, TB_SLICED_HB, TB_SLICED_HB_QUANTIZED);
}
void llamaFfn2(TASK_ARGS) {
TASK_VARIABLES;
float *hb = (float*)transformer->buffer->getSliced(TB_SLICED_HB_QUANTIZED, transformer->sliceIndex);
float *xbv = (float*)transformer->buffer->getSliced(TB_SLICED_XBV, transformer->sliceIndex);
block->w20mm->forward(hb, xbv, nThreads, threadIndex);
}
void llamaQuantizeFfn2(TASK_ARGS) {
TASK_VARIABLES;
quantizeSlicedBuffer(nThreads, threadIndex, ctx, false, TB_SLICED_XBV, TB_SLICED_XBV_QUANTIZED);
}
void llamaSyncFfn2(TASK_ARGS) {
TASK_VARIABLES;
syncSliceOfSlicedBuffer(nThreads, threadIndex, ctx, TB_SLICED_XBV_QUANTIZED);
}
void llamaDequantizeFfn2(TASK_ARGS) {
TASK_VARIABLES;
dequantizeSlicedBuffer(nThreads, threadIndex, ctx, false, TB_SLICED_XBV_QUANTIZED, TB_SLICED_XBV);
}
void llamaMergeFfn2(TASK_ARGS) {
TASK_VARIABLES;
for (slice_index_t sliceIndex = 0; sliceIndex < spec->nSlices; sliceIndex++) {
float* xbv = (float*)transformer->buffer->getSliced(TB_SLICED_XBV, sliceIndex);
add(transformer->x, xbv, spec->dim, nThreads, threadIndex);
}
}
void llamaNextBlock(TASK_ARGS) {
TASK_VARIABLES;
if (threadIndex == 0) {
ctx->currentBlockIndex++;
}
}
void llamaRmsFinal(TASK_ARGS) {
TASK_VARIABLES;
if (threadIndex == 0) {
float* x = transformer->x;
transformer->rms = rms(x, spec->dim);
}
}
void llamaRmsFinalNorm(TASK_ARGS) {
TASK_VARIABLES;
float* x = transformer->x;
rmsnorm(x, x, transformer->rms, (float*)transformer->rmsFinal, spec->dim, nThreads, threadIndex);
}
void llamaFinalize(TASK_ARGS) {
TASK_VARIABLES;
transformer->wclsMm->forward(transformer->x, transformer->logits, nThreads, threadIndex);
}
TransformerArch buildLlamaArch(TransformerSpec* spec) {
TransformerArch a;
// inference
a.I(sendPos, TASK_TYPE_TRANSFER);
for (int i = 0; i < spec->nLayers; i++) {
a.I(llamaRmsAtt, TASK_TYPE_INFERENCE);
a.I(llamaRmsAttNorm, TASK_TYPE_INFERENCE);
a.I(llamaQuantizeRmsAtt, TASK_TYPE_INFERENCE);
a.I(llamaSyncRmsAtt, TASK_TYPE_TRANSFER);
a.I(llamaQkv, TASK_TYPE_INFERENCE);
a.I(llamaRope, TASK_TYPE_INFERENCE);
a.I(llamaMultiheadAtt, TASK_TYPE_INFERENCE);
a.I(llamaQuantizeMultiheadAtt, TASK_TYPE_INFERENCE);
a.I(llamaAtt, TASK_TYPE_INFERENCE);
a.I(llamaQuantizeAtt, TASK_TYPE_INFERENCE);
a.I(llamaSyncAtt, TASK_TYPE_TRANSFER);
a.I(llamaDequantizeAtt, TASK_TYPE_INFERENCE);
a.I(llamaMergeAtt, TASK_TYPE_INFERENCE);
a.I(llamaRmfFfn, TASK_TYPE_INFERENCE);
a.I(llamaRmfFfnNorm, TASK_TYPE_INFERENCE);
a.I(llamaQuantizeRmfFfn, TASK_TYPE_INFERENCE);
a.I(llamaSyncFfn, TASK_TYPE_TRANSFER);
a.I(llamaFfn0, TASK_TYPE_INFERENCE);
a.I(llamaFfn1, TASK_TYPE_INFERENCE);
a.I(llamaFfn2, TASK_TYPE_INFERENCE);
a.I(llamaQuantizeFfn2, TASK_TYPE_INFERENCE);
a.I(llamaSyncFfn2, TASK_TYPE_TRANSFER);
a.I(llamaDequantizeFfn2, TASK_TYPE_INFERENCE);
a.I(llamaMergeFfn2, TASK_TYPE_INFERENCE);
a.I(llamaNextBlock, TASK_TYPE_INFERENCE);
}
a.I(llamaRmsFinal, TASK_TYPE_INFERENCE);
a.I(llamaRmsFinalNorm, TASK_TYPE_INFERENCE);
a.I(llamaFinalize, TASK_TYPE_INFERENCE);
// worker
for (int i = 0; i < spec->nLayers; i++) {
a.W(llamaSyncRmsAtt, TASK_TYPE_TRANSFER);
a.W(llamaQkv, TASK_TYPE_INFERENCE);
a.W(llamaRope, TASK_TYPE_INFERENCE);
a.W(llamaMultiheadAtt, TASK_TYPE_INFERENCE);
a.W(llamaQuantizeMultiheadAtt, TASK_TYPE_INFERENCE);
a.W(llamaAtt, TASK_TYPE_INFERENCE);
a.W(llamaQuantizeAtt, TASK_TYPE_INFERENCE);
a.W(llamaSyncAtt, TASK_TYPE_TRANSFER);
a.W(llamaSyncFfn, TASK_TYPE_TRANSFER);
a.W(llamaFfn0, TASK_TYPE_INFERENCE);
a.W(llamaFfn1, TASK_TYPE_INFERENCE);
a.W(llamaFfn2, TASK_TYPE_INFERENCE);
a.W(llamaQuantizeFfn2, TASK_TYPE_INFERENCE);
a.W(llamaSyncFfn2, TASK_TYPE_TRANSFER);
a.W(llamaNextBlock, TASK_TYPE_INFERENCE);
}
return a;
}