-
Notifications
You must be signed in to change notification settings - Fork 11
/
replit.cpp
190 lines (148 loc) · 4.69 KB
/
replit.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
// https://github.com/ggerganov/ggml/pull/145
#include "ggml.h"
#include "common-ggml.h"
#include "common.h"
#include "replit.h"
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <stdint.h>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ggml.cpp/examples/replit/main.cpp"
struct replit_state {
replit_tokenizer vocab;
replit_model model;
struct {
int64_t t_load_us = -1;
int64_t t_sample_us = -1;
int64_t t_predict_us = -1;
} timing;
};
int replit_predict(void* params_ptr, void* state_pr, char* result) {
gpt_params params = *(gpt_params*) params_ptr;
replit_state state = *(replit_state*) state_pr;
replit_tokenizer vocab = state.vocab;
replit_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<std::size_t> embd_inp =
replit_tokenizer_tokenize(vocab, params.prompt);
printf("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
for (int i = 0; i < embd_inp.size(); i++) {
printf("%s: token[%d] = %6d\n", __func__, i, embd_inp[i]);
// vocab.id_to_token.at(embd_inp[i]).c_str()
}
printf("\n");
params.n_predict = std::min(params.n_predict,
model.hparams.max_seq_len - (int)embd_inp.size());
std::vector<gpt_vocab::id> embd;
std::string res = "";
// determine the required inference memory per token:
size_t mem_per_token = 0;
replit_eval(model, params.n_threads, 0, {0, 1, 2, 3}, logits, false, 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 (!replit_eval(model, params.n_threads, n_past, embd, logits, false, 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.raw_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 += replit_tokenizer_detokenize(vocab, {static_cast<std::size_t>(id)})
.c_str();
}
// end of text token
if (embd.back() == 0) {
break;
}
}
strcpy(result, res.c_str());
return 0;
}
int replit_bootstrap(const char *model_path, void* state_pr)
// load the model
{
ggml_time_init();
replit_state* state = (replit_state*) state_pr;
const int64_t t_start_us = ggml_time_us();
if (!replit_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* replit_allocate_state() {
return new replit_state;
}
void replit_free_model(void *state_ptr) {
replit_state* state = (replit_state*) state_ptr;
ggml_free(state->model.ctx);
}
void replit_free_params(void* params_ptr) {
gpt_params* params = (gpt_params*) params_ptr;
delete params;
}
void* replit_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;
}