Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function DI.value_and_pullback(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
y = fc(x)
dotclosure(z, dy) = dot(fc(z), dy)
fc = DI.fix_tail(f, contexts_for_input(contexts, x)...)
fc_primal = DI.fix_tail(f, map(DI.unwrap, contexts)...)
y = fc_primal(x)
dotclosure(z, dy) = dot(fc(z), pullback_cotangent(contexts, dy))
tx = map(ty) do dy
if y isa Number
dy .* gradient(fc, x)
Expand All @@ -39,9 +40,10 @@ function DI.value_and_pullback!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
y = fc(x)
dotclosure(z, dy) = dot(fc(z), dy)
fc = DI.fix_tail(f, contexts_for_input(contexts, x)...)
fc_primal = DI.fix_tail(f, map(DI.unwrap, contexts)...)
y = fc_primal(x)
dotclosure(z, dy) = dot(fc(z), pullback_cotangent(contexts, dy))
for b in eachindex(tx, ty)
dx, dy = tx[b], ty[b]
if y isa Number
Expand Down Expand Up @@ -73,10 +75,11 @@ end

### Without contexts

struct ReverseDiffGradientPrep{SIG, C, T} <: DI.GradientPrep{SIG}
struct ReverseDiffGradientPrep{SIG, C, T, PC} <: DI.GradientPrep{SIG}
_sig::Val{SIG}
config::C
tape::T
prep_contexts::PC
end

function DI.prepare_gradient_nokwarg(
Expand All @@ -85,10 +88,10 @@ function DI.prepare_gradient_nokwarg(
_sig = DI.signature(f, backend, x; strict)
if compile
tape = ReverseDiff.compile(GradientTape(f, x))
return ReverseDiffGradientPrep(_sig, nothing, tape)
return ReverseDiffGradientPrep(_sig, nothing, tape, nothing)
else
config = GradientConfig(x)
return ReverseDiffGradientPrep(_sig, config, nothing)
return ReverseDiffGradientPrep(_sig, config, nothing, nothing)
end
end

Expand Down Expand Up @@ -148,7 +151,8 @@ function DI.prepare_gradient_nokwarg(
) where {C}
_sig = DI.signature(f, backend, x, contexts...; strict)
config = GradientConfig(x)
return ReverseDiffGradientPrep(_sig, config, nothing)
prep_contexts = translate_toprep(eltype(config.input), contexts)
return ReverseDiffGradientPrep(_sig, config, nothing, prep_contexts)
end

function DI.value_and_gradient!(
Expand All @@ -160,7 +164,7 @@ function DI.value_and_gradient!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
result = MutableDiffResult(zero(eltype(x)), (grad,)) # ReverseDiff#251
result = gradient!(result, fc, x, prep.config)
return DR.value(result), grad # ReverseDiff#269
Expand All @@ -174,7 +178,7 @@ function DI.value_and_gradient(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
# GradientResult tries to mutate an SArray
result = MutableDiffResult(zero(eltype(x)), (similar(x),))
result = gradient!(result, fc, x, prep.config)
Expand All @@ -190,7 +194,7 @@ function DI.gradient!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return gradient!(grad, fc, x, prep.config)
end

Expand All @@ -202,18 +206,19 @@ function DI.gradient(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return gradient(fc, x, prep.config)
end

## Jacobian

### Without contexts

struct ReverseDiffOneArgJacobianPrep{SIG, C, T} <: DI.JacobianPrep{SIG}
struct ReverseDiffOneArgJacobianPrep{SIG, C, T, PC} <: DI.JacobianPrep{SIG}
_sig::Val{SIG}
config::C
tape::T
prep_contexts::PC
end

function DI.prepare_jacobian_nokwarg(
Expand All @@ -222,10 +227,10 @@ function DI.prepare_jacobian_nokwarg(
_sig = DI.signature(f, backend, x; strict)
if compile
tape = ReverseDiff.compile(JacobianTape(f, x))
return ReverseDiffOneArgJacobianPrep(_sig, nothing, tape)
return ReverseDiffOneArgJacobianPrep(_sig, nothing, tape, nothing)
else
config = JacobianConfig(x)
return ReverseDiffOneArgJacobianPrep(_sig, config, nothing)
return ReverseDiffOneArgJacobianPrep(_sig, config, nothing, nothing)
end
end

Expand Down Expand Up @@ -285,7 +290,8 @@ function DI.prepare_jacobian_nokwarg(
) where {C}
_sig = DI.signature(f, backend, x, contexts...; strict)
config = JacobianConfig(x)
return ReverseDiffOneArgJacobianPrep(_sig, config, nothing)
prep_contexts = translate_toprep(eltype(config.input), contexts)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, the trick is to create an array of the appropriate TrackedReal element type? Shouldn't we rather have an TrackedArray? I'm not sure how these ReverseDiff internals work since they're not documented

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it's currently based on Array{TrackedReal}, not a ReverseDiff.TrackedArray. I will modify this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so it seems that using ReverseDiff.TrackedArray for the cache is "impossible" with the current design in ReverseDiff.jl. In short, the TrackedArray wrapper is immutable and doesn’t support element assignment, see here: https://github.com/JuliaDiff/ReverseDiff.jl/blob/2584eaa222f616b64ebbcb815b7ceae0622da281/src/tracked.jl#L430. Since the whole point of the Cache is to mutate its content, I don't see a workaround. The Copilot neither.

FYI, PreallocationTools.jl also use a Array{TrackedReal} for the cache.

return ReverseDiffOneArgJacobianPrep(_sig, config, nothing, prep_contexts)
end

function DI.value_and_jacobian!(
Expand All @@ -297,7 +303,7 @@ function DI.value_and_jacobian!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
y = fc(x)
result = DiffResult(y, (jac,))
result = jacobian!(result, fc, x, prep.config)
Expand All @@ -314,7 +320,7 @@ function DI.value_and_jacobian(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return fc(x), jacobian(fc, x, prep.config)
end

Expand All @@ -327,7 +333,7 @@ function DI.jacobian!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return jacobian!(jac, fc, x, prep.config)
end

Expand All @@ -339,19 +345,20 @@ function DI.jacobian(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return jacobian(fc, x, prep.config)
end

## Hessian

### Without contexts

struct ReverseDiffHessianPrep{SIG, G <: ReverseDiffGradientPrep, HC, HT} <: DI.HessianPrep{SIG}
struct ReverseDiffHessianPrep{SIG, G <: ReverseDiffGradientPrep, HC, HT, PC} <: DI.HessianPrep{SIG}
_sig::Val{SIG}
gradient_prep::G
hessian_config::HC
hessian_tape::HT
prep_contexts::PC
end

function DI.prepare_hessian_nokwarg(
Expand All @@ -361,10 +368,12 @@ function DI.prepare_hessian_nokwarg(
gradient_prep = DI.prepare_gradient_nokwarg(strict, f, backend, x)
if compile
hessian_tape = ReverseDiff.compile(HessianTape(f, x))
return ReverseDiffHessianPrep(_sig, gradient_prep, nothing, hessian_tape)
return ReverseDiffHessianPrep(_sig, gradient_prep, nothing, hessian_tape, nothing)
else
hessian_config = HessianConfig(x)
return ReverseDiffHessianPrep(_sig, gradient_prep, hessian_config, nothing)
return ReverseDiffHessianPrep(
_sig, gradient_prep, hessian_config, nothing, nothing
)
end
end

Expand Down Expand Up @@ -418,7 +427,12 @@ function DI.prepare_hessian_nokwarg(
_sig = DI.signature(f, backend, x, contexts...; strict)
gradient_prep = DI.prepare_gradient_nokwarg(strict, f, backend, x, contexts...)
hessian_config = HessianConfig(x)
return ReverseDiffHessianPrep(_sig, gradient_prep, hessian_config, nothing)
prep_contexts = translate_toprep(
eltype(hessian_config.gradient_config.input), contexts
)
return ReverseDiffHessianPrep(
_sig, gradient_prep, hessian_config, nothing, prep_contexts
)
end

function DI.hessian!(
Expand All @@ -430,7 +444,7 @@ function DI.hessian!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return hessian!(hess, fc, x, prep.hessian_config)
end

Expand All @@ -442,7 +456,7 @@ function DI.hessian(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f, prep, backend, x, contexts...)
fc = DI.fix_tail(f, map(DI.unwrap, contexts)...)
fc = DI.fix_tail(f, prepared_contexts(prep.prep_contexts, contexts)...)
return hessian(fc, x, prep.hessian_config)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ function DI.value_and_pullback(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, contexts_for_input(contexts, x)...)
fc_primal! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
function dotclosure(x, dy)
y_copy = similar(y, eltype(x))
fc!(y_copy, x)
return dot(y_copy, dy)
return dot(y_copy, pullback_cotangent(contexts, dy))
end
tx = map(ty) do dy
gradient(Fix2(dotclosure, dy), x)
end
fc!(y, x)
fc_primal!(y, x)
return y, tx
end

Expand All @@ -49,17 +50,18 @@ function DI.value_and_pullback!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, contexts_for_input(contexts, x)...)
fc_primal! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
function dotclosure(x, dy)
y_copy = similar(y, eltype(x))
fc!(y_copy, x)
return dot(y_copy, dy)
return dot(y_copy, pullback_cotangent(contexts, dy))
end
for b in eachindex(tx, ty)
dx, dy = tx[b], ty[b]
gradient!(dx, Fix2(dotclosure, dy), x)
end
fc!(y, x)
fc_primal!(y, x)
return y, tx
end

Expand All @@ -73,11 +75,11 @@ function DI.pullback(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, contexts_for_input(contexts, x)...)
function dotclosure(x, dy)
y_copy = similar(y, eltype(x))
fc!(y_copy, x)
return dot(y_copy, dy)
return dot(y_copy, pullback_cotangent(contexts, dy))
end
tx = map(ty) do dy
gradient(Fix2(dotclosure, dy), x)
Expand All @@ -96,11 +98,11 @@ function DI.pullback!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, contexts_for_input(contexts, x)...)
function dotclosure(x, dy)
y_copy = similar(y, eltype(x))
fc!(y_copy, x)
return dot(y_copy, dy)
return dot(y_copy, pullback_cotangent(contexts, dy))
end
for b in eachindex(tx, ty)
dx, dy = tx[b], ty[b]
Expand Down Expand Up @@ -133,10 +135,11 @@ end

### Without contexts

struct ReverseDiffTwoArgJacobianPrep{SIG, C, T} <: DI.JacobianPrep{SIG}
struct ReverseDiffTwoArgJacobianPrep{SIG, C, T, PC} <: DI.JacobianPrep{SIG}
_sig::Val{SIG}
config::C
tape::T
prep_contexts::PC
end

function DI.prepare_jacobian_nokwarg(
Expand All @@ -145,10 +148,10 @@ function DI.prepare_jacobian_nokwarg(
_sig = DI.signature(f!, y, backend, x; strict)
if compile
tape = ReverseDiff.compile(JacobianTape(f!, y, x))
return ReverseDiffTwoArgJacobianPrep(_sig, nothing, tape)
return ReverseDiffTwoArgJacobianPrep(_sig, nothing, tape, nothing)
else
config = JacobianConfig(y, x)
return ReverseDiffTwoArgJacobianPrep(_sig, config, nothing)
return ReverseDiffTwoArgJacobianPrep(_sig, config, nothing, nothing)
end
end

Expand Down Expand Up @@ -210,7 +213,8 @@ function DI.prepare_jacobian_nokwarg(
) where {C}
_sig = DI.signature(f!, y, backend, x, contexts...; strict)
config = JacobianConfig(y, x)
return ReverseDiffTwoArgJacobianPrep(_sig, config, nothing)
prep_contexts = translate_toprep(eltype(config.input), contexts)
return ReverseDiffTwoArgJacobianPrep(_sig, config, nothing, prep_contexts)
end

function DI.value_and_jacobian(
Expand All @@ -222,7 +226,7 @@ function DI.value_and_jacobian(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, prepared_contexts(prep.prep_contexts, contexts)...)
jac = similar(y, length(y), length(x))
result = MutableDiffResult(y, (jac,))
result = jacobian!(result, fc!, y, x, prep.config)
Expand All @@ -239,7 +243,7 @@ function DI.value_and_jacobian!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, prepared_contexts(prep.prep_contexts, contexts)...)
result = MutableDiffResult(y, (jac,))
result = jacobian!(result, fc!, y, x, prep.config)
return DiffResults.value(result), DiffResults.derivative(result)
Expand All @@ -254,7 +258,7 @@ function DI.jacobian(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, prepared_contexts(prep.prep_contexts, contexts)...)
jac = jacobian(fc!, y, x, prep.config)
return jac
end
Expand All @@ -269,7 +273,7 @@ function DI.jacobian!(
contexts::Vararg{DI.Context, C},
) where {C}
DI.check_prep(f!, y, prep, backend, x, contexts...)
fc! = DI.fix_tail(f!, map(DI.unwrap, contexts)...)
fc! = DI.fix_tail(f!, prepared_contexts(prep.prep_contexts, contexts)...)
jac = jacobian!(jac, fc!, y, x, prep.config)
return jac
end
Loading
Loading