MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / read_tests

Function read_tests

subprojects/llama.cpp/tests/test-tokenizer-0.cpp:61–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59using llama_tests = std::map<std::string, std::vector<llama_token>>;
60
61static llama_tests read_tests(const std::string & fname_inp, const std::string & fname_out) {
62 llama_tests tests;
63
64 std::ifstream ifs_inp(fname_inp);
65 if (!ifs_inp) {
66 fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_inp.c_str());
67 return tests;
68 }
69
70 std::string sraw((std::istreambuf_iterator<char>(ifs_inp)), std::istreambuf_iterator<char>());
71
72 std::ifstream ifs_out(fname_out);
73 if (!ifs_out) {
74 fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_out.c_str());
75 return tests;
76 }
77
78 std::vector<std::string> sout;
79 for (std::string line; std::getline(ifs_out, line);) {
80 sout.push_back(line);
81 }
82
83 const std::string sep = "\n__ggml_vocab_test__\n";
84
85 std::vector<std::string> sinp;
86
87 size_t pos = 0;
88 while (pos < sraw.size()) {
89 const size_t next = sraw.find(sep, pos);
90 if (next == std::string::npos) {
91 sinp.push_back(sraw.substr(pos));
92 break;
93 }
94 sinp.push_back(sraw.substr(pos, next - pos));
95 pos = next + sep.size();
96 }
97
98 if (sinp.size() != sout.size()) {
99 fprintf(stderr, "%s : error: input and output files have different number of tests\n", __func__);
100 return tests;
101 }
102
103 for (size_t i = 0; i < sinp.size(); ++i) {
104 const std::string & s = sinp[i];
105 const std::string & o = string_strip(sout[i]);
106
107 std::vector<llama_token> toks;
108
109 size_t pos = 0;
110 while (pos < o.size()) {
111 size_t next = o.find(' ', pos);
112 if (next == std::string::npos) {
113 next = o.size();
114 }
115 const std::string stok = o.substr(pos, next - pos);
116 toks.push_back(std::stoi(stok));
117 pos = next + 1;
118 }

Callers 1

mainFunction · 0.85

Calls 4

string_stripFunction · 0.85
sizeMethod · 0.65
findMethod · 0.65
push_backMethod · 0.45

Tested by

no test coverage detected