forked from pkoukk/tiktoken-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_bpe.go
191 lines (170 loc) · 4.78 KB
/
core_bpe.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
package tiktoken
import (
"bytes"
"errors"
"fmt"
"regexp"
"sort"
"strings"
"github.com/dlclark/regexp2"
)
type CoreBPE struct {
encoder map[string]int
decoder map[int]string
specialTokensEncoder map[string]int
specialTokensDecoder map[int]string
tlRegex *regexp2.Regexp
tlSpecialRegex *regexp2.Regexp
sortedTokenBytes [][]byte
}
func NewCoreBPE(encoder map[string]int, specialTokensEncoder map[string]int, pattern string) (*CoreBPE, error) {
regex, err := regexp2.Compile(pattern, regexp2.None)
if err != nil {
return nil, fmt.Errorf("error compiling regex: %s", err)
}
specialRegexStrs := make([]string, 0, len(specialTokensEncoder))
for k := range specialTokensEncoder {
specialRegexStrs = append(specialRegexStrs, regexp.QuoteMeta(k))
}
specialRegex, err := regexp2.Compile(strings.Join(specialRegexStrs, "|"), regexp2.None)
if err != nil {
return nil, fmt.Errorf("error compiling special regex: %s", err)
}
decoder := make(map[int]string, len(encoder))
for k, v := range encoder {
decoder[v] = k
}
if len(encoder) != len(decoder) {
return nil, errors.New("encoder and decoder map sizes are different")
}
specialTokensDecoder := make(map[int]string, len(specialTokensEncoder))
for k, v := range specialTokensEncoder {
specialTokensDecoder[v] = k
}
sortedTokenBytes := make([][]byte, 0, len(encoder))
for k := range encoder {
sortedTokenBytes = append(sortedTokenBytes, []byte(k))
}
sort.Slice(sortedTokenBytes, func(i, j int) bool {
return bytes.Compare(sortedTokenBytes[i], sortedTokenBytes[j]) < 0
})
return &CoreBPE{
encoder: encoder,
specialTokensEncoder: specialTokensEncoder,
decoder: decoder,
specialTokensDecoder: specialTokensDecoder,
tlRegex: regex,
tlSpecialRegex: specialRegex,
sortedTokenBytes: sortedTokenBytes,
}, nil
}
func (bp *CoreBPE) encodeNative(text string, allowedSpecial map[string]any) ([]int, int) {
specialRegex := bp.tlSpecialRegex
regex := bp.tlRegex
ret := []int{}
lastPieceTokenLen := 0
textRunes := []rune(text)
start := 0
for {
var nextSpecial []int
startFind := start
for {
// Find the next allowed special token, if any
temp := cutRunes(textRunes, startFind, len(textRunes))
nextSpecial = findRegex2StringIndex(temp, specialRegex)
if nextSpecial != nil {
token := cutRunes(textRunes, startFind+nextSpecial[0], startFind+nextSpecial[1])
if _, ok := allowedSpecial[token]; ok {
break
}
startFind += nextSpecial[1]
} else {
break
}
}
end := len([]rune(text))
if nextSpecial != nil {
end = start + nextSpecial[0]
}
// Okay, here we go, compare this logic to _encode_ordinary_native
for _, mat := range findRegex2AllStringMatchIndex(cutRunes(textRunes, start, end), regex) {
piece := cutRunes(textRunes, start+mat[0], start+mat[1])
if token, ok := bp.encoder[piece]; ok {
lastPieceTokenLen = 1
ret = append(ret, token)
continue
}
tokens := bytePairEncode([]byte(piece), bp.encoder)
lastPieceTokenLen = len(tokens)
ret = append(ret, tokens...)
}
if nextSpecial != nil {
temp := cutRunes(textRunes, start+nextSpecial[0], start+nextSpecial[1])
token := bp.specialTokensEncoder[temp]
ret = append(ret, token)
start = start + nextSpecial[1]
lastPieceTokenLen = 0
} else {
break
}
}
return ret, lastPieceTokenLen
}
func (bp *CoreBPE) encodeOrdinaryNative(text string) []int {
ret := []int{}
textRunes := []rune(text)
for _, mat := range findRegex2AllStringMatchIndex(text, bp.tlRegex) {
piece := cutRunes(textRunes, mat[0], mat[1])
if token, ok := bp.encoder[piece]; ok {
ret = append(ret, token)
continue
}
tokens := bytePairEncode([]byte(piece), bp.encoder)
ret = append(ret, tokens...)
}
return ret
}
func (bpe *CoreBPE) decodeNative(tokens []int) []byte {
ret := make([]byte, 0, len(tokens)*2)
for _, token := range tokens {
tokenBytes, ok := bpe.decoder[token]
if !ok {
tokenBytes = bpe.specialTokensDecoder[token]
}
if len(tokenBytes) > 0 {
ret = append(ret, tokenBytes...)
}
}
return ret
}
func findRegex2StringIndex(text string, reg *regexp2.Regexp) []int {
m, _ := reg.FindStringMatch(text)
if m == nil {
return nil
}
result := make([]int, 2)
result[0] = m.Index
result[1] = m.Index + m.Length
return result
}
func findRegex2AllStringMatchIndex(text string, reg *regexp2.Regexp) [][]int {
var matches [][]int
m, _ := reg.FindStringMatch(text)
for m != nil {
result := make([]int, 2)
result[0] = m.Index
result[1] = m.Index + m.Length
matches = append(matches, result)
m, _ = reg.FindNextMatch(m)
}
return matches
}
func cutRunes(runes []rune, start, end int) string {
if start < 0 {
start = 0
}
if end > len(runes) {
end = len(runes)
}
return string(runes[start:end])
}