-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Secret preparation modifier for resizing (#521)
* Secret preparation modifier * Add docstrings
- Loading branch information
Showing
12 changed files
with
547 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
for op in [ | ||
:derivative, | ||
:gradient, | ||
:jacobian, | ||
:second_derivative, | ||
:hessian, | ||
:pushforward, | ||
:pullback, | ||
:hvp, | ||
] | ||
op! = Symbol(op, "!") | ||
val_and_op = if op == :second_derivative | ||
:value_derivative_and_second_derivative | ||
elseif op == :hessian | ||
:value_gradient_and_hessian | ||
elseif op == :hvp | ||
nothing | ||
else | ||
Symbol("value_and_", op) | ||
end | ||
val_and_op! = Symbol(val_and_op, "!") | ||
prep_op = Symbol("prepare_", op) | ||
prep_op! = Symbol("prepare!_", op) | ||
prep_op_same_point = Symbol("prepare_", op, "_same_point") | ||
P = if op == :derivative | ||
DerivativePrep | ||
elseif op == :gradient | ||
GradientPrep | ||
elseif op == :jacobian | ||
JacobianPrep | ||
elseif op == :second_derivative | ||
SecondDerivativePrep | ||
elseif op == :hessian | ||
HessianPrep | ||
elseif op == :pushforward | ||
PushforwardPrep | ||
elseif op == :pullback | ||
PullbackPrep | ||
elseif op == :hvp | ||
HVPPrep | ||
end | ||
|
||
if op in (:derivative, :gradient, :jacobian) | ||
# 1-arg | ||
@eval function $prep_op!( | ||
f::F, ::$P, backend::AbstractADType, x, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
return $prep_op(f, backend, x, contexts...) | ||
end | ||
op == :gradient && continue | ||
# 2-arg | ||
@eval function $prep_op!( | ||
f!::F, y, ::$P, backend::AbstractADType, x, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
return $prep_op(f!, y, backend, x, contexts...) | ||
end | ||
|
||
elseif op in (:second_derivative, :hessian) | ||
# 1-arg | ||
@eval function $prep_op!( | ||
f::F, ::$P, backend::AbstractADType, x, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
return $prep_op(f, backend, x, contexts...) | ||
end | ||
|
||
elseif op in (:pushforward, :pullback, :hvp) | ||
# 1-arg | ||
@eval function $prep_op!( | ||
f::F, | ||
::$P, | ||
backend::AbstractADType, | ||
x, | ||
seed::NTuple, | ||
contexts::Vararg{Context,C}, | ||
) where {F,C} | ||
return $prep_op(f, backend, x, seed, contexts...) | ||
end | ||
@eval function $prep_op_same_point( | ||
f::F, | ||
prep::$P, | ||
backend::AbstractADType, | ||
x, | ||
seed::NTuple, | ||
contexts::Vararg{Context,C}, | ||
) where {F,C} | ||
return prep | ||
end | ||
@eval function $prep_op_same_point( | ||
f::F, backend::AbstractADType, x, seed::NTuple, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
prep = $prep_op(f, backend, x, seed, contexts...) | ||
return $prep_op_same_point(f, prep, backend, x, seed, contexts...) | ||
end | ||
op == :hvp && continue | ||
# 2-arg | ||
@eval function $prep_op!( | ||
f!::F, | ||
y, | ||
::$P, | ||
backend::AbstractADType, | ||
x, | ||
seed::NTuple, | ||
contexts::Vararg{Context,C}, | ||
) where {F,C} | ||
return $prep_op(f!, y, backend, x, seed, contexts...) | ||
end | ||
@eval function $prep_op_same_point( | ||
f!::F, | ||
y, | ||
prep::$P, | ||
backend::AbstractADType, | ||
x, | ||
seed::NTuple, | ||
contexts::Vararg{Context,C}, | ||
) where {F,C} | ||
return prep | ||
end | ||
@eval function $prep_op_same_point( | ||
f!::F, y, backend::AbstractADType, x, seed::NTuple, contexts::Vararg{Context,C} | ||
) where {F,C} | ||
prep = $prep_op(f!, y, backend, x, seed, contexts...) | ||
return $prep_op_same_point(f!, y, prep, backend, x, seed, contexts...) | ||
end | ||
end | ||
end |
Oops, something went wrong.