-
Notifications
You must be signed in to change notification settings - Fork 11
/
mpt.cpp
208 lines (154 loc) · 5.23 KB
/
mpt.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
// https://github.com/ggerganov/ggml/pull/139
#include "ggml.h"
#include "common.h"
#include "common-ggml.h"
#include "mpt.h"
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cinttypes>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>
#include "ggml.cpp/examples/mpt/main.cpp"
struct mpt_state {
gpt_vocab vocab;
mpt_model model;
struct {
int64_t t_load_us = -1;
int64_t t_sample_us = -1;
int64_t t_predict_us = -1;
} timing;
};
int mpt_predict(void* params_ptr, void* state_pr, char* result) {
mpt_params params = *(mpt_params*) params_ptr;
mpt_state state = *(mpt_state*) state_pr;
gpt_vocab vocab = state.vocab;
mpt_model model = state.model;
const int64_t t_main_start_us = ggml_time_us();
if (params.seed < 0) {
params.seed = time(NULL);
}
printf("%s: seed = %d\n", __func__, params.seed);
std::mt19937 rng(params.seed);
int64_t t_load_us = 0;
int64_t t_sample_us = 0;
int64_t t_predict_us = 0;
std::vector<float> logits;
if ( params.n_predict <= 0 ) {
params.n_predict = 200;
}
model.hparams.n_ctx = params.n_ctx;
if (params.top_k == 0) {
params.top_k = model.hparams.n_vocab;
}
if (params.repeat_last_n == -1) {
params.repeat_last_n = params.n_ctx;
}
std::vector<int32_t> last_n_tokens(params.n_ctx);
std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
// tokenize the prompt
std::vector<int> embd_inp = ::gpt_tokenize(vocab, params.prompt);
std::string res = "";
std::vector<gpt_vocab::id> embd;
// determine the required inference memory per token:
size_t mem_per_token = 0;
mpt_eval(model, params.n_threads, 0, {0, 1, 2, 3}, logits, false, mem_per_token);
int n_past = 0;
int n_consumed = 0;
int n_sampled = 0;
while (n_sampled < params.n_predict) {
// predict
if (embd.size() > 0) {
const int64_t t_start_us = ggml_time_us();
if (!mpt_eval(model, params.n_threads, n_past, embd, logits, false, mem_per_token)) {
printf("%s: failed to predict\n", __func__);
return 1;
}
t_predict_us += ggml_time_us() - t_start_us;
n_past += embd.size();
embd.clear();
}
if ((int)embd_inp.size() <= n_consumed) {
// sample next token
const int top_k = params.top_k;
const float top_p = params.top_p;
const float temp = params.temp;
const int repeat_last_n = params.repeat_last_n;
const float repeat_penalty = params.repeat_penalty;
gpt_vocab::id id = 0;
{
const int64_t t_start_sample_us = ggml_time_us();
id = gpt_sample_top_k_top_p_repeat(vocab, logits.data() + (logits.size() - model.hparams.n_vocab), last_n_tokens.data(), last_n_tokens.size(), top_k, top_p, temp, repeat_last_n, repeat_penalty, rng);
last_n_tokens.erase(last_n_tokens.begin());
last_n_tokens.push_back(id);
t_sample_us += ggml_time_us() - t_start_sample_us;
}
// add it to the context
embd.push_back(id);
++n_sampled;
} else {
// if here, it means we are still processing the input prompt
while ((int) embd_inp.size() > n_consumed) {
embd.push_back(embd_inp[n_consumed]);
last_n_tokens.erase(last_n_tokens.begin());
last_n_tokens.push_back(embd_inp[n_consumed]);
++n_consumed;
if ((int) embd.size() >= params.n_batch) {
break;
}
}
}
// display text
for (auto id : embd) {
res += vocab.id_to_token[id].c_str();
}
// end of text token
if (embd.back() == 0) {
break;
}
}
strcpy(result, res.c_str());
return 0;
}
int mpt_bootstrap(const char *model_path, void* state_pr)
// load the model
{
ggml_time_init();
mpt_state* state = ( mpt_state*) state_pr;
const int64_t t_start_us = ggml_time_us();
if (! mpt_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* mpt_allocate_state() {
return new mpt_state;
}
void mpt_free_model(void *state_ptr) {
mpt_state* state = ( mpt_state*) state_ptr;
ggml_free(state->model.ctx);
}
void mpt_free_params(void* params_ptr) {
mpt_params* params = (mpt_params*) params_ptr;
delete params;
}
void* mpt_allocate_params(const char *prompt, int seed, int threads, int tokens, int top_k,
float top_p, float temp, int n_batch) {
mpt_params* params = new mpt_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;
}