-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge_cases_test.go
More file actions
815 lines (757 loc) · 20.4 KB
/
edge_cases_test.go
File metadata and controls
815 lines (757 loc) · 20.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
806
807
808
809
810
811
812
813
814
815
package ztoken
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestBPETokenizer_RoundTripVaried verifies encode/decode identity on varied inputs.
func TestBPETokenizer_RoundTripVaried(t *testing.T) {
tok := makeTestBPE()
tests := []struct {
name string
input string
}{
{"single word", "hello"},
{"single char in vocab", "h"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
decoded, err := tok.Decode(ids)
if err != nil {
t.Fatalf("Decode(%v) error: %v", ids, err)
}
if decoded != tc.input {
t.Errorf("round-trip failed: %q -> %v -> %q", tc.input, ids, decoded)
}
})
}
}
// TestBPETokenizer_SentencePieceRoundTrip verifies round-trip identity for
// SentencePiece-mode tokenizers on varied inputs including unicode.
func TestBPETokenizer_SentencePieceRoundTrip(t *testing.T) {
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"\u2581hello": 3,
"\u2581world": 4,
"\u2581foo": 5,
"\u2581bar": 6,
"\u2581a": 7,
"\u2581": 8,
"h": 9,
"e": 10,
"l": 11,
"o": 12,
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, nil, special, false)
tok.SetSentencePiece(true)
tok.SetScores([]float32{
-100, -100, -100,
-1, -1, -1, -1, -1, -1,
-5, -5, -5, -5,
})
tests := []struct {
name string
input string
}{
{"two words", "hello world"},
{"three words", "foo bar a"},
{"single word", "hello"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
decoded, err := tok.Decode(ids)
if err != nil {
t.Fatalf("Decode(%v) error: %v", ids, err)
}
if decoded != tc.input {
t.Errorf("round-trip failed: %q -> %v -> %q", tc.input, ids, decoded)
}
})
}
}
// TestWhitespaceTokenizer_RoundTripVaried verifies encode/decode identity for
// the whitespace tokenizer on varied inputs.
func TestWhitespaceTokenizer_RoundTripVaried(t *testing.T) {
tok := NewWhitespaceTokenizer()
tok.AddToken("hello")
tok.AddToken("world")
tok.AddToken("foo")
tok.AddToken("bar")
tests := []struct {
name string
input string
}{
{"single word", "hello"},
{"four words", "hello world foo bar"},
{"repeated", "hello hello hello"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
decoded, err := tok.Decode(ids)
if err != nil {
t.Fatalf("Decode(%v) error: %v", ids, err)
}
if decoded != tc.input {
t.Errorf("round-trip failed: %q -> %v -> %q", tc.input, ids, decoded)
}
})
}
}
// TestBPETokenizer_UnicodeMultibyte tests encoding of multibyte unicode sequences.
func TestBPETokenizer_UnicodeMultibyte(t *testing.T) {
// Vocabulary with multibyte unicode tokens.
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"\u2581": 3, // ▁
"\u2581café": 4,
"\u2581naïve": 5,
"c": 6,
"a": 7,
"f": 8,
"é": 9,
// Byte fallback tokens for multibyte chars.
"<0xC3>": 10,
"<0xA9>": 11, // é in UTF-8
"<0xAF>": 12, // ï in UTF-8 (second byte)
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, nil, special, false)
tok.SetSentencePiece(true)
tok.SetScores([]float32{
-100, -100, -100, -100,
-1, -1, -5, -5, -5, -5, -5, -5, -5,
})
tests := []struct {
name string
input string
wantIDs []int
}{
{"accented word", "café", []int{4}},
{"diaeresis word", "naïve", []int{5}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
if len(ids) != len(tc.wantIDs) {
t.Fatalf("Encode(%q) = %v (len=%d), want %v", tc.input, ids, len(ids), tc.wantIDs)
}
for i, id := range ids {
if id != tc.wantIDs[i] {
t.Errorf("Encode(%q)[%d] = %d, want %d", tc.input, i, id, tc.wantIDs[i])
}
}
})
}
}
// TestBPETokenizer_ControlChars tests encoding of control characters.
func TestBPETokenizer_ControlChars(t *testing.T) {
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"hello": 3,
"\t": 4,
"\n": 5,
"<0x00>": 6,
"<0x01>": 7,
"<0x7F>": 8,
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, nil, special, false)
tests := []struct {
name string
input string
wantIDs []int
}{
// strings.Fields splits on whitespace and discards control chars,
// so tab/newline inputs produce no tokens.
{"tab character", "\t", []int{}},
{"newline character", "\n", []int{}},
// "hello\thello" splits to ["hello","hello"]; individual chars
// are not in vocab so each maps to UNK (0).
{"mixed text and tab", "hello\thello", []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
if len(ids) != len(tc.wantIDs) {
t.Fatalf("Encode(%q) = %v (len=%d), want %v", tc.input, ids, len(ids), tc.wantIDs)
}
for i, id := range ids {
if id != tc.wantIDs[i] {
t.Errorf("Encode(%q)[%d] = %d, want %d", tc.input, i, id, tc.wantIDs[i])
}
}
})
}
}
// TestBPETokenizer_SentencePieceByteFallback tests byte fallback encoding
// for characters not in the SentencePiece vocabulary.
func TestBPETokenizer_SentencePieceByteFallback(t *testing.T) {
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"\u2581": 3,
"a": 4,
"b": 5,
"<0xC3>": 6,
"<0xA9>": 7, // UTF-8 bytes for 'é' (0xC3, 0xA9)
"<0xF0>": 8,
"<0x9F>": 9,
"<0x98>": 10,
"<0x80>": 11, // UTF-8 bytes for '😀' (0xF0, 0x9F, 0x98, 0x80)
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, nil, special, false)
tok.SetSentencePiece(true)
tok.SetScores([]float32{
-100, -100, -100, -100,
-1, -1, -1, -1, -1, -1, -1, -1,
})
t.Run("byte fallback for accented char", func(t *testing.T) {
// 'é' not in vocab as a single token, so should use byte fallback.
ids, err := tok.Encode("é")
if err != nil {
t.Fatalf("Encode error: %v", err)
}
// Expect ▁ + <0xC3> + <0xA9>
if len(ids) < 2 {
t.Fatalf("expected byte fallback tokens, got %v", ids)
}
})
t.Run("byte fallback decode round-trip", func(t *testing.T) {
// Manually construct byte fallback tokens and decode them.
// ▁ + <0xC3> + <0xA9> should decode to "é"
decoded, err := tok.Decode([]int{3, 6, 7})
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if decoded != "é" {
t.Errorf("Decode byte fallback = %q, want %q", decoded, "é")
}
})
t.Run("emoji byte fallback decode", func(t *testing.T) {
// ▁ + <0xF0> + <0x9F> + <0x98> + <0x80> should decode to "😀"
decoded, err := tok.Decode([]int{3, 8, 9, 10, 11})
if err != nil {
t.Fatalf("Decode error: %v", err)
}
if decoded != "😀" {
t.Errorf("Decode emoji byte fallback = %q, want %q", decoded, "😀")
}
})
}
// TestBPETokenizer_EmptyAndWhitespace tests edge cases with empty and
// whitespace-only inputs.
func TestBPETokenizer_EmptyAndWhitespace(t *testing.T) {
tok := makeTestBPE()
tests := []struct {
name string
input string
wantLen int
}{
{"empty string", "", 0},
{"single space", " ", 0},
{"multiple spaces", " ", 0},
{"tab only", "\t", 0},
{"newline only", "\n", 0},
{"mixed whitespace", " \t\n\r ", 0},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
if len(ids) != tc.wantLen {
t.Errorf("Encode(%q) len = %d, want %d; got %v", tc.input, len(ids), tc.wantLen, ids)
}
})
}
}
// TestDecodeSentencePieceBytes tests the decodeSentencePieceBytes helper
// with various hex byte patterns.
func TestDecodeSentencePieceBytes(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"no hex bytes", "hello", "hello"},
{"single hex byte", "<0x41>", "A"},
{"multiple hex bytes", "<0x48><0x69>", "Hi"},
{"mixed text and hex", "hello<0x21>", "hello!"},
{"incomplete pattern", "<0x4", "<0x4"},
{"invalid hex digit", "<0xGG>", "<0xGG>"},
{"lowercase hex", "<0xff>", "\xff"},
{"null byte", "<0x00>", "\x00"},
{"uppercase hex", "<0xFF>", "\xff"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := decodeSentencePieceBytes(tc.input)
if got != tc.want {
t.Errorf("decodeSentencePieceBytes(%q) = %q, want %q", tc.input, got, tc.want)
}
})
}
}
// TestUnhex tests the unhex helper for all valid and invalid inputs.
func TestUnhex(t *testing.T) {
tests := []struct {
input byte
want int
}{
{'0', 0}, {'1', 1}, {'9', 9},
{'A', 10}, {'F', 15},
{'a', 10}, {'f', 15},
{'G', -1}, {'g', -1}, {'/', -1}, {':', -1}, {' ', -1},
}
for _, tc := range tests {
t.Run(string(tc.input), func(t *testing.T) {
got := unhex(tc.input)
if got != tc.want {
t.Errorf("unhex(%q) = %d, want %d", tc.input, got, tc.want)
}
})
}
}
// TestWordPieceTokenizer_UnicodeMultibyte tests WordPiece with multibyte
// unicode tokens.
func TestWordPieceTokenizer_UnicodeMultibyte(t *testing.T) {
vocab := map[string]int{
"[PAD]": 0,
"[UNK]": 1,
"[CLS]": 2,
"[SEP]": 3,
"café": 4,
"naïve": 5,
"über": 6,
}
special := SpecialTokens{BOS: 2, EOS: 3, PAD: 0, UNK: 1}
tok := NewWordPieceTokenizer(vocab, special)
tests := []struct {
name string
input string
wantIDs []int
}{
{"accented", "café", []int{4}},
{"diaeresis", "naïve", []int{5}},
{"umlaut", "über", []int{6}},
{"unknown multibyte", "résumé", []int{1}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
if len(ids) != len(tc.wantIDs) {
t.Fatalf("Encode(%q) = %v (len=%d), want %v", tc.input, ids, len(ids), tc.wantIDs)
}
for i, id := range ids {
if id != tc.wantIDs[i] {
t.Errorf("Encode(%q)[%d] = %d, want %d", tc.input, i, id, tc.wantIDs[i])
}
}
})
}
}
// TestWordPieceTokenizer_ControlChars tests encoding of strings containing
// control characters in WordPiece mode.
func TestWordPieceTokenizer_ControlChars(t *testing.T) {
vocab := map[string]int{
"[PAD]": 0,
"[UNK]": 1,
"[CLS]": 2,
"[SEP]": 3,
"hello": 4,
"world": 5,
}
special := SpecialTokens{BOS: 2, EOS: 3, PAD: 0, UNK: 1}
tok := NewWordPieceTokenizer(vocab, special)
tests := []struct {
name string
input string
wantLen int
}{
{"tab separated", "hello\tworld", 2},
{"newline separated", "hello\nworld", 2},
{"only whitespace", "\t\n\r", 0},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
if len(ids) != tc.wantLen {
t.Errorf("Encode(%q) len = %d, want %d; got %v", tc.input, len(ids), tc.wantLen, ids)
}
})
}
}
// TestLoadFromJSON_MalformedMetadata tests error paths for malformed tokenizer
// JSON metadata.
func TestLoadFromJSON_MalformedMetadata(t *testing.T) {
tests := []struct {
name string
json string
wantErr bool
}{
{
name: "empty object",
json: `{}`,
wantErr: false, // empty vocab is valid
},
{
name: "null model",
json: `{"model": null}`,
wantErr: false,
},
{
name: "missing vocab key",
json: `{"model": {"type": "BPE"}}`,
wantErr: false,
},
{
name: "invalid merges type",
json: `{"model": {"type": "BPE", "vocab": {}, "merges": 42}}`,
wantErr: true,
},
{
name: "merges with three-element array",
json: `{"model": {"type": "BPE", "vocab": {}, "merges": [["a","b","c"]]}}`,
wantErr: true,
},
{
name: "truncated JSON",
json: `{"model": {"type": "BPE"`,
wantErr: true,
},
{
name: "empty merges string entry",
json: `{"model": {"type": "BPE", "vocab": {}, "merges": [""]}}`,
wantErr: true,
},
{
name: "non-BPE model type",
json: `{"model": {"type": "Unigram"}}`,
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tokenizer.json")
if err := os.WriteFile(path, []byte(tc.json), 0o600); err != nil {
t.Fatal(err)
}
_, err := LoadFromJSON(path)
if tc.wantErr && err == nil {
t.Error("expected error, got nil")
}
if !tc.wantErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}
// TestLoad_MalformedMetadata tests error paths for the Load function with
// various malformed JSON inputs.
func TestLoad_MalformedMetadata(t *testing.T) {
tests := []struct {
name string
json string
wantErr bool
}{
{
name: "completely empty file",
json: ``,
wantErr: true,
},
{
name: "binary content",
json: "\x00\x01\x02\x03",
wantErr: true,
},
{
name: "unsupported model type",
json: `{"model": {"type": "Unigram"}, "added_tokens": []}`,
wantErr: true,
},
{
name: "WordPiece with missing vocab",
json: `{"model": {"type": "WordPiece"}, "added_tokens": []}`,
wantErr: false, // empty vocab is valid
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tokenizer.json")
if err := os.WriteFile(path, []byte(tc.json), 0o600); err != nil {
t.Fatal(err)
}
_, err := Load(path)
if tc.wantErr && err == nil {
t.Error("expected error, got nil")
}
if !tc.wantErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}
// TestLoadFromJSON_EmptyFile tests loading from a zero-byte file.
func TestLoadFromJSON_EmptyFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "tokenizer.json")
if err := os.WriteFile(path, nil, 0o600); err != nil {
t.Fatal(err)
}
_, err := LoadFromJSON(path)
if err == nil {
t.Error("expected error for empty file, got nil")
}
}
// TestBPETokenizer_SentencePieceNewlines tests that newlines are handled
// correctly in SentencePiece mode.
func TestBPETokenizer_SentencePieceNewlines(t *testing.T) {
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"\u2581hello": 3,
"\u2581world": 4,
"\n": 5,
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, nil, special, false)
tok.SetSentencePiece(true)
tok.SetScores([]float32{-100, -100, -100, -1, -1, -1})
ids, err := tok.Encode("hello\nworld")
if err != nil {
t.Fatalf("Encode error: %v", err)
}
// Expect: ▁hello, \n, world (world may not have ▁ after newline)
if len(ids) < 2 {
t.Fatalf("expected at least 2 tokens, got %v", ids)
}
// First token should be ▁hello
if ids[0] != 3 {
t.Errorf("first token = %d, want 3 (▁hello)", ids[0])
}
}
// TestBPETokenizer_SetAddLeadingSpace tests the addLeadingSpace toggle.
func TestBPETokenizer_SetAddLeadingSpace(t *testing.T) {
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"\u2581hello": 3,
"hello": 4,
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, nil, special, false)
tok.SetSentencePiece(true)
tok.SetScores([]float32{-100, -100, -100, -1, -1})
t.Run("with leading space", func(t *testing.T) {
tok.SetAddLeadingSpace(true)
ids, err := tok.Encode("hello")
if err != nil {
t.Fatalf("Encode error: %v", err)
}
if len(ids) != 1 || ids[0] != 3 {
t.Errorf("with leading space: Encode(\"hello\") = %v, want [3]", ids)
}
})
t.Run("without leading space", func(t *testing.T) {
tok.SetAddLeadingSpace(false)
ids, err := tok.Encode("hello")
if err != nil {
t.Fatalf("Encode error: %v", err)
}
if len(ids) != 1 || ids[0] != 4 {
t.Errorf("without leading space: Encode(\"hello\") = %v, want [4]", ids)
}
})
}
// TestBPETokenizer_EncodeWithSpecialsAndSegments tests encoding text that
// contains special token strings mixed with regular text.
func TestBPETokenizer_EncodeWithSpecialsAndSegments(t *testing.T) {
vocab := map[string]int{
"<unk>": 0,
"<s>": 1,
"</s>": 2,
"<start_of_turn>": 3,
"<end_of_turn>": 4,
"hello": 5,
"world": 6,
"h": 7,
"e": 8,
"l": 9,
"o": 10,
"he": 11,
"lo": 12,
"hel": 13,
}
merges := []MergePair{
{Left: "h", Right: "e"},
{Left: "l", Right: "o"},
{Left: "he", Right: "l"},
{Left: "hel", Right: "lo"},
}
special := SpecialTokens{BOS: 1, EOS: 2, UNK: 0}
tok := NewBPETokenizer(vocab, merges, special, false)
tok.SetSpecialTokenStrings(map[string]int{
"<start_of_turn>": 3,
"<end_of_turn>": 4,
})
ids, err := tok.Encode("<start_of_turn>hello<end_of_turn>")
if err != nil {
t.Fatalf("Encode error: %v", err)
}
want := []int{3, 5, 4}
if len(ids) != len(want) {
t.Fatalf("Encode = %v, want %v", ids, want)
}
for i, id := range ids {
if id != want[i] {
t.Errorf("[%d] = %d, want %d", i, id, want[i])
}
}
}
// TestBPETokenizer_DecodeUnknownID tests that Decode returns an error for
// unknown token IDs.
func TestBPETokenizer_DecodeUnknownID(t *testing.T) {
tok := makeTestBPE()
_, err := tok.Decode([]int{9999})
if err == nil {
t.Error("expected error for unknown token ID, got nil")
}
if !strings.Contains(err.Error(), "unknown token ID") {
t.Errorf("error = %q, want containing 'unknown token ID'", err.Error())
}
}
// TestBPETokenizer_NormalizerApplied tests that the normalizer is applied
// before encoding.
func TestBPETokenizer_NormalizerApplied(t *testing.T) {
tok := makeTestBPE()
tok.normalizer = strings.ToLower
// "HELLO" lowercased to "hello" which is token 17.
ids, err := tok.Encode("HELLO")
if err != nil {
t.Fatalf("Encode error: %v", err)
}
if len(ids) != 1 || ids[0] != 17 {
t.Errorf("Encode(\"HELLO\") with lowercase normalizer = %v, want [17]", ids)
}
}
// TestWordPieceTokenizer_EmptyAndWhitespace tests edge cases for WordPiece.
func TestWordPieceTokenizer_EmptyAndWhitespace(t *testing.T) {
tok := testWordPieceTokenizer()
tests := []struct {
name string
input string
wantLen int
}{
{"empty", "", 0},
{"spaces only", " ", 0},
{"tabs only", "\t\t", 0},
{"newlines only", "\n\n\n", 0},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ids, err := tok.Encode(tc.input)
if err != nil {
t.Fatalf("Encode(%q) error: %v", tc.input, err)
}
if len(ids) != tc.wantLen {
t.Errorf("Encode(%q) len = %d, want %d", tc.input, len(ids), tc.wantLen)
}
})
}
}
// TestPreTokenize_Unicode tests pre-tokenization with unicode punctuation
// and mixed scripts.
func TestPreTokenize_Unicode(t *testing.T) {
tests := []struct {
name string
input string
want []string
}{
{"ascii punctuation", "hello,world!", []string{"hello", ",", "world", "!"}},
{"unicode em-dash", "hello\u2014world", []string{"hello", "\u2014", "world"}},
{"cjk characters", "hello世界", []string{"hello世界"}},
{"mixed with spaces", "a , b", []string{"a", ",", "b"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := preTokenize(tc.input)
if len(got) != len(tc.want) {
t.Fatalf("preTokenize(%q) = %v (len=%d), want %v (len=%d)",
tc.input, got, len(got), tc.want, len(tc.want))
}
for i := range got {
if got[i] != tc.want[i] {
t.Errorf("preTokenize(%q)[%d] = %q, want %q", tc.input, i, got[i], tc.want[i])
}
}
})
}
}
// TestIsPrintableGPT2Byte tests the GPT-2 printable byte classification.
func TestIsPrintableGPT2Byte(t *testing.T) {
tests := []struct {
name string
b byte
want bool
}{
{"exclamation", '!', true},
{"tilde", '~', true},
{"space", ' ', false},
{"null", 0, false},
{"tab", '\t', false},
{"newline", '\n', false},
{"DEL", 0x7F, false},
{"latin supplement start", 0xA1, true},
{"0xAC", 0xAC, true},
{"0xAD", 0xAD, false},
{"0xAE", 0xAE, true},
{"0xFF", 0xFF, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := isPrintableGPT2Byte(tc.b)
if got != tc.want {
t.Errorf("isPrintableGPT2Byte(0x%02X) = %v, want %v", tc.b, got, tc.want)
}
})
}
}