Skip to content

Commit

Permalink
Add support for copy kwarg in astype to match Array API
Browse files Browse the repository at this point in the history
  • Loading branch information
Micky774 committed Apr 11, 2024
1 parent 2215021 commit ffb25b8
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions jax/_src/numpy/lax_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2270,28 +2270,25 @@ def astype(x: ArrayLike, dtype: DTypeLike | None,
if dtype is None:
dtype = dtypes.canonicalize_dtype(float_)
dtypes.check_user_dtype_supported(dtype, "astype")
if (
issubdtype(x_arr.dtype, complexfloating)
and dtypes.isdtype(dtype, ("integral", "real floating"))
):
warnings.warn(
"Casting from complex to real dtypes will soon raise a ValueError. "
"Please first use jnp.real or jnp.imag to take the real/imaginary "
"component of your input.",
DeprecationWarning, stacklevel=2
)

out = x_arr
# convert_element_type(complex, bool) has the wrong semantics.
if np.dtype(dtype) == bool and issubdtype(x_arr.dtype, complexfloating):
out = (x_arr != _lax_const(x_arr, 0))
out = _place_array(out, device=device, copy=copy)
if issubdtype(x_arr.dtype, complexfloating):
if dtypes.isdtype(dtype, ("integral", "real floating")):
warnings.warn(
"Casting from complex to real dtypes will soon raise a ValueError. "
"Please first use jnp.real or jnp.imag to take the real/imaginary "
"component of your input.",
DeprecationWarning, stacklevel=2
)
elif np.dtype(dtype) == bool:
# convert_element_type(complex, bool) has the wrong semantics.
x_arr = (x_arr != _lax_const(x_arr, 0))

x_arr = _place_array(x_arr, device=device, copy=copy)

# We offer a more specific warning than the usual ComplexWarning so we prefer
# to issue our warning.
with warnings.catch_warnings():
warnings.simplefilter("ignore", ComplexWarning)
return lax.convert_element_type(out, dtype)
return lax.convert_element_type(x_arr, dtype)

def _place_array(x, device=None, copy=None):
# TODO(micky774): Implement in future PRs as we formalize device placement
Expand Down

0 comments on commit ffb25b8

Please sign in to comment.