forked from a1k0n/a1gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpe_test.cpp
48 lines (45 loc) · 1.13 KB
/
bpe_test.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
#include "bpe.h"
int main(int argc, char **argv) {
BPEDecoder decoder;
if (!decoder.Init("model/vocab.bin")) {
printf("failed to init decoder\n");
return 1;
}
BPEEncoder encoder;
if (!encoder.Init(decoder.vocab_)) {
printf("failed to init encoder\n");
return 1;
}
const char *prompt = "The rain in spain falls mainly on the";
if (argc > 1) {
prompt = argv[1];
}
int outbuf[1024];
int ntokens;
const char *leftover = encoder.Encode(prompt, outbuf, 1024, &ntokens);
printf("encoding: ");
for (int i = 0; i < ntokens; i++) {
printf("%d ", outbuf[i]);
}
printf("\n");
char outbuf2[256];
decoder.Decode(outbuf, ntokens, outbuf2, 256);
printf("re-decoded: %s\n", outbuf2);
{
// test partial input
const char *prompt = "The rain in spain falls mainly on the";
for (;;) {
int buf[4];
prompt = encoder.Encode(prompt, buf, 4, &ntokens);
printf("partial-encoding(%d): ", ntokens);
for (int i = 0; i < ntokens; i++) {
printf("%d ", buf[i]);
}
if (!*prompt) {
break;
}
printf("\nleftover: %s\n", prompt);
}
}
return 0;
}