Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`@grad`/`@grad_from_chainrules` fail for rules returning structured arrays (`Diagonal`, `UpperTriangular`, ...)

未关闭
#321 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
56/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
julia
领域
devtools

调研方向

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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JuliaDiff/ReverseDiff.jl 的其他 Issue

查看 JuliaDiff/ReverseDiff.jl 的全部 Issue

相似的 Issue

更多 Julia Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。