Skip to content

Commit

Permalink
Deepcopy extras in benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Apr 29, 2024
1 parent 4f44e80 commit 500102e
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 194 deletions.
4 changes: 2 additions & 2 deletions DifferentiationInterfaceTest/docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ Note that we only compare (possibly) in-place operators, because they are always

```@example tuto
function formatter(v, i, j)
if j in (14, 15) # time, bytes
if j in (15, 16) # time, bytes
return Printf.@sprintf("%.1e", v)
elseif j == 16 # allocs
elseif j == 17 # allocs
return Printf.@sprintf("%.1f", v)
else
return v
Expand Down
8 changes: 4 additions & 4 deletions DifferentiationInterfaceTest/src/scenarios/component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ end
function comp_to_num_scenarios_onearg(x::ComponentVector)
# pushforward stays out of place
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PullbackScenario(comp_to_num; x=x, ref=comp_to_num_pullback, operator=op),
GradientScenario(comp_to_num; x=x, ref=comp_to_num_gradient, operator=op),
PullbackScenario(comp_to_num; x=x, ref=comp_to_num_pullback, place=place),
GradientScenario(comp_to_num; x=x, ref=comp_to_num_gradient, place=place),
],
)
end
Expand All @@ -32,7 +32,7 @@ function comp_to_num_scenarios_onearg(x::ComponentVector)
scens,
[
PushforwardScenario(
comp_to_num; x=x, ref=comp_to_num_pushforward, operator=op
comp_to_num; x=x, ref=comp_to_num_pushforward, place=place
),
],
)
Expand Down
114 changes: 55 additions & 59 deletions DifferentiationInterfaceTest/src/scenarios/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ num_to_num_pullback(x, dy) = num_to_num_derivative(x) * dy
function num_to_num_scenarios_onearg(x::Number)
# everyone out of place
return [
PushforwardScenario(
num_to_num; x=x, ref=num_to_num_pushforward, operator=:outofplace
),
PullbackScenario(num_to_num; x=x, ref=num_to_num_pullback, operator=:outofplace),
DerivativeScenario(
num_to_num; x=x, ref=num_to_num_derivative, operator=:outofplace
),
PushforwardScenario(num_to_num; x=x, ref=num_to_num_pushforward, place=:outofplace),
PullbackScenario(num_to_num; x=x, ref=num_to_num_pullback, place=:outofplace),
DerivativeScenario(num_to_num; x=x, ref=num_to_num_derivative, place=:outofplace),
SecondDerivativeScenario(
num_to_num; x=x, ref=num_to_num_second_derivative, operator=:outofplace
num_to_num; x=x, ref=num_to_num_second_derivative, place=:outofplace
),
]
end
Expand Down Expand Up @@ -72,28 +68,28 @@ end
function num_to_arr_scenarios_onearg(x::Number, a::AbstractArray)
# pullback stays out of place
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PushforwardScenario(
_num_to_arr(a); x=x, ref=_num_to_arr_pushforward(a), operator=op
_num_to_arr(a); x=x, ref=_num_to_arr_pushforward(a), place=place
),
DerivativeScenario(
_num_to_arr(a); x=x, ref=_num_to_arr_derivative(a), operator=op
_num_to_arr(a); x=x, ref=_num_to_arr_derivative(a), place=place
),
SecondDerivativeScenario(
_num_to_arr(a); x=x, ref=_num_to_arr_second_derivative(a), operator=op
_num_to_arr(a); x=x, ref=_num_to_arr_second_derivative(a), place=place
),
],
)
end
for op in (:outofplace,)
for place in (:outofplace,)
append!(
scens,
[
PullbackScenario(
_num_to_arr(a); x=x, ref=_num_to_arr_pullback(a), operator=op
_num_to_arr(a); x=x, ref=_num_to_arr_pullback(a), place=place
),
],
)
Expand All @@ -104,7 +100,7 @@ end
function num_to_arr_scenarios_twoarg(x::Number, a::AbstractArray)
# pullback stays out of place
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
Expand All @@ -113,19 +109,19 @@ function num_to_arr_scenarios_twoarg(x::Number, a::AbstractArray)
x=x,
y=similar(float.(a)),
ref=_num_to_arr_pushforward(a),
operator=op,
place=place,
),
DerivativeScenario(
_num_to_arr!(a);
x=x,
y=similar(float.(a)),
ref=_num_to_arr_derivative(a),
operator=op,
place=place,
),
],
)
end
for op in (:outofplace,)
for place in (:outofplace,)
append!(
scens,
[
Expand All @@ -134,7 +130,7 @@ function num_to_arr_scenarios_twoarg(x::Number, a::AbstractArray)
x=x,
y=similar(float.(a)),
ref=_num_to_arr_pullback(a),
operator=op,
place=place,
),
],
)
Expand All @@ -155,22 +151,22 @@ arr_to_num_hessian(x) = Matrix(Diagonal(-sin.(vec(x))))
function arr_to_num_scenarios_onearg(x::AbstractArray)
# pushforward stays out of place
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PullbackScenario(arr_to_num; x=x, ref=arr_to_num_pullback, operator=op),
GradientScenario(arr_to_num; x=x, ref=arr_to_num_gradient, operator=op),
GradientScenario(arr_to_num; x=x, ref=arr_to_num_gradient, operator=op),
HVPScenario(arr_to_num; x=x, ref=arr_to_num_hvp, operator=op),
HessianScenario(arr_to_num; x=x, ref=arr_to_num_hessian, operator=op),
PullbackScenario(arr_to_num; x=x, ref=arr_to_num_pullback, place=place),
GradientScenario(arr_to_num; x=x, ref=arr_to_num_gradient, place=place),
GradientScenario(arr_to_num; x=x, ref=arr_to_num_gradient, place=place),
HVPScenario(arr_to_num; x=x, ref=arr_to_num_hvp, place=place),
HessianScenario(arr_to_num; x=x, ref=arr_to_num_hessian, place=place),
],
)
end
for op in (:outofplace,)
for place in (:outofplace,)
append!(
scens,
[PushforwardScenario(arr_to_num; x=x, ref=arr_to_num_pushforward, operator=op)],
[PushforwardScenario(arr_to_num; x=x, ref=arr_to_num_pushforward, place=place)],
)
end
return scens
Expand All @@ -192,15 +188,15 @@ vec_to_vec_jacobian(x) = vcat(Diagonal(cos.(x)), Diagonal(-sin.(x)))

function vec_to_vec_scenarios_onearg(x::AbstractVector)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PushforwardScenario(
vec_to_vec; x=x, ref=vec_to_vec_pushforward, operator=op
vec_to_vec; x=x, ref=vec_to_vec_pushforward, place=place
),
PullbackScenario(vec_to_vec; x=x, ref=vec_to_vec_pullback, operator=op),
JacobianScenario(vec_to_vec; x=x, ref=vec_to_vec_jacobian, operator=op),
PullbackScenario(vec_to_vec; x=x, ref=vec_to_vec_pullback, place=place),
JacobianScenario(vec_to_vec; x=x, ref=vec_to_vec_jacobian, place=place),
],
)
end
Expand All @@ -210,7 +206,7 @@ end
function vec_to_vec_scenarios_twoarg(x::AbstractVector)
n = length(x)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
Expand All @@ -219,13 +215,13 @@ function vec_to_vec_scenarios_twoarg(x::AbstractVector)
x=x,
y=similar(x, 2n),
ref=vec_to_vec_pushforward,
operator=op,
place=place,
),
PullbackScenario(
vec_to_vec!; x=x, y=similar(x, 2n), ref=vec_to_vec_pullback, operator=op
vec_to_vec!; x=x, y=similar(x, 2n), ref=vec_to_vec_pullback, place=place
),
JacobianScenario(
vec_to_vec!; x=x, y=similar(x, 2n), ref=vec_to_vec_jacobian, operator=op
vec_to_vec!; x=x, y=similar(x, 2n), ref=vec_to_vec_jacobian, place=place
),
],
)
Expand All @@ -247,15 +243,15 @@ vec_to_mat_jacobian(x) = vcat(Diagonal(cos.(x)), Diagonal(-sin.(x)))

function vec_to_mat_scenarios_onearg(x::AbstractVector)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PushforwardScenario(
vec_to_mat; x=x, ref=vec_to_mat_pushforward, operator=op
vec_to_mat; x=x, ref=vec_to_mat_pushforward, place=place
),
PullbackScenario(vec_to_mat; x=x, ref=vec_to_mat_pullback, operator=op),
JacobianScenario(vec_to_mat; x=x, ref=vec_to_mat_jacobian, operator=op),
PullbackScenario(vec_to_mat; x=x, ref=vec_to_mat_pullback, place=place),
JacobianScenario(vec_to_mat; x=x, ref=vec_to_mat_jacobian, place=place),
],
)
end
Expand All @@ -265,7 +261,7 @@ end
function vec_to_mat_scenarios_twoarg(x::AbstractVector)
n = length(x)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
Expand All @@ -274,21 +270,21 @@ function vec_to_mat_scenarios_twoarg(x::AbstractVector)
x=x,
y=similar(x, n, 2),
ref=vec_to_mat_pushforward,
operator=op,
place=place,
),
PullbackScenario(
vec_to_mat!;
x=x,
y=similar(x, n, 2),
ref=vec_to_mat_pullback,
operator=op,
place=place,
),
JacobianScenario(
vec_to_mat!;
x=x,
y=similar(x, n, 2),
ref=vec_to_mat_jacobian,
operator=op,
place=place,
),
],
)
Expand Down Expand Up @@ -319,18 +315,18 @@ mat_to_vec_jacobian(x) = vcat(Diagonal(vec(cos.(x))), Diagonal(vec(-sin.(x))))
function mat_to_vec_scenarios_onearg(x::AbstractMatrix)
m, n = size(x)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PushforwardScenario(
mat_to_vec; x=x, ref=mat_to_vec_pushforward, operator=op
mat_to_vec; x=x, ref=mat_to_vec_pushforward, place=place
),
PullbackScenario(
mat_to_vec; x=randn(m, n), ref=mat_to_vec_pullback, operator=op
mat_to_vec; x=randn(m, n), ref=mat_to_vec_pullback, place=place
),
JacobianScenario(
mat_to_vec; x=randn(m, n), ref=mat_to_vec_jacobian, operator=op
mat_to_vec; x=randn(m, n), ref=mat_to_vec_jacobian, place=place
),
],
)
Expand All @@ -341,7 +337,7 @@ end
function mat_to_vec_scenarios_twoarg(x::AbstractMatrix)
m, n = size(x)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
Expand All @@ -350,21 +346,21 @@ function mat_to_vec_scenarios_twoarg(x::AbstractMatrix)
x=x,
y=similar(x, m * n * 2),
ref=mat_to_vec_pushforward,
operator=op,
place=place,
),
PullbackScenario(
mat_to_vec!;
x=x,
y=similar(x, m * n * 2),
ref=mat_to_vec_pullback,
operator=op,
place=place,
),
JacobianScenario(
mat_to_vec!;
x=x,
y=similar(x, m * n * 2),
ref=mat_to_vec_jacobian,
operator=op,
place=place,
),
],
)
Expand Down Expand Up @@ -393,15 +389,15 @@ mat_to_mat_jacobian(x) = vcat(Diagonal(vec(cos.(x))), Diagonal(vec(-sin.(x))))

function mat_to_mat_scenarios_onearg(x::AbstractMatrix)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
PushforwardScenario(
mat_to_mat; x=x, ref=mat_to_mat_pushforward, operator=op
mat_to_mat; x=x, ref=mat_to_mat_pushforward, place=place
),
PullbackScenario(mat_to_mat; x=x, ref=mat_to_mat_pullback, operator=op),
JacobianScenario(mat_to_mat; x=x, ref=mat_to_mat_jacobian, operator=op),
PullbackScenario(mat_to_mat; x=x, ref=mat_to_mat_pullback, place=place),
JacobianScenario(mat_to_mat; x=x, ref=mat_to_mat_jacobian, place=place),
],
)
end
Expand All @@ -411,7 +407,7 @@ end
function mat_to_mat_scenarios_twoarg(x::AbstractMatrix)
m, n = size(x)
scens = AbstractScenario[]
for op in (:outofplace, :inplace)
for place in (:outofplace, :inplace)
append!(
scens,
[
Expand All @@ -420,21 +416,21 @@ function mat_to_mat_scenarios_twoarg(x::AbstractMatrix)
x=x,
y=similar(x, m * n, 2),
ref=mat_to_mat_pushforward,
operator=op,
place=place,
),
PullbackScenario(
mat_to_mat!;
x=x,
y=similar(x, m * n, 2),
ref=mat_to_mat_pullback,
operator=op,
place=place,
),
JacobianScenario(
mat_to_mat!;
x=x,
y=similar(x, m * n, 2),
ref=mat_to_mat_jacobian,
operator=op,
place=place,
),
],
)
Expand Down
Loading

0 comments on commit 500102e

Please sign in to comment.