Skip to content

deepcopy of a GradientConfig keeps the original's tracked input, so the copy returns zero gradients #337

Open

Description

@shravanngoswamii

A copy of a GradientConfig keeps the original's tracked input but gets its own tape, so gradients with the copy come back as zeros.

The next gradient with the original is wrong too:

using ReverseDiff

f(x) = sum(abs2, x)
x = [1.0, 2.0]
cfg = ReverseDiff.GradientConfig(x)
cfg2 = deepcopy(cfg)
cfg2.input === cfg.input # true
ReverseDiff.gradient!(zeros(2), f, x, cfg2) # [0.0, 0.0]
ReverseDiff.gradient!(zeros(2), f, x, cfg) # [4.0, 8.0]

Both should be [2.0, 4.0]. Copies of a JacobianConfig or a HessianConfig return zeros as well.

It comes from deepcopy_internal, which leaves a tracked value as it is while its tape is recording (tracked.jl#L440-L446).

The tape of a config counts as recording between calls (tape.jl#L41, gradients.jl#L44), so f(cfg2.input) gets recorded onto the original tape while the reverse pass runs over the empty tape of the copy (tape.jl#L213-L218).

Those instructions stay on the original tape until its next call replays them.

We ran into this with AbstractMCMC's MCMCThreads, which samples from deepcopies of the model, and a gradient prepared by DifferentiationInterface for AutoReverseDiff().

With a JuliaBUGS model and two chains, the original tape held 85 million instructions after one sample, and the next deepcopy of the model threw a StackOverflowError.

Activity

  1. devmotion commented on Nov 16, 2026

    @devmotion
    Member

    As in #327 (comment), a general warning: One should be very very careful with using deepcopy at all in general JuliaLang/julia#42899 (comment)

  2. shravanngoswamii commented on Nov 16, 2026

    @shravanngoswamii
    Author

    Agreed. Here it's MCMCThreads that deepcopies the model (sample.jl#L593).

  3. devmotion commented on Nov 16, 2026

    @devmotion
    Member

    Ideally, that would use copy (which might require a specialization of copy for models).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions