-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_gpt2.cpp
349 lines (310 loc) · 13.3 KB
/
test_gpt2.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
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
#include "boost/ut.hpp"
#define TESTING
#include <cmath>
#include <cstddef>
#include <ctime>
#include <iostream>
#include <random>
#include "train_gpt2.cpp"
using namespace boost; // NOLINT
// the parameters of the model
#define NUM_PARAMETER_TENSORS 16
typedef struct {
float *wte; // (V, C)
float *wpe; // (maxT, C)
float *ln1w; // (L, C)
float *ln1b; // (L, C)
float *qkvw; // (L, 3*C, C)
float *qkvb; // (L, 3*C)
float *attprojw; // (L, C, C)
float *attprojb; // (L, C)
float *ln2w; // (L, C)
float *ln2b; // (L, C)
float *fcw; // (L, 4*C, C)
float *fcb; // (L, 4*C)
float *fcprojw; // (L, C, 4*C)
float *fcprojb; // (L, C)
float *lnfw; // (C)
float *lnfb; // (C)
} ParameterTensors;
bool similar(float a, float b) { return !std::isnan(a) && !std::isnan(b) && std::abs(a - b) < 1e-2; }
// poor man's tensor checker
int check_tensor(std::vector<float *> const &a, int m, float *b, int n, std::string const &label,
std::string const &indent = "") {
const int print_upto = 5;
int ok = 1;
int okc = 0, notokc = 0;
std::cout << indent << label << std::endl;
for (int i = 0; i < n; i++) {
const int i0 = i / m, i1 = i % m;
if (similar(a[i0][i1], b[i])) {
if (okc < print_upto) {
std::cout << indent << "OK " << a[i0][i1] << " " << b[i] << "\n";
}
okc++;
} else {
if (notokc < print_upto) {
std::cout << indent << "NOT OK " << a[i0][i1] << " " << b[i] << "\n";
}
notokc++;
ok = 0;
}
}
// print the final result
if (ok) {
std::cout << indent << "TENSOR OK" << std::endl;
} else {
std::cout << indent << "TENSOR NOT OK" << std::endl;
}
return ok;
}
int check_tensor(float *a, float *b, int n, std::string const &label, std::string const &indent = "") {
return check_tensor({a}, n, b, n, label, indent);
}
int check_ml_tensor(std::vector<Block> const &blocks, std::function<Tensor *(const Block &)> const &select, float *b,
int n_layer, int n, std::string const &label) {
const int m = n / n_layer;
std::vector<float *> data(n_layer);
for (int i = 0; i < n_layer; i++) {
data[i] = (float *)select(blocks[i])->data();
}
const int ok = check_tensor(data, m, b, n, label);
if (!ok) {
// if not ok, find the first layer that is not ok
for (int l = 0; l < n_layer; l++) {
const int res = check_tensor((float *)select(blocks[l])->data(), b + (size_t)l * m, m,
label + "-L" + std::to_string(l), " ");
if (!res) {
break;
}
}
}
return ok;
}
// allocate memory for the parameters and point the individual tensors to the
// right places
float *malloc_and_point_parameters(ParameterTensors *params, size_t *param_sizes) {
size_t num_parameters = 0;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++) {
num_parameters += param_sizes[i];
}
// malloc all parameters all at once
float *params_memory = (float *)malloc(num_parameters * sizeof(float));
// assign all the tensors
float **ptrs[] = {¶ms->wte, ¶ms->wpe, ¶ms->ln1w, ¶ms->ln1b, ¶ms->qkvw, ¶ms->qkvb,
¶ms->attprojw, ¶ms->attprojb, ¶ms->ln2w, ¶ms->ln2b, ¶ms->fcw, ¶ms->fcb,
¶ms->fcprojw, ¶ms->fcprojb, ¶ms->lnfw, ¶ms->lnfb};
float *params_memory_iterator = params_memory;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++) {
*(ptrs[i]) = params_memory_iterator;
params_memory_iterator += param_sizes[i];
}
return params_memory;
}
void TestGPT2() {
// build the GPT-2 model from a checkpoint
GPT2 model;
gpt2_build_from_checkpoint(&model, "gpt2_124M.bin");
size_t C, V, Vp, maxT, L;
C = model.config.channels;
V = model.config.vocab_size;
Vp = model.config.padded_vocab_size;
maxT = model.config.max_seq_len;
L = model.config.num_layers;
// load additional information that we will use for debugging and error
// checking
FILE *state_file = fopen("gpt2_124M_debug_state.bin", "rb");
if (state_file == nullptr) {
throw std::runtime_error("Error opening state file");
}
int state_header[256];
fread(state_header, sizeof(int), 256, state_file);
if (state_header[0] != 20240327) {
throw std::runtime_error("Bad magic state file");
}
if (state_header[1] != 2) {
throw std::runtime_error("Bad version in state file");
}
const size_t B = state_header[2];
const size_t T = state_header[3];
std::cout << "[State]" << std::endl;
std::cout << "batch_size: " << B << std::endl;
std::cout << "seq_len: " << T << std::endl;
size_t param_sizes[NUM_PARAMETER_TENSORS];
// allocate space for all the parameters and read them in
param_sizes[0] = Vp * C;
param_sizes[1] = maxT * C;
param_sizes[2] = L * C;
param_sizes[3] = L * C;
param_sizes[4] = L * (3 * C) * C;
param_sizes[5] = L * (3 * C);
param_sizes[6] = L * C * C;
param_sizes[7] = L * C;
param_sizes[8] = L * C;
param_sizes[9] = L * C;
param_sizes[10] = L * (4 * C) * C;
param_sizes[11] = L * (4 * C);
param_sizes[12] = L * C * (4 * C);
param_sizes[13] = L * C;
param_sizes[14] = C;
param_sizes[15] = C;
size_t num_parameters = 0;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++) {
num_parameters += param_sizes[i];
}
ParameterTensors expected_grads;
float *expected_grads_memory = malloc_and_point_parameters(&expected_grads, param_sizes);
// inputs and expected outputs, only used for error checking
int *x = (int *)malloc(B * T * sizeof(int));
int *y = (int *)malloc(B * T * sizeof(int));
float *expected_logits = (float *)malloc(B * T * V * sizeof(float));
float *expected_loss = (float *)malloc(1 * sizeof(float));
fread(x, sizeof(int), B * T, state_file);
fread(y, sizeof(int), B * T, state_file);
fread(expected_logits, sizeof(float), B * T * V, state_file);
fread(expected_loss, sizeof(float), 1, state_file);
fread(expected_grads_memory, sizeof(float), num_parameters, state_file);
fclose(state_file);
bool allok = true;
// expected losses are as follows, from Python
const float expected_losses[10] = {5.270007133483887, 4.059706687927246, 3.3751230239868164, 2.8007826805114746,
2.315382242202759, 1.8490285873413086, 1.3946564197540283, 0.9991465210914612,
0.6240804195404053, 0.37651097774505615};
for (int step = 0; step < 10; step++) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
gpt2_forward(&model, x, y, B, T);
gpt2_zero_grad(&model);
gpt2_backward(&model);
clock_gettime(CLOCK_MONOTONIC, &end);
const double time_elapsed_s = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
if (step == 0) {
// error checking at step 0 for reference activations/gradients
// at this point, target should be equal to expected_logits, let's
// compare
bool logits_ok = true;
auto logits = model.logits->Flatten();
for (int bt = 0; bt < B * T; bt++) {
for (int v = 0; v < V; v++) {
const int i = bt * Vp + v;
const int ei = bt * V + v;
if (i < 10) {
std::cout << expected_logits[ei] << " " << logits[i] << std::endl;
}
if (!similar(expected_logits[ei], logits[i])) {
std::cout << "MISMATCH AT INDEX " << i << ": ";
std::cout << expected_logits[ei] << " " << logits[i] << std::endl;
logits_ok = false;
bt = B * T; // break out of the loop
break;
}
}
}
if (!logits_ok) {
std::cout << "NOT ";
}
std::cout << "OK (LOGITS)" << std::endl;
allok = allok && logits_ok;
if (!similar(model.mean_loss, *expected_loss)) {
std::cout << "LOSS MISMATCH: " << model.mean_loss << " " << *expected_loss << std::endl;
allok = false;
} else {
std::cout << "OK (LOSS): " << model.mean_loss << " " << *expected_loss << std::endl;
}
// finally, compare the gradients
bool gradoks[NUM_PARAMETER_TENSORS];
gradoks[0] = check_tensor((float *)model.embedding.wte->grad()->data(), expected_grads.wte,
param_sizes[0], "dwte");
gradoks[1] = check_tensor((float *)model.embedding.wpe->grad()->data(), expected_grads.wpe,
param_sizes[1], "dwpe");
gradoks[2] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.ln1w->grad(); }, expected_grads.ln1w, L,
param_sizes[2], "dln1w");
gradoks[3] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.ln1b->grad(); }, expected_grads.ln1b, L,
param_sizes[3], "dln1b");
gradoks[4] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.qkvw->grad(); }, expected_grads.qkvw, L,
param_sizes[4], "dqkvw");
gradoks[5] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.qkvb->grad(); }, expected_grads.qkvb, L,
param_sizes[5], "dqkvb");
gradoks[6] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.attprojw->grad(); }, expected_grads.attprojw, L,
param_sizes[6], "dattprojw");
gradoks[7] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.attprojb->grad(); }, expected_grads.attprojb, L,
param_sizes[7], "dattprojb");
gradoks[8] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.ln2w->grad(); }, expected_grads.ln2w, L,
param_sizes[8], "dln2w");
gradoks[9] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.ln2b->grad(); }, expected_grads.ln2b, L,
param_sizes[9], "dln2b");
gradoks[10] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.fcw->grad(); }, expected_grads.fcw, L,
param_sizes[10], "dfcw");
gradoks[11] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.fcb->grad(); }, expected_grads.fcb, L,
param_sizes[11], "dfcb");
gradoks[12] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.fcprojw->grad(); }, expected_grads.fcprojw, L,
param_sizes[12], "dfcprojw");
gradoks[13] = check_ml_tensor(
model.blocks, [](Block const &block) { return block.fcprojb->grad(); }, expected_grads.fcprojb, L,
param_sizes[13], "dfcprojb");
gradoks[14] = check_tensor((float *)model.lm_head.lnfw->grad()->data(), expected_grads.lnfw,
param_sizes[14], "dlnfw");
gradoks[15] = check_tensor((float *)model.lm_head.lnfb->grad()->data(), expected_grads.lnfb,
param_sizes[15], "dlnfb");
bool gradok = true;
for (int i = 0; i < NUM_PARAMETER_TENSORS; i++) {
gradok = gradok && gradoks[i];
}
if (!gradok) {
std::cout << "NOT OK (GRADIENTS)" << std::endl;
} else {
std::cout << "OK (GRADIENTS)" << std::endl;
}
allok = allok && gradok;
}
gpt2_update(&model, 1e-4f, 0.9f, 0.999f, 1e-8f, 0.01f, step + 1);
const float expected_loss = expected_losses[step];
const float actual_loss = model.mean_loss;
const bool step_loss_ok = similar(expected_loss, actual_loss);
allok = allok && step_loss_ok;
std::cout << "Step " << step << ": loss " << model.mean_loss << " (took " << time_elapsed_s * 1000 << " ms) ";
if (step_loss_ok) {
std::cout << "OK" << std::endl;
} else {
std::cout << "NOT OK, expected loss " << expected_loss << std::endl;
}
std::cout << "\n\n";
// print profiling information
std::cout << "Forward ";
Tensor::ForwardProfile.Print();
std::cout << "Backward ";
Tensor::BackwardProfile.Print();
std::cout << "\n\n";
}
if (allok) {
std::cout << "All OK" << std::endl;
} else {
std::cout << "NOT OK" << std::endl;
}
free(x);
free(y);
free(expected_logits);
free(expected_loss);
free(expected_grads_memory);
gpt2_free(&model);
}
int main() {
try {
TestGPT2();
} catch (std::exception const &e) {
std::cerr << e.what() << std::endl;
exit(1);
}
return 0;
}