Skip to content

Commit

Permalink
Merge branch 'unifyai:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
xingshuodong authored Sep 10, 2023
2 parents 6565579 + 09163ec commit 8cf2a80
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 52 deletions.
3 changes: 3 additions & 0 deletions ivy/functional/backends/tensorflow/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def asarray(
try:
ret = tf.convert_to_tensor(obj, dtype)
except (TypeError, ValueError):
obj = (
obj if isinstance(obj, tf.Tensor) else tf.convert_to_tensor(obj, tf.float64)
)
ret = tf.cast(obj, dtype)
return tf.identity(ret) if copy else ret

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from_zero_dim_arrays_to_scalar,
handle_numpy_out,
)
import ivy.functional.frontends.numpy as np_frontend


@handle_numpy_out
Expand Down Expand Up @@ -114,9 +115,10 @@ def prod(
initial=None,
where=True,
):
if ivy.is_array(where):
if where is not True:
x = ivy.where(where, x, ivy.default(out, ivy.ones_like(x)), out=out)
if initial is not None:
initial = np_frontend.array(initial, dtype=dtype).tolist()
if axis is not None:
s = ivy.to_list(ivy.shape(x, as_array=True))
s[axis] = 1
Expand Down
14 changes: 8 additions & 6 deletions ivy/functional/frontends/torch/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def eigh(a, /, UPLO="L", out=None):
return ivy.eigh(a, UPLO=UPLO, out=out)


@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)


@to_ivy_arrays_and_back
@with_supported_dtypes(
{"2.0.1 and below": ("float32", "float64", "complex32", "complex64")}, "torch"
Expand Down Expand Up @@ -390,9 +398,3 @@ 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)
89 changes: 44 additions & 45 deletions ivy_tests/test_ivy/test_frontends/test_torch/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,50 @@ def test_torch_eigh(
)


@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,
)


# eigvals
@handle_frontend_test(
fn_tree="torch.linalg.eigvals",
Expand Down Expand Up @@ -1364,48 +1408,3 @@ 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,

)
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ requests
pyvis
dill
astunparse
patchelf

0 comments on commit 8cf2a80

Please sign in to comment.