Skip to content

BUG: Off by one in memory overlap check#26972

Merged
seberg merged 2 commits into
numpy:mainfrom
yairchu:overlap-off-by-one
Jul 19, 2024
Merged

BUG: Off by one in memory overlap check#26972
seberg merged 2 commits into
numpy:mainfrom
yairchu:overlap-off-by-one

Conversation

@yairchu

@yairchu yairchu commented Jul 17, 2024

Copy link
Copy Markdown
Contributor

An off-by-one in the nomemoverlap check caused heisenbugs in platforms where the SIMD and non-SIMD code paths produced different results, due to arrays which semi-randomly got allocated adjacent to the input array failed the test and used the uncommon non-SIMD fallback.

This PR was originally part of #26956, but split at the request of @seberg as well as fixed after a bug he noticed in the originally suggested fix.

@yairchu yairchu changed the title BUG: Overlap off by one BUG: Off by one in memory overlap check Jul 17, 2024
@yairchu yairchu force-pushed the overlap-off-by-one branch 2 times, most recently from a329ef2 to d19ae28 Compare July 17, 2024 19:52

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I do think we should add a test reverse strides arr[::-1] (all four combinations of it).

I like passing on len, that seems like a nice clean solution. One suggestion for the core change, but not needed, it does look right.

Comment thread numpy/_core/tests/test_regression.py Outdated
Comment thread numpy/_core/tests/test_regression.py Outdated
Comment thread numpy/_core/src/umath/loops_utils.h.src Outdated
@seberg

seberg commented Jul 19, 2024

Copy link
Copy Markdown
Member

Not important for this PR, but for general awareness: NumPy ufuncs are actually protected against overlap with the exception of the ufunc.accumulate implementation which relies on it. In that case dst and one src have an offset of exactly one stride.
Not sure it is useful to use that information, but it might be good to keep it in mind (we could even pass that information into the ufunc if we update the loop signatures to have the new extra info).

yairchu added 2 commits July 19, 2024 11:52
The test fails when compiling on clang with "-ffp-contract=off", due to a combination of factors:
* Difference between SIMD and non-SIMD loops
* nomemoverlap falling back to non-SIMD loop due to off-by-one bug
The original check (since introduced in 085cdbe) returned overlap for adjacent arrays.

This fixes one of the factors causing test_repeated_square_consistency failure, so the test should now always pass.
@yairchu yairchu force-pushed the overlap-off-by-one branch from d19ae28 to 9d68e6e Compare July 19, 2024 09:02
@seberg

seberg commented Jul 19, 2024

Copy link
Copy Markdown
Member

Thanks @yairchu, let's put it in.

In case anyone ever comes back to it: The test is somewhat interesting, but doesn't guarantee much unfortunately. To really test this, you would need a unit-test that gives different results for SIMD vs. non-SIMD paths (and even then, you cannot really test memory overlapping part easily).

@seberg seberg merged commit 472b938 into numpy:main Jul 19, 2024
@yairchu yairchu deleted the overlap-off-by-one branch July 19, 2024 13:02
@ngoldbaum

ngoldbaum commented Jul 19, 2024

Copy link
Copy Markdown
Member

EDIT: I posted something incorrect here.

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.

4 participants