This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathEpochsTests.swift
411 lines (366 loc) · 13.6 KB
/
EpochsTests.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
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
// 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 XCTest
@testable import TensorFlow
var rng = ARC4RandomNumberGenerator(seed: [42])
final class EpochsTests: XCTestCase {
// An element that keeps track of when it was first accessed.
class AccessTracker {
var accessed: Bool = false
}
// A struct keeping track of when its elements have been first accessed. We
// use it in the tests to check whether methods that are not supposed to break
// the laziness work as intended.
/// An adapted collection that presents the elements of `Base` but
/// tracks whether elements have been read.
///
/// - Warning: distinct elements may be read concurrently, but reading
/// the same element from two threads is a race condition.
struct ReadTracker<Base: RandomAccessCollection>: RandomAccessCollection {
let base: Base
let accessed_: [AccessTracker]
public typealias Element = Base.Element
/// A type whose instances represent positions in `self`.
public typealias Index = Base.Index
/// The position of the first element.
public var startIndex: Index { base.startIndex }
/// The position one past the last element.
public var endIndex: Index { base.endIndex }
/// Returns the position after `i`.
public func index(after i: Index) -> Index { base.index(after: i) }
/// Returns the position after `i`.
public func index(before i: Index) -> Index { base.index(before: i) }
init(_ base: Base) {
self.base = base
accessed_ = (0..<base.count).map { _ in AccessTracker() }
}
subscript(i: Base.Index) -> Base.Element {
accessed_[base.distance(from: base.startIndex, to: i)].accessed = true
return base[i]
}
var accessed: LazyMapCollection<[AccessTracker], Bool> {
accessed_.lazy.map(\.accessed)
}
}
func testBaseUse() {
let batchSize = 64
let dataset = (0..<512).map { (_) -> Tensor<Float> in
Tensor<Float>(randomNormal: [224, 224, 3])
}
let batches = dataset.inBatches(of: batchSize).lazy.map(\.collated)
XCTAssertEqual(
batches.count, dataset.count / batchSize,
"Incorrect number of batches.")
for batch in batches {
XCTAssertEqual(
batch.shape, TensorShape([64, 224, 224, 3]),
"Wrong shape for batch: \(batch.shape), should be [64, 224, 224, 3]")
}
}
func testInBatchesIsLazy() {
let batchSize = 64
let items = Array(0..<512)
let dataset = ReadTracker(items)
let batches = dataset.inBatches(of: batchSize)
// `inBatches` is lazy so no elements were accessed.
XCTAssert(
dataset.accessed.allSatisfy { !$0 },
"Laziness failure: no elements should have been accessed yet.")
for (i, batch) in batches.enumerated() {
// Elements are not accessed until we do something with `batch` so only
// the elements up to `i * batchSize` have been accessed yet.
XCTAssert(
dataset.accessed[..<(i * batchSize)].allSatisfy { $0 },
"Some samples in a prior batch were unexpectedly skipped.")
XCTAssert(
dataset.accessed[(i * batchSize)...].allSatisfy { !$0 },
"Laziness failure: some samples were read prematurely.")
let _ = Array(batch)
let limit = (i + 1) * batchSize
// We accessed elements up to `limit` but no further.
XCTAssert(
dataset.accessed[..<limit].allSatisfy { $0 },
"Some samples in a prior batch were unexpectedly skipped.")
XCTAssert(
dataset.accessed[limit...].allSatisfy { !$0 },
"Laziness failure: some samples were read prematurely.")
}
}
func testTrainingEpochsShuffles() {
let batchSize = 64
let dataset = Array(0..<512)
let epochs = TrainingEpochs(
samples: dataset, batchSize: batchSize,
entropy: rng
).prefix(10)
var lastEpochSampleOrder: [Int]? = nil
for batches in epochs {
var newEpochSampleOrder: [Int] = []
for batch in batches {
XCTAssertEqual(batches.count, 8, "Incorrect number of batches.")
let samples = Array(batch)
XCTAssertEqual(
samples.count, batchSize,
"This batch doesn't have batchSize elements.")
newEpochSampleOrder += samples
}
if let l = lastEpochSampleOrder {
XCTAssertNotEqual(
l, newEpochSampleOrder,
"Dataset should have been reshuffled.")
}
let uniqueSamples = Set(newEpochSampleOrder)
XCTAssertEqual(
uniqueSamples.count, newEpochSampleOrder.count,
"Every epoch sample should be drawn from a different input sample.")
lastEpochSampleOrder = newEpochSampleOrder
}
}
func testTrainingEpochsShapes() {
let batchSize = 64
let dataset = 0..<500
let epochs = TrainingEpochs(
samples: dataset, batchSize: batchSize,
entropy: rng
).prefix(1)
for epochBatches in epochs {
XCTAssertEqual(epochBatches.count, 7, "Incorrect number of batches.")
var epochSampleCount = 0
for batch in epochBatches {
XCTAssertEqual(
batch.count, batchSize, "unexpected batch size: \(batch.count)")
epochSampleCount += batch.count
}
let expectedDropCount = dataset.count % 64
let actualDropCount = dataset.count - epochSampleCount
XCTAssertEqual(
expectedDropCount, actualDropCount,
"Dropped \(actualDropCount) samples but expected \(expectedDropCount).")
}
}
func testTrainingEpochsIsLazy() {
let batchSize = 64
let items = Array(0..<512)
let dataset = ReadTracker(items)
let epochs = TrainingEpochs(
samples: dataset, batchSize: batchSize,
entropy: rng
).prefix(1)
// `inBatches` is lazy so no elements were accessed.
XCTAssert(
dataset.accessed.allSatisfy { !$0 },
"No elements should have been accessed yet.")
for batches in epochs {
for (i, batch) in batches.enumerated() {
// Elements are not accessed until we do something with `batch` so only
// `i * batchSize` elements have been accessed yet.
XCTAssertEqual(
dataset.accessed.filter { $0 }.count, i * batchSize,
"Should have accessed \(i * batchSize) elements.")
let _ = Array(batch)
XCTAssertEqual(
dataset.accessed.filter { $0 }.count, (i + 1) * batchSize,
"Should have accessed \((i + 1) * batchSize) elements.")
}
}
}
// Use with padding
// Let's create an array of things of various lengths (for instance texts)
let nonuniformDataset: [Tensor<Int32>] = {
var dataset: [Tensor<Int32>] = []
for _ in 0..<512 {
dataset.append(
Tensor<Int32>(
repeating: 1,
shape: [Int.random(in: 1...200, using: &rng)]
))
}
return dataset
}()
func paddingTest(padValue: Int32, atStart: Bool) {
let batches = nonuniformDataset.inBatches(of: 64)
.lazy.map { $0.paddedAndCollated(with: padValue, atStart: atStart) }
for (i, b) in batches.enumerated() {
let shapes = nonuniformDataset[(i * 64)..<((i + 1) * 64)]
.map { Int($0.shape[0]) }
let expectedShape = shapes.reduce(0) { max($0, $1) }
XCTAssertEqual(
Int(b.shape[1]), expectedShape,
"The batch does not have the expected shape: \(expectedShape).")
for k in 0..<64 {
let currentShape = nonuniformDataset[i * 64 + k].shape[0]
let paddedPart =
atStart ? b[k, 0..<(expectedShape - currentShape)] : (b[k, currentShape..<expectedShape])
XCTAssertEqual(
paddedPart,
Tensor<Int32>(
repeating: padValue,
shape: [expectedShape - currentShape]),
"Padding was not found where it should be.")
}
}
}
func testAllPadding() {
paddingTest(padValue: 0, atStart: false)
paddingTest(padValue: 42, atStart: false)
paddingTest(padValue: 0, atStart: true)
paddingTest(padValue: -1, atStart: true)
}
let cuts = [0, 5, 8, 15, 24, 30]
var texts: [[Int]] { (0..<5).map { Array(cuts[$0]..<cuts[$0 + 1]) } }
// To reindex the dataset such that the first batch samples are given by
// indices (0, batchCount, batchCount * 2, ...
func preBatchTranspose<C: Collection>(_ base: C, for batchSize: Int)
-> [C.Index]
{
let batchCount = base.count / batchSize
return (0..<base.count).map { (i: Int) -> C.Index in
let j = batchCount * (i % batchSize) + i / batchSize
return base.index(base.startIndex, offsetBy: j)
}
}
//Now let's look at what it gives us:
func testLanguageModel() {
let sequenceLength = 3
let batchSize = 2
let sequences = texts.joined()
.inBatches(of: sequenceLength)
let indices = preBatchTranspose(sequences, for: batchSize)
let batches = sequences.sampled(at: indices).inBatches(of: batchSize)
var results: [[Int32]] = [[], []]
for batch in batches {
let tensor = Tensor<Int32>(
batch.map {
Tensor<Int32>(
$0.map { Int32($0) })
})
XCTAssertEqual(tensor.shape, TensorShape([2, 3]))
results[0] += tensor[0].scalars
results[1] += tensor[1].scalars
}
XCTAssertEqual(results[0] + results[1], (0..<30).map { Int32($0) })
}
func isSubset(_ x: [Int], from y: [Int]) -> Bool {
if let i = y.firstIndex(of: x[0]) {
return x.enumerated().allSatisfy { (k: Int, o: Int) -> Bool in
o == y[i + k]
}
}
return false
}
func testLanguageModelShuffled() {
let sequenceLength = 3
let batchSize = 2
let sequences = texts.shuffled().joined()
.inBatches(of: sequenceLength)
let indices = preBatchTranspose(sequences, for: batchSize)
let batches = sequences.sampled(at: indices).inBatches(of: batchSize)
var results: [[Int32]] = [[], []]
for batch in batches {
let tensor = Tensor<Int32>(
batch.map {
Tensor<Int32>(
$0.map { Int32($0) })
})
XCTAssertEqual(tensor.shape, TensorShape([2, 3]))
results[0] += tensor[0].scalars
results[1] += tensor[1].scalars
}
let stream = (results[0] + results[1]).map { Int($0) }
XCTAssertEqual(stream.count, 30)
XCTAssert(texts.allSatisfy { isSubset($0, from: stream) })
}
class SizedSample {
init(size: Int) { self.size = size }
var size: Int
}
func testNonuniformInferenceBatches() {
let sampleCount = 503
let batchSize = 7
let samples = (0..<sampleCount).map {
_ in SizedSample.init(size: Int.random(in: 0..<1000, using: &rng))
}
let batches = NonuniformInferenceBatches(
samples: samples, batchSize: batchSize
) { $0.size < $1.size }
XCTAssertEqual(
batches.count, sampleCount / batchSize + 1,
"Wrong number of batches")
var previousSize: Int? = nil
for (i, batchSamples) in batches.enumerated() {
let batch = Array(batchSamples)
XCTAssertEqual(
batch.count,
i == batches.count - 1 ? sampleCount % batchSize : batchSize,
"Wrong number of samples in this batch.")
let newSize = batch.map(\.size).max()!
if let size = previousSize {
XCTAssert(
size >= newSize,
"Batch should be sorted through size.")
}
previousSize = Int(newSize)
}
}
func testNonuniformTrainingEpochs() {
let sampleCount = 503
let batchSize = 7
let samples = (0..<sampleCount).map {
_ in SizedSample.init(size: Int.random(in: 0..<1000, using: &rng))
}
let epochs = NonuniformTrainingEpochs(
samples: samples,
batchSize: batchSize,
entropy: rng
) { $0.size < $1.size }
// The first sample ordering observed during this test.
var observedSampleOrder: [ObjectIdentifier]?
for batches in epochs.prefix(10) {
XCTAssertEqual(batches.count, sampleCount / batchSize)
XCTAssert(batches.allSatisfy { $0.count == batchSize })
let epochSamples = batches.joined()
let epochSampleOrder = epochSamples.lazy.map(ObjectIdentifier.init)
if let o = observedSampleOrder {
XCTAssertFalse(
o.elementsEqual(epochSampleOrder),
"Batches should be randomized")
} else {
observedSampleOrder = Array(epochSampleOrder)
}
let maxEpochSampleSize = epochSamples.lazy.map(\.size).max()!
XCTAssertEqual(
batches.first!.lazy.map(\.size).max(),
maxEpochSampleSize,
"The first batch should contain a sample of maximal size.")
let uniqueSamples = Set(epochSampleOrder)
XCTAssertEqual(
uniqueSamples.count, epochSamples.count,
"Every epoch sample should be drawn from a different input sample.")
}
}
}
extension EpochsTests {
static var allTests = [
("testAllPadding", testAllPadding),
("testInBatchesIsLazy", testInBatchesIsLazy),
("testBaseUse", testBaseUse),
("testTrainingEpochsShuffles", testTrainingEpochsShuffles),
("testTrainingEpochsShapes", testTrainingEpochsShapes),
("testTrainingEpochsIsLazy", testTrainingEpochsIsLazy),
("testLanguageModel", testLanguageModel),
("testLanguageModelShuffled", testLanguageModelShuffled),
("testNonuniformInferenceBatches", testNonuniformInferenceBatches),
("testNonuniformTrainingEpochs", testNonuniformTrainingEpochs),
]
}