Skip to content

Give a zero remainder the sign of the divisor - #4316

Open
krrishapatel wants to merge 1 commit into
ml-explore:mainfrom
krrishapatel:fix-remainder-signed-zero
Open

Give a zero remainder the sign of the divisor#4316
krrishapatel wants to merge 1 commit into
ml-explore:mainfrom
krrishapatel:fix-remainder-signed-zero

Conversation

@krrishapatel

Copy link
Copy Markdown

Proposed changes

Fixes #4315.

mx.remainder gives a zero result the sign of whichever code path produced it. All nine inputs here are -0.0 % 3.0:

np.signbit(np.array(mx.remainder(mx.full((9,), -0.0), mx.full((9,), 3.0)))).astype(int)
# [0 0 0 0 0 0 0 0 1]

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:

if (r != 0 && (r < 0 != b < 0)) {
  r += b;
}

so the zero keeps whatever sign it had, and asd::remainder and std::remainder do not agree on that. The guard cannot just be dropped, since adding b to a zero returns b itself, 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_zero covers the residual lane, the strided view, both divisor signs against numpy, the % operator, and that integers are unaffected. It fails on main at the first assertion.

No overlap with #4108. That one floors the quotient for divmod and integer floor_divide; this is the sign of a zero remainder. They share two files but different structs, FloorDivide versus Remainder. I merged #4108 into this branch to check: no conflicts, both fixes active, test_ops.py green with both test sets.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

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

[BUG] mx.remainder gives a zero result a path-dependent sign

1 participant