Fix integer pow zeroing a whole SIMD vector on a negative exponent - #4354
Open
ayaangazali wants to merge 1 commit into
Open
Fix integer pow zeroing a whole SIMD vector on a negative exponent#4354ayaangazali wants to merge 1 commit into
ayaangazali wants to merge 1 commit into
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.
What is wrong
On the CPU Accelerate backend,
mx.poweron integers zeroes an entire SIMD vector when any single element has a negative exponent, so correct positive-exponent elements come back as 0.Only the element with exponent -1 should be 0 (mlx defines integer-to-negative-power as 0). Instead the whole 8-lane int32 register that element lands in is zeroed, so
2 ** 3silently returns 0 for its neighbors.Why
simd::powfor integers bailed out for the whole vector as soon as one lane was negative:any(exp < 0)is true if a single lane is negative, and thereturn 0then applies to all N lanes. The scalar path (base_simd.h, N == 1) and the Metal/CUDA kernels are per element and are not affected, so this is specific to the vectorized Accelerate path and only shows up when negative and non-negative exponents share a register.What this changes
Run the existing exponentiation loop unconditionally and mask to 0 only the lanes whose exponent is negative:
Negative lanes never satisfy
exp > 0, so they stay at the identity through the loop and are set to 0 at the end; the arithmetic right shift keeps them negative so the final mask still selects them. The all-negative fast path is preserved (thewhilenever runs), and theexp < 0compare is guarded byif constexpr (std::is_signed_v<T>)so the unsigned instantiations do not trip-Wtype-limitsunder-DCMAKE_COMPILE_WARNING_AS_ERROR=ON.Added a mixed-sign case to
test_integer_power; it fails on main ([0, 0, 0, 0, 0, 0, 0, 0, 8, ...]) and passes with this change. Full C++ suite (251 cases) andtest_opspass.Sent by a beginner running Claude Code, so I checked it carefully: reverted the header on a fresh build to confirm the new test fails without the fix, and reran the C++ and ops suites after restoring it. Please tell me if anything needs another pass.