[CUDA][Performance] Topk is slow
Open
Nobody has claimed this yet.
performance
- Dominant language
- C++
- Stars
- 28.5k
- Forks
- 2.3k
- Avg merge
- 3d 8h
- Merged PRs (30d)
- 62
Description
MLX topk on CUDA is pretty slow in some cases (especially compared to PyTorch).
Here is a benchmark:
import time
import mlx.core as mx
b = 2048
v = 8192
k = 32
q = mx.random.normal(shape=(b, v)).astype(mx.bfloat16)
def fun(q):
for _ in range(50):
idx = mx.argpartition(-q, kth=k-1, axis=-1)[:, :k]
values = mx.take_along_axis(q, idx, axis=-1)
q = mx.put_along_axis(q, idx, values, axis=-1)
mx.eval(q)
for _ in range(20):
fun(q)
tic = time.time()
for _ in range(20):
fun(q)
toc = time.time()
ms = 1e3 * (toc - tic)
print(f"MLX {ms=:.3f}")
import torch
q = torch.randn(size=(b, v)).to("cuda").to(torch.bfloat16)
def topk_old(q):
return idx, values
def fun(q):
for _ in range(50):
values, idx = torch.topk(q, k=k, axis=-1)
q = torch.scatter(q, -1, idx, values)
torch.cuda.synchronize()
for _ in range(20):
fun(q)
tic = time.time()
for _ in range(20):
fun(q)
toc = time.time()
ms = 1e3 * (toc - tic)
print(f"PyTorch {ms=:.3f}")
On a spark:
MLX ms=2975.014
PyTorch ms=919.764
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Python benchmark to reproduce the CUDA slowdown, then trace the CUDA path used by mlx.core.argpartition and compare it with the PyTorch topk case. Done means the benchmark shows substantially improved MLX timing for the reported shape and dtype without changing the result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python, pytorch
- Domain
- machine-learning, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100