JuliaDiff / JuliaDiff/ChainRulesTestUtils.jl

`test_rrule` failed when pullback return array buffer allocated in rrule.

Open
#264 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
50
Forks
18
PR merge metrics
No merged PRs in 30d

Description

MWE:

julia> using ChainRulesCore, ChainRulesTestUtils

julia> f(x) = x .* fill(2, size(x))
f (generic function with 1 method)

julia> f1(x) = f(x)
f1 (generic function with 1 method)

julia> f2(x) = f(x)
f2 (generic function with 1 method)

julia> function ChainRulesCore.rrule(::typeof(f1), x)
    tmp = similar(x)
    fill!(tmp, 2)
    y = x .* tmp
    function pullback(Ȳ)
        tmp .*= Ȳ
        ∂ = tmp
        return (NoTangent(), ∂)
    end
    return y, pullback
end

julia> function ChainRulesCore.rrule(::typeof(f2), x)
    tmp = similar(x)
    fill!(tmp, 2)
    y = x .* tmp
    function pullback(Ȳ)
        ∂ = tmp .* Ȳ
        return (NoTangent(), ∂)
    end
    return y, pullback
end

julia> test_rrule(f2, [1.0,2.0,3.0])
Test.DefaultTestSet("test_rrule: f2 on Vector{Float64}", Any[], 7, false, false, true, 1.666336561747233e9, 1.666336562127431e9)

julia> test_rrule(f1, [1.0,2.0,3.0])
test_rrule: f1 on Vector{Float64}: Test Failed at /home/peter/.julia/packages/ChainRulesTestUtils/YbVdW/src/check_result.jl:24
Expression: isapprox(actual, expected; kwargs...)
Evaluated: isapprox([32.0, 132.5192, 18.7272], [-7.999999999999777, 16.27999999999986, -6.120000000000349]; rtol = 1.0e-9, atol = 1.0e-9)
Stacktrace:
[...]

It seems a single pullback is called multiple times without being recreated by rrule and thus the assign the value to the same place multiple times.

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 test_rrule and the reported check_result.jl failure at line 24, then reproduce the f1 and f2 examples from the issue. Trace how pullbacks are invoked and how returned arrays are compared; done means valid rules using an allocated return buffer no longer fail from repeated pullback checks, while genuine discrepancies still fail.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.