`@grad`/`@grad_from_chainrules` fail for rules returning structured arrays (`Diagonal`, `UpperTriangular`, ...)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
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
- 星标
- 396
- 派生
- 61
- 平均合并
- 23 小时 18 分钟
- 30 天内合并 PR
- 16
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
JuliaDiff/ReverseDiff.jl 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 86/100
JuliaDiff/ReverseDiff.jl#318 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 83/100
JuliaDiff/ReverseDiff.jl#315 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
JuliaDiff/ReverseDiff.jl#314 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 72/100
JuliaDiff/ReverseDiff.jl#320 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 68/100
JuliaDiff/ReverseDiff.jl#319 ·
维护者通常 1 天内回复
查看 JuliaDiff/ReverseDiff.jl 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
oxfordcontrol/COSMO.jl#211 ·
-
documentation
难度 2/5 半天 新手友好度 65/100
维护者通常 6 天内回复
-
Out-of-place JLArray/GPU problem with VectorContinuousCallback scalar-indexes (callback cache built with CPU zeros)可能已有人在做 @ChrisRackauckas-Claude 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
SciML/OrdinaryDiffEq.jl#4813 ·
维护者通常 1 天内回复
-
ARKODE: callbacks that modify `u` throw MethodError on reinit可能已有人在做 @devmotion 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 79/100
SciML/Sundials.jl#575 ·
-
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复