-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
197 lines (164 loc) · 5.73 KB
/
utils.cpp
File metadata and controls
197 lines (164 loc) · 5.73 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
#include "utils.h"
Tokenizer::Tokenizer(const std::string &fname) {
// fill _token_to_id
_json_parse(fname);
for (const auto & kv : _token_to_id) {
_id_to_token[kv.second] = kv.first;
}
}
std::string Tokenizer::_replace(const std::string &s, const std::string &from, const std::string &to) {
std::string result = s;
size_t pos = 0;
while ((pos = result.find(from, pos)) != std::string::npos) {
result.replace(pos, from.length(), to);
pos += to.length();
}
return result;
}
void Tokenizer::_json_parse(const std::string &fname) {
// read file into string
std::string json;
{
std::ifstream ifs(fname);
if (!ifs) {
std::cerr << "Failed to open " << fname;
exit(1);
}
json = std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
// parse json
{
bool has_key = false;
bool in_token = false;
std::string str_key;
std::string str_val;
u_int n = json.size();
for (int i = 1; i < n; ++i) {
if (!in_token) {
if (json[i] == ' ') continue;
if (json[i] == '"') {
in_token = true;
continue;
}
} else {
if (json[i] == '\\' && i+1 < n) {
if (!has_key) {
str_key += json[i];
} else {
str_val += json[i];
}
++i;
} else if (json[i] == '"') {
if (!has_key) {
has_key = true;
++i;
while (json[i] == ' ') ++i;
++i; // :
while (json[i] == ' ') ++i;
if (json[i] != '\"') {
while (json[i] != ',' && json[i] != '}') {
str_val += json[i++];
}
has_key = false;
} else {
in_token = true;
continue;
}
} else {
has_key = false;
}
str_key = _replace(str_key, "\\u0120", " " ); // \u0120 -> space
str_key = _replace(str_key, "\\u010a", "\n"); // \u010a -> new line
str_key = _replace(str_key, "\\\"", "\""); // \\\" -> "
_token_to_id[str_key] = std::stoi(str_val);
str_key = "";
str_val = "";
in_token = false;
continue;
}
if (!has_key) {
str_key += json[i];
} else {
str_val += json[i];
}
}
}
}
}
void Tokenizer::_split_words(std::string str, std::vector<std::string> &words) {
const std::string pattern = R"('s|'t|'re|'ve|'m|'ll|'d| ?[[:alpha:]]+| ?[[:digit:]]+| ?[^\s[:alpha:][:digit:]]+|\s+(?!\S)|\s+)";
const std::regex re(pattern);
std::smatch m;
while (std::regex_search(str, m, re)) {
for (auto x : m) {
words.push_back(x);
}
str = m.suffix();
}
}
std::vector<int> Tokenizer::encode(const std::string &text) {
std::vector<std::string> words;
// first split the text into words
_split_words(text, words);
// find the longest token that forms each word in words:
std::vector<int> tokens;
for (const auto & word : words) {
for (int i = 0; i < (int) word.size(); ){
for (int j = (int) word.size() - 1; j >= i; j--){
auto cand = word.substr(i, j-i+1);
auto it = _token_to_id.find(cand);
if (it != _token_to_id.end()){ // word.substr(i, j-i+1)
tokens.push_back(it->second);
i = j + 1;
break;
}
}
}
}
return tokens;
}
std::string Tokenizer::decode(const std::vector<int> & tokens) {
std::string result;
for (const auto& token : tokens) {
// Find the token in the id_to_token map
auto it = _id_to_token.find(token);
if (it != _id_to_token.end()) {
result += it->second; // Append the corresponding token text to the result
}
}
return result;
}
void storeFloatsToFile(const float* arr, int n, const std::string& filename) {
std::ofstream outFile(filename); // Open file for writing
if (!outFile) {
std::cerr << "Error opening file!" << std::endl;
return;
}
for (int i = 0; i < n; ++i) {
outFile << arr[i] << std::endl; // Write each float to the file
}
outFile.close(); // Close the file
}
float* read_checkpoint(const std::string& filename, size_t& total_params) {
std::ifstream infile(filename, std::ios::binary | std::ios::ate);
if (!infile) {
std::cerr << "Error opening weights file: " << filename << std::endl;
return nullptr;
}
// Get the size of the file
std::streamsize file_size = infile.tellg();
infile.seekg(0, std::ios::beg);
// Calculate the total number of floats
total_params = file_size / sizeof(float);
// Allocate memory for the weights
float* weights = new float[total_params];
// Read the weights into the array
if (!infile.read(reinterpret_cast<char*>(weights), file_size)) {
std::cerr << "Error reading weights from file: " << filename << std::endl;
delete[] weights;
return nullptr;
}
infile.close();
return weights;
}