-
Notifications
You must be signed in to change notification settings - Fork 11
/
gpt2.cpp
174 lines (134 loc) · 4.49 KB
/
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
#include "ggml.h"
#include "gpt2.h"
#include "common.h"
#include "common-ggml.h"
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>
#include "ggml.cpp/examples/gpt-2/main.cpp"
struct gpt2_state {
gpt_vocab vocab;
gpt2_model model;
struct {
int64_t t_load_us = -1;
int64_t t_sample_us = -1;
int64_t t_predict_us = -1;
} timing;
};
int gpt2_predict(void* params_ptr, void* state_pr, char* result) {
gpt_params params = *(gpt_params*) params_ptr;
gpt2_state state = *(gpt2_state*) state_pr;
gpt_vocab vocab = state.vocab;
gpt2_model model = state.model;
const int64_t t_main_start_us = ggml_time_us();
if (params.seed < 0) {
params.seed = time(NULL);
}
std::mt19937 rng(params.seed);
int64_t t_load_us = 0;
int n_past = 0;
int64_t t_sample_us = 0;
int64_t t_predict_us = 0;
std::vector<float> logits;
// tokenize the prompt
std::vector<gpt_vocab::id> embd_inp = ::gpt_tokenize(vocab, params.prompt);
params.n_predict = std::min(params.n_predict, model.hparams.n_ctx - (int) embd_inp.size());
std::string res = "";
// submit the input prompt token-by-token
// this reduces the memory usage during inference, at the cost of a bit of speed at the beginning
std::vector<gpt_vocab::id> embd;
// determine the required inference memory per token:
size_t mem_per_token = 0;
gpt2_eval(model, params.n_threads, 0, { 0, 1, 2, 3 }, logits, mem_per_token);
for (int i = embd.size(); i < embd_inp.size() + params.n_predict; i++) {
// predict
if (embd.size() > 0) {
const int64_t t_start_us = ggml_time_us();
if (!gpt2_eval(model, params.n_threads, n_past, embd, logits, mem_per_token)) {
printf("Failed to predict\n");
return 1;
}
t_predict_us += ggml_time_us() - t_start_us;
}
n_past += embd.size();
embd.clear();
if (i >= embd_inp.size()) {
// sample next token
const int top_k = params.top_k;
const float top_p = params.top_p;
const float temp = params.temp;
const int n_vocab = model.hparams.n_vocab;
gpt_vocab::id id = 0;
{
const int64_t t_start_sample_us = ggml_time_us();
id = gpt_sample_top_k_top_p(vocab, logits.data() + (logits.size() - n_vocab), top_k, top_p, temp, rng);
t_sample_us += ggml_time_us() - t_start_sample_us;
}
// add it to the context
embd.push_back(id);
} else {
// if here, it means we are still processing the input prompt
for (int k = i; k < embd_inp.size(); k++) {
embd.push_back(embd_inp[k]);
if (embd.size() >= params.n_batch) {
break;
}
}
i += embd.size() - 1;
}
// display text
for (auto id : embd) {
res += vocab.id_to_token[id].c_str();
}
// end of text token
if (embd.back() == 50256) {
break;
}
}
strcpy(result, res.c_str());
return 0;
}
int gpt2_bootstrap(const char *model_path, void* state_pr)
// load the model
{
ggml_time_init();
gpt2_state* state = (gpt2_state*) state_pr;
const int64_t t_start_us = ggml_time_us();
if (!gpt2_model_load(model_path, state->model, state->vocab)) {
fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, model_path);
return 1;
}
state->timing.t_load_us = ggml_time_us() - t_start_us;
return 0;
}
void* gpt2_allocate_state() {
return new gpt2_state;
}
void gpt2_free_model(void *state_ptr) {
gpt2_state* state = (gpt2_state*) state_ptr;
ggml_free(state->model.ctx);
}
void gpt2_free_params(void* params_ptr) {
gpt_params* params = (gpt_params*) params_ptr;
delete params;
}
void* gpt2_allocate_params(const char *prompt, int seed, int threads, int tokens, int top_k,
float top_p, float temp, int n_batch) {
gpt_params* params = new gpt_params;
params->seed = seed;
params->n_threads = threads;
params->n_predict = tokens;
params->top_k = top_k;
params->top_p = top_p;
params->temp = temp;
params->n_batch = n_batch;
params->prompt = prompt;
return params;
}