Skip to content

Implements statistical functions mean, std, var #1465

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

Merged
merged 6 commits into from
Nov 8, 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
4 changes: 4 additions & 0 deletions dpctl/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
)
from dpctl.tensor._reshape import reshape
from dpctl.tensor._search_functions import where
from dpctl.tensor._statistical_functions import mean, std, var
from dpctl.tensor._usmarray import usm_ndarray
from dpctl.tensor._utility_functions import all, any

Expand Down Expand Up @@ -336,6 +337,9 @@
"clip",
"logsumexp",
"reduce_hypot",
"mean",
"std",
"var",
"__array_api_version__",
"__array_namespace_info__",
]
48 changes: 24 additions & 24 deletions dpctl/tensor/_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def _reduction_over_axis(
_reduction_fn,
_dtype_supported,
_default_reduction_type_fn,
_identity=None,
):
if not isinstance(x, dpt.usm_ndarray):
raise TypeError(f"Expected dpctl.tensor.usm_ndarray, got {type(x)}")
Expand All @@ -106,23 +105,8 @@ def _reduction_over_axis(
res_dt = _to_device_supported_dtype(res_dt, q.sycl_device)

res_usm_type = x.usm_type
if x.size == 0:
if _identity is None:
raise ValueError("reduction does not support zero-size arrays")
else:
if keepdims:
res_shape = res_shape + (1,) * red_nd
inv_perm = sorted(range(nd), key=lambda d: perm[d])
res_shape = tuple(res_shape[i] for i in inv_perm)
return dpt.full(
res_shape,
_identity,
dtype=res_dt,
usm_type=res_usm_type,
sycl_queue=q,
)
if red_nd == 0:
return dpt.astype(x, res_dt, copy=False)
return dpt.astype(x, res_dt, copy=True)

host_tasks_list = []
if _dtype_supported(inp_dt, res_dt, res_usm_type, q):
Expand Down Expand Up @@ -251,7 +235,6 @@ def sum(x, axis=None, dtype=None, keepdims=False):
tri._sum_over_axis,
tri._sum_over_axis_dtype_supported,
_default_reduction_dtype,
_identity=0,
)


Expand Down Expand Up @@ -312,7 +295,6 @@ def prod(x, axis=None, dtype=None, keepdims=False):
tri._prod_over_axis,
tri._prod_over_axis_dtype_supported,
_default_reduction_dtype,
_identity=1,
)


Expand Down Expand Up @@ -368,7 +350,6 @@ def logsumexp(x, axis=None, dtype=None, keepdims=False):
inp_dt, res_dt
),
_default_reduction_dtype_fp_types,
_identity=-dpt.inf,
)


Expand Down Expand Up @@ -424,7 +405,6 @@ def reduce_hypot(x, axis=None, dtype=None, keepdims=False):
inp_dt, res_dt
),
_default_reduction_dtype_fp_types,
_identity=0,
)


Expand All @@ -446,9 +426,19 @@ def _comparison_over_axis(x, axis, keepdims, _reduction_fn):
res_dt = x.dtype
res_usm_type = x.usm_type
if x.size == 0:
raise ValueError("reduction does not support zero-size arrays")
if any([x.shape[i] == 0 for i in axis]):
raise ValueError(
"reduction cannot be performed over zero-size axes"
)
else:
return dpt.empty(
res_shape,
dtype=res_dt,
usm_type=res_usm_type,
sycl_queue=exec_q,
)
if red_nd == 0:
return x
return dpt.copy(x)

res = dpt.empty(
res_shape,
Expand Down Expand Up @@ -549,7 +539,17 @@ def _search_over_axis(x, axis, keepdims, _reduction_fn):
res_dt = ti.default_device_index_type(exec_q.sycl_device)
res_usm_type = x.usm_type
if x.size == 0:
raise ValueError("reduction does not support zero-size arrays")
if any([x.shape[i] == 0 for i in axis]):
raise ValueError(
"reduction cannot be performed over zero-size axes"
)
else:
return dpt.empty(
res_shape,
dtype=res_dt,
usm_type=res_usm_type,
sycl_queue=exec_q,
)
if red_nd == 0:
return dpt.zeros(
res_shape, dtype=res_dt, usm_type=res_usm_type, sycl_queue=exec_q
Expand Down
Loading