-
Notifications
You must be signed in to change notification settings - Fork 23
/
tokenizer.go
417 lines (350 loc) · 10.5 KB
/
tokenizer.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
package tokenizers
// TODO packaging: how do we build the rust lib for distribution?
/*
#cgo LDFLAGS: -ltokenizers -ldl -lm -lstdc++
#include <stdlib.h>
#include "tokenizers.h"
*/
import "C"
// NOTE: There should be NO space between the comments and the `import "C"` line.
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"unsafe"
)
const (
WANT_VERSION = "1.20.2"
baseURL = "https://huggingface.co"
)
// List of necessary tokenizer files and their mandatory status.
// True means mandatory, false means optional.
var tokenizerFiles = map[string]bool{
"tokenizer.json": true,
"vocab.txt": false,
"merges.txt": false,
"special_tokens_map.json": false,
"added_tokens.json": false,
}
func init() {
version := C.version()
got := C.GoString(version)
if got != WANT_VERSION {
panic(fmt.Errorf("tokenizers library version mismatch, want: %s, got: %s", WANT_VERSION, got))
}
}
type Tokenizer struct {
tokenizer unsafe.Pointer
}
type tokenizerOpts struct {
encodeSpecialTokens C.bool
}
type TokenizerOption func(to *tokenizerOpts)
func WithEncodeSpecialTokens() TokenizerOption {
return func(to *tokenizerOpts) {
to.encodeSpecialTokens = C.bool(true)
}
}
type TruncationDirection int
const (
TruncationDirectionLeft TruncationDirection = iota
TruncationDirectionRight
)
var _ io.Closer = (*Tokenizer)(nil)
func FromBytes(data []byte, opts ...TokenizerOption) (*Tokenizer, error) {
allOpts := &tokenizerOpts{
// by default, we do not encode special tokens
encodeSpecialTokens: C.bool(false),
}
for _, opt := range opts {
opt(allOpts)
}
tokenizer := C.from_bytes((*C.uchar)(unsafe.Pointer(&data[0])), C.uint(len(data)), (*C.struct_TokenizerOptions)(unsafe.Pointer(allOpts)))
return &Tokenizer{tokenizer: tokenizer}, nil
}
func FromBytesWithTruncation(data []byte, maxLen uint32, dir TruncationDirection) (*Tokenizer, error) {
tokenizer := C.from_bytes_with_truncation((*C.uchar)(unsafe.Pointer(&data[0])), C.uint(len(data)), C.uint(maxLen), C.uchar(dir))
return &Tokenizer{tokenizer: tokenizer}, nil
}
func FromFile(path string) (*Tokenizer, error) {
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
tokenizer, err := C.from_file(cPath)
if err != nil {
return nil, err
}
return &Tokenizer{tokenizer: tokenizer}, nil
}
type tokenizerConfig struct {
cacheDir *string
authToken *string
}
type TokenizerConfigOption func(cfg *tokenizerConfig)
func WithCacheDir(path string) TokenizerConfigOption {
return func(cfg *tokenizerConfig) {
cfg.cacheDir = &path
}
}
func WithAuthToken(token string) TokenizerConfigOption {
return func(cfg *tokenizerConfig) {
cfg.authToken = &token
}
}
// FromPretrained downloads necessary files and initializes the tokenizer.
// Parameters:
// - modelID: The Hugging Face model identifier (e.g., "bert-base-uncased").
// - destination: Optional. If provided and not nil, files will be downloaded to this folder.
// If nil, a temporary directory will be used.
// - authToken: Optional. If provided and not nil, it will be used to authenticate requests.
func FromPretrained(modelID string, opts ...TokenizerConfigOption) (*Tokenizer, error) {
cfg := &tokenizerConfig{}
for _, opt := range opts {
opt(cfg)
}
if strings.TrimSpace(modelID) == "" {
return nil, fmt.Errorf("modelID cannot be empty")
}
// Construct the model URL
modelURL := fmt.Sprintf("%s/%s/resolve/main", baseURL, modelID)
// Determine the download directory
var downloadDir string
if cfg.cacheDir != nil {
downloadDir = fmt.Sprintf("%s/%s", *cfg.cacheDir, modelID)
// Create the destination directory if it doesn't exist
err := os.MkdirAll(downloadDir, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("failed to create destination directory %s: %w", downloadDir, err)
}
} else {
// Create a temporary directory
tmpDir, err := os.MkdirTemp("", "huggingface-tokenizer-*")
if err != nil {
return nil, fmt.Errorf("error creating temporary directory: %w", err)
}
downloadDir = tmpDir
}
var wg sync.WaitGroup
errCh := make(chan error)
// Download each tokenizer file concurrently
for filename, isMandatory := range tokenizerFiles {
wg.Add(1)
go func(fn string, mandatory bool) {
defer wg.Done()
fileURL := fmt.Sprintf("%s/%s", modelURL, fn)
destPath := filepath.Join(downloadDir, fn)
err := downloadFile(fileURL, destPath, cfg.authToken)
if err != nil && mandatory {
// If the file is mandatory, report an error
errCh <- fmt.Errorf("failed to download mandatory file %s: %w", fn, err)
}
}(filename, isMandatory)
}
go func() {
wg.Wait()
close(errCh)
}()
var errs []error
for err := range errCh {
errs = append(errs, err)
}
if len(errs) > 0 {
if err := os.RemoveAll(downloadDir); err != nil {
fmt.Printf("Warning: failed to clean up directory %s: %v\n", downloadDir, err)
}
return nil, errs[0]
}
return FromFile(filepath.Join(downloadDir, "tokenizer.json"))
}
// downloadFile downloads a file from the given URL and saves it to the specified destination.
// If authToken is provided (non-nil), it will be used for authorization.
// Returns an error if the download fails.
func downloadFile(url, destination string, authToken *string) error {
// Check if the file already exists
if _, err := os.Stat(destination); err == nil {
return nil
}
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("failed to create request for %s: %w", url, err)
}
// If authToken is provided, set the Authorization header
if authToken != nil {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *authToken))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download from %s: %w", url, err)
}
defer resp.Body.Close()
// Check for successful response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download from %s: status code %d", url, resp.StatusCode)
}
// Create the destination file
out, err := os.Create(destination)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", destination, err)
}
defer out.Close()
// Write the response body to the file
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("failed to write to file %s: %w", destination, err)
}
fmt.Printf("Successfully downloaded %s\n", destination)
return nil
}
func (t *Tokenizer) Close() error {
C.free_tokenizer(t.tokenizer)
t.tokenizer = nil
return nil
}
type Offset [2]uint
type Encoding struct {
IDs []uint32
TypeIDs []uint32
SpecialTokensMask []uint32
AttentionMask []uint32
Tokens []string
Offsets []Offset
}
type encodeOpts struct {
AddSpecialTokens C.bool
ReturnTypeIDs C.bool
ReturnTokens C.bool
ReturnSpecialTokensMask C.bool
ReturnAttentionMask C.bool
ReturnOffsets C.bool
}
type EncodeOption func(eo *encodeOpts)
func uintVecToSlice(arrPtr *C.uint, len int) []uint32 {
arr := unsafe.Slice(arrPtr, len)
slice := make([]uint32, len)
for i, v := range arr {
slice[i] = uint32(v)
}
return slice
}
func offsetVecToSlice(arrPtr *C.size_t, tokenLength int) []Offset {
arr := unsafe.Slice(arrPtr, tokenLength*2)
slice := make([]Offset, tokenLength)
counter := 0
for i := 0; i < tokenLength; i++ {
offset := Offset{uint(arr[counter]), uint(arr[counter+1])}
slice[i] = offset
counter = counter + 2
}
return slice
}
func (t *Tokenizer) Encode(str string, addSpecialTokens bool) ([]uint32, []string) {
cStr := C.CString(str)
defer C.free(unsafe.Pointer(cStr))
options := encodeOpts{
AddSpecialTokens: C.bool(addSpecialTokens),
ReturnTokens: C.bool(true),
}
res := C.encode(t.tokenizer, cStr, (*C.struct_EncodeOptions)(unsafe.Pointer(&options)))
len := int(res.len)
if len == 0 {
return nil, nil
}
defer C.free_buffer(res)
ids := uintVecToSlice(res.ids, len)
var tokens []string
if res.tokens != nil {
tokens = make([]string, len)
for i, s := range (*[1 << 30]*C.char)(unsafe.Pointer(res.tokens))[:len:len] {
tokens[i] = C.GoString(s)
}
}
return ids, tokens
}
func WithReturnAllAttributes() EncodeOption {
return func(eo *encodeOpts) {
eo.ReturnTypeIDs = C.bool(true)
eo.ReturnSpecialTokensMask = C.bool(true)
eo.ReturnAttentionMask = C.bool(true)
eo.ReturnTokens = C.bool(true)
eo.ReturnOffsets = C.bool(true)
}
}
func WithReturnTypeIDs() EncodeOption {
return func(eo *encodeOpts) {
eo.ReturnTypeIDs = C.bool(true)
}
}
func WithReturnSpecialTokensMask() EncodeOption {
return func(eo *encodeOpts) {
eo.ReturnSpecialTokensMask = C.bool(true)
}
}
func WithReturnTokens() EncodeOption {
return func(eo *encodeOpts) {
eo.ReturnTokens = C.bool(true)
}
}
func WithReturnAttentionMask() EncodeOption {
return func(eo *encodeOpts) {
eo.ReturnAttentionMask = C.bool(true)
}
}
func WithReturnOffsets() EncodeOption {
return func(eo *encodeOpts) {
eo.ReturnOffsets = C.bool(true)
}
}
func (t *Tokenizer) EncodeWithOptions(str string, addSpecialTokens bool, opts ...EncodeOption) Encoding {
cStr := C.CString(str)
defer C.free(unsafe.Pointer(cStr))
encOptions := encodeOpts{
AddSpecialTokens: C.bool(addSpecialTokens),
}
for _, opt := range opts {
opt(&encOptions)
}
res := C.encode(t.tokenizer, cStr, (*C.struct_EncodeOptions)(unsafe.Pointer(&encOptions)))
len := int(res.len)
if len == 0 {
return Encoding{}
}
defer C.free_buffer(res)
encoding := Encoding{}
encoding.IDs = uintVecToSlice(res.ids, len)
if encOptions.ReturnTypeIDs && res.type_ids != nil {
encoding.TypeIDs = uintVecToSlice(res.type_ids, len)
}
if encOptions.ReturnTokens && res.tokens != nil {
tokens := make([]string, len)
for i, s := range (*[1 << 30]*C.char)(unsafe.Pointer(res.tokens))[:len:len] {
tokens[i] = C.GoString(s)
}
encoding.Tokens = tokens
}
if encOptions.ReturnSpecialTokensMask && res.special_tokens_mask != nil {
encoding.SpecialTokensMask = uintVecToSlice(res.special_tokens_mask, len)
}
if encOptions.ReturnAttentionMask && res.attention_mask != nil {
encoding.AttentionMask = uintVecToSlice(res.attention_mask, len)
}
if encOptions.ReturnOffsets && res.offsets != nil {
encoding.Offsets = offsetVecToSlice(res.offsets, len)
}
return encoding
}
func (t *Tokenizer) Decode(tokenIDs []uint32, skipSpecialTokens bool) string {
if len(tokenIDs) == 0 {
return ""
}
len := C.uint(len(tokenIDs))
res := C.decode(t.tokenizer, (*C.uint)(unsafe.Pointer(&tokenIDs[0])), len, C.bool(skipSpecialTokens))
defer C.free_string(res)
return C.GoString(res)
}
func (t *Tokenizer) VocabSize() uint32 {
return uint32(C.vocab_size(t.tokenizer))
}