Skip to content

api: Fix staggered sum indices - #3012

Open
mloubout wants to merge 5 commits into
mainfrom
fix-staggered-sum-indices
Open

api: Fix staggered sum indices#3012
mloubout wants to merge 5 commits into
mainfrom
fix-staggered-sum-indices

Conversation

@mloubout

Copy link
Copy Markdown
Contributor

Misc FD (and bundle) bug fixes I ran into with some adjoint/gradient tests

@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.71%. Comparing base (4109b58) to head (09c6005).

Files with missing lines Patch % Lines
devito/core/gpu.py 0.00% 1 Missing and 2 partials ⚠️
devito/passes/iet/definitions.py 92.00% 2 Missing ⚠️
devito/ir/clusters/algorithms.py 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3012      +/-   ##
==========================================
+ Coverage   83.68%   83.71%   +0.03%     
==========================================
  Files         257      257              
  Lines       54711    54820     +109     
  Branches     4686     4693       +7     
==========================================
+ Hits        45785    45894     +109     
+ Misses       8115     8114       -1     
- Partials      811      812       +1     
Flag Coverage Δ
pytest-gpu-aomp-amdgpuX 68.47% <55.26%> (-0.03%) ⬇️
pytest-gpu-gcc- 78.35% <90.15%> (+0.03%) ⬆️
pytest-gpu-icx- 78.28% <90.15%> (+0.05%) ⬆️
pytest-gpu-nvc-nvidiaX 69.13% <60.52%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

Where `expr` sits along `dim`, or None if its terms disagree.
"""
try:
indices = {i.indices_ref[dim] for i in expr.args} if expr.is_Add \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ultra-nitpicking, I'm not fond of this style when u have to break to new line like that anyway

Comment thread devito/types/utils.py Outdated
class DimensionTuple(EnrichedTuple):

def __getitem_hook__(self, dim):
# An exact hit wins over a `_defines` overlap. A derived Dimension

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these AI-written comments are uselessly verbose and thus only confusing, I think the first sentence until the full stop is enough

Comment thread devito/types/utils.py
@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch from e96d4ca to 6993249 Compare August 28, 2026 16:01
`__getitem_hook__` matched on `_defines` overlap alone. A derived Dimension
carries its parent in `_defines`, so for a Bundle indexed by
`(p_rec, rp_recx)` -- `rp_recx` being a `CustomDimension` whose parent is
`p_rec` -- the lookup for `rp_recx` matched the `p_rec` entry first and
returned the number of sparse points where the number of interpolation
weights was meant.

That size becomes the innermost stride in `_generate_fsz`, so the receiver
kernels of a vectorized Operator read `w[p*npoint + rp]` instead of
`w[p*2 + rp]` and run off the end of the array. Observed as an out-of-bounds
`__global__` read under compute-sanitizer and a run-to-run varying, sometimes
NaN, elastic TTI gradient on CUDA.

Try an exact hit before falling back to the overlap, in both
`__getitem_hook__` and `dindex`.
@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch from 6993249 to 5580d46 Compare August 28, 2026 16:12
`Add` reports its first argument's `indices_ref`, so a sum whose terms sit at
different staggered locations names a position only one of them has, and `x0`
gets resolved against it for all of them. The shear strain `v_x.dy + v_y.dx`
of a staggered velocity is the canonical case: both terms land on the cell
corner, so a shift onto it should be a no-op, and instead each picked up a
spurious one.

Differentiation is linear at every order, so split such a sum in
`Derivative._eval_fd`. Relative error on `D(a+b)` against `D(a) + D(b)` was
0.63 at order 0, 1.20 at order 1 and 0.95 at order 2, with `expand=False` at
order 2 returning exactly zero.

`generic_derivative` also short-circuited a zeroth order derivative only when
`x0` was empty, building a stencil around an expression already sitting at
`x0`. `index_at` answers where an expression sits, and both call sites use it.
@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch from 5580d46 to 8e805ab Compare August 28, 2026 17:11
A LocalObject carries expressions in its constructor arguments and in its
initializer, and both end up in the generated code, but FindApplications only
visited Expressions, Iterations and Calls. Any macro they apply was therefore
left undefined -- ROUND_UP, say, for an auto-padded stride reaching a plan
descriptor.
@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch from 49f0b43 to 6a5a4a4 Compare August 31, 2026 11:05
@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch 4 times, most recently from 634dd6d to 9547cd7 Compare August 31, 2026 13:45
# would otherwise ask for a zero-init of its own, ad infinitum
self.zero_init = (options or {}).get('zero-init', True)

def _zero_init(self, obj, storage):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we actually need an extra method like this, why not putting everything in _make_zero_init ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes it easier for subclass to only have to implement the init and not have to put back the check

for d, (h0, h1), s in zip(
obj.dimensions, obj._size_halo, obj.symbolic_shape, strict=True
for d, (h0, h1), (p0, p1), s in zip(
obj.dimensions, obj._size_halo, obj._size_padding, obj.symbolic_shape,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it must be a symbolic padding or it's wrong (an operator override would kill it)

@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch 3 times, most recently from f374bc2 to e312bc4 Compare August 31, 2026 16:42
@mloubout
mloubout force-pushed the fix-staggered-sum-indices branch from e312bc4 to 09c6005 Compare August 31, 2026 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants