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

Reductions with `dims` bypass the recorded instructions, and `sum(x, dims)` errors

Aperta
#303 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à
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
julia
Ambito
tooling

Direzione di ricerca

Read src/derivatives/linalg/reductions.jl, especially line 41 and the existing scalar and sum! instruction paths. Run the documented sum(t, 1) reproduction and inspect the reduction tests, noting that the issue says this method is currently untested. Done means dims reductions use the intended instruction paths and the positional form no longer errors.

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

Descrizione

ReverseDiff records sum over an array in one instruction, and sum! writes the derivative back in one step. Neither is reachable through the documented dims keyword, so sum(x; dims = 1) falls through to Base's generic reduction and records one instruction per element.

Results stay correct. Only the tape size does not.

Tape entries, n × 3 matrix reduced over dimension 1

call n=4 n=40 n=400
sum(x) 1 1 1
sum!(y, x) 1 1 1
mean(x) 1 1 1
sum(x; dims = :) 11 119 1199
sum(x; dims = 1) 12 120 1200
prod(x; dims = 1) 12 120 1200
mean(x; dims = 1) 28 244 2404

maximum, minimum and cumsum with dims behave the same way.

sum(x; dims = :) is worth calling out separately: it is sum(x) by definition, and the one-instruction method for it already exists — the keyword form simply does not dispatch to it.

sum(x, dims) errors

src/derivatives/linalg/reductions.jl:41 defines the positional form:

function Base.sum(x::TrackedArray{V,D}, dims) where {V,D}
    tp = tape(x)
    out = track(sum(value(x), dims), D, tp)

sum(A, dims) was removed from Base in Julia 1.0, and that spelling now means sum(f, itr). So the inner call passes a Matrix where a function is expected, and every call to this method fails:

julia> t = ReverseDiff.track(rand(4, 3), ReverseDiff.InstructionTape());

julia> sum(t, 1)
ERROR: MethodError: objects of type Matrix{Float64} are not callable

It is the only method routing to the sum! instruction, and nothing in the test suite calls it.

What a fix would look like

A Base.sum(x::TrackedArray; dims = :) method that forwards dims === Colon() to the existing scalar path and anything else to the existing sum! instruction, replacing the positional method. prod, maximum, minimum and mean would each need their own instruction rather than a rename.

Found while fixing #172, which is a separate problem on the generic fall-through path.

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.