Skip to content

Commit dfe017f

Browse files
authored
No mapreduce in default test scenarios (#282)
* No mapreduce in default test scenarios * Fix efficiency tests * Replace randn with rand * Default alpha * Fix * Fix missing ChainRule in 1.6 * Maybe Zygote on 1.6 will finaly shut up * Zygote HVP uses ForwardDiff * No CRC second order on 1.6 * Typo * Version bump
1 parent e43cf6b commit dfe017f

File tree

11 files changed

+168
-79
lines changed

11 files changed

+168
-79
lines changed

DifferentiationInterface/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DifferentiationInterface"
22
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
33
authors = ["Guillaume Dalle", "Adrian Hill"]
4-
version = "0.5.1"
4+
version = "0.5.2"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

DifferentiationInterface/ext/DifferentiationInterfaceZygoteExt/DifferentiationInterfaceZygoteExt.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
module DifferentiationInterfaceZygoteExt
22

3-
using ADTypes: AutoZygote
3+
using ADTypes: AutoForwardDiff, AutoZygote
44
import DifferentiationInterface as DI
55
using DifferentiationInterface:
6-
NoGradientExtras, NoHessianExtras, NoJacobianExtras, NoPullbackExtras, PullbackExtras
6+
HVPExtras,
7+
NoGradientExtras,
8+
NoHessianExtras,
9+
NoJacobianExtras,
10+
NoPullbackExtras,
11+
PullbackExtras
712
using DocStringExtensions
813
using Zygote:
914
ZygoteRuleConfig, gradient, hessian, jacobian, pullback, withgradient, withjacobian
@@ -88,6 +93,31 @@ function DI.jacobian!(f, jac, backend::AutoZygote, x, extras::NoJacobianExtras)
8893
return copyto!(jac, DI.jacobian(f, backend, x, extras))
8994
end
9095

96+
## HVP (with ForwardDiff)
97+
98+
# TODO: find a way to do this without cheating?
99+
100+
struct ZygoteHVPExtras{G,PE} <: HVPExtras
101+
∇f::G
102+
pushforward_extras::PE
103+
end
104+
105+
function DI.prepare_hvp(f, ::AutoZygote, x, v)
106+
∇f(x) = only(gradient(f, x))
107+
pushforward_extras = DI.prepare_pushforward(∇f, AutoForwardDiff(), x, v)
108+
return ZygoteHVPExtras(∇f, pushforward_extras)
109+
end
110+
111+
function DI.hvp(f, ::AutoZygote, x, v, extras::ZygoteHVPExtras)
112+
@compat (; ∇f, pushforward_extras) = extras
113+
return DI.pushforward(∇f, AutoForwardDiff(), x, v, pushforward_extras)
114+
end
115+
116+
function DI.hvp!(f, p, ::AutoZygote, x, v, extras::ZygoteHVPExtras)
117+
@compat (; ∇f, pushforward_extras) = extras
118+
return DI.pushforward!(∇f, p, AutoForwardDiff(), x, v, pushforward_extras)
119+
end
120+
91121
## Hessian
92122

93123
DI.prepare_hessian(f, ::AutoZygote, x) = NoHessianExtras()

DifferentiationInterface/test/Single/Diffractor.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ for backend in [AutoDiffractor()]
88
@test !check_hessian(backend; verbose=false)
99
end
1010

11-
test_differentiation(AutoDiffractor(); second_order=false, logging=LOGGING);
11+
test_differentiation(
12+
AutoDiffractor(), default_scenarios(; linalg=false); second_order=false, logging=LOGGING
13+
);

DifferentiationInterface/test/Single/ForwardDiff/efficiency.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@ using DifferentiationInterface, DifferentiationInterfaceTest
33
using ForwardDiff: ForwardDiff
44
using SparseConnectivityTracer
55
using SparseMatrixColorings
6+
using Test
7+
8+
function vecexp!(y, x)
9+
y .= exp.(x)
10+
return nothing
11+
end
12+
13+
sumexp(x) = sum(exp, x)
614

715
@testset verbose = false "Dense efficiency" begin
816
# derivative and gradient for `f(x)`
917

1018
results1 = benchmark_differentiation(
1119
[AutoForwardDiff()],
12-
default_scenarios();
13-
outofplace=false,
14-
twoarg=false,
15-
input_type=Union{Number,AbstractVector},
16-
output_type=Number,
17-
second_order=false,
18-
excluded=[PullbackScenario],
20+
[
21+
DerivativeScenario(exp; x=1.0, place=:outofplace),
22+
GradientScenario(sumexp; x=rand(10), place=:inplace),
23+
];
1924
logging=LOGGING,
2025
)
2126

2227
# derivative and jacobian for f!(x, y)
2328

2429
results2 = benchmark_differentiation(
2530
[AutoForwardDiff()],
26-
default_scenarios();
27-
outofplace=false,
28-
onearg=false,
29-
input_type=Union{Number,AbstractVector},
30-
output_type=AbstractVector,
31-
second_order=false,
32-
excluded=[PullbackScenario],
31+
[
32+
DerivativeScenario(vecexp!; x=1.0, y=zeros(10), place=:inplace),
33+
JacobianScenario(vecexp!; x=rand(10), y=zeros(10), place=:inplace),
34+
];
3335
logging=LOGGING,
3436
)
3537

DifferentiationInterface/test/Single/Zygote.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ end
2222

2323
## Dense backends
2424

25-
test_differentiation(dense_backends; excluded=[SecondDerivativeScenario], logging=LOGGING);
25+
test_differentiation(
26+
AutoChainRules(Zygote.ZygoteRuleConfig());
27+
excluded=[SecondDerivativeScenario],
28+
second_order=VERSION >= v"1.10",
29+
logging=LOGGING,
30+
);
31+
32+
test_differentiation(AutoZygote(); excluded=[SecondDerivativeScenario], logging=LOGGING);
2633

2734
test_differentiation(
2835
AutoZygote(),

DifferentiationInterfaceTest/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DifferentiationInterfaceTest"
22
uuid = "a82114a7-5aa3-49a8-9643-716bb13727a3"
33
authors = ["Guillaume Dalle", "Adrian Hill"]
4-
version = "0.4.1"
4+
version = "0.4.2"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

DifferentiationInterfaceTest/src/scenarios/component.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
## Vector to scalar
22

33
function comp_to_num(x::ComponentVector)::Number
4-
return sum(sin, x.a) + sum(cos, x.b)
4+
return sum(sin.(x.a)) + sum(cos.(x.b))
55
end
66

77
comp_to_num_gradient(x) = ComponentVector(; a=cos.(x.a), b=-sin.(x.b))
88

99
function comp_to_num_pushforward(x, dx)
1010
g = comp_to_num_gradient(x)
11-
return dot(g.a, dx.a) + dot(g.b, dx.b)
11+
return sum(g.a .* dx.a) + sum(g.b .* dx.b)
1212
end
1313

1414
function comp_to_num_pullback(x, dy)

DifferentiationInterfaceTest/src/scenarios/default.jl

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,63 @@ end
140140

141141
## Array to scalar
142142

143-
arr_to_num(x::AbstractArray)::Number = sum(sin, x)
143+
const DEFAULT_α = 4
144+
const DEFAULT_β = 6
144145

145-
arr_to_num_gradient(x) = cos.(x)
146-
arr_to_num_hvp(x, v) = -sin.(x) .* v
146+
arr_to_num_aux_linalg(x; α, β) = sum(vec(x .^ α) .* transpose(vec(x .^ β)))
147+
148+
function arr_to_num_aux_no_linalg(x; α, β)
149+
n = length(x)
150+
s = zero(eltype(x))
151+
for i in 1:n, j in 1:n
152+
s += x[i]^α * x[j]^β
153+
end
154+
return s
155+
end
156+
157+
function arr_to_num_aux_gradient(x; α, β)
158+
x = Array(x) # GPU arrays don't like indexing
159+
g = similar(x)
160+
for k in eachindex(g, x)
161+
g[k] = (
162+
α * x[k]^- 1) * sum(x[j]^β for j in eachindex(x) if j != k) +
163+
β * x[k]^- 1) * sum(x[i]^α for i in eachindex(x) if i != k) +
164+
+ β) * x[k]^+ β - 1)
165+
)
166+
end
167+
return g
168+
end
169+
170+
function arr_to_num_aux_hessian(x; α, β)
171+
x = Array(x) # GPU arrays don't like indexing
172+
H = similar(x, length(x), length(x))
173+
for k in axes(H, 1), l in axes(H, 2)
174+
if k == l
175+
H[k, k] = (
176+
α *- 1) * x[k]^- 2) * sum(x[j]^β for j in eachindex(x) if j != k) +
177+
β *- 1) * x[k]^- 2) * sum(x[i]^α for i in eachindex(x) if i != k) +
178+
+ β) *+ β - 1) * x[k]^+ β - 2)
179+
)
180+
else
181+
H[k, l] = α * β * (x[k]^- 1) * x[l]^- 1) + x[k]^- 1) * x[l]^- 1))
182+
end
183+
end
184+
return H
185+
end
186+
187+
arr_to_num_linalg(x::AbstractArray)::Number =
188+
arr_to_num_aux_linalg(x; α=DEFAULT_α, β=DEFAULT_β)
189+
arr_to_num_no_linalg(x::AbstractArray)::Number =
190+
arr_to_num_aux_no_linalg(x; α=DEFAULT_α, β=DEFAULT_β)
191+
192+
arr_to_num_gradient(x) = arr_to_num_aux_gradient(x; α=DEFAULT_α, β=DEFAULT_β)
193+
arr_to_num_hessian(x) = arr_to_num_aux_hessian(x; α=DEFAULT_α, β=DEFAULT_β)
147194
arr_to_num_pushforward(x, dx) = dot(arr_to_num_gradient(x), dx)
148195
arr_to_num_pullback(x, dy) = arr_to_num_gradient(x) .* dy
149-
arr_to_num_hessian(x) = Matrix(Diagonal(-sin.(vec(x))))
196+
arr_to_num_hvp(x, v) = reshape(arr_to_num_hessian(x) * vec(v), size(x))
150197

151-
function arr_to_num_scenarios_onearg(x::AbstractArray)
198+
function arr_to_num_scenarios_onearg(x::AbstractArray; linalg=true)
199+
arr_to_num = linalg ? arr_to_num_linalg : arr_to_num_no_linalg
152200
# pushforward stays out of place
153201
scens = AbstractScenario[]
154202
for place in (:outofplace, :inplace)
@@ -448,24 +496,24 @@ const IMAT = Matrix((1:2) .* transpose(1:3))
448496
449497
Create a vector of [`AbstractScenario`](@ref)s with standard array types.
450498
"""
451-
function default_scenarios()
499+
function default_scenarios(; linalg=true)
452500
return vcat(
453501
# one argument
454-
num_to_num_scenarios_onearg(randn()),
455-
num_to_arr_scenarios_onearg(randn(), IVEC),
456-
num_to_arr_scenarios_onearg(randn(), IMAT),
457-
arr_to_num_scenarios_onearg(randn(6)),
458-
arr_to_num_scenarios_onearg(randn(2, 3)),
459-
vec_to_vec_scenarios_onearg(randn(6)),
460-
vec_to_mat_scenarios_onearg(randn(6)),
461-
mat_to_vec_scenarios_onearg(randn(2, 3)),
462-
mat_to_mat_scenarios_onearg(randn(2, 3)),
502+
num_to_num_scenarios_onearg(rand()),
503+
num_to_arr_scenarios_onearg(rand(), IVEC),
504+
num_to_arr_scenarios_onearg(rand(), IMAT),
505+
arr_to_num_scenarios_onearg(rand(6); linalg),
506+
arr_to_num_scenarios_onearg(rand(2, 3); linalg),
507+
vec_to_vec_scenarios_onearg(rand(6)),
508+
vec_to_mat_scenarios_onearg(rand(6)),
509+
mat_to_vec_scenarios_onearg(rand(2, 3)),
510+
mat_to_mat_scenarios_onearg(rand(2, 3)),
463511
# two arguments
464-
num_to_arr_scenarios_twoarg(randn(), IVEC),
465-
num_to_arr_scenarios_twoarg(randn(), IMAT),
466-
vec_to_vec_scenarios_twoarg(randn(6)),
467-
vec_to_mat_scenarios_twoarg(randn(6)),
468-
mat_to_vec_scenarios_twoarg(randn(2, 3)),
469-
mat_to_mat_scenarios_twoarg(randn(2, 3)),
512+
num_to_arr_scenarios_twoarg(rand(), IVEC),
513+
num_to_arr_scenarios_twoarg(rand(), IMAT),
514+
vec_to_vec_scenarios_twoarg(rand(6)),
515+
vec_to_mat_scenarios_twoarg(rand(6)),
516+
mat_to_vec_scenarios_twoarg(rand(2, 3)),
517+
mat_to_mat_scenarios_twoarg(rand(2, 3)),
470518
)
471519
end

DifferentiationInterfaceTest/src/scenarios/gpu.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ const JLMAT = jl(IMAT)
66
77
Create a vector of [`AbstractScenario`](@ref)s with GPU array types from [JLArrays.jl](https://github.com/JuliaGPU/GPUArrays.jl/tree/master/lib/JLArrays).
88
"""
9-
function gpu_scenarios()
9+
function gpu_scenarios(; linalg=true)
1010
return vcat(
1111
# one argument
12-
num_to_arr_scenarios_onearg(randn(), JLVEC),
13-
num_to_arr_scenarios_onearg(randn(), JLMAT),
14-
arr_to_num_scenarios_onearg(jl(randn(6))),
15-
arr_to_num_scenarios_onearg(jl(randn(2, 3))),
16-
vec_to_vec_scenarios_onearg(jl(randn(6))),
17-
vec_to_mat_scenarios_onearg(jl(randn(6))),
18-
mat_to_vec_scenarios_onearg(jl(randn(2, 3))),
19-
mat_to_mat_scenarios_onearg(jl(randn(2, 3))),
12+
num_to_arr_scenarios_onearg(rand(), JLVEC),
13+
num_to_arr_scenarios_onearg(rand(), JLMAT),
14+
arr_to_num_scenarios_onearg(jl(rand(6)); linalg),
15+
arr_to_num_scenarios_onearg(jl(rand(2, 3)); linalg),
16+
vec_to_vec_scenarios_onearg(jl(rand(6))),
17+
vec_to_mat_scenarios_onearg(jl(rand(6))),
18+
mat_to_vec_scenarios_onearg(jl(rand(2, 3))),
19+
mat_to_mat_scenarios_onearg(jl(rand(2, 3))),
2020
# two arguments
21-
num_to_arr_scenarios_twoarg(randn(), JLVEC),
22-
num_to_arr_scenarios_twoarg(randn(), JLMAT),
23-
vec_to_vec_scenarios_twoarg(jl(randn(6))),
24-
vec_to_mat_scenarios_twoarg(jl(randn(6))),
25-
mat_to_vec_scenarios_twoarg(jl(randn(2, 3))),
26-
mat_to_mat_scenarios_twoarg(jl(randn(2, 3))),
21+
num_to_arr_scenarios_twoarg(rand(), JLVEC),
22+
num_to_arr_scenarios_twoarg(rand(), JLMAT),
23+
vec_to_vec_scenarios_twoarg(jl(rand(6))),
24+
vec_to_mat_scenarios_twoarg(jl(rand(6))),
25+
mat_to_vec_scenarios_twoarg(jl(rand(2, 3))),
26+
mat_to_mat_scenarios_twoarg(jl(rand(2, 3))),
2727
)
2828
end

DifferentiationInterfaceTest/src/scenarios/sparse.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ Create a vector of [`AbstractScenario`](@ref)s with sparse array types, focused
229229
"""
230230
function sparse_scenarios()
231231
return vcat(
232-
sparse_vec_to_vec_scenarios(randn(6)),
233-
sparse_vec_to_mat_scenarios(randn(6)),
234-
sparse_mat_to_vec_scenarios(randn(2, 3)),
235-
sparse_mat_to_mat_scenarios(randn(2, 3)),
236-
sparse_vec_to_num_scenarios(randn(6)),
237-
sparse_mat_to_num_scenarios(randn(2, 3)),
232+
sparse_vec_to_vec_scenarios(rand(6)),
233+
sparse_vec_to_mat_scenarios(rand(6)),
234+
sparse_mat_to_vec_scenarios(rand(2, 3)),
235+
sparse_mat_to_mat_scenarios(rand(2, 3)),
236+
sparse_vec_to_num_scenarios(rand(6)),
237+
sparse_mat_to_num_scenarios(rand(2, 3)),
238238
)
239239
end

0 commit comments

Comments
 (0)