-
Notifications
You must be signed in to change notification settings - Fork 50
/
test-tokenizer.cpp
81 lines (65 loc) · 2.53 KB
/
test-tokenizer.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
#include <cstdio>
#include <string>
#include <map>
#include <vector>
#define BARK_API_INTERNAL
#include "bark.h"
struct bark_vocab {
using id = int32_t;
using token = std::string;
std::map<token, id> token_to_id;
std::map<id, token> id_to_token;
};
static const std::map<std::string, bark_sequence> & k_tests()
{
static std::map<std::string, bark_sequence> _k_tests = {
{ "Hello world!", { 31178, 11356, 106, }, },
{ "Hello world", { 31178, 11356, }, },
{ " Hello world!", { 31178, 11356, 106, }, },
{ "this is an audio generated by bark", { 10531, 10124, 10151, 23685, 48918, 10155, 18121, 10174, }, },
{ "we are living on earth", { 11951, 10301, 14625, 10135, 39189 }, }
};
return _k_tests;
};
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <model-file>\n", argv[0]);
return 1;
}
const std::string fname = argv[1];
fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
bark_vocab vocab;
int max_ctx_size = 256;
if (bark_vocab_load(fname.c_str(), &vocab, 119547) > 0) {
fprintf(stderr, "%s: invalid vocab file '%s'\n", __func__, fname.c_str());
return 1;
}
for (const auto & test_kv : k_tests()) {
bark_sequence res(test_kv.first.size());
int n_tokens;
bert_tokenize(&vocab, test_kv.first.c_str(), res.data(), &n_tokens, max_ctx_size);
res.resize(n_tokens);
bool correct = res.size() == test_kv.second.size();
for (int i = 0; i < (int) res.size() && correct; ++i) {
if (res[i] != test_kv.second[i]) {
correct = false;
}
}
if (!correct) {
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
fprintf(stderr, "%s : expected tokens: ", __func__);
for (const auto & t : test_kv.second) {
fprintf(stderr, "%6d, ", t);
}
fprintf(stderr, "\n");
fprintf(stderr, "%s : got tokens: ", __func__);
for (const auto & t : res) {
fprintf(stderr, "%6d, ", t);
}
fprintf(stderr, "\n");
return 3;
}
}
fprintf(stderr, "%s : tests passed successfully.\n", __func__);
return 0;
}