forked from daulet/tokenizers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.go
246 lines (204 loc) · 5.86 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
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 (
"io"
"unsafe"
)
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
}
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))
}