From 11d3180460a1a863c62bd4f1ad03e16bbc23c793 Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Mon, 16 Nov 2026 16:40:24 +0530 Subject: [PATCH 1/7] Give copies of a config their own tapes --- src/api/Config.jl | 14 ++++++++++++++ src/tracked.jl | 4 ++-- test/api/ConfigTests.jl | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/api/Config.jl b/src/api/Config.jl index afd2b261..1b046852 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 716b12f7..264bcf9b 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 5c6971b2..a2b2d04a 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 From af8687ac16a54a4873fbd22eee99bc7e4ece650f Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Tue, 17 Nov 2026 12:13:25 +0530 Subject: [PATCH 2/7] Keep the tapes of idle configs finished --- src/api/Config.jl | 18 ++---------------- src/api/gradients.jl | 4 ++-- src/api/jacobians.jl | 8 ++++---- src/api/tape.jl | 7 +++++++ src/tape.jl | 12 ++++++++++++ src/tracked.jl | 4 ++-- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/api/Config.jl b/src/api/Config.jl index 1b046852..761c907e 100644 --- a/src/api/Config.jl +++ b/src/api/Config.jl @@ -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()) @@ -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()) @@ -168,17 +168,3 @@ 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/api/gradients.jl b/src/api/gradients.jl index b52801fa..c27a37da 100644 --- a/src/api/gradients.jl +++ b/src/api/gradients.jl @@ -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 @@ -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 diff --git a/src/api/jacobians.jl b/src/api/jacobians.jl index 925d0bdd..bc6b2037 100644 --- a/src/api/jacobians.jl +++ b/src/api/jacobians.jl @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/api/tape.jl b/src/api/tape.jl index 5639b1bd..3ad26e47 100644 --- a/src/api/tape.jl +++ b/src/api/tape.jl @@ -211,6 +211,7 @@ 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)) + isidle(cfg.tape) && empty!(cfg.tape) track!(cfg.input, input) tracked_ouput = f(cfg.input) finish!(cfg.tape) @@ -218,6 +219,7 @@ function GradientTape(f, input, cfg::GradientConfig = GradientConfig(input)) end function GradientTape(f, input::Tuple, cfg::GradientConfig = GradientConfig(input)) + isidle(cfg.tape) && empty!(cfg.tape) for i in eachindex(cfg.input) track!(cfg.input[i], input[i]) end @@ -243,6 +245,7 @@ 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)) + isidle(cfg.tape) && empty!(cfg.tape) track!(cfg.input, input) tracked_ouput = f(cfg.input) finish!(cfg.tape) @@ -250,6 +253,7 @@ function JacobianTape(f, input, cfg::JacobianConfig = JacobianConfig(input)) end function JacobianTape(f, input::Tuple, cfg::JacobianConfig = JacobianConfig(input)) + isidle(cfg.tape) && empty!(cfg.tape) for i in eachindex(cfg.input) track!(cfg.input[i], input[i]) end @@ -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)) + isidle(cfg.tape) && empty!(cfg.tape) track!(cfg.output, output, cfg.tape) track!(cfg.input, input) f!(cfg.output, cfg.input) @@ -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)) + isidle(cfg.tape) && empty!(cfg.tape) track!(cfg.output, output, cfg.tape) for i in eachindex(input) track!(cfg.input[i], input[i]) @@ -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 + isidle(jcfg.tape) && empty!(jcfg.tape) ht = _HessianTape(f, jcfg.input, similar(deriv(gcfg.input)), jcfg.tape) track!(ht.input, input) gt = GradientTape(f, ht.input, gcfg) diff --git a/src/tape.jl b/src/tape.jl index 96ff6210..16d5b466 100644 --- a/src/tape.jl +++ b/src/tape.jl @@ -68,6 +68,18 @@ 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`. +isidle(tp::InstructionTape) = !isrecording(tp) && isempty(tp.instructions) + +# a new tape becomes idle; a tape that something is recorded onto is left alone +function finish_if_new!(tp::InstructionTape) + (@atomic tp.last) === nothing && finish!(tp) + 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() diff --git a/src/tracked.jl b/src/tracked.jl index 264bcf9b..716b12f7 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, or if their tape is copied as well, they are copied together with their tape +# afterwards they are copied together with their tape function Base.deepcopy_internal(t::Union{TrackedReal, TrackedArray}, dict::IdDict) - if isrecording(tape(t)) && !haskey(dict, tape(t)) + if isrecording(tape(t)) return t else return invoke(Base.deepcopy_internal, Tuple{Any, IdDict}, t, dict) From 63da42bf5db93cdf3c835bc3f8ce3cd9a2f18d96 Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Tue, 17 Nov 2026 12:13:25 +0530 Subject: [PATCH 3/7] Test copies of used configs with functions at module scope --- test/api/ConfigTests.jl | 63 +++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/test/api/ConfigTests.jl b/test/api/ConfigTests.jl index a2b2d04a..237eb153 100644 --- a/test/api/ConfigTests.jl +++ b/test/api/ConfigTests.jl @@ -78,20 +78,53 @@ cfg = HessianConfig(DiffResults.HessianResult(y), x, gtp, jtp) # deepcopy # ############ -@testset "`deepcopy` of a config is independent" begin +# `MCMCThreads` deepcopies models, which may hold configs that were already used + +sum_abs2(x) = sum(abs2, x) +sum_prod(x, y) = sum(x .* y) +double(x) = 2 .* x +double!(y, x) = (y .= 2 .* x) + +@testset "`deepcopy` of a `GradientConfig`" begin + x = [1.0, 2.0] + cfg = GradientConfig(x) + @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, deepcopy(cfg)) == [2.0, 4.0] + @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, cfg) == [2.0, 4.0] + copied = deepcopy(cfg) + @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, copied) == [2.0, 4.0] + @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, cfg) == [2.0, 4.0] +end + +@testset "`deepcopy` of a `GradientConfig` of several inputs" 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 + cfg = GradientConfig((x, y)) + ReverseDiff.gradient(sum_prod, (x, y), cfg) + @test ReverseDiff.gradient(sum_prod, (x, y), deepcopy(cfg)) == (y, x) + @test ReverseDiff.gradient(sum_prod, (x, y), cfg) == (y, x) +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] + + cfg = JacobianConfig(zeros(2), x) + ReverseDiff.jacobian(double!, zeros(2), x, cfg) + @test ReverseDiff.jacobian(double!, zeros(2), x, deepcopy(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] + @test ReverseDiff.hessian(sum_abs2, x, cfg) == [2.0 0.0; 0.0 2.0] +end + +@testset "a config holding a recorded tape cannot record another" begin + x = [1.0, 2.0] + cfg = GradientConfig(x) + ReverseDiff.GradientTape(sum_abs2, x, cfg) + @test_throws ArgumentError ReverseDiff.GradientTape(sum_abs2, x, cfg) end From 311ab111c9bdf54057ea3c6c93b4051f217df332 Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Tue, 17 Nov 2026 12:56:50 +0530 Subject: [PATCH 4/7] Keep one config copy test per config type --- test/api/ConfigTests.jl | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/test/api/ConfigTests.jl b/test/api/ConfigTests.jl index 237eb153..ae274d01 100644 --- a/test/api/ConfigTests.jl +++ b/test/api/ConfigTests.jl @@ -81,26 +81,14 @@ cfg = HessianConfig(DiffResults.HessianResult(y), x, gtp, jtp) # `MCMCThreads` deepcopies models, which may hold configs that were already used sum_abs2(x) = sum(abs2, x) -sum_prod(x, y) = sum(x .* y) double(x) = 2 .* x -double!(y, x) = (y .= 2 .* x) @testset "`deepcopy` of a `GradientConfig`" begin x = [1.0, 2.0] cfg = GradientConfig(x) - @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, deepcopy(cfg)) == [2.0, 4.0] - @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, cfg) == [2.0, 4.0] - copied = deepcopy(cfg) - @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, copied) == [2.0, 4.0] - @test ReverseDiff.gradient!(zeros(2), sum_abs2, x, cfg) == [2.0, 4.0] -end - -@testset "`deepcopy` of a `GradientConfig` of several inputs" begin - x, y = [1.0, 2.0], [3.0, 4.0] - cfg = GradientConfig((x, y)) - ReverseDiff.gradient(sum_prod, (x, y), cfg) - @test ReverseDiff.gradient(sum_prod, (x, y), deepcopy(cfg)) == (y, x) - @test ReverseDiff.gradient(sum_prod, (x, y), cfg) == (y, 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 @@ -108,10 +96,6 @@ end cfg = JacobianConfig(x) ReverseDiff.jacobian(double, x, cfg) @test ReverseDiff.jacobian(double, x, deepcopy(cfg)) == [2.0 0.0; 0.0 2.0] - - cfg = JacobianConfig(zeros(2), x) - ReverseDiff.jacobian(double!, zeros(2), x, cfg) - @test ReverseDiff.jacobian(double!, zeros(2), x, deepcopy(cfg)) == [2.0 0.0; 0.0 2.0] end @testset "`deepcopy` of a `HessianConfig`" begin @@ -119,7 +103,6 @@ end 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] - @test ReverseDiff.hessian(sum_abs2, x, cfg) == [2.0 0.0; 0.0 2.0] end @testset "a config holding a recorded tape cannot record another" begin From 7ce69dd7966ced8eaa5d9167cab60d0459e0c57d Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Wed, 18 Nov 2026 03:52:03 +0530 Subject: [PATCH 5/7] Fold the idle check into reopen! --- src/api/tape.jl | 14 +++++++------- src/tape.jl | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/api/tape.jl b/src/api/tape.jl index 3ad26e47..7db1bdc2 100644 --- a/src/api/tape.jl +++ b/src/api/tape.jl @@ -211,7 +211,7 @@ 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)) - isidle(cfg.tape) && empty!(cfg.tape) + reopen!(cfg.tape) track!(cfg.input, input) tracked_ouput = f(cfg.input) finish!(cfg.tape) @@ -219,7 +219,7 @@ function GradientTape(f, input, cfg::GradientConfig = GradientConfig(input)) end function GradientTape(f, input::Tuple, cfg::GradientConfig = GradientConfig(input)) - isidle(cfg.tape) && empty!(cfg.tape) + reopen!(cfg.tape) for i in eachindex(cfg.input) track!(cfg.input[i], input[i]) end @@ -245,7 +245,7 @@ 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)) - isidle(cfg.tape) && empty!(cfg.tape) + reopen!(cfg.tape) track!(cfg.input, input) tracked_ouput = f(cfg.input) finish!(cfg.tape) @@ -253,7 +253,7 @@ function JacobianTape(f, input, cfg::JacobianConfig = JacobianConfig(input)) end function JacobianTape(f, input::Tuple, cfg::JacobianConfig = JacobianConfig(input)) - isidle(cfg.tape) && empty!(cfg.tape) + reopen!(cfg.tape) for i in eachindex(cfg.input) track!(cfg.input[i], input[i]) end @@ -275,7 +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)) - isidle(cfg.tape) && empty!(cfg.tape) + reopen!(cfg.tape) track!(cfg.output, output, cfg.tape) track!(cfg.input, input) f!(cfg.output, cfg.input) @@ -284,7 +284,7 @@ function JacobianTape(f!, output, input, cfg::JacobianConfig = JacobianConfig(ou end function JacobianTape(f!, output, input::Tuple, cfg::JacobianConfig = JacobianConfig(output, input)) - isidle(cfg.tape) && empty!(cfg.tape) + reopen!(cfg.tape) track!(cfg.output, output, cfg.tape) for i in eachindex(input) track!(cfg.input[i], input[i]) @@ -313,7 +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 - isidle(jcfg.tape) && empty!(jcfg.tape) + reopen!(jcfg.tape) ht = _HessianTape(f, jcfg.input, similar(deriv(gcfg.input)), jcfg.tape) track!(ht.input, input) gt = GradientTape(f, ht.input, gcfg) diff --git a/src/tape.jl b/src/tape.jl index 16d5b466..afed4364 100644 --- a/src/tape.jl +++ b/src/tape.jl @@ -70,11 +70,20 @@ 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`. -isidle(tp::InstructionTape) = !isrecording(tp) && isempty(tp.instructions) -# a new tape becomes idle; a tape that something is recorded onto is left alone +# finish a tape that has not recorded anything; tapes with recordings are left alone function finish_if_new!(tp::InstructionTape) - (@atomic tp.last) === nothing && finish!(tp) + 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 From e80b1774db8880a347c961d035690a291e1cfa34 Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Wed, 18 Nov 2026 03:52:14 +0530 Subject: [PATCH 6/7] Move the re-recording test out of the deepcopy section --- test/api/ConfigTests.jl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/api/ConfigTests.jl b/test/api/ConfigTests.jl index ae274d01..0bb0623d 100644 --- a/test/api/ConfigTests.jl +++ b/test/api/ConfigTests.jl @@ -74,6 +74,17 @@ cfg = HessianConfig(DiffResults.HessianResult(y), x, gtp, jtp) #@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 # ############ @@ -104,10 +115,3 @@ end ReverseDiff.hessian(sum_abs2, x, cfg) @test ReverseDiff.hessian(sum_abs2, x, deepcopy(cfg)) == [2.0 0.0; 0.0 2.0] end - -@testset "a config holding a recorded tape cannot record another" begin - x = [1.0, 2.0] - cfg = GradientConfig(x) - ReverseDiff.GradientTape(sum_abs2, x, cfg) - @test_throws ArgumentError ReverseDiff.GradientTape(sum_abs2, x, cfg) -end From 3ee63b7780e3692a3e00175c132f899586af4e15 Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Wed, 18 Nov 2026 03:52:14 +0530 Subject: [PATCH 7/7] Check the original configs after their copies were used --- test/api/ConfigTests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/api/ConfigTests.jl b/test/api/ConfigTests.jl index 0bb0623d..25b6c93a 100644 --- a/test/api/ConfigTests.jl +++ b/test/api/ConfigTests.jl @@ -107,6 +107,7 @@ end cfg = JacobianConfig(x) ReverseDiff.jacobian(double, x, cfg) @test ReverseDiff.jacobian(double, x, deepcopy(cfg)) == [2.0 0.0; 0.0 2.0] + @test ReverseDiff.jacobian(double, x, cfg) == [2.0 0.0; 0.0 2.0] end @testset "`deepcopy` of a `HessianConfig`" begin @@ -114,4 +115,5 @@ end 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] + @test ReverseDiff.hessian(sum_abs2, x, cfg) == [2.0 0.0; 0.0 2.0] end