-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathPostProcessor.swift
193 lines (162 loc) · 7.36 KB
/
PostProcessor.swift
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
//
// PostProcessor.swift
//
//
// Created by Pedro Cuenca on 17/7/23.
//
import Foundation
import Hub
public protocol PostProcessor {
func postProcess(tokens: [String], tokensPair: [String]?, addSpecialTokens: Bool) -> [String]
func callAsFunction(tokens: [String], tokensPair: [String]?, addSpecialTokens: Bool) -> [String]
init(config: Config)
}
extension PostProcessor {
func callAsFunction(tokens: [String], tokensPair: [String]? = nil, addSpecialTokens: Bool = true) -> [String] {
return postProcess(tokens: tokens, tokensPair: tokensPair, addSpecialTokens: addSpecialTokens)
}
}
enum PostProcessorType: String {
case TemplateProcessing
case ByteLevel
case RobertaProcessing
case BertProcessing
case Sequence
}
struct PostProcessorFactory {
static func fromConfig(config: Config?) -> PostProcessor? {
guard let config = config else { return nil }
guard let typeName = config.type?.stringValue else { return nil }
let type = PostProcessorType(rawValue: typeName)
switch type {
case .TemplateProcessing : return TemplateProcessing(config: config)
case .ByteLevel : return ByteLevelPostProcessor(config: config)
case .RobertaProcessing : return RobertaProcessing(config: config)
case .BertProcessing : return BertProcessing(config: config)
case .Sequence : return SequenceProcessing(config: config)
default : fatalError("Unsupported PostProcessor type: \(typeName)")
}
}
}
class TemplateProcessing: PostProcessor {
let single: [Config]
let pair: [Config]
required public init(config: Config) {
guard let single = config.single?.arrayValue else { fatalError("Missing `single` processor configuration") }
guard let pair = config.pair?.arrayValue else { fatalError("Missing `pair` processor configuration") }
self.single = single
self.pair = pair
}
func postProcess(tokens: [String], tokensPair: [String]? = nil, addSpecialTokens: Bool = true) -> [String] {
let config = tokensPair == nil ? single : pair
var toReturn: [String] = []
for item in config {
if let specialToken = item.SpecialToken {
if addSpecialTokens {
toReturn.append(specialToken.id!.stringValue!)
}
} else if let sequence = item.Sequence {
if sequence.id?.stringValue == "A" {
toReturn += tokens
} else if sequence.id?.stringValue == "B" {
toReturn += tokensPair!
}
}
}
return toReturn
}
}
class ByteLevelPostProcessor: PostProcessor {
required public init(config: Config) {}
func postProcess(tokens: [String], tokensPair: [String]? = nil, addSpecialTokens: Bool = true) -> [String] { tokens }
}
class RobertaProcessing: PostProcessor {
private let sep: (UInt, String)
private let cls: (UInt, String)
/// Trim all remaining space, or leave one space character if `addPrefixSpace` is `true`.
private let trimOffset: Bool
/// Keep one space character on each side. Depends on `trimOffsets` being `true`.
private let addPrefixSpace: Bool
required public init(config: Config) {
guard let sep = config.sep?.tokenValue else { fatalError("Missing `sep` processor configuration") }
guard let cls = config.cls?.tokenValue else { fatalError("Missing `cls` processor configuration") }
self.sep = sep
self.cls = cls
self.trimOffset = config.trimOffset?.boolValue ?? true
self.addPrefixSpace = config.addPrefixSpace?.boolValue ?? true
}
func postProcess(tokens: [String], tokensPair: [String]?, addSpecialTokens: Bool = true) -> [String] {
var outTokens = tokens
var tokensPair = tokensPair
if trimOffset {
if addPrefixSpace {
outTokens = outTokens.map({ trimExtraSpaces(token: $0) })
tokensPair = tokensPair?.map({ trimExtraSpaces(token: $0) })
} else {
outTokens = outTokens.map({ $0.trimmingCharacters(in: .whitespaces) })
tokensPair = tokensPair?.map({ $0.trimmingCharacters(in: .whitespaces) })
}
}
outTokens = [self.cls.1] + outTokens + [self.sep.1]
if let tokensPair = tokensPair, !tokensPair.isEmpty {
// Yes, it adds another `sep`.
// https://github.com/facebookresearch/fairseq/blob/main/fairseq/models/roberta/hub_interface.py#L58-L65
outTokens += [self.sep.1] + tokensPair + [self.sep.1]
}
return outTokens
}
/// Some tokens need one space around them
/// https://github.com/huggingface/tokenizers/blob/main/tokenizers/src/pre_tokenizers/byte_level.rs#L203-L235
private func trimExtraSpaces(token: String) -> String {
let prefixOffset = findPrefixIndex(text: token)
let suffixOffset = findSuffixIndex(text: token)
let prefixIndex = token.index(token.startIndex, offsetBy: prefixOffset)
let suffixIndex = token.index(token.startIndex, offsetBy: token.count - suffixOffset)
return String(token[prefixIndex..<suffixIndex])
}
private func findPrefixIndex(text: String) -> Int {
guard !text.isEmpty, text.first!.isWhitespace else { return 0 }
return text.prefix(while: { $0.isWhitespace }).count - 1
}
private func findSuffixIndex(text: String) -> Int {
guard !text.isEmpty, text.last!.isWhitespace else { return 0 }
return text.reversed().prefix(while: { $0.isWhitespace }).count - 1
}
}
class BertProcessing: PostProcessor {
private let sep: (UInt, String)
private let cls: (UInt, String)
required public init(config: Config) {
guard let sep = config.sep?.tokenValue else { fatalError("Missing `sep` processor configuration") }
guard let cls = config.cls?.tokenValue else { fatalError("Missing `cls` processor configuration") }
self.sep = sep
self.cls = cls
}
func postProcess(tokens: [String], tokensPair: [String]?, addSpecialTokens: Bool = true) -> [String] {
guard addSpecialTokens else { return tokens + (tokensPair ?? []) }
var outTokens = [self.cls.1] + tokens + [self.sep.1]
if let tokensPair = tokensPair, !tokensPair.isEmpty {
outTokens += tokensPair + [self.sep.1]
}
return outTokens
}
}
class SequenceProcessing: PostProcessor {
private let processors: [PostProcessor]
required public init(config: Config) {
guard let processorConfigs = config.processors?.arrayValue else {
fatalError("Missing `processors` configuration")
}
self.processors = processorConfigs.compactMap { PostProcessorFactory.fromConfig(config: $0) }
}
func postProcess(tokens: [String], tokensPair: [String]?, addSpecialTokens: Bool = true) -> [String] {
var currentTokens = tokens
var currentTokensPair = tokensPair
for processor in processors {
let processed = processor.postProcess(tokens: currentTokens, tokensPair: currentTokensPair, addSpecialTokens: addSpecialTokens)
currentTokens = processed
currentTokensPair = nil // After the first processor, we no longer have a separate pair
}
return currentTokens
}
}