Give a zero remainder the sign of the divisor - #4316
Open
krrishapatel wants to merge 1 commit into
Open
Conversation
mx.remainder returns zero with a sign that depends on which code path the
element took, so lane 8 of a length-9 array disagrees with lanes 0 to 7 on
identical inputs, and a strided view disagrees with a contiguous array of the
same values.
The floored fixup skips zero:
if (r != 0 && (r < 0 != b < 0)) {
r += b;
}
so the zero keeps whatever sign the path produced. The Accelerate SIMD body and
the scalar residual do not agree on that sign, and neither matches NumPy, which
gives a zero remainder the sign of the divisor.
The guard cannot be dropped, since adding b to a zero returns b itself, so the
zero case sets the sign directly instead. Integers are untouched: they have no
signed zero, and adding the divisor there would be wrong.
Fixed in all four backends. Verified on CPU; the Metal and CUDA changes are the
same edit to the same guard and are untested locally.
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.
Proposed changes
Fixes #4315.
mx.remaindergives a zero result the sign of whichever code path produced it. All nine inputs here are-0.0 % 3.0:Lane 8 is the scalar residual. A strided view disagrees with a contiguous array the same way, and neither matches numpy, which gives a zero remainder the sign of the divisor.
The floored fixup skips zero:
so the zero keeps whatever sign it had, and
asd::remainderandstd::remainderdo not agree on that. The guard cannot just be dropped, since addingbto a zero returnsbitself, so the zero case sets the sign directly. Integers are untouched, they have no signed zero. Non-zero results are unchanged.The same guard was in all four backends, so all four are fixed. CPU is verified locally on an M-series Mac. Metal and CUDA are the same edit to the same guard but I cannot run them here, so please treat those two as unverified.
test_remainder_signed_zerocovers the residual lane, the strided view, both divisor signs against numpy, the%operator, and that integers are unaffected. It fails onmainat the first assertion.No overlap with #4108. That one floors the quotient for
divmodand integerfloor_divide; this is the sign of a zero remainder. They share two files but different structs,FloorDivideversusRemainder. I merged #4108 into this branch to check: no conflicts, both fixes active,test_ops.pygreen with both test sets.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes