-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathderivative_attr.swift
143 lines (117 loc) · 3.78 KB
/
derivative_attr.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
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -emit-module -parse-as-library -o %t
// RUN: llvm-bcanalyzer %t/derivative_attr.swiftmodule | %FileCheck %s -check-prefix=BCANALYZER
// RUN: %target-sil-opt -enable-sil-verify-all %t/derivative_attr.swiftmodule -o - | %FileCheck %s
// BCANALYZER-NOT: UnknownCode
import _Differentiation
// Dummy `Differentiable`-conforming type.
struct S: Differentiable & AdditiveArithmetic {
static var zero: S { S() }
static func + (_: S, _: S) -> S { S() }
static func - (_: S, _: S) -> S { S() }
typealias TangentVector = S
}
// Test top-level functions.
func top1(_ x: S) -> S {
x
}
// CHECK: @derivative(of: top1, wrt: x)
@derivative(of: top1, wrt: x)
func derivativeTop1(_ x: S) -> (value: S, differential: (S) -> S) {
(x, { $0 })
}
func top2<T, U>(_ x: T, _ i: Int, _ y: U) -> U {
y
}
// CHECK: @derivative(of: top2, wrt: (x, y))
@derivative(of: top2, wrt: (x, y))
func derivativeTop2<T: Differentiable, U: Differentiable>(
_ x: T, _ i: Int, _ y: U
) -> (value: U, differential: (T.TangentVector, U.TangentVector) -> U.TangentVector) {
(y, { (dx, dy) in dy })
}
// Test instance methods.
extension S {
func instanceMethod(_ x: S) -> S {
self
}
// CHECK: @derivative(of: instanceMethod, wrt: x)
@derivative(of: instanceMethod, wrt: x)
func derivativeInstanceMethodWrtX(_ x: S) -> (value: S, differential: (S) -> S) {
(self, { _ in .zero })
}
// CHECK: @derivative(of: instanceMethod, wrt: self)
@derivative(of: instanceMethod, wrt: self)
func derivativeInstanceMethodWrtSelf(_ x: S) -> (value: S, differential: (S) -> S) {
(self, { $0 })
}
// Note: qualified name base types are not yet serialized and are not printed
// when round-tripping.
// CHECK: @derivative(of: instanceMethod, wrt: (self, x))
@derivative(of: S.instanceMethod, wrt: (self, x))
func derivativeInstanceMethodWrtAll(_ x: S) -> (value: S, differential: (S, S) -> S) {
(self, { (dself, dx) in self })
}
}
// Test static methods.
extension S {
static func staticMethod(_ x: S) -> S {
x
}
// CHECK: @derivative(of: staticMethod, wrt: x)
@derivative(of: staticMethod, wrt: x)
static func derivativeStaticMethod(_ x: S) -> (value: S, differential: (S) -> S) {
(x, { $0 })
}
}
// Test computed properties.
extension S {
var computedProperty: S {
get { self }
set {}
}
// CHECK: @derivative(of: computedProperty, wrt: self)
@derivative(of: computedProperty, wrt: self)
func derivativeProperty() -> (value: S, differential: (S) -> S) {
(self, { $0 })
}
// CHECK: @derivative(of: computedProperty.get, wrt: self)
@derivative(of: computedProperty.get, wrt: self)
func derivativePropertyGetter() -> (value: S, pullback: (S) -> S) {
fatalError()
}
// CHECK: @derivative(of: computedProperty.set, wrt: (self, newValue))
@derivative(of: computedProperty.set, wrt: (self, newValue))
mutating func derivativePropertySetter(_ newValue: S) -> (
value: (), pullback: (inout S) -> S
) {
fatalError()
}
}
// Test subscripts.
extension S {
subscript() -> S {
get { self }
set {}
}
subscript<T: Differentiable>(x: T) -> S {
self
}
// CHECK: @derivative(of: subscript, wrt: self)
@derivative(of: subscript(_:), wrt: self)
func derivativeSubscript<T: Differentiable>(x: T) -> (value: S, differential: (S) -> S) {
(self, { $0 })
}
// CHECK: @derivative(of: subscript.get, wrt: self)
@derivative(of: subscript.get, wrt: self)
func derivativeSubscriptGetter() -> (value: S, pullback: (S) -> S) {
fatalError()
}
// CHECK: @derivative(of: subscript.set, wrt: (self, newValue))
@derivative(of: subscript.set, wrt: (self, newValue))
mutating func derivativeSubscriptSetter(_ newValue: S) -> (
value: (), pullback: (inout S) -> S
) {
fatalError()
}
}