Repository navigation
[Metal] Fix fill loop in layer_norm - #4772
Conversation
|
Thanks for fixing this. Your change is correct and gives the same behaviour as the fix on my branch. I pushed it yesterday but couldn't open a PR because GitHub said “open pull request limit has been reached for this user”. #4717 was still open then and merged today. My version clamps The results may be useful here too. On an M5 Pro, using real builds of main at 56c2bf9 with and without the fix, the comparison gave bit-for-bit identical outputs across 900 arrays covering 60 row lengths, 3 dtypes, forward and all gradients. The full suite ran 1,005 tests and reported OK, with 56 skipped. For float16 rows of 16, forward went from 91.52 to 8.55 ms (10.7x) and backward from 94.84 to 14.63 ms (6.5x), which is close to your M2 Pro results. |
e7fa01f to
56c7a16
Compare
Co-authored-by: Arup Kanti Dash <30518803+ArupKantiDas@users.noreply.github.com>
56c7a16 to
9b4b828
Compare
|
Thanks for the heads up, apologies for that. I went ahead and added you as a co-author, and adopted your method for clamping n, since I believe it serves as a more robust solution. This should keep your PR slot open, so you have the ability to make any other PRs if you wish, as an added bonus. I am also fine with closing this one, if you wish to make a PR off your own branch and have it be your name alone. |
|
Thanks for adding me as a co-author and adopting the clamp. I'm happy to be a co-author on the commit and let #4772 go ahead, so there's no need to close it or for me to open a separate PR. |
Fixes #4759
In the single-row forward and vjp kernels,
n = axis_size - lid * N_READSis negative for threads after the end of the row. The fill loop wrote out of bounds because of this, up to 240 iterations. This keeps the whole SIMD group waiting. Lettingn = max(int(axis_size) - int(lid * N_READS), 0)in both kernels keeps behavior as intended without undefined behavior, without slowdowns.On an M2 Pro, fp16, 2^26 values per call, in milliseconds:
Forward (
mx.fast.layer_norm)Backward (grad w.r.t. x, w, b)
Outputs are identical on main and this PR across 521 row sizes, three dtypes, forward and backward.
Thanks to ArupKantiDas for flagging the issue and creating a fix independently.