Repository navigation
Make the getindex add! agree with its value - #872
Open
CarloLucibello wants to merge 1 commit into
Open
CarloLucibello wants to merge 1 commit into
CarloLucibello wants to merge 1 commit into
Conversation
The `add!` of the `InplaceableThunk` returned by the `getindex` and `view` rules added the raw cotangent, skipping the `ProjectTo(x)` that `val` applies, so it could differ from `unthunk` or throw. It now projects `dy` when `ProjectTo(x)` acts elementwise, staying O(length(dy)), and adds `val` otherwise. Scalar `∇getindex` also projects `dy` before building the `OneElement`, which avoids materializing it for mixed precision. Part 1 of #863. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment on lines
+2
to
+3
| ChainRulesCore, ChainRulesTestUtils, FiniteDifferences, GPUArraysCore, JLArrays, | ||
| LinearAlgebra, Test |
Contributor
There was a problem hiding this comment.
[JuliaFormatter] reported by reviewdog 🐶
Suggested change
| ChainRulesCore, ChainRulesTestUtils, FiniteDifferences, GPUArraysCore, JLArrays, | |
| LinearAlgebra, Test | |
| ChainRulesCore, | |
| ChainRulesTestUtils, | |
| FiniteDifferences, | |
| GPUArraysCore, | |
| JLArrays, | |
| LinearAlgebra, | |
| Test |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #872 +/- ##
==========================================
- Coverage 96.65% 96.61% -0.05%
==========================================
Files 28 28
Lines 3615 3630 +15
==========================================
+ Hits 3494 3507 +13
- Misses 121 123 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Part 1 of #863. The
add!of theInplaceableThunkfrom thegetindex/viewrules added the rawdy, skipping theProjectTo(x)thatvalapplies. Sot.add!(dx)could differ fromdx + unthunk(t), or throw (e.g. a complexdyfor a realx), and an AD couldn't safely call it.ProjectTo(x)acts elementwise (arrays of numbers, including GPU arrays),add!projectsdyand adds it intodx[inds...], staying O(length(dy)). Arrays of arrays use only the projectors of the indexed elements. Other arrays (Diagonal,Symmetric, sparse, …) fall back toadd!!(dx, val).∇getindexprojectsdybefore building theOneElement, so aFloat64cotangent for aFloat32array no longer materializes a dense array.Benchmarks with Zygote 0.7.16 on Julia 1.13:
x[i]loop,x::Vector{Float32}of length 10⁴, summed inFloat64: 26.5 ms / 391 MiB → 1.80 ms / 8.3 MiB.X[:, j]loop over a 100×1000 matrix: unchanged at 36 ms / 767 MiB, since Zygote unthunks. With Zygote callingadd!when the inferred eltype ofvalfits its buffer, which this PR makes safe: 0.41 ms / 5.1 MiB.add!itself costs the same as before, on CPU and on GPU.🧔♂️🤖 Assisted by AI