Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Zero tangents (`NoTangent`, `ZeroTangent`, `nothing`) for tracked arguments throw a `MethodError` in `@grad`/`@grad_from_chainrules`

Aperta Adatta ai principianti
#314 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
julia
Ambito
backend

Direzione di ricerca

Inizia in src/macros.jl, da _add_to_deriv!, la funzione indicata nell’issue, e leggi le sue funzioni chiamanti e i test correlati. Riproduci gli esempi forniti con @grad e @grad_from_chainrules, quindi verifica che le tangenti nulle non causino un MethodError. Il lavoro è completato quando gli esempi restituiscono i gradienti attesi e i test pertinenti passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

If a rule returns a zero tangent for a tracked argument, the reverse pass throws a MethodError. This affects NoTangent() and ZeroTangent() from an rrule imported with @grad_from_chainrules, and nothing from a @grad pullback.

using ReverseDiff, ChainRulesCore

f(x, y) = sum(x)
ChainRulesCore.rrule(::typeof(f), x, y) = f(x, y), Δ -> (NoTangent(), fill(Δ, size(x)), ZeroTangent())
ReverseDiff.@grad_from_chainrules f(x::ReverseDiff.TrackedArray, y::ReverseDiff.TrackedArray)
ReverseDiff.gradient((x, y) -> f(x, y), ([1.0, 2.0], [3.0, 4.0]))
ERROR: MethodError: no method matching increment_deriv!(::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}, ::ZeroTangent, ::Int64)

A tracked scalar fails the same way:

h(x, y) = x
ChainRulesCore.rrule(::typeof(h), x, y) = h(x, y), Δ -> (NoTangent(), Δ, NoTangent())
ReverseDiff.@grad_from_chainrules h(x::ReverseDiff.TrackedReal, y::ReverseDiff.TrackedReal)
ReverseDiff.gradient(z -> h(z[1], z[2]), [1.0, 2.0])
ERROR: MethodError: no method matching increment_deriv!(::ReverseDiff.TrackedReal{Float64, Float64, ReverseDiff.TrackedArray{…}}, ::NoTangent)

So does @grad with nothing:

g(x, y) = sum(x)
g(x::ReverseDiff.TrackedArray, y::ReverseDiff.TrackedArray) = ReverseDiff.track(g, x, y)
ReverseDiff.@grad function g(x, y)
    return sum(ReverseDiff.value(x)), Δ -> (fill(Δ, size(x)), nothing)
end
ReverseDiff.gradient((x, y) -> g(x, y), ([1.0, 2.0], [3.0, 4.0]))
ERROR: MethodError: no method matching increment_deriv!(::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}, ::Nothing, ::Int64)

Expected: ([1.0, 1.0], [0.0, 0.0]) and [1.0, 0.0]. ChainRules uses NoTangent()/ZeroTangent() routinely for arguments a function doesn't depend on, so any such rule breaks as soon as that argument happens to be tracked.

Cause: _add_to_deriv! unthunks thunks but passes every other tangent straight to increment_deriv!, which only accepts Reals and AbstractArrays. AbstractZero (and nothing for @grad) should be a no-op.

ReverseDiff master (b796032, v1.18.4), Julia 1.13.1.

Lingua principale
Julia
Stelle
396
Fork
61
Merge medio
23h 18m
PR unite (30g)
16

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaDiff/ReverseDiff.jl

Tutte le issue di JuliaDiff/ReverseDiff.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.