-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy patharray.swift
456 lines (405 loc) · 13.5 KB
/
array.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// Would fail due to unavailability of swift_autoDiffCreateLinearMapContext.
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
import StdlibUnittest
import _Differentiation
var ArrayAutoDiffTests = TestSuite("ArrayAutoDiff")
typealias FloatArrayTan = Array<Float>.TangentVector
extension Array.DifferentiableView {
/// A subscript that always fatal errors.
///
/// The differentiation transform should never emit calls to this.
subscript(alwaysFatalError: Int) -> Element {
fatalError("wrong subscript")
}
}
ArrayAutoDiffTests.test("ArrayIdentity") {
func arrayIdentity(_ x: [Float]) -> [Float] {
return x
}
let backprop = pullback(at: [5, 6, 7, 8], of: arrayIdentity)
expectEqual(
FloatArrayTan([1, 2, 3, 4]),
backprop(FloatArrayTan([1, 2, 3, 4])))
}
ArrayAutoDiffTests.test("ArraySubscript") {
func sumFirstThree(_ array: [Float]) -> Float {
return array[0] + array[1] + array[2]
}
expectEqual(
FloatArrayTan([1, 1, 1, 0, 0, 0]),
gradient(at: [2, 3, 4, 5, 6, 7], of: sumFirstThree))
}
ArrayAutoDiffTests.test("ArrayLiteral") {
do {
func twoElementLiteral(_ x: Float, _ y: Float) -> [Float] {
return [x, y]
}
let pb = pullback(at: 1, 1, of: twoElementLiteral)
expectEqual((1, 2), pb(FloatArrayTan([Float(1), Float(2)])))
}
do {
// TF-952: Test array literal initialized from an address (e.g. `var`).
func twoElementLiteralAddress(_ x: Float, _ y: Float) -> [Float] {
var result = x
result = result * y
return [result, result]
}
let pb = pullback(at: 3, 4, of: twoElementLiteralAddress)
expectEqual((8, 6), pb(FloatArrayTan([1, 1])))
}
do {
// TF-952: Test array literal initialized with function call results.
func twoElementLiteralFunctionResult(_ x: Float, _ y: Float) -> [Float] {
return [x * y, x * y]
}
let pb = pullback(at: 3, 4, of: twoElementLiteralFunctionResult)
expectEqual((8, 6), pb(FloatArrayTan([1, 1])))
}
do {
// TF-975: Test multiple array literals.
func twoElementLiterals(_ x: Float, _ y: Float) -> [Float] {
let array = [x * y, x * y]
return [array[0], array[1]]
}
let pb = pullback(at: 3, 4, of: twoElementLiterals)
expectEqual((8, 6), pb(FloatArrayTan([1, 1])))
}
}
ArrayAutoDiffTests.test("ArrayLiteralIndirect") {
do {
func twoElementLiteralIndirect<T: Differentiable>(_ x: T, _ y: T) -> [T] {
return [x, y]
}
let pb = pullback(at: Float(1), 1, of: { twoElementLiteralIndirect($0, $1) })
expectEqual((1, 2), pb(FloatArrayTan([1, 2])))
}
do {
func twoElementLiteralIndirectVar<T: Differentiable>(_ x: T, _ y: T) -> [T] {
var result: [T] = []
result = result + [x]
result = result + [y]
return result
}
let pb = pullback(at: Float(1), 1, of: { twoElementLiteralIndirectVar($0, $1) })
expectEqual((1, 2), pb(FloatArrayTan([1, 2])))
}
}
struct Struct<T> {
var x, y: T
}
extension Struct: Differentiable where T: Differentiable {}
ArrayAutoDiffTests.test("ArrayLiteralStruct") {
typealias TV = Struct<Float>.TangentVector
let s = Struct<Float>(x: 3, y: 4)
do {
func structElementLiteral<T>(_ s: Struct<T>) -> [T] {
return [s.x, s.y]
}
func structGeneric<T>(_ s: Struct<T>) -> T {
return structElementLiteral(s)[0]
}
func structConcrete1(_ s: Struct<Float>) -> Float {
return structElementLiteral(s)[0] * structElementLiteral(s)[1]
}
func structConcrete2(_ s: Struct<Float>) -> Float {
let array = structElementLiteral(s)
return array[0] * array[1]
}
expectEqual(TV(x: 1, y: 0), gradient(at: s, of: { s in structGeneric(s) }))
expectEqual(TV(x: 4, y: 3), gradient(at: s, of: structConcrete1))
expectEqual(TV(x: 4, y: 3), gradient(at: s, of: structConcrete2))
}
do {
func structElementAddressLiteral<T>(_ s: Struct<T>) -> [T] {
var s2 = Struct<T>(x: s.x, y: s.y)
return [s2.x, s2.y]
}
func structGeneric<T>(_ s: Struct<T>) -> T {
return structElementAddressLiteral(s)[0]
}
func structConcrete1(_ s: Struct<Float>) -> Float {
return structElementAddressLiteral(s)[0] *
structElementAddressLiteral(s)[1]
}
func structConcrete2(_ s: Struct<Float>) -> Float {
let array = structElementAddressLiteral(s)
return array[0] * array[1]
}
expectEqual(TV(x: 1, y: 0), gradient(at: s, of: { s in structGeneric(s) }))
expectEqual(TV(x: 4, y: 3), gradient(at: s, of: structConcrete1))
expectEqual(TV(x: 4, y: 3), gradient(at: s, of: structConcrete2))
}
do {
func structElementAddressLiteral2<T>(_ s: Struct<T>) -> [T] {
var s2 = Struct<T>(x: s.x, y: s.y)
let array = [s2.x, s2.y]
return [array[0], array[1]]
}
func structGeneric<T>(_ s: Struct<T>) -> T {
return structElementAddressLiteral2(s)[0]
}
func structConcrete1(_ s: Struct<Float>) -> Float {
return structElementAddressLiteral2(s)[0] *
structElementAddressLiteral2(s)[1]
}
func structConcrete2(_ s: Struct<Float>) -> Float {
let array = structElementAddressLiteral2(s)
return array[0] * array[1]
}
expectEqual(TV(x: 1, y: 0), gradient(at: s, of: { s in structGeneric(s) }))
expectEqual(TV(x: 4, y: 3), gradient(at: s, of: structConcrete1))
expectEqual(TV(x: 4, y: 3), gradient(at: s, of: structConcrete2))
}
// TF-978: Test array literal initialized with `apply` indirect results.
do {
func applyIndirectResult<T>(_ x: T, _ y: T) -> [Struct<T>] {
return [Struct(x: x, y: y), Struct(x: x, y: y)]
}
let pb = pullback(at: Float(3), 4, of: { applyIndirectResult($0, $1) })
let v = TV(x: 1, y: 1)
expectEqual((2, 2), pb(.init([v, v])))
}
}
ArrayAutoDiffTests.test("ArrayLiteralTuple") {
do {
func tupleElementGeneric<T>(_ x: T, _ y: T) -> [T] {
var tuple = (x, y)
return [tuple.0, tuple.1]
}
let pb = pullback(at: Float(3), 4, of: { tupleElementGeneric($0, $1) })
// FIXME(TF-977): Fix incorrect derivative for array literal with
// `tuple_element_addr` elements.
// expectEqual((1, 1), pb(FloatArrayTan([1, 1])))
expectEqual((0, 2), pb(FloatArrayTan([1, 1])))
}
}
ArrayAutoDiffTests.test("ArrayLiteralNested") {
do {
func nested0(
_ x: Float, _ y: Float, _ bool: Bool = true
) -> [Float] {
let result = [[[[x, y]]]]
return result[0][0][0]
}
let pb = pullback(at: 3, 4, of: { nested0($0, $1) })
expectEqual((1, 1), pb(FloatArrayTan([1, 1, 1, 1])))
}
do {
func nested1(
_ x: Float, _ y: Float, _ bool: Bool = true
) -> [Float] {
var result = [[x, y], [x, y]]
return result[0] + result[1]
}
let pb = pullback(at: 3, 4, of: { nested1($0, $1) })
expectEqual((2, 2), pb(FloatArrayTan([1, 1, 1, 1])))
}
do {
// Convoluted function computing `[x + y]`.
func nested2(
_ x: Float, _ y: Float, _ bool: Bool = true
) -> [Float] {
var result = [[], [x]]
result = result + []
result = result + [[]]
result = result + [[y]]
var nested = [result, [], result]
return nested[0][1] + result[3]
}
let (value, pb) = valueWithPullback(at: 3, 4, of: { nested2($0, $1) })
expectEqual([3, 4], value)
expectEqual((1, 1), pb(FloatArrayTan([1, 1])))
}
}
ArrayAutoDiffTests.test("ArrayLiteralControlFlow") {
do {
// TF-922: Test array literal and control flow.
func controlFlow(
_ x: Float, _ y: Float, _ bool: Bool = true
) -> [Float] {
var result = [x * y, x * y]
let result2 = bool ? result : result
var result3 = bool ? (bool ? result2 : result) : result2
return result3
}
let pb = pullback(at: 3, 4, of: { controlFlow($0, $1) })
expectEqual((8, 6), pb(FloatArrayTan([1, 1])))
}
do {
// TF-922: Test array literal and control flow.
func controlFlowAddress(
_ x: Float, _ y: Float, _ bool: Bool = true
) -> [Float] {
var product = x * y // initial value is an address
var result = [product, product]
let result2 = bool ? result : result
var result3 = bool ? (bool ? result2 : result) : result2
return result3
}
let pb = pullback(at: 3, 4, of: { controlFlowAddress($0, $1) })
expectEqual((8, 6), pb(FloatArrayTan([1, 1])))
}
do {
// TF-922: Test array literal and control flow.
func controlFlowGeneric<T>(_ x: T, _ y: T, _ bool: Bool = true) -> [T] {
var result = [x, y] // initial values are addresses
let result2 = bool ? result : result
var result3 = bool ? (bool ? result2 : result) : result2
return result3
}
let pb = pullback(at: Float(3), 4, of: { controlFlowGeneric($0, $1) })
expectEqual((1, 1), pb(FloatArrayTan([1, 1])))
}
do {
// Test nested array literal and control flow.
func controlFlowNestedLiteral(
_ x: Float, _ y: Float, _ bool: Bool = true
) -> [Float] {
var result: [[Float]] = []
var result2 = bool ? result + [[x]] : result + [[x]]
var result3 = bool ? (bool ? result2 + [[y]] : result2 + [[y]]) : result2 + [[y]]
return result3[0] + [result3[1][0]]
}
let pb = pullback(at: 3, 4, of: { controlFlowNestedLiteral($0, $1) })
expectEqual((1, 1), pb(FloatArrayTan([1, 1])))
}
}
ArrayAutoDiffTests.test("ExpressibleByArrayLiteralIndirect") {
struct Indirect<T: Differentiable>: Differentiable & ExpressibleByArrayLiteral {
var x: T
typealias ArrayLiteralElement = T
init(arrayLiteral: T...) {
assert(arrayLiteral.count > 1)
self.x = arrayLiteral[0]
}
}
func testArrayUninitializedIntrinsic<T>(_ x: T, _ y: T) -> Indirect<T> {
return [x, y]
}
let (gradX, gradY) = pullback(at: Float(1), Float(1), of: {
x, y in testArrayUninitializedIntrinsic(x, y)
})(Indirect<Float>.TangentVector(x: 1))
expectEqual(1, gradX)
expectEqual(0, gradY)
}
ArrayAutoDiffTests.test("Array.+") {
func sumFirstThreeConcatenating(_ a: [Float], _ b: [Float]) -> Float {
let c = a + b
return c[0] + c[1] + c[2]
}
expectEqual(
(.init([1, 1]), .init([1, 0])),
gradient(at: [0, 0], [0, 0], of: sumFirstThreeConcatenating))
expectEqual(
(.init([1, 1, 1, 0]), .init([0, 0])),
gradient(at: [0, 0, 0, 0], [0, 0], of: sumFirstThreeConcatenating))
expectEqual(
(.init([]), .init([1, 1, 1, 0])),
gradient(at: [], [0, 0, 0, 0], of: sumFirstThreeConcatenating))
func identity(_ array: [Float]) -> [Float] {
var results: [Float] = []
for i in withoutDerivative(at: array.indices) {
results = results + [array[i]]
}
return results
}
let v = FloatArrayTan([4, -5, 6])
expectEqual(v, pullback(at: [1, 2, 3], of: identity)(v))
let v1: [Float] = [1, 1]
let v2: [Float] = [1, 1, 1]
expectEqual((.zero, .zero), pullback(at: v1, v2, of: +)(.zero))
}
ArrayAutoDiffTests.test("Array.+=") {
func sumFirstThreeConcatenating(_ a: [Float], _ b: [Float]) -> Float {
var c = a
c += b
return c[0] + c[1] + c[2]
}
expectEqual(
(.init([1, 1]), .init([1, 0])),
gradient(at: [0, 0], [0, 0], of: sumFirstThreeConcatenating))
expectEqual(
(.init([1, 1, 1, 0]), .init([0, 0])),
gradient(at: [0, 0, 0, 0], [0, 0], of: sumFirstThreeConcatenating))
expectEqual(
(.init([]), .init([1, 1, 1, 0])),
gradient(at: [], [0, 0, 0, 0], of: sumFirstThreeConcatenating))
func identity(_ array: [Float]) -> [Float] {
var results: [Float] = []
for i in withoutDerivative(at: array.indices) {
results += [array[i]]
}
return results
}
let v = FloatArrayTan([4, -5, 6])
expectEqual(v, pullback(at: [1, 2, 3], of: identity)(v))
}
ArrayAutoDiffTests.test("Array.append") {
func appending(_ array: [Float], _ element: Float) -> [Float] {
var result = array
result.append(element)
return result
}
do {
let v = FloatArrayTan([1, 2, 3, 4])
expectEqual((.init([1, 2, 3]), 4),
pullback(at: [0, 0, 0], 0, of: appending)(v))
}
func identity(_ array: [Float]) -> [Float] {
var results: [Float] = []
for i in withoutDerivative(at: array.indices) {
results.append(array[i])
}
return results
}
do {
let v = FloatArrayTan([4, -5, 6])
expectEqual(v, pullback(at: [1, 2, 3], of: identity)(v))
}
}
ArrayAutoDiffTests.test("Array.init(repeating:count:)") {
@differentiable(reverse)
func repeating(_ x: Float) -> [Float] {
Array(repeating: x, count: 10)
}
expectEqual(Float(10), gradient(at: .zero) { x in
repeating(x).differentiableReduce(0, {$0 + $1})
})
expectEqual(Float(20), pullback(at: .zero, of: { x in
repeating(x).differentiableReduce(0, {$0 + $1})
})(2))
}
ArrayAutoDiffTests.test("Array.DifferentiableView.init") {
@differentiable(reverse)
func constructView(_ x: [Float]) -> Array<Float>.DifferentiableView {
return Array<Float>.DifferentiableView(x)
}
let backprop = pullback(at: [5, 6, 7, 8], of: constructView)
expectEqual(
FloatArrayTan([1, 2, 3, 4]),
backprop(FloatArrayTan([1, 2, 3, 4])))
}
ArrayAutoDiffTests.test("Array.DifferentiableView.base") {
@differentiable(reverse)
func accessBase(_ x: Array<Float>.DifferentiableView) -> [Float] {
return x.base
}
let backprop = pullback(
at: Array<Float>.DifferentiableView([5, 6, 7, 8]),
of: accessBase)
expectEqual(
FloatArrayTan([1, 2, 3, 4]),
backprop(FloatArrayTan([1, 2, 3, 4])))
}
ArrayAutoDiffTests.test("Array.DifferentiableView.move") {
var v: [Float] = [1, 2, 3]
v.move(by: .zero)
expectEqual(v, [1, 2, 3])
var z: [Float] = []
z.move(by: .zero)
expectEqual(z, [])
}
runAllTests()