Repository navigation
Conversation
devmotion
force-pushed
the
dmw/tangent-broadcastable
branch
from
October 29, 2026 22:34
f3c3d59 to
d10dfcf
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #724 +/- ##
==========================================
+ Coverage 93.54% 93.65% +0.11%
==========================================
Files 15 15
Lines 1022 1024 +2
==========================================
+ Hits 956 959 +3
+ Misses 66 65 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Tangent of tuples like their backing tupleTangents like their primal
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
force-pushed
the
dmw/tangent-broadcastable
branch
from
October 29, 2026 22:53
225ac8f to
a35f1b0
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tangentdefines noBroadcast.broadcastable, so broadcasting over one falls back tocollect, which iterates over the backing and produces aVector. That is wrong in different ways depending on the primal:This PR makes a
Tangentbroadcast like its primal:(1, 2) .+ 1 == (2, 3)), so the tangent of a tuple broadcasts like its backing tuple, asindexed_iterateandmapalready treat it.Tangent{Any}(1.0, [2.0]) .* 2now returns(2.0, [4.0]), which is inferrable.AbstractZero. TheNamedTuple(orDict) backing only stores the fields; it is not a collection that the primal broadcasts over.Tangent{Foo}(; y=2.0, x=1.0) .* 2now returnsTangent{Foo}(; x=2.0, y=4.0), andTangent{Dict}(Dict(:a => 1.0)) .* 2returnsTangent{Dict}(Dict(:a => 2.0)).Neither choice works for both. Forwarding all tangents to their backing would make struct tangents throw (
broadcasting over dictionaries and NamedTuples is reserved), and treating tuple tangents as scalars would stop them from lining up with the elements of the tuple.Why this matters for AD
The pullbacks of ChainRules' broadcast rules broadcast the output cotangent together with the per-element pullbacks, so the cotangent must broadcast with the same shape as the output. With ChainRules + Zygote:
Tangent{Any,<:Tuple}.collectturned it into aVector, often with an abstract eltype, so all downstream code was inferred on abstract types; in Loading BlockTensorKit triggers severe gradient compilation slowdown for ordinary TensorMaps QuantumKitHub/BlockTensorKit.jl#86 this took minutes. With this PR the cotangent stays aTuple(see Skip inferring the trailing-dims reduction in tupleunbroadcastfor vectors ChainRules.jl#857 and Keep tuple structure of broadcast cotangents ChainRules.jl#858).f.(Ref(x))withf(x) = S(x, 3x): the per-field values were passed to the pullback offone by one, which fails withNeed an adjoint for constructor S. Gradient is of type Float64. With this PR the gradient is correct.Broadcasting over a
Tangenttherefore no longer returns aVector: tuple tangents give aTuple, all others behave as scalars.🤖 Generated with Claude Code