-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtokenizer_test.go
More file actions
805 lines (739 loc) · 28.4 KB
/
tokenizer_test.go
File metadata and controls
805 lines (739 loc) · 28.4 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
package tokenizers_test
import (
_ "embed"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"github.com/daulet/tokenizers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//go:embed test/data/sentence-transformers-labse.json
var embeddedBytes []byte
// TODO test for leaks
// TODO fuzz tests
func TestInvalidConfigPath(t *testing.T) {
_, err := tokenizers.FromFile("./non-existent.json")
require.Error(t, err)
}
func TestEmbeddingConfig(t *testing.T) {
tk, err := tokenizers.FromBytes(embeddedBytes)
require.NoError(t, err)
defer tk.Close()
tests := []struct {
name string
str string
addSpecial bool
wantIDs []uint32
wantTypeIDs []uint32
wantTokens []string
wantSpecialTokensMask []uint32
wantAttentionMask []uint32
wantOffsets []tokenizers.Offset
}{
{
name: "without special tokens",
str: "brown fox jumps over the lazy dog",
addSpecial: false,
wantIDs: []uint32{0xca3f, 0x2f304, 0x5185b, 0x3c54, 0x3a89, 0x35fc3, 0x57b4},
wantTypeIDs: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTokens: []string{"brown", "fox", "jumps", "over", "the", "lazy", "dog"},
wantSpecialTokensMask: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantAttentionMask: []uint32{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1},
wantOffsets: []tokenizers.Offset{{0x0, 0x5}, {0x6, 0x9}, {0xa, 0xf}, {0x10, 0x14}, {0x15, 0x18}, {0x19, 0x1d}, {0x1e, 0x21}},
},
{
name: "with special tokens",
str: "brown fox jumps over the lazy dog",
addSpecial: true,
wantIDs: []uint32{0x65, 0xca3f, 0x2f304, 0x5185b, 0x3c54, 0x3a89, 0x35fc3, 0x57b4, 0x66},
wantTypeIDs: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTokens: []string{"[CLS]", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "[SEP]"},
wantSpecialTokensMask: []uint32{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
wantAttentionMask: []uint32{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1},
wantOffsets: []tokenizers.Offset{{0x0, 0x0}, {0x0, 0x5}, {0x6, 0x9}, {0xa, 0xf}, {0x10, 0x14}, {0x15, 0x18}, {0x19, 0x1d}, {0x1e, 0x21}, {0x0, 0x0}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoding := tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnAllAttributes())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, tt.wantTypeIDs, encoding.TypeIDs, "wrong type ids")
assert.Equal(t, tt.wantTokens, encoding.Tokens, "wrong tokens")
assert.Equal(t, tt.wantSpecialTokensMask, encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, tt.wantAttentionMask, encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, tt.wantOffsets, encoding.Offsets, "wrong offsets")
ids, tokens := tk.Encode(tt.str, tt.addSpecial)
assert.Equal(t, tt.wantIDs, ids, "wrong ids")
assert.Equal(t, tt.wantTokens, tokens, "wrong tokens")
})
}
}
func TestEncodeWithAndWithoutOptions(t *testing.T) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(t, err)
defer tk.Close()
tests := []struct {
name string
str string
addSpecial bool
wantIDs []uint32
wantTypeIDs []uint32
wantTokens []string
wantSpecialTokensMask []uint32
wantAttentionMask []uint32
wantOffsets []tokenizers.Offset
}{
{
name: "without special tokens",
str: "brown fox jumps over the lazy dog",
addSpecial: false,
wantIDs: []uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899},
wantTypeIDs: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTokens: []string{"brown", "fox", "jumps", "over", "the", "lazy", "dog"},
wantSpecialTokensMask: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantAttentionMask: []uint32{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1},
wantOffsets: []tokenizers.Offset{{0x0, 0x5}, {0x6, 0x9}, {0xa, 0xf}, {0x10, 0x14}, {0x15, 0x18}, {0x19, 0x1d}, {0x1e, 0x21}},
},
{
name: "with special tokens",
str: "brown fox jumps over the lazy dog",
addSpecial: true,
wantIDs: []uint32{101, 2829, 4419, 14523, 2058, 1996, 13971, 3899, 102},
wantTypeIDs: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTokens: []string{"[CLS]", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "[SEP]"},
wantSpecialTokensMask: []uint32{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
wantAttentionMask: []uint32{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1},
wantOffsets: []tokenizers.Offset{{0x0, 0x0}, {0x0, 0x5}, {0x6, 0x9}, {0xa, 0xf}, {0x10, 0x14}, {0x15, 0x18}, {0x19, 0x1d}, {0x1e, 0x21}, {0x0, 0x0}},
},
{
name: "empty string",
str: "",
addSpecial: false,
},
{
name: "empty string with special tokens",
str: "",
addSpecial: true,
wantTypeIDs: []uint32{0x0, 0x0},
wantSpecialTokensMask: []uint32{0x1, 0x1},
wantAttentionMask: []uint32{0x1, 0x1},
wantIDs: []uint32{101, 102},
wantTokens: []string{"[CLS]", "[SEP]"},
wantOffsets: []tokenizers.Offset{{0x0, 0x0}, {0x0, 0x0}},
},
{
name: "invalid utf8 string",
str: "\x91D",
wantIDs: []uint32{1040},
wantTypeIDs: []uint32{0x0},
wantTokens: []string{"d"}, // should be \x91D but tokenizer doesn't handle non-utf8 well
wantOffsets: []tokenizers.Offset{{0x3, 0x4}},
addSpecial: false,
wantSpecialTokensMask: []uint32{0x0},
wantAttentionMask: []uint32{0x1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoding := tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnAllAttributes())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, tt.wantTypeIDs, encoding.TypeIDs, "wrong type ids")
assert.Equal(t, tt.wantTokens, encoding.Tokens, "wrong tokens")
assert.Equal(t, tt.wantSpecialTokensMask, encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, tt.wantAttentionMask, encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, tt.wantOffsets, encoding.Offsets, "wrong offsets mask")
ids, tokens := tk.Encode(tt.str, tt.addSpecial)
assert.Equal(t, tt.wantIDs, ids, "wrong ids")
assert.Equal(t, tt.wantTokens, tokens, "wrong tokens")
})
}
}
func TestEncodeSpecialTokens(t *testing.T) {
tk, err := tokenizers.FromBytes(embeddedBytes)
require.NoError(t, err)
// special tokens are not encoded by default,
// meaning if input matches a special token, encoding will include the special token
ids, _ := tk.Encode("[CLS]fox[SEP]", false)
assert.Equal(t, []uint32{101, 193284, 102}, ids)
tk.Close()
tk, err = tokenizers.FromBytes(embeddedBytes, tokenizers.WithEncodeSpecialTokens())
require.NoError(t, err)
ids, _ = tk.Encode("[CLS]fox[SEP]", false)
// assert that special tokens 101 and 102 are not present
assert.Equal(t, []uint32{164, 304910, 166, 193284, 164, 211703, 166}, ids)
tk.Close()
}
func TestEncodeOptions(t *testing.T) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(t, err)
defer tk.Close()
tests := []struct {
name string
str string
addSpecial bool
wantIDs []uint32
wantTypeIDs []uint32
wantTokens []string
wantSpecialTokensMask []uint32
wantAttentionMask []uint32
wantOffsets []tokenizers.Offset
}{
{
name: "without special tokens",
str: "brown fox jumps over the lazy dog",
addSpecial: false,
wantIDs: []uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899},
wantTypeIDs: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTokens: []string{"brown", "fox", "jumps", "over", "the", "lazy", "dog"},
wantSpecialTokensMask: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantAttentionMask: []uint32{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1},
wantOffsets: []tokenizers.Offset{{0x0, 0x5}, {0x6, 0x9}, {0xa, 0xf}, {0x10, 0x14}, {0x15, 0x18}, {0x19, 0x1d}, {0x1e, 0x21}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoding := tk.EncodeWithOptions(tt.str, tt.addSpecial)
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, []uint32(nil), encoding.TypeIDs, "wrong type ids")
assert.Equal(t, []string(nil), encoding.Tokens, "wrong tokens")
assert.Equal(t, []uint32(nil), encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, []uint32(nil), encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, []tokenizers.Offset(nil), encoding.Offsets, "wrong offsets")
encoding = tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnTokens())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, []uint32(nil), encoding.TypeIDs, "wrong type ids")
assert.Equal(t, tt.wantTokens, encoding.Tokens, "wrong tokens")
assert.Equal(t, []uint32(nil), encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, []uint32(nil), encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, []tokenizers.Offset(nil), encoding.Offsets, "wrong offsets")
encoding = tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnTypeIDs())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, tt.wantTypeIDs, encoding.TypeIDs, "wrong type ids")
assert.Equal(t, []string(nil), encoding.Tokens, "wrong tokens")
assert.Equal(t, []uint32(nil), encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, []uint32(nil), encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, []tokenizers.Offset(nil), encoding.Offsets, "wrong offsets")
encoding = tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnSpecialTokensMask())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, []uint32(nil), encoding.TypeIDs, "wrong type ids")
assert.Equal(t, []string(nil), encoding.Tokens, "wrong tokens")
assert.Equal(t, tt.wantSpecialTokensMask, encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, []uint32(nil), encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, []tokenizers.Offset(nil), encoding.Offsets, "wrong offsets")
encoding = tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnAttentionMask())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, []uint32(nil), encoding.TypeIDs, "wrong type ids")
assert.Equal(t, []string(nil), encoding.Tokens, "wrong tokens")
assert.Equal(t, []uint32(nil), encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, tt.wantAttentionMask, encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, []tokenizers.Offset(nil), encoding.Offsets, "wrong offsets")
encoding = tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnOffsets())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, []uint32(nil), encoding.TypeIDs, "wrong type ids")
assert.Equal(t, []string(nil), encoding.Tokens, "wrong tokens")
assert.Equal(t, []uint32(nil), encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, []uint32(nil), encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, tt.wantOffsets, encoding.Offsets, "wrong offsets")
})
}
}
func TestEncodeWithTruncation(t *testing.T) {
tests := []struct {
name string
str string
addSpecial bool
maxLen int
dir tokenizers.TruncationDirection
wantIDs []uint32
wantTokens []string
}{
{
name: "without special tokens, left truncation",
str: "brown fox jumps over the lazy dog",
addSpecial: false,
maxLen: 5,
dir: tokenizers.TruncationDirectionLeft,
wantIDs: []uint32{0x5185b, 0x3c54, 0x3a89, 0x35fc3, 0x57b4},
wantTokens: []string{"jumps", "over", "the", "lazy", "dog"},
},
{
name: "without special tokens, right truncation",
str: "brown fox jumps over the lazy dog",
addSpecial: false,
maxLen: 5,
dir: tokenizers.TruncationDirectionRight,
wantIDs: []uint32{0xca3f, 0x2f304, 0x5185b, 0x3c54, 0x3a89},
wantTokens: []string{"brown", "fox", "jumps", "over", "the"},
},
{
name: "with special tokens, left truncation",
str: "brown fox jumps over the lazy dog",
addSpecial: true,
maxLen: 5,
dir: tokenizers.TruncationDirectionLeft,
wantIDs: []uint32{0x65, 0x3a89, 0x35fc3, 0x57b4, 0x66},
wantTokens: []string{"[CLS]", "the", "lazy", "dog", "[SEP]"},
},
{
name: "with special tokens, right truncation",
str: "brown fox jumps over the lazy dog",
addSpecial: true,
maxLen: 5,
dir: tokenizers.TruncationDirectionRight,
wantIDs: []uint32{0x65, 0xca3f, 0x2f304, 0x5185b, 0x66},
wantTokens: []string{"[CLS]", "brown", "fox", "jumps", "[SEP]"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tk, err := tokenizers.FromBytesWithTruncation(embeddedBytes, uint32(tt.maxLen), tt.dir)
require.NoError(t, err)
defer tk.Close()
ids, tokens := tk.Encode(tt.str, tt.addSpecial)
assert.Equal(t, tt.wantIDs, ids, "wrong ids")
assert.Equal(t, tt.wantTokens, tokens, "wrong tokens")
})
}
}
func TestEncodeWithPadding(t *testing.T) {
tk, err := tokenizers.FromFile("./test/data/all-minilm-l6-v2.json")
require.NoError(t, err)
defer tk.Close()
tests := []struct {
name string
str string
addSpecial bool
wantIDs []uint32
wantTypeIDs []uint32
wantTokens []string
wantSpecialTokensMask []uint32
wantAttentionMask []uint32
wantOffsets []tokenizers.Offset
}{
{
name: "sentence with padding",
str: "this short sentence",
addSpecial: false,
wantIDs: []uint32{0x7e7, 0x99c, 0x186b, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTypeIDs: []uint32{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
wantTokens: []string{"this", "short", "sentence", "[PAD]", "[PAD]", "[PAD]", "[PAD]", "[PAD]"},
wantSpecialTokensMask: []uint32{0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1},
wantAttentionMask: []uint32{0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0},
wantOffsets: []tokenizers.Offset{{0x0, 0x4}, {0x5, 0xa}, {0xb, 0x13}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoding := tk.EncodeWithOptions(tt.str, tt.addSpecial, tokenizers.WithReturnAllAttributes())
assert.Equal(t, tt.wantIDs, encoding.IDs, "wrong ids")
assert.Equal(t, tt.wantTypeIDs, encoding.TypeIDs, "wrong type ids")
assert.Equal(t, tt.wantTokens, encoding.Tokens, "wrong tokens")
assert.Equal(t, tt.wantSpecialTokensMask, encoding.SpecialTokensMask, "wrong special tokens mask")
assert.Equal(t, tt.wantAttentionMask, encoding.AttentionMask, "wrong attention mask")
assert.Equal(t, tt.wantOffsets, encoding.Offsets, "wrong offsets")
ids, tokens := tk.Encode(tt.str, tt.addSpecial)
assert.Equal(t, tt.wantIDs, ids, "wrong ids")
assert.Equal(t, tt.wantTokens, tokens, "wrong tokens")
})
}
}
func TestDecode(t *testing.T) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(t, err)
defer tk.Close()
tests := []struct {
name string
tokens []uint32
skipSpecial bool
want string
}{
{
name: "without special tokens, skip special tokens",
tokens: []uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899},
skipSpecial: true,
want: "brown fox jumps over the lazy dog",
},
{
name: "with special tokens, skip special tokens",
tokens: []uint32{101, 2829, 4419, 14523, 2058, 1996, 13971, 3899, 102},
skipSpecial: true,
want: "brown fox jumps over the lazy dog",
},
{
name: "without special tokens, don't skip special tokens",
tokens: []uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899},
skipSpecial: false,
want: "brown fox jumps over the lazy dog",
},
{
name: "with special tokens, don't skip special tokens",
tokens: []uint32{101, 2829, 4419, 14523, 2058, 1996, 13971, 3899, 102},
skipSpecial: false,
want: "[CLS] brown fox jumps over the lazy dog [SEP]",
},
{
name: "no tokens",
tokens: []uint32{},
skipSpecial: false,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tk.Decode(tt.tokens, tt.skipSpecial)
assert.Equal(t, tt.want, got)
})
}
}
func TestDecodeInvalidString(t *testing.T) {
tk, err := tokenizers.FromFile("test/data/cohere-tokenizer.json")
require.NoError(t, err)
defer tk.Close()
str := tk.Decode([]uint32{196}, true)
assert.Empty(t, str)
}
func TestErrorAPIOnClosedTokenizer(t *testing.T) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(t, err)
require.NoError(t, tk.Close())
_, _, err = tk.EncodeErr("hello world", false)
require.Error(t, err)
_, err = tk.EncodeWithOptionsErr("hello world", false, tokenizers.WithReturnTokens())
require.Error(t, err)
_, err = tk.DecodeErr([]uint32{101, 7592, 102}, true)
require.Error(t, err)
}
func TestVocabSize(t *testing.T) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(t, err)
defer tk.Close()
assert.Equal(t, uint32(30522), tk.VocabSize())
}
func BenchmarkEncodeNTimes(b *testing.B) {
hfTk, err := tokenizers.FromFile("./test/data/meta-llama-3-8b-instruct.json")
require.NoError(b, err)
defer hfTk.Close()
// Source: https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py
ttTk, err := tokenizers.FromTiktoken(
"./test/data/meta-llama-3-8b-instruct/tiktoken.model",
"./test/data/meta-llama-3-8b-instruct/tokenizer_config.json",
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
)
require.NoError(b, err)
defer ttTk.Close()
tests := []struct {
name string
tk *tokenizers.Tokenizer
}{
{name: "huggingface", tk: hfTk},
{name: "tiktoken", tk: ttTk},
}
for _, tt := range tests {
expected := []uint32{0x10019, 0x9bff, 0x89ec, 0x39f, 0x117, 0x3eb5, 0x162f}
b.Run(tt.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
ids, _ := tt.tk.Encode("brown fox jumps over the lazy dog", false)
assert.Equal(b, expected, ids)
}
})
}
}
func BenchmarkEncodeNChars(b *testing.B) {
hfTk, err := tokenizers.FromFile("./test/data/meta-llama-3-8b-instruct.json")
require.NoError(b, err)
defer hfTk.Close()
// Source: https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py
ttTk, err := tokenizers.FromTiktoken(
"./test/data/meta-llama-3-8b-instruct/tiktoken.model",
"./test/data/meta-llama-3-8b-instruct/tokenizer_config.json",
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
)
require.NoError(b, err)
defer ttTk.Close()
tests := []struct {
name string
tk *tokenizers.Tokenizer
}{
{name: "huggingface", tk: hfTk},
{name: "tiktoken", tk: ttTk},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
vocabSize := tt.tk.VocabSize()
input := make([]rune, 0, b.N)
for i := 0; i < b.N; i++ {
input = append(input, rune(rand.Uint32()%vocabSize))
}
str := string(input)
b.ResetTimer()
ids, _ := tt.tk.Encode(str, false)
assert.Greater(b, len(ids), 0, "input (len: %d): %v", len(input), str)
})
}
}
func BenchmarkDecodeNTimes(b *testing.B) {
hfTk, err := tokenizers.FromFile("./test/data/meta-llama-3-8b-instruct.json")
require.NoError(b, err)
defer hfTk.Close()
// Source: https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py
ttTk, err := tokenizers.FromTiktoken(
"./test/data/meta-llama-3-8b-instruct/tiktoken.model",
"./test/data/meta-llama-3-8b-instruct/tokenizer_config.json",
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
)
require.NoError(b, err)
defer ttTk.Close()
tests := []struct {
name string
tk *tokenizers.Tokenizer
}{
{name: "huggingface", tk: hfTk},
{name: "tiktoken", tk: ttTk},
}
// Token IDs for "brown fox jumps over the lazy dog" in Llama tokenizer
tokenIDs := []uint32{0x10019, 0x9bff, 0x89ec, 0x39f, 0x117, 0x3eb5, 0x162f}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
str := tt.tk.Decode(tokenIDs, true)
assert.Equal(b, "brown fox jumps over the lazy dog", str)
}
})
}
}
func BenchmarkDecodeNTokens(b *testing.B) {
hfTk, err := tokenizers.FromFile("./test/data/meta-llama-3-8b-instruct.json")
require.NoError(b, err)
defer hfTk.Close()
// Source: https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py
ttTk, err := tokenizers.FromTiktoken(
"./test/data/meta-llama-3-8b-instruct/tiktoken.model",
"./test/data/meta-llama-3-8b-instruct/tokenizer_config.json",
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
)
require.NoError(b, err)
defer ttTk.Close()
tests := []struct {
name string
tk *tokenizers.Tokenizer
}{
{name: "huggingface", tk: hfTk},
{name: "tiktoken", tk: ttTk},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
vocabSize := tt.tk.VocabSize()
// Generate some valid tokens by encoding text first
sampleText := "The quick brown fox jumps over the lazy dog. This is a sample text for benchmarking."
validTokens, _ := tt.tk.Encode(sampleText, false)
if len(validTokens) == 0 {
b.Fatal("Failed to generate valid tokens from sample text")
}
input := make([]uint32, 0, b.N)
for i := 0; i < b.N; i++ {
// Use valid tokens from our sample, cycling through them
input = append(input, validTokens[i%len(validTokens)])
}
b.ResetTimer()
text := tt.tk.Decode(input, true)
// a token is one or more characters
assert.GreaterOrEqual(b, len(text), b.N, "decoded text length: %d, expected at least: %d (vocab size: %d)", len(text), b.N, vocabSize)
})
}
}
func TestFromTiktoken(t *testing.T) {
// Define the pattern for tiktoken tokenization
pattern := strings.Join([]string{
`[\p{Han}]+`,
`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}&&[^\p{Han}]]*[\p{Ll}\p{Lm}\p{Lo}\p{M}&&[^\p{Han}]]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?`,
`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}&&[^\p{Han}]]+[\p{Ll}\p{Lm}\p{Lo}\p{M}&&[^\p{Han}]]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?`,
`\p{N}{1,3}`,
` ?[^\s\p{L}\p{N}]+[\r\n]*`,
`\s*[\r\n]+`,
`\s+(?!\S)`,
`\s+`,
}, "|")
tk, err := tokenizers.FromTiktoken(
"./test/data/kimi-k2-instruct/tiktoken.model",
"./test/data/kimi-k2-instruct/tokenizer_config.json",
pattern,
)
require.NoError(t, err)
defer tk.Close()
{
text := "Hello, world! 你好,世界!"
ids, _ := tk.Encode(text, false)
assert.Equal(t, []uint32{19180, 11, 2695, 0, 220, 33845, 378, 2243, 856}, ids)
ids, _ = tk.Encode(text, true)
assert.Equal(t, []uint32{19180, 11, 2695, 0, 220, 33845, 378, 2243, 856}, ids)
decoded := tk.Decode(ids, false)
assert.Equal(t, text, decoded)
decoded = tk.Decode(ids, true)
assert.Equal(t, text, decoded)
}
{
text := "<|im_middle|>Hello, world! 你好,世界!<|im_end|>"
ids, _ := tk.Encode(text, true)
assert.Equal(t, []uint32{163601, 19180, 11, 2695, 0, 220, 33845, 378, 2243, 856, 163586}, ids)
decoded := tk.Decode(ids, false)
assert.Equal(t, text, decoded)
decoded = tk.Decode(ids, true)
assert.Equal(t, "Hello, world! 你好,世界!", decoded)
}
vocabSize := tk.VocabSize()
assert.Equal(t, uint32(163840), vocabSize)
}
func TestTiktokenReplacementCharacter(t *testing.T) {
// Test tiktoken tokenizer with replacement character (U+FFFD)
// Source: https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py
tk, err := tokenizers.FromTiktoken(
"./test/data/meta-llama-3-8b-instruct/tiktoken.model",
"./test/data/meta-llama-3-8b-instruct/tokenizer_config.json",
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
)
require.NoError(t, err)
defer tk.Close()
tests := []struct {
name string
input string
shouldWork bool
}{
{
name: "normal text",
input: "Hello world",
shouldWork: true,
},
{
name: "text with replacement character",
input: "Hello �world",
shouldWork: true, // This currently fails but should work
},
{
name: "multiple replacement characters",
input: "Test � multiple � chars",
shouldWork: true,
},
{
name: "only replacement character",
input: "�",
shouldWork: true,
},
{
name: "UTF-8 replacement character bytes",
input: "\xEF\xBF\xBD", // UTF-8 encoding of U+FFFD
shouldWork: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ids, _ := tk.Encode(tt.input, false)
if tt.shouldWork {
assert.NotEmpty(t, ids, "Expected non-empty token IDs for input: %q", tt.input)
// assert.NotEmpty(t, tokens, "Expected non-empty tokens for input: %q", tt.input)
// Test decoding
if len(ids) > 0 {
decoded := tk.Decode(ids, false)
assert.Equal(t, tt.input, decoded, "Decoded text should match input")
}
} else {
assert.Empty(t, ids, "Expected empty token IDs for input: %q", tt.input)
}
})
}
}
func TestFromPretrained(t *testing.T) {
if os.Getenv("TOKENIZERS_RUN_NETWORK_TESTS") != "1" {
t.Skip("set TOKENIZERS_RUN_NETWORK_TESTS=1 to run network-dependent tests")
}
tests := []struct {
name string
modelID string
setupOpts func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string)
wantErr bool
expectedToken bool
}{
{
name: "valid public model with cache dir",
modelID: "bert-base-uncased",
expectedToken: true,
setupOpts: func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string) {
tmpDir := t.TempDir()
return []tokenizers.TokenizerConfigOption{
tokenizers.WithCacheDir(tmpDir),
}, tmpDir
},
},
{
name: "valid public model without cache dir",
modelID: "bert-base-uncased",
expectedToken: true,
setupOpts: func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string) {
return nil, ""
},
},
{
name: "private model with invalid auth token",
modelID: "private-model",
wantErr: true,
setupOpts: func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string) {
tmpDir := t.TempDir()
return []tokenizers.TokenizerConfigOption{
tokenizers.WithCacheDir(tmpDir),
tokenizers.WithAuthToken("invalid-token"),
}, tmpDir
},
},
{
name: "empty model ID",
modelID: "",
wantErr: true,
setupOpts: func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string) {
return nil, ""
},
},
{
name: "nonexistent model",
modelID: "nonexistent/model",
wantErr: true,
setupOpts: func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string) {
tmpDir := t.TempDir()
return []tokenizers.TokenizerConfigOption{
tokenizers.WithCacheDir(tmpDir),
}, tmpDir
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts, cacheDir := tt.setupOpts(t)
tokenizer, err := tokenizers.FromPretrained(tt.modelID, opts...)
if gotErr := err != nil; gotErr != tt.wantErr {
t.Fatalf("expected error: %v, got error: %v", tt.wantErr, err)
}
if tt.wantErr {
return
}
if cacheDir != "" {
validateCache(t, cacheDir, tt.modelID)
}
if err := tokenizer.Close(); err != nil {
t.Fatalf("error closing tokenizer: %v", err)
}
})
}
}
func validateCache(t *testing.T, dir string, modelID string) {
t.Helper()
files := []string{"tokenizer.json", "vocab.txt"}
for _, file := range files {
path := filepath.Join(dir, modelID, file)
if _, err := os.Stat(path); err != nil {
t.Errorf("expected file %s to exist in cache for model %s", file, modelID)
}
}
}