Repository navigation
Launch fast.cross_entropy in slices past the Metal grid limit - #4776
Merged
zcbenz merged 1 commit intoOct 31, 2026
Merged
Conversation
The forward and VJP kernels run one threadgroup per row as a single grid of n_rows * threadgroup_size threads. Metal truncates a grid dimension past UINT32_MAX without an error, so with a small vocabulary (32-thread threadgroups) rows past 2^27 were never written. Use dispatch_threadgroups_in_slices like softmax and the norm kernels (ml-explore#4717), binding each buffer at its row offset. n_rows is now int64_t.
zcbenz
force-pushed
the
fix-cross-entropy-grid-slices
branch
from
October 31, 2026 02:14
8dcbe76 to
25b70f6
Compare
zcbenz
approved these changes
Oct 31, 2026
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.
Issue
fast.cross_entropy has the possibility of silently skipping rows when the Metal grid gets too large. This is the same kind of issue as #4624, fixed in #4717.
The launch uses 1 threadgroup per row and dispatches n_rows * threadgroup_size threads in a single grid. Metal truncates the grid dimension past UINT32_MAX without reporting an error. With up to 128 classes, the threadgroup has 32 threads, so this happens at 2^27 rows. Rows beyond the wrapped grid are never written, leaving those outputs with their previous buffer contents.
Fix
This uses
dispatch_threadgroups_in_slicesfor forward and VJP, the same helper used by softmax since #4717. Each slice binds the buffers at its starting row, so the kernels don't need any changes.n_rowsis nowint64_tandcross_entropy_dimsbecomescross_entropy_threadgroup_sizesince it only needs to return the threadgroup size. Below the limit there is still 1 slice and 1 dispatch.Test and reproduction
AI Assistant ran the comparison on an M5 Pro with 48 GB, using distinct random rows with seed 0, random targets and 8 classes. The reference was calculated in NumPy float64 in chunks of 2^22 rows. A row was counted as wrong if the error was greater than 1e-3.
At 2^27 - 16 rows, float32 had 0 wrong rows in forward and VJP on PyPI 0.32.4, a clean build of main at 8329918 and the fix.
At 2^27 + 2^20 rows:
The full suite on the cross_entropy-only commit ran 1,010 tests and reported OK, with 56 skipped. pre-commit also passes.
No new test is included because the 2^27-row tests in #4717 were removed as too slow. I can add one if wanted.
Benchmarks
These compare a clean build of main at 8329918 against the fix on the same base. Timings are milliseconds, using the median of 3 alternating runs per build. Each run takes the median of 7 batches of 20 iterations.
The largest difference is 1.06%, with the fix faster in that case. The 3 runs of a single build varied by up to 4.7%.