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

`gradient!`, `jacobian!` and `hessian!` do not support an immutable `DiffResult` unless `x` is a `StaticArray`

Aperta
#849 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Start with extract_gradient_chunk! in src/gradient.jl, hessian!(::DiffResult, …) in src/hessian.jl, and extract_jacobian! in src/jacobian.jl; compare them with vector-mode gradient!'s DiffResults.gradient! handling. Run the supplied immutable DiffResult examples across the listed chunk sizes and confirm gradient!, jacobian!, and hessian! work without requiring mutable buffers.

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

Descrizione

The methods in src store derivatives by mutating the buffer they pull out of a DiffResult, so they cannot handle a result whose buffers are genuinely immutable. The StaticArrays extension does handle one, but its methods are only reached when x is itself a StaticArray.

using ForwardDiff, DiffResults, StaticArrays
x = [1.0, 2.0, 3.0]
sq(z) = z .^ 2

# succeeds or fails depending only on the chunk size
for c in (1, 2, 3)
    cfg = ForwardDiff.GradientConfig(prod, x, ForwardDiff.Chunk{c}())
    ForwardDiff.gradient!(DiffResults.GradientResult(@SVector zeros(3)), prod, x, cfg)
end
# chunk 1, 2: ERROR: setindex!(::SVector{3, Float64}, value, ::Int) is not defined.
# chunk 3:    ImmutableDiffResult(6.0, ([6.0, 3.0, 2.0],))

ForwardDiff.hessian!(DiffResults.HessianResult(@SVector zeros(3)), prod, x)
# ERROR: setindex!(::SVector{3, Float64}, value, ::Int) is not defined.

ForwardDiff.jacobian!(DiffResults.JacobianResult(@SVector(zeros(3)), @SVector(zeros(3))), sq, x)
# ERROR: MethodError: no method matching extract_jacobian!(…, ::ImmutableDiffResult{…}, …)

All three work with x = SVector(1.0, 2.0, 3.0).

Cause

extract_gradient_chunk! (gradient.jl#L83-L86) and hessian!(::DiffResult, …) (hessian.jl#L69) write the extracted buffer via setindex!/broadcast, and extract_jacobian! (jacobian.jl#L104) only has AbstractArray and MutableDiffResult methods.

Vector-mode gradient! shows what the others need: extract_gradient!(::Type{T}, ::DiffResult, ::Dual) stores through DiffResults.gradient!, rebuilding the struct instead of writing the buffer.

Scope

Only about genuinely immutable buffers. A result holding MArray buffers hits some of the same errors, but that is DiffResults#25 — those are writable and should give a MutableDiffResult.

Independent of #696; reproduces before and after the re-aliasing fix there.

ForwardDiff master (v1.4.6), Julia 1.12.7, StaticArrays 1.9.22, DiffResults 1.1.0.

Lingua principale
Julia
Stelle
1k
Fork
162
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

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/ForwardDiff.jl

Tutte le issue di JuliaDiff/ForwardDiff.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.