-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathrule_definition_tools.jl
More file actions
395 lines (320 loc) · 14.8 KB
/
rule_definition_tools.jl
File metadata and controls
395 lines (320 loc) · 14.8 KB
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
"""
Along same lines as `@test_throws` but to test if a macro throw an exception when it is
expanded.
"""
macro test_macro_throws(err_expr, expr)
quote
err = nothing
try
@macroexpand($(esc(expr)))
catch _err
# https://github.com/JuliaLang/julia/pull/38379
if VERSION >= v"1.7.0-DEV.937"
err = _err
else
# until Julia v1.7
# all errors thrown at macro expansion time are LoadErrors, we need to unwrap
@assert _err isa LoadError
err = _err.error
end
end
# Reuse `@test_throws` logic
if err !== nothing
@test_throws $(esc(err_expr)) ($(Meta.quot(expr)); throw(err))
else
@test_throws $(esc(err_expr)) $(Meta.quot(expr))
end
end
end
# struct need to be defined outside of tests for julia 1.0 compat
struct NonDiffExample
x
end
struct NonDiffCounterExample
x
end
module NonDiffModuleExample
nondiff_2_1(x, y) = fill(7.5, 100)[x + y]
end
@testset "rule_definition_tools.jl" begin
@testset "@non_differentiable" begin
@testset "two input one output function" begin
nondiff_2_1(x, y) = fill(7.5, 100)[x + y]
@non_differentiable nondiff_2_1(::Any, ::Any)
@test frule((ZeroTangent(), 1.2, 2.3), nondiff_2_1, 3, 2) == (7.5, NoTangent())
res, pullback = rrule(nondiff_2_1, 3, 2)
@test res == 7.5
@test pullback(4.5) == (NoTangent(), NoTangent(), NoTangent())
end
@testset "one input, 2-tuple output function" begin
nondiff_1_2(x) = (5.0, 3.0)
@non_differentiable nondiff_1_2(::Any)
@test frule((ZeroTangent(), 1.2), nondiff_1_2, 3.1) == ((5.0, 3.0), NoTangent())
res, pullback = rrule(nondiff_1_2, 3.1)
@test res == (5.0, 3.0)
@test isequal(
pullback(Tangent{Tuple{Float64,Float64}}(1.2, 3.2)),
(NoTangent(), NoTangent()),
)
end
@testset "constrained signature" begin
nonembed_identity(x) = x
@non_differentiable nonembed_identity(::Integer)
@test frule((ZeroTangent(), 1.2), nonembed_identity, 2) == (2, NoTangent())
@test frule((ZeroTangent(), 1.2), nonembed_identity, 2.0) == nothing
res, pullback = rrule(nonembed_identity, 2)
@test res == 2
@test pullback(1.2) == (NoTangent(), NoTangent())
@test rrule(nonembed_identity, 2.0) == nothing
end
@testset "Pointy UnionAll constraints" begin
pointy_identity(x) = x
@non_differentiable pointy_identity(::Vector{<:AbstractString})
@test frule((ZeroTangent(), 1.2), pointy_identity, ["2"]) ==
(["2"], NoTangent())
@test frule((ZeroTangent(), 1.2), pointy_identity, 2.0) == nothing
res, pullback = rrule(pointy_identity, ["2"])
@test res == ["2"]
@test pullback(1.2) == (NoTangent(), NoTangent())
@test rrule(pointy_identity, 2.0) == nothing
end
@testset "kwargs" begin
kw_demo(x; kw=2.0) = x + kw
@non_differentiable kw_demo(::Any)
@testset "not setting kw" begin
@assert kw_demo(1.5) == 3.5
res, pullback = rrule(kw_demo, 1.5)
@test res == 3.5
@test pullback(4.1) == (NoTangent(), NoTangent())
@test frule((ZeroTangent(), 11.1), kw_demo, 1.5) == (3.5, NoTangent())
end
@testset "setting kw" begin
@assert kw_demo(1.5; kw=3.0) == 4.5
res, pullback = rrule(kw_demo, 1.5; kw=3.0)
@test res == 4.5
@test pullback(1.1) == (NoTangent(), NoTangent())
@test frule((ZeroTangent(), 11.1), kw_demo, 1.5; kw=3.0) ==
(4.5, NoTangent())
end
end
@testset "Constructors" begin
@non_differentiable NonDiffExample(::Any)
@test isequal(
frule((ZeroTangent(), 1.2), NonDiffExample, 2.0),
(NonDiffExample(2.0), NoTangent()),
)
res, pullback = rrule(NonDiffExample, 2.0)
@test res == NonDiffExample(2.0)
@test pullback(1.2) == (NoTangent(), NoTangent())
# https://github.com/JuliaDiff/ChainRulesCore.jl/issues/213
# problem was that `@nondiff Foo(x)` was also defining rules for other types.
# make sure that isn't happenning
@test frule((ZeroTangent(), 1.2), NonDiffCounterExample, 2.0) === nothing
@test rrule(NonDiffCounterExample, 2.0) === nothing
end
@testset "Varargs" begin
fvarargs(a, xs...) = sum((a, xs...))
@testset "xs::Float64..." begin
@non_differentiable fvarargs(a, xs::Float64...)
y, pb = rrule(fvarargs, 1)
@test y == fvarargs(1)
@test pb(1) == (NoTangent(), NoTangent())
y, pb = rrule(fvarargs, 1, 2.0, 3.0)
@test y == fvarargs(1, 2.0, 3.0)
@test pb(1) == (NoTangent(), NoTangent(), NoTangent(), NoTangent())
@test frule((1, 1), fvarargs, 1, 2.0) == (fvarargs(1, 2.0), NoTangent())
@test frule((1, 1), fvarargs, 1, 2) == nothing
@test rrule(fvarargs, 1, 2) == nothing
end
@testset "::Float64..." begin
@non_differentiable fvarargs(a, ::Float64...)
y, pb = rrule(fvarargs, 1, 2.0, 3.0)
@test y == fvarargs(1, 2.0, 3.0)
@test pb(1) == (NoTangent(), NoTangent(), NoTangent(), NoTangent())
@test frule((1, 1), fvarargs, 1, 2.0) == (fvarargs(1, 2.0), NoTangent())
end
@testset "::Vararg{Float64}" begin
@non_differentiable fvarargs(a, ::Vararg{Float64})
y, pb = rrule(fvarargs, 1, 2.0, 3.0)
@test y == fvarargs(1, 2.0, 3.0)
@test pb(1) == (NoTangent(), NoTangent(), NoTangent(), NoTangent())
@test frule((1, 1), fvarargs, 1, 2.0) == (fvarargs(1, 2.0), NoTangent())
end
@testset "::Vararg" begin
@non_differentiable fvarargs(a, ::Vararg)
@test frule((1, 1), fvarargs, 1, 2) == (fvarargs(1, 2), NoTangent())
y, pb = rrule(fvarargs, 1, 1)
@test y == fvarargs(1, 1)
@test pb(1) == (NoTangent(), NoTangent(), NoTangent())
end
@testset "xs..." begin
@non_differentiable fvarargs(a, xs...)
@test frule((1, 1), fvarargs, 1, 2) == (fvarargs(1, 2), NoTangent())
y, pb = rrule(fvarargs, 1, 1)
@test y == fvarargs(1, 1)
@test pb(1) == (NoTangent(), NoTangent(), NoTangent())
end
end
@testset "Functors" begin
(f::NonDiffExample)(y) = fill(7.5, 100)[f.x + y]
@non_differentiable (::NonDiffExample)(::Any)
@test frule((Tangent{NonDiffExample}(; x=1.2), 2.3), NonDiffExample(3), 2) ==
(7.5, NoTangent())
res, pullback = rrule(NonDiffExample(3), 2)
@test res == 7.5
@test pullback(4.5) == (NoTangent(), NoTangent())
end
@testset "Module specified explicitly" begin
@non_differentiable NonDiffModuleExample.nondiff_2_1(::Any, ::Any)
@test frule(
(ZeroTangent(), 1.2, 2.3), NonDiffModuleExample.nondiff_2_1, 3, 2
) == (7.5, NoTangent())
res, pullback = rrule(NonDiffModuleExample.nondiff_2_1, 3, 2)
@test res == 7.5
@test pullback(4.5) == (NoTangent(), NoTangent(), NoTangent())
end
@testset "Not supported (Yet)" begin
# Where clauses are not supported.
@test_macro_throws(
ErrorException,
(@non_differentiable where_identity(::Vector{T}) where {T<:AbstractString})
)
end
end
@testset "@scalar_rule" begin
@testset "@scalar_rule with multiple output" begin
simo(x) = (x, 2x)
@scalar_rule(simo(x), 1.0f0, 2.0f0)
y, simo_pb = rrule(simo, π)
@test simo_pb((10.0f0, 20.0f0)) == (NoTangent(), 50.0f0)
y, ẏ = frule((NoTangent(), 50.0f0), simo, π)
@test y == (π, 2π)
@test ẏ == Tangent{typeof(y)}(50.0f0, 100.0f0)
# make sure type is exactly as expected:
@test ẏ isa Tangent{Tuple{Irrational{:π},Float64},Tuple{Float32,Float32}}
xs, Ω = (3,), (3, 6)
@test ChainRulesCore.derivatives_given_output(Ω, simo, xs...) ==
((1.0f0,), (2.0f0,))
end
@testset "@scalar_rule projection" begin
make_imaginary(x) = im * x
@scalar_rule make_imaginary(x) im
# note: the === will make sure that these are Float64, not ComplexF64
@test (NoTangent(), 1.0) === rrule(make_imaginary, 2.0)[2](1.0 * im)
@test (NoTangent(), 0.0) === rrule(make_imaginary, 2.0)[2](1.0)
@test (NoTangent(), 1.0 + 0.0im) === rrule(make_imaginary, 2.0im)[2](1.0 * im)
@test (NoTangent(), 0.0 - 1.0im) === rrule(make_imaginary, 2.0im)[2](1.0)
end
@testset "@scalar_rule strong zero (co)tangents" begin
suminv(x, y) = inv(x) + inv(y)
@scalar_rule suminv(x, y) (-(inv(x)^2), -(inv(y)^2))
@test frule((NoTangent(), 1.0, 1.0), suminv, 0.0, 1.0) === (Inf, -Inf)
@test frule((NoTangent(), ZeroTangent(), 1.0), suminv, 0.0, 1.0) === (Inf, -1.0)
@test frule((NoTangent(), 0.0, 1.0), suminv, 0.0, 1.0) === (Inf, -1.0)
@test frule((NoTangent(), 1.0, 1.0), suminv, 1.0, 0.0) === (Inf, -Inf)
@test frule((NoTangent(), 1.0, ZeroTangent()), suminv, 1.0, 0.0) === (Inf, -1.0)
@test frule((NoTangent(), 1.0, 0.0), suminv, 1.0, 0.0) === (Inf, -1.0)
@test rrule(suminv, 0.0, 1.0)[2](1.0) === (NoTangent(), -Inf, -1.0)
@test rrule(suminv, 0.0, 1.0)[2](ZeroTangent()) ===
(NoTangent(), ZeroTangent(), ZeroTangent())
@test rrule(suminv, 0.0, 1.0)[2](0.0) === (NoTangent(), 0.0, 0.0)
@test rrule(suminv, 1.0, 0.0)[2](1.0) === (NoTangent(), -1.0, -Inf)
@test rrule(suminv, 1.0, 0.0)[2](ZeroTangent()) ===
(NoTangent(), ZeroTangent(), ZeroTangent())
@test rrule(suminv, 1.0, 0.0)[2](0.0) === (NoTangent(), 0.0, 0.0)
end
@testset "Regression tests against #276 and #265" begin
# https://github.com/JuliaDiff/ChainRulesCore.jl/pull/276
# https://github.com/JuliaDiff/ChainRulesCore.jl/pull/265
# Symptom of these problems is creation of global variables and type instability
num_globals_before = length(names(ChainRulesCore; all=true))
simo2(x) = (x, 2x)
@scalar_rule(simo2(x), 1.0, 2.0)
_, simo2_pb = rrule(simo2, 43.0)
# make sure it infers: inferability implies type stability
@inferred simo2_pb(Tangent{Tuple{Float64,Float64}}(3.0, 6.0))
# Test no new globals were created
@test length(names(ChainRulesCore; all=true)) == num_globals_before
# Example in #265
simo3(x) = sincos(x)
@scalar_rule simo3(x) @setup((sinx, cosx) = Ω) cosx -sinx
_, simo3_pb = @inferred rrule(simo3, randn())
@inferred simo3_pb(Tangent{Tuple{Float64,Float64}}(randn(), randn()))
end
end
end
#! format: off
# workaround for https://github.com/domluna/JuliaFormatter.jl/issues/484
module IsolatedModuleForTestingScoping
# check that rules can be defined by macros without any additional imports
using ChainRulesCore: @scalar_rule, @non_differentiable, @opt_out
# ensure that functions, types etc. in module `ChainRulesCore` can't be resolved
const ChainRulesCore = nothing
# this is
# https://github.com/JuliaDiff/ChainRulesCore.jl/issues/317
fixed(x) = :abc
@non_differentiable fixed(x)
# check name collision between a primal input called `kwargs` and the actual keyword
# arguments
fixed_kwargs(x; kwargs...) = :abc
@non_differentiable fixed_kwargs(kwargs)
my_id(x) = x
@scalar_rule(my_id(x), 1.0)
# @opt_out
first_oa(x, y) = x
@scalar_rule(first_oa(x, y), (1, 0))
# Declared without using the ChainRulesCore namespace qualification
# see https://github.com/JuliaDiff/ChainRulesCore.jl/issues/545
@opt_out rrule(::typeof(first_oa), x::T, y::T) where {T<:Float16}
@opt_out frule(::Any, ::typeof(first_oa), x::T, y::T) where {T<:Float16}
module IsolatedSubmodule
# check that rules defined in isolated module without imports can be called
# without errors
using ChainRulesCore: frule, rrule, ZeroTangent, NoTangent, derivatives_given_output
using ChainRulesCore: no_rrule, no_frule
using ..IsolatedModuleForTestingScoping: fixed, fixed_kwargs, my_id, first_oa
using Test
@testset "@non_differentiable" begin
for f in (fixed, fixed_kwargs)
y, ẏ = frule((ZeroTangent(), randn()), f, randn())
@test y === :abc
@test ẏ === NoTangent()
y, f_pullback = rrule(f, randn())
@test y === :abc
@test f_pullback(randn()) === (NoTangent(), NoTangent())
end
y, f_pullback = rrule(fixed_kwargs, randn(); keyword=randn())
@test y === :abc
@test f_pullback(randn()) === (NoTangent(), NoTangent())
end
@testset "@scalar_rule" begin
x, ẋ = randn(2)
y, ẏ = frule((ZeroTangent(), ẋ), my_id, x)
@test y == x
@test ẏ == ẋ
Δy = randn()
y, f_pullback = rrule(my_id, x)
@test y == x
@test f_pullback(Δy) == (NoTangent(), Δy)
@test derivatives_given_output(y, my_id, x) == ((1.0,),)
end
@testset "@optout" begin
# rrule
@test rrule(first_oa, Float16(3.0), Float16(4.0)) === nothing
@test !isempty(
Iterators.filter(methods(no_rrule)) do m
m.sig <: Tuple{Any,typeof(first_oa),T,T} where {T<:Float16}
end,
)
# frule
@test frule((NoTangent(), 1, 0), first_oa, Float16(3.0), Float16(4.0)) ===
nothing
@test !isempty(
Iterators.filter(methods(no_frule)) do m
m.sig <: Tuple{Any,Any,typeof(first_oa),T,T} where {T<:Float16}
end,
)
end
end
end
#! format: on