-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathautodiff_diagnostics.swift
269 lines (226 loc) · 9.05 KB
/
autodiff_diagnostics.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
// RUN: %target-swift-frontend -emit-sil -verify %s
//===----------------------------------------------------------------------===//
// Top-level (before VJP/adjoint synthesis)
//===----------------------------------------------------------------------===//
// expected-note @+1 {{opaque non-'@differentiable' function is not differentiable}}
func foo(_ f: (Float) -> Float) -> Float {
// expected-error @+1 {{function is not differentiable}}
return gradient(at: 0, in: f)
}
//===----------------------------------------------------------------------===//
// Basic function
//===----------------------------------------------------------------------===//
func one_to_one_0(_ x: Float) -> Float {
return x + 2
}
_ = gradient(at: 0, in: one_to_one_0) // okay!
//===----------------------------------------------------------------------===//
// Non-differentiable stored properties
//===----------------------------------------------------------------------===//
struct S {
var p: Float
}
extension S : Differentiable, VectorProtocol {
// Test custom `TangentVector` type with non-matching stored property name.
struct TangentVector: Differentiable, VectorProtocol {
var dp: Float
}
typealias AllDifferentiableVariables = S
static var zero: S { return S(p: 0) }
typealias Scalar = Float
static func + (lhs: S, rhs: S) -> S { return S(p: lhs.p + rhs.p) }
static func - (lhs: S, rhs: S) -> S { return S(p: lhs.p - rhs.p) }
static func * (lhs: Float, rhs: S) -> S { return S(p: lhs * rhs.p) }
mutating func move(along direction: TangentVector) {
p.move(along: direction.dp)
}
}
// expected-error @+2 {{function is not differentiable}}
// expected-note @+1 {{property cannot be differentiated because 'S.TangentVector' does not have a member named 'p'}}
_ = gradient(at: S(p: 0)) { s in 2 * s.p }
struct NoDerivativeProperty : Differentiable {
var x: Float
@noDerivative var y: Float
}
// expected-error @+1 {{function is not differentiable}}
_ = gradient(at: NoDerivativeProperty(x: 1, y: 1)) { s -> Float in
var tmp = s
// expected-note @+1 {{cannot differentiate through a '@noDerivative' stored property; do you want to add '.withoutDerivative()'?}}
tmp.y = tmp.x
return tmp.x
}
_ = gradient(at: NoDerivativeProperty(x: 1, y: 1)) { s in
// expected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to add '.withoutDerivative()'?}} {{13-13=.withoutDerivative()}}
return s.y
}
_ = gradient(at: NoDerivativeProperty(x: 1, y: 1)) {
// expected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to add '.withoutDerivative()'?}} {{7-7=.withoutDerivative()}}
$0.y
}
//===----------------------------------------------------------------------===//
// Function composition
//===----------------------------------------------------------------------===//
func uses_optionals(_ x: Float) -> Float {
var maybe: Float? = 10
maybe = x
// expected-note @+1 {{differentiating control flow is not yet supported}}
return maybe!
}
// expected-error @+1 {{function is not differentiable}}
_ = gradient(at: 0, in: uses_optionals)
func base(_ x: Float) -> Float {
// expected-error @+2 {{expression is not differentiable}}
// expected-note @+1 {{cannot differentiate through a non-differentiable result; do you want to add '.withoutDerivative()'?}}
return Float(Int(x))
}
// TODO: Fix nested differentiation diagnostics. Need to fix indirect differentiation invokers.
func nested(_ x: Float) -> Float {
// xpected-note @+1 {{when differentiating this function call}}
return base(x)
}
func middle(_ x: Float) -> Float {
// xpected-note @+1 {{when differentiating this function call}}
return nested(x)
}
func middle2(_ x: Float) -> Float {
// xpected-note @+1 {{when differentiating this function call}}
return middle(x)
}
func func_to_diff(_ x: Float) -> Float {
// xpected-note @+1 {{expression is not differentiable}}
return middle2(x)
}
func calls_grad_of_nested(_ x: Float) -> Float {
// xpected-error @+1 {{function is not differentiable}}
return gradient(at: x, in: func_to_diff)
}
//===----------------------------------------------------------------------===//
// Control flow
//===----------------------------------------------------------------------===//
func if_else(_ x: Float, _ flag: Bool) -> Float {
let y: Float
if flag {
y = x + 1
} else {
y = x
}
return y
}
_ = gradient(at: 0) { x in if_else(x, true) }
//===----------------------------------------------------------------------===//
// @differentiable attributes
//===----------------------------------------------------------------------===//
var a: Float = 3.0
protocol P {
@differentiable(wrt: x)
func foo(x: Float) -> Float
}
enum T : P {
// expected-note @+2 {{when differentiating this function definition}}
// expected-error @+1 {{function is not differentiable}}
@differentiable(wrt: x) func foo(x: Float) -> Float {
// expected-note @+1 {{cannot differentiate writes to global variables}}
a = a + x
return a
}
}
// expected-note @+2 {{when differentiating this function definition}}
// expected-error @+1 {{function is not differentiable}}
@differentiable(wrt: x) func foo(x: Float) -> Float {
// expected-note @+1 {{cannot differentiate writes to global variables}}
a = a + x
return a
}
// Test `@differentiable` on initializer with assignments.
struct TF_305 : Differentiable {
var filter: Float
var bias: Float
typealias Activation = @differentiable (Float) -> Float
@noDerivative let activation: Activation
@noDerivative let strides: (Int, Int)
@differentiable
public init(
filter: Float,
bias: Float,
activation: @escaping Activation,
strides: (Int, Int)
) {
self.filter = filter
self.bias = bias
self.activation = activation
self.strides = strides
}
}
//===----------------------------------------------------------------------===//
// Classes and existentials (not yet supported)
//===----------------------------------------------------------------------===//
class Foo {
// @differentiable cannot be put here. It's rejected by Sema already.
func class_method(_ x: Float) -> Float {
return x
}
}
// Nested call case.
@differentiable
func triesToDifferentiateClassMethod(x: Float) -> Float {
// expected-error @+2 {{expression is not differentiable}}
// expected-note @+1 {{differentiating class members is not yet supported}}
return Foo().class_method(x)
}
// expected-error @+2 {{function is not differentiable}}
// expected-note @+1 {{opaque non-'@differentiable' function is not differentiable}}
_ = gradient(at: .zero, in: Foo().class_method)
//===----------------------------------------------------------------------===//
// Unreachable
//===----------------------------------------------------------------------===//
// expected-error @+1 {{function is not differentiable}}
let no_return: @differentiable (Float) -> Float = { x in
let _ = x + 1
// expected-error @+2 {{missing return in a closure expected to return 'Float'}}
// expected-note @+1 {{missing return for differentiation}}
}
// expected-error @+1 {{function is not differentiable}}
@differentiable
// expected-note @+1 {{when differentiating this function definition}}
func roundingGivesError(x: Float) -> Float {
// expected-note @+1 {{cannot differentiate through a non-differentiable result; do you want to add '.withoutDerivative()'?}}
return Float(Int(x))
}
//===----------------------------------------------------------------------===//
// Inout arguments
//===----------------------------------------------------------------------===//
func activeInoutArg(_ x: Float) -> Float {
var a = x
// expected-note @+1 {{cannot differentiate through 'inout' arguments}}
a += x
return a
}
// expected-error @+1 {{function is not differentiable}}
_ = pullback(at: .zero, in: activeInoutArg(_:))
func activeInoutArgTuple(_ x: Float) -> Float {
var tuple = (x, x)
// expected-note @+1 {{cannot differentiate through 'inout' arguments}}
tuple.0 *= x
return x * tuple.0
}
// expected-error @+1 {{function is not differentiable}}
_ = pullback(at: .zero, in: activeInoutArgTuple(_:))
//===----------------------------------------------------------------------===//
// Non-varied results
//===----------------------------------------------------------------------===//
func one() -> Float {
return 1
}
@differentiable
func nonVariedResult(_ x: Float) -> Float {
// expected-warning @+1 {{result does not depend on differentiation arguments and will always have a zero derivative; do you want to add '.withoutDerivative()'?}} {{15-15=.withoutDerivative()}}
return one()
}
//===----------------------------------------------------------------------===//
// Subset parameters
//===----------------------------------------------------------------------===//
func nondiff(_ f: @differentiable (Float, @nondiff Float) -> Float) -> Float {
// expected-note @+2 {{cannot differentiate with respect to a '@nondiff' parameter}}
// expected-error @+1 {{function is not differentiable}}
return gradient(at: 2) { x in f(x * x, x) }
}