Make views of TrackedArray return a TrackedArray - #291
Merged
Merged
Conversation
`Base.view` had no `TrackedArray` method, so it fell through to the generic `SubArray` constructor and returned a `SubArray` of `TrackedReal`s. That is an array of individually tracked scalars rather than a single tracked array, so every subsequent operation recorded one instruction per element and the tape grew linearly with the size of the view. `view` now returns a `TrackedArray` backed by views of the parent's value and deriv arrays. Since the deriv array is shared with the parent, derivatives accumulate into the parent directly and no instruction has to be recorded at all. Views that are not `IndexLinear` cannot back a `TrackedArray` and are materialized through `getindex` instead. This also fixes logical indexing in the generic `getindex`: since `Bool <: Integer`, masks were routed there but never normalized, so `false` indexed element 0 and `x[mask]` threw a `BoundsError`. Indices are now normalized with `Base.to_indices`. Fixes #281 Co-Authored-By: Claude Opus 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 #291 +/- ##
==========================================
+ Coverage 85.10% 85.91% +0.80%
==========================================
Files 18 18
Lines 1914 1945 +31
==========================================
+ Hits 1629 1671 +42
+ Misses 285 274 -11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
`BitVector(::Tuple)` needs `BitArray{1}(itr)`, which Julia 1.0 does not
define — it has only the unparameterized `BitArray(itr)`. Construct from
a `Vector{Bool}` literal instead, which hits `BitArray{1}(::Array{Bool,1})`
and works on all supported versions.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Fixes #281.
Base.viewhad noTrackedArraymethod, so it returned aSubArrayofTrackedReals — an array of individually tracked scalars rather than a single tracked array. Every subsequent operation then recorded one instruction per element, so the tape grew linearly with the size of the view:dx[2:end]@view x[2:end]viewnow returns aTrackedArraybacked by views of the parent's value and deriv arrays. Since the deriv array is shared with the parent, derivatives accumulate into the parent directly and no instruction has to be recorded at all — the tape length becomes 1 and is constant ind. Views that are notIndexLinearcannot back aTrackedArrayand are materialized throughgetindexinstead.Gradient timings from the issue:
dThis also fixes logical indexing in the generic
getindex, which is a separate pre-existing bug: sinceBool <: Integer, masks were routed there but never normalized, sofalseindexed element 0 andx[mask]threw aBoundsError. Indices are now normalized withBase.to_indices, which handlesColons and masks axis-aware and covers whole-array logical indexing such asm[mask]. Theviewfix relies on this, since logical views are notIndexLinearand so go throughgetindex.🤖 Generated with Claude Code