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

feat(frontend): Add torch.complex, torch.polar and its tests #22979

Merged
merged 3 commits into from
Sep 18, 2023
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
34 changes: 33 additions & 1 deletion ivy/functional/frontends/torch/creation_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
to_ivy_arrays_and_back,
to_ivy_shape,
)
from ivy.func_wrapper import with_unsupported_dtypes
from ivy.func_wrapper import (
with_unsupported_dtypes,
with_supported_dtypes,
)
import ivy.functional.frontends.torch as torch_frontend


Expand Down Expand Up @@ -71,6 +74,24 @@ def asarray(
return ivy.asarray(obj, copy=copy, dtype=dtype, device=device)


@with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch")
@to_ivy_arrays_and_back
def complex(
real,
imag,
*,
out=None,
):
assert real.dtype == imag.dtype, ValueError(
"Expected real and imag to have the same dtype, "
f" but got real.dtype = {real.dtype} and imag.dtype = {imag.dtype}."
)

complex_dtype = ivy.complex64 if real.dtype != ivy.float64 else ivy.complex128
complex_array = real + imag * 1j
return complex_array.astype(complex_dtype, out=out)


@to_ivy_arrays_and_back
def empty(
*args,
Expand Down Expand Up @@ -234,6 +255,17 @@ def ones_like_v_0p4p0_and_above(
return ret


@with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch")
@to_ivy_arrays_and_back
def polar(
abs,
angle,
*,
out=None,
):
return complex(abs * angle.cos(), abs * angle.sin(), out=out)


@to_ivy_arrays_and_back
@with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch")
def range(
Expand Down
55 changes: 55 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_torch/test_creation_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,61 @@ def _start_stop_step(draw):
# ------------ #


# complex
@handle_frontend_test(
fn_tree="torch.complex",
dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("float")),
)
AnnaTz marked this conversation as resolved.
Show resolved Hide resolved
def test_complex(
*,
dtype_and_x,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
input_dtype, input = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
real=input[0],
imag=input[0],
)


# polar
@handle_frontend_test(
fn_tree="torch.polar",
dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("float")),
test_with_out=st.just(False),
)
def test_polar(
*,
dtype_and_x,
on_device,
fn_tree,
frontend,
test_flags,
backend_fw,
):
input_dtype, input = dtype_and_x
helpers.test_frontend_function(
input_dtypes=input_dtype,
backend_to_test=backend_fw,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
abs=input[0],
angle=input[0],
)


# arange
@handle_frontend_test(
fn_tree="torch.arange",
Expand Down
Loading