-
Notifications
You must be signed in to change notification settings - Fork 23
/
tokenizer_test.go
580 lines (544 loc) · 21.3 KB
/
tokenizer_test.go
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
package tokenizers_test
import (
_ "embed"
"math/rand"
"os"
"path/filepath"
"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
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",
addSpecial: false,
},
}
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 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) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(b, err)
defer tk.Close()
expected := []uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ids, _ := tk.Encode("brown fox jumps over the lazy dog", false)
assert.Equal(b, expected, ids)
}
}
func BenchmarkEncodeNChars(b *testing.B) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(b, err)
defer tk.Close()
vocabSize := 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()
_, tokens := tk.Encode(str, false)
assert.Greater(b, len(tokens), 0)
}
func BenchmarkDecodeNTimes(b *testing.B) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(b, err)
defer tk.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
str := tk.Decode([]uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899}, true)
assert.Equal(b, "brown fox jumps over the lazy dog", str)
}
}
func BenchmarkDecodeNTokens(b *testing.B) {
tk, err := tokenizers.FromFile("./test/data/bert-base-uncased.json")
require.NoError(b, err)
defer tk.Close()
vocabSize := tk.VocabSize()
input := make([]uint32, 0, b.N)
for i := 0; i < b.N; i++ {
input = append(input, rand.Uint32()%vocabSize)
}
b.ResetTimer()
text := tk.Decode(input, true)
// a token is one or more characters
assert.Greater(b, len(text), b.N)
}
func TestFromPretrained(t *testing.T) {
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 valid auth token",
modelID: "bert-base-uncased",
expectedToken: true,
setupOpts: func(t *testing.T) ([]tokenizers.TokenizerConfigOption, string) {
tmpDir := t.TempDir()
return []tokenizers.TokenizerConfigOption{
tokenizers.WithCacheDir(tmpDir),
tokenizers.WithAuthToken("test-token"),
}, tmpDir
},
},
{
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)
}
}
}