forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
differentiable_attr_type_checking.swift
439 lines (370 loc) · 13.6 KB
/
differentiable_attr_type_checking.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
// RUN: %target-swift-frontend -typecheck -verify %s
@differentiable(reverse, adjoint: dfoo) // expected-error {{@differentiable may only be used on 'func' declarations}}
let x: Float = 1
@differentiable(reverse, adjoint: dfoo) // expected-error {{@differentiable may only be used on 'func' declarations}}
protocol P {}
func dfoo(_ x: Float, primal: Float, seed: Float) -> Float {
return 2 * x
}
@differentiable(reverse, adjoint: dfoo(_:primal:seed:)) // ok!
func foo(_ x: Float) -> Float {
return x * x
}
@differentiable(reverse) // ok!
func no_prim_or_adj(_ x: Float) -> Float {
return x * x
}
@differentiable(reverse, primal: dfoo) // expected-error {{a corresponding adjoint must be specified when the primal is provided}}
func prim_but_no_adj(_ x: Float) -> Float {
return x * x
}
// Original function must return non-Void type.
@differentiable(reverse, adjoint: dvoid) // expected-error {{cannot differentiate void function 'void'}}
func void(_ a: Float) {}
func dvoid(_ a: Float, _ x: (), _ y: ()) -> Float { return 1 }
// Primal returns custom checkpoints type.
struct CheckpointsFoo {}
func pfoo(_ x: Float) -> (checkpoints: CheckpointsFoo, originalValue: Float) {
return (CheckpointsFoo(), x * x)
}
func dfoo_checkpointed(_ x: Float, checkpoints: CheckpointsFoo, originalValue: Float, seed: Float) -> Float {
return 2 * x
}
@differentiable(reverse, primal: pfoo(_:), adjoint: dfoo_checkpointed(_:checkpoints:originalValue:seed:)) // ok!
func foo_checkpointed(_ x: Float) -> Float {
return x * x
}
func dbar(_ x: Float, _ y: Float, primal: Float, seed: Float) -> (Float, Float) {
return (1, 1)
}
@differentiable(reverse, adjoint: dbar(_:_:primal:seed:)) // ok!
func bar(_ x: Float, _ y: Float) -> Float {
return x + y
}
@differentiable(forward, adjoint: dbar(_:_:primal:seed:)) // expected-error {{forward-mode automatic differentiation is not supported yet}}
func bar_fwd(_ x: Float, _ y: Float) -> Float {
return x + y
}
func dfoo2_wrong_type(_ x: Float, primal: Float, seed: Double) -> Float {
return 2 * x
}
@differentiable(reverse, adjoint: dfoo2_wrong_type(_:primal:seed:)) // expected-error {{'dfoo2_wrong_type(_:primal:seed:)' does not have expected type '(Float, Float, Float) -> Float'}}
func foo2(_ x: Float) -> Float {
return x * x
}
@differentiable(reverse, adjoint: dfoo(_:primal:_:)) // expected-error {{use of unresolved identifier 'dfoo(_:primal:_:)'}}
func foo3(_ x: Float) -> Float {
return x * x
}
@differentiable(reverse, adjoint: meow) // expected-error {{use of unresolved identifier}}
func foo4(_ x: Float) -> Float {
return x * x
}
@differentiable(reverse, adjoint: woof) // expected-error {{'foo5' has no parameters to differentiate with respect to}}
func foo5() -> Float {
return 1
}
@differentiable(reverse, wrt: (self, .0, .1), adjoint: dmeow1(_:_:_:_:)) // expected-error {{'self' parameter is only applicable to instance methods}}
func meow1(_ x: Float, _: Float) -> Float {
return 1 + x
}
func dmeow1_out_of_S(_ s: S, _ x: Float, _ primal: Float, _ seed: Float) -> (S, Float) {
return (s, x)
}
func dPlus(_ x: S, _ y: S, _ primal: S, _ seed: S) -> (S, S) {
return (x, y)
}
func dPlus_curried(_ x: S.Type) -> (S, S, S, S) -> (S, S) {
fatalError("never run")
}
func dPlus(_ x: Int, _ y: S, _ primal: S, _ seed: S) -> (Int, S) {
return (x, y)
}
struct C {
@differentiable(reverse, adjoint: adjoint)
func primal(x: Float) -> Float {
return x
}
func adjoint(x: Float, _: Float, _: Float) -> Float {
return x
}
}
struct S {
@differentiable(reverse, wrt: (self, .0), adjoint: dmeow1_out_of_S(_:_:_:_:)) // expected-error {{'dmeow1_out_of_S' is not defined in the current type context}}
func meow1(_ x: Float) -> Float {
return x + 1
}
@differentiable(reverse, wrt: (.1, self, .2), adjoint: dmeow1(_:_:_:_:)) // expected-error {{'self' parameter must come first in the parameter list}}
func meow1_not_ok(_ x: Float, _: Float, _: Float) -> Float {
return 1 + x
}
@differentiable(reverse, wrt: (self, .0), adjoint: dmeow1_in_S(_:_:_:_:)) // expected-error {{'self' parameter is only applicable to instance methods}}
static func meow1(_ x: Float) -> Float {
return x + 1
}
@differentiable(reverse, wrt: (self, .0, .1), adjoint: dPlus(_:_:_:_:)) // expected-error {{'self' parameter is only applicable to instance methods}}
static func + (lhs: S, rhs: S) -> S {
return lhs
}
@differentiable(reverse, wrt: (.0, .1), adjoint: dPlus(_:_:_:_:)) // expected-error {{'dPlus' is not defined in the current type context}}
static func - (lhs: S, rhs: S) -> S {
return lhs
}
@differentiable(reverse, wrt: (.0, .1), adjoint: dPlus_curried(_:)) // expected-error {{'dPlus_curried' is not defined in the current type context}}
static func try_plus_curried_adjoint(lhs: S, rhs: S) -> S {
return lhs
}
static func dMul(_ lhs: Int, _ rhs: S, _: S, _: S) -> (Int, S) {
return (lhs, rhs)
}
@differentiable(reverse, wrt: (.0, .1), adjoint: dMul) // ok
static func * (lhs: Int, rhs: S) -> S {
return rhs
}
@differentiable(reverse, wrt: (.0, .1), adjoint: dMul) // expected-error {{'dMul' does not have expected type '(S) -> (Int, S, S, S) -> (Int, S)'}}
func instance_mul(lhs: Int, rhs: S) -> S {
return rhs
}
@differentiable(reverse, adjoint: dIdentity_wrt_self) // expected-error {{specify at least one parameter to differentiate with respect to}}
func identity() -> S {
return self
}
func dIdentity_wrt_self(_: S, seed: S) -> S {
return seed
}
@differentiable(reverse, adjoint: dStaticIdentity_wrt_0) // ok
static func staticIdentity(_ s: S) -> S {
return s
}
static func dStaticIdentity_wrt_0(_: S, _: S, seed: S) -> S {
return seed
}
}
@differentiable(reverse, wrt: (.1, .2), adjoint: dmeow2(_:_:_:_:_:)) // ok
func meow2(_ x: Float, _: Float, _: Float) -> Float {
return 1 + x
}
@differentiable(reverse, wrt: (.2, .1), adjoint: dmeow1(_:_:_:_:)) // expected-error {{parameter indices must be ascending}}
func meow3(_ x: Float, _: Float, _: Float) -> Float {
return 1 + x
}
@differentiable(reverse, wrt: (.2, self, .1), adjoint: dmeow1(_:_:_:_:)) // expected-error {{'self' parameter is only applicable to instance methods}}
func meow4(_ x: Float, _: Float, _: Float) -> Float {
return 1 + x
}
func dmeow1(_ x: Float, _: Float, _: Float, _: Float) -> (Float, Float) {
return (x, x)
}
func dmeow2(_ x: Float, _: Float, _: Float, _: Float, _: Float) -> (Float, Float) {
return (x, x)
}
// Test cross-declaration references.
// NOTE: Cross-declaration references across files is not tested because it is
// difficult to set up and requires creating a new test target. Since the
// TensorFlow library will declare original functions/adjoints in separate
// files, successful compilation of the library itself is a sufficient test.
// Original function in struct definition, adjoint in extension.
struct E1 {
@differentiable(reverse, adjoint: adjoint)
func original(x: Float) -> Float {
return x
}
}
extension E1 {
func adjoint(x: Float, _: Float, _: Float) -> Float {
return x
}
}
// Original function and adjoint in separate struct extensions.
struct E2 {}
extension E2 {
@differentiable(reverse, adjoint: adjoint)
func original(x: Float) -> Float {
return x
}
}
extension E2 {
func adjoint(x: Float, _: Float, _: Float) -> Float {
return x
}
}
// Original function and adjoint in separate struct extensions, with matching
// generic constraints.
struct E3<T> {}
extension E3 where T == Float {
@differentiable(reverse, adjoint: adjoint_same_constraint)
func original(x: Float) -> Float {
return x
}
}
extension E3 where T == Float {
func adjoint_same_constraint(x: Float, _: Float, _: Float) -> Float {
return x
}
}
struct E4<T> {}
extension E4 {
@differentiable(reverse, adjoint: adjoint_no_constraint)
func original(x: Float) -> Float {
return x
}
}
extension E4 {
func adjoint_no_constraint(x: Float, _: Float, _: Float) -> Float {
return x
}
}
// Original function and adjoint in separate struct extensions, with
// non-matching generic constraints.
struct E5<T> {}
extension E5 {
@differentiable(reverse, adjoint: adjoint_diff_constraint)
// expected-error @-1 {{'adjoint_diff_constraint' does not have expected type '<T> (E5<T>) -> (Float, Float, Float) -> Float'}}
func original(x: Float) -> Float {
return x
}
}
extension E5 where T == Float {
func adjoint_diff_constraint(x: Float, _: Float, _: Float) -> Float {
return x
}
}
// Original function and adjoint in separate struct extensions, differentiating
// with respect to self.
struct E6<T> {}
extension E6 {
@differentiable(reverse, wrt: (self), adjoint: adjoint_wrt_self)
func original(x: Float) -> Float {
return x
}
@differentiable(reverse, wrt: (self), primal: primal, adjoint: adjoint_checkpointed)
func original2(x: Float) -> Float {
return x
}
@differentiable(reverse, wrt: (self), primal: primal, adjoint: adjoint_checkpointed_mismatch)
// expected-error @-1 {{'adjoint_checkpointed_mismatch' does not have expected type '<T> (E6<T>) -> (Float, E6<T>.Checkpoints, Float, Float) -> E6<T>'}}
func original3(x: Float) -> Float {
return x
}
}
extension E6 {
struct Checkpoints {
let e6: E6
}
func primal(x: Float) -> (Checkpoints, Float) {
return (Checkpoints(e6: self), x)
}
func adjoint_checkpointed(x: Float, _: Checkpoints, _: Float, _: Float) -> E6 {
return self
}
func adjoint_checkpointed_mismatch(x: Float, _: Float, _: Float) -> E6 {
return self
}
}
extension E6 {
func adjoint_wrt_self(x: Float, _: Float, _: Float) -> E6 {
return self
}
}
// Generic functions with no constraints.
func dbaz1<T>(_ x: T, _ y: T, primal: T, seed: T) -> (T, T) {
return (y, x)
}
@differentiable(reverse, adjoint: dbaz1(_:_:primal:seed:)) // ok!
func baz1<T>(_ x: T, _ y: T) -> T {
return x
}
func pbaz1<T>(_ x: T, _ y: T) -> ((T, T), T) {
return ((y, y), x)
}
func dbaz1_checkpointed<T>(_ x: T, _ y: T, primal: (T, T), originalValue: T, seed: T) -> (T, T) {
return (y, x)
}
@differentiable(reverse, primal: pbaz1(_:_:), adjoint: dbaz1_checkpointed(_:_:primal:originalValue:seed:)) // ok!
func baz1_checkpointed<T>(_ x: T, _ y: T) -> T {
return x
}
// Generic functions with matching constraints.
func dbaz2<T : FloatingPoint>(_ x: T, _ y: T, primal: T, seed: T) -> (T, T) {
return (1, 1)
}
@differentiable(reverse, adjoint: dbaz2(_:_:primal:seed:)) // ok!
func baz2<T : FloatingPoint>(_ x: T, _ y: T) -> T {
return x + y
}
struct CheckpointsFP<T : FloatingPoint> {
let meow: T
}
func pbaz2<T : FloatingPoint>(_ x: T, _ y: T) -> (CheckpointsFP<T>, T) {
return (CheckpointsFP(meow: 1), x + y)
}
func dbaz2_checkpointed<T : FloatingPoint>(_ x: T, _ y: T, primal: CheckpointsFP<T>, originalValue: T, seed: T) -> (T, T) {
return (1, 1)
}
@differentiable(reverse, primal: pbaz2(_:_:), adjoint: dbaz2_checkpointed(_:_:primal:originalValue:seed:)) // ok!
func baz2_checkpointed<T : FloatingPoint>(_ x: T, _ y: T) -> T {
return x
}
// Generic functions with different constraints.
func dbaz3<T : Numeric>(_ x: T, _ y: T, primal: T, seed: T) -> (T, T) {
return (1, 1)
}
@differentiable(reverse, adjoint: dbaz3(_:_:primal:seed:))
// expected-error @-1 {{'dbaz3(_:_:primal:seed:)' does not have expected type '<T where T : FloatingPoint> (T, T, T, T) -> (T, T)'}}
func baz3<T : FloatingPoint>(_ x: T, _ y: T) -> T {
return x + y
}
struct CheckpointsNumeric<T : Numeric> {
let meow: T
}
func pbaz3<T : Numeric>(_ x: T, _ y: T) -> (CheckpointsNumeric<T>, T) {
return (CheckpointsNumeric(meow: 1), x + y)
}
func dbaz3_checkpointed<T : Numeric>(_ x: T, _ y: T, primal: CheckpointsNumeric<T>, originalValue: T, seed: T) -> (T, T) {
return (1, 1)
}
@differentiable(reverse, primal: pbaz3(_:_:), adjoint: dbaz3_checkpointed(_:_:primal:originalValue:seed:))
// expected-error @-1 {{'pbaz3' does not have expected parameters' type '(T, T)'}}
func baz3_checkpointed<T : FloatingPoint>(_ x: T, _ y: T) -> T {
return x
}
// Adjoints trying to define derivatives with respect to classes and existentials.
class ParamClass {}
protocol ParamProtocol {}
struct ParamProtocolStruct : ParamProtocol {}
@differentiable(reverse, adjoint: dClassParameter)
// expected-error @-1 {{class objects and protocol existentials ('ParamClass') cannot be differentiated with respect to}}
func classParameter1(_: ParamClass) -> Float {
return 1
}
@differentiable(reverse, wrt: (.0), adjoint: dClassParameter)
// expected-error @-1 {{class objects and protocol existentials ('ParamClass') cannot be differentiated with respect to}}
func classParameter2(_: ParamClass) -> Float {
return 1
}
func dClassParameter(_: ParamClass, _: Float, seed: Float) -> ParamClass {
return ParamClass()
}
@differentiable(reverse, adjoint: dProtocolParameter)
// expected-error @-1 {{class objects and protocol existentials ('ParamProtocol') cannot be differentiated with respect to}}
func protocolParameter1(_: ParamProtocol) -> Float {
return 1
}
@differentiable(reverse, wrt: (.0), adjoint: dProtocolParameter)
// expected-error @-1 {{class objects and protocol existentials ('ParamProtocol') cannot be differentiated with respect to}}
func protocolParameter2(_: ParamProtocol) -> Float {
return 1
}
func dProtocolParameter(_: ParamProtocol, _: Float, seed: Float) -> ParamProtocol {
return ParamProtocolStruct()
}
class ClassWithDifferentiableMethods {
@differentiable(reverse, wrt: (self), adjoint: dMethod)
// expected-error @-1 {{class objects and protocol existentials ('ClassWithDifferentiableMethods') cannot be differentiated with respect to}}
func method() -> Float {
return 1
}
func dMethod(_: Float, _: Float) -> ClassWithDifferentiableMethods {
return ClassWithDifferentiableMethods()
}
}