Repository navigation
Expand file tree
/
Copy pathtokenizer.zig
More file actions
1412 lines (1282 loc) · 56.2 KB
/
Copy pathtokenizer.zig
File metadata and controls
1412 lines (1282 loc) · 56.2 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! GPT-2-style byte-level BPE tokenizer (std-only), fed either from a GGUF
//! file (`tokenizer.ggml.*` metadata) or from an HF `tokenizer.json`.
//!
//! Encoding pipeline (matching the reference GPT-2 / llama.cpp behaviour):
//! 1. optional BOS
//! 2. special/added tokens are matched literally on the raw text
//! 3. the remaining spans are pre-tokenized with an approximation of the
//! GPT-2 regex `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`
//! 4. each pre-token's bytes are mapped through the GPT-2 byte->unicode
//! table (so a space becomes `Ġ`, U+0120)
//! 5. BPE merges are applied by merge rank
//! 6. symbols are looked up in the vocabulary
//!
//! `decode` concatenates token strings and reverses the byte->unicode mapping,
//! skipping CONTROL tokens (BOS/EOS and other specials), so
//! `decode(encode(text)) == text` for ordinary text.
//!
//! Documented limitations:
//! * `tokenizer.ggml.pre` is ignored: the GPT-2 pre-tokenizer above is used
//! for every model. For "llama-bpe"/"qwen2" vocabularies the difference is
//! that those split digit runs into single digits, so numbers may tokenize
//! into fewer tokens than the reference implementation produces.
//! * `\p{L}`/`\p{N}`/`\s` are approximated by the ranges in `isLetter`,
//! `isDigit` and `isWhitespace` (exact for ASCII, covering the major Unicode
//! blocks otherwise).
//! * BPE merges are applied with the standard rank-ordered algorithm, which is
//! quadratic in pre-token length; pre-tokens are word-sized, so this is fine
//! for ordinary text.
const std = @import("std");
const Allocator = std.mem.Allocator;
const Io = std.Io;
const gguf = @import("gguf.zig");
/// `tokenizer.ggml.token_type` values.
pub const TokenType = enum(u32) {
undefined = 0,
normal = 1,
unknown = 2,
control = 3,
user_defined = 4,
unused = 5,
byte = 6,
_,
/// Tokens that `decode` skips and that `encode` matches literally.
pub fn isSpecial(self: TokenType) bool {
return self == .control or self == .user_defined;
}
};
/// GPT-2 byte -> unicode codepoint table: printable bytes map to themselves,
/// the remaining 68 bytes map to U+0100.. (so byte 32/space becomes U+0120 Ġ).
pub const byte_to_unicode: [256]u21 = blk: {
var map: [256]u21 = @splat(0);
var used: [256]bool = @splat(false);
for (33..127) |b| {
map[b] = @intCast(b);
used[b] = true;
}
for (161..173) |b| {
map[b] = @intCast(b);
used[b] = true;
}
for (174..256) |b| {
map[b] = @intCast(b);
used[b] = true;
}
var next: u21 = 256;
for (0..256) |b| {
if (!used[b]) {
map[b] = next;
next += 1;
}
}
break :blk map;
};
/// Largest codepoint produced by `byte_to_unicode` (256 + 67 = 323).
pub const max_mapped_codepoint: usize = blk: {
var max: usize = 0;
for (byte_to_unicode) |cp| max = @max(max, cp);
break :blk max;
};
/// Reverse table: codepoint -> byte, -1 when the codepoint is not a mapped byte.
const unicode_to_byte: [max_mapped_codepoint + 1]i16 = blk: {
var map: [max_mapped_codepoint + 1]i16 = @splat(-1);
for (byte_to_unicode, 0..) |cp, b| map[cp] = @intCast(b);
break :blk map;
};
/// Maps a byte-level codepoint back to its byte, or null if it is not one.
pub fn byteForCodepoint(cp: u21) ?u8 {
if (cp >= unicode_to_byte.len) return null;
const v = unicode_to_byte[cp];
return if (v < 0) null else @intCast(v);
}
const decoded = struct { cp: u21, len: usize };
/// Decodes one codepoint; invalid UTF-8 is treated as a single raw byte so
/// that arbitrary byte strings survive a round trip.
fn decodeChar(s: []const u8) decoded {
if (s.len == 0) return .{ .cp = 0, .len = 0 };
const len = std.unicode.utf8ByteSequenceLength(s[0]) catch return .{ .cp = s[0], .len = 1 };
if (len > s.len) return .{ .cp = s[0], .len = 1 };
const cp = std.unicode.utf8Decode(s[0..len]) catch return .{ .cp = s[0], .len = 1 };
return .{ .cp = cp, .len = len };
}
/// Approximation of Unicode `\p{L}` (documented, exact for ASCII): the major
/// letter blocks, so CJK/Greek/Cyrillic/accented Latin text is treated as
/// letters the way GPT-2's regex does.
pub fn isLetter(cp: u21) bool {
return switch (cp) {
'a'...'z', 'A'...'Z', 0xAA, 0xB5, 0xBA => true,
0xC0...0xD6, 0xD8...0xF6, 0xF8...0x2FF => true, // Latin-1 letters, Latin Extended-A/B
0x370...0x3FF, 0x400...0x52F => true, // Greek, Cyrillic
0x531...0x58F => true, // Armenian
0x5D0...0x5EA, 0x5EF...0x5F2 => true, // Hebrew
0x620...0x64A, 0x66E...0x6D3 => true, // Arabic
0x900...0x97F => true, // Devanagari
0x1E00...0x1FFF => true, // Latin/Greek extended additional
0x2C60...0x2C7F, 0xA720...0xA7FF => true,
0x3040...0x30FF => true, // Hiragana, Katakana
0x3400...0x4DBF, 0x4E00...0x9FFF => true, // CJK
0xA960...0xA97F, 0xAC00...0xD7FF => true, // Hangul
0xF900...0xFAFF => true, // CJK compatibility ideographs
0x20000...0x3FFFF => true, // CJK extensions
else => false,
};
}
/// Approximation of Unicode `\p{N}`.
pub fn isDigit(cp: u21) bool {
return switch (cp) {
'0'...'9' => true,
0x660...0x669, 0x6F0...0x6F9 => true, // Arabic-Indic
0x966...0x96F, 0x9E6...0x9EF => true, // Devanagari, Bengali
0xFF10...0xFF19 => true, // fullwidth
else => false,
};
}
/// Approximation of `\s` (ASCII plus the usual Unicode space separators).
pub fn isWhitespace(cp: u21) bool {
return switch (cp) {
' ', '\t', '\n', '\r', 0x0B, 0x0C => true,
0x85, 0xA0, 0x1680 => true,
0x2000...0x200A, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000 => true,
else => false,
};
}
const CharClass = enum { letter, digit, other };
fn classMatches(class: CharClass, cp: u21) bool {
return switch (class) {
.letter => isLetter(cp),
.digit => isDigit(cp),
.other => !isWhitespace(cp) and !isLetter(cp) and !isDigit(cp),
};
}
/// The GPT-2 pre-tokenizer regex, implemented as a scanner. Alternatives are
/// tried in the reference order, so e.g. " hello" (space + word) stays one
/// pre-token while " hello" yields " " followed by " hello".
pub const PreTokenizer = struct {
text: []const u8,
pos: usize = 0,
const contractions = [_][]const u8{ "'s", "'t", "'re", "'ve", "'m", "'ll", "'d" };
pub fn next(self: *PreTokenizer) ?[]const u8 {
if (self.pos >= self.text.len) return null;
const start = self.pos;
const rest = self.text[self.pos..];
// 1. contractions ('s|'t|'re|'ve|'m|'ll|'d)
for (contractions) |c| {
if (std.mem.startsWith(u8, rest, c)) {
self.pos += c.len;
return self.text[start..self.pos];
}
}
// 2.-4. optional space + run of letters / numbers / other
inline for (.{ CharClass.letter, CharClass.digit, CharClass.other }) |class| {
if (self.runEnd(start, class)) |end| {
self.pos = end;
return self.text[start..end];
}
}
// 5. \s+(?!\S): all but the last whitespace character of a run that is
// followed by a non-whitespace character. A single whitespace
// character cannot satisfy the lookahead, so it falls through to 6.
const ws_end = self.whitespaceEnd(start);
if (ws_end > start) {
if (ws_end < self.text.len and ws_end - 1 > start) {
self.pos = ws_end - 1;
} else {
self.pos = ws_end;
}
return self.text[start..self.pos];
}
// 6. \s+ (unreachable in practice: 5 already covers whitespace runs)
const d = decodeChar(rest);
self.pos += @max(d.len, 1);
return self.text[start..self.pos];
}
fn runEnd(self: PreTokenizer, start: usize, class: CharClass) ?usize {
var i = start;
if (i < self.text.len and self.text[i] == ' ') i += 1;
const body = i;
while (i < self.text.len) {
const d = decodeChar(self.text[i..]);
if (!classMatches(class, d.cp)) break;
i += d.len;
}
if (i == body) return null;
return i;
}
fn whitespaceEnd(self: PreTokenizer, start: usize) usize {
var i = start;
while (i < self.text.len) {
const d = decodeChar(self.text[i..]);
if (!isWhitespace(d.cp)) break;
i += d.len;
}
return i;
}
};
/// Splits `text` with the GPT-2 pre-tokenizer. Returned slices alias `text`;
/// only the outer slice is allocated.
pub fn pretokenize(allocator: Allocator, text: []const u8) ![][]const u8 {
var list: std.ArrayList([]const u8) = .empty;
defer list.deinit(allocator);
var it = PreTokenizer{ .text = text };
while (it.next()) |piece| try list.append(allocator, piece);
return list.toOwnedSlice(allocator);
}
const Special = struct {
/// The token's text with the byte-level mapping reversed (e.g. "<s>").
text: []const u8,
id: u32,
};
/// A byte-level BPE tokenizer. Owns all of its data: it stays valid after the
/// `Gguf` it was built from is deinitialized.
pub const Tokenizer = struct {
allocator: Allocator,
arena: std.heap.ArenaAllocator,
/// id -> token string (byte-level mapped form, e.g. "Ġhello").
tokens: [][]const u8 = &.{},
token_types: []TokenType = &.{},
/// token string -> id (first definition wins, except for added tokens).
index: std.StringHashMapUnmanaged(u32) = .empty,
/// "left\x00right" -> merge rank (lower rank merges first).
merge_ranks: std.StringHashMapUnmanaged(u32) = .empty,
/// Special tokens matched literally by `encode`, longest first.
specials: []const Special = &.{},
bos: ?u32 = null,
eos: ?u32 = null,
/// True when the vocabulary is SentencePiece (`tokenizer.ggml.model =
/// "llama"`): pieces use U+2581 for a leading space and the vocabulary has
/// no `merges` list, so encoding works by longest-match rather than BPE.
sentencepiece: bool = false,
/// The GGUF `general.architecture`, when the tokenizer came from one.
arch: []const u8 = "",
/// The GGUF `tokenizer.chat_template`, when present. This is the only
/// reliable way to pick a template for models whose markers (<|user|> etc.)
/// are plain text rather than vocabulary entries.
chat_template: []const u8 = "",
/// SentencePiece scores, used to derive token classes when the file has no
/// `tokenizer.ggml.token_type` (TinyLlama, Llama-2 era).
scores: ?[]const f32 = null,
/// `tokenizer.ggml.add_bos_token` (default true).
add_bos: bool = true,
/// Builds a tokenizer from GGUF metadata:
/// `tokenizer.ggml.tokens`, `tokenizer.ggml.merges`,
/// `tokenizer.ggml.token_type`, `tokenizer.ggml.bos_token_id`,
/// `tokenizer.ggml.eos_token_id`, `tokenizer.ggml.add_bos_token`.
///
/// Returns `error.UnsupportedTokenizerModel` for non-BPE models (e.g.
/// SentencePiece `model = "llama"`).
/// Diagnostics from the last fromGguf failure (shown by the CLI).
pub var last_error_detail: ?[]const u8 = null;
pub fn fromGguf(allocator: Allocator, g: *const gguf.Gguf) !Tokenizer {
last_error_detail = null;
var is_spm = false;
if (g.getString("tokenizer.ggml.model")) |model| {
// The Gemma family declares the SentencePiece variant it uses by name rather than
// calling it "llama": Gemma 4's files say `tokenizer.ggml.model = "gemma4"`, and the
// layout is the one already implemented (U+2581 for a leading space, the same
// token_type codes). Treating it as unknown refused the whole model.
if (std.mem.eql(u8, model, "llama") or std.mem.startsWith(u8, model, "gemma")) {
is_spm = true;
} else if (!std.mem.eql(u8, model, "gpt2") and !std.mem.eql(u8, model, "bpe")) {
last_error_detail = "only byte-level BPE (\"gpt2\"/\"bpe\") and SentencePiece (\"llama\") vocabularies are implemented.";
return error.UnsupportedTokenizerModel;
}
} else {
last_error_detail = "the GGUF file has no tokenizer.ggml.model key, so the vocabulary type is unknown.";
return error.UnsupportedTokenizerModel;
}
if (g.getStringArray("tokenizer.ggml.tokens") == null) {
last_error_detail = "the GGUF file has no tokenizer.ggml.tokens array.";
return error.MissingTokens;
}
const tokens = g.getStringArray("tokenizer.ggml.tokens") orelse return error.MissingTokens;
var t = Tokenizer{
.allocator = allocator,
.arena = std.heap.ArenaAllocator.init(allocator),
};
errdefer t.deinit();
try t.setTokens(tokens);
t.sentencepiece = is_spm;
if (g.arch()) |arch| t.arch = try t.arena.allocator().dupe(u8, arch);
if (g.getString("tokenizer.chat_template")) |tmpl| {
t.chat_template = try t.arena.allocator().dupe(u8, tmpl);
}
if (g.getValue("tokenizer.ggml.token_type")) |v| {
try t.setTokenTypes(v);
} else if (is_spm) {
// No token_type array: derive the classes from the scores, which is
// how llama.cpp does it for SentencePiece vocabularies.
try t.deriveTokenTypesFromScores(g);
}
if (g.getStringArray("tokenizer.ggml.merges")) |merges| try t.addMerges(merges);
t.bos = g.getU32("tokenizer.ggml.bos_token_id");
t.eos = g.getU32("tokenizer.ggml.eos_token_id");
if (g.getBool("tokenizer.ggml.add_bos_token")) |b| t.add_bos = b;
try t.buildSpecials();
return t;
}
/// Reads an HF `tokenizer.json` from disk (BPE models only).
pub fn fromTokenizerJson(allocator: Allocator, path: []const u8) !Tokenizer {
const text = try Io.Dir.cwd().readFileAlloc(
Io.Threaded.global_single_threaded.io(),
path,
allocator,
.limited(1 << 30),
);
defer allocator.free(text);
return fromTokenizerJsonSlice(allocator, text);
}
/// Same as `fromTokenizerJson`, from an in-memory document.
pub fn fromTokenizerJsonSlice(allocator: Allocator, text: []const u8) !Tokenizer {
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, text, .{});
defer parsed.deinit();
const root = parsed.value;
if (root != .object) return error.InvalidTokenizerJson;
const model = root.object.get("model") orelse return error.InvalidTokenizerJson;
if (model != .object) return error.InvalidTokenizerJson;
const model_type = model.object.get("type") orelse return error.InvalidTokenizerJson;
if (model_type != .string or !std.mem.eql(u8, model_type.string, "BPE")) {
return error.UnsupportedTokenizerModel;
}
var t = Tokenizer{
.allocator = allocator,
.arena = std.heap.ArenaAllocator.init(allocator),
};
errdefer t.deinit();
const a = t.arena.allocator();
// Pass 1: size the id table from the vocab.
const vocab = model.object.get("vocab") orelse return error.InvalidTokenizerJson;
if (vocab != .object) return error.InvalidTokenizerJson;
var max_id: u32 = 0;
{
var it = vocab.object.iterator();
while (it.next()) |entry| {
const id = jsonId(entry.value_ptr.*) orelse return error.InvalidTokenizerJson;
max_id = @max(max_id, id);
}
}
if (root.object.get("added_tokens")) |added| {
if (added == .array) {
for (added.array.items) |item| {
if (item != .object) continue;
const id_value = item.object.get("id") orelse continue;
const id = jsonId(id_value) orelse continue;
max_id = @max(max_id, id);
}
}
}
const count: usize = @as(usize, max_id) + 1;
const tokens = try a.alloc([]const u8, count);
for (tokens) |*tok| tok.* = "";
t.tokens = tokens;
const types = try a.alloc(TokenType, count);
@memset(types, .unused);
t.token_types = types;
try t.index.ensureTotalCapacity(a, @intCast(count));
{
var it = vocab.object.iterator();
while (it.next()) |entry| {
const id = jsonId(entry.value_ptr.*) orelse return error.InvalidTokenizerJson;
const tok = try a.dupe(u8, entry.key_ptr.*);
tokens[id] = tok;
types[id] = .normal;
if (tok.len != 0) {
const gop = t.index.getOrPutAssumeCapacity(tok);
if (!gop.found_existing) gop.value_ptr.* = id;
}
}
}
// added_tokens win over the vocab for their content.
if (root.object.get("added_tokens")) |added| {
if (added == .array) {
for (added.array.items) |item| {
if (item != .object) continue;
const id_value = item.object.get("id") orelse continue;
const id = jsonId(id_value) orelse continue;
const content = item.object.get("content") orelse continue;
if (content != .string) continue;
const special = if (item.object.get("special")) |s| (s == .bool and s.bool) else false;
const tok = try a.dupe(u8, content.string);
tokens[id] = tok;
types[id] = if (special) .control else .normal;
if (tok.len != 0) try t.index.put(a, tok, id);
}
}
}
if (model.object.get("merges")) |merges| try t.addJsonMerges(merges);
try t.buildSpecials();
return t;
}
pub fn deinit(self: *Tokenizer) void {
self.arena.deinit();
self.* = undefined;
}
/// Number of token ids (including unused slots).
pub fn vocabSize(self: *const Tokenizer) usize {
return self.tokens.len;
}
pub fn bosId(self: *const Tokenizer) ?u32 {
return self.bos;
}
pub fn eosId(self: *const Tokenizer) ?u32 {
return self.eos;
}
/// Raw token string for `id` (byte-level mapped form, e.g. "Ġhello"),
/// or "" when the id is out of range.
pub fn tokenText(self: *const Tokenizer, id: u32) []const u8 {
if (id >= self.tokens.len) return "";
return self.tokens[id];
}
pub fn tokenType(self: *const Tokenizer, id: u32) TokenType {
if (id >= self.token_types.len) return .undefined;
return self.token_types[id];
}
/// Looks a token string up; the argument must be in byte-level form.
pub fn tokenId(self: *const Tokenizer, token: []const u8) ?u32 {
return self.index.get(token);
}
/// Encodes `text`.
///
/// When `add_special` is true, BOS is prepended if the tokenizer knows one
/// and `tokenizer.ggml.add_bos_token` was not explicitly false.
/// The caller owns the returned slice.
pub fn encode(self: *const Tokenizer, allocator: Allocator, text: []const u8, add_special: bool) ![]u32 {
const a = allocator;
var out: std.ArrayList(u32) = .empty;
errdefer out.deinit(a);
if (add_special and self.add_bos) {
if (self.bos) |b| try out.append(a, b);
}
var i: usize = 0;
var segment_start: usize = 0;
while (i < text.len) {
const spec = self.matchSpecial(text[i..]) orelse {
i += 1;
continue;
};
if (i > segment_start) try self.encodeSegment(a, text[segment_start..i], &out);
try out.append(a, spec.id);
i += spec.text.len;
segment_start = i;
}
if (segment_start < text.len) try self.encodeSegment(a, text[segment_start..], &out);
return out.toOwnedSlice(a);
}
/// Derive token classes from SentencePiece scores.
///
/// SentencePiece vocabularies usually ship without `tokenizer.ggml.token_type`.
/// llama.cpp falls back to the scores: control pieces (like <s>, </s>) have
/// score 0, and the 256 byte pieces have the most negative scores. This
/// mirrors that so `decode` skips the right pieces.
fn deriveTokenTypesFromScores(self: *Tokenizer, g: *const gguf.Gguf) !void {
const scores_val = g.getValue("tokenizer.ggml.scores") orelse return;
if (scores_val != .array) return;
const raw: []const f32 = switch (scores_val.array.data) {
.f32 => |s| s,
.f64 => |s| blk: {
const a0 = self.arena.allocator();
const conv = try a0.alloc(f32, s.len);
for (s, 0..) |v, i| conv[i] = @floatCast(v);
break :blk conv;
},
else => return,
};
if (raw.len < self.tokens.len) return;
const a = self.arena.allocator();
const owned = try a.dupe(f32, raw[0..self.tokens.len]);
self.scores = owned;
// The 256 single-byte pieces have the lowest scores; control tokens sit
// at 0.0. Everything else is a normal piece.
var lowest: f32 = 0;
for (owned) |v| lowest = @min(lowest, v);
for (self.tokens, 0..) |tok, i| {
if (owned[i] <= lowest) {
self.token_types[i] = .byte;
} else if (owned[i] == 0 and tok.len > 1 and tok[0] == '<') {
self.token_types[i] = .control;
}
}
}
/// SentencePiece encoding: split on whitespace, prefix each word with U+2581,
/// then take the longest matching vocabulary piece at each position.
///
/// Not BPE: a SentencePiece GGUF has no `merges` list, so the merge ranks the
/// GPT-2 path relies on do not exist. Longest-match over the pieces is what
/// llama.cpp's SPM path approximates.
fn encodeSegmentSpm(self: *const Tokenizer, a: Allocator, text: []const u8, out: *std.ArrayList(u32)) !void {
const SEP = "\u{2581}";
var i: usize = 0;
while (i < text.len) {
// A literal space is encoded by the separator that prefixes the next
// piece, so consume it here rather than emitting a <0x20> piece (which
// would double the space on decode).
if (text[i] == ' ') {
i += 1;
continue;
}
// At a word boundary, try the piece formed with the separator first.
const at_boundary = blk: {
if (i == 0) break :blk true;
const prev = text[i - 1];
break :blk prev == ' ' or prev == '\n' or prev == '\t';
};
var best_len: usize = 0;
var best_id: u32 = 0;
// Candidate pieces: longest match starting at i, with or without the
// leading separator.
var len: usize = text.len - i;
while (len > 0) : (len -= 1) {
const piece = text[i..][0..len];
if (at_boundary) {
var buf: [256]u8 = undefined;
const with_sep = std.fmt.bufPrint(&buf, "{s}{s}", .{ SEP, piece }) catch null;
if (with_sep) |ws| {
if (self.index.get(ws)) |id| {
if (len > best_len) {
best_len = len;
best_id = id;
}
}
}
}
if (self.index.get(piece)) |id| {
if (len > best_len) {
best_len = len;
best_id = id;
}
}
if (len > 64) {
// Long pieces are rare; cap the scan so a pathological input
// cannot make this quadratic in the whole segment.
len = 64;
continue;
}
}
if (best_len > 0) {
try out.append(a, best_id);
i += best_len;
continue;
}
// No piece matched: fall back to a single byte. SentencePiece
// vocabularies contain all 256 byte pieces, so this should not
// normally happen.
try out.append(a, self.byteFallbackId(text[i]) orelse return error.UnencodableText);
i += 1;
}
}
/// The vocabulary id whose piece is the raw byte `b` (SentencePiece byte
/// pieces are usually `\u2581`+letter forms, so this is a best effort).
fn byteFallbackId(self: *const Tokenizer, b: u8) ?u32 {
var buf: [8]u8 = undefined;
const s = std.fmt.bufPrint(&buf, "<0x{X:0>2}>", .{b}) catch return null;
if (self.index.get(s)) |id| return id;
return self.index.get(&[_]u8{b});
}
/// Decodes token ids back to bytes. CONTROL tokens (BOS/EOS and other
/// specials) are skipped; unknown control-ish slots decode to nothing.
pub fn decode(self: *const Tokenizer, allocator: Allocator, ids: []const u32) ![]u8 {
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(allocator);
for (ids) |id| {
if (id >= self.tokens.len) return error.InvalidTokenId;
if (self.token_types[id].isSpecial()) continue;
if (self.sentencepiece) {
try appendDecodedSpm(allocator, &out, self.tokens[id]);
} else {
try appendDecoded(allocator, &out, self.tokens[id]);
}
}
return out.toOwnedSlice(allocator);
}
/// Decodes a single token to bytes (CONTROL tokens decode to "").
pub fn tokenBytes(self: *const Tokenizer, allocator: Allocator, id: u32) ![]u8 {
if (id >= self.tokens.len) return error.InvalidTokenId;
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(allocator);
if (!self.token_types[id].isSpecial()) {
// The same branch `decode` takes, so streamed output and `decode`
// never disagree (this path emitted literal "\u2581" for a while).
if (self.sentencepiece) {
try appendDecodedSpm(allocator, &out, self.tokens[id]);
} else {
try appendDecoded(allocator, &out, self.tokens[id]);
}
}
return out.toOwnedSlice(allocator);
}
// ------------------------------------------------------------ internals
fn setTokens(self: *Tokenizer, tokens: []const []const u8) !void {
const a = self.arena.allocator();
const list = try a.alloc([]const u8, tokens.len);
for (tokens, 0..) |tok, i| list[i] = try a.dupe(u8, tok);
self.tokens = list;
const types = try a.alloc(TokenType, tokens.len);
@memset(types, .normal);
self.token_types = types;
try self.index.ensureTotalCapacity(a, @intCast(tokens.len));
for (list, 0..) |tok, i| {
if (tok.len == 0) continue;
const gop = self.index.getOrPutAssumeCapacity(tok);
if (!gop.found_existing) gop.value_ptr.* = @intCast(i);
}
}
fn setTokenTypes(self: *Tokenizer, value: gguf.Value) !void {
const data = value.array.data;
const n = @min(data.len(), self.token_types.len);
for (0..n) |i| {
const raw: i64 = switch (data) {
.i32 => |s| s[i],
.u32 => |s| s[i],
.i64 => |s| s[i],
.u64 => |s| @intCast(s[i]),
.i16 => |s| s[i],
.u16 => |s| s[i],
.i8 => |s| s[i],
.u8 => |s| s[i],
else => return,
};
if (raw < 0 or raw > 6) continue;
self.token_types[i] = @fromBackingInt(@intCast(@as(u32, @intCast(raw))));
}
}
fn addMerges(self: *Tokenizer, merges: []const []const u8) !void {
try self.merge_ranks.ensureTotalCapacity(self.arena.allocator(), @intCast(merges.len));
for (merges, 0..) |m, merge_rank| {
const sep = std.mem.indexOfScalar(u8, m, ' ') orelse continue;
const right = m[sep + 1 ..];
if (right.len == 0) continue;
try self.putMerge(m[0..sep], right, @intCast(merge_rank));
}
}
fn addJsonMerges(self: *Tokenizer, merges: std.json.Value) !void {
if (merges != .array) return;
try self.merge_ranks.ensureTotalCapacity(self.arena.allocator(), @intCast(merges.array.items.len));
for (merges.array.items, 0..) |item, merge_rank| {
switch (item) {
// Newer tokenizer.json writes merges as ["a", "b"] pairs.
.array => |pair| {
if (pair.items.len != 2) continue;
if (pair.items[0] != .string or pair.items[1] != .string) continue;
try self.putMerge(pair.items[0].string, pair.items[1].string, @intCast(merge_rank));
},
// Older files write a single "a b" string.
.string => |s| {
const sep = std.mem.indexOfScalar(u8, s, ' ') orelse continue;
const right = s[sep + 1 ..];
if (right.len == 0) continue;
try self.putMerge(s[0..sep], right, @intCast(merge_rank));
},
else => continue,
}
}
}
fn putMerge(self: *Tokenizer, left: []const u8, right: []const u8, merge_rank: u32) !void {
const a = self.arena.allocator();
const key = try std.mem.concat(a, u8, &.{ left, "\x00", right });
const gop = try self.merge_ranks.getOrPut(a, key);
if (!gop.found_existing) gop.value_ptr.* = merge_rank;
}
fn buildSpecials(self: *Tokenizer) !void {
const a = self.arena.allocator();
var list: std.ArrayList(Special) = .empty;
for (self.tokens, 0..) |tok, i| {
if (!self.token_types[i].isSpecial()) continue;
var buf: std.ArrayList(u8) = .empty;
try appendDecoded(a, &buf, tok);
if (buf.items.len == 0) continue;
try list.append(a, .{ .text = buf.items, .id = @intCast(i) });
}
// Longest first, so "<|endoftext|>" wins over a hypothetical "<|".
std.mem.sort(Special, list.items, {}, struct {
fn lessThan(_: void, x: Special, y: Special) bool {
return x.text.len > y.text.len;
}
}.lessThan);
self.specials = try list.toOwnedSlice(a);
}
fn matchSpecial(self: *const Tokenizer, rest: []const u8) ?Special {
for (self.specials) |spec| {
if (std.mem.startsWith(u8, rest, spec.text)) return spec;
}
return null;
}
/// Pre-tokenizes and BPE-merges one plain-text span, or takes the
/// SentencePiece path for `llama`-style vocabularies.
fn encodeSegment(self: *const Tokenizer, a: Allocator, text: []const u8, out: *std.ArrayList(u32)) !void {
if (self.sentencepiece) return self.encodeSegmentSpm(a, text, out);
var mapped: std.ArrayList(u8) = .empty;
defer mapped.deinit(a);
var symbols: std.ArrayList(Symbol) = .empty;
defer symbols.deinit(a);
var key_buf: std.ArrayList(u8) = .empty;
defer key_buf.deinit(a);
var it = PreTokenizer{ .text = text };
while (it.next()) |piece| {
mapped.clearRetainingCapacity();
symbols.clearRetainingCapacity();
// One symbol per byte, encoded through the byte->unicode table.
for (piece) |b| {
var cp_buf: [4]u8 = undefined;
const n = std.unicode.utf8Encode(byte_to_unicode[b], &cp_buf) catch unreachable;
try symbols.append(a, .{ .start = mapped.items.len, .len = n });
try mapped.appendSlice(a, cp_buf[0..n]);
}
try self.merge(a, &symbols, mapped.items, &key_buf);
for (symbols.items) |s| {
const symbol = mapped.items[s.start .. s.start + s.len];
if (self.index.get(symbol)) |id| {
try out.append(a, id);
} else {
try self.emitByteFallback(a, symbol, out);
}
}
}
}
/// Standard BPE: repeatedly find the lowest-rank adjacent pair and merge
/// every occurrence of it.
fn merge(self: *const Tokenizer, a: Allocator, symbols: *std.ArrayList(Symbol), mapped: []const u8, key_buf: *std.ArrayList(u8)) !void {
while (symbols.items.len >= 2) {
var best: u32 = std.math.maxInt(u32);
var found = false;
for (0..symbols.items.len - 1) |i| {
const l = mapped[symbols.items[i].start..][0..symbols.items[i].len];
const r = mapped[symbols.items[i + 1].start..][0..symbols.items[i + 1].len];
if (try self.pairRank(a, key_buf, l, r)) |merge_rank| {
if (merge_rank < best) {
best = merge_rank;
found = true;
}
}
}
if (!found) break;
var i: usize = 0;
while (i + 1 < symbols.items.len) {
const l = mapped[symbols.items[i].start..][0..symbols.items[i].len];
const r = mapped[symbols.items[i + 1].start..][0..symbols.items[i + 1].len];
const merge_rank = try self.pairRank(a, key_buf, l, r);
if (merge_rank != null and merge_rank.? == best) {
symbols.items[i].len += symbols.items[i + 1].len;
_ = symbols.orderedRemove(i + 1);
} else {
i += 1;
}
}
}
}
fn pairRank(self: *const Tokenizer, a: Allocator, key_buf: *std.ArrayList(u8), left: []const u8, right: []const u8) !?u32 {
key_buf.clearRetainingCapacity();
try key_buf.appendSlice(a, left);
try key_buf.append(a, 0);
try key_buf.appendSlice(a, right);
return self.merge_ranks.get(key_buf.items);
}
/// Last resort for symbols that are not in the vocabulary: emit the
/// individual byte tokens (always present in a byte-level BPE vocab).
fn emitByteFallback(self: *const Tokenizer, a: Allocator, symbol: []const u8, out: *std.ArrayList(u32)) !void {
var i: usize = 0;
while (i < symbol.len) {
const d = decodeChar(symbol[i..]);
if (d.len == 0) break;
const one = symbol[i .. i + d.len];
const id = self.index.get(one) orelse return error.UnknownToken;
try out.append(a, id);
i += d.len;
}
}
};
const Symbol = struct { start: usize, len: usize };
/// Appends `token`'s text with the byte-level mapping reversed.
/// Append a SentencePiece piece in plain text form: U+2581 becomes a space and
/// `<0xXX>` becomes the raw byte.
pub fn appendDecodedSpm(allocator: Allocator, out: *std.ArrayList(u8), token: []const u8) !void {
if (token.len == 6 and token[0] == '<' and token[1] == '0' and token[2] == 'x' and token[5] == '>') {
const b = std.fmt.parseInt(u8, token[3..5], 16) catch {
try out.appendSlice(allocator, token);
return;
};
try out.append(allocator, b);
return;
}
var i: usize = 0;
while (i < token.len) {
const len = std.unicode.utf8ByteSequenceLength(token[i]) catch 1;
if (i + len <= token.len) {
const cp = std.unicode.utf8Decode(token[i..][0..len]) catch 0;
if (cp == 0x2581) {
try out.append(allocator, ' ');
i += len;
continue;
}
}
try out.append(allocator, token[i]);
i += 1;
}
}
fn appendDecoded(a: Allocator, out: *std.ArrayList(u8), token: []const u8) !void {
var i: usize = 0;
while (i < token.len) {
const d = decodeChar(token[i..]);
if (d.len == 0) break;
if (byteForCodepoint(d.cp)) |_| {
try out.append(a, byteForCodepoint(d.cp).?);
} else {
try out.appendSlice(a, token[i .. i + d.len]);
}
i += d.len;
}
}
fn jsonId(value: std.json.Value) ?u32 {
return switch (value) {
.integer => |i| if (i >= 0 and i <= std.math.maxInt(u32)) @intCast(i) else null,
.float => |f| if (f >= 0 and f <= std.math.maxInt(u32)) @intFromFloat(f) else null,
else => null,
};
}
// ===========================================================================
// Tests
// ===========================================================================
const testing = std.testing;
/// `<repo>/tests/fixtures/<rel>`, derived from this file's own path so tests do
/// not depend on the process working directory.
fn fixturePath(a: Allocator, rel: []const u8) ![]u8 {
return std.fs.path.join(a, &.{ "tests/fixtures", rel });
}
/// The tiny gpt2 vocabulary generated by tools/make_fixtures.py: ids 0..255 are
/// the single-byte tokens (id == byte value), then the merged tokens, then the
/// two control tokens.
const merged_tokens = [_][]const u8{
"he", "hel", "hell", "hello",
"\u{0120}w", "\u{0120}wo", "\u{0120}wor", "\u{0120}worl",
"\u{0120}world",
};
const merge_strings = [_][]const u8{
"h e", "he l", "hel l", "hell o",
"\u{0120} w", "\u{0120}w o", "\u{0120}wo r", "\u{0120}wor l",
"\u{0120}worl d",
};
const bos_token = "<s>";
const eos_token = "</s>";
const eot_token = "<|endoftext|>";
/// Minimal GGUF v3 writer (metadata only) for tokenizer tests.
const KvBuilder = struct {
a: Allocator,
buf: std.ArrayList(u8) = .empty,
fn int(self: *KvBuilder, comptime T: type, v: T) !void {
var tmp: [@sizeOf(T)]u8 = undefined;
std.mem.writeInt(T, &tmp, v, .little);
try self.buf.appendSlice(self.a, &tmp);
}
fn str(self: *KvBuilder, s: []const u8) !void {
try self.int(u64, s.len);
try self.buf.appendSlice(self.a, s);
}
fn kvString(self: *KvBuilder, key: []const u8, v: []const u8) !void {
try self.str(key);
try self.int(u32, 8);
try self.str(v);
}
fn kvU32(self: *KvBuilder, key: []const u8, v: u32) !void {
try self.str(key);
try self.int(u32, 4);
try self.int(u32, v);
}
fn kvBool(self: *KvBuilder, key: []const u8, v: bool) !void {
try self.str(key);
try self.int(u32, 7);
try self.int(u8, @intFromBool(v));
}
fn kvStringArray(self: *KvBuilder, key: []const u8, items: []const []const u8) !void {
try self.str(key);
try self.int(u32, 9);
try self.int(u32, 8);
try self.int(u64, items.len);
for (items) |s| try self.str(s);
}
fn kvF32Array(self: *KvBuilder, key: []const u8, items: []const f32) !void {
try self.str(key);
try self.int(u32, 9); // array
try self.int(u32, 6); // f32 elements
try self.int(u64, items.len);
for (items) |v| try self.int(u32, @bitCast(v));
}
fn kvI32Array(self: *KvBuilder, key: []const u8, items: []const i32) !void {
try self.str(key);
try self.int(u32, 9);
try self.int(u32, 5);
try self.int(u64, items.len);
for (items) |v| try self.int(i32, v);
}