-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtools_tokenize.cpp
More file actions
63 lines (61 loc) · 1.94 KB
/
Copy pathtools_tokenize.cpp
File metadata and controls
63 lines (61 loc) · 1.94 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
#include "memvanta/gguf.hpp"
#include "memvanta/llama_model.hpp"
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
static std::string hex(const std::string& s) {
std::ostringstream o;
o << std::hex << std::setfill('0');
for (unsigned char c : s)
o << std::setw(2) << static_cast<unsigned>(c);
return o.str();
}
static std::string unhex(const std::string& h) {
if (h.size() % 2)
throw std::runtime_error("hex input must have even length");
std::string s;
s.reserve(h.size() / 2);
for (std::size_t i = 0; i < h.size(); i += 2) {
unsigned v = 0;
std::istringstream is(h.substr(i, 2));
is >> std::hex >> v;
if (is.fail())
throw std::runtime_error("invalid hex input");
s.push_back(static_cast<char>(v));
}
return s;
}
int main(int argc, char** argv) {
if (argc < 3) {
std::cerr << "usage: memvanta_tokenize <tokenizer.gguf> <text>|--hex-input <hex> [--bos]\n";
return 2;
}
try {
std::string text;
int opt = 3;
if (std::string(argv[2]) == "--hex-input") {
if (argc < 4)
throw std::runtime_error("--hex-input requires data");
text = unhex(argv[3]);
opt = 4;
} else
text = argv[2];
bool bos = argc > opt && std::string(argv[opt]) == "--bos";
memvanta::GgufFile f(argv[1]);
memvanta::GgufTokenizer t(f);
auto ids = t.encode(text, bos);
auto dec = t.decode(ids);
std::cout << "model=" << t.model_type() << " pre=" << t.pre_type() << "\nids=";
for (std::size_t i = 0; i < ids.size(); ++i) {
if (i)
std::cout << ',';
std::cout << ids[i];
}
std::cout << "\ndecoded_hex=" << hex(dec) << "\n";
} catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
}