From e8926a9f75e678eb673a347e901a144410610e31 Mon Sep 17 00:00:00 2001 From: yanghaojin Date: Tue, 13 Oct 2026 23:49:06 +0800 Subject: [PATCH 1/2] Compute grouped GEMM pointers in 64 bits gather_mm with sorted indices returned wrong rows once the weight tensor passed 2 GiB (512 x 1024 x 2560 bf16): group * item_size * b_batch_stride overflowed int. --- .../cuda/gemms/grouped_gemm_unaligned.cu | 10 +++++++--- python/tests/test_blas.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu b/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu index 319c759c22..25dc465c08 100644 --- a/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu +++ b/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu @@ -87,9 +87,13 @@ __global__ void prepare_grouped_mm_data( out_lds[group] = N; // Fill pointers. auto offset = group == 0 ? 0 : cum_histo[group - 1]; - a_ptrs[group] = a_start + offset * item_size * a_batch_stride; - b_ptrs[group] = b_start + group * item_size * b_batch_stride; - out_ptrs[group] = out_start + offset * item_size * out_batch_stride; + // In 64 bits: an expert tensor past 2 GiB overflowed the int product. + a_ptrs[group] = + a_start + static_cast(offset) * item_size * a_batch_stride; + b_ptrs[group] = + b_start + static_cast(group) * item_size * b_batch_stride; + out_ptrs[group] = + out_start + static_cast(offset) * item_size * out_batch_stride; } } diff --git a/python/tests/test_blas.py b/python/tests/test_blas.py index eaed51dae7..7b0b596da6 100644 --- a/python/tests/test_blas.py +++ b/python/tests/test_blas.py @@ -1,6 +1,7 @@ # Copyright © 2023-2024 Apple Inc. import math +import unittest import mlx.core as mx import mlx_tests @@ -1574,6 +1575,22 @@ def gather_mm_test(a, b, rhs): c2 = gather_mm_test(a, b, rhs) self.assertTrue(mx.allclose(c1, c2, rtol=tol, atol=tol)) + @unittest.skipIf(not mx.cuda.is_available(), "CUDA grouped GEMM path only") + def test_gather_mm_sorted_large_weights(self): + # More than 2**31 bytes of weights: the per-group pointer offsets must + # not overflow 32 bits. + if mx.device_info().get("total_memory", 0) < 16 * 2**30: + self.skipTest("needs 16 GB of device memory") + mx.random.seed(0) + E, N, K, T = 512, 1024, 2560, 64 + w = (mx.random.normal((E, N, K)) * 0.02).astype(mx.bfloat16) + rhs = mx.sort(mx.random.randint(0, E, (T,)).astype(mx.uint32)) + rhs[-1] = E - 1 + a = mx.random.normal((T, 1, K)).astype(mx.bfloat16) + ref = a.astype(mx.float32) @ w[rhs].astype(mx.float32).swapaxes(-1, -2) + out = mx.gather_mm(a, w.swapaxes(-1, -2), rhs_indices=rhs, sorted_indices=True) + self.assertTrue(mx.allclose(out.astype(mx.float32), ref, rtol=1e-2, atol=1e-2)) + def test_gather_mm_sorted_vjp(self): def gather_mm_ref(a, b, rhs): b = b[rhs] From 4600d210a5fb061a1602520132602b49e55970dd Mon Sep 17 00:00:00 2001 From: Cheng Date: Thu, 15 Oct 2026 08:54:48 +0900 Subject: [PATCH 2/2] test using too much ram is not practical to put in tests --- .../cuda/gemms/grouped_gemm_unaligned.cu | 1 - python/tests/test_blas.py | 17 ----------------- 2 files changed, 18 deletions(-) diff --git a/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu b/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu index 25dc465c08..c27c6be1a7 100644 --- a/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu +++ b/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu @@ -87,7 +87,6 @@ __global__ void prepare_grouped_mm_data( out_lds[group] = N; // Fill pointers. auto offset = group == 0 ? 0 : cum_histo[group - 1]; - // In 64 bits: an expert tensor past 2 GiB overflowed the int product. a_ptrs[group] = a_start + static_cast(offset) * item_size * a_batch_stride; b_ptrs[group] = diff --git a/python/tests/test_blas.py b/python/tests/test_blas.py index 7b0b596da6..eaed51dae7 100644 --- a/python/tests/test_blas.py +++ b/python/tests/test_blas.py @@ -1,7 +1,6 @@ # Copyright © 2023-2024 Apple Inc. import math -import unittest import mlx.core as mx import mlx_tests @@ -1575,22 +1574,6 @@ def gather_mm_test(a, b, rhs): c2 = gather_mm_test(a, b, rhs) self.assertTrue(mx.allclose(c1, c2, rtol=tol, atol=tol)) - @unittest.skipIf(not mx.cuda.is_available(), "CUDA grouped GEMM path only") - def test_gather_mm_sorted_large_weights(self): - # More than 2**31 bytes of weights: the per-group pointer offsets must - # not overflow 32 bits. - if mx.device_info().get("total_memory", 0) < 16 * 2**30: - self.skipTest("needs 16 GB of device memory") - mx.random.seed(0) - E, N, K, T = 512, 1024, 2560, 64 - w = (mx.random.normal((E, N, K)) * 0.02).astype(mx.bfloat16) - rhs = mx.sort(mx.random.randint(0, E, (T,)).astype(mx.uint32)) - rhs[-1] = E - 1 - a = mx.random.normal((T, 1, K)).astype(mx.bfloat16) - ref = a.astype(mx.float32) @ w[rhs].astype(mx.float32).swapaxes(-1, -2) - out = mx.gather_mm(a, w.swapaxes(-1, -2), rhs_indices=rhs, sorted_indices=True) - self.assertTrue(mx.allclose(out.astype(mx.float32), ref, rtol=1e-2, atol=1e-2)) - def test_gather_mm_sorted_vjp(self): def gather_mm_ref(a, b, rhs): b = b[rhs]