Skip to content

Value is sometimes not set when using DiffResults #269

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

Open
gdalle opened this issue Feb 15, 2025 · 5 comments
Open

Value is sometimes not set when using DiffResults #269

gdalle opened this issue Feb 15, 2025 · 5 comments

Comments

@gdalle
Copy link
Member

gdalle commented Feb 15, 2025

Here's an MWE I'm struggling to understand. My best guess sofar is that this is hitting a Julia compiler optimization which somehow eludes the value computation. Unless I'm just holding the thing wrong?

using ReverseDiff: ReverseDiff
using DiffResults: DiffResults

f(x) = sum(abs2, x)
x = [3.0, 5.0]
compiled_tape = ReverseDiff.compile(ReverseDiff.GradientTape(f, x))

function value_and_gradient1!(grad, tape, x)
    result = DiffResults.MutableDiffResult(zero(eltype(x)), (grad,))
    result = ReverseDiff.gradient!(result, tape, x)
    return DiffResults.value(result), DiffResults.gradient(result)
end

function value_and_gradient2!(grad, tape, x)
    result = DiffResults.MutableDiffResult(zero(eltype(x)), (grad,))
    result = ReverseDiff.gradient!(result, tape, x)
    return DiffResults.value(result), grad
end

function value_and_gradient_nested1!(grad, tape, x)
    y, _ = value_and_gradient1!(grad, tape, x)
    return y, grad
end

function value_and_gradient_nested2!(grad, tape, x)
    y, _ = value_and_gradient2!(grad, tape, x)
    return y, grad
end
julia> value_and_gradient1!(similar(x), compiled_tape, x)
(34.0, [6.0, 10.0])

julia> value_and_gradient_nested1!(similar(x), compiled_tape, x)  # wrong value
(0.0, [6.0, 10.0])

julia> value_and_gradient2!(similar(x), compiled_tape, x)
(34.0, [6.0, 10.0])

julia> value_and_gradient_nested2!(similar(x), compiled_tape, x)
(34.0, [6.0, 10.0])

Related:

(jl_gKdWaj) pkg> st
Status `/private/var/folders/pt/myrxq2gs5hzcp79v5475_dh40000gp/T/jl_gKdWaj/Project.toml`
  [163ba53b] DiffResults v1.1.0
  [37e2e3b7] ReverseDiff v1.15.3

julia> versioninfo()
Julia Version 1.11.3
Commit d63adeda50d (2025-01-21 19:42 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin24.0.0)
  CPU: 11 × Apple M3 Pro
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, apple-m2)
Threads: 5 default, 0 interactive, 2 GC (on 5 virtual cores)
Environment:
  JULIA_EDITOR = code
@nsajko
Copy link

nsajko commented Feb 16, 2025

This doesn't reproduce on nightly Julia.

@nsajko
Copy link

nsajko commented Feb 16, 2025

BTW, as mentioned on Slack, --compile=min is a good way to check if a bug is a miscompilation.

@gdalle
Copy link
Member Author

gdalle commented Feb 16, 2025

You're right, with --compile=min it disappears as well. Not sure what to do about it then.

@nsajko
Copy link

nsajko commented Feb 16, 2025

Not sure what to do about it then.

Create a MWE and report an issue to JuliaLang/julia.

Create the MWE by:

  1. starting from the reproducer above
  2. inlining the contents of the two packages and all dependencies
  3. delete irrelevant code manually
  4. optionally (I think it's slightly demanding to set up), use https://github.com/maleadt/creduce_julia to further reduce the MWE

Another thing to do is bisect the fix (given that the bug is perhaps fixed on nightly), and see if it's backportable.

@gdalle
Copy link
Member Author

gdalle commented Feb 16, 2025

Unfortunately I wouldn't even know where to begin to inline ReverseDiff. I tried removing it entirely and the effect disappeared.

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

No branches or pull requests

2 participants