JuliaDiff / JuliaDiff/ReverseDiff.jl

Combine Dual numbers with compiled gradient tape

Open
#132 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
393
Forks
60
Avg merge
18h 24m
Merged PRs (30d)
8

Description

It seems that in some cases, the compiled tape does not return correct gradient in the following application of obtaining gradients.

function get_hessian_reversediff(params0::AbstractArray{T}) where T
    tape2 = ReverseDiff.GradientTape(embedding_loss, (Dual.(randn(k, 10), zeros(k, 10)),))
    ctape2 = ReverseDiff.compile(tape2)
    get_hessian_reversediff(ctape2, params0)
end

function get_hessian_reversediff(tape, params0::AbstractArray{T}) where T
    N = length(params0)
    params = Dual.(params0, zero(T))
    hes = zeros(T, N, N)
    for i=1:N
        @inbounds i !== 1 && (params[i-1] = Dual(params0[i-1], zero(T)))
        @inbounds params[i] = Dual(params0[i], one(T))
        res = ReverseDiff.gradient!(tape, (params,))[1]
        res2 = ReverseDiff.gradient(embedding_loss, params)
        h1 = vec(ForwardDiff.partials.(res, 1)) 
        h2 = vec(ForwardDiff.partials.(res2, 1))
        @show h1 - h2   # they are different!!!!
        hes[:,i] .= vec(ForwardDiff.partials.(res, 1))
    end
    hes
end

It turns out h1 - h2 is not zero.

The compiled tape returns a gradient slightly different with the not compiled version. I checked that the not compiled version gradient is correct.

To reproduce the result. Please check
https://github.com/JuliaReverse/NiGraphEmbedding.jl/blob/master/benchmarks/benchmark.jl

Wondering what is a correct way the obtain gradient through forward differentiating over back program?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in benchmarks/benchmark.jl and compare the compiled-tape path using ReverseDiff.gradient! with the uncompiled ReverseDiff.gradient path. Inspect how Dual values from ForwardDiff are handled during tape compilation. Done means the compiled and uncompiled gradients agree for the reported case, including the hessian calculation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.