[BUG] Compiled kernel segfaults on an empty matmul output on GPU
☑️ I understand it is strictly prohibited to use AI to write issues.
Describe the bug On the GPU, a compiled elementwise function segfaults when its input is the empty output of a matmul. The same code works on the CPU, without mx.compile, or when the empty input comes from mx.zeros instead of a matmul.
To Reproduce
import mlx.core as mx
x = mx.zeros((0, 32)) @ mx.zeros((32, 16))
mx.eval(mx.compile(lambda x: x * 2 + 1)(x)) # segfault
Evaluating x first did not help. mlx.nn.gelu on the same x also crashes, since it is compiled.
Expected behavior An empty (0, 16) result, as on the CPU.
Desktop (please complete the following information):
- OS Version: macOS 26.6 (Apple M5) and also on GitHub's macOS arm64 runners
- Version: 0.32.4
We hit this in KerasHub with vision-language models on text-only batches, where the image branch runs on zero-size arrays.
I'd like to work on this. I'll fix the compiled GPU path so an empty matmul output returns an empty tensor instead of segfaulting, and add a regression test covering the reported mx.compile case. I'll open a PR shortly.