-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpt-neox.cpp
367 lines (326 loc) · 10.3 KB
/
gpt-neox.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/***
gpt-neox.cpp
C++ API to GPT-NeoX-20B, a large language model (LLM) trained by EluthierAI.
Can be used for other EleutherAI LLMs with the same run harness.
Copyright (c) 2022, Chris Street.
***/
#ifdef CODEHAPPY_NATIVE
// NB: This is largely obsolete now; the ggml library is perfectly capable of running
// GPT-NeoX inference natively.
/* path should have the trailing slash */
#define API_SCRIPT_PATH "/home/exx/ml/gpt-neox/"
/* API.yml is a config created by set_max_output_token_length() below. 20B.yml is the GPT-NeoX-20B model config. */
#define API_SCRIPT_CMD "python ./deepy.py generate.py -d configs 20B.yml API.yml" DEV_NULL
#define CONDITIONING_IN "sample_input.txt"
#define RESPONSE_OUT "sample_output.txt"
/* String replacement: placeholders (@1, @2, @3, etc.) are filled from the vector of strings. */
void placeholder_string(std::string& format_str, std::vector<std::string>& replacements, char replace_char) {
char str[16];
std::string s;
for (int e = 0; e < replacements.size(); ++e) {
sprintf(str, "%c%d", replace_char, e + 1);
s = str;
string_replace(format_str, s, replacements[e]);
}
/* Allow simply '@' to stand in for the first replacement string */
str[0] = replace_char;
str[1] = '\000';
s = str;
string_replace(format_str, s, replacements[0]);
}
/* String replacement: in string 's', replace all instances of string 'f' with 'r' */
void string_replace(std::string& s, const std::string& f, const std::string& r) {
size_t pos = 0;
forever {
pos = s.find(f, pos);
if (pos == std::string::npos) {
break;
}
s.erase(pos, f.length());
s.insert(pos, r);
pos += r.length();
}
}
void string_replace(std::string& s, const char* f, const char* r) {
std::string ff, rr;
ff = f;
rr = r;
string_replace(s, ff, rr);
}
/* Convert escaped C-style characters (\n for newline, etc.) in a string. */
void string_convert_c_escaped(std::string& s) {
string_replace(s, "\\n", "\n");
string_replace(s, "\\t", "\t");
string_replace(s, "\\r", "\r");
string_replace(s, "\\a", "\a");
string_replace(s, "\\b", "\b");
string_replace(s, "\\f", "\f");
string_replace(s, "\\v", "\v");
string_replace(s, "\\\\", "\\");
string_replace(s, "\\'", "'");
string_replace(s, "\\\"", "\"");
string_replace(s, "\\?", "?");
/* let's replace some Unicode characters: umlauts, diareses, es-stett, etc. */
string_replace(s, "\\u00a1", "¡");
string_replace(s, "\\u00a2", "¢");
string_replace(s, "\\u00a3", "£");
string_replace(s, "\\u00a4", "¤");
string_replace(s, "\\u00a5", "¥");
string_replace(s, "\\u00a6", "|");
string_replace(s, "\\u00a7", "§");
string_replace(s, "\\u00a8", "¨");
string_replace(s, "\\u00a9", "©");
string_replace(s, "\\u00aa", "ª");
string_replace(s, "\\u00ab", "«");
string_replace(s, "\\u00ac", "¬");
string_replace(s, "\\u00ad", "--");
string_replace(s, "\\u00ae", "®");
string_replace(s, "\\u00af", "¯");
string_replace(s, "\\u00b0", "°");
string_replace(s, "\\u00b1", "±");
string_replace(s, "\\u00b2", "²");
string_replace(s, "\\u00b3", "³");
string_replace(s, "\\u00b4", "´");
string_replace(s, "\\u00b5", "µ");
string_replace(s, "\\u00b6", "¶");
string_replace(s, "\\u00b7", "·");
string_replace(s, "\\u00b8", "¸");
string_replace(s, "\\u00b9", "¹");
string_replace(s, "\\u00ba", "º");
string_replace(s, "\\u00bb", "»");
string_replace(s, "\\u00bd", "½");
string_replace(s, "\\u00bc", "¼");
string_replace(s, "\\u00be", "¾");
string_replace(s, "\\u00bf", "¿");
string_replace(s, "\\u00c0", "À");
string_replace(s, "\\u00c1", "Á");
string_replace(s, "\\u00c2", "Â");
string_replace(s, "\\u00c3", "Ã");
string_replace(s, "\\u00c4", "Ä");
string_replace(s, "\\u00c5", "Å");
string_replace(s, "\\u00c6", "Æ");
string_replace(s, "\\u00c7", "Ç");
string_replace(s, "\\u00c8", "È");
string_replace(s, "\\u00c9", "É");
string_replace(s, "\\u00ca", "Ê");
string_replace(s, "\\u00cb", "Ë");
string_replace(s, "\\u00cc", "Ì");
string_replace(s, "\\u00cd", "Í");
string_replace(s, "\\u00ce", "Î");
string_replace(s, "\\u00cf", "Ï");
string_replace(s, "\\u00d0", "Ð");
string_replace(s, "\\u00d1", "Ñ");
string_replace(s, "\\u00d2", "Ò");
string_replace(s, "\\u00d3", "Ó");
string_replace(s, "\\u00d4", "Ô");
string_replace(s, "\\u00d5", "Õ");
string_replace(s, "\\u00d6", "Ö");
string_replace(s, "\\u00d7", "×");
string_replace(s, "\\u00d8", "Ø");
string_replace(s, "\\u00d9", "Ù");
string_replace(s, "\\u00da", "Ú");
string_replace(s, "\\u00db", "Û");
string_replace(s, "\\u00dc", "Ü");
string_replace(s, "\\u00dd", "Ý");
string_replace(s, "\\u00de", "Þ");
string_replace(s, "\\u00df", "ß");
string_replace(s, "\\u00e0", "à");
string_replace(s, "\\u00e1", "á");
string_replace(s, "\\u00e2", "â");
string_replace(s, "\\u00e3", "ã");
string_replace(s, "\\u00e4", "ä");
string_replace(s, "\\u00e5", "å");
string_replace(s, "\\u00e6", "æ");
string_replace(s, "\\u00e7", "ç");
string_replace(s, "\\u00e8", "è");
string_replace(s, "\\u00e9", "é");
string_replace(s, "\\u00ea", "ê");
string_replace(s, "\\u00eb", "ë");
string_replace(s, "\\u00ec", "ì");
string_replace(s, "\\u00ed", "í");
string_replace(s, "\\u00ee", "î");
string_replace(s, "\\u00ef", "ï");
string_replace(s, "\\u00f0", "ð");
string_replace(s, "\\u00f1", "ñ");
string_replace(s, "\\u00f2", "ò");
string_replace(s, "\\u00f3", "ó");
string_replace(s, "\\u00f4", "ô");
string_replace(s, "\\u00f5", "õ");
string_replace(s, "\\u00f6", "ö");
string_replace(s, "\\u00f7", "÷");
string_replace(s, "\\u00f8", "ø");
string_replace(s, "\\u00f9", "ù");
string_replace(s, "\\u00fa", "ú");
string_replace(s, "\\u00fb", "û");
string_replace(s, "\\u00fc", "ü");
string_replace(s, "\\u00fd", "ý");
string_replace(s, "\\u00fe", "þ");
string_replace(s, "\\u00ff", "ÿ");
string_replace(s, "\\u2013", "--");
string_replace(s, "\\u2014", "---");
string_replace(s, "\\u2018", "'");
string_replace(s, "\\u2019", "'");
string_replace(s, "\\u201c", "\"");
string_replace(s, "\\u201d", "\"");
string_replace(s, "\\u2026", "...");
// TODO: Octal, hexadecimal, Unicode characters?
}
/* Convert http entities in a string. */
void string_convert_http_entities(std::string& s) {
std::unordered_map<std::string, std::string> conversions = {
{ " ", " " }, { "<", "<" }, { ">", ">", }, { "&", "&" },
{ """, "\"" }, { "'", "'" }, { "¢", "\242" }, { "£", "\243" },
{ "¤", "\244" }, { "¥", "\245" }, { "¦", "|" }, { "§", "\247" },
{ "¨", "\250" }, { "©", "\251" }, { "ª", "\252" }, { "«", "\253" },
{ "¬", "\254" }, { "­", "\255" }, { "®", "\256" }, { "¯", "\257" }, { "°", "\260" },
{ "±", "\261" }, { "²", "\262" }, { "³", "\263" }, { "´", "\264" }, { "µ", "\265" },
{ "¶", "\266" }, { "¸", "\267" }, { "¹", "\270" }, { "º", "\271" }, { "»", "\272" },
{ "¼", "\273" }, { "½", "\274" }, { "¾", "\275" }, { "¿", "\276" }, { "×", "\327" },
{ "÷", "\367" },
};
for (const auto& e : conversions) {
string_replace(s, e.first, e.second);
}
}
GPTNeoX::GPTNeoX() {
max_out_tokens = 300;
generate_api_yml();
}
void GPTNeoX::execute_script() const {
if (FileExists(RESPONSE_OUT)) {
remove(RESPONSE_OUT);
}
system(API_SCRIPT_CMD);
}
/* Prompt with the passed-in string; return the response. */
GPTResponse GPTNeoX::prompt(const std::string& p) const {
std::ofstream o;
std::vector<GPTResponse> rs;
SaveCurDir();
ChangeDir(API_SCRIPT_PATH);
o.open(CONDITIONING_IN);
o << p << "\n";
o.close();
o.clear();
execute_script();
parse_responses(rs);
assert(rs.size() >= 1);
RestoreCurDir();
return rs[0];
}
/* Prompt with the passed-in string n times; fill the vector r with the responses. */
void GPTNeoX::prompt(const std::string& p, int np, std::vector<GPTResponse>& r) const {
std::ofstream o;
if (0 == np)
return;
SaveCurDir();
ChangeDir(API_SCRIPT_PATH);
o.open(CONDITIONING_IN);
for (int e = 0; e < np; ++e)
o << p << "\n";
o.close();
o.clear();
execute_script();
parse_responses(r);
assert(r.size() >= np);
RestoreCurDir();
}
/* Prompt with a vector of strings; fill the vector r with the responses. */
void GPTNeoX::prompt(std::vector<std::string> ps, std::vector<GPTResponse>& r) const {
std::ofstream o;
if (ps.empty())
return;
SaveCurDir();
ChangeDir(API_SCRIPT_PATH);
o.open(CONDITIONING_IN);
for (const auto& s : ps)
o << s << "\n";
o.close();
o.clear();
execute_script();
parse_responses(r);
assert(r.size() >= ps.size());
RestoreCurDir();
}
void GPTNeoX::parse_responses(std::vector<GPTResponse>& r) const {
SaveCurDir();
ChangeDir(API_SCRIPT_PATH);
if (!FileExists(RESPONSE_OUT)) {
RestoreCurDir();
return;
}
RamFile rf(RESPONSE_OUT, RAMFILE_READONLY);
char* w = (char *)rf.buffer(), *w2;
GPTResponse rr;
forever {
w = strstr(w, "{\"context\"");
BREAK_NULL(w);
w += 12;
if (*w == '\"')
++w;
w2 = strstr(w, "\", \"text");
BREAK_NULL(w2);
*w2 = '\000';
rr.context = w;
w = w2 + 12;
w2 = strstr(w, "\", \"length\": ");
BREAK_NULL(w2);
*w2 = '\000';
rr.completion = w;
w = w2 + 13;
rr.length = atoi(w);
w2 = strstr(w, "finished\": false,");
rr.finished = is_null(w2);
rr.message.clear();
w2 = strstr(w, "\"message\": null");
if (is_null(w2)) {
w2 = strstr(w, "\"message\": \"");
if (!is_null(w2)) {
w = w2 + 12;
w2 = strstr(w, "\", \"duration_");
BREAK_NULL(w2);
*w2 = '\000';
rr.message = w;
w = w2 + 1;
}
}
w = strstr(w, ", \"duration_");
BREAK_NULL(w);
w += 22;
rr.duration_sec = atof(w);
r.push_back(rr);
}
RestoreCurDir();
}
void GPTNeoX::generate_api_yml() const {
std::ofstream o;
SaveCurDir();
ChangeDir(API_SCRIPT_PATH "configs/");
o.open("API.yml");
o << R"foo(
{
"text-gen-type": "input-file",
"maximum_tokens": )foo";
o << max_out_tokens << ",\n";
o << R"foo(
"temperature": 1.0,
"top_p": 0.0,
"top_k": 0,
"recompute": false,
"num-samples": 10,
"sample-input-file": "sample_input.txt",
"sample-output-file": "sample_output.txt",
}
)foo";
o.close();
o.clear();
RestoreCurDir();
}
void GPTNeoX::set_max_output_token_length(int mot) {
if (mot != max_out_tokens) {
max_out_tokens = mot;
generate_api_yml();
}
}
#endif // CODEHAPPY_NATIVE
/* end gpt-neox.cpp */