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

Remove type annotations from aux calls #65

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
12 changes: 4 additions & 8 deletions src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ function value_and_derivative(
return value_and_derivative_aux(backend, f, x, extras, mode(backend))
end

function value_and_derivative_aux(
backend::AbstractADType, f, x::Number, extras, ::ForwardMode
)
function value_and_derivative_aux(backend, f, x, extras, ::ForwardMode)
return value_and_pushforward(backend, f, x, one(x), extras)
end

function value_and_derivative_aux(
backend::AbstractADType, f, x::Number, extras, ::ReverseMode
)
function value_and_derivative_aux(backend, f, x, extras, ::ReverseMode)
return value_and_pullback(backend, f, x, one(x), extras)
end

Expand All @@ -32,10 +28,10 @@ function derivative(
return derivative_aux(backend, f, x, extras, mode(backend))
end

function derivative_aux(backend::AbstractADType, f, x::Number, extras, ::ForwardMode)
function derivative_aux(backend, f, x, extras, ::ForwardMode)
return pushforward(backend, f, x, one(x), extras)
end

function derivative_aux(backend::AbstractADType, f, x::Number, extras, ::ReverseMode)
function derivative_aux(backend, f, x, extras, ::ReverseMode)
return pullback(backend, f, x, one(x), extras)
end
33 changes: 8 additions & 25 deletions src/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ function value_and_gradient!(
return value_and_gradient_aux!(grad, backend, f, x, extras, mode(backend))
end

function value_and_gradient_aux!(
grad::AbstractArray, backend::AbstractADType, f, x::AbstractArray, extras, ::ForwardMode
)
function value_and_gradient_aux!(grad, backend::AbstractADType, f, x, extras, ::ForwardMode)
y = f(x)
for j in eachindex(IndexCartesian(), grad)
dx_j = basisarray(backend, grad, j)
Expand All @@ -24,9 +22,7 @@ function value_and_gradient_aux!(
return y, grad
end

function value_and_gradient_aux!(
grad::AbstractArray, backend::AbstractADType, f, x::AbstractArray, extras, ::ReverseMode
)
function value_and_gradient_aux!(grad, backend, f, x, extras, ::ReverseMode)
return value_and_pullback!(grad, backend, f, x, one(eltype(x)), extras)
end

Expand All @@ -41,16 +37,12 @@ function value_and_gradient(
return value_and_gradient_aux(backend, f, x, extras, mode(backend))
end

function value_and_gradient_aux(
backend::AbstractADType, f, x::AbstractArray, extras, ::AbstractMode
)
function value_and_gradient_aux(backend, f, x, extras, ::AbstractMode)
grad = similar(x)
return value_and_gradient!(grad, backend, f, x, extras)
end

function value_and_gradient_aux(
backend::AbstractADType, f, x::AbstractArray, extras, ::ReverseMode
)
function value_and_gradient_aux(backend, f, x, extras, ::ReverseMode)
return value_and_pullback(backend, f, x, one(eltype(x)), extras)
end

Expand All @@ -69,20 +61,11 @@ function gradient!(
return gradient_aux!(grad, backend, f, x, extras, mode(backend))
end

function gradient_aux!(
grad::AbstractArray,
backend::AbstractADType,
f,
x::AbstractArray,
extras,
::AbstractMode,
)
function gradient_aux!(grad, backend, f, x, extras, ::AbstractMode)
return last(value_and_gradient!(grad, backend, f, x, extras))
end

function gradient_aux!(
grad::AbstractArray, backend::AbstractADType, f, x::AbstractArray, extras, ::ReverseMode
)
function gradient_aux!(grad, backend, f, x, extras, ::ReverseMode)
return pullback!(grad, backend, f, x, one(eltype(x)), extras)
end

Expand All @@ -97,10 +80,10 @@ function gradient(
return gradient_aux(backend, f, x, extras, mode(backend))
end

function gradient_aux(backend::AbstractADType, f, x::AbstractArray, extras, ::AbstractMode)
function gradient_aux(backend, f, x, extras, ::AbstractMode)
return last(value_and_gradient(backend, f, x, extras))
end

function gradient_aux(backend::AbstractADType, f, x::AbstractArray, extras, ::ReverseMode)
function gradient_aux(backend, f, x, extras, ::ReverseMode)
return pullback(backend, f, x, one(eltype(x)), extras)
end
18 changes: 2 additions & 16 deletions src/hessian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ function value_gradient_and_hessian!(
end

function value_gradient_and_hessian_aux!(
grad::AbstractArray,
hess::AbstractMatrix,
backend::SecondOrder,
f,
x::AbstractArray,
extras,
::AbstractMode,
::ForwardMode,
grad, hess, backend, f, x, extras, ::AbstractMode, ::ForwardMode
)
y = f(x)
check_hess(hess, x)
Expand All @@ -68,14 +61,7 @@ function value_gradient_and_hessian_aux!(
end

function value_gradient_and_hessian_aux!(
grad::AbstractArray,
hess::AbstractMatrix,
backend::SecondOrder,
f,
x::AbstractArray,
extras,
::AbstractMode,
::ReverseMode,
grad, hess, backend, f, x, extras, ::AbstractMode, ::ReverseMode
)
y, _ = value_and_gradient!(grad, inner(backend), f, x, extras)
check_hess(hess, x)
Expand Down
93 changes: 10 additions & 83 deletions src/hessian_vector_product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,14 @@ function gradient_and_hessian_vector_product(
end

function gradient_and_hessian_vector_product_aux(
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::AbstractMode,
::ForwardMode,
backend, f, x, v, extras, ::AbstractMode, ::ForwardMode
)
grad_aux(z) = gradient(inner(backend), f, z, extras)
return value_and_pushforward(outer(backend), grad_aux, x, v, extras)
end

function gradient_and_hessian_vector_product_aux(
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::AbstractMode,
::ReverseMode,
backend, f, x, v, extras, ::AbstractMode, ::ReverseMode
)
throw(ArgumentError("HVP must be computed without gradient for reverse-over-something"))
end
Expand Down Expand Up @@ -102,15 +90,7 @@ function gradient_and_hessian_vector_product!(
end

function gradient_and_hessian_vector_product_aux!(
grad::AbstractArray,
hvp::AbstractArray,
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::AbstractMode,
::ForwardMode,
grad, hvp, backend, f, x, v, extras, ::AbstractMode, ::ForwardMode
)
function grad_aux!(storage, z)
gradient!(storage, inner(backend), f, z, extras)
Expand All @@ -120,15 +100,7 @@ function gradient_and_hessian_vector_product_aux!(
end

function gradient_and_hessian_vector_product_aux!(
grad::AbstractArray,
hvp::AbstractArray,
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::AbstractMode,
::ReverseMode,
grad, hvp, backend, f, x, v, extras, ::AbstractMode, ::ReverseMode
)
throw(ArgumentError("HVP must be computed without gradient for reverse-over-something"))
end
Expand Down Expand Up @@ -162,41 +134,17 @@ function hessian_vector_product(
)
end

function hessian_vector_product_aux(
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::ReverseMode,
::ReverseMode,
)
function hessian_vector_product_aux(backend, f, x, v, extras, ::ReverseMode, ::ReverseMode)
dotgrad_aux(z) = dot(gradient(inner(backend), f, z, extras), v)
return gradient(outer(backend), dotgrad_aux, x, extras)
end

function hessian_vector_product_aux(
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::ForwardMode,
::ReverseMode,
)
function hessian_vector_product_aux(backend, f, x, v, extras, ::ForwardMode, ::ReverseMode)
jvp_aux(z) = pushforward(inner(backend), f, z, v, extras)
return gradient(outer(backend), jvp_aux, x, extras)
end

function hessian_vector_product_aux(
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::AbstractMode,
::ForwardMode,
)
function hessian_vector_product_aux(backend, f, x, v, extras, ::AbstractMode, ::ForwardMode)
_, hvp = gradient_and_hessian_vector_product(backend, f, x, v, extras)
return hvp
end
Expand Down Expand Up @@ -231,42 +179,21 @@ function hessian_vector_product!(
end

function hessian_vector_product_aux!(
hvp::AbstractArray,
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::ReverseMode,
::ReverseMode,
hvp, backend, f, x, v, extras, ::ReverseMode, ::ReverseMode
)
dotgrad_aux(z) = dot(gradient(inner(backend), f, z, extras), v) # allocates
return gradient!(hvp, outer(backend), dotgrad_aux, x, extras)
end

function hessian_vector_product_aux!(
hvp::AbstractArray,
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::ForwardMode,
::ReverseMode,
hvp, backend, f, x, v, extras, ::ForwardMode, ::ReverseMode
)
jvp_aux(z) = pushforward(inner(backend), f, z, v, extras)
return gradient!(hvp, outer(backend), jvp_aux, x, extras)
end

function hessian_vector_product_aux!(
hvp::AbstractArray,
backend::SecondOrder,
f,
x::AbstractArray,
v::AbstractArray,
extras,
::AbstractMode,
::ForwardMode,
hvp, backend, f, x, v, extras, ::AbstractMode, ::ForwardMode
)
grad = similar(x) # allocates
_, hvp = gradient_and_hessian_vector_product!(grad, hvp, backend, f, x, v, extras)
Expand Down
28 changes: 4 additions & 24 deletions src/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ function value_and_jacobian!(
return value_and_jacobian_aux!(y, jac, backend, f, x, extras, mode(backend))
end

function value_and_jacobian_aux!(
jac::AbstractMatrix, backend::AbstractADType, f, x::AbstractArray, extras, ::ForwardMode
)
function value_and_jacobian_aux!(jac, backend, f, x, extras, ::ForwardMode)
y = f(x)
check_jac(jac, x, y)
for (k, j) in enumerate(eachindex(IndexCartesian(), x))
Expand All @@ -55,15 +53,7 @@ function value_and_jacobian_aux!(
return y, jac
end

function value_and_jacobian_aux!(
y::AbstractArray,
jac::AbstractMatrix,
backend::AbstractADType,
f!,
x::AbstractArray,
extras,
::ForwardMode,
)
function value_and_jacobian_aux!(y, jac, backend, f!, x, extras, ::ForwardMode)
check_jac(jac, x, y)
for (k, j) in enumerate(eachindex(IndexCartesian(), x))
dx_j = basisarray(backend, x, j)
Expand All @@ -73,9 +63,7 @@ function value_and_jacobian_aux!(
return y, jac
end

function value_and_jacobian_aux!(
jac::AbstractMatrix, backend::AbstractADType, f, x::AbstractArray, extras, ::ReverseMode
)
function value_and_jacobian_aux!(jac, backend, f, x, extras, ::ReverseMode)
y = f(x)
check_jac(jac, x, y)
for (k, i) in enumerate(eachindex(IndexCartesian(), y))
Expand All @@ -86,15 +74,7 @@ function value_and_jacobian_aux!(
return y, jac
end

function value_and_jacobian_aux!(
y::AbstractArray,
jac::AbstractMatrix,
backend::AbstractADType,
f!,
x::AbstractArray,
extras,
::ReverseMode,
)
function value_and_jacobian_aux!(y, jac, backend, f!, x, extras, ::ReverseMode)
check_jac(jac, x, y)
for (k, i) in enumerate(eachindex(IndexCartesian(), y))
dy_i = basisarray(backend, y, i)
Expand Down
Loading
Loading