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
4 changes: 3 additions & 1 deletion mlx/backend/metal/kernels/gemv.metal
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ instantiate_gemv_bs_blocks(complex64, complex64_t);
instantiate_gemv_t(name, itype, 1, 2, 8, 4, 4, 4) \
instantiate_gemv_t(name, itype, 1, 4, 8, 4, 4, 4) \
instantiate_gemv_t(name, itype, 1, 16, 8, 4, 4, 4) \
instantiate_gemv_t(name, itype, 1, 16, 4, 8, 4, 4) // clang-format on
instantiate_gemv_t(name, itype, 1, 16, 4, 8, 4, 4) \
instantiate_gemv_t(name, itype, 1, 8, 8, 4, 4, 4) \
instantiate_gemv_t(name, itype, 4, 2, 8, 4, 4, 4) // clang-format on

// clang-format off
instantiate_gemv_t_blocks(float32, float);
Expand Down
22 changes: 17 additions & 5 deletions mlx/backend/metal/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,25 +1241,37 @@ void gemv_axbpy(
std::ostringstream kname;

if (transpose_mat) {
if (in_vector_len >= 8192 && out_vector_len >= 2048) {
// Specialized kernel for very small outputs
tn = out_vector_len < tn ? 1 : tn;

// A long K with few outputs launches too few simdgroups to fill the GPU,
// so split K or use smaller threadgroups
bool large_mat = in_vector_len >= 8192 && out_vector_len >= 2048;
size_t n_simdgroups =
batch_size_out * ceildiv(out_vector_len, (large_mat ? 8 : 4) * tn);
bool few_simdgroups = tn == 4 && in_vector_len >= 256 && n_simdgroups < 128;

if (large_mat && !few_simdgroups) {
sm = 4;
sn = 8;
} else {
sm = 8;
sn = 4;
}

if (out_vector_len >= 2048) {
if (few_simdgroups && out_vector_len < 1024) {
bm = 4;
bn = 2;
} else if (few_simdgroups && large_mat) {
bn = 8;
} else if (out_vector_len >= 2048) {
bn = 16;
} else if (out_vector_len >= 512) {
bn = 4;
} else {
bn = 2;
}

// Specialized kernel for very small outputs
tn = out_vector_len < tn ? 1 : tn;

n_out_per_tgp = bn * sn * tn;
kname << "gemv_t_" << type_to_name(out);

Expand Down
18 changes: 18 additions & 0 deletions python/tests/test_blas.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def test_matrix_vector(self):
(2, 2),
(32, 32),
(64, 64),
(300, 40),
(2048, 2048),
):
for mi in (-1, 0, 1):
Expand Down Expand Up @@ -369,6 +370,8 @@ def test_matrix_vector_batched(self):
((32, 1, 128), (128, 64)),
((1, 128), (32, 128, 64)),
((1, 8, 4, 1, 128), (1, 8, 1, 128, 6)),
((3, 1, 300), (3, 300, 40)),
((2, 1, 1, 300), (1, 3, 300, 40)),
):
self.__gemv_test(
shape_mat, shape_vec, mat_first=False, np_dtype=np_dtype
Expand Down Expand Up @@ -814,6 +817,21 @@ def test_addmm(self):
self.assertListEqual(list(d_npy.shape), list(d_mlx.shape))
self.assertTrue(np.allclose(d_mlx, d_npy, atol=1e-5))

# Matmul with vector and a long K, on a local generator to keep
# the global numpy state for later tests
rng = np.random.default_rng(0)
for k, n, batch in ((512, 128, (4,)), (8192, 2048, ())):
a_npy = rng.normal(0.0, 1.0 / 128, (k,)).astype(np.float32)
b_npy = rng.normal(0.0, 1.0 / 128, batch + (k, n)).astype(np.float32)

for c_shape in ((1,), (n,), batch + (n,)):
c_npy = np.ones(c_shape).astype(np.float32)
d_npy = alpha * (a_npy @ b_npy) + beta * c_npy
d_mlx = mx.addmm(
mx.array(c_npy), mx.array(a_npy), mx.array(b_npy), alpha, beta
)
self.assertTrue(np.allclose(d_mlx, d_npy, atol=1e-5))

# Matmul with vector
a_npy = np.random.normal(0.0, 1.0 / 128, (32, 128, 16)).astype(np.float32)
b_npy = np.random.normal(0.0, 1.0 / 128, (16,)).astype(np.float32)
Expand Down
Loading