forked from bigcode-project/starcoder.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
276 lines (221 loc) · 8.19 KB
/
Copy pathserver.cpp
File metadata and controls
276 lines (221 loc) · 8.19 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
#include "ggml.h"
#include "common.h"
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <unistd.h>
#include <vector>
#include "util/httplib.hpp"
#include "util/json.hpp"
#include "starcoder.hpp"
std::vector<std::string>
starcoder_demo_generate(const starcoder_model &model, const gpt_vocab &vocab,
std::vector<gpt_vocab::id> input_ids, int n_predict,
int top_k, float top_p, float temp, int n_threads,
int n_batch, std::mt19937 rng,
std::string stop_sequence = "") {
int n_past = 0;
int64_t t_sample_us = 0;
int64_t t_predict_us = 0;
std::vector<float> logits;
n_predict = std::min(n_predict, model.hparams.n_ctx - (int)input_ids.size());
// printf("%s: corrected n_predict = %d\n", __func__, n_predict);
std::string model_output_text = "";
std::vector<std::string> model_output_tokens;
// 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;
starcoder_eval(model, n_threads, 0, {0, 1, 2, 3}, logits, mem_per_token);
for (int i = embd.size(); i < input_ids.size() + n_predict; i++) {
// predict
if (embd.size() > 0) {
const int64_t t_start_us = ggml_time_us();
if (!starcoder_eval(model, n_threads, n_past, embd, logits,
mem_per_token)) {
// printf("Failed to predict\n");
// return 1;
// throw error
throw std::runtime_error("Failed to predict");
}
t_predict_us += ggml_time_us() - t_start_us;
}
n_past += embd.size();
embd.clear();
if (i >= input_ids.size()) {
// sample next token
// printf("%s: sampling token #%d (top_k = %d, top_p = %f, temp = %f)\n",
// __func__, i, top_k, top_p, 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
// printf("%s: processing input token #%d\n", __func__, i);
for (int k = i; k < input_ids.size(); k++) {
embd.push_back(input_ids[k]);
if (embd.size() >= n_batch) {
break;
}
}
i += embd.size() - 1;
}
// display text
for (auto id : embd) {
// printf("%s", vocab.id_to_token[id].c_str());
// use find instead of []
auto it = vocab.id_to_token.find(id);
if (it != vocab.id_to_token.end()) {
printf("%s", it->second.c_str());
model_output_text += it->second;
model_output_tokens.push_back(it->second);
} else {
// throw an error (wtf token?)
throw std::runtime_error("failed to decode token: " +
std::to_string(id));
}
}
fflush(stdout);
// handle end of text tokens
// check if model is santacoder
if (model.hparams.n_layer <= 30 && embd.back() == 49152) {
break;
}
// check if model is starcoder
else if (embd.back() == 0) { // TODO: this is only for starcoder
// printf("stopping generation due to end token\n");
break;
}
// handle specific stop sequence (see if output text ends with it)
if (stop_sequence.size() > 0) {
// check if output text ends with stop sequence
if (model_output_text.size() >= stop_sequence.size() &&
model_output_text.substr(model_output_text.size() -
stop_sequence.size(),
stop_sequence.size()) == stop_sequence) {
// printf("stopping generation due to stop sequence\n");
break;
}
}
}
// return model_output_text;
return model_output_tokens;
}
int main(int argc, char **argv) {
ggml_time_init();
const int64_t t_main_start_us = ggml_time_us();
gpt_params params;
params.model = "models/bigcode/gpt_bigcode-santacoder-ggml.bin";
if (gpt_params_parse(argc, argv, params) == false) {
return 1;
}
if (params.seed < 0) {
params.seed = time(NULL);
}
printf("%s: seed = %d\n", __func__, params.seed);
std::mt19937 rng(params.seed);
// if (params.prompt.empty()) {
// if (!isatty(STDIN_FILENO)) {
// std::string line;
// while (std::getline(std::cin, line)) {
// params.prompt = params.prompt + "\n" + line;
// }
// } else {
// params.prompt = gpt_random_prompt(rng);
// }
// }
int64_t t_load_us = 0;
gpt_vocab vocab;
starcoder_model model;
// load the model
{
const int64_t t_start_us = ggml_time_us();
if (!starcoder_model_load(params.model, model, vocab)) {
fprintf(stderr, "%s: failed to load model from '%s'\n", __func__,
params.model.c_str());
return 1;
}
t_load_us = ggml_time_us() - t_start_us;
}
// start http server
fprintf(stderr, "%s: starting http server on port %d\n", __func__,
params.http_server_port);
httplib::Server svr;
// POST /v1/starcoder/generate
// Input: { "prompt": "...", "n_predict": 200, "top_k": 40, "top_p": 0.9,
// "temp": 0.9, "stop_sequence": "..." } Output: { "text": "..." }
svr.Post("/v1/starcoder/generate", [model, vocab, params,
rng](const httplib::Request &req,
httplib::Response &res) {
try {
// read the request body as JSON
nlohmann::json req_json = nlohmann::json::parse(req.body);
// ensure prompt field is present
if (!req_json.contains("prompt")) {
throw std::runtime_error("missing prompt field");
}
std::string req_prompt = req_json["prompt"];
// all other fields are optional
int req_n_predict = req_json.value("n_predict", 200);
int req_top_k = req_json.value("top_k", 40);
float req_top_p = req_json.value("top_p", 0.9);
float req_temp = req_json.value("temp", 0.9);
std::string stop_sequence = req_json.value("stop_sequence", "");
// tokenize the prompt
std::vector<gpt_vocab::id> input_ids = ::gpt_tokenize(vocab, req_prompt);
printf("%s: prompt: '%s'\n", __func__, req_prompt.c_str());
printf("%s: prompt_n = %zu\n", __func__, input_ids.size());
printf("%s: n_predict = %d\n", __func__, req_n_predict);
printf("%s: top_k = %d\n", __func__, req_top_k);
printf("%s: top_p = %f\n", __func__, req_top_p);
printf("%s: temp = %f\n", __func__, req_temp);
// call model
std::vector<std::string> output_tokens = starcoder_demo_generate(
model, vocab, input_ids, req_n_predict, req_top_k, req_top_p,
req_temp, params.n_threads, params.n_batch, rng, stop_sequence);
printf("%s: output_n = %zu\n", __func__, output_tokens.size());
// // dump output tokens
// printf("%s: output: [", __func__);
// for (auto &token : output_tokens) {
// printf("%s, ", token.c_str());
// }
// printf("]\n");
std::string model_output;
// join tokens into a string
for (auto &token : output_tokens) {
model_output += token;
}
// create response json
nlohmann::json res_json;
res_json["text"] = model_output;
// set response content type
res.set_content(res_json.dump(), "application/json");
res.status = 200;
} catch (const std::exception &e) {
fprintf(stderr, "%s: exception: %s\n", __func__, e.what());
// res.set_content("{}", "application/json");
res.set_content(e.what(), "text/plain");
res.status = 400;
}
});
// listen on port
svr.listen("0.0.0.0", params.http_server_port);
ggml_free(model.ctx);
return 0;
}