forked from guidance-ai/llguidance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_sample.cpp
More file actions
288 lines (250 loc) · 9.38 KB
/
Copy pathc_sample.cpp
File metadata and controls
288 lines (250 loc) · 9.38 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
277
278
279
280
281
282
283
284
285
286
287
288
#include <cstdio>
#include <cstdint>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <cassert>
#include <cstring>
#include "llguidance.h"
// Create an LlgTokenizer using the v2 API.
// eos_tokens[0] is the primary EOS; any remaining entries are extra EOS token IDs.
LlgTokenizer *create_tokenizer_v2(std::vector<std::vector<uint8_t>> &tokens,
std::vector<uint32_t> eos_tokens,
LlgTokenizeFn tokenize_fn,
const void *tokenize_user_data) {
assert(!eos_tokens.empty());
std::vector<uint32_t> token_lens(tokens.size());
size_t total_size = 0;
for (size_t i = 0; i < tokens.size(); i++) {
token_lens[i] = tokens[i].size();
total_size += token_lens[i];
}
std::vector<uint8_t> token_bytes(total_size);
size_t offset = 0;
for (size_t i = 0; i < tokens.size(); i++) {
std::copy(tokens[i].begin(), tokens[i].end(), token_bytes.data() + offset);
offset += token_lens[i];
}
LlgTokenizerInitV2 tok_init = {};
tok_init.struct_size = sizeof(tok_init);
tok_init.vocab_size = (uint32_t)tokens.size();
tok_init.tok_eos = eos_tokens[0];
tok_init.token_lens = token_lens.data();
tok_init.token_bytes = token_bytes.data();
tok_init.tokenize_assumes_string = false;
tok_init.tokenize_user_data = tokenize_user_data;
tok_init.tokenize_fn = tokenize_fn;
if (eos_tokens.size() > 1) {
tok_init.tok_eos_extra = eos_tokens.data() + 1;
tok_init.tok_eos_extra_count = (uint32_t)(eos_tokens.size() - 1);
}
char error_buf[128];
auto tok = llg_new_tokenizer_v2(&tok_init, error_buf, sizeof(error_buf));
if (tok == nullptr) {
printf("Error (v2): %s\n", error_buf);
exit(1);
}
return tok;
}
// Create an LlgTokenizer; tokens[token_id] is a byte sequence corresponding to
// given token_id; see below for tokenize_fn
LlgTokenizer *create_tokenizer(std::vector<std::vector<uint8_t>> &tokens,
uint32_t tok_eos, LlgTokenizeFn tokenize_fn,
const void *tokenize_user_data) {
std::vector<uint32_t> token_lens(tokens.size());
size_t total_size = 0;
for (size_t i = 0; i < tokens.size(); i++) {
token_lens[i] = tokens[i].size();
total_size += token_lens[i];
}
std::vector<uint8_t> token_bytes(total_size);
size_t offset = 0;
for (size_t i = 0; i < tokens.size(); i++) {
std::copy(tokens[i].begin(), tokens[i].end(), token_bytes.data() + offset);
offset += token_lens[i];
}
LlgTokenizerInit tok_init = {};
tok_init.vocab_size = (uint32_t)tokens.size();
tok_init.tok_eos = tok_eos;
tok_init.token_lens = token_lens.data();
tok_init.token_bytes = token_bytes.data();
tok_init.tokenize_assumes_string = false;
tok_init.tokenize_user_data = tokenize_user_data;
tok_init.tokenize_fn = tokenize_fn;
char error_buf[128];
auto tok = llg_new_tokenizer(&tok_init, error_buf, sizeof(error_buf));
if (tok == nullptr) {
printf("Error: %s\n", error_buf);
exit(1);
}
return tok;
}
// This function assumes that each byte is a single token.
// You want to replace this. This has to be thread-safe!
std::vector<uint32_t> bogus_tokenize(const uint8_t *bytes_ptr, size_t nbytes) {
std::vector<uint32_t> token_ids;
for (size_t i = 0; i < nbytes; i++) {
token_ids.push_back(bytes_ptr[i]);
}
return token_ids;
}
// This wraps a C++-style "bogus_tokenize()" in a way llg wants it.
size_t tokenize_callback(const void *user_data, const uint8_t *bytes,
size_t bytes_len, uint32_t *output_tokens,
size_t output_tokens_len) {
(void)user_data;
auto tokens = bogus_tokenize(bytes, bytes_len);
if (output_tokens_len > 0) {
auto n = std::min(output_tokens_len, tokens.size());
std::copy(tokens.begin(), tokens.begin() + n, output_tokens);
}
return tokens.size();
}
// This creates a tokenizer that treats each byte as a token.
LlgTokenizer *create_byte_tokenizer(void) {
std::vector<std::vector<uint8_t>> tokens;
tokens.reserve(257); // 256 byte tokens + 1 EOS
// every byte is a token
for (size_t i = 0; i < 256; i++) {
tokens.push_back({(uint8_t)i});
}
const char *eos = "<EOS>";
tokens.push_back(std::vector<uint8_t>(eos, eos + strlen(eos)));
return create_tokenizer(tokens, tokens.size() - 1, tokenize_callback,
nullptr);
}
// Same as above but using the v2 API with an extra (unused) EOS token.
LlgTokenizer *create_byte_tokenizer_v2(void) {
std::vector<std::vector<uint8_t>> tokens;
tokens.reserve(258); // 256 byte tokens + 2 EOS
for (size_t i = 0; i < 256; i++) {
tokens.push_back({(uint8_t)i});
}
const char *eos = "<EOS>";
tokens.push_back(std::vector<uint8_t>(eos, eos + strlen(eos)));
const char *eos2 = "<EOS2>";
tokens.push_back(std::vector<uint8_t>(eos2, eos2 + strlen(eos2)));
// EOS tokens: token 256 (<EOS>) is primary, token 257 (<EOS2>) is extra
std::vector<uint32_t> eos_tokens = {(uint32_t)(tokens.size() - 2),
(uint32_t)(tokens.size() - 1)};
return create_tokenizer_v2(tokens, eos_tokens, tokenize_callback, nullptr);
}
LlgTokenizer *create_hf_tokenizer(std::string tokenizer_json,
uint32_t tok_eos) {
LlgTokenizerInit tok_init = {};
tok_init.tok_eos = tok_eos;
tok_init.use_approximate_greedy_tokenize_fn = true;
tok_init.tokenizer_json = tokenizer_json.c_str();
char error_buf[128];
auto tok = llg_new_tokenizer(&tok_init, error_buf, sizeof(error_buf));
if (tok == nullptr) {
printf("Error: %s\n", error_buf);
exit(1);
}
return tok;
}
std::string read_file(const std::string &filePath) {
std::ifstream file(filePath);
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
void fail_constraint(LlgConstraint *c) {
printf("Error: %s\n", llg_get_error(c));
llg_free_constraint(c);
exit(1);
}
std::vector<uint32_t> do_llg_tokenize(const LlgTokenizer *tok, std::string s) {
std::vector<uint32_t> tokens;
size_t n_tokens =
llg_tokenize_bytes(tok, (const uint8_t *)s.c_str(), s.size(), nullptr, 0);
tokens.resize(n_tokens);
llg_tokenize_bytes(tok, (const uint8_t *)s.c_str(), s.size(), tokens.data(),
n_tokens);
return tokens;
}
std::string do_llg_stringify_tokens(const LlgTokenizer *tok,
std::vector<uint32_t> tokens) {
char buffer[1024];
size_t n_bytes = llg_stringify_tokens(tok, tokens.data(), tokens.size(),
buffer, sizeof(buffer));
if (n_bytes >= sizeof(buffer)) {
char *new_buffer = new char[n_bytes + 1];
llg_stringify_tokens(tok, tokens.data(), tokens.size(), new_buffer,
n_bytes + 1);
auto r = std::string(new_buffer);
delete[] new_buffer;
return r;
} else {
return std::string(buffer);
}
}
void run_constraint_test(LlgTokenizer *tokenizer, const std::string &schema_json,
const std::string &sample_json, const char *label) {
LlgConstraintInit init;
llg_constraint_init_set_defaults(&init, tokenizer);
init.log_stderr_level = 0; // default to 1 (warnings only)
LlgConstraint *c = llg_new_constraint(&init, schema_json.c_str());
// this is a very common place where errors can happen - for example the
// schema was invalid
if (llg_get_error(c)) {
fail_constraint(c);
}
// we assume our "LLM" will generate these tokens
auto tokens = do_llg_tokenize(tokenizer, sample_json);
LlgMaskResult mask_res;
for (size_t i = 0; i < tokens.size(); i++) {
// compute mask - this can be done with parallel with logit generation
if (llg_compute_mask(c, &mask_res) != 0) {
fail_constraint(c);
}
// here, we would normally sample constrained to mask_res.sample_mask
// using mask_res.temperature
uint32_t token = tokens[i];
// make sure token is in the mask
assert(mask_res.sample_mask[token / 32] & (1 << (token % 32)));
// here we commit the token
// if "ff_tokens" are enabled, this can return more than one token
// to fast-forward
LlgCommitResult commit_res;
if (llg_commit_token(c, tokens[i], &commit_res) != 0) {
fail_constraint(c);
}
// we didn't enable ff_tokens, so the exact token that we passed should be
// returned
assert(commit_res.n_tokens == 1);
assert(commit_res.tokens[0] == token);
}
if (llg_compute_mask(c, &mask_res) != 0) {
fail_constraint(c);
}
// we assume the constraint will force EOS at the end of the input
assert(mask_res.is_stop);
llg_free_constraint(c);
printf("%s: OK!\n", label);
}
int main(int argc, const char *argv[]) {
if (argc < 3) {
printf("Usage: %s <schema.ll.json> <sample.json> [tokenizer.json]\n",
argv[0]);
return 1;
}
auto schema_json = read_file(argv[1]);
auto sample_json = read_file(argv[2]);
// Test with v1 API (LlgTokenizerInit + llg_new_tokenizer)
{
LlgTokenizer *tokenizer = argc > 3
? create_hf_tokenizer(read_file(argv[3]), 2)
: create_byte_tokenizer();
run_constraint_test(tokenizer, schema_json, sample_json, "v1");
llg_free_tokenizer(tokenizer);
}
// Test with v2 API (LlgTokenizerInitV2 + llg_new_tokenizer_v2)
{
LlgTokenizer *tokenizer = create_byte_tokenizer_v2();
run_constraint_test(tokenizer, schema_json, sample_json, "v2");
llg_free_tokenizer(tokenizer);
}
return 0;
}