Skip to content

Call MLX linalg ops directly on batched inputs instead of mx.vmap - #2425

Merged
jessegrabowski merged 4 commits into
pymc-devs:mainfrom
jessegrabowski:mlx-natively-batched-linalg
Sep 18, 2026
Merged

jessegrabowski merged 4 commits into
pymc-devs:mainfrom
jessegrabowski:mlx-natively-batched-linalg

Conversation

@jessegrabowski

@jessegrabowski jessegrabowski commented Sep 17, 2026

Copy link
Copy Markdown
Member

funcify_Blockwise on MLX now calls a core function directly when that function sets natively_batched = True, after broadcasting the inputs to the common batch shape. Before, every Blockwise went through nested mx.vmap. Every mx.linalg dispatch sets the flag, the same way numba's core functions set handles_out. mx.vmap has no rule for the LUF and QRF primitives, so batched solve, lu, lu_factor, qr, det and slogdet raised. Its rule for solve_triangular calls linalg::inv with the triangular flags dropped, so a batched solve_triangular or cho_solve returned the full solve with nothing raised.

The solve dispatches expand a vector b to a column and squeeze it back. mx.linalg.solve only special-cases a 1-d b, and a batched vector rhs is 2-d.

PivotToPermutations stays on mx.vmap. It is a hand-written scatter loop with no mx.linalg equivalent.

The two test_mlx_eigh failures on this branch are the MLX 0.32.2 eigenvector sign flip that #2424 fixes.

Closes #2385. Supersedes and closes #2399.

@ricardoV94 ricardoV94 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we open an issue with them to implement the vmap rules for these on MLX. Silly to have to patch around it.

@ricardoV94

Copy link
Copy Markdown
Member

Add other PR as co-author?

jessegrabowski and others added 4 commits September 18, 2026 16:38
Co-authored-by: guillaume-osmo <guillaume@osmo.ai>
Co-authored-by: guillaume-osmo <guillaume@osmo.ai>
Co-authored-by: guillaume-osmo <guillaume@osmo.ai>
@jessegrabowski
jessegrabowski force-pushed the mlx-natively-batched-linalg branch from efa6548 to 4362114 Compare September 18, 2026 09:14
@jessegrabowski
jessegrabowski merged commit 54c16ae into pymc-devs:main Sep 18, 2026
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MLX: batched solve fails with "[Primitive::vmap] Not implemented for LUF"

2 participants