forked from sugarme/tokenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroberta.go
More file actions
222 lines (183 loc) · 6.84 KB
/
roberta.go
File metadata and controls
222 lines (183 loc) · 6.84 KB
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
package processor
import (
"github.com/Canva/tokenizer"
"github.com/Canva/tokenizer/pretokenizer"
)
// RobertaProcessing is a post post processor for Roberta model
type RobertaProcessing struct {
sep PostToken
cls PostToken
trimOffsets bool
addPrefixSpace bool
}
// DefaultRobertaProcessing creates a RobertaProcessing with default values
func DefaultRobertaProcessing() *RobertaProcessing {
return &RobertaProcessing{
sep: PostToken{Value: "</s>", Id: 2},
cls: PostToken{Value: "<s>", Id: 0},
trimOffsets: true,
addPrefixSpace: true,
}
}
func NewRobertaProcessing(sep, cls PostToken, trimOffsets bool, addPrefixSpace bool) *RobertaProcessing {
return &RobertaProcessing{
sep: sep,
cls: cls,
trimOffsets: trimOffsets,
addPrefixSpace: addPrefixSpace,
}
}
// TrimOffsets set whether the processor will trim offsets
func (rp *RobertaProcessing) TrimOffsets(trimOffsets bool) {
rp.trimOffsets = trimOffsets
}
// AddPrefixSpace set whether the processor will add a prefix space
func (rp *RobertaProcessing) AddPrefixSpace(addPrefixSpace bool) {
rp.addPrefixSpace = addPrefixSpace
}
// Implement PostProcessor interface for RobertaProcessing:
// ========================================================
func (rp *RobertaProcessing) AddedTokens(isPair bool) int {
if isPair {
return 4
} else {
return 2
}
}
// Process post-processes input encoding(s) by adding special tokens if instructed to do so.
//
// Specifically, if addSpecialToken=true, it will add special tokens patterns
// - Single encoding: <s> Sequence </s>
// - Pair encoding: <s> SequenceA </s> </s> SequenceB </s>
func (rp *RobertaProcessing) Process(encoding, pairEncoding *tokenizer.Encoding, addSpecialTokens bool) *tokenizer.Encoding {
var (
newEncoding *tokenizer.Encoding
newOverflowEncodings []tokenizer.Encoding
newPairEncoding *tokenizer.Encoding
newOverflowPairEncoding []tokenizer.Encoding
)
if rp.trimOffsets {
newEncoding = pretokenizer.ProcessOffsets(encoding, rp.addPrefixSpace)
overflowEncodings := newEncoding.GetOverflowing()
for _, e := range overflowEncodings {
newEn := pretokenizer.ProcessOffsets(&e, rp.addPrefixSpace)
newOverflowEncodings = append(newOverflowEncodings, *newEn)
}
newEncoding.Overflowing = newOverflowEncodings
if pairEncoding != nil {
newPairEncoding = pretokenizer.ProcessOffsets(pairEncoding, rp.addPrefixSpace)
for _, en := range newPairEncoding.Overflowing {
newEn := pretokenizer.ProcessOffsets(&en, rp.addPrefixSpace)
newOverflowPairEncoding = append(newOverflowPairEncoding, *newEn)
}
newPairEncoding.Overflowing = newOverflowPairEncoding
}
}
if !addSpecialTokens {
return tokenizer.DefaultProcess(newEncoding, newPairEncoding, addSpecialTokens)
}
// add special token to itself
finalEncoding := rp.addSpecialToken(newEncoding)
// add special token to its overflowing
var overflowing []tokenizer.Encoding
for _, en := range newEncoding.Overflowing {
newEn := rp.addSpecialToken(&en)
overflowing = append(overflowing, *newEn)
}
finalEncoding.Overflowing = overflowing
if pairEncoding != nil {
// add special tokens for pair itself
finalPairEncoding := rp.pairAddSpecialToken(newPairEncoding)
// add special tokens for pair's overflowing
var pairOverflowing []tokenizer.Encoding
for _, en := range newPairEncoding.Overflowing {
newEn := rp.pairAddSpecialToken(&en)
pairOverflowing = append(pairOverflowing, *newEn)
}
finalPairEncoding.Overflowing = pairOverflowing
// Merge with pair
finalEncoding.MergeWith(finalPairEncoding, false)
}
return finalEncoding
}
// addSpecialToken adds special tokens to input encoding. It ignores the `Overflowing` field
// of input encoding.
//
// Specifically, it adds: <s> Sequence </s>
func (rp *RobertaProcessing) addSpecialToken(encoding *tokenizer.Encoding) *tokenizer.Encoding {
var ids []int
ids = append(ids, rp.cls.Id)
ids = append(ids, encoding.Ids...)
ids = append(ids, rp.sep.Id)
var typeIds []int
typeIds = append(typeIds, 0)
typeIds = append(typeIds, encoding.TypeIds...)
typeIds = append(typeIds, 0)
var tokens []string
tokens = append(tokens, rp.cls.Value)
tokens = append(tokens, encoding.Tokens...)
tokens = append(tokens, rp.sep.Value)
var words []int
words = append(words, -1)
words = append(words, encoding.Words...)
words = append(words, -1)
var offsets [][]int
offsets = append(offsets, []int{0, 0})
offsets = append(offsets, encoding.Offsets...)
offsets = append(offsets, []int{0, 0})
var specialTokens []int
specialTokens = append(specialTokens, 1)
for i := 0; i < len(encoding.SpecialTokenMask); i++ {
specialTokens = append(specialTokens, 0)
}
specialTokens = append(specialTokens, 1)
var attentionMask []int
attentionMask = append(attentionMask, 1)
for i := 0; i < len(encoding.AttentionMask); i++ {
attentionMask = append(attentionMask, 1)
}
attentionMask = append(attentionMask, 1)
wordsOpt := tokenizer.WithWordsEncodingOpt(words)
return tokenizer.NewEncoding(ids, typeIds, tokens, offsets, specialTokens, attentionMask, []tokenizer.Encoding{}, wordsOpt)
}
// addSpecialToken adds special tokens to input pair encoding. It ignores the `Overflowing` field
// of input pair encoding.
//
// Specifically, it adds </s> PairEncoding </s>
func (rp *RobertaProcessing) pairAddSpecialToken(pair *tokenizer.Encoding) *tokenizer.Encoding {
var pairIds []int
pairIds = append(pairIds, rp.sep.Id)
pairIds = append(pairIds, pair.Ids...)
pairIds = append(pairIds, rp.sep.Id)
var pairTypeIds []int
pairTypeIds = append(pairTypeIds, 1)
pairTypeIds = append(pairTypeIds, pair.TypeIds...)
pairTypeIds = append(pairTypeIds, 1)
var pairTokens []string
pairTokens = append(pairTokens, rp.sep.Value)
pairTokens = append(pairTokens, pair.Tokens...)
pairTokens = append(pairTokens, rp.sep.Value)
var pairWords []int
pairWords = append(pairWords, -1)
pairWords = append(pairWords, pair.Words...)
pairWords = append(pairWords, -1)
var pairOffsets [][]int
pairOffsets = append(pairOffsets, []int{0, 0})
pairOffsets = append(pairOffsets, pair.Offsets...)
pairOffsets = append(pairOffsets, []int{0, 0})
var pairSpecialTokens []int
pairSpecialTokens = append(pairSpecialTokens, 1)
for i := 0; i < len(pair.SpecialTokenMask); i++ {
pairSpecialTokens = append(pairSpecialTokens, 0)
}
pairSpecialTokens = append(pairSpecialTokens, 1)
var pairAttentionMask []int
pairAttentionMask = append(pairAttentionMask, 1)
for i := 0; i < len(pair.AttentionMask); i++ {
pairAttentionMask = append(pairAttentionMask, 1)
}
pairAttentionMask = append(pairAttentionMask, 1)
pairWordsOpt := tokenizer.WithWordsEncodingOpt(pairWords)
return tokenizer.NewEncoding(pairIds, pairTypeIds, pairTokens, pairOffsets, pairSpecialTokens, pairAttentionMask, []tokenizer.Encoding{}, pairWordsOpt)
}
// TODO: implement Serialize interface for RobertaProcessing