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

Optimize Enzyme gradient #515

Merged
merged 2 commits into from
Sep 30, 2024
Merged
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 @@ -210,82 +210,138 @@ end

## Gradient

### Without preparation

function DI.gradient(
f::F,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
grad = make_zero(x)
autodiff(
reverse_noprimal(backend),
f_and_df,
Active,
Duplicated(x, grad),
map(translate, contexts)...,
)
return grad
end

function DI.value_and_gradient(
f::F,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
grad = make_zero(x)
_, y = autodiff(
reverse_withprimal(backend),
f_and_df,
Active,
Duplicated(x, grad),
map(translate, contexts)...,
)
return y, grad
end

### With preparation

struct EnzymeGradientPrep{G} <: GradientPrep
grad_righttype::G
end

function DI.prepare_gradient(
f::F,
::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
return NoGradientPrep()
grad_righttype = make_zero(x)
return EnzymeGradientPrep(grad_righttype)
end

function DI.gradient(
f::F,
::NoGradientPrep,
::EnzymeGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
derivs = gradient(reverse_noprimal(backend), f_and_df, x, map(translate, contexts)...)
return first(derivs)
grad = make_zero(x)
autodiff(
reverse_noprimal(backend),
f_and_df,
Duplicated(x, grad),
map(translate, contexts)...,
)
return grad
end

function DI.gradient!(
f::F,
grad,
::NoGradientPrep,
prep::EnzymeGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
dx_righttype = convert(typeof(x), grad)
make_zero!(dx_righttype)
grad_righttype = grad isa typeof(x) ? grad : prep.grad_righttype
make_zero!(grad_righttype)
autodiff(
reverse_noprimal(backend),
f_and_df,
Active,
Duplicated(x, dx_righttype),
Duplicated(x, grad_righttype),
map(translate, contexts)...,
)
dx_righttype === grad || copyto!(grad, dx_righttype)
grad isa typeof(x) || copyto!(grad, grad_righttype)
return grad
end

function DI.value_and_gradient(
f::F,
::NoGradientPrep,
::EnzymeGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
(; derivs, val) = gradient(
reverse_withprimal(backend), f_and_df, x, map(translate, contexts)...
grad = make_zero(x)
_, y = autodiff(
reverse_withprimal(backend),
f_and_df,
Active,
Duplicated(x, grad),
map(translate, contexts)...,
)
return val, first(derivs)
return y, grad
end

function DI.value_and_gradient!(
f::F,
grad,
::NoGradientPrep,
prep::EnzymeGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
dx_righttype = convert(typeof(x), grad)
make_zero!(dx_righttype)
grad_righttype = grad isa typeof(x) ? grad : prep.grad_righttype
make_zero!(grad_righttype)
_, y = autodiff(
reverse_withprimal(backend),
f_and_df,
Active,
Duplicated(x, dx_righttype),
Duplicated(x, grad_righttype),
map(translate, contexts)...,
)
dx_righttype === grad || copyto!(grad, dx_righttype)
grad isa typeof(x) || copyto!(grad, grad_righttype)
return y, grad
end

Expand Down
Loading