Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiffResults objects are not re-aliased properly #251

Open
gdalle opened this issue Mar 15, 2024 · 2 comments
Open

DiffResults objects are not re-aliased properly #251

gdalle opened this issue Mar 15, 2024 · 2 comments
Labels

Comments

@gdalle
Copy link
Member

gdalle commented Mar 15, 2024

The DiffResults documentation clearly states that whenever a DiffResult object is used, it must be realiased at the end of the function call:

result = do_stuff!(result, args...)

See for instance the docstrings in https://juliadiff.org/DiffResults.jl/stable/#Mutating-a-DiffResult, or the issue DiffResults#17

This is not done by ReverseDiff, for instance here:

function gradient(f, input, cfg::GradientConfig = GradientConfig(input))
tape = GradientTape(f, input, cfg)
result = construct_result(input_hook(tape))
seeded_reverse_pass!(result, tape)
empty!(cfg.tape)
return result
end

Am I right in deducing that it can lead to incorrectness?

@gdalle
Copy link
Member Author

gdalle commented Mar 15, 2024

See also JuliaDiff/ForwardDiff.jl#696

@gdalle gdalle added the bug label Mar 15, 2024
@gdalle
Copy link
Member Author

gdalle commented Mar 15, 2024

Here's an MWE demonstrating the bug:

julia> using StaticArrays, ReverseDiff, DiffResults

julia> x = MVector{2}(3.0, 4.0);

julia> result_before = DiffResults.GradientResult(x)
ImmutableDiffResult(3.0, ([3.0, 4.0],))

julia> result_after = ReverseDiff.gradient!(result_before, sum, x)
ImmutableDiffResult(3.0, ([1.0, 1.0],))  # the first value should be 7 = sum(x)

julia> x = [3.0, 4.0];

julia> result_before = DiffResults.GradientResult(x)
MutableDiffResult(3.0, ([6.365987374e-314, 1.0e-323],))

julia> result_after = ReverseDiff.gradient!(result_before, sum, x)
MutableDiffResult(7.0, ([1.0, 1.0],))  # the first value is 7 = sum(x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant