JuliaDiff / JuliaDiff/ForwardDiff.jl
`gradient!` writes to the wrong entries for structured inputs
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 1k
- Forks
- 160
- PR merge metrics
- No merged PRs in 30d
Description
extract_gradient! and extract_gradient_chunk! take their target positions from structural_eachindex(result), i.e. from the result container, while the seeds are laid out according to structural_eachindex(x). When the two disagree, the derivatives land in the wrong entries.
julia> using ForwardDiff, LinearAlgebra, DiffResults
julia> x = UpperTriangular([1.0 2.0 3.0; 0.0 4.0 5.0; 0.0 0.0 6.0]);
julia> f(z) = sum(abs2, z) / 2; # df/dz[i,j] == z[i,j] on the structural entries
julia> ForwardDiff.gradient(f, x) # correct -- the result is allocated as `similar(x)`
3×3 UpperTriangular{Float64, Matrix{Float64}}:
1.0 2.0 3.0
⋅ 4.0 5.0
⋅ ⋅ 6.0
julia> out = fill(NaN, 3, 3); ForwardDiff.gradient!(out, f, x); out
3×3 Matrix{Float64}:
1.0 3.0 NaN
2.0 5.0 NaN
4.0 6.0 NaN
The six derivatives are written to linear positions 1:6 of the dense result instead of to the upper triangle. No error, no warning, and it happens at every chunk size including vector mode.
With a DiffResult it errors instead, because DiffResults.gradient! does a linear copyto! into the UpperTriangular buffer that GradientResult allocated:
julia> ForwardDiff.gradient!(DiffResults.GradientResult(x), f, x)
ERROR: ArgumentError: cannot set index (2, 1) in the lower triangular part of an UpperTriangular matrix to a nonzero value (2.0)
Same for LowerTriangular and Diagonal. Introduced in #739.
The fix is presumably to derive the positions from x rather than from result — gradient! has x right there. That is what I ended up doing for the Hessian in #837, where the same delegation would otherwise have mis-scattered the gradient stored in a HessianResult (whose gradient buffer is dense even when x is not).
ForwardDiff v1.4.5, Julia 1.12.6.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with gradient!, extract_gradient!, and extract_gradient_chunk!, tracing how structural_eachindex is chosen for structured inputs. Compare the gradient and DiffResults paths, then run or add coverage for UpperTriangular, LowerTriangular, and Diagonal inputs across chunk sizes. Done means derivatives land in the structural positions of x without errors in DiffResult.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100