Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#321 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
56/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
julia
Área
devtools

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Julia
Estrellas
396
Forks
61
Merge medio
23 h 18 min
PR fusionados (30 d)
16

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de JuliaDiff/ReverseDiff.jl

Todos los issues de JuliaDiff/ReverseDiff.jl

Issues similares

Más issues de Julia

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.