Guard scatter_axis against 64-bit outputs on the GPU#3695
Open
obchain wants to merge 1 commit into
Open
Conversation
put_along_axis / scatter_add_axis with int64/uint64 values failed the Metal library JIT build instead of raising a clean error: packing_size<T> in atomic.h is sizeof(uint)/sizeof(T) == 0 for 8-byte T, producing a zero-length array and a divide-by-zero in the fallback atomic union. The plain Scatter path already guards this in scatter() and Scatter::eval_gpu; ScatterAxis had no equivalent guard. Mirror it in scatter_axis() (GPU only, matching scatter()) and ScatterAxis::eval_gpu, and add a test.
zcbenz
reviewed
Jun 17, 2026
| } | ||
|
|
||
| // TODO, remove when scatter_axis supports 64-bit outputs | ||
| if (to_stream(s).device == Device::gpu && size_of(a.dtype()) == 8) { |
Collaborator
There was a problem hiding this comment.
The check in metal/indexing.cpp along is enough, and it actually works in the cuda backend, the test should also be updated for metal only
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 #3690.
mx.put_along_axis/scatter_add_axiswith a 64-bit element dtype (int64/uint64) fail the Metal library JIT build instead of raising a clean "unsupported dtype" error. Inmlx/backend/metal/kernels/atomic.h,packing_size<T> = sizeof(uint)/sizeof(T)is0for 8-byteT, souint_or_packed<T>declares a zero-length array (hard C++ error) andoffset / packing_size<T>divides by zero. The wholemlx-metallibbuild then fails.The plain
Scatterpath already guards 8-byte outputs on the GPU — inscatter()(ops.cpp) andScatter::eval_gpu(indexing.cpp) — butScatterAxiswas missing the equivalent guard. This adds it in both places, mirroringScatter:scatter_axis()inmlx/ops.cpp— raises on GPU for 8-byte dtypes, matching the existingscatter()guard. CPU is unaffected.ScatterAxis::eval_gpuinmlx/backend/metal/indexing.cpp— same guard asScatter::eval_gpu.Repro before the fix (Metal device):
After the fix this raises a clean
ValueErroron the GPU, and continues to work on the CPU.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes