Skip to content

Project dy in the add! of the getindex InplaceableThunk - #875

Open
devmotion wants to merge 2 commits into
mainfrom
dmw/getindex-add
Open

devmotion wants to merge 2 commits into
mainfrom
dmw/getindex-add

Conversation

@devmotion

@devmotion devmotion commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes the first part of #863. This is an alternative to #872.

In the InplaceableThunk of the getindex/view rrules, add! adds the raw dy, but val applies ProjectTo(x). As a result, add! throws for complex cotangents of real arrays and gives a different result than val for mixed precision.

This PR changes add! to:

  • project dy with ProjectTo(@views x[inds...]) and accumulate only into dx[inds...] if ProjectTo(x) acts elementwise (arrays of numbers or arrays of arrays). The scalar case uses a 0-dim view, so it doesn't scalar-index GPU arrays.
  • fall back to add!!(dx, ∇getindex(x, dy, inds...)) otherwise, e.g. for Diagonal or Symmetric.

The slice branch of ∇getindex uses the same projection. Side effects:

  • Both paths now throw a DimensionMismatch if dy doesn't match the shape of the slice. Previously a scalar or length-1 dy was broadcast silently. Trailing singleton dimensions are still accepted.
  • unthunk no longer throws for a Vector{ZeroTangent} cotangent.

The scalar OneElement path is unchanged.

Update (2026-11-20)

  • Rebased on main (version 1.74.15).
  • Made keep inferred in the unique rrule. eachslice(x; dims) is not inferred, and since ∇getindex now builds dx from the projected cotangent, the unique(x; dims) pullback was no longer inferred.

🤖 Generated with Claude Code

devmotion and others added 2 commits November 20, 2026 22:19
`add!` added the raw `dy`, whereas `val` applies `ProjectTo(x)`, so `add!`
failed for complex cotangents of real arrays and disagreed with `val` for
mixed precision (#863). `add!` now projects `dy` onto `x[inds...]` if
`ProjectTo(x)` acts elementwise and falls back to `add!!(dx, val)` otherwise.

Both paths now throw a `DimensionMismatch` if `dy` does not match the slice,
instead of silently broadcasting a scalar or length-1 `dy`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`eachslice(x; dims)` is not inferred, so `keep` was `Any`, and with the
projected cotangent the `∇getindex` result was no longer inferred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.77%. Comparing base (98dc481) to head (f74495e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #875      +/-   ##
==========================================
+ Coverage   96.65%   96.77%   +0.11%     
==========================================
  Files          28       28              
  Lines        3615     3625      +10     
==========================================
+ Hits         3494     3508      +14     
+ Misses        121      117       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

1 participant