-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathadded-vocabulary.go
539 lines (454 loc) · 15.9 KB
/
added-vocabulary.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package tokenizer
import (
"fmt"
"log"
"regexp"
"sort"
"unicode"
"github.com/sugarme/regexpset"
"github.com/sugarme/tokenizer/normalizer"
)
// AddedToken represents a token added by the user on top of the
// existing model vocabulary.
//
// AddedToken can be configured to specify the behaviour they should
// have in various situations. I.e.,:
// - Whether they should only match single words
// - Whether to include any whitespace on its left or right
type AddedToken struct {
// Content is the content of added token
Content string
// whether this token is single word or break words
SingleWord bool
// Whether this token should strip whitespace on its left
LStrip bool
// Whether this token should strip whitespace on its right
RStrip bool
// Whether this token should be normalized
Normalized bool
}
// DefaultAddedToken initiates a default AddedToken
func DefaultAddedToken() (retVal AddedToken) {
return AddedToken{
Content: "",
SingleWord: false,
LStrip: false,
RStrip: false,
Normalized: true,
}
}
type ATOption func(at *AddedToken)
// WithSingleWord specifies whether this token should only match on whole
// single words, and never part of a word.
func WithSingleWord(singleWord bool) ATOption {
return func(at *AddedToken) {
at.SingleWord = singleWord
}
}
// WithLStrip specify whether this token should include all the whitespaces
// on its left in order to strip them out.
func WithLStrip(lstrip bool) ATOption {
return func(at *AddedToken) {
at.LStrip = lstrip
}
}
// WithRStrip specify whether this token should include all the whitespaces
// on its right in order to strip them out.
func WithRStrip(rstrip bool) ATOption {
return func(at *AddedToken) {
at.RStrip = rstrip
}
}
// WithNormalized specifies whether this token should be normalized and match against its normalized
// version in the input text.
func WithNormalized(normalized bool) ATOption {
return func(at *AddedToken) {
at.Normalized = normalized
}
}
// NewAddedToken builds an AddedToken from given content
// specifying whether it is intended to be a special token.
// NOTE. Special token ar not normalized by default.
func NewAddedToken(s string, special bool, opts ...ATOption) (retVal AddedToken) {
addedTok := DefaultAddedToken()
addedTok.Content = s
addedTok.Normalized = !special
for _, opt := range opts {
opt(&addedTok)
}
return addedTok
}
// Specify whether this token should only match on whole single words, and never
// part of a word.
func (at AddedToken) SetSingleWord(singleWord bool) (retVal AddedToken) {
at.SingleWord = singleWord
return at
}
// Specify whether this token should include all the whitespaces on its left, in
// order to strip them out.
func (at AddedToken) SetLStrip(lstrip bool) (retVal AddedToken) {
at.LStrip = lstrip
return at
}
// Specify whether this token should include all the whitespaces on its right, in
// order to strip them out.
func (at AddedToken) SetRStrip(rstrip bool) (retVal AddedToken) {
at.RStrip = rstrip
return at
}
// Specify whether this token should be normalized and match against its normalized
// version in the input text.
func (at AddedToken) SetNormalized(normalized bool) (retVal AddedToken) {
at.Normalized = normalized
return at
}
// GetPattern retrieves the pattern built for this token, according to all the specified parameters.
//
// NOTE. normalizer input is optional
func (at AddedToken) GetPattern(n normalizer.Normalizer) (retVal string) {
var reStr string // regular expression pattern
if at.SingleWord {
var firstB, lastB string
runes := []rune(at.Content)
firstChar := runes[0]
lastChar := runes[len(runes)-1]
if isWordCharacter(firstChar) {
firstB = `\b`
} else {
firstB = ``
}
if isWordCharacter(lastChar) {
lastB = `\b`
} else {
lastB = ``
}
// normalize the content
content := normalizer.NewNormalizedFrom(at.Content)
var normalized string
if n != nil {
normalizedString, err := n.Normalize(content)
if err != nil {
log.Fatal(err)
}
normalized = normalizedString.GetNormalized()
} else { // don't have a normalizer, just use content as is
normalized = at.Content
}
reStr = fmt.Sprintf("%v%v%v", firstB, regexp.QuoteMeta(normalized), lastB)
} else {
reStr = regexp.QuoteMeta(at.Content)
}
if at.LStrip && at.RStrip {
reStr = fmt.Sprintf("(\\s)?%v(\\s)?", reStr)
} else if at.LStrip {
reStr = fmt.Sprintf("(\\s)?%v", reStr)
} else if at.RStrip {
reStr = fmt.Sprintf("%v(\\s)?", reStr)
}
return reStr
}
func isWordCharacter(r rune) bool {
if unicode.IsLetter(r) || unicode.IsMark(r) || unicode.IsDigit(r) || unicode.IsControl(r) || unicode.IsPunct(r) {
return true
}
return false
}
// matchingSet is a set of regular expression string
type matchingSet struct {
regexSet regexpset.RegexpSet
ids []int
}
// AddedVocabulary is a vocabulary built on top of the Model
//
// This provides a way to add new vocabulary to a Tokenizer that has already been trained,
// in a previous process, maybe by someone else. This is especially interesting in the case
// of fine-tunings, where we want to finetune a model while adding some new functionalities
// using some new special tokens, or maybe add some tokens in the case of unknown tokens, etc.
//
// One of the reasons we need to handle these tokens outside of the model is simply that
// for many models, it is not possible to add new tokens after the training process. For example,
// using BPE, the training process generates merges pairs along the vocabulary, and any token
// in the vocabulary can be decomposed in other tokens, down to the original alphabet. If we
// were to add new tokens after this training process, we couldn't make sure the merges pairs
// exist as required.
type AddedVocabulary struct {
// Contains the mapping from String (token content) to ID. This map contains both special
// tokens and classic added tokens that were added to the this vocabulary.
addedTokenMap map[string]int
// Contains the mapping from ID to AddedToken for all the added tokens, both special
// and classic.
addedTokenMapR map[int]string
// Contains only the classic AddedToken, in the specific order the user gave them.
addedTokens []AddedToken
// Contains only the special AddedToken, in the specific order the user gave them.
specialTokens []AddedToken
// A map, containing all the special token for easy access while decoding. This let's
// us remove them easily with an O(1) complexity.
specialTokensSet map[string]bool
// A struct containing all the non-normalized patterns used to split on AddedTokens
splitRe matchingSet
// A struct containing all the normalized patterns used to split on AddedTokens
splitNormalizedRe matchingSet
}
func NewAddedVocabulary() (retVal AddedVocabulary) {
return AddedVocabulary{
addedTokenMap: make(map[string]int, 0),
addedTokenMapR: make(map[int]string, 0),
addedTokens: []AddedToken{},
specialTokens: []AddedToken{},
specialTokensSet: make(map[string]bool, 0),
splitRe: matchingSet{},
splitNormalizedRe: matchingSet{},
}
}
// Len returns size of the additional vocabulary
func (av *AddedVocabulary) Len() int {
return len(av.addedTokenMap)
}
// GetVocab gets the additional vocabulary
func (av *AddedVocabulary) GetVocab() (retVal map[string]int) {
return av.addedTokenMap
}
// Get the id matching one of our token if it exists
func (av *AddedVocabulary) TokenToId(token string, model Model) (retVal int, ok bool) {
retVal, ok = av.addedTokenMap[token]
if !ok {
return model.TokenToId(token)
}
return retVal, ok
}
// Get the token matching the given id if it exists
func (av *AddedVocabulary) IdToToken(id int, model Model) (retVal string, ok bool) {
retVal, ok = av.addedTokenMapR[id]
if !ok {
return model.IdToToken(id)
}
return retVal, ok
}
// Check if a token is a special token
func (av *AddedVocabulary) IsSpecialToken(token string) bool {
_, ok := av.specialTokensSet[token]
return ok
}
// Add some special tokens to the vocabulary
// It returns number of added tokens
func (av *AddedVocabulary) AddSpecialTokens(tokens []AddedToken, model Model, normalizer normalizer.Normalizer) (retVal int) {
for _, tok := range tokens {
_, isExist := av.specialTokensSet[tok.Content]
if tok.Content != "" && !isExist {
av.specialTokens = append(av.specialTokens, tok)
av.specialTokensSet[tok.Content] = true
}
}
// Then we delegate to `add_tokens`, that will take care of refreshing added tokens too.
return av.AddTokens(tokens, model, normalizer)
}
// Add some tokens to the vocabulary
// It returns number of added tokens
func (av *AddedVocabulary) AddTokens(tokens []AddedToken, model Model, normalizer normalizer.Normalizer) (retVal int) {
ignored := 0
for _, token := range tokens {
if token.Content == "" {
ignored++
continue
}
var id int
if i, ok := av.TokenToId(token.Content, model); ok {
ignored++
id = i
} else {
id = model.GetVocabSize() + len(av.addedTokenMap)
av.addedTokenMap[token.Content] = id
if _, ok := av.specialTokensSet[token.Content]; !ok {
av.addedTokens = append(av.addedTokens, token)
}
}
// Update the current revert operation
av.addedTokenMapR[id] = token.Content
}
av.refreshAddedTokens(model, normalizer)
// return the number of added tokens
return len(tokens) - ignored
}
type tokenId struct {
token AddedToken
id int
}
// refreshAddedTokens reconstructs our internal RegexSet when new tokens are added to the vocabulary.
//
// NOTE. We keep two different regular expression sets, one that will take care of matching against the
// non-normalized string, and one matching against the normalized one.
func (av *AddedVocabulary) refreshAddedTokens(model Model, normalizer normalizer.Normalizer) {
var normIds, nnormIds []int
var normPatterns, nnormPatterns []string
tokens := append(av.specialTokens, av.addedTokens...)
for _, token := range tokens {
id, ok := av.TokenToId(token.Content, model)
if !ok {
log.Fatalf("Missing additional token.\n")
}
pattern := token.GetPattern(normalizer)
if token.Normalized {
normIds = append(normIds, id)
normPatterns = append(normPatterns, pattern)
} else {
nnormIds = append(nnormIds, id)
nnormPatterns = append(nnormPatterns, pattern)
}
}
normSet, err := regexpset.NewRegexpSet(normPatterns)
if err != nil {
log.Fatal(err)
}
nnormSet, err := regexpset.NewRegexpSet(nnormPatterns)
if err != nil {
log.Fatal(err)
}
av.splitNormalizedRe = matchingSet{*normSet, normIds}
av.splitRe = matchingSet{*nnormSet, nnormIds}
}
type idOffsets struct {
id int // optional - None value = -1
offsets []int
}
// helper functions to sort idOffsets
// By implement sort interface of package sort
// byStart sort by offset.Start
type byStart []idOffsets
func (s byStart) Len() int { return len(s) }
func (s byStart) Less(i, j int) bool { return s[i].offsets[0] < s[j].offsets[0] }
func (s byStart) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// byId sort by id
type byId []idOffsets
func (bi byId) Len() int { return len(bi) }
func (bi byId) Less(i, j int) bool { return bi[i].id < bi[j].id }
func (bi byId) Swap(i, j int) { bi[i], bi[j] = bi[j], bi[i] }
// findMatches finds any AddedToken in the given sentence, using the provided MatchingSet.
// This method returns a list "splits", each of them being a pair of Offsets
// and an optional ID if it is an AddedToken. The list of splits cover the entire input string.
func (av *AddedVocabulary) findMatches(sentence string, splitRe matchingSet) (retVal []idOffsets) {
if len(sentence) == 0 {
return []idOffsets{{-1, []int{0, 0}}}
}
matches := splitRe.regexSet.Matches(sentence).Matches()
var ioPairs []idOffsets
for _, idx := range matches {
r := regexp.MustCompile(splitRe.regexSet.Patterns()[idx])
locs := r.FindAllStringIndex(sentence, -1)
for _, loc := range locs {
id := idx
ioPair := idOffsets{id: id, offsets: []int{loc[0], loc[1]}}
ioPairs = append(ioPairs, ioPair)
}
}
// Sort id-offsets by start then by pattern id
sort.Sort(byStart(ioPairs))
sort.Sort(byId(ioPairs))
// Select the matches, if they overlap, keep them
var (
i int = 0
currentOffsets int = 0
splits []idOffsets = make([]idOffsets, 0)
)
for i < len(ioPairs) {
ioPair := ioPairs[i]
// current match is before the current offset, skip it
if ioPair.offsets[0] < currentOffsets {
i++
continue
}
// Find out whether having overlapping neighbours.
// If so, keep the one with lowest Idx. All other will be skipped
// because `currentOffsets` will have been increased.
if i+1 < len(ioPairs) {
overlapPairs := ioPairs[i:]
sort.Sort(byId(overlapPairs))
lowestPair := overlapPairs[0] // lowest Id one
splits = append(splits, lowestPair)
currentOffsets = ioPair.offsets[1]
i++
continue
}
// Not found overlap neighbours. Just apply itself
splits = append(splits, ioPair)
currentOffsets = ioPair.offsets[1]
i++
}
// Also, insert the splits in-between added tokens, to split the entire string
var (
startOffset int = 0
finalSplits []idOffsets
)
for _, ioPair := range splits {
if startOffset < ioPair.offsets[0] {
finalSplits = append(finalSplits, idOffsets{-1, []int{startOffset, ioPair.offsets[0]}})
}
finalSplits = append(finalSplits, idOffsets{splitRe.ids[ioPair.id], ioPair.offsets})
startOffset = ioPair.offsets[1]
}
totalByteLen := len(sentence)
if startOffset != totalByteLen {
finalSplits = append(finalSplits, idOffsets{-1, []int{startOffset, totalByteLen}})
}
return finalSplits
}
type SplitIdx struct {
Normalized *normalizer.NormalizedString
Tokens []Token
}
// splitWithIndices splits the input sentence to extract anything found from the `MatchingSet`, as well as
// the list of corresponding IDs.
//
// NOTE.The list of IDs have the exact same number of elements as the Iterator.
func (av *AddedVocabulary) splitWithIndices(sentence *normalizer.NormalizedString, splitRe matchingSet) []SplitIdx {
ioPairs := av.findMatches(sentence.GetNormalized(), splitRe)
var splits []SplitIdx
for _, p := range ioPairs {
slice := sentence.Slice(normalizer.NewRange(p.offsets[0], p.offsets[1], normalizer.NormalizedTarget))
if p.id == -1 {
splits = append(splits, SplitIdx{slice, nil})
} else {
value := slice.GetNormalized()
length := len(value)
split := SplitIdx{slice, []Token{NewToken(p.id, value, []int{0, length})}}
splits = append(splits, split)
}
}
return splits
}
// ExtractAndNormalize extracts the additional vocabulary from the given sentence, normalizing it along the way.
//
// Some tokens should match against their normalized representation, as well as the
// non-normalized one. For example, when we expect to extract the token `yesterday` in the
// input sentence `I read a book Yesterday`, if the normalizer is supposed to lowercase
// everything, we expect a match.
func (av *AddedVocabulary) ExtractAndNormalize(sequence string, n normalizer.Normalizer) *PreTokenizedString {
pretokenized := NewPreTokenizedString(sequence)
// 1. Extract all non-normalized tokens from the non-normalized string
pretok1 := pretokenized.Split(func(idx int, seq *normalizer.NormalizedString) []SplitIdx {
return av.splitWithIndices(seq, av.splitRe)
})
// 2. Extract the normalized tokens from the normalized pieces of the string
pretok2 := pretok1.Split(func(i int, seq *normalizer.NormalizedString) []SplitIdx {
newSeq := seq
var err error
if n != nil {
newSeq, err = n.Normalize(seq)
if err != nil {
log.Fatal(err)
}
}
return av.splitWithIndices(newSeq, av.splitNormalizedRe)
})
return pretok2
}
type AddedTokenWithId struct {
Id int // Id assigned to this token
Special bool // whether this is a special token
Token AddedToken // the target AddedToken
}
// Implement Serialize interface for AddedVocabular:
// =================================================
// Serialize implements Serialize interface for AddedVocabular
// TODO. implement it
// func(av AddedVocabulary) Serialize(s Serializer)(retVal ...)