Skip to content

Fix vmap of tri_inv ignoring the triangular flags - #4748

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
mahimn01:fix-tri-inv-vmap
Oct 25, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
mahimn01:fix-tri-inv-vmap

Conversation

@mahimn01

Copy link
Copy Markdown
Contributor

Inverse::vmap always calls linalg::inv, so it ignores the tri_ and upper_ flags on the primitive. Under mx.vmap, tri_inv returns the full inverse instead of the triangular one, with no error. solve_triangular and cholesky_inv go through the same primitive, so they're affected too.

It only shows up when the unused triangle is nonzero, which happens with packed LU factors or matrices where only one triangle is meaningful.

The vmap rule was added in #849, before #1307 gave Inverse the triangular state, and it never picked up the new flags.

Repro (CPU):

import mlx.core as mx
import numpy as np

a = mx.random.normal((3, 4, 4)) + 4 * mx.eye(4)
f = lambda x: mx.linalg.tri_inv(x, upper=False, stream=mx.cpu)
print(np.allclose(mx.vmap(f)(a), f(a)))  # False
print(np.allclose(mx.vmap(f)(a), np.linalg.inv(np.array(a))))  # True

The fix calls linalg::tri_inv(a, upper_, ...) when tri_ is set and leaves the axis handling as it was.

I added test_vmap_tri_inverse, which checks upper and lower with in_axes 0 and 1 against the call without vmap. It fails before the fix and passes after. test_vmap, test_linalg and test_autograd all pass locally, and pre-commit is clean. My local build is CPU only, so CI will cover Metal.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Used AI tools to help find the bug and draft the fix. I reviewed and tested every line myself.

@zcbenz
zcbenz merged commit 001a2da into ml-explore:main Oct 25, 2026
30 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.

2 participants