Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/api/Config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,17 @@ function HessianConfig(result::DiffResult, input::AbstractArray, gtp::Instructio
gcfg = GradientConfig(jcfg.input, gtp)
return HessianConfig(gcfg, jcfg)
end

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

# nothing records onto the tapes of a config between calls, so a copy gets copies of them,
# and its tracked values move onto these copies
function Base.deepcopy_internal(cfg::AbstractConfig, dict::IdDict)
foreach(tp -> Base.deepcopy_internal(tp, dict), config_tapes(cfg))
return invoke(Base.deepcopy_internal, Tuple{Any, IdDict}, cfg, dict)
end

config_tapes(cfg::Union{GradientConfig, JacobianConfig}) = (cfg.tape,)
config_tapes(cfg::HessianConfig) = (cfg.gradient_config.tape, cfg.jacobian_config.tape)
4 changes: 2 additions & 2 deletions src/tracked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ Base.size(t::TrackedArray) = size(value(t))
Base.copy(t::TrackedArray) = t

# while recording, tracked values are numbers on the tape: copying them would detach them;
# afterwards they are copied together with their tape
# afterwards, or if their tape is copied as well, they are copied together with their tape
function Base.deepcopy_internal(t::Union{TrackedReal, TrackedArray}, dict::IdDict)
if isrecording(tape(t))
if isrecording(tape(t)) && !haskey(dict, tape(t))
return t
else
return invoke(Base.deepcopy_internal, Tuple{Any, IdDict}, t, dict)
Expand Down
22 changes: 22 additions & 0 deletions test/api/ConfigTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,25 @@ 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))

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

@testset "`deepcopy` of a config is independent" begin
x, y = [1.0, 2.0], [3.0, 4.0]
I2 = [2.0 0.0; 0.0 2.0]
cases = (
(cfg -> ReverseDiff.gradient!(zeros(2), x -> sum(abs2, x), x, cfg), GradientConfig(x), [2.0, 4.0]),
(cfg -> ReverseDiff.gradient!((zeros(2), zeros(2)), (x, y) -> sum(x .* y), (x, y), cfg), GradientConfig((x, y)), (y, x)),
(cfg -> ReverseDiff.jacobian!(zeros(2, 2), x -> 2 .* x, x, cfg), JacobianConfig(x), I2),
(cfg -> ReverseDiff.jacobian!(zeros(2, 2), (z, x) -> (z .= 2 .* x), zeros(2), x, cfg), JacobianConfig(zeros(2), x), I2),
(cfg -> ReverseDiff.hessian!(zeros(2, 2), x -> sum(abs2, x), x, cfg), HessianConfig(x), I2),
)
for (run, cfg, expected) in cases
copied = deepcopy(cfg)
@test run(copied) == expected
@test run(cfg) == expected
@test run(copied) == expected
end
end
Loading