forked from FluxML/Zygote.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.jl
339 lines (270 loc) · 8.61 KB
/
compiler.jl
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
using Zygote, Test
using Zygote: pullback, @adjoint, Context
macro test_inferred(ex)
:(let res = nothing
@test begin
res = @inferred $ex
true
end
res
end) |> esc
end
trace_contains(st, func, file, line) = any(st) do fr
func in (nothing, fr.func) && endswith(String(fr.file), file) &&
fr.line == line
end
bad(x) = x
const bad_def_line = (@__LINE__) + 1
@adjoint bad(x) = x, Δ -> error("bad")
const bad_call_line = (@__LINE__) + 3
function badly(x)
x = x + 1
x = bad(x)
return x
end
y, back = pullback(badly, 2)
@test y == 3
@test_throws Exception back(1)
bt = try back(1) catch e stacktrace(catch_backtrace()) end
@test trace_contains(bt, nothing, "compiler.jl", bad_def_line)
@test trace_contains(bt, :badly, "compiler.jl", bad_call_line)
# Type inference checks
Zygote.refresh()
y, back = @test_inferred pullback(*, 2, 3)
@test_inferred(back(1))
_sincos(x) = sin(cos(x))
y, back = @test_inferred pullback(_sincos, 0.5)
@test_inferred back(1)
f(x) = 3x^2 + 2x + 1
y, back = @test_inferred pullback(f, 5)
@test y == 86
@test_inferred(back(1))
y, back = @test_inferred pullback(Core._apply, +, (1, 2, 3))
@test_inferred back(1)
# TODO fix bcast inference
# bcast(x) = x .* 5
# y, back = @test_inferred pullback(bcast, [1,2,3])
# @test_inferred back([1,1,1])
foo = let a = 4
x -> x*a
end
@test_inferred gradient(f -> f(5), foo)
getx(x) = x.x
y, back = @test_inferred pullback(getx, (x=1,y=2.0))
@test_inferred back(1)
y, back = @test_inferred pullback(x->x[1], (5,:a))
@test_inferred back(1)
y, back = @test_inferred pullback(((a,b),) -> a, (5, 10))
@test_inferred back(1)
# testcase for issue #808
# testing that methods(Base.show) does not throw. Having something more specific would be too fragile
show_err = try
buf = IOBuffer()
Base.show(buf, methods(Base.show))
nothing
catch ex
ex
end
@test show_err === nothing
struct Funky
x
y
end
@testset "issue #851" begin
f = Funky(1, 1);
function Base.getproperty(f::Funky, i::Symbol)
return 2
end
@test getproperty(f, :x) == 2
@test getfield(f, :x) == 1
y, pb = Zygote._pullback(getproperty, f, :x)
@test y == 2
@test pb(1) == (nothing, nothing, nothing)
y, pb = Zygote._pullback((f, x) -> getproperty(f, x), f, :x)
@test y == 2
@test pb(1) == (nothing, nothing, nothing)
y, pb = Zygote._pullback(getfield, f, :x)
@test y == 1
@test pb(1) == (nothing, (x = 1, y = nothing), nothing)
end
@testset "issue #922" begin
# checks whether getproperty gets accumulated correctly
# instead of defining a test function as in the issue, compare the two pullbacks
function two_svds(X::StridedMatrix{<:Union{Real, Complex}})
return svd(X).U * svd(X).V'
end
function one_svd(X::StridedMatrix{<:Union{Real, Complex}})
F = svd(X)
return F.U * F.V'
end
Δoutput = randn(3,2)
X = randn(3,2)
d_two = Zygote.pullback(two_svds, X)[2](Δoutput)
d_one = Zygote.pullback(one_svd, X)[2](Δoutput)
@test d_one == d_two
end
# this test fails if adjoint for literal_getproperty is added
# https://github.com/FluxML/Zygote.jl/issues/922#issuecomment-804128905
@testset "overloaded getproperty" begin
struct MyStruct
a
b
end
Base.getproperty(ms::MyStruct, s::Symbol) = s === :c ? ms.a + ms.b : getfield(ms, s)
sumall(ms::MyStruct) = ms.a + ms.b + ms.c
ms = MyStruct(1, 2)
@test Zygote.gradient(sumall, ms) == ((a = 2, b = 2),)
end
using ChainRulesCore
function _Gaussian(suffix::Symbol)
name = gensym(Symbol(:Gaussian_, suffix))
return @eval begin
struct $name{Tm, TP}
m::Tm
P::TP
end
$name
end
end
module MyMod
const C = 1
func(a, b) = a * b
end
@eval usesmod(x) = Base.getproperty($MyMod, :func)(x, Base.getproperty($MyMod, :C))
@testset "inference for `getproperty`" begin
Gaussian = _Gaussian(:getproperty)
g = Gaussian(randn(3), randn(3, 3))
y_explicit, back_explicit = @inferred pullback(x -> x.m, g)
y_implicit, back_implicit = @inferred pullback(x -> x.m, Context{true}(nothing), g)
@test y_explicit == y_implicit == getfield(g, :m)
∇args = ((m = [1.0, 0.0, 0.0], P = nothing),)
# This type instability is due to the handling of non-bitstypes in `accum_param`
@test Base.return_types(back_implicit, Tuple{Vector{Float64}}) == Any[Union{Tuple{Nothing}, typeof(∇args)}]
# But the same should infer if implicit parameters are disabled
@test Base.return_types(back_explicit, Tuple{Vector{Float64}}) == Any[typeof(∇args)]
@test back_explicit([1., 0, 0]) == back_implicit([1., 0, 0]) == ∇args
Base.getproperty(g::Gaussian, s::Symbol) = 2getfield(g, s)
y, back = pullback(x -> x.m, g)
@test y == 2getfield(g, :m)
@test back([1., 0, 0]) == ((m = [2.0, 0.0, 0.0], P = nothing),)
Gaussian = _Gaussian(:pullback)
g = Gaussian(randn(3), randn(3, 3))
y, back = @inferred pullback(x -> x.m, g)
Zygote._pullback(::Zygote.AContext, ::typeof(getproperty), g::Gaussian, s::Symbol) = 3getfield(g, s), Δ -> (nothing, (; ((:m, :P) .=> nothing)..., s => 3Δ), nothing)
y, back = pullback(x -> x.m, g)
@test y == 3getfield(g, :m)
@test back([1., 0, 0]) == ((m = [3.0, 0.0, 0.0], P = nothing),)
Gaussian = _Gaussian(:rrule)
g = Gaussian(randn(3), randn(3, 3))
y, back = @inferred pullback(x -> x.m, g)
ChainRulesCore.rrule(::typeof(getproperty), g::Gaussian, s::Symbol) = 4getfield(g, s), Δ -> (NoTangent(), Tangent{typeof(g)}(; s => 4Δ), NoTangent())
y, back = pullback(x -> x.m, g)
@test y == 4getfield(g, :m)
@test back([1., 0, 0]) == ((m = [4.0, 0.0, 0.0], P = nothing),)
Gaussian = _Gaussian(:bitstype)
g = Gaussian(randn(), randn())
y, back = @inferred pullback(x -> x.m, g)
@test y == getfield(g, :m)
@test @inferred(back(1.0)) == ((m = 1.0, P = nothing),)
# Const properties on modules should be lowered as-is (not differentiated)
@test @inferred gradient(usesmod, 1)[1] == 1.0
end
# issue 897
@test gradient(x -> sum(norm, collect(eachcol(x))), ones(3, 400))[1] ≈ fill(0.5773502691896258, 3, 400)
# Tests adapted from https://github.com/dfdx/Umlaut.jl/pull/35
@eval _has_boundscheck(x) = ifelse($(Expr(:boundscheck)), 2x, x)
@testset "Meta Expr handling" begin
y, (dx,) = withgradient(_has_boundscheck, 1)
@test y == 2
@test dx == 2
end
# issue 1118 & 1380
function f_1380(x)
if rand(Bool)
return x
else
return 2x
end
# unreachable
return nothing
end
@testset "unreachable block" begin
y, back = Zygote.pullback(f_1380, 1.)
# There should not be a compiler error
local g
@test_nowarn g = back(1.)
@test only(g) ∈ (1., 2.)
end
function throws_and_catches_if_x_negative(x,y)
z = x + y
try
if x < 0.
throw(DomainError("x is negative"))
end
z = 2z + x + y
catch err
@error "something went wrong" exception=(err,catch_backtrace())
end
return 3z
end
function try_catch_finally(cond, x)
try
x = 2x
cond && throw(DomainError())
catch
x = 2x
finally
x = 3x
end
x
end
function try_catch_else(cond, x)
x = 2x
try
x = 2x
cond && throw(nothing)
catch
x = 3x
else
x = 2x
end
x
end
@testset "try/catch" begin
@testset "happy path (nothrow)" begin
res, (dx,dy) = withgradient(throws_and_catches_if_x_negative, 1., 2.)
@test res == 3 * (2 * (1. + 2.) + 1. + 2.)
@test dx == 3. * (2. + 1.)
@test dy == 3. * (2. + 1.)
end
@testset "try/catch/finally" begin
res, (_, dx,) = withgradient(try_catch_finally, false, 1.)
@test res == 6.
@test dx == 6.
res, pull = pullback(try_catch_finally, true, 1.)
@test res == 12.
@test_throws ErrorException pull(1.)
err = try pull(1.) catch ex; ex end
@test occursin("Can't differentiate function execution in catch block", string(err))
end
@testset "try/catch/else" begin
@test Zygote.gradient(try_catch_else, false, 1.0) == (nothing, 8.0)
@test_throws ErrorException Zygote.gradient(try_catch_else, true, 1.0)
end
function foo_try(f)
y = 1
try
y = f()
catch
y
end
y
end
g, = gradient(x -> foo_try(() -> x), 1) # 1
@test g == 1.
vy, pull = pullback(foo_try, () -> 0//0) # bypass because of expr
@test vy === 1
@test_throws ErrorException pull(1.)
err = try pull(1.) catch ex; ex end
@test occursin("Can't differentiate function execution in catch block", string(err))
end