-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathBert.swift
More file actions
340 lines (312 loc) · 13.6 KB
/
Copy pathBert.swift
File metadata and controls
340 lines (312 loc) · 13.6 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
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
// Copyright © 2024 Apple Inc.
import MLX
import MLXLMCommon
import MLXNN
extension MLXArray {
public static func arange(_ size: Int) -> MLXArray {
return MLXArray(Array(0 ..< size))
}
}
private class BertEmbedding: Module {
let typeVocabularySize: Int
@ModuleInfo(key: "word_embeddings") var wordEmbeddings: Embedding
@ModuleInfo(key: "norm") var norm: LayerNorm
@ModuleInfo(key: "token_type_embeddings") var tokenTypeEmbeddings: Embedding?
@ModuleInfo(key: "position_embeddings") var positionEmbeddings: Embedding
init(_ config: BertConfiguration) {
typeVocabularySize = config.typeVocabularySize
_wordEmbeddings.wrappedValue = Embedding(
embeddingCount: config.vocabularySize, dimensions: config.embedDim)
_norm.wrappedValue = LayerNorm(
dimensions: config.embedDim, eps: config.layerNormEps)
if config.typeVocabularySize > 0 {
_tokenTypeEmbeddings.wrappedValue = Embedding(
embeddingCount: config.typeVocabularySize,
dimensions: config.embedDim)
}
_positionEmbeddings.wrappedValue = Embedding(
embeddingCount: config.maxPositionEmbeddings,
dimensions: config.embedDim)
}
func callAsFunction(
_ inputIds: MLXArray,
positionIds: MLXArray? = nil,
tokenTypeIds: MLXArray? = nil
) -> MLXArray {
let posIds = positionIds ?? broadcast(MLXArray.arange(inputIds.dim(1)), to: inputIds.shape)
var words = wordEmbeddings(inputIds) + positionEmbeddings(posIds)
if let tokenTypeIds, let tokenTypeEmbeddings {
words += tokenTypeEmbeddings(tokenTypeIds)
}
return norm(words)
}
}
private class TransformerBlock: Module {
let attention: MultiHeadAttention
@ModuleInfo(key: "ln1") var preLayerNorm: LayerNorm
@ModuleInfo(key: "ln2") var postLayerNorm: LayerNorm
@ModuleInfo(key: "linear1") var up: Linear
@ModuleInfo(key: "linear2") var down: Linear
init(_ config: BertConfiguration) {
attention = MultiHeadAttention(
dimensions: config.embedDim, numHeads: config.numHeads, bias: true)
_preLayerNorm.wrappedValue = LayerNorm(
dimensions: config.embedDim, eps: config.layerNormEps)
_postLayerNorm.wrappedValue = LayerNorm(
dimensions: config.embedDim, eps: config.layerNormEps)
_up.wrappedValue = Linear(config.embedDim, config.interDim)
_down.wrappedValue = Linear(config.interDim, config.embedDim)
}
func callAsFunction(_ inputs: MLXArray, mask: MLXArray? = nil) -> MLXArray {
let attentionOut = attention(inputs, keys: inputs, values: inputs, mask: mask)
let preNorm = preLayerNorm(inputs + attentionOut)
let mlpOut = down(gelu(up(preNorm)))
return postLayerNorm(mlpOut + preNorm)
}
}
private class Encoder: Module {
let layers: [TransformerBlock]
init(_ config: BertConfiguration) {
precondition(config.vocabularySize > 0)
layers = (0 ..< config.numLayers).map { _ in TransformerBlock(config) }
}
func callAsFunction(_ inputs: MLXArray, attentionMask: MLXArray? = nil) -> MLXArray {
var outputs = inputs
for layer in layers {
outputs = layer(outputs, mask: attentionMask)
}
return outputs
}
}
private class LMHead: Module {
@ModuleInfo(key: "dense") var dense: Linear
@ModuleInfo(key: "ln") var layerNorm: LayerNorm
@ModuleInfo(key: "decoder") var decoder: Linear
init(_ config: BertConfiguration) {
_dense.wrappedValue = Linear(
config.embedDim, config.embedDim, bias: true)
_layerNorm.wrappedValue = LayerNorm(
dimensions: config.embedDim, eps: config.layerNormEps)
_decoder.wrappedValue = Linear(
config.embedDim, config.vocabularySize, bias: true)
}
func callAsFunction(_ inputs: MLXArray) -> MLXArray {
return decoder(layerNorm(silu(dense(inputs))))
}
}
public class BertModel: Module, EmbeddingModel {
@ModuleInfo(key: "lm_head") fileprivate var lmHead: LMHead?
@ModuleInfo(key: "embeddings") fileprivate var embedder: BertEmbedding
let pooler: Linear?
fileprivate let encoder: Encoder
public var vocabularySize: Int
public init(
_ config: BertConfiguration, lmHead: Bool = false
) {
precondition(config.vocabularySize > 0)
vocabularySize = config.vocabularySize
encoder = Encoder(config)
_embedder.wrappedValue = BertEmbedding(config)
if lmHead {
_lmHead.wrappedValue = LMHead(config)
self.pooler = nil
} else {
pooler = Linear(config.embedDim, config.embedDim)
_lmHead.wrappedValue = nil
}
}
public func callAsFunction(
_ inputs: MLXArray, positionIds: MLXArray? = nil, tokenTypeIds: MLXArray? = nil,
attentionMask: MLXArray? = nil
)
-> EmbeddingModelOutput
{
var inp = inputs
if inp.ndim == 1 {
inp = inp.reshaped(1, -1)
}
var mask = attentionMask
if mask != nil {
mask = mask!.asType(embedder.wordEmbeddings.weight.dtype).expandedDimensions(axes: [
1, 2,
]).log()
}
let outputs = encoder(
embedder(inp, positionIds: positionIds, tokenTypeIds: tokenTypeIds),
attentionMask: mask)
if let lmHead {
return EmbeddingModelOutput(hiddenStates: lmHead(outputs), pooledOutput: nil)
} else {
return EmbeddingModelOutput(
hiddenStates: outputs, pooledOutput: tanh(pooler!(outputs[0..., 0])))
}
}
public func sanitize(weights: [String: MLXArray]) -> [String: MLXArray] {
weights.reduce(into: [:]) { result, item in
var key = item.key.replacingOccurrences(of: ".layer.", with: ".layers.")
key = key.replacingOccurrences(of: ".self.key.", with: ".key_proj.")
key = key.replacingOccurrences(of: ".self.query.", with: ".query_proj.")
key = key.replacingOccurrences(of: ".self.value.", with: ".value_proj.")
key = key.replacingOccurrences(
of: ".attention.output.dense.", with: ".attention.out_proj.")
key = key.replacingOccurrences(of: ".attention.output.LayerNorm.", with: ".ln1.")
key = key.replacingOccurrences(of: ".output.LayerNorm.", with: ".ln2.")
key = key.replacingOccurrences(of: ".intermediate.dense.", with: ".linear1.")
key = key.replacingOccurrences(of: ".output.dense.", with: ".linear2.")
key = key.replacingOccurrences(of: ".LayerNorm.", with: ".norm.")
key = key.replacingOccurrences(of: "pooler.dense.", with: "pooler.")
key = key.replacingOccurrences(
of:
"cls.predictions.transform.dense.",
with: "lm_head.dense.")
key = key.replacingOccurrences(
of:
"cls.predictions.transform.LayerNorm.",
with: "lm_head.ln.")
key = key.replacingOccurrences(
of:
"cls.predictions.decoder",
with: "lm_head.decoder")
key = key.replacingOccurrences(
of: "cls.predictions.transform.norm.weight",
with: "lm_head.ln.weight")
key = key.replacingOccurrences(
of: "cls.predictions.transform.norm.bias",
with: "lm_head.ln.bias")
key = key.replacingOccurrences(of: "cls.predictions.bias", with: "lm_head.decoder.bias")
key = key.replacingOccurrences(of: "bert.", with: "")
result[key] = item.value
}.filter { key, _ in key != "embeddings.position_ids" }
}
public func sanitize(
weights: [String: MLXArray], quantizationConfig: MLXLMCommon.BaseConfiguration.Quantization?
) -> [String: MLXArray] {
fatalError("Bert does not support quantization")
}
}
public class DistilBertModel: BertModel {
public override func sanitize(weights: [String: MLXArray]) -> [String: MLXArray] {
weights.reduce(into: [:]) { result, item in
var key = item.key.replacingOccurrences(of: ".layer.", with: ".layers.")
key = key.replacingOccurrences(of: "transformer.", with: "encoder.")
key = key.replacingOccurrences(of: "embeddings.LayerNorm", with: "embeddings.norm")
key = key.replacingOccurrences(of: ".attention.q_lin.", with: ".attention.query_proj.")
key = key.replacingOccurrences(of: ".attention.k_lin.", with: ".attention.key_proj.")
key = key.replacingOccurrences(of: ".attention.v_lin.", with: ".attention.value_proj.")
key = key.replacingOccurrences(of: ".attention.out_lin.", with: ".attention.out_proj.")
key = key.replacingOccurrences(of: ".sa_layer_norm.", with: ".ln1.")
key = key.replacingOccurrences(of: ".ffn.lin1.", with: ".linear1.")
key = key.replacingOccurrences(of: ".ffn.lin2.", with: ".linear2.")
key = key.replacingOccurrences(of: ".output_layer_norm.", with: ".ln2.")
key = key.replacingOccurrences(of: "vocab_transform", with: "lm_head.dense")
key = key.replacingOccurrences(of: "vocab_layer_norm", with: "lm_head.ln")
key = key.replacingOccurrences(of: "vocab_projector", with: "lm_head.decoder")
key = key.replacingOccurrences(of: "distilbert.", with: "")
result[key] = item.value
}.filter { key, _ in key != "embeddings.position_ids" }
}
}
public struct BertConfiguration: Decodable, Sendable {
var layerNormEps: Float = 1e-12
var maxTrainedPositions: Int = 2048
var embedDim: Int = 768
var numHeads: Int = 12
var interDim: Int = 3072
var numLayers: Int = 12
var typeVocabularySize: Int = 2
var vocabularySize: Int = 30528
var maxPositionEmbeddings: Int = 0
var modelType: String
enum CodingKeys: String, CodingKey {
case layerNormEps = "layer_norm_eps"
case maxTrainedPositions = "max_trained_positions"
case vocabularySize = "vocab_size"
case maxPositionEmbeddings = "max_position_embeddings"
case modelType = "model_type"
}
enum BertCodingKeys: String, CodingKey {
case embedDim = "hidden_size"
case numHeads = "num_attention_heads"
case interDim = "intermediate_size"
case numLayers = "num_hidden_layers"
case typeVocabularySize = "type_vocab_size"
}
enum DistilBertCodingKeys: String, CodingKey {
case embedDim = "dim"
case numLayers = "n_layers"
case numHeads = "n_heads"
case interDim = "hidden_dim"
}
public init(from decoder: Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> =
try decoder.container(
keyedBy: CodingKeys.self)
layerNormEps =
try container.decodeIfPresent(
Float.self,
forKey: CodingKeys.layerNormEps.self)
?? 1e-12
maxTrainedPositions =
try container.decodeIfPresent(
Int.self,
forKey: CodingKeys.maxTrainedPositions
.self) ?? 2048
vocabularySize =
try container.decodeIfPresent(
Int.self,
forKey: CodingKeys.vocabularySize.self)
?? 30528
maxPositionEmbeddings =
try container.decodeIfPresent(
Int.self,
forKey: CodingKeys.maxPositionEmbeddings
.self) ?? 0
modelType = try container.decode(String.self, forKey: CodingKeys.modelType.self)
if modelType == "distilbert" {
let distilBertConfig: KeyedDecodingContainer<DistilBertCodingKeys> =
try decoder.container(
keyedBy: DistilBertCodingKeys.self)
embedDim =
try distilBertConfig.decodeIfPresent(
Int.self,
forKey: DistilBertCodingKeys.embedDim.self) ?? 768
numHeads =
try distilBertConfig.decodeIfPresent(
Int.self,
forKey: DistilBertCodingKeys.numHeads.self) ?? 12
interDim =
try distilBertConfig.decodeIfPresent(
Int.self, forKey: DistilBertCodingKeys.interDim.self)
?? 3072
numLayers =
try distilBertConfig.decodeIfPresent(
Int.self,
forKey: DistilBertCodingKeys.numLayers.self) ?? 12
typeVocabularySize = 0
} else {
let bertConfig: KeyedDecodingContainer<BertCodingKeys> = try decoder.container(
keyedBy: BertCodingKeys.self)
embedDim =
try bertConfig.decodeIfPresent(
Int.self,
forKey: BertCodingKeys.embedDim.self) ?? 768
numHeads =
try bertConfig.decodeIfPresent(
Int.self,
forKey: BertCodingKeys.numHeads.self) ?? 12
interDim =
try bertConfig.decodeIfPresent(
Int.self, forKey: BertCodingKeys.interDim.self)
?? 3072
numLayers =
try bertConfig.decodeIfPresent(
Int.self,
forKey: BertCodingKeys.numLayers.self) ?? 12
typeVocabularySize =
try bertConfig.decodeIfPresent(
Int.self,
forKey: BertCodingKeys.typeVocabularySize
.self) ?? 2
}
}
}