Add mapreduce_layout hook for reductions on LayoutArrays - #281
Merged
Merged
Conversation
`mapreduce(f, op, A)` for a `LayoutArray` (or a view of one) now calls `mapreduce_layout(MemoryLayout(A), f, op, A, dims; kw...)`. By default this falls back to Base's implementation. Layouts can overload it to reduce more efficiently, e.g. to avoid iterating over the zeros of an infinite padded array. Since `sum`, `prod`, `maximum`, `minimum` and `extrema` all go through `mapreduce`, a single overload covers them all. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #281 +/- ##
===========================================
+ Coverage 0.00% 90.24% +90.24%
===========================================
Files 11 12 +1
Lines 1858 1969 +111
===========================================
+ Hits 0 1777 +1777
+ Misses 1858 192 -1666 ☔ 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.
Adds a layout hook for reductions, alongside the existing
_normhook:sum,prod,maximum,minimum,extremaandreduceall end inmapreduce, so one overload per layout covers them all. The default falls back to Base, so behaviour doesn't change unless a layout opts in.dimsandinitare passed through.Motivation: padded arrays in LazyArrays. For example,
sum(zeros(∞,2))andsum(zeros(2,∞))currently loop forever, because Base iterates over all the zeros. A LazyArrays PR will overloadmapreduce_layoutforAbstractPaddedLayoutso that it reduces over the padded data only.any,allandcountdon't go throughmapreducein Base, so they aren't affected.Testing:
sum,maximum,extrema,prodandmapreduce(withdimsandinit) match Base forLayoutArrays and their views.Bumps the version to v1.13.0 since this adds a new overloadable function.
🤖 Generated with Claude Code