Skip to content

Commit

Permalink
Fix size-1 Jacobian in Enzyme
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Jul 22, 2024
1 parent 5419f9a commit 25797a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ using Enzyme:
gradient!,
jacobian,
make_zero,
make_zero!
make_zero!,
onehot

struct AutoDeferredEnzyme{M} <: ADTypes.AbstractADType
mode::M
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ end

function DI.prepare_jacobian(f, backend::AutoEnzyme{<:Union{ForwardMode,Nothing}}, x)
B = pick_batchsize(backend, length(x))
shadow = chunkedonehot(x, Val(B))
if B == 1
shadow = onehot(x)
else
shadow = chunkedonehot(x, Val(B))
end
return EnzymeForwardOneArgJacobianExtras{B,typeof(shadow)}(shadow)
end

Expand Down
30 changes: 29 additions & 1 deletion DifferentiationInterfaceTest/src/scenarios/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,40 @@ function num_to_num_scenarios_onearg(x::Number; dx::Number, dy::Number)
der2 = num_to_num_second_derivative(x)

# everyone out of place
return [
scens = Scenario[
PushforwardScenario(f; x, y, dx, dy=dy_from_dx, nb_args, place),
PullbackScenario(f; x, y, dy, dx=dx_from_dy, nb_args, place),
DerivativeScenario(f; x, y, der, nb_args, place),
SecondDerivativeScenario(f; x, y, der, der2, nb_args, place),
]

# add scenarios [x] -> [y] to test 1-sized everything
f_vec(x) = [f(only(x))]
f_vec!(y, x) = y[only(eachindex(y))] = f(only(x))

for place in (:outofplace, :inplace)
append!(
scens,
[
PushforwardScenario(
f_vec; x=[x], y=[y], dx=[dx], dy=[dy_from_dx], nb_args=1, place
),
PushforwardScenario(
f_vec!; x=[x], y=[y], dx=[dx], dy=[dy_from_dx], nb_args=2, place
),
PullbackScenario(
f_vec; x=[x], y=[y], dy=[dy], dx=[dx_from_dy], nb_args=1, place
),
PullbackScenario(
f_vec!; x=[x], y=[y], dy=[dy], dx=[dx_from_dy], nb_args=2, place
),
JacobianScenario(f_vec; x=[x], y=[y], jac=[der;;], nb_args=1, place),
JacobianScenario(f_vec!; x=[x], y=[y], jac=[der;;], nb_args=2, place),
],
)
end

return scens
end

## Number to array
Expand Down

0 comments on commit 25797a8

Please sign in to comment.