Skip to content

Commit

Permalink
Tests for no alloc and reduced calls in (sparse) ForwardDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Apr 29, 2024
1 parent 17cdd2a commit 720d7c5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DifferentiationInterface/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ julia = "1.10"
[extras]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Diffractor = "9f5e2b26-1114-432f-b630-d3fe2085c51c"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
Expand All @@ -89,6 +90,7 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
test = [
"ADTypes",
"Aqua",
"DataFrames",
"Diffractor",
"Documenter",
"Enzyme",
Expand Down
77 changes: 77 additions & 0 deletions DifferentiationInterface/test/bonus/efficiency.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@testset verbose = false "ForwardDiff" begin
# derivative and gradient for `f(x)`

results1 = benchmark_differentiation(
[AutoForwardDiff()];
outofplace=false,
twoarg=false,
input_type=Union{Number,AbstractVector},
output_type=Number,
second_order=false,
excluded=[PullbackScenario],
logging=get(ENV, "CI", "false") == "false",
)

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

results2 = benchmark_differentiation(
[AutoForwardDiff()];
outofplace=false,
onearg=false,
input_type=Union{Number,AbstractVector},
output_type=AbstractVector,
second_order=false,
excluded=[PullbackScenario],
logging=get(ENV, "CI", "false") == "false",
)

data = vcat(DataFrame(results1), DataFrame(results2))

useless_rows =
startswith.(string.(data[!, :operator]), Ref("prepare")) .|
startswith.(string.(data[!, :operator]), Ref("value_and"))

useful_data = data[.!useless_rows, :]

@testset "$(row[:operator]) - $(row[:func]) : $(row[:input_type]) -> $(row[:output_type])" for row in
eachrow(
useful_data
)
@test row[:allocs] == 0
end
end

@testset verbose = false "Sparse ForwardDiff" begin
# sparse jacobian for f!(x, y)

b_sparse = AutoSparse(
AutoForwardDiff(; chunksize=1);
sparsity_detector=DI.SymbolicsSparsityDetector(),
coloring_algorithm=DI.GreedyColoringAlgorithm(),
)

results1 = benchmark_differentiation(
[b_sparse],
sparse_scenarios();
input_type=AbstractVector,
output_type=AbstractVector,
outofplace=false,
onearg=false,
second_order=false,
logging=get(ENV, "CI", "false") == "false",
)

data = vcat(DataFrame(results1))

useless_rows = startswith.(string.(data[!, :operator]), Ref("prepare"))

useful_data = data[.!useless_rows, :]

@testset "$(row[:operator]) - $(row[:func]) : $(row[:input_type]) -> $(row[:output_type])" for row in
eachrow(
useful_data
)
@test row[:allocs] == 0
@test row[:calls] < prod(row[:input_size])
end
end
8 changes: 6 additions & 2 deletions DifferentiationInterface/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ include("test_imports.jl")

@testset verbose = true "Bonus round" begin
@testset "Type stability" begin
include("type_stability.jl")
include("bonus/type_stability.jl")
end

@testset "Efficiency" begin
include("bonus/efficiency.jl")
end

@testset "Weird arrays" begin
include("weird_arrays.jl")
include("bonus/weird_arrays.jl")
end
end

Expand Down
1 change: 1 addition & 0 deletions DifferentiationInterface/test/test_imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using JET: JET
using JuliaFormatter: JuliaFormatter
using Test

using DataFrames: DataFrame
using LinearAlgebra
using SparseArrays

Expand Down

0 comments on commit 720d7c5

Please sign in to comment.