Skip to content
Merged
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
2 changes: 1 addition & 1 deletion mlx/backend/cpu/gemms/bnns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions mlx/backend/cpu/gemms/simd_bf16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void matmul<bfloat16_t>(
K,
lda,
ldb,
ldc,
alpha,
beta);
}
Expand Down
1 change: 1 addition & 0 deletions mlx/backend/cpu/gemms/simd_fp16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void matmul<float16_t>(
K,
lda,
ldb,
ldc,
alpha,
beta);
}
Expand Down
5 changes: 3 additions & 2 deletions mlx/backend/cpu/gemms/simd_gemm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void load_block(
}

// lda and ldb are the row strides of a and b as stored (K x M for a
// transposed a). c is dense, with row stride N.
// transposed a). ldc is the row stride of c.
template <typename T, typename AccT>
void simd_gemm(
const T* a,
Expand All @@ -50,6 +50,7 @@ void simd_gemm(
int K,
int lda,
int ldb,
int ldc,
float alpha,
float beta) {
constexpr int block_size = 16;
Expand Down Expand Up @@ -128,7 +129,7 @@ void simd_gemm(
// Store
for (int ii = 0; ii < block_size && i * block_size + ii < M; ++ii) {
for (int jj = 0; jj < block_size && j * block_size + jj < N; ++jj) {
auto c_idx = (i * block_size + ii) * N + j * block_size + jj;
auto c_idx = (i * block_size + ii) * ldc + j * block_size + jj;
if (beta != 0) {
c[c_idx] = static_cast<T>(
alpha * c_block[ii * block_size + jj] + beta * c[c_idx]);
Expand Down
1 change: 1 addition & 0 deletions mlx/backend/cpu/gemms/simd_low_precision_gemm.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ void matmul_lowp(
K,
lda,
ldb,
ldc,
alpha,
beta);
}
Expand Down
4 changes: 2 additions & 2 deletions mlx/backend/cpu/scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void scan_dispatch(
auto op = [](U y, T x) {
if constexpr (is_floating_point_v<U>) {
if (std::isnan(y) || std::isnan(static_cast<U>(x))) {
return std::numeric_limits<U>::quiet_NaN();
return static_cast<U>(std::numeric_limits<float>::quiet_NaN());
}
}
return x < y ? x : y;
Expand All @@ -245,7 +245,7 @@ void scan_dispatch(
auto op = [](U y, T x) {
if constexpr (is_floating_point_v<U>) {
if (std::isnan(y) || std::isnan(static_cast<U>(x))) {
return std::numeric_limits<U>::quiet_NaN();
return static_cast<U>(std::numeric_limits<float>::quiet_NaN());
}
}
return x < y ? y : x;
Expand Down
14 changes: 14 additions & 0 deletions python/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,20 @@ def test_cummax_cummin_nan(self):
msg=f"{op} reverse={reverse} inclusive={inclusive} "
f"arr={arr}\ngot={got}\nexp={expected}",
)
for dtype in (mx.float16, mx.bfloat16):
got = getattr(mx, op)(
a_mx.astype(dtype), reverse=reverse, inclusive=inclusive
)
self.assertEqual(got.dtype, dtype)
self.assertTrue(
np.array_equal(
np.array(got.astype(mx.float32)),
expected,
equal_nan=True,
),
msg=f"{op} {dtype} reverse={reverse} "
f"inclusive={inclusive} arr={arr}",
)

def test_diff(self):
a = mx.array([1, 2, 4, 7, 0])
Expand Down
39 changes: 39 additions & 0 deletions tests/blas_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,42 @@ TEST_CASE("test segmented_mm in bfloat16 and float16 on the cpu") {
}
}
}

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