Skip to content

Commit 2cac72b

Browse files
authored
Tests for no alloc and reduced calls in (sparse) ForwardDiff (#233)
1 parent 17cdd2a commit 2cac72b

6 files changed

Lines changed: 86 additions & 2 deletions

File tree

DifferentiationInterface/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ julia = "1.10"
6666
[extras]
6767
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
6868
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
69+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
6970
Diffractor = "9f5e2b26-1114-432f-b630-d3fe2085c51c"
7071
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
7172
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
@@ -89,6 +90,7 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
8990
test = [
9091
"ADTypes",
9192
"Aqua",
93+
"DataFrames",
9294
"Diffractor",
9395
"Documenter",
9496
"Enzyme",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
@testset verbose = false "ForwardDiff" begin
2+
# derivative and gradient for `f(x)`
3+
4+
results1 = benchmark_differentiation(
5+
[AutoForwardDiff()];
6+
outofplace=false,
7+
twoarg=false,
8+
input_type=Union{Number,AbstractVector},
9+
output_type=Number,
10+
second_order=false,
11+
excluded=[PullbackScenario],
12+
logging=get(ENV, "CI", "false") == "false",
13+
)
14+
15+
# derivative and jacobian for f!(x, y)
16+
17+
results2 = benchmark_differentiation(
18+
[AutoForwardDiff()];
19+
outofplace=false,
20+
onearg=false,
21+
input_type=Union{Number,AbstractVector},
22+
output_type=AbstractVector,
23+
second_order=false,
24+
excluded=[PullbackScenario],
25+
logging=get(ENV, "CI", "false") == "false",
26+
)
27+
28+
data = vcat(DataFrame(results1), DataFrame(results2))
29+
30+
useless_rows =
31+
startswith.(string.(data[!, :operator]), Ref("prepare")) .|
32+
startswith.(string.(data[!, :operator]), Ref("value_and"))
33+
34+
useful_data = data[.!useless_rows, :]
35+
36+
@testset "$(row[:operator]) - $(row[:func]) : $(row[:input_type]) -> $(row[:output_type])" for row in
37+
eachrow(
38+
useful_data
39+
)
40+
@test row[:allocs] == 0
41+
end
42+
end
43+
44+
@testset verbose = false "Sparse ForwardDiff" begin
45+
# sparse jacobian for f!(x, y)
46+
47+
b_sparse = AutoSparse(
48+
AutoForwardDiff(; chunksize=1);
49+
sparsity_detector=DI.SymbolicsSparsityDetector(),
50+
coloring_algorithm=DI.GreedyColoringAlgorithm(),
51+
)
52+
53+
results1 = benchmark_differentiation(
54+
[b_sparse],
55+
sparse_scenarios();
56+
input_type=AbstractVector,
57+
output_type=AbstractVector,
58+
outofplace=false,
59+
onearg=false,
60+
second_order=false,
61+
logging=get(ENV, "CI", "false") == "false",
62+
)
63+
64+
data = vcat(DataFrame(results1))
65+
66+
useless_rows = startswith.(string.(data[!, :operator]), Ref("prepare"))
67+
68+
useful_data = data[.!useless_rows, :]
69+
70+
@testset "$(row[:operator]) - $(row[:func]) : $(row[:input_type]) -> $(row[:output_type])" for row in
71+
eachrow(
72+
useful_data
73+
)
74+
@test row[:allocs] == 0
75+
@test row[:calls] < prod(row[:input_size])
76+
end
77+
end
File renamed without changes.
File renamed without changes.

DifferentiationInterface/test/runtests.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ include("test_imports.jl")
4141

4242
@testset verbose = true "Bonus round" begin
4343
@testset "Type stability" begin
44-
include("type_stability.jl")
44+
include("bonus/type_stability.jl")
45+
end
46+
47+
@testset "Efficiency" begin
48+
include("bonus/efficiency.jl")
4549
end
4650

4751
@testset "Weird arrays" begin
48-
include("weird_arrays.jl")
52+
include("bonus/weird_arrays.jl")
4953
end
5054
end
5155

DifferentiationInterface/test/test_imports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using JET: JET
1818
using JuliaFormatter: JuliaFormatter
1919
using Test
2020

21+
using DataFrames: DataFrame
2122
using LinearAlgebra
2223
using SparseArrays
2324

0 commit comments

Comments
 (0)