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

Added nanprod to ivy experimental API #21198

Merged
merged 13 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 60 additions & 0 deletions ivy/data_classes/array/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,66 @@ def nanmean(
self._data, axis=axis, keepdims=keepdims, dtype=dtype, out=out
)

def nanprod(
self: ivy.Array,
/,
*,
axis: Optional[Union[Tuple[int], int]] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype]] = None,
out: Optional[ivy.Array] = None,
keepdims: Optional[bool] = False,
initial: Optional[Union[int, float, complex]] = None,
where: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
ivy.Array instance method variant of ivy.nanprod. This method simply wraps the
function, and so the docstring for ivy.prod also applies to this method with
minimal changes.

Parameters
----------
self
Input array.
axis
Axis or axes along which the product is computed.
The default is to compute the product of the flattened array.
dtype
The desired data type of returned array. Default is None.
out
optional output array, for writing the result to.
keepdims
If this is set to True, the axes which are reduced are left in the result
as dimensions with size one. With this option, the result will broadcast
correctly against the original a.
initial
The starting value for this product.
where
Elements to include in the product

Returns
-------
ret
The product of array elements over a given axis treating
Not a Numbers (NaNs) as ones

Examples
--------
>>> a = ivy.array([[1, 2], [3, ivy.nan]])
>>> a.nanprod(a)
6.0
>>> a.nanprod(a, axis=0)
ivy.array([3., 2.])
"""
return ivy.nanprod(
self._data,
axis=axis,
keepdims=keepdims,
dtype=dtype,
out=out,
initial=initial,
where=where,
)

def quantile(
self: ivy.Array,
q: Union[ivy.Array, float],
Expand Down
134 changes: 134 additions & 0 deletions ivy/data_classes/container/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,140 @@ def nanmean(
self, axis=axis, keepdims=keepdims, dtype=dtype, out=out
)

@staticmethod
def static_nanprod(
input: ivy.Container,
/,
*,
axis: Optional[Union[Tuple[int], int, ivy.Container]] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype, ivy.Container]] = None,
keepdims: Optional[Union[bool, ivy.Container]] = False,
key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None,
to_apply: Union[bool, ivy.Container] = True,
prune_unapplied: Union[bool, ivy.Container] = False,
map_sequences: Union[bool, ivy.Container] = False,
out: Optional[Union[ivy.Array, ivy.Container]] = None,
initial: Optional[Union[int, float, complex, ivy.Container]] = 1,
where: Optional[Union[ivy.Array, ivy.Container]] = None,
) -> ivy.Container:
"""
ivy.Container static method variant of ivy.nanprod. This method simply wraps the
function, and so the docstring for ivy.nanprod also applies to this method with
minimal changes.

Parameters
----------
input
Input container including arrays.
axis
Axis or axes along which the product is computed.
The default is to compute the product of the flattened array.
dtype
The desired data type of returned array. Default is None.
out
optional output array, for writing the result to.
keepdims
If this is set to True, the axes which are reduced are left in the result
as dimensions with size one. With this option, the result will broadcast
correctly against the original a.
initial
The starting value for this product.
where
Elements to include in the product

Returns
-------
ret
The product of array elements over a given axis treating
Not a Numbers (NaNs) as ones

Examples
--------
>>> a = ivy.Container(x=ivy.array([[1, 2], [3, ivy.nan]]),\
y=ivy.array([[ivy.nan, 1, 2], [1, 2, 3]])
>>> ivy.Container.static_moveaxis(a)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean static_nanprod, careful when copying examples from other functions

{
x: 12.0
y: 12.0
}
"""
return ContainerBase.cont_multi_map_in_function(
"nanprod",
input,
axis=axis,
keepdims=keepdims,
dtype=dtype,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
initial=initial,
where=where,
)

def nanprod(
self: ivy.Container,
/,
*,
axis: Optional[Union[Tuple[int], int, ivy.Container]] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype, ivy.Container]] = None,
keepdims: Optional[Union[bool, ivy.Container]] = False,
out: Optional[ivy.Container] = None,
initial: Optional[Union[int, float, complex, ivy.Container]] = None,
where: Optional[Union[ivy.Array, ivy.Container]] = None,
) -> ivy.Container:
"""
ivy.Container instance method variant of ivy.nanprod. This method simply wraps
the function, and so the docstring for ivy.nanprod also applies to this method
with minimal changes.

Parameters
----------
self
Input container including arrays.
axis
Axis or axes along which the product is computed.
The default is to compute the product of the flattened array.
dtype
The desired data type of returned array. Default is None.
out
optional output array, for writing the result to.
keepdims
If this is set to True, the axes which are reduced are left in the result
as dimensions with size one. With this option, the result will broadcast
correctly against the original a.
initial
The starting value for this product.
where
Elements to include in the product

Returns
-------
ret
The product of array elements over a given axis treating
Not a Numbers (NaNs) as ones

Examples
--------
>>> a = ivy.Container(x=ivy.array([[1, 2], [3, ivy.nan]]),\
y=ivy.array([[ivy.nan, 1, 2], [1, 2, 3]])
>>> ivy.Container.static_moveaxis(a)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise. Also this should be a.nanprod(axis, etc...)

{
x: 12.0
y: 12.0
}
"""
return self.static_nanprod(
self,
axis=axis,
keepdims=keepdims,
dtype=dtype,
out=out,
initial=initial,
where=where,
)

@staticmethod
def static_quantile(
a: Union[ivy.Container, ivy.Array, ivy.NativeArray],
Expand Down
18 changes: 18 additions & 0 deletions ivy/functional/backends/jax/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ def nanmean(
return jnp.nanmean(a, axis=axis, keepdims=keepdims, dtype=dtype, out=out)


def nanprod(
a: JaxArray,
/,
*,
axis: Optional[Union[int, Sequence[int]]] = None,
dtype: Optional[jnp.dtype] = None,
keepdims: Optional[bool] = False,
out: Optional[JaxArray] = None,
initial: Optional[Union[int, float, complex]] = None,
where: Optional[JaxArray] = None,
) -> JaxArray:
dtype = ivy.as_native_dtype(dtype)
if dtype is None:
dtype = _infer_dtype(a.dtype)
axis = tuple(axis) if isinstance(axis, list) else axis
return jnp.nanprod(a, axis=axis, keepdims=keepdims, dtype=dtype, out=out)


def quantile(
a: JaxArray,
q: Union[float, JaxArray],
Expand Down
23 changes: 23 additions & 0 deletions ivy/functional/backends/numpy/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ def nanmean(
nanmean.support_native_out = True


def nanprod(
a: np.ndarray,
/,
*,
axis: Optional[Union[int, Sequence[int]]] = None,
dtype: Optional[np.dtype] = None,
keepdims: Optional[bool] = False,
out: Optional[np.ndarray] = None,
initial: Optional[Union[int, float, complex]] = None,
where: Optional[np.ndarray] = None,
) -> np.ndarray:
dtype = ivy.as_native_dtype(dtype)
if dtype is None:
dtype = _infer_dtype(a.dtype)
axis = tuple(axis) if isinstance(axis, list) else axis
return np.asarray(
np.nanprod(a=a, axis=axis, dtype=dtype, keepdims=keepdims, out=out)
)


nanprod.support_native_out = True


def quantile(
a: np.ndarray,
q: Union[float, np.ndarray],
Expand Down
36 changes: 36 additions & 0 deletions ivy/functional/backends/paddle/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@ def nanmean(
return ret.astype(ret_dtype)


def _infer_dtype(dtype: paddle.dtype):
default_dtype = ivy.infer_default_dtype(dtype)
if ivy.dtype_bits(dtype) < ivy.dtype_bits(default_dtype):
return default_dtype
return dtype


def nanprod(
a: paddle.Tensor,
/,
*,
axis: Optional[Union[int, Tuple[int]]] = None,
keepdims: Optional[bool] = False,
dtype: Optional[paddle.dtype] = None,
out: Optional[paddle.Tensor] = None,
initial: Optional[Union[int, float, complex]] = None,
where: Optional[paddle.Tensor] = None,
) -> paddle.Tensor:
dtype = ivy.as_native_dtype(dtype)
if dtype is None:
dtype = _infer_dtype(a.dtype)
if a.dtype not in [paddle.int32, paddle.int64, paddle.float32, paddle.float64]:
a = paddle.nan_to_num(a.cast("float32"), nan=1.0)
ret = paddle.prod(a, axis=axis, keepdim=keepdims)
else:
a = paddle.nan_to_num(a, nan=1.0)
ret = paddle.prod(a, axis=axis, keepdim=keepdims)

if isinstance(axis, Sequence):
if len(axis) == a.ndim:
axis = None
if (a.ndim == 1 or axis is None) and not keepdims:
ret = ret.squeeze()
return ret.cast(dtype)


def _compute_quantile(
x, q, axis=None, keepdim=False, ignore_nan=False, interpolation="linear"
):
Expand Down
26 changes: 26 additions & 0 deletions ivy/functional/backends/tensorflow/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ def nanmean(
return tf.experimental.numpy.nanmean(a, axis=axis, keepdims=keepdims, dtype=dtype)


def _infer_dtype(dtype: tf.DType):
default_dtype = ivy.infer_default_dtype(dtype)
if ivy.dtype_bits(dtype) < ivy.dtype_bits(default_dtype):
return default_dtype
return dtype


def nanprod(
a: Union[tf.Tensor, tf.Variable],
/,
*,
axis: Optional[Union[int, Sequence[int]]] = None,
dtype: Optional[tf.DType] = None,
keepdims: Optional[bool] = False,
out: Optional[Union[tf.Tensor, tf.Variable]] = None,
initial: Optional[Union[int, float, complex]] = None,
where: Optional[Union[tf.Tensor, tf.Variable]] = None,
) -> Union[tf.Tensor, tf.Variable]:
np_math_ops.enable_numpy_methods_on_tensor()
dtype = ivy.as_native_dtype(dtype)
if dtype is None:
dtype = _infer_dtype(a.dtype)
axis = tuple(axis) if isinstance(axis, list) else axis
return tf.experimental.numpy.nanprod(a, axis=axis, keepdims=keepdims, dtype=dtype)


def quantile(
a: Union[tf.Tensor, tf.Variable],
q: Union[tf.Tensor, float],
Expand Down
29 changes: 29 additions & 0 deletions ivy/functional/backends/torch/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,35 @@ def nanmean(
nanmean.support_native_out = True


def nanprod(
a: torch.Tensor,
/,
*,
axis: Optional[Union[int, Sequence[int]]] = None,
dtype: Optional[torch.dtype] = None,
keepdims: Optional[bool] = False,
out: Optional[torch.Tensor] = None,
initial: Optional[Union[int, float, complex, ivy.Container]] = None,
where: Optional[torch.Tensor] = None,
) -> torch.Tensor:
a = torch.nan_to_num(a, nan=1.0)
dtype = ivy.as_native_dtype(dtype)
if dtype is None:
dtype = _infer_dtype(a.dtype)
if axis == ():
return a.type(dtype)
if axis is None:
return torch.prod(input=a, dtype=dtype, out=out)
if isinstance(axis, tuple) or isinstance(axis, list):
for i in axis:
a = torch.prod(a, dim=i, keepdim=keepdims, dtype=dtype, out=out)
return a
return torch.prod(a, dim=axis, keepdim=keepdims, dtype=dtype, out=out)


nanprod.support_native_out = True


@with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "float16")}, backend_version)
def quantile(
a: torch.Tensor,
Expand Down
Loading
Loading