`@grad`/`@grad_from_chainrules` fail for rules returning structured arrays (`Diagonal`, `UpperTriangular`, ...)
まだ誰も着手していません。
評価
調査の方向性
Start with the two tracking paths in src/macros.jl named in the issue: @grad and @grad_from_chainrules. Reproduce the Diagonal and UpperTriangular examples, then trace how output tracking and forward replay handle structured arrays. Done means both examples complete without the IndexStyle assertion and return the expected gradients.
索引モデルが issue の本文から書いたものです。
説明
If a rule's primal output is an array without linear indexing, such as Diagonal or UpperTriangular, ReverseDiff fails when it wraps the output in a TrackedArray. This affects both @grad_from_chainrules and @grad.
using ReverseDiff, ChainRulesCore, LinearAlgebra
f(x) = Diagonal(x)
ChainRulesCore.rrule(::typeof(f), x) = f(x), Δ -> (NoTangent(), diag(unthunk(Δ)))
ReverseDiff.@grad_from_chainrules f(x::ReverseDiff.TrackedArray)
ReverseDiff.gradient(x -> sum(f(x)), [1.0, 2.0])
ERROR: LoadError: AssertionError: IndexStyle(value) === IndexLinear()
Stacktrace:
[1] ReverseDiff.TrackedArray{Float64, Float64, 2, Diagonal{Float64, Vector{Float64}}, Diagonal{Float64, Vector{Float64}}}(value::Diagonal{Float64, Vector{Float64}}, deriv::Diagonal{Float64, Vector{Float64}}, tape::ReverseDiff.InstructionTape)
...
[5] track(::typeof(f), x::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
@ Main ~/.julia/dev/ReverseDiff/src/macros.jl:339
@grad with an UpperTriangular output fails the same way:
g(x) = UpperTriangular(x)
g(x::ReverseDiff.TrackedArray) = ReverseDiff.track(g, x)
ReverseDiff.@grad function g(x)
return UpperTriangular(ReverseDiff.value(x)), Δ -> (triu(Δ),)
end
ReverseDiff.gradient(x -> sum(g(x)), [1.0 2.0; 3.0 4.0])
# ERROR: AssertionError: IndexStyle(value) === IndexLinear()
Expected: [1.0, 1.0] and [1.0 1.0; 0.0 1.0]. Many ChainRules rules return structured matrices (Diagonal, Symmetric, triangular factors, ...), so importing them fails as soon as the output is tracked.
Cause: the macros call track(output_value, tp) on the primal output as is (@grad, @grad_from_chainrules). TrackedArray requires IndexLinear() storage, and even with #216 a Diagonal/UpperTriangular deriv couldn't store the off-structure entries the reverse pass seeds. Materializing such outputs (e.g. collect) before tracking would avoid this. The forward replay value!(output, out_value) has to do the same.
ReverseDiff master (b796032, v1.18.4), Julia 1.13.1.
- 主要言語
- Julia
- スター
- 397
- フォーク
- 62
- 平均マージ
- 12時間 50分
- マージ済み PR(30日)
- 8
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
JuliaDiff/ReverseDiff.jl のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
JuliaDiff/ReverseDiff.jl#318 ·
-
`@grad`/`@grad_from_chainrules` silently reuse a tangent if a pullback returns too few tangentsオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
JuliaDiff/ReverseDiff.jl#315 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
JuliaDiff/ReverseDiff.jl#314 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
JuliaDiff/ReverseDiff.jl#320 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
JuliaDiff/ReverseDiff.jl#319 ·
JuliaDiff/ReverseDiff.jl の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
SciML/OrdinaryDiffEq.jl#4907 ·
メンテナーはふだん 4 日以内に返信
-
documentation model-weights
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
FluxML/Metalhead.jl#311 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
jump-dev/MathOptInterface.jl#3113 ·
メンテナーはふだん 1 日以内に返信