forked from daulet/tokenizers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.go
285 lines (231 loc) · 7.38 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
package tokenizers
/*
#cgo LDFLAGS: ${SRCDIR}/libtokenizers.a -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 (
"io"
"unsafe"
)
type Offset struct {
Start uint32
End uint32
}
type TokenizerResult struct {
TokenIds []uint32
TypeIds []uint32
SpecialTokensMask []uint32
AttentionMask []uint32
Tokens []string
Offsets []Offset
}
type EncodeOptions struct {
AddSpecialTokens C.bool
ReturnTypeIds C.bool
ReturnSpecialTokensMask C.bool
ReturnAttentionMask C.bool
ReturnOffsets C.bool
WithOffsetsCharMode C.bool
}
type EncodeOption func(eo *EncodeOptions)
func WithReturnAll(withCharMode bool) EncodeOption {
return func(eo *EncodeOptions) {
eo.ReturnTypeIds = C.bool(true)
eo.ReturnSpecialTokensMask = C.bool(true)
eo.ReturnAttentionMask = C.bool(true)
eo.ReturnOffsets = C.bool(true)
eo.WithOffsetsCharMode = C.bool(withCharMode)
}
}
func WithReturnTypeIds() EncodeOption {
return func(eo *EncodeOptions) {
eo.ReturnTypeIds = C.bool(true)
}
}
func WithReturnSpecialTokensMask() EncodeOption {
return func(eo *EncodeOptions) {
eo.ReturnSpecialTokensMask = C.bool(true)
}
}
func WithReturnAttentionMask() EncodeOption {
return func(eo *EncodeOptions) {
eo.ReturnAttentionMask = C.bool(true)
}
}
func WithReturnOffsets() EncodeOption {
return func(eo *EncodeOptions) {
eo.ReturnOffsets = C.bool(true)
}
}
func WithReturnCharModeOffsets() EncodeOption {
return func(eo *EncodeOptions) {
eo.ReturnOffsets = C.bool(true)
eo.WithOffsetsCharMode = C.bool(true)
}
}
// uint vector to golang slice
func uintVecToSlice(arrPtr *C.uint, arrLen int) []uint32 {
slice := make([]uint32, arrLen)
for i, v := range unsafe.Slice(arrPtr, arrLen) {
slice[i] = uint32(v)
}
return slice
}
type Tokenizer struct {
tokenizer unsafe.Pointer
}
type TruncationDirection int
const (
TruncationDirectionLeft TruncationDirection = iota
TruncationDirectionRight
)
var _ io.Closer = (*Tokenizer)(nil)
func FromBytes(data []byte) (*Tokenizer, error) {
tokenizer := C.from_bytes((*C.uchar)(unsafe.Pointer(&data[0])), C.uint(len(data)))
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
}
func (t *Tokenizer) Close() error {
C.free_tokenizer(t.tokenizer)
t.tokenizer = nil
return nil
}
func (t *Tokenizer) Encode(str string, addSpecialTokens bool, opts ...EncodeOption) *TokenizerResult {
cStr := C.CString(str)
defer C.free(unsafe.Pointer(cStr))
encOptions := EncodeOptions{AddSpecialTokens: C.bool(addSpecialTokens)}
for _, opt := range opts {
opt(&encOptions)
}
res := C.encode(t.tokenizer, cStr, (*C.struct_EncodeOptions)(unsafe.Pointer(&encOptions)))
resLen := int(res.len)
if resLen == 0 {
return new(TokenizerResult)
}
defer C.free_buffer(res)
encodeResult := &TokenizerResult{Tokens: make([]string, resLen)}
// TokenIds
encodeResult.TokenIds = uintVecToSlice(res.ids, resLen)
// Tokens
for i, s := range (*[1 << 30]*C.char)(unsafe.Pointer(res.tokens))[:resLen:resLen] {
encodeResult.Tokens[i] = C.GoString(s)
}
// Token offsets
if encOptions.ReturnOffsets && res.offsets != nil {
encodeResult.Offsets = make([]Offset, resLen)
cOffsets := (*[1 << 30]C.struct_Offset)(unsafe.Pointer(res.offsets))
for i := 0; i < resLen; i++ {
encodeResult.Offsets[i] = Offset{
Start: uint32(uintptr(unsafe.Pointer(cOffsets[i].start))), // Convert C.uintptr_t to uintptr
End: uint32(uintptr(unsafe.Pointer(cOffsets[i].end))), // Convert C.uintptr_t to uintptr
}
}
}
// TypeIds
if encOptions.ReturnTypeIds && res.type_ids != nil {
encodeResult.TypeIds = uintVecToSlice(res.type_ids, resLen)
}
// SpecialTokensMask
if encOptions.ReturnSpecialTokensMask && res.special_tokens_mask != nil {
encodeResult.SpecialTokensMask = uintVecToSlice(res.special_tokens_mask, resLen)
}
// AttentionMask
if encOptions.ReturnAttentionMask && res.attention_mask != nil {
encodeResult.AttentionMask = uintVecToSlice(res.attention_mask, resLen)
}
return encodeResult
}
func (t *Tokenizer) EncodeBatch(strArr []string, addSpecialTokens bool, opts ...EncodeOption) []*TokenizerResult {
batchLen := len(strArr)
if batchLen == 0 {
return nil
}
// parse encode options
encOptions := EncodeOptions{AddSpecialTokens: C.bool(addSpecialTokens)}
for _, opt := range opts {
opt(&encOptions)
}
// Make string vector to Rust
cStrings := make([]*C.char, len(strArr))
for i, s := range strArr {
cStrings[i] = C.CString(s)
}
defer func() {
// release c-char
for i := range cStrings {
C.free(unsafe.Pointer(cStrings[i]))
}
}()
// call encode batch
batchRes := C.encode_batch(
t.tokenizer,
(**C.char)(unsafe.Pointer(&cStrings[0])),
(*C.struct_EncodeOptions)(unsafe.Pointer(&encOptions)),
)
// parse tokenizer encode result
batchResult := make([]*TokenizerResult, batchLen)
if batchLen > 0 {
defer C.free_batch_buffer(batchRes)
}
for i, encodeResult := range (*[1 << 30]C.struct_Buffer)(unsafe.Pointer(batchRes))[:batchLen:batchLen] {
subResLen := int(encodeResult.len)
subTokenizerResult := &TokenizerResult{Tokens: make([]string, subResLen)}
// TokenIds
subTokenizerResult.TokenIds = uintVecToSlice(encodeResult.ids, subResLen)
// Tokens
for j, s := range (*[1 << 30]*C.char)(unsafe.Pointer(encodeResult.tokens))[:subResLen:subResLen] {
subTokenizerResult.Tokens[j] = C.GoString(s)
}
// Token offsets
if encOptions.ReturnOffsets && encodeResult.offsets != nil {
subTokenizerResult.Offsets = make([]Offset, subResLen)
cOffsets := (*[1 << 30]C.struct_Offset)(unsafe.Pointer(encodeResult.offsets))
for j := 0; j < subResLen; j++ {
subTokenizerResult.Offsets[j] = Offset{
Start: uint32(uintptr(unsafe.Pointer(cOffsets[j].start))), // Convert C.uintptr_t to uintptr
End: uint32(uintptr(unsafe.Pointer(cOffsets[j].end))), // Convert C.uintptr_t to uintptr
}
}
}
// TypeIds
if encOptions.ReturnTypeIds && encodeResult.type_ids != nil {
subTokenizerResult.TypeIds = uintVecToSlice(encodeResult.type_ids, subResLen)
}
// SpecialTokensMask
if encOptions.ReturnSpecialTokensMask && encodeResult.special_tokens_mask != nil {
subTokenizerResult.SpecialTokensMask = uintVecToSlice(encodeResult.special_tokens_mask, subResLen)
}
// AttentionMask
if encOptions.ReturnAttentionMask && encodeResult.attention_mask != nil {
subTokenizerResult.AttentionMask = uintVecToSlice(encodeResult.attention_mask, subResLen)
}
batchResult[i] = subTokenizerResult
}
return batchResult
}
func (t *Tokenizer) Decode(tokenIDs []uint32, skipSpecialTokens bool) string {
if len(tokenIDs) == 0 {
return ""
}
res := C.decode(t.tokenizer, (*C.uint)(unsafe.Pointer(&tokenIDs[0])), C.uint(len(tokenIDs)), C.bool(skipSpecialTokens))
defer C.free_string(res)
return C.GoString(res)
}
func (t *Tokenizer) VocabSize() uint32 {
return uint32(C.vocab_size(t.tokenizer))
}