-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.c
368 lines (284 loc) · 9.44 KB
/
tokenizer.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "tokenizer.h"
/*
Tokenizer which uses UTF-8
author: Diederik Mathijs
Based on video by Andrej Karpathy
run with: gcc tokenizer.c decoder.c encoder.c -o tokenizer && ./tokenizer < input.txt
*/
#define INTERMEDIATE_TOKENS_LIST_MAX_SIZE 4096u
// Unlike in GPT2, there's no collapsing of spaces, this is because negative lookahead is not included in POSIX regex
char *GPT_2_PATTERN_SPLITTER = "'s|'t|'re|'ve|'m|'ll|'d| ?[[:alpha:]]+| ?[[:digit:]]+| ?[^[:space:][:alpha:][:digit:]]+|[:space:]+";
unsigned *readTextFile();
VocabularyItem *getBaseVocabulary(unsigned *s);
VocabularyItem *buildVocabulary(unsigned *text);
void printVocabulary(VocabularyItem *vocabulary);
int compareTokenPairs(const void *a, const void *b);
unsigned *mergeTokenPairInText(unsigned *text, TokenPair *tokenPair, unsigned idx, int freeTxtPtr);
char **splitTextWithGPTRegex(unsigned *text);
unsigned *splitTextIntoTokens(unsigned *text, VocabularyItem *vocabulary);
unsigned getCharStringLength(unsigned *string);
extern unsigned *encode(char *string, VocabularyItem *vocabulary);
extern char *decode(unsigned *string, VocabularyItem *vocabulary);
extern TokenPair *getOrderedTokenBytePairs(unsigned *s);
int main()
{
unsigned *s = readTextFile();
VocabularyItem *vocabulary = buildVocabulary(s);
unsigned *ptr = encode("“हि”", vocabulary);
while (*ptr != '\0')
{
printf("%d\n", *ptr);
ptr++;
}
// printf("%s\n", decode(encode("“हि”", vocabulary), vocabulary));
// unsigned *tokenizedText = splitTextIntoTokens(s, vocabulary);
// unsigned *tokenizedTextPtr = tokenizedText;
// while (*tokenizedTextPtr != '\0')
// {
// printf("%d ", *tokenizedTextPtr);
// tokenizedTextPtr++;
// }
// printf("%s\n", decode(tokenizedText, vocabulary));
}
void printVocabulary(VocabularyItem *vocabulary)
{
VocabularyItem *vocabularyPtr = vocabulary;
for (int i = 0; i < VOCABULARY_SIZE; i++)
{
unsigned *concatenatedArray = malloc(sizeof(unsigned) * 100);
unsigned *concatenatedArrayPtr = concatenatedArray;
if (vocabularyPtr->is_pair)
{
*concatenatedArrayPtr = vocabularyPtr->vocabularyCharacter.pair.first;
concatenatedArrayPtr++;
*concatenatedArrayPtr = vocabularyPtr->vocabularyCharacter.pair.second;
concatenatedArrayPtr++;
}
else
{
*concatenatedArrayPtr = vocabularyPtr->vocabularyCharacter.character;
concatenatedArrayPtr++;
}
vocabularyPtr++;
*concatenatedArrayPtr = '\0';
char *result = decode(concatenatedArray, vocabulary);
printf("Vocabulary item %d: [%s]\n", i, result);
free(result);
free(concatenatedArray);
}
}
extern VocabularyItem *getBaseVocabulary(unsigned *s)
{
VocabularyItem *vocabulary = malloc(sizeof(VocabularyItem) * VOCABULARY_SIZE); // arbitrary number of tokens
VocabularyItem *vocabularyPtr = vocabulary;
for (int i = 0; i < 256; i++)
{
vocabularyPtr->is_pair = 0;
vocabularyPtr->vocabularyCharacter.character = i;
vocabularyPtr++;
}
return vocabulary;
}
int compareTokenPairs(const void *a, const void *b)
{
return (((TokenPair *)b)->occurrences - ((TokenPair *)a)->occurrences);
}
TokenPair *getOrderedTokenBytePairs(unsigned *s)
{
TokenPair *tokenPairs = malloc(sizeof(TokenPair) * INTERMEDIATE_TOKENS_LIST_MAX_SIZE); // arbitrary number of pairs
TokenPair *tokenPairsPtr = tokenPairs;
unsigned *stringPtr = s;
while (*stringPtr != '\0' && tokenPairsPtr - tokenPairs < INTERMEDIATE_TOKENS_LIST_MAX_SIZE)
{
unsigned char1 = *stringPtr;
if (*stringPtr++ == '\0')
break;
unsigned char2 = *stringPtr;
int found = 0;
for (int j = 0; j < tokenPairsPtr - tokenPairs; j++)
{
if (tokenPairs[j].first == char1 && tokenPairs[j].second == char2)
{
tokenPairs[j].occurrences += 1;
found = 1;
break;
}
}
if (!found)
{
tokenPairsPtr->first = char1;
tokenPairsPtr->second = char2;
tokenPairsPtr->occurrences = 1;
tokenPairsPtr++;
}
}
// t(qsort) < t(bubblesort). .3s difference for size 500 vocabulary.
qsort(tokenPairs, tokenPairsPtr - tokenPairs, sizeof(TokenPair), compareTokenPairs);
return tokenPairs;
}
unsigned *mergeTokenPairInText(unsigned *text, TokenPair *tokenPair, unsigned idx, int freeTextPtr)
{
unsigned *stringPtr = text;
unsigned *newString = malloc(sizeof(unsigned) * MAX_LENGTH);
unsigned *newStringPtr = newString;
int count = 0;
while (*stringPtr != '\0')
{
unsigned char1 = *stringPtr;
stringPtr++;
unsigned char2 = *stringPtr;
if (char1 == tokenPair->first && char2 == tokenPair->second)
{
*newStringPtr = idx;
newStringPtr++;
}
else
{
stringPtr--;
*newStringPtr = *stringPtr;
newStringPtr++;
}
stringPtr++;
count++;
}
*newStringPtr = '\0';
if (freeTextPtr)
{
free(text);
}
return newString;
}
VocabularyItem *buildVocabulary(unsigned *text)
{
// Vocabulary size can be higher because of UTF-8 encoding
// thus we need to compress the vocabulary to it's target size
// 256 characters are reserved for the first 256 ASCII characters
unsigned merges = VOCABULARY_SIZE - 256;
unsigned *txtPtr = text;
VocabularyItem *vocabulary = getBaseVocabulary(txtPtr);
unsigned originalLength = getCharStringLength(txtPtr);
for (int i = 0; i < merges; i++)
{
TokenPair *tokenPairs = getOrderedTokenBytePairs(txtPtr);
TokenPair *pairsPtr = tokenPairs;
TokenPair *max = &tokenPairs[0];
unsigned idx = 256 + i;
VocabularyItem *item = &vocabulary[idx];
item->is_pair = 1;
item->vocabularyCharacter.pair.first = max->first;
item->vocabularyCharacter.pair.second = max->second;
printf("Merging %d occurrences of: %d (%u, %u)\n", max->occurrences, idx, max->first, max->second);
// Don't free the original text
txtPtr = mergeTokenPairInText(txtPtr, max, idx, i > 1);
free(tokenPairs);
}
unsigned compressedLength = getCharStringLength(txtPtr);
printf("compression ratio: %0.2fX\n", (float)originalLength / (float)compressedLength);
return vocabulary;
}
char **splitTextWithGPTRegex(unsigned *text)
{
regex_t regex;
int status = regcomp(®ex, GPT_2_PATTERN_SPLITTER, REG_EXTENDED);
if (status != 0)
{
char error_message[100];
regerror(status, ®ex, error_message, sizeof(error_message));
fprintf(stderr, "Regex compilation failed: %s\n", error_message);
exit(EXIT_FAILURE);
}
// Calculate the length of the unsigned array
unsigned *sPtr = text;
// Allocate memory for the character array
char *charArray = malloc(MAX_LENGTH * sizeof(char));
char *charPtr = charArray;
// Copy unsigned integers to characters, discarding overflows
while (*sPtr != '\0')
{
// Check if the unsigned integer fits within the range of a char
if (*sPtr <= UCHAR_MAX && *sPtr >= 0)
{
*charPtr++ = (char)*sPtr;
}
else
{
// Handle overflow by setting to a default value
*charPtr++ = '?'; // For example, you can use '?' character
}
sPtr++;
}
charPtr = '\0';
// Execute the regex split
char *token;
char *str = strdup(charArray); // Duplicate the string because regexec modifies it
if (str == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
char **textGroupPointers = malloc(sizeof(char *) * MAX_LENGTH);
char **textGroupPointersPtr = textGroupPointers;
char *textGroups = malloc(sizeof(char) * MAX_LENGTH);
char *textGroupsPtr = textGroups;
while ((token = strsep(&str, " \t\n")) != NULL)
{
regmatch_t pmatch;
if (regexec(®ex, token, 1, &pmatch, 0) == 0)
{
*textGroupPointersPtr++ = textGroupsPtr;
strncpy(textGroupsPtr, token + pmatch.rm_so, pmatch.rm_eo - pmatch.rm_so);
printf("Match: %.*s|\n", pmatch.rm_eo - pmatch.rm_so, token + pmatch.rm_so);
textGroupsPtr += pmatch.rm_eo - pmatch.rm_so;
*textGroupsPtr++ = '\0';
}
}
*textGroupPointersPtr = NULL;
// Free the memory and compiled regex
free(str);
regfree(®ex);
return textGroupPointers;
}
unsigned *splitTextIntoTokens(unsigned *text, VocabularyItem *vocabulary)
{
char **arr = splitTextWithGPTRegex(text);
char **arrPtr = arr;
unsigned *concatenatedTokens = malloc(sizeof(unsigned) * MAX_LENGTH);
unsigned *concatenatedTokensPtr = concatenatedTokens;
while (*arrPtr != NULL)
{
unsigned *encodedString = encode((char *)*arrPtr, vocabulary);
unsigned *encodedStringPtr = encodedString;
while (*encodedStringPtr != '\0')
{
*concatenatedTokensPtr = *encodedStringPtr;
concatenatedTokensPtr++;
encodedStringPtr++;
}
free(encodedString);
arrPtr++;
}
*concatenatedTokensPtr = '\0';
free(*arr);
free(arr);
return concatenatedTokens;
}
unsigned getCharStringLength(unsigned *string)
{
unsigned *stringPtr = string;
unsigned length = 0;
while (*stringPtr != '\0')
{
length++;
stringPtr++;
}
return length;
}
unsigned *readTextFile()
{
unsigned *buffer = malloc(sizeof(unsigned) * MAX_LENGTH);
int index = 0;
while ((buffer[index++] = getchar()) != EOF)
;
buffer[index - 1] = '\0';
return buffer;
}