Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mlx/backend/cuda/layer_norm.cu
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ struct BlockBroadcastReduce {
block.sync();
x = warp.thread_rank() < warp.meta_group_size() ? temp[warp.thread_rank()]
: init_value;
return cg::reduce(warp, x, op);
x = cg::reduce(warp, x, op);
// All warps must finish reading temp before the next reduction writes it.
block.sync();
return x;
}

__device__ T Sum(const T& input) {
Expand Down
68 changes: 68 additions & 0 deletions tests/cuda_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

#include <cuda_runtime.h>

#include <cmath>
#include <cstdint>
#include <limits>
#include <vector>

using namespace mlx::core;

Expand Down Expand Up @@ -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") {

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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>());
}
}
}
}