Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allocation benchmarks on StaticArrays #580

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ function compute_ydual_onearg(
tx::NTuple,
contexts::Vararg{Context,C},
) where {F,T,C}
(; xdual_tmp) = prep
make_dual!(T, xdual_tmp, x, tx)
# (; xdual_tmp) = prep
# make_dual!(T, xdual_tmp, x, tx)
xdual_tmp = make_dual(T, x, tx) # TODO: discuss reuse of mutable dual array
ydual = f(xdual_tmp, map(unwrap, contexts)...)
return ydual
end
Expand Down
17 changes: 17 additions & 0 deletions DifferentiationInterface/test/Back/ForwardDiff/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Pkg.add("ForwardDiff")

using ComponentArrays: ComponentArrays
using DifferentiationInterface, DifferentiationInterfaceTest
import DifferentiationInterfaceTest as DIT
using ForwardDiff: ForwardDiff
using StaticArrays: StaticArrays
using Test
Expand Down Expand Up @@ -57,3 +58,19 @@ test_differentiation(
## Static

test_differentiation(AutoForwardDiff(), static_scenarios(); logging=LOGGING)

@testset verbose = true "No allocations on StaticArrays" begin
filtered_static_scenarios = filter(static_scenarios(; include_batchified=false)) do scen
DIT.function_place(scen) == :out && DIT.operator_place(scen) == :out
end
data = benchmark_differentiation(
AutoForwardDiff(),
filtered_static_scenarios;
benchmark=:prepared,
excluded=[:hessian, :pullback], # TODO: figure this out
logging=LOGGING,
)
@testset "$(row[:scenario])" for row in eachrow(data)
@test row[:allocs] == 0
end
end;
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@ using SparseArrays: SparseArrays, SparseMatrixCSC, nnz, spdiagm
using StaticArrays: MArray, MMatrix, MVector, SArray, SMatrix, SVector

mySArray(f::Function) = f
myMArray(f::Function) = f

mySArray(::DIT.NumToArr{A}) where {T,A<:AbstractVector{T}} = DIT.NumToArr(SVector{6,T})
myMArray(::DIT.NumToArr{A}) where {T,A<:AbstractVector{T}} = DIT.NumToArr(MVector{6,T})

mySArray(::DIT.NumToArr{A}) where {T,A<:AbstractMatrix{T}} = DIT.NumToArr(SMatrix{2,3,T,6})
myMArray(::DIT.NumToArr{A}) where {T,A<:AbstractMatrix{T}} = DIT.NumToArr(MMatrix{2,3,T,6})

mySArray(f::DIT.MultiplyByConstant) = f
myMArray(f::DIT.MultiplyByConstant) = f

mySArray(f::DIT.WritableClosure) = f
myMArray(f::DIT.WritableClosure) = f

mySArray(x::Number) = x
myMArray(x::Number) = x
Expand All @@ -36,13 +27,8 @@ function myMArray(x::AbstractMatrix{T}) where {T}
end

mySArray(x::Tuple) = map(mySArray, x)
myMArray(x::Tuple) = map(myMArray, x)

mySArray(x::DI.Constant) = DI.Constant(mySArray(DI.unwrap(x)))
myMArray(x::DI.Constant) = DI.Constant(myMArray(DI.unwrap(x)))

mySArray(::Nothing) = nothing
myMArray(::Nothing) = nothing

function mySArray(scen::Scenario{op,pl_op,pl_fun}) where {op,pl_op,pl_fun}
(; f, x, y, tang, contexts, res1, res2) = scen
Expand All @@ -57,22 +43,9 @@ function mySArray(scen::Scenario{op,pl_op,pl_fun}) where {op,pl_op,pl_fun}
)
end

function myMArray(scen::Scenario{op,pl_op,pl_fun}) where {op,pl_op,pl_fun}
(; f, x, y, tang, contexts, res1, res2) = scen
return Scenario{op,pl_op,pl_fun}(
myMArray(f);
x=myMArray(x),
y=pl_fun == :in ? myMArray(y) : myMArray(y),
tang=myMArray(tang),
contexts=myMArray(contexts),
res1=myMArray(res1),
res2=myMArray(res2),
)
end

function DIT.static_scenarios(args...; kwargs...)
scens = DIT.default_scenarios(args...; kwargs...)
return vcat(mySArray.(scens), myMArray.(scens))
return mySArray.(scens)
end

end
4 changes: 2 additions & 2 deletions DifferentiationInterfaceTest/src/scenarios/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ end

## Number to array

multiplicator(::Type{A}) where {A<:AbstractVector} = convert(A, float.(1:6))
multiplicator(::Type{A}) where {A<:AbstractMatrix} = convert(A, reshape(float.(1:6), 2, 3))
multiplicator(::Type{A}) where {A<:AbstractVector} = convert(A, 1:6)
multiplicator(::Type{A}) where {A<:AbstractMatrix} = convert(A, reshape(1:6, 2, 3))

struct NumToArr{A} end
NumToArr(::Type{A}) where {A} = NumToArr{A}()
Expand Down
Loading