Skip to content

Commit 3e6164f

Browse files
authored
Fix code coverage issues (#133)
* Fix code coverage issues * Double benchmark * Remove test from docs
1 parent 5775e5c commit 3e6164f

11 files changed

Lines changed: 30 additions & 27 deletions

File tree

docs/src/tutorial.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ We present a typical workflow with DifferentiationInterface.jl and showcase its
88

99
```@repl tuto
1010
using DifferentiationInterface
11-
using BenchmarkTools
12-
import ForwardDiff, Enzyme, DataFrames
11+
import ForwardDiff, Enzyme
12+
using BenchmarkTools, DataFrames
1313
```
1414

1515
## Computing a gradient

ext/DifferentiationInterfaceEnzymeExt/DifferentiationInterfaceEnzymeExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ end
5656

5757
function zero_sametype!!(x_target, x)
5858
x_sametype = convert(typeof(x), x_target)
59-
x_sametype .= zero(eltype(x))
59+
x_sametype .= zero(eltype(x_sametype))
6060
return x_sametype
6161
end
6262

ext/DifferentiationInterfaceEnzymeExt/reverse_allocating.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757

5858
## Gradient
5959

60-
DI.prepare_gradient(f, ::AutoReverseEnzyme) = NoGradientExtras()
60+
DI.prepare_gradient(f, ::AutoReverseEnzyme, x) = NoGradientExtras()
6161

6262
function DI.gradient(f, ::AutoReverseEnzyme, x::AbstractArray, ::NoGradientExtras)
6363
return gradient(Reverse, f, x)

ext/DifferentiationInterfaceForwardDiffExt/utils.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@ choose_chunk(::AnyAutoForwardDiff{C}, x) where {C} = Chunk{C}()
44

55
tag_type(::F, x::Number) where {F} = Tag{F,typeof(x)}
66
tag_type(::F, x::AbstractArray) where {F} = Tag{F,eltype(x)}
7-
tag_type(::F, x) where {F} = Tag{F,typeof(x)}
87

98
make_dual(::Type{T}, x::Number, dx::Number) where {T} = Dual{T}(x, dx)
109
make_dual(::Type{T}, x::AbstractArray, dx) where {T} = Dual{T}.(x, dx)
1110

1211
myvalue(::Type{T}, ydual::Number) where {T} = value(T, ydual)
1312
myvalue(::Type{T}, ydual::AbstractArray) where {T} = value.(T, ydual)
1413

15-
myvalue!!(::Type{T}, y::Number, ydual::Number) where {T} = value(T, ydual)
1614
myvalue!!(::Type{T}, y::AbstractArray, ydual) where {T} = y .= value.(T, ydual)
1715

1816
myderivative(::Type{T}, ydual::Number) where {T} = extract_derivative(T, ydual)
1917
myderivative(::Type{T}, ydual::AbstractArray) where {T} = extract_derivative(T, ydual)
2018

21-
function myderivative!!(::Type{T}, dy::Number, ydual::Number) where {T}
22-
return extract_derivative(T, ydual)
23-
end
24-
2519
function myderivative!!(::Type{T}, dy::AbstractArray, ydual::AbstractArray) where {T}
2620
return extract_derivative!(T, dy, ydual)
2721
end

ext/DifferentiationInterfaceTrackerExt/DifferentiationInterfaceTrackerExt.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ function DI.gradient(f, ::AutoTracker, x, ::NoGradientExtras)
3030
return data(only(grad))
3131
end
3232

33-
function DI.value_and_gradient(f, ::AutoTracker, x::Number, ::NoGradientExtras)
34-
# fix for https://github.com/FluxML/Tracker.jl/issues/165
35-
return f(x), data(only(gradient(f, x)))
36-
end
37-
38-
function DI.gradient(f, ::AutoTracker, x::Number, ::NoGradientExtras)
39-
# fix for https://github.com/FluxML/Tracker.jl/issues/165
40-
return data(only(gradient(f, x)))
41-
end
42-
4333
function DI.value_and_gradient!!(f, grad, backend::AutoTracker, x, extras::NoGradientExtras)
4434
return DI.value_and_gradient(f, backend, x, extras)
4535
end

ext/DifferentiationInterfaceZygoteExt/DifferentiationInterfaceZygoteExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function DI.hessian(f, ::AnyAutoZygote, x, ::NoHessianExtras)
7575
return hessian(f, x)
7676
end
7777

78-
function DI.hessian!!(f, hess, backend::AnyAutoZygote, x, ::NoHessianExtras)
78+
function DI.hessian!!(f, hess, backend::AnyAutoZygote, x, extras::NoHessianExtras)
7979
return DI.hessian(f, backend, x, extras)
8080
end
8181

lib/DifferentiationInterfaceTest/src/scenarios/default.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ num_to_num_derivative(x) = cos(x)
1717
num_to_num_second_derivative(x) = -sin(x)
1818
num_to_num_pushforward(x, dx) = num_to_num_derivative(x) * dx
1919
num_to_num_pullback(x, dy) = num_to_num_derivative(x) * dy
20-
num_to_num_gradient(x) = num_to_num_derivative(x)
21-
num_to_num_hvp(x, v) = num_to_num_second_derivative(x) * v
2220

2321
function num_to_num_scenarios_allocating(x::Number)
2422
return [
@@ -95,7 +93,7 @@ arr_to_num_gradient(x) = cos.(x)
9593
arr_to_num_hvp(x, v) = -sin.(x) .* v
9694
arr_to_num_pushforward(x, dx) = dot(arr_to_num_gradient(x), dx)
9795
arr_to_num_pullback(x, dy) = arr_to_num_gradient(x) .* dy
98-
arr_to_num_hessian(x) = Diagonal(-sin.(vec(x)))
96+
arr_to_num_hessian(x) = Matrix(Diagonal(-sin.(vec(x))))
9997

10098
function arr_to_num_scenarios_allocating(x::AbstractArray)
10199
return [

lib/DifferentiationInterfaceTest/src/tests/benchmark.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ function run_benchmark!(
7272
(; f, x, y, dx) = deepcopy(scen)
7373
extras = prepare_pushforward(f, ba, x)
7474
bench1 = @be mysimilar(y) value_and_pushforward!!(f, _, ba, x, dx, extras)
75+
bench2 = @be mysimilar(y) pushforward!!(f, _, ba, x, dx, extras)
7576
record!(data, ba, value_and_pushforward!!, scen, bench1)
77+
record!(data, ba, pushforward!!, scen, bench2)
7678
return nothing
7779
end
7880

@@ -97,7 +99,9 @@ function run_benchmark!(
9799
(; f, x, y, dy) = deepcopy(scen)
98100
extras = prepare_pullback(f, ba, x)
99101
bench1 = @be mysimilar(x) value_and_pullback!!(f, _, ba, x, dy, extras)
102+
bench2 = @be mysimilar(x) pullback!!(f, _, ba, x, dy, extras)
100103
record!(data, ba, value_and_pullback!!, scen, bench1)
104+
record!(data, ba, pullback!!, scen, bench2)
101105
return nothing
102106
end
103107

@@ -122,7 +126,9 @@ function run_benchmark!(
122126
(; f, x, y) = deepcopy(scen)
123127
extras = prepare_derivative(f, ba, x)
124128
bench1 = @be mysimilar(y) value_and_derivative!!(f, _, ba, x, extras)
129+
bench2 = @be mysimilar(y) derivative!!(f, _, ba, x, extras)
125130
record!(data, ba, value_and_derivative!!, scen, bench1)
131+
record!(data, ba, derivative!!, scen, bench2)
126132
return nothing
127133
end
128134

@@ -147,7 +153,9 @@ function run_benchmark!(
147153
(; f, x) = deepcopy(scen)
148154
extras = prepare_gradient(f, ba, x)
149155
bench1 = @be mysimilar(x) value_and_gradient!!(f, _, ba, x, extras)
156+
bench2 = @be mysimilar(x) gradient!!(f, _, ba, x, extras)
150157
record!(data, ba, value_and_gradient!!, scen, bench1)
158+
record!(data, ba, gradient!!, scen, bench2)
151159
return nothing
152160
end
153161

@@ -160,7 +168,9 @@ function run_benchmark!(
160168
extras = prepare_jacobian(f, ba, x)
161169
jac_template = Matrix{eltype(y)}(undef, length(y), length(x))
162170
bench1 = @be mysimilar(jac_template) value_and_jacobian!!(f, _, ba, x, extras)
171+
bench2 = @be mysimilar(jac_template) jacobian!!(f, _, ba, x, extras)
163172
record!(data, ba, value_and_jacobian!!, scen, bench1)
173+
record!(data, ba, jacobian!!, scen, bench2)
164174
return nothing
165175
end
166176

src/second_derivative.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ struct NoSecondDerivativeExtras <: SecondDerivativeExtras end
1111

1212
"""
1313
prepare_second_derivative([other_extras], f, backend, x) -> extras
14-
prepare_second_derivative([other_extras], f!, backend, y, x) -> extras
1514
1615
Create an `extras` object subtyping [`SecondDerivativeExtras`](@ref) that can be given to second derivative operators.
1716
"""
@@ -20,7 +19,6 @@ function prepare_second_derivative(::Extras, f_or_f!, backend::AbstractADType, a
2019
end
2120

2221
prepare_second_derivative(f, ::AbstractADType, x) = NoSecondDerivativeExtras()
23-
prepare_second_derivative(f!, ::AbstractADType, y, x) = NoSecondDerivativeExtras()
2422

2523
## Allocating
2624

test/first_order.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ all_backends = [
88
AutoFiniteDifferences(FiniteDifferences.central_fdm(3, 1)),
99
AutoForwardDiff(),
1010
AutoPolyesterForwardDiff(; chunksize=2),
11-
AutoReverseDiff(),
11+
AutoReverseDiff(; compile=true),
1212
AutoTapir(),
1313
AutoTracker(),
1414
AutoZygote(),

0 commit comments

Comments
 (0)