Send out of range trig arguments to libm - #4157
Merged
Merged
Conversation
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
mx.sinandmx.cosreturn values outside[-1, 1]for large finite inputs on the CPU:numpy and pytorch both return the right answer for all of these (
-0.48750603forsin(1e10)).The cause is the Cephes range reduction in
sincos:yis a float, so it stops being an exact integer above2^24, and the conversion overflowsuint32above2^32. The extended precision correction that follows subtractsy * DP1..DP3fromx, so onceyis wrong the reduced argument is wrong, and past the overflow it is garbage. Degradation starts around1e8, becomes gross at1e9, and reachesinfat1e10.Arguments that the reduction cannot represent exactly now go to libm instead.
2^23is the cutoff, which keepsx * 4/pibelow2^24with room to spare:Those match
np.sinexactly. Across 2000 values drawn from[-1e12, 1e12]plus±1e30and±3.4e38, the maximum difference from numpy is now0for bothsinandcos, nothing is NaN, andnan/±infinputs still givenanas before.The fast path is unchanged for ordinary arguments, since the branch is one
anyover the block. 4M float32 in[-10, 10], 20 iterations of each op, stayed at 0.054 s forsinand 0.053 s forcos, and accuracy there is unchanged at6e-8against numpy.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesCPU only build (
MLX_BUILD_METAL=OFF) at e78d894,test_ops.py150 tests pass and the new assertion fails on main.test_autograd.pyandtest_nn.pyalso pass. I only touched the CPU path, so the Metal and CUDA kernels are untouched.freshman contributor, i work through these alongside Claude Code. found it by running every unary op over a fixed set of awkward values and diffing against both numpy and pytorch rather than numpy alone, which is what made this one stand out: the two references agreed with each other and not with us.