Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for linalg.lu #8227

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion experimental/torch_xla2/test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"linalg.ldl_factor_ex",
"linalg.ldl_solve",
"linalg.lstsq",
"linalg.lu",
"linalg.lu_factor",
"linalg.lu_factor_ex",
"linalg.lu_solve",
Expand Down
32 changes: 32 additions & 0 deletions experimental/torch_xla2/torch_xla2/ops/jaten.py
Original file line number Diff line number Diff line change
Expand Up @@ -2259,10 +2259,42 @@ def _aten_linalg_eig(A):
def _aten_linalg_eigh(A, UPLO='L'):
return jnp.linalg.eigh(A, UPLO)


@op(torch.ops.aten.linalg_lu)
def _aten_linalg_lu(A, pivot=True, out=None):
dtype = A.dtype

*_, m, n = A.shape
k = jnp.minimum(m, n)

lu, _, permutation = jax.lax.linalg.lu(A)

L = jnp.tril(lu[..., :, :k], k=-1)
eye_L = jnp.eye(m, k, dtype=dtype)
L = L + eye_L

U = jnp.triu(lu[..., :k, :])

def perm_to_P(perm):
m = perm.shape[-1]
P = jnp.eye(m, dtype=dtype)[perm].T
return P

if permutation.ndim > 1:
num_batch_dims = permutation.ndim - 1
for _ in range(num_batch_dims):
perm_to_P = jax.vmap(perm_to_P, in_axes=0)

P = perm_to_P(permutation)

return P,L,U


@op(torch.ops.aten.gcd)
def _aten_gcd(input, other):
return jnp.gcd(input, other)


# aten.lcm
@op(torch.ops.aten.lcm)
def _aten_lcm(input, other):
Expand Down
Loading