-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathCheckpointIndexWriter.swift
255 lines (217 loc) · 9.51 KB
/
CheckpointIndexWriter.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
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
// Copyright 2020 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 Foundation
import TensorFlow
class CheckpointIndexWriter {
// TODO: Extend handling to different tensor types.
let tensors: [String: Tensor<Float>]
let orderedTensors: [String]
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/lib/io/table_options.h#L48
let blockRestartInterval = 15
init(tensors: [String: Tensor<Float>]) {
self.tensors = tensors
self.orderedTensors = tensors.keys.sorted()
}
}
extension CheckpointIndexWriter {
func serializedHeader() -> Data {
var outputBuffer = Data()
// TODO: Expand beyond using a single binary shard.
outputBuffer.append(headerBlock(shards: 1))
var lastString = ""
var intervalSinceLastRestart = 1
var restarts: [UInt32] = [0]
var offset: Int64 = 0
for key in orderedTensors {
outputBuffer.append(keyValueBlock(key: key, lastString: lastString, offset: &offset))
// With prefix compression, the entire string is used as a restart at defined intervals.
if intervalSinceLastRestart < blockRestartInterval {
lastString = key
intervalSinceLastRestart += 1
} else {
lastString = ""
intervalSinceLastRestart = 0
restarts.append(UInt32(outputBuffer.count))
}
}
restarts.append(UInt32(restarts.count))
// Write the restart offsets as a trailer to the data block.
restarts.withUnsafeBufferPointer { (ptr) in
ptr.baseAddress!.withMemoryRebound(
to: UInt8.self, capacity: ptr.count * MemoryLayout<UInt32>.size
) {
outputBuffer.append($0, count: ptr.count * MemoryLayout<UInt32>.size)
}
}
// The type of the block, with 0 signifying uncompressed.
outputBuffer.append(contentsOf: [0])
let crc32C = outputBuffer.maskedCRC32C()
outputBuffer.append(contentsOf: crc32C.littleEndianBuffer)
let headerSize = outputBuffer.count - 5
// Data block is finished, terminate the file with meta index, index, and footer blocks.
let metaIndex = metaIndexBlock()
let metaIndexSize = metaIndex.count - 5
let metaIndexOffset = outputBuffer.count
outputBuffer.append(metaIndex)
let index = indexBlock(lastKey: lastString, headerSize: headerSize)
let indexSize = index.count - 5
let indexOffset = outputBuffer.count
outputBuffer.append(index)
outputBuffer.append(
footerBlock(
metaIndexHandle: (metaIndexOffset, metaIndexSize),
indexHandle: (indexOffset, indexSize)))
return outputBuffer
}
// Based on the LevelDB implementation of the same function.
func findShortestSuccessor(_ key: String) -> [UInt8] {
var newKeyBytes: [UInt8] = []
for byte in key.utf8 {
let castByte = UInt8(byte)
if castByte != 0xFF {
newKeyBytes.append(castByte + 1)
return newKeyBytes
}
newKeyBytes.append(castByte)
}
return newKeyBytes
}
func headerBlock(shards: Int) -> Data {
var headerVersion = Tensorflow_VersionDef()
headerVersion.producer = 1
var headerProtobuf = Tensorflow_BundleHeaderProto()
headerProtobuf.numShards = Int32(shards)
headerProtobuf.version = headerVersion
do {
let headerValue = try headerProtobuf.serializedData()
var outputBuffer = indexBytes(
sharedKeyBytes: 0, newKeyBytes: 0, valueLength: headerValue.count)
outputBuffer.append(headerValue)
return outputBuffer
} catch {
fatalError("Could not serialize header protobuf: \(error).")
}
}
func keyValueBlock(key: String, lastString: String, offset: inout Int64) -> Data {
guard let tensor = tensors[key] else { fatalError("Mismatch on tensor key: \(key).") }
var entryProtobuf = Tensorflow_BundleEntryProto()
var shape = Tensorflow_TensorShapeProto()
shape.dim = tensor.shape.dimensions.map { size -> Tensorflow_TensorShapeProto.Dim in
var dim = Tensorflow_TensorShapeProto.Dim()
dim.size = Int64(size)
return dim
}
let tensorSize: Int64 = Int64(
MemoryLayout<Float>.size * tensor.shape.dimensions.reduce(1) { $0 * $1 })
entryProtobuf.dtype = .dtFloat
entryProtobuf.shape = shape
entryProtobuf.offset = offset
entryProtobuf.size = tensorSize
// TODO: Find a more efficient way of calculating this without casting to bytes twice.
let scalars = tensor.array.scalars
scalars.withUnsafeBufferPointer { (ptr) in
ptr.baseAddress!.withMemoryRebound(
to: UInt8.self, capacity: ptr.count * MemoryLayout<Float>.size
) {
let tensorData = Data(bytes: $0, count: ptr.count * MemoryLayout<Float>.size)
entryProtobuf.crc32C = tensorData.maskedCRC32C()
}
}
offset += tensorSize
do {
let entryValue = try entryProtobuf.serializedData()
let commonPrefix = lastString.commonPrefix(with: key)
let newCharacters = key.count - commonPrefix.count
var outputBuffer = indexBytes(
sharedKeyBytes: commonPrefix.count, newKeyBytes: newCharacters,
valueLength: entryValue.count)
let suffix = key.suffix(newCharacters).utf8
outputBuffer.append(contentsOf: suffix)
outputBuffer.append(entryValue)
return outputBuffer
} catch {
fatalError("Could not serialize header protobuf: \(error).")
}
}
func indexBlock(lastKey: String, headerSize: Int) -> Data {
var headerHandle = Data()
headerHandle.appendVarint(0)
headerHandle.appendVarint(headerSize)
let shortestSuccessor = findShortestSuccessor(lastKey)
var outputBuffer = indexBytes(
sharedKeyBytes: 0, newKeyBytes: shortestSuccessor.count,
valueLength: headerHandle.count)
outputBuffer.append(contentsOf: shortestSuccessor)
outputBuffer.append(headerHandle)
// There were no restarts, but need to write out the buffer and count.
outputBuffer.append(contentsOf: [0, 0, 0, 0])
outputBuffer.append(contentsOf: [1, 0, 0, 0])
// The type of the block, with 0 signifying uncompressed.
outputBuffer.append(contentsOf: [0])
let crc32C = outputBuffer.maskedCRC32C()
outputBuffer.append(contentsOf: crc32C.littleEndianBuffer)
return outputBuffer
}
func metaIndexBlock() -> Data {
// The meta index block is unused, but is still written.
var outputBuffer = Data()
outputBuffer.append(contentsOf: [0, 0, 0, 0])
outputBuffer.append(contentsOf: [1, 0, 0, 0, 0])
let crc32C = outputBuffer.maskedCRC32C()
outputBuffer.append(contentsOf: crc32C.littleEndianBuffer)
return outputBuffer
}
func footerBlock(metaIndexHandle: (Int, Int), indexHandle: (Int, Int)) -> Data {
// Footer format, as defined in LevelDB:
// https://github.com/google/leveldb/blob/master/doc/table_format.md
// Two handles (offset, size varint pairs) for the meta index and index blocks are followed
// by zeroes to pad out to `footerSize - 8` bytes, with an 8-byte terminating magic number.
var footerBytes = Data()
footerBytes.appendVarint(metaIndexHandle.0)
footerBytes.appendVarint(metaIndexHandle.1)
footerBytes.appendVarint(indexHandle.0)
footerBytes.appendVarint(indexHandle.1)
footerBytes.append(Data(count: footerSize - 8 - footerBytes.count))
let magicNumber: [UInt8] = [0x57, 0xFB, 0x80, 0x8B, 0x24, 0x75, 0x47, 0xDB]
footerBytes.append(contentsOf: magicNumber)
return footerBytes
}
func indexBytes(sharedKeyBytes: Int, newKeyBytes: Int, valueLength: Int) -> Data {
var outputBuffer = Data()
outputBuffer.appendVarint(sharedKeyBytes)
outputBuffer.appendVarint(newKeyBytes)
outputBuffer.appendVarint(valueLength)
return outputBuffer
}
}
extension Data {
// Logic from https://github.com/apple/swift-protobuf/blob/master/Sources/SwiftProtobuf/BinaryEncoder.swift#L68
mutating func appendVarint(_ value: Int) {
var v = value
while v > 127 {
self.append(contentsOf: [UInt8(v & 0x7f | 0x80)])
v >>= 7
}
self.append(contentsOf: [UInt8(v)])
}
}
extension UInt32 {
var littleEndianBuffer: [UInt8] {
return [self].withUnsafeBufferPointer { (ptr) in
ptr.baseAddress!.withMemoryRebound(
to: UInt8.self, capacity: 4
) { [UInt8](UnsafeBufferPointer(start: $0, count: 4)) }
}
}
}