diff --git a/src/api/Config.jl b/src/api/Config.jl index afd2b26..1b04685 100644 --- a/src/api/Config.jl +++ b/src/api/Config.jl @@ -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) diff --git a/src/tracked.jl b/src/tracked.jl index 716b12f..264bcf9 100644 --- a/src/tracked.jl +++ b/src/tracked.jl @@ -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) diff --git a/test/api/ConfigTests.jl b/test/api/ConfigTests.jl index 5c6971b..a2b2d04 100644 --- a/test/api/ConfigTests.jl +++ b/test/api/ConfigTests.jl @@ -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