Skip to content

Commit 0ae32d8

Browse files
devmotionclaudegithub-actions[bot]
authored
Collapse all-zero tuple projections after projecting (#725)
* Check for zero tuple type * Test tuple projector collapse and inference Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Bump version Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 4e9c8aa commit 0ae32d8

3 files changed

Lines changed: 15 additions & 3 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.7"
3+
version = "1.26.8"
44

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

‎src/projection.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function (project::ProjectTo{<:Tangent{<:Tuple}})(dx::Tuple)
339339
end
340340
# Here map will fail if the lengths don't match, but gives a much less helpful error:
341341
dy = map((f, x) -> f(x), project.elements, dx)
342-
if all(d -> d isa AbstractZero, dy)
342+
if dy isa Tuple{Vararg{AbstractZero}}
343343
return NoTangent()
344344
else
345345
return project_type(project)(dy...)
@@ -392,7 +392,7 @@ function (project::ProjectTo{<:Tangent{<:Tuple}})(dx::AbstractArray)
392392
end
393393
dy = reshape(dx, axes(project.elements)) # allows for dx::OffsetArray
394394
dz = ntuple(i -> project.elements[i](dy[i]), length(project.elements))
395-
if all(d -> d isa AbstractZero, dy)
395+
if dz isa Tuple{Vararg{AbstractZero}}
396396
return NoTangent()
397397
else
398398
return project_type(project)(dz...)

‎test/projection.jl‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ struct NoSuperType end
173173
pt3 = ProjectTo(([1, 2, 3], false, :gamma)) # partly non-differentiable
174174
@test pt3((1:3, 4, 5)) == Tangent{Tuple{Vector{Int}, Bool, Symbol}}([1.0, 2.0, 3.0], NoTangent(), NoTangent())
175175
@test ProjectTo((true, [false])) isa ProjectTo{NoTangent}
176+
177+
pt2 = ProjectTo((1.0, 2.0))
178+
@test @inferred(pt2([3, 4])) == Tangent{Tuple{Float64,Float64}}(3.0, 4.0)
179+
@test @inferred(pt2((ZeroTangent(), 1))) ==
180+
Tangent{Tuple{Float64,Float64}}(ZeroTangent(), 1.0)
181+
@test @inferred(pt2((ZeroTangent(), NoTangent()))) === NoTangent()
182+
@test pt2([ZeroTangent(), NoTangent()]) === NoTangent()
183+
184+
ptb = ProjectTo((true, 1.0))
185+
@test @inferred(ptb((2.0, ZeroTangent()))) === NoTangent()
186+
@test ptb(Any[2.0, ZeroTangent()]) === NoTangent() # collapse all-zero after projection
187+
@test ptb([2.0, 3.0]) == Tangent{Tuple{Bool,Float64}}(NoTangent(), 3.0)
176188
end
177189

178190
@testset "Base: NamedTuple" begin

0 commit comments

Comments
 (0)