Repository navigation
[CUDA] Synchronize LayerNorm reduction scratch reuse #4866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nastya236
merged 7 commits into
ml-explore:main
from
strayberry:fix/cuda-layernorm-scratch-reuse
Nov 24, 2026
+83
−45
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d0af56
Synchronize CUDA LayerNorm reduction scratch reuse
strayberry 1c56b8e
fix the race in rms norm vjp
nastya236 1303f5e
Move normalization VJP regression tests to Python
strayberry c7c706f
Merge branch 'main' into fix/cuda-layernorm-scratch-reuse
strayberry 160f8fe
Merge branch 'main' into fix/cuda-layernorm-scratch-reuse
nastya236 35812c9
Merge branch 'main' into fix/cuda-layernorm-scratch-reuse
strayberry 7b927e1
fixed tests
nastya236 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,10 @@ | |
|
|
||
| #include <cuda_runtime.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <vector> | ||
|
|
||
| using namespace mlx::core; | ||
|
|
||
|
|
@@ -119,3 +121,69 @@ TEST_CASE("test eval after cuda error") { | |
| CHECK_NOTHROW(eval(a)); | ||
| CHECK(array_equal(a, ones({4})).item<bool>()); | ||
| } | ||
|
|
||
| TEST_CASE("test CUDA layer norm shared scratch") { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be removed |
||
| constexpr int width = 1024; | ||
| std::vector<float> data(width), expected(width); | ||
| double mean_value = 0; | ||
| for (int i = 0; i < width; ++i) { | ||
| data[i] = 2.0f + (i % 31 - 15) * 0.125f; | ||
| mean_value += data[i]; | ||
| } | ||
| mean_value /= width; | ||
| double variance = 0; | ||
| for (float value : data) { | ||
| variance += (value - mean_value) * (value - mean_value); | ||
| } | ||
| const double inv = 1.0 / std::sqrt(variance / width + 1e-5); | ||
| for (int i = 0; i < width; ++i) { | ||
| expected[i] = (data[i] - mean_value) * inv; | ||
| } | ||
| auto x = array(data.data(), {1, width}); | ||
| auto reference = array(expected.data(), {1, width}); | ||
| auto w = ones({width}); | ||
| auto b = zeros({width}); | ||
| eval(x, reference, w, b); | ||
| for (int i = 0; i < 8; ++i) { | ||
| auto y = fast::layer_norm(x, w, b, 1e-5f); | ||
| CHECK(allclose(y, reference, 3e-5f, 3e-5f).item<bool>()); | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("test CUDA layer norm forward and vjp") { | ||
| for (auto dtype : {float32, float16, bfloat16}) { | ||
| const float tolerance = dtype == float32 ? 1e-4f | ||
| : dtype == float16 ? 1e-2f | ||
| : 8e-2f; | ||
| for (int width : {32, 128, 1024, 4096}) { | ||
| CAPTURE(dtype); | ||
| CAPTURE(width); | ||
| auto x = astype(reshape(arange(3 * width), {3, width}) % 31, dtype) / 8; | ||
| auto w = astype(arange(width) % 5, dtype) / 16 + 0.75f; | ||
| auto b = astype(arange(width) % 3, dtype) / 8; | ||
| auto g = astype(reshape(arange(3 * width), {3, width}) % 13, dtype) / 16; | ||
| eval(x, w, b, g); | ||
| auto fused = [](const std::vector<array>& p) { | ||
| return std::vector<array>{fast::layer_norm(p[0], p[1], p[2], 1e-5f)}; | ||
| }; | ||
| auto reference = [](const std::vector<array>& p) { | ||
| auto input = astype(p[0], float32); | ||
| auto centered = input - mean(input, -1, true); | ||
| auto normalized = | ||
| centered * rsqrt(mean(square(centered), -1, true) + 1e-5f); | ||
| return std::vector<array>{ | ||
| astype(normalized, p[0].dtype()) * p[1] + p[2]}; | ||
| }; | ||
| auto actual = vjp(fused, {x, w, b}, {g}); | ||
| auto expected = vjp(reference, {x, w, b}, {g}); | ||
| CHECK(allclose(actual.first[0], expected.first[0], tolerance, tolerance) | ||
| .item<bool>()); | ||
| for (int i = 0; i < 3; ++i) { | ||
| CAPTURE(i); | ||
| CHECK( | ||
| allclose(actual.second[i], expected.second[i], tolerance, tolerance) | ||
| .item<bool>()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better place for these type of test will be python API.