Skip to content

Commit

Permalink
Feature/add frontend function pytorch eigh (#22637)
Browse files Browse the repository at this point in the history
Co-authored-by: nathzi1505 <[email protected]>
  • Loading branch information
AliTarekk and p3jitnath authored Sep 9, 2023
1 parent c98b5a3 commit a6307a8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ivy/functional/frontends/torch/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,9 @@ def vector_norm(input, ord=2, dim=None, keepdim=False, *, dtype=None, out=None):
return ivy.vector_norm(
input, axis=dim, keepdims=keepdim, ord=ord, out=out, dtype=dtype
)

@with_supported_dtypes(
{"2.0.1 and below": ("float32", "float64", "complex32", "complex64", "complex128")}, "torch",
)
def eigh(A, UPLO="L", *, out=None):
return ivy.eigh(A, UPLO=UPLO, out=out)
45 changes: 45 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_torch/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,48 @@ def test_torch_vector_norm(
keepdim=kd,
dtype=dtype[0],
)


@handle_frontend_test(
fn_tree="torch.linalg.eigh",
dtype_and_x=_get_dtype_and_matrix(dtype="valid", square=True, invertible=True),
UPLO=st.sampled_from(("L", "U")),
)
def test_torch_eigh(
*,
dtype_and_x,
UPLO,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
dtype, x = dtype_and_x
x = np.array(x[0], dtype=dtype[0])
# make symmetric positive-definite beforehand
x = np.matmul(x.T, x) + np.identity(x.shape[0]) * 1e-3

ret, frontend_ret = helpers.test_frontend_function(
input_dtypes=dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
test_values=False,
a=x,
UPLO=UPLO,
)
ret = [ivy.to_numpy(x) for x in ret]
frontend_ret = [np.asarray(x) for x in frontend_ret]

L, Q = ret
frontend_L, frontend_Q = frontend_ret

assert_all_close(
ret_np=Q @ np.diag(L) @ Q.T,
ret_from_gt_np=frontend_Q @ np.diag(frontend_L) @ frontend_Q.T,
atol=1e-02,

)

0 comments on commit a6307a8

Please sign in to comment.