Skip to content

Preserve tuple cotangent types in ChainRules pullbacks - #329

Merged
lkdvos merged 3 commits into
mainfrom
crc-backing
Nov 19, 2026
Merged

lkdvos merged 3 commits into
mainfrom
crc-backing

Conversation

@Yue-Zhengyuan

@Yue-Zhengyuan Yue-Zhengyuan commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Broadcasting unthunk over a tuple cotangent wrapped in Tangent collects its components into a vector, losing their individual types. Unpack the cotangent in the pullback argument before mapping unthunk to preserve concrete component types.

using ChainRulesCore, LinearAlgebra

Δ = Tangent{Any}(
    Thunk(() -> ones(ComplexF64, 2, 2)),
    Diagonal(ones(2)),
    Thunk(() -> ones(ComplexF64, 2, 2)),
)

typeof(unthunk.(Δ))
# Vector{AbstractMatrix}

materialize((ΔU, ΔS, ΔVᴴ)) = map(unthunk, (ΔU, ΔS, ΔVᴴ))

typeof(materialize(Δ))
# Tuple{Matrix{ComplexF64}, Diagonal{Float64, Vector{Float64}},
#       Matrix{ComplexF64}}

Argument unpacking also preserves support for a thunk around the entire cotangent container: ChainRulesCore's iteration methods evaluate the outer thunk, while map(unthunk, ...) evaluates any component thunks.

materialize_pair((ΔQ, ΔR)) = map(unthunk, (ΔQ, ΔR))
Δ = (ones(2, 2), Diagonal(ones(2)))

materialize_pair(Thunk(() -> Δ)) == materialize_pair(Δ)
# true

The tuple-preserving materialization is applied to QR, LQ, eigen, SVD, and polar pullbacks.

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for upstreaming this!

@lkdvos
lkdvos enabled auto-merge (squash) November 19, 2026 02:07
@Yue-Zhengyuan

Copy link
Copy Markdown
Member Author

Just find out that first explicitly unpack the cotangent also works:

ΔU, ΔS, ΔVᴴ = ΔUSVᴴ′
ΔUSVᴴ = map(unthunk, (ΔU, ΔS, ΔVᴴ))

This may be more readable. @lkdvos Which approach do you prefer?

@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
ext/MatrixAlgebraKitChainRulesCoreExt.jl 72.15% <100.00%> (-0.62%) ⬇️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really have a strong preference, but it's nice to not have to use the backing, so I like this

@lkdvos
lkdvos merged commit 81435d1 into main Nov 19, 2026
34 checks passed
@lkdvos
lkdvos deleted the crc-backing branch November 19, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants