Skip to content

Commit 0d375e8

Browse files
devmotionclaude
andauthored
Apply array shape rules in Diagonal/Bidiagonal/SymTridiagonal projectors (#720)
The projectors for Diagonal, Bidiagonal and SymTridiagonal now use the same shape rules as ProjectTo{AbstractArray}, via a shared _projection_reshape helper: - gradients with trailing singleton dimensions, e.g. dx = ones(3, 3, 1), are accepted (previously a MethodError); - wrongly sized matrices throw a DimensionMismatch (previously Diagonal silently took the diagonal of e.g. 3×4 and 4×3 matrices, and Bidiagonal of 4×3 ones). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 44772e9 commit 0d375e8

3 files changed

Lines changed: 50 additions & 25 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "1.26.4"
3+
version = "1.26.5"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

‎src/projection.jl‎

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,9 @@ function ProjectTo(xs::AbstractArray)
221221
end
222222
end
223223

224-
function (project::ProjectTo{AbstractArray})(dx::AbstractArray{S,M}) where {S,M}
225-
# First deal with shape. The rule is that we reshape to add or remove trivial dimensions
226-
# like dx = ones(4,1), where x = ones(4), but throw an error on dx = ones(1,4) etc.
227-
dy = if axes(dx) === project.axes
228-
dx
229-
elseif project.axes isa Tuple{Vararg{Base.OneTo}} && axes(dx) isa typeof(project.axes)
230-
# Same number of `OneTo` axes that are not identical: the sizes differ. Resolved from
231-
# the types alone, so inference does not see the type-unstable `reshape` below.
232-
throw(_projection_mismatch(project.axes, size(dx)))
233-
else
234-
for d in 1:max(M, length(project.axes))
235-
if size(dx, d) != length(get(project.axes, d, 1))
236-
throw(_projection_mismatch(project.axes, size(dx)))
237-
end
238-
end
239-
reshape(dx, project.axes)
240-
end
224+
function (project::ProjectTo{AbstractArray})(dx::AbstractArray{S}) where {S}
225+
# First deal with shape:
226+
dy = _projection_reshape(project.axes, dx)
241227
# Then deal with the elements. One projector if AbstractArray{<:Number},
242228
# or one per element for arrays of anything else, including arrays of arrays:
243229
dz = if hasproperty(project, :element)
@@ -270,6 +256,25 @@ function (project::ProjectTo{AbstractArray})(dx::Number) # ... so we restore fro
270256
return fill(project.element(dx))
271257
end
272258

259+
# Reshape dx to axes_x, adding or removing trivial dimensions like dx = ones(4,1),
260+
# where x = ones(4), but throw an error on dx = ones(1,4) etc.
261+
function _projection_reshape(axes_x::Tuple, dx::AbstractArray)
262+
return if axes(dx) === axes_x
263+
dx
264+
elseif axes_x isa Tuple{Vararg{Base.OneTo}} && axes(dx) isa typeof(axes_x)
265+
# Same number of `OneTo` axes that are not identical: the sizes differ. Resolved from
266+
# the types alone, so inference does not see the type-unstable `reshape` below.
267+
throw(_projection_mismatch(axes_x, size(dx)))
268+
else
269+
for d in 1:max(ndims(dx), length(axes_x))
270+
if size(dx, d) != length(get(axes_x, d, 1))
271+
throw(_projection_mismatch(axes_x, size(dx)))
272+
end
273+
end
274+
reshape(dx, axes_x)
275+
end
276+
end
277+
273278
function _projection_mismatch(axes_x::Tuple, size_dx::Tuple)
274279
size_x = map(length, axes_x)
275280
return DimensionMismatch(
@@ -439,7 +444,11 @@ end
439444

440445
# Diagonal
441446
ProjectTo(x::Diagonal) = ProjectTo{Diagonal}(; diag=ProjectTo(x.diag))
442-
(project::ProjectTo{Diagonal})(dx::AbstractMatrix) = Diagonal(project.diag(diag(dx)))
447+
function (project::ProjectTo{Diagonal})(dx::AbstractArray)
448+
ax = only(project.diag.axes)
449+
dy = _projection_reshape((ax, ax), dx)
450+
return Diagonal(project.diag(diag(dy)))
451+
end
443452
(project::ProjectTo{Diagonal})(dx::Diagonal) = Diagonal(project.diag(dx.diag))
444453

445454
# Symmetric
@@ -489,10 +498,12 @@ end
489498
# Weird -- not exhaustive!
490499
# one strategy is to recurse into the struct:
491500
ProjectTo(x::Bidiagonal{T}) where {T<:Number} = generic_projector(x)
492-
function (project::ProjectTo{Bidiagonal})(dx::AbstractMatrix)
501+
function (project::ProjectTo{Bidiagonal})(dx::AbstractArray)
502+
ax = only(project.dv.axes)
503+
dy = _projection_reshape((ax, ax), dx)
493504
uplo = LinearAlgebra.sym_uplo(project.uplo)
494-
dv = project.dv(diag(dx))
495-
ev = project.ev(uplo === :U ? diag(dx, 1) : diag(dx, -1))
505+
dv = project.dv(diag(dy))
506+
ev = project.ev(uplo === :U ? diag(dy, 1) : diag(dy, -1))
496507
return Bidiagonal(dv, ev, uplo)
497508
end
498509
function (project::ProjectTo{Bidiagonal})(dx::Bidiagonal)
@@ -507,9 +518,11 @@ function (project::ProjectTo{Bidiagonal})(dx::Bidiagonal)
507518
end
508519

509520
ProjectTo(x::SymTridiagonal{T}) where {T<:Number} = generic_projector(x)
510-
function (project::ProjectTo{SymTridiagonal})(dx::AbstractMatrix)
511-
dv = project.dv(diag(dx))
512-
ev = project.ev((diag(dx, 1) .+ diag(dx, -1)) ./ 2)
521+
function (project::ProjectTo{SymTridiagonal})(dx::AbstractArray)
522+
ax = only(project.dv.axes)
523+
dy = _projection_reshape((ax, ax), dx)
524+
dv = project.dv(diag(dy))
525+
ev = project.ev((diag(dy, 1) .+ diag(dy, -1)) ./ 2)
513526
return SymTridiagonal(dv, ev)
514527
end
515528
(project::ProjectTo{SymTridiagonal})(dx::SymTridiagonal) = generic_projection(project, dx)

‎test/projection.jl‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ struct NoSuperType end
322322
@test pstri(stri) == real(stri)
323323
@test_throws DimensionMismatch pstri(rand(ComplexF32, 3, 2))
324324

325+
# same shape rules as ProjectTo{AbstractArray}, e.g. trailing singleton dimensions
326+
for p in (pdiag, pbi, pstri)
327+
@test @inferred(p(reshape(1:9, 3, 3, 1))) == p(reshape(1:9, 3, 3))
328+
@test p(reshape(1:9, 3, 3, 1, 1)) == p(reshape(1:9, 3, 3))
329+
@test p(rand(ComplexF32, 3, 3, 1)) isa typeof(p(rand(3, 3)))
330+
@test_throws DimensionMismatch p(rand(3, 3, 2))
331+
@test_throws DimensionMismatch p(rand(3, 1, 3))
332+
@test_throws DimensionMismatch p(rand(3, 4))
333+
end
334+
psdiag = ProjectTo(Diagonal(SA[1.0, 2.0, 3.0]))
335+
@test psdiag(reshape(1:9, 3, 3, 1)) == psdiag(reshape(1:9, 3, 3))
336+
325337
ptri = ProjectTo(Tridiagonal(rand(3, 3)))
326338
@test ptri(reshape(1:9, 3, 3)) == [1.0 4.0 0.0; 2.0 5.0 8.0; 0.0 6.0 9.0]
327339
@test ptri(ptri(reshape(1:9, 3, 3))) == ptri(reshape(1:9, 3, 3))

0 commit comments

Comments
 (0)