forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_differentiable.swift
577 lines (492 loc) · 17.2 KB
/
class_differentiable.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// RUN: %target-swift-frontend -typecheck -verify -primary-file %s %S/Inputs/class_differentiable_other_module.swift
import _Differentiation
// Verify that a type `T` conforms to `AdditiveArithmetic`.
func assertConformsToAdditiveArithmetic<T>(_: T.Type)
where T: AdditiveArithmetic {}
// Dummy protocol with default implementations for `AdditiveArithmetic` requirements.
// Used to test `Self : AdditiveArithmetic` requirements.
protocol DummyAdditiveArithmetic: AdditiveArithmetic {}
extension DummyAdditiveArithmetic {
static func == (lhs: Self, rhs: Self) -> Bool {
fatalError()
}
static var zero: Self {
fatalError()
}
static func + (lhs: Self, rhs: Self) -> Self {
fatalError()
}
static func - (lhs: Self, rhs: Self) -> Self {
fatalError()
}
}
class Empty: Differentiable {}
func testEmpty() {
assertConformsToAdditiveArithmetic(Empty.TangentVector.self)
}
// Test structs with `let` stored properties.
// Derived conformances fail because `mutating func move` requires all stored
// properties to be mutable.
class ImmutableStoredProperties: Differentiable {
var okay: Float
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Int' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
let nondiff: Int
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'ImmutableStoredProperties' requires all stored properties not marked with `@noDerivative` to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
let diff: Float
init() {
okay = 0
nondiff = 0
diff = 0
}
}
func testImmutableStoredProperties() {
_ = ImmutableStoredProperties.TangentVector(okay: 1)
}
class MutableStoredPropertiesWithInitialValue: Differentiable {
var x = Float(1)
var y = Double(1)
}
// Test class with both an empty constructor and memberwise initializer.
class AllMixedStoredPropertiesHaveInitialValue: Differentiable {
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'AllMixedStoredPropertiesHaveInitialValue' requires all stored properties not marked with `@noDerivative` to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
let x = Float(1)
var y = Float(1)
// Memberwise initializer should be `init(y:)` since `x` is immutable.
static func testMemberwiseInitializer() {
_ = AllMixedStoredPropertiesHaveInitialValue()
}
}
/*
class HasCustomConstructor: Differentiable {
var x = Float(1)
var y = Float(1)
// Custom constructor should not affect synthesis.
init(x: Float, y: Float, z: Bool) {}
}
*/
class Simple: Differentiable {
var w: Float
var b: Float
init(w: Float, b: Float) {
self.w = w
self.b = b
}
}
func testSimple() {
let simple = Simple(w: 1, b: 1)
let tangent = Simple.TangentVector(w: 1, b: 1)
simple.move(along: tangent)
}
// Test type with mixed members.
class Mixed: Differentiable {
var simple: Simple
var float: Float
init(simple: Simple, float: Float) {
self.simple = simple
self.float = float
}
}
func testMixed(_ simple: Simple, _ simpleTangent: Simple.TangentVector) {
let mixed = Mixed(simple: simple, float: 1)
let tangent = Mixed.TangentVector(simple: simpleTangent, float: 1)
mixed.move(along: tangent)
}
// Test type with manual definition of vector space types to `Self`.
final class VectorSpacesEqualSelf: Differentiable & DummyAdditiveArithmetic {
var w: Float
var b: Float
typealias TangentVector = VectorSpacesEqualSelf
init(w: Float, b: Float) {
self.w = w
self.b = b
}
}
/*
extension VectorSpacesEqualSelf : Equatable, AdditiveArithmetic {
static func == (lhs: VectorSpacesEqualSelf, rhs: VectorSpacesEqualSelf) -> Bool {
fatalError()
}
static var zero: VectorSpacesEqualSelf {
fatalError()
}
static func + (lhs: VectorSpacesEqualSelf, rhs: VectorSpacesEqualSelf) -> VectorSpacesEqualSelf {
fatalError()
}
static func - (lhs: VectorSpacesEqualSelf, rhs: VectorSpacesEqualSelf) -> VectorSpacesEqualSelf {
fatalError()
}
}
*/
// Test generic type with vector space types to `Self`.
class GenericVectorSpacesEqualSelf<T>: Differentiable
where T: Differentiable, T == T.TangentVector {
var w: T
var b: T
init(w: T, b: T) {
self.w = w
self.b = b
}
}
func testGenericVectorSpacesEqualSelf() {
let genericSame = GenericVectorSpacesEqualSelf<Double>(w: 1, b: 1)
let tangent = GenericVectorSpacesEqualSelf.TangentVector(w: 1, b: 1)
genericSame.move(along: tangent)
}
// Test nested type.
class Nested: Differentiable {
var simple: Simple
var mixed: Mixed
var generic: GenericVectorSpacesEqualSelf<Double>
init(
simple: Simple, mixed: Mixed, generic: GenericVectorSpacesEqualSelf<Double>
) {
self.simple = simple
self.mixed = mixed
self.generic = generic
}
}
func testNested(
_ simple: Simple, _ mixed: Mixed,
_ genericSame: GenericVectorSpacesEqualSelf<Double>
) {
_ = Nested(simple: simple, mixed: mixed, generic: genericSame)
}
// Test type that does not conform to `AdditiveArithmetic` but whose members do.
// Thus, `Self` cannot be used as `TangentVector` or `TangentVector`.
// Vector space structs types must be synthesized.
// Note: it would be nice to emit a warning if conforming `Self` to
// `AdditiveArithmetic` is possible.
class AllMembersAdditiveArithmetic: Differentiable {
var w: Float
var b: Float
init(w: Float, b: Float) {
self.w = w
self.b = b
}
}
// Test type whose properties are not all differentiable.
class DifferentiableSubset: Differentiable {
var w: Float
var b: Float
@noDerivative var flag: Bool
@noDerivative let technicallyDifferentiable: Float = .pi
init(w: Float, b: Float, flag: Bool) {
self.w = w
self.b = b
self.flag = flag
}
}
func testDifferentiableSubset() {
_ = DifferentiableSubset.TangentVector(w: 1, b: 1)
_ = DifferentiableSubset.TangentVector(w: 1, b: 1)
}
// Test nested type whose properties are not all differentiable.
class NestedDifferentiableSubset: Differentiable {
var x: DifferentiableSubset
var mixed: Mixed
@noDerivative var technicallyDifferentiable: Float
init(x: DifferentiableSubset, mixed: Mixed) {
self.x = x
self.mixed = mixed
technicallyDifferentiable = 0
}
}
// Test type that uses synthesized vector space types but provides custom
// method.
class HasCustomMethod: Differentiable {
var simple: Simple
var mixed: Mixed
var generic: GenericVectorSpacesEqualSelf<Double>
init(
simple: Simple, mixed: Mixed, generic: GenericVectorSpacesEqualSelf<Double>
) {
self.simple = simple
self.mixed = mixed
self.generic = generic
}
func move(along direction: TangentVector) {
print("Hello world")
simple.move(along: direction.simple)
mixed.move(along: direction.mixed)
generic.move(along: direction.generic)
}
}
// Test type with user-defined memberwise initializer.
class TF_25: Differentiable {
public var bar: Float
public init(bar: Float) {
self.bar = bar
}
}
// Test user-defined memberwise initializer.
class TF_25_Generic<T: Differentiable>: Differentiable {
public var bar: T
public init(bar: T) {
self.bar = bar
}
}
// Test initializer that is not a memberwise initializer because of stored property name vs parameter label mismatch.
class HasCustomNonMemberwiseInitializer<T: Differentiable>: Differentiable {
var value: T
init(randomLabel value: T) { self.value = value }
}
// Test type with generic environment.
class HasGenericEnvironment<Scalar: FloatingPoint & Differentiable>: Differentiable
{
var x: Float = 0
}
// Test type with generic members that conform to `Differentiable`.
class GenericSynthesizeAllStructs<T: Differentiable>: Differentiable {
var w: T
var b: T
init(w: T, b: T) {
self.w = w
self.b = b
}
}
// Test type in generic context.
class A<T: Differentiable> {
class B<U: Differentiable, V>: Differentiable {
class InGenericContext: Differentiable {
@noDerivative var a: A
var b: B
var t: T
var u: U
init(a: A, b: B, t: T, u: U) {
self.a = a
self.b = b
self.t = t
self.u = u
}
}
}
}
// Test extension.
class Extended {
var x: Float
init(x: Float) {
self.x = x
}
}
extension Extended: Differentiable {}
// Test extension of generic type.
class GenericExtended<T> {
var x: T
init(x: T) {
self.x = x
}
}
extension GenericExtended: Differentiable where T: Differentiable {}
// Test constrained extension of generic type.
class GenericConstrained<T> {
var x: T
init(x: T) {
self.x = x
}
}
extension GenericConstrained: Differentiable
where T: Differentiable {}
final class TF_260<T: Differentiable>: Differentiable & DummyAdditiveArithmetic
{
var x: T.TangentVector
init(x: T.TangentVector) {
self.x = x
}
}
// TF-269: Test crash when differentiation properties have no getter.
// Related to access levels and associated type inference.
// TODO(TF-631): Blocked by class type differentiation support.
// [AD] Unhandled instruction in adjoint emitter: %2 = ref_element_addr %0 : $TF_269, #TF_269.filter // user: %3
// [AD] Diagnosing non-differentiability.
// [AD] For instruction:
// %2 = ref_element_addr %0 : $TF_269, #TF_269.filter // user: %3
/*
public protocol TF_269_Layer: Differentiable & KeyPathIterable
where TangentVector: KeyPathIterable {
associatedtype Input: Differentiable
associatedtype Output: Differentiable
func applied(to input: Input) -> Output
}
public class TF_269 : TF_269_Layer {
public var filter: Float
public typealias Activation = @differentiable (Output) -> Output
@noDerivative public let activation: @differentiable (Output) -> Output
init(filter: Float, activation: @escaping Activation) {
self.filter = filter
self.activation = activation
}
// NOTE: `KeyPathIterable` derived conformances do not yet support class
// types.
public var allKeyPaths: [PartialKeyPath<TF_269>] {
[]
}
public func applied(to input: Float) -> Float {
return input
}
}
*/
// Test errors.
// expected-error @+1 {{class 'MissingInitializer' has no initializers}}
class MissingInitializer: Differentiable {
// expected-note @+1 {{stored property 'w' without initial value prevents synthesized initializers}}
var w: Float
// expected-note @+1 {{stored property 'b' without initial value prevents synthesized initializers}}
var b: Float
}
// Test manually customizing vector space types.
// These should fail. Synthesis is semantically unsupported if vector space
// types are customized.
final class TangentVectorWB: DummyAdditiveArithmetic, Differentiable {
var w: Float
var b: Float
init(w: Float, b: Float) {
self.w = w
self.b = b
}
}
// expected-error @+3 {{'Differentiable' requires the types 'VectorSpaceTypeAlias.TangentVector' (aka 'TangentVectorWB') and 'TangentVectorWB.TangentVector' be equivalent}}
// expected-note @+2 {{requirement specified as 'Self.TangentVector' == 'Self.TangentVector.TangentVector' [with Self = VectorSpaceTypeAlias]}}
// expected-error @+1 {{type 'VectorSpaceTypeAlias' does not conform to protocol 'Differentiable'}}
final class VectorSpaceTypeAlias: DummyAdditiveArithmetic, Differentiable {
var w: Float
var b: Float
typealias TangentVector = TangentVectorWB
init(w: Float, b: Float) {
self.w = w
self.b = b
}
}
// expected-error @+1 {{type 'VectorSpaceCustomStruct' does not conform to protocol 'Differentiable'}}
final class VectorSpaceCustomStruct: DummyAdditiveArithmetic, Differentiable {
var w: Float
var b: Float
struct TangentVector: AdditiveArithmetic, Differentiable {
var w: Float.TangentVector
var b: Float.TangentVector
typealias TangentVector = VectorSpaceCustomStruct.TangentVector
}
init(w: Float, b: Float) {
self.w = w
self.b = b
}
}
class StaticNoDerivative: Differentiable {
@noDerivative static var s: Bool = true
}
final class StaticMembersShouldNotAffectAnything: DummyAdditiveArithmetic,
Differentiable
{
static var x: Bool = true
static var y: Bool = false
}
class ImplicitNoDerivative: Differentiable {
var a: Float = 0
var b: Bool = true // expected-warning {{stored property 'b' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
}
class ImplicitNoDerivativeWithSeparateTangent: Differentiable {
var x: DifferentiableSubset
var b: Bool = true // expected-warning {{stored property 'b' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
init(x: DifferentiableSubset) {
self.x = x
}
}
// TF-1018: verify that `@noDerivative` warnings are always silenceable, even
// when the `Differentiable` conformance context is not the nominal type
// declaration.
class ExtensionDifferentiableNoDerivative<T> {
// expected-warning @+2 {{stored property 'x' has no derivative because 'T' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
// expected-warning @+1 {{stored property 'y' has no derivative because 'T' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
var x, y: T
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
var nondiff: Bool
init() { fatalError() }
}
extension ExtensionDifferentiableNoDerivative: Differentiable {}
class ExtensionDifferentiableNoDerivativeFixed<T> {
@noDerivative var x, y: T
@noDerivative var nondiff: Bool
init() { fatalError() }
}
extension ExtensionDifferentiableNoDerivativeFixed: Differentiable {}
class ConditionalDifferentiableNoDerivative<T> {
var x, y: T
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
var nondiff: Bool
init() { fatalError() }
}
extension ConditionalDifferentiableNoDerivative: Differentiable
where T: Differentiable {}
class ConditionalDifferentiableNoDerivativeFixed<T> {
var x, y: T
@noDerivative var nondiff: Bool
init() { fatalError() }
}
extension ConditionalDifferentiableNoDerivativeFixed: Differentiable
where T: Differentiable {}
// TF-265: Test invalid initializer (that uses a non-existent type).
class InvalidInitializer: Differentiable {
init(filterShape: (Int, Int, Int, Int), blah: NonExistentType) {} // expected-error {{cannot find type 'NonExistentType' in scope}}
}
// Test memberwise initializer synthesis.
final class NoMemberwiseInitializerExtended<T> {
var value: T
init(_ value: T) {
self.value = value
}
}
extension NoMemberwiseInitializerExtended: Equatable, AdditiveArithmetic,
DummyAdditiveArithmetic
where T: AdditiveArithmetic {}
extension NoMemberwiseInitializerExtended: Differentiable
where T: Differentiable & AdditiveArithmetic {}
// SR-12793: Test interaction with `@differentiable` and `@derivative` type-checking.
class SR_12793: Differentiable {
@differentiable
var x: Float = 0
@differentiable
func method() -> Float { x }
@derivative(of: method)
func vjpMethod() -> (value: Float, pullback: (Float) -> TangentVector) { fatalError() }
// Test usage of synthesized `TangentVector` type.
// This should not produce an error: "reference to invalid associated type 'TangentVector'".
func move(along direction: TangentVector) {}
}
// Test property wrappers.
@propertyWrapper
struct ImmutableWrapper<Value> {
private var value: Value
var wrappedValue: Value { value }
init(wrappedValue: Value) {
self.value = wrappedValue
}
}
@propertyWrapper
struct Wrapper<Value> {
var wrappedValue: Value
}
@propertyWrapper
class ClassWrapper<Value> {
var wrappedValue: Value
init(wrappedValue: Value) { self.wrappedValue = wrappedValue }
}
struct Generic<T> {}
extension Generic: Differentiable where T: Differentiable {}
class WrappedProperties: Differentiable {
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'WrappedProperties' requires 'wrappedValue' in property wrapper 'ImmutableWrapper' to be mutable; add an explicit '@noDerivative' attribute}}
@ImmutableWrapper var immutableInt: Generic<Int> = Generic()
// expected-warning @+1 {{stored property 'mutableInt' has no derivative because 'Generic<Int>' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
@Wrapper var mutableInt: Generic<Int> = Generic()
@Wrapper var float: Generic<Float> = Generic()
@ClassWrapper var float2: Generic<Float> = Generic()
// SR-13071: Test `@differentiable` wrapped property.
@differentiable @Wrapper var float3: Generic<Float> = Generic()
@noDerivative @ImmutableWrapper var nondiff: Generic<Int> = Generic()
static func testTangentMemberwiseInitializer() {
_ = TangentVector(float: .init(), float2: .init(), float3: .init())
}
}
// Test derived conformances in disallowed contexts.
// expected-error @+1 2 {{implementation of 'Differentiable' cannot be automatically synthesized in an extension in a different file to the type}}
extension OtherFileNonconforming: Differentiable {}
// expected-error @+1 2 {{implementation of 'Differentiable' cannot be automatically synthesized in an extension in a different file to the type}}
extension GenericOtherFileNonconforming: Differentiable {}