Repository navigation
Stop integer power from hanging on a negative exponent - #4100
Merged
Merged
Conversation
zcbenz
reviewed
Aug 9, 2026
Contributor
Author
There was a problem hiding this comment.
Removed, sorry about that. It was an editor settings file my git add -A swept in. The branch is now just base_simd.h and test_ops.py, and I have excluded the directory locally so it cannot happen again.
1 task done
rcfa
added a commit
to rcfa/mlx
that referenced
this pull request
Oct 23, 2026
The facade's integer pow looped forever on a negative exponent. Return 0, as base_simd does since upstream ml-explore#4100.
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
Raising an integer to a negative power hangs the CPU backend:
The integer branch of
powinmlx/backend/cpu/simd/base_simd.his exponentiation by squaring:exp >>= 1on a negative signed value is an arithmetic shift, so-1 >> 1is-1and the loop never reaches zero. Any negative exponent spins, for every signed integer width.Both GPU backends already handle this and settled on returning 0, so this is the CPU falling out of step rather than an open question:
Added the same guard to the CPU path, kept behind
if constexpr (std::is_signed_v<T>)so the unsigned instantiations do not get an always-false comparison under-DCMAKE_COMPILE_WARNING_AS_ERROR=ON.test_integer_poweralready had a case for this, added with the comment# Doesn't hang:It never hung because nothing evaluates
x, so the graph was built and dropped. The eval is now there, which is what makes it fail on main, along with the same check across the four signed widths.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 8c28c38,test_ops.py149 tests pass. Checked that unsigned integer powers, positive exponents, a zero exponent and float powers are all unchanged. Worth flagging that on main the new assertions hang rather than fail, since that is the bug.freshman contributor, i use Claude Code while working through these. found it by running ops against numpy for odd inputs and having the whole script time out with no output, which took a couple of passes to narrow down to one line. numpy raises for this instead of returning 0, but i matched what Metal and CUDA already do here rather than introduce a third behaviour.