You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] matmul with size greater than 2^31 gives undermined results - appears to be memory leak #3591
matmul at [12_347, 174_000] produces intermittently incorrect results. Some runs are ok, but others are wildly inaccurate. This appears to be a downcast memory address from UInt64 to Int32 somewhere in the kernel. MacBook Pro M5 Max 128 Gb.
Built a workaround using manual block multiplications. Key finding beyond the original report: sliced inputs to matmul also produce incorrect results unless explicitly materialized via slice + 0 (or equivalent). matmul(left[0..., lo..<hi], right[lo..<hi, 0...]) produces wrong results; matmul((left[0..., lo..<hi] + 0), (right[lo..<hi, 0...] + 0)) is correct.
Both conditions (large operand size and strided-view-into-large-parent) appear to trigger the same offset-arithmetic issue in the matmul kernel. The workaround below handles both by chunking and materializing each chunk.
infix operator ×:MultiplicationPrecedencefunc×(left:MLXArray,right:MLXArray)->MLXArray{letsafeLimit=2_000_000_000ifleft.size<=safeLimit&&right.size<=safeLimit{returnmatmul(left,right)}assert(left.ndim==2&&right.ndim==2,"Only for matrix multiplication")letm=left.shape[0]letk=left.shape[1]letn=right.shape[1]assert(k==right.shape[0],"Dimension Mismatch")assert(n<=safeLimit&&m<=safeLimit,"Single matrix dimension too large")letkChunk=safeLimit/max(m,n)letnumChunks=(k+kChunk-1)/kChunkvarresult=MLXArray.zeros([m,n],dtype:left.dtype)foriin0..<numChunks{letlo=i*kChunklethi=min(k,lo+kChunk)result=result+matmul(left[0...,lo..<hi]+0,right[lo..<hi,0...]+0)result.eval()}returnresult}
Activity
vansickledavid-sketch commentedon May 26, 2026
matmul at [12_347, 174_000] produces intermittently incorrect results. Some runs are ok, but others are wildly inaccurate. This appears to be a downcast memory address from UInt64 to Int32 somewhere in the kernel. MacBook Pro M5 Max 128 Gb.
Typical output:
vansickledavid-sketch commentedon May 27, 2026
Built a workaround using manual block multiplications. Key finding beyond the original report: sliced inputs to matmul also produce incorrect results unless explicitly materialized via slice + 0 (or equivalent). matmul(left[0..., lo..<hi], right[lo..<hi, 0...]) produces wrong results; matmul((left[0..., lo..<hi] + 0), (right[lo..<hi, 0...] + 0)) is correct.
Both conditions (large operand size and strided-view-into-large-parent) appear to trigger the same offset-arithmetic issue in the matmul kernel. The workaround below handles both by chunking and materializing each chunk.
vansickledavid-sketch commentedon May 27, 2026
Related
#3248: same overflow pattern in conv_general
#3327: root cause identified as ShapeElem = int32_t in mlx/array.h, affecting multiple operations
zcbenz commentedon May 27, 2026
What is the version of MLX you are using and can you also try some old versions of MLX?
vansickledavid-sketch commentedon May 28, 2026
Currently using 0.31.3. Switched to 0.25.1 and produced similar output...
Typical Output: