-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathPythonCheckpointReader.swift
134 lines (122 loc) · 4.97 KB
/
PythonCheckpointReader.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
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Checkpoints
import ModelSupport
import TensorFlow
public struct TransformerLMConfig: Codable {
public let vocabSize: Int
public let contextSize: Int
public let embeddingSize: Int
public let headCount: Int
public let layerCount: Int
enum CodingKeys: String, CodingKey {
case vocabSize = "n_vocab"
case contextSize = "n_ctx"
case embeddingSize = "n_embd"
case headCount = "n_head"
case layerCount = "n_layer"
}
}
extension CheckpointReader {
func readTensor<Scalar: TensorFlowScalar>(
name: String
) -> Tensor<Scalar> {
return Tensor<Scalar>(loadTensor(named: name))
}
}
protocol InitializableFromPythonCheckpoint {
init(reader: CheckpointReader, config: TransformerLMConfig, scope: String)
}
extension Dense: InitializableFromPythonCheckpoint {
init(reader: CheckpointReader, config: TransformerLMConfig, scope: String) {
var kernel: Tensor<Scalar> = reader.readTensor(name: scope + "/w")
if kernel.shape.dimensions.count > 2 {
// The OpenAI checkpoints have a batch dimension, and our checkpoints do not.
kernel = kernel.squeezingShape(at: 0)
}
self.init(
weight: kernel,
bias: reader.readTensor(name: scope + "/b"),
activation: identity)
}
init(
reader: CheckpointReader,
config: TransformerLMConfig,
scope: String,
activation: String
) {
var kernel: Tensor<Scalar> = reader.readTensor(name: scope + "/w")
if kernel.shape.dimensions.count > 2 {
// The OpenAI checkpoints have a batch dimension, and our checkpoints do not.
kernel = kernel.squeezingShape(at: 0)
}
self.init(
weight: kernel,
bias: reader.readTensor(name: scope + "/b"),
activation: gelu)
}
}
extension LayerNorm: InitializableFromPythonCheckpoint {
init(reader: CheckpointReader, config: TransformerLMConfig, scope: String) {
self.init(
offset: reader.readTensor(name: scope + "/b"),
scale: reader.readTensor(name: scope + "/g"),
axis: -1,
epsilon: 1e-5)
}
}
extension MultiHeadAttentionGPT2: InitializableFromPythonCheckpoint {
init(reader: CheckpointReader, config: TransformerLMConfig, scope: String) {
attention = Attention(
size: config.embeddingSize / config.headCount,
causal: true,
dropProbability: 0.1)
wqkv = TimeDistributed(
Dense<Float>(reader: reader, config: config, scope: scope + "/c_attn"))
wo = TimeDistributed(
Dense<Float>(reader: reader, config: config, scope: scope + "/c_proj"))
headCount = config.headCount
}
}
extension FeedForward: InitializableFromPythonCheckpoint {
init(reader: CheckpointReader, config: TransformerLMConfig, scope: String) {
dense1 = TimeDistributed(
Dense<Float>(reader: reader, config: config, scope: scope + "/c_fc", activation: "gelu")
)
dense2 = TimeDistributed(
Dense<Float>(reader: reader, config: config, scope: scope + "/c_proj"))
}
}
extension EncoderLayer: InitializableFromPythonCheckpoint {
init(reader: CheckpointReader, config: TransformerLMConfig, scope: String) {
selfAttention = MultiHeadAttentionGPT2(
reader: reader, config: config, scope: scope + "/attn")
selfAttentionDropout = Dropout(probability: 0.1)
selfAttentionNorm = LayerNorm(reader: reader, config: config, scope: scope + "/ln_1")
feedForward = FeedForward(reader: reader, config: config, scope: scope + "/mlp")
feedForwardDropout = Dropout(probability: 0.1)
feedForwardNorm = LayerNorm(reader: reader, config: config, scope: scope + "/ln_2")
}
}
extension TransformerLM: InitializableFromPythonCheckpoint {
public init(reader: CheckpointReader, config: TransformerLMConfig, scope: String) {
embedding = Embedding(embeddings: reader.readTensor(name: scope + "/wte"))
positionalEmbeddings = reader.readTensor(name: scope + "/wpe")
embeddingDropout = Dropout(probability: 0.1)
layers = (0..<config.layerCount).map { i in
EncoderLayer(reader: reader, config: config, scope: scope + "/h\(i)")
}
norm = LayerNorm(reader: reader, config: config, scope: scope + "/ln_f")
}
}