Skip to content
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
4 changes: 2 additions & 2 deletions src/api/Config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct GradientConfig{I} <: AbstractConfig
end

# "private" convienence constructor
_GradientConfig(input::I, tape::InstructionTape) where {I} = GradientConfig{I}(input, tape)
_GradientConfig(input::I, tape::InstructionTape) where {I} = GradientConfig{I}(input, finish_if_new!(tape))

"""
ReverseDiff.GradientConfig(input, tp::InstructionTape = InstructionTape())
Expand Down Expand Up @@ -63,7 +63,7 @@ struct JacobianConfig{I, O} <: AbstractConfig
end

# "private" convienence constructor
_JacobianConfig(input::I, output::O, tape::InstructionTape) where {I, O} = JacobianConfig{I, O}(input, output, tape)
_JacobianConfig(input::I, output::O, tape::InstructionTape) where {I, O} = JacobianConfig{I, O}(input, output, finish_if_new!(tape))

"""
ReverseDiff.JacobianConfig(input, tp::InstructionTape = InstructionTape())
Expand Down
4 changes: 2 additions & 2 deletions src/api/gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function gradient(f, input, cfg::GradientConfig = GradientConfig(input))
tape = GradientTape(f, input, cfg)
result = construct_result(input_hook(tape))
result = seeded_reverse_pass!(result, tape)
empty!(cfg.tape)
idle!(cfg.tape)
return result
end

Expand All @@ -41,7 +41,7 @@ the returned value: `result = ReverseDiff.gradient!(result, f, input, cfg)`.
function gradient!(result, f, input, cfg::GradientConfig = GradientConfig(input))
tape = GradientTape(f, input, cfg)
result = seeded_reverse_pass!(result, tape)
empty!(cfg.tape)
idle!(cfg.tape)
return result
end

Expand Down
8 changes: 4 additions & 4 deletions src/api/jacobians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function jacobian(f, input, cfg::JacobianConfig = JacobianConfig(input))
tape = JacobianTape(f, input, cfg)
isa(input, TrackedArray) && empty!(input.tape)
result = jacobian!(tape, input)
empty!(tape.tape)
idle!(tape.tape)
return result
end

Expand All @@ -43,7 +43,7 @@ function jacobian!(result, f, input, cfg::JacobianConfig = JacobianConfig(input)
tape = JacobianTape(f, input, cfg)
isa(input, TrackedArray) && empty!(input.tape)
result = jacobian!(result, tape, input)
empty!(tape.tape)
idle!(tape.tape)
return result
end

Expand All @@ -62,7 +62,7 @@ function jacobian(f!, output, input, cfg::JacobianConfig = JacobianConfig(output
isa(input, TrackedArray) && empty!(input.tape)
result = jacobian!(tape, input)
extract_result_value!(output, output_hook(tape))
empty!(tape.tape)
idle!(tape.tape)
return result
end

Expand All @@ -77,7 +77,7 @@ function jacobian!(result, f!, output, input, cfg::JacobianConfig = JacobianConf
isa(input, TrackedArray) && empty!(input.tape)
result = jacobian!(result, tape, input)
extract_result_value!(output, output_hook(tape))
empty!(tape.tape)
idle!(tape.tape)
return result
end

Expand Down
7 changes: 7 additions & 0 deletions src/api/tape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,15 @@ element type and shape as `input`.
See `ReverseDiff.gradient` for a description of acceptable types for `input`.
"""
function GradientTape(f, input, cfg::GradientConfig = GradientConfig(input))
reopen!(cfg.tape)
track!(cfg.input, input)
tracked_ouput = f(cfg.input)
finish!(cfg.tape)
return _GradientTape(f, cfg.input, tracked_ouput, cfg.tape)
end

function GradientTape(f, input::Tuple, cfg::GradientConfig = GradientConfig(input))
reopen!(cfg.tape)
for i in eachindex(cfg.input)
track!(cfg.input[i], input[i])
end
Expand All @@ -243,13 +245,15 @@ element type and shape as `input`.
See `ReverseDiff.jacobian` for a description of acceptable types for `input`.
"""
function JacobianTape(f, input, cfg::JacobianConfig = JacobianConfig(input))
reopen!(cfg.tape)
track!(cfg.input, input)
tracked_ouput = f(cfg.input)
finish!(cfg.tape)
return _JacobianTape(f, cfg.input, tracked_ouput, cfg.tape)
end

function JacobianTape(f, input::Tuple, cfg::JacobianConfig = JacobianConfig(input))
reopen!(cfg.tape)
for i in eachindex(cfg.input)
track!(cfg.input[i], input[i])
end
Expand All @@ -271,6 +275,7 @@ element type and shape as `input`.
See `ReverseDiff.jacobian` for a description of acceptable types for `input`.
"""
function JacobianTape(f!, output, input, cfg::JacobianConfig = JacobianConfig(output, input))
reopen!(cfg.tape)
track!(cfg.output, output, cfg.tape)
track!(cfg.input, input)
f!(cfg.output, cfg.input)
Expand All @@ -279,6 +284,7 @@ function JacobianTape(f!, output, input, cfg::JacobianConfig = JacobianConfig(ou
end

function JacobianTape(f!, output, input::Tuple, cfg::JacobianConfig = JacobianConfig(output, input))
reopen!(cfg.tape)
track!(cfg.output, output, cfg.tape)
for i in eachindex(input)
track!(cfg.input[i], input[i])
Expand Down Expand Up @@ -307,6 +313,7 @@ See `ReverseDiff.hessian` for a description of acceptable types for `input`.
function HessianTape(f, input, cfg::HessianConfig = HessianConfig(input))
gcfg = cfg.gradient_config
jcfg = cfg.jacobian_config
reopen!(jcfg.tape)
ht = _HessianTape(f, jcfg.input, similar(deriv(gcfg.input)), jcfg.tape)
track!(ht.input, input)
gt = GradientTape(f, ht.input, gcfg)
Expand Down
21 changes: 21 additions & 0 deletions src/tape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ function Base.empty!(tp::InstructionTape)
return tp
end

# The tape of a config is idle, i.e. finished and empty, between uses, so that a copy of the
# config gets its own tape: only tracked values on a recording tape are kept by `deepcopy`.

# finish a tape that has not recorded anything; tapes with recordings are left alone
function finish_if_new!(tp::InstructionTape)
if (@atomic tp.last) === nothing
finish!(tp)
end
return tp
end

# reopen an idle (finished and empty) tape for recording
function reopen!(tp::InstructionTape)
if !isrecording(tp) && isempty(tp.instructions)
empty!(tp)
end
return tp
end

idle!(tp::InstructionTape) = finish!(empty!(tp))

# undo the swap in `record!` so that the tape stays finished
@noinline function throw_finished(tp::InstructionTape)
@atomic tp.last = Finished()
Expand Down
44 changes: 44 additions & 0 deletions test/api/ConfigTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,47 @@ cfg = HessianConfig(DiffResults.HessianResult(y), x, gtp, jtp)
# These two act weird: work locally but fail in test mode
#@test issimilar(cfg.gradient_config, GradientConfig(track(x, Int), gtp))
#@test issimilar(cfg.jacobian_config, JacobianConfig(y, x, jtp))

#############
# recording #
#############

@testset "a config holding a recorded tape cannot record another" begin
x = [1.0, 2.0]
cfg = GradientConfig(x)
ReverseDiff.GradientTape(sum, x, cfg)
@test_throws ArgumentError ReverseDiff.GradientTape(sum, x, cfg)
end

############
# deepcopy #
############

# `MCMCThreads` deepcopies models, which may hold configs that were already used

sum_abs2(x) = sum(abs2, x)
double(x) = 2 .* x

@testset "`deepcopy` of a `GradientConfig`" begin
x = [1.0, 2.0]
cfg = GradientConfig(x)
@test ReverseDiff.gradient(sum_abs2, x, deepcopy(cfg)) == [2.0, 4.0]
@test ReverseDiff.gradient(sum_abs2, x, cfg) == [2.0, 4.0]
@test ReverseDiff.gradient(sum_abs2, x, deepcopy(cfg)) == [2.0, 4.0]
end

@testset "`deepcopy` of a `JacobianConfig`" begin
x = [1.0, 2.0]
cfg = JacobianConfig(x)
ReverseDiff.jacobian(double, x, cfg)
@test ReverseDiff.jacobian(double, x, deepcopy(cfg)) == [2.0 0.0; 0.0 2.0]
Comment thread
shravanngoswamii marked this conversation as resolved.
Resolved
@test ReverseDiff.jacobian(double, x, cfg) == [2.0 0.0; 0.0 2.0]
end

@testset "`deepcopy` of a `HessianConfig`" begin
x = [1.0, 2.0]
cfg = HessianConfig(x)
ReverseDiff.hessian(sum_abs2, x, cfg)
@test ReverseDiff.hessian(sum_abs2, x, deepcopy(cfg)) == [2.0 0.0; 0.0 2.0]
Comment thread
shravanngoswamii marked this conversation as resolved.
Resolved
@test ReverseDiff.hessian(sum_abs2, x, cfg) == [2.0 0.0; 0.0 2.0]
end
Loading