Skip to content

Commit fe4ba4c

Browse files
authored
Increase code coverage (#264)
* Increase code coverage * Fix * Move detector tests
1 parent d5420a1 commit fe4ba4c

File tree

19 files changed

+147
-139
lines changed

19 files changed

+147
-139
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.4.0"
4+
version = "0.4.1"
55

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

DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/onearg.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,13 @@ function DI.derivative(
4242
return finite_difference_derivative(f, x, fdtype(backend))
4343
end
4444

45-
function DI.derivative!(
46-
f, der, backend::AutoFiniteDiff, x, extras::FiniteDiffOneArgDerivativeExtras{Nothing}
47-
)
48-
return copyto!(der, DI.derivative(f, backend, x, extras))
49-
end
50-
5145
function DI.value_and_derivative(
5246
f, backend::AutoFiniteDiff, x, ::FiniteDiffOneArgDerivativeExtras{Nothing}
5347
)
5448
y = f(x)
5549
return y, finite_difference_derivative(f, x, fdtype(backend), eltype(y), y)
5650
end
5751

58-
function DI.value_and_derivative!(
59-
f, der, backend::AutoFiniteDiff, x, extras::FiniteDiffOneArgDerivativeExtras{Nothing}
60-
)
61-
y, new_der = DI.value_and_derivative(f, backend, x, extras)
62-
return y, copyto!(der, new_der)
63-
end
64-
6552
### Scalar to array
6653

6754
function DI.derivative(

DifferentiationInterface/ext/DifferentiationInterfaceFiniteDifferencesExt/DifferentiationInterfaceFiniteDifferencesExt.jl

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

3232
## Pullback
3333

34-
DI.prepare_pullback(f, ::AutoFiniteDifferences, x) = NoPullbackExtras()
34+
DI.prepare_pullback(f, ::AutoFiniteDifferences, x, dy) = NoPullbackExtras()
3535

3636
function DI.pullback(f, backend::AutoFiniteDifferences, x, dy, ::NoPullbackExtras)
3737
return only(j′vp(backend.fdm, f, dy, x))

DifferentiationInterface/ext/DifferentiationInterfaceForwardDiffExt/utils.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ choose_chunk(::AutoForwardDiff{C}, x) where {C} = Chunk{C}()
44
tag_type(f, ::AutoForwardDiff{C,T}, x) where {C,T} = T
55
tag_type(f, ::AutoForwardDiff{C,Nothing}, x) where {C} = Tag{typeof(f),eltype(x)}
66

7-
make_dual(::Type{T}, x::Number, dx) where {T} = Dual{T}(x, dx)
8-
make_dual(::Type{T}, x, dx) where {T} = Dual{T}.(x, dx) # TODO: map causes Enzyme to fail
9-
107
make_dual_similar(::Type{T}, x::Number) where {T} = Dual{T}(x, x)
118
make_dual_similar(::Type{T}, x) where {T} = similar(x, Dual{T,eltype(x),1})
129

10+
make_dual(::Type{T}, x::Number, dx) where {T} = Dual{T}(x, dx)
1311
make_dual!(::Type{T}, xdual, x, dx) where {T} = map!(Dual{T}, xdual, x, dx)
1412

1513
myvalue(::Type{T}, ydual::Number) where {T} = value(T, ydual)

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/onearg.jl

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ function DI.prepare_gradient(f, ::AutoSymbolics, x)
109109
grad_var = gradient(f(x_var), vec(x_var))
110110

111111
res = build_function(grad_var, vec(x_var); expression=Val(false))
112-
(grad_exe, grad_exe!) = if res isa Tuple
113-
res
114-
elseif res isa RuntimeGeneratedFunction
115-
res, nothing
116-
end
112+
(grad_exe, grad_exe!) = res
117113
return SymbolicsOneArgGradientExtras(grad_exe, grad_exe!)
118114
end
119115

@@ -156,11 +152,7 @@ function DI.prepare_jacobian(
156152
end
157153

158154
res = build_function(jac_var, x_var; expression=Val(false))
159-
(jac_exe, jac_exe!) = if res isa Tuple
160-
res
161-
elseif res isa RuntimeGeneratedFunction
162-
res, nothing
163-
end
155+
(jac_exe, jac_exe!) = res
164156
return SymbolicsOneArgJacobianExtras(jac_exe, jac_exe!)
165157
end
166158

@@ -220,11 +212,7 @@ function DI.prepare_hessian(f, backend::Union{AutoSymbolics,AutoSparse{<:AutoSym
220212
end
221213

222214
res = build_function(hess_var, vec(x_var); expression=Val(false))
223-
(hess_exe, hess_exe!) = if res isa Tuple
224-
res
225-
elseif res isa RuntimeGeneratedFunction
226-
res, nothing
227-
end
215+
(hess_exe, hess_exe!) = res
228216
return SymbolicsOneArgHessianExtras(hess_exe, hess_exe!)
229217
end
230218

DifferentiationInterface/ext/DifferentiationInterfaceSymbolicsExt/twoarg.jl

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ function DI.prepare_pushforward(f!, y, ::AutoSymbolics, x, dx)
2323
pf_var = substitute(step_der_var, Dict(t_var => zero(eltype(x))))
2424

2525
res = build_function(pf_var, vcat(myvec(x_var), myvec(dx_var)); expression=Val(false))
26-
(pushforward_exe, pushforward_exe!) = if res isa Tuple
27-
res
28-
elseif res isa RuntimeGeneratedFunction
29-
res, nothing
30-
end
26+
(pushforward_exe, pushforward_exe!) = res
3127
return SymbolicsTwoArgPushforwardExtras(pushforward_exe, pushforward_exe!)
3228
end
3329

@@ -77,11 +73,7 @@ function DI.prepare_derivative(f!, y, ::AutoSymbolics, x)
7773
der_var = derivative(y_var, x_var)
7874

7975
res = build_function(der_var, x_var; expression=Val(false))
80-
(der_exe, der_exe!) = if res isa Tuple
81-
res
82-
elseif res isa RuntimeGeneratedFunction
83-
res, nothing
84-
end
76+
(der_exe, der_exe!) = res
8577
return SymbolicsTwoArgDerivativeExtras(der_exe, der_exe!)
8678
end
8779

@@ -132,11 +124,7 @@ function DI.prepare_jacobian(
132124
end
133125

134126
res = build_function(jac_var, x_var; expression=Val(false))
135-
(jac_exe, jac_exe!) = if res isa Tuple
136-
res
137-
elseif res isa RuntimeGeneratedFunction
138-
res, nothing
139-
end
127+
(jac_exe, jac_exe!) = res
140128
return SymbolicsTwoArgJacobianExtras(jac_exe, jac_exe!)
141129
end
142130

DifferentiationInterface/src/sparse/coloring.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ function neighbors(g::AdjacencyGraph, j::Integer)
130130
return filter(!isequal(j), nz_in_col(g.A_colmajor, j))
131131
end
132132

133-
function colored_neighbors(g::AdjacencyGraph, j::Integer, colors::AbstractVector{<:Integer})
134-
return filter(neighbors(g, j)) do i
135-
!iszero(colors[i])
136-
end
137-
end
138-
139133
function star_coloring(g::AdjacencyGraph)
140134
n = length(columns(g))
141135
colors = zeros(Int, n)

DifferentiationInterface/src/utils/exceptions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
struct MissingBackendError <: Exception
22
backend::AbstractADType
33
end
4+
45
function Base.showerror(io::IO, e::MissingBackendError)
56
println(io, "failed to use $(backend_str(e.backend)) backend.")
67
if !check_available(e.backend)

DifferentiationInterface/test/Internals/coloring.jl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,41 @@ import DifferentiationInterface as DI
33
import DifferentiationInterfaceTest as DIT
44
using LinearAlgebra: I, Symmetric
55
using SparseArrays: sprand
6+
using Test
67

78
alg = DI.GreedyColoringAlgorithm()
89

9-
A = sprand(Bool, 100, 200, 0.05)
10+
@testset "Grouping" begin
11+
colors = [1, 3, 1, 3, 1, 2]
12+
@test DI.color_groups(colors) == [[1, 3, 5], [6], [2, 4]]
13+
colors = [2, 3, 2, 3, 2, 1]
14+
@test DI.color_groups(colors) == [[6], [1, 3, 5], [2, 4]]
15+
colors = [2, 3, 2, 3, 2]
16+
@test_throws AssertionError DI.color_groups(colors)
17+
end
1018

11-
column_colors = ADTypes.column_coloring(A, alg)
12-
@test DIT.check_structurally_orthogonal_columns(A, column_colors)
13-
@test maximum(column_colors) < size(A, 2) ÷ 2
19+
@testset "Column coloring" begin
20+
for A in (sprand(Bool, 100, 200, 0.05), sprand(Bool, 200, 100, 0.05))
21+
column_colors = ADTypes.column_coloring(A, alg)
22+
@test DIT.check_structurally_orthogonal_columns(A, column_colors)
23+
@test minimum(column_colors) == 1
24+
@test maximum(column_colors) < size(A, 2) ÷ 2
25+
end
26+
end
1427

15-
row_colors = ADTypes.row_coloring(A, alg)
16-
@test DIT.check_structurally_orthogonal_rows(A, row_colors)
17-
@test maximum(row_colors) < size(A, 1) ÷ 2
28+
@testset "Row coloring" begin
29+
for A in (sprand(Bool, 100, 200, 0.05), sprand(Bool, 200, 100, 0.05))
30+
row_colors = ADTypes.row_coloring(A, alg)
31+
@test DIT.check_structurally_orthogonal_rows(A, row_colors)
32+
@test minimum(row_colors) == 1
33+
@test maximum(row_colors) < size(A, 1) ÷ 2
34+
end
35+
end
1836

19-
S = Symmetric(sprand(Bool, 100, 100, 0.05)) + I
20-
symmetric_colors = ADTypes.symmetric_coloring(S, alg)
21-
@test DIT.check_symmetrically_structurally_orthogonal(S, symmetric_colors)
22-
@test maximum(symmetric_colors) < size(A, 2) ÷ 2
37+
@testset "Symmetric coloring" begin
38+
S = Symmetric(sprand(Bool, 100, 100, 0.05)) + I
39+
symmetric_colors = ADTypes.symmetric_coloring(S, alg)
40+
@test DIT.check_symmetrically_structurally_orthogonal(S, symmetric_colors)
41+
@test minimum(symmetric_colors) == 1
42+
@test maximum(symmetric_colors) < size(S, 2) ÷ 2
43+
end

DifferentiationInterface/test/Internals/exceptions.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using ADTypes: ADTypes, AbstractADType
2+
using DifferentiationInterface
23
import DifferentiationInterface as DI
4+
using Test
35

46
"""
57
AutoBrokenForward <: ADTypes.AbstractADType
@@ -19,16 +21,20 @@ Used to test error messages.
1921
"""
2022
struct AutoBrokenReverse <: AbstractADType end
2123
ADTypes.mode(::AutoBrokenReverse) = ADTypes.ReverseMode()
22-
DI.check_available(::AutoBrokenReverse) = true
2324

2425
## Test exceptions
2526
@testset "MissingBackendError" begin
26-
f(x::AbstractArray) = sum(abs2, x)
27-
x = [1.0, 2.0, 3.0]
27+
x = [1.0]
28+
y = similar(x)
29+
dx = similar(x)
30+
dy = similar(y)
2831

29-
@test_throws DI.MissingBackendError gradient(f, AutoBrokenForward(), x)
30-
@test_throws DI.MissingBackendError gradient(f, AutoBrokenReverse(), x)
32+
@test_throws DI.MissingBackendError jacobian(copy, AutoBrokenForward(), x)
33+
@test_throws DI.MissingBackendError jacobian(copy, AutoBrokenReverse(), x)
3134

32-
@test_throws DI.MissingBackendError hvp(f, AutoBrokenForward(), x, x)
33-
@test_throws DI.MissingBackendError hvp(f, AutoBrokenReverse(), x, x)
35+
@test_throws DI.MissingBackendError jacobian(copyto!, y, AutoBrokenForward(), x)
36+
@test_throws DI.MissingBackendError jacobian(copyto!, y, AutoBrokenReverse(), x)
37+
38+
@test_throws DI.MissingBackendError hessian(sum, AutoBrokenForward(), x)
39+
@test_throws DI.MissingBackendError hessian(sum, AutoBrokenReverse(), x)
3440
end

0 commit comments

Comments
 (0)