Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions mlx/backend/cpu/gemms/bnns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void matmul_bnns(
/* BNNSDataLayout layout = */ BNNSDataLayoutRowMajorMatrix,

/* size_t size[BNNS_MAX_TENSOR_DIMENSION] = */
{lda, (M * K) / lda, 0, 0, 0, 0, 0, 0},
{a_transposed ? M : K, a_transposed ? K : M, 0, 0, 0, 0, 0, 0},
/* size_t stride[BNNS_MAX_TENSOR_DIMENSION] = */
{1, lda, 0, 0, 0, 0, 0, 0},

Expand All @@ -90,7 +90,7 @@ void matmul_bnns(
/* BNNSDataLayout layout = */ BNNSDataLayoutRowMajorMatrix,

/* size_t size[BNNS_MAX_TENSOR_DIMENSION] = */
{ldb, (K * N) / ldb, 0, 0, 0, 0, 0, 0},
{b_transposed ? K : N, b_transposed ? N : K, 0, 0, 0, 0, 0, 0},
/* size_t stride[BNNS_MAX_TENSOR_DIMENSION] = */
{1, ldb, 0, 0, 0, 0, 0, 0},

Expand All @@ -111,7 +111,7 @@ void matmul_bnns(
/* size_t size[BNNS_MAX_TENSOR_DIMENSION] = */
{N, M, 0, 0, 0, 0, 0, 0},
/* size_t stride[BNNS_MAX_TENSOR_DIMENSION] = */
{1, N, 0, 0, 0, 0, 0, 0},
{1, ldc, 0, 0, 0, 0, 0, 0},

/* void * _Nullable data = */ nullptr,
/* BNNSDataType data_type = */ bnns_dtype,
Expand Down
39 changes: 39 additions & 0 deletions tests/blas_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,42 @@ TEST_CASE("test matmul") {
out = matmul(transpose(a, {0, 2, 1}), transpose(b, {0, 2, 1}));
CHECK(array_equal(out, full({2, 4, 4}, 2.0f)).item<bool>());
}

TEST_CASE("test segmented_mm float16 and bfloat16") {
auto a_f32 = reshape(arange(0, 4 * 12, float32), {4, 12}) * 0.1f;
auto b_f32 = reshape(arange(0, 12 * 6, float32), {12, 6}) * 0.1f;
auto segments = array({0u, 3u, 3u, 8u, 8u, 12u}, {3, 2}, uint32);

for (auto dt : {float16, bfloat16}) {
auto a = astype(a_f32, dt);
auto b = astype(b_f32, dt);
auto a_ref = astype(a, float32);
auto b_ref = astype(b, float32);

// Non-transposed
auto expected = astype(segmented_mm(a_ref, b_ref, segments), float32);
auto actual = astype(segmented_mm(a, b, segments), float32);
CHECK(allclose(actual, expected, 1e-2, 1e-2).item<bool>());

// Transposed a
auto a_t = transpose(
astype(reshape(arange(0, 12 * 4, float32), {12, 4}) * 0.1f, dt));
auto a_t_ref = astype(a_t, float32);
expected = astype(segmented_mm(a_t_ref, b_ref, segments), float32);
actual = astype(segmented_mm(a_t, b, segments), float32);
CHECK(allclose(actual, expected, 1e-2, 1e-2).item<bool>());

// Transposed b
auto b_t = transpose(
astype(reshape(arange(0, 6 * 12, float32), {6, 12}) * 0.1f, dt));
auto b_t_ref = astype(b_t, float32);
expected = astype(segmented_mm(a_ref, b_t_ref, segments), float32);
actual = astype(segmented_mm(a, b_t, segments), float32);
CHECK(allclose(actual, expected, 1e-2, 1e-2).item<bool>());

// Both transposed
expected = astype(segmented_mm(a_t_ref, b_t_ref, segments), float32);
actual = astype(segmented_mm(a_t, b_t, segments), float32);
CHECK(allclose(actual, expected, 1e-2, 1e-2).item<bool>());
}
}
Loading