-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathgenerics.swift
390 lines (350 loc) · 11.9 KB
/
generics.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
// RUN: %target-swift-emit-sil -verify %s | %FileCheck %s -check-prefix=CHECK-SIL
import _Differentiation
@_silgen_name("identity")
func identity<T : Differentiable>(_ x: T) -> T {
return x
}
_ = gradient(at: Float(1), of: { x in identity(x) })
// Test PullbackCloner local buffer allocation.
// Verify that local buffers are immediately set to zero.
// CHECK-SIL-LABEL: sil private @identity16_Differentiation14DifferentiableRzlTJpSpSr
// CHECK-SIL: [[ORIG_COTAN:%.*]] = alloc_stack $τ_0_0.TangentVector
// CHECK-SIL-NEXT: [[ZERO_WITNESS:%.*]] = witness_method $τ_0_0.TangentVector, #AdditiveArithmetic.zero!getter
// CHECK-SIL-NEXT: [[ORIG_COTAN_METATYPE:%.*]] = metatype $@thick τ_0_0.TangentVector.Type
// CHECK-SIL-NEXT: [[EMIT_ZERO_INDIRECT:%.*]] = apply [[ZERO_WITNESS]]<τ_0_0.TangentVector>([[ORIG_COTAN]], [[ORIG_COTAN_METATYPE]])
// CHECK-SIL: }
// Test TF-201: differentiate direct references to generic function.
// This involves reabstraction thunk differentiation.
_ = gradient(at: Float(1), of: identity)
protocol DifferentiableAdditiveArithmetic: Differentiable & AdditiveArithmetic {
@differentiable(reverse)
static func + (lhs: Self, rhs: Self) -> Self
}
extension Float: DifferentiableAdditiveArithmetic {}
func generic<T: DifferentiableAdditiveArithmetic>(_ x: T) -> T {
x + x + x
}
_ = gradient(at: Float(10), of: generic)
struct Wrapper<Scalar : Differentiable> : Differentiable {
var value: Scalar
init(_ value: Scalar) { self.value = value }
}
func generic<T>(_ x: Wrapper<T>) -> T {
return x.value
}
_ = gradient(at: Wrapper<Float>(1), of: generic)
func generic2<T: Differentiable, U: Differentiable>(_ x: T, _ y: Float, _ z: U) -> T {
return x
}
func foo<T>(_ x: Wrapper<T>) {
_ = gradient(at: Float(1), 2, x, of: generic2)
}
// Test case where associated derivative function's requirements are met.
extension Wrapper where Scalar : Numeric {
@differentiable(reverse, wrt: self where Scalar : Differentiable & FloatingPoint)
func mean() -> Wrapper {
return self
}
@differentiable(reverse, wrt: self where Scalar : Differentiable & FloatingPoint)
func variance() -> Wrapper {
return mean() // ok
}
}
_ = pullback(at: Wrapper<Float>(1), of: { $0.variance() })
// Tests TF-277.
protocol Layer : Differentiable {
associatedtype Output : Differentiable
}
struct SupervisedTrainer<Model : Layer> {
var model: Model
var lossFunction: @differentiable(reverse) (Model.Output, Model.Output) -> Float
func fit(y: Model.Output) {
_ = gradient(at: y) { y in return self.lossFunction(y, y) }
}
}
// Tests TF-440.
struct TF_440_Input<Input: Differentiable, State: Differentiable>
: Differentiable {
var input: Input
var state: State
}
struct TF_440<T : Differentiable> {
@differentiable(reverse)
func applied(to input: TF_440_Input<Float, Float>) -> Float {
return input.state
}
@differentiable(reverse)
func applied(to input: TF_440_Input<T, Float>) -> Float {
return input.state
}
@differentiable(reverse)
func applied(to input: TF_440_Input<T, Float>) -> T {
return input.input
}
}
// Tests TF-508: differentiation requirements with dependent member types.
protocol TF_508_Proto {
associatedtype Scalar
}
extension TF_508_Proto where Scalar : FloatingPoint {
@differentiable(reverse
where Self : Differentiable, Scalar : Differentiable,
// Conformance requirement with dependent member type.
Self.TangentVector : TF_508_Proto
)
static func +(lhs: Self, rhs: Self) -> Self {
return lhs
}
@differentiable(reverse
where Self : Differentiable, Scalar : Differentiable,
// Same-type requirement with dependent member type.
Self.TangentVector == Float
)
static func -(lhs: Self, rhs: Self) -> Self {
return lhs
}
}
extension TF_508_Proto where Self : Differentiable,
Scalar : FloatingPoint & Differentiable,
Self.TangentVector : TF_508_Proto {
@derivative(of: +)
static func vjpAdd(lhs: Self, rhs: Self)
-> (value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)) {
return (lhs, { v in (v, v) })
}
}
extension TF_508_Proto where Self : Differentiable,
Scalar : FloatingPoint & Differentiable,
Self.TangentVector == Float {
@derivative(of: -)
static func vjpSubtract(lhs: Self, rhs: Self)
-> (value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)) {
return (lhs, { v in (v, v) })
}
}
struct TF_508_Struct<Scalar : AdditiveArithmetic>
: TF_508_Proto, AdditiveArithmetic {}
extension TF_508_Struct : Differentiable where Scalar : Differentiable {
typealias TangentVector = TF_508_Struct
}
func TF_508() {
let x = TF_508_Struct<Float>()
// Test conformance requirement with dependent member type.
_ = pullback(at: x, of: { (x: TF_508_Struct<Float>) -> TF_508_Struct<Float> in
return x + x
})
// Test same-type requirement with dependent member type.
_ = pullback(at: x, of: { (x: TF_508_Struct<Float>) -> TF_508_Struct<Float> in
return x - x
})
}
// TF-523
struct TF_523_Struct : Differentiable & AdditiveArithmetic {
var a: Float = 1
typealias TangentVector = TF_523_Struct
}
@differentiable(reverse)
func TF_523_f(_ x: TF_523_Struct) -> Float {
return x.a * 2
}
// TF-534: Thunk substitution map remapping.
protocol TF_534_Layer : Differentiable {
associatedtype Input : Differentiable
associatedtype Output : Differentiable
@differentiable(reverse)
func callAsFunction(_ input: Input) -> Output
}
struct TF_534_Tensor<Scalar> : Differentiable {}
func TF_534<Model: TF_534_Layer>(
_ model: inout Model, inputs: Model.Input
) -> TF_534_Tensor<Float> where Model.Output == TF_534_Tensor<Float> {
return valueWithPullback(at: model) { model -> Model.Output in
return model(inputs)
}.0
}
// TF-546: Test that SILGen linear map thunk performs correct reabstraction.
struct TF_546<T: FloatingPoint>: AdditiveArithmetic {
var real: T
var imaginary: T
@differentiable(reverse where T: Differentiable, T == T.TangentVector)
init(real: T = 0, imaginary: T = 0) {
self.real = real
self.imaginary = imaginary
}
}
extension TF_546: Differentiable where T: Differentiable {
typealias TangentVector = TF_546
}
extension TF_546 where T: Differentiable, T == T.TangentVector {
@derivative(of: init)
static func _vjpInit(real: T, imaginary: T) -> (value: TF_546, pullback: (TF_546) -> (T, T)) {
return (TF_546(real: real, imaginary: imaginary), { ($0.real, $0.imaginary) })
}
}
let _: @differentiable(reverse) (Float, Float) -> TF_546<Float> = { r, i in
TF_546(real: r, imaginary: i)
}
// TF-652: Test VJPCloner substitution map generic signature.
// The substitution map should have the VJP's generic signature, not the
// original function's.
struct TF_652<Scalar> {}
extension TF_652 : Differentiable where Scalar : FloatingPoint {}
@differentiable(reverse, wrt: x where Scalar: FloatingPoint)
func test<Scalar: Numeric>(x: TF_652<Scalar>) -> TF_652<Scalar> {
for _ in 0..<10 {
let _ = x
}
return x
}
// TF-682: Test that SILGen linear map thunk performs correct reabstraction.
protocol TF_682_Proto {
associatedtype Scalar
}
extension TF_682_Proto where Scalar : FloatingPoint {
@differentiable(reverse
where Self : Differentiable, Scalar : Differentiable,
// Same-type requirement with dependent member type.
Self.TangentVector == Float
)
func foo(lhs: Self) -> Self {
return lhs
}
}
extension TF_682_Proto where Self : Differentiable,
Scalar : FloatingPoint & Differentiable,
Self.TangentVector == Float {
@derivative(of: foo)
func vjpFoo(lhs: Self) -> (
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)
) {
return (lhs, { v in (v, v) })
}
}
// NOTE(TF-1208): Differentiation regression due to changes in curry thunk generation.
/*
// TF-688: Test generic curry thunk cloning.
public struct TF_688_Struct<Scalar> {
var x: Scalar
}
extension TF_688_Struct: Differentiable where Scalar: Differentiable {
@differentiable(reverse)
public static func id(x: Self) -> Self {
return x
}
}
@differentiable(reverse, wrt: x)
public func TF_688<Scalar: Differentiable>(
_ x: TF_688_Struct<Scalar>,
reduction: @differentiable(reverse) (TF_688_Struct<Scalar>) -> TF_688_Struct<Scalar> = TF_688_Struct.id
) -> TF_688_Struct<Scalar> {
reduction(x)
}
*/
// TF-697: Test generic requirements of generated derivative function.
protocol TF_697_Module: Differentiable {
associatedtype Input
associatedtype Output: Differentiable
@differentiable(reverse, wrt: self)
func callModule(_ input: Input) -> Output
}
protocol TF_697_Layer: TF_697_Module where Input: Differentiable {
@differentiable(reverse)
func callLayer(_ input: Input) -> Output
}
struct TF_697_Sequential<Layer1: TF_697_Module, Layer2: TF_697_Layer>: TF_697_Module
where Layer1.Output == Layer2.Input {
var layer1: Layer1
var layer2: Layer2
@differentiable(reverse, wrt: self)
func callModule(_ input: Layer1.Input) -> Layer2.Output {
layer2.callLayer(layer1.callModule(input))
}
}
extension TF_697_Sequential: TF_697_Layer where Layer1: TF_697_Layer {
@differentiable(reverse)
func callLayer(_ input: Layer1.Input) -> Layer2.Output {
layer2.callLayer(layer1.callLayer(input))
}
}
// TF-817: Test remapping `apply` callee types in derivative function context.
struct TF_817<T> {
func foo(_ index: Int) -> T {
fatalError()
}
}
extension TF_817: Differentiable where T: Differentiable {
@derivative(of: foo)
func vjpFoo(index: Int) -> (value: T, pullback: (T.TangentVector) -> (TangentVector)) {
fatalError()
}
}
extension TF_817 {
@differentiable(reverse, wrt: self where T: Differentiable)
public func test(index: Int) -> T {
return self.foo(0) // crash happened here
}
}
// TF-886: Test `partial_apply` of linear map subset parameters thunk.
@differentiable(reverse)
func TF_886_foo<T, U: Differentiable>(_: Float, _: T, _: U) -> Float {
return 0
}
@differentiable(reverse)
func TF_886_bar<T>(x: Float, y: T) -> Float {
return TF_886_foo(x, y, 0)
}
// Test layout requirements.
// The layout requirement is "contextual": the requirement is not on `T`, the
// differentiable function parameter/result type.
struct ContextualLayoutRequirement<T: Differentiable, U: AnyObject> {
var stored: T
}
extension ContextualLayoutRequirement {
func test(_ x: T) {
let _: @differentiable(reverse) (T) -> T = { _ in self.stored }
let _: @differentiable(reverse) (T) -> T = { $0 }
}
}
// The layout requirement directly involves `T`, the differentiable function
// parameter/result type.
// TODO(TF-851): Uncomment the tests below after `@differentiable` function
// SILGen thunking is fixed.
/*
struct LayoutRequirement<T: AnyObject & Differentiable> {
var stored: T
}
extension LayoutRequirement {
func test(_ x: T) {
let _: @differentiable(reverse) (T) -> T = { _ in self.stored }
let _: @differentiable(reverse) (T) -> T = { $0 }
}
}
*/
// Test superclass requirements.
class Super: Differentiable {}
// The superclass requirement is "contextual": the requirement is not on `T`,
// the differentiable function parameter/result type.
struct ContextualSuperclassRequirement<T: Differentiable, U: Super> {
var stored: T
}
extension ContextualSuperclassRequirement {
func test(_ x: T) {
let _: @differentiable(reverse) (T) -> T = { _ in self.stored }
let _: @differentiable(reverse) (T) -> T = { $0 }
}
}
// The superclass requirement directly involves `T`, the differentiable
// function parameter/result type.
// TODO(TF-851): Uncomment the tests below after `@differentiable` function
// SILGen thunking is fixed.
/*
struct SuperclassRequirement<T: Super & Differentiable> {
var stored: T
}
extension SuperclassRequirement {
func test(_ x: T) {
let _: @differentiable(reverse) (T) -> T = { _ in self.stored }
let _: @differentiable(reverse) (T) -> T = { $0 }
}
}
*/