Repository navigation
Make unzip of tuples inferable and validate its input - #859
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #859 +/- ##
=======================================
Coverage 96.63% 96.63%
=======================================
Files 28 28
Lines 3596 3598 +2
=======================================
+ Hits 3475 3477 +2
Misses 121 121 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
unzip(xs::Tuple)can't be inferred:ntuple(i -> map(Get(i), xs), length(x1))buildsGet(i)from a runtimei, so the components are inferred withAnyelements:This becomes visible with JuliaDiff/ChainRulesCore.jl#724, where the cotangent of a tuple-valued broadcast stays a
Tupleinstead of beingcollected into aVector.unzip_broadcastthen takes its tuple branch, andtest_rrule(copy∘broadcasted, BT1, atan, Tuple(rand(3)), Tuple(rand(3)))fails its inference check (the only failure in the ChainRules integration job of that PR).This PR dispatches on
Tuple{Vararg{NTuple{N,Any}}}, a non-empty tuple of tuples of equal lengthN, and forwards to the existingunzip(xs, Val(N))method, as the array path already does. After this change:Invalid inputs now throw descriptive errors instead of failing in
first, throwing aBoundsError, or silently dropping elements:()ArgumentError: tuple must be non-emptyArgumentError: unzip cannot determine the number of components of an empty tuple((1, 2), 3)BoundsErrorArgumentError: unzip only accepts arrays or tuples of tuples((1, 2), [3, 4])((1, 3), (2, 4))ArgumentError: unzip only accepts arrays or tuples of tuples((1, 2), (3,))BoundsErrorDimensionMismatch: unzip requires tuples of equal length, got lengths (2, 1)((1,), (2, 3))((1, 2),)DimensionMismatch: unzip requires tuples of equal length, got lengths (1, 2)Inside ChainRules, all elements come from the same pullback, so they are always tuples of the same length. The empty-tuple broadcast test (
atanover()and()) does not reachunzip.This supersedes the
unzippart of #858. The_bc_cotangentpart of #858 is no longer needed with JuliaDiff/ChainRulesCore.jl#724. It is independent of #857; both bump the version to 1.74.9, so whichever is merged second needs a trivial rebase.Tests
@inferredtests for valid inputs, and tests for each of the invalid inputs above. Locally on Julia 1.13:test/unzipped.jlpasses with Throw when broadcasting over structural tangents ChainRulesCore.jl#724;ntuplecall withunzip(xs, Val(length(x1)))and gives the same results for valid inputs,test/unzipped.jlandrulesets/Base/broadcastpassed with ChainRulesCore 1.26.7, and the failingatantest passed with Throw when broadcasting over structural tangents ChainRulesCore.jl#724.The full test suite was not run locally.
🤖 Generated with Claude Code