JuliaDiff / JuliaDiff/ForwardDiff.jl

Chunked `jacobian` throws for `Diagonal`/`LowerTriangular`/`UpperTriangular` inputs

Open
#839 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

jacobian allocates its result with chunksize(cfg) columns, which is structural_length(x), but reshape_jacobian reshapes it to length(xdual) columns. For inputs whose structural length differs from their length the two disagree, so chunk mode always throws while vector mode works.

julia> using ForwardDiff, LinearAlgebra

julia> g(z) = [sum(z), sum(abs2, z)];

julia> x = UpperTriangular(rand(3, 3));

julia> size(ForwardDiff.jacobian(g, x))   # vector mode
(2, 6)

julia> ForwardDiff.jacobian(g, x, ForwardDiff.JacobianConfig(g, x, ForwardDiff.Chunk{2}()))
ERROR: DimensionMismatch: new dimensions (2, 9) must be consistent with array length 12

All three wrapper types, only the full-length chunk survives:

input chunk=1 chunk=2 chunk=structural_length(x)
UpperTriangular(rand(3,3)) DimensionMismatch DimensionMismatch (2, 6)
LowerTriangular(rand(3,3)) DimensionMismatch DimensionMismatch (2, 6)
Diagonal(rand(3,3)) DimensionMismatch DimensionMismatch (2, 3)

Since Chunk(x) picks the full structural length for anything up to DEFAULT_CHUNK_THRESHOLD, this only shows up for larger inputs or an explicit Chunk. Introduced in #739.

Worth deciding the column convention at the same time: jacobian currently uses structural columns while gradient returns a result shaped like x. See the discussion in #837, where I argued for indexing the Hessian by the linear indices of x on both axes.

ForwardDiff v1.4.5, Julia 1.12.6.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the chunked jacobian path, chunksize(cfg), and reshape_jacobian, then review the column-convention discussion in #837. Verify completion using the shown UpperTriangular, LowerTriangular, and Diagonal examples with chunk sizes 1, 2, and structural length, comparing the results with vector mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.