Skip to content

Fix getindex - #202

Merged
devmotion merged 1 commit into
masterfrom
dw/getindex
Jun 5, 2022
Merged

devmotion merged 1 commit into
masterfrom
dw/getindex

Conversation

@devmotion

Copy link
Copy Markdown
Member

This PR fixes getindex for non-Int indices (fixes #139), removes undesired definitions for getindex caused by Varargs, adds @propagate_inbounds annotations, and adds tests.

@devmotion

Copy link
Copy Markdown
Member Author

As a general observation, I think it would be nice if it would be possible to e.g. rely on to_index/to_indices and use a more orthogonal design, i.e., without specifying getindex for all kinds of indices.

@codecov-commenter

codecov-commenter commented May 25, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #202 (acbb3ff) into master (553ea01) will decrease coverage by 2.94%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #202      +/-   ##
==========================================
- Coverage   85.14%   82.19%   -2.95%     
==========================================
  Files          18       18              
  Lines        1811     1528     -283     
==========================================
- Hits         1542     1256     -286     
- Misses        269      272       +3     
Impacted Files Coverage Δ
src/tracked.jl 88.42% <100.00%> (-3.89%) ⬇️
src/derivatives/broadcast.jl 76.62% <0.00%> (-13.94%) ⬇️
src/tape.jl 55.55% <0.00%> (-9.03%) ⬇️
src/api/hessians.jl 84.00% <0.00%> (-3.50%) ⬇️
src/api/tape.jl 72.22% <0.00%> (-2.78%) ⬇️
src/api/utils.jl 84.84% <0.00%> (-2.06%) ⬇️
src/derivatives/elementwise.jl 77.48% <0.00%> (-1.33%) ⬇️
src/derivatives/linalg/reductions.jl 70.31% <0.00%> (-1.30%) ⬇️
src/derivatives/scalars.jl 95.74% <0.00%> (-0.98%) ⬇️
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 553ea01...acbb3ff. Read the comment docs.

@devmotion
devmotion merged commit 4a50321 into master Jun 5, 2022
@yebai
yebai deleted the dw/getindex branch June 5, 2022 14:03
devmotion added a commit that referenced this pull request Sep 22, 2026
…, and pin #126 (#302)

* Test indexing a zero-dimensional `TrackedArray` (#126)

`gradient(x -> x[], fill(1.0))` threw a `MethodError` for an ambiguity between
the `AbstractRange...` and `Colon...` methods of `getindex`: a bare vararg
matches zero arguments, so `t[]` matched both equally.

4a50321 fixed it incidentally while fixing #202, by rewriting every
`getindex(t::TrackedArray, i::T...)` as `i1::T, is::T...`. Requiring a leading
index sends `t[]` down `Base`'s generic zero-index `AbstractArray` path, which
lands on the `Integer` method via linear indexing -- so, like any scalar
`getindex`, it aliases its parent through the origin and records nothing.

Nothing pinned that, and the ambiguity would come back unnoticed with any
future widening of those signatures.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Give tracked reals with a free origin a `zero` and a `one` (fixes #172)

`map`ping over a tracked array mixes elements that carry an origin with ones
that do not, and the resulting eltype leaves the origin free. `reducedim_init`
allocates a reduction's accumulator by asking that eltype for its `zero`, or
`one` for `prod`, which fell through to `convert(T, 0)` and then to `valtype`,
defined only for a pinned origin:

    MethodError: no method matching valtype(::Type{TrackedReal{Float64, Float64}})

Neither identity depends on the origin, so both are deducible from the type
alone, as `zero` requires. `Nothing` is the origin `zero` has always used, and
`TrackedReal{V,D,Nothing}` is an instance of `TrackedReal{V,D}`, so the
documented `zero(T) isa T` holds. The signature matches that type exactly
rather than its subtypes: a `Union` of two origins is also a subtype, and no
single member of it could be returned.

The `zero` method is the one #173 added and #177 reverted wholesale over #175,
whose cause -- `increment_deriv!` indexing into untracked elements -- was fixed
by #300. `one` is its counterpart, which #173 never added.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem with getindex for ReverseDiff.TrackedArray if Integer is a UInt16

3 participants