Skip to content

Value is sometimes not set when using DiffResults #269

Open
@gdalle

Description

@gdalle

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

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