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

Remove data synchronize where possible #1930

Merged
merged 5 commits into from
Jul 17, 2024
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
3 changes: 0 additions & 3 deletions dpnp/dpnp_algo/dpnp_elementwise_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def __call__(
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
res_usm = super().__call__(x_usm, out=out_usm, order=order)

dpnp.synchronize_array_data(res_usm)
if out is not None and isinstance(out, dpnp_array):
return out
return dpnp_array._create_from_usm_ndarray(res_usm)
Expand Down Expand Up @@ -352,7 +351,6 @@ def __call__(
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
res_usm = super().__call__(x1_usm, x2_usm, out=out_usm, order=order)

dpnp.synchronize_array_data(res_usm)
if out is not None and isinstance(out, dpnp_array):
return out
return dpnp_array._create_from_usm_ndarray(res_usm)
Expand Down Expand Up @@ -540,7 +538,6 @@ def __call__(self, x, decimals=0, out=None, dtype=None):
if dtype is not None:
res_usm = dpt.astype(res_usm, dtype, copy=False)

dpnp.synchronize_array_data(res_usm)
if out is not None and isinstance(out, dpnp_array):
return out
return dpnp_array._create_from_usm_ndarray(res_usm)
Expand Down
4 changes: 0 additions & 4 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,6 @@ def __getitem__(self, key):

res = self.__new__(dpnp_array)
res._array_obj = item

if self._array_obj.usm_data is not res._array_obj.usm_data:
dpnp.synchronize_array_data(self)
return res

def __gt__(self, other):
Expand Down Expand Up @@ -456,7 +453,6 @@ def __setitem__(self, key, val):
val = val.get_array()

self._array_obj.__setitem__(key, val)
dpnp.synchronize_array_data(self)

# '__setstate__',
# '__sizeof__',
Expand Down
10 changes: 0 additions & 10 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def arange(
sycl_queue=sycl_queue_normalized,
)

dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj)


Expand Down Expand Up @@ -133,7 +132,6 @@ def asarray(
if array_obj is x1_obj and isinstance(x1, dpnp_array):
return x1

dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


Expand All @@ -143,7 +141,6 @@ def copy(x1, /, *, order="K"):
order = "K"

array_obj = dpt.copy(dpnp.get_usm_ndarray(x1), order=order)
dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")


Expand Down Expand Up @@ -205,7 +202,6 @@ def eye(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


Expand Down Expand Up @@ -240,7 +236,6 @@ def full(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


Expand Down Expand Up @@ -269,21 +264,18 @@ def ones(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


def tril(x1, /, *, k=0):
"""Creates `dpnp_array` as lower triangular part of an input array."""
array_obj = dpt.tril(dpnp.get_usm_ndarray(x1), k=k)
dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")


def triu(x1, /, *, k=0):
"""Creates `dpnp_array` as upper triangular part of an input array."""
array_obj = dpt.triu(dpnp.get_usm_ndarray(x1), k=k)
dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")


Expand Down Expand Up @@ -312,6 +304,4 @@ def zeros(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
# TODO: uncomment once dpctl implements asynchronous call
# dpnp.synchronize_array_data(array_obj)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
6 changes: 2 additions & 4 deletions dpnp/dpnp_iface.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ def astype(x1, dtype, order="K", casting="unsafe", copy=True, device=None):
x1_obj, dtype, order=order, casting=casting, copy=copy, device=device
)

dpnp.synchronize_array_data(x1)
if array_obj is x1_obj and isinstance(x1, dpnp_array):
# return x1 if dpctl returns a zero copy of x1_obj
return x1
Expand Down Expand Up @@ -797,6 +796,5 @@ def synchronize_array_data(a):

"""

if hasattr(dpu, "SequentialOrderManager"):
check_supported_arrays_type(a)
dpu.SequentialOrderManager[a.sycl_queue].wait()
check_supported_arrays_type(a)
dpu.SequentialOrderManager[a.sycl_queue].wait()
22 changes: 4 additions & 18 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@ def geomspace(

"""

res = dpnp_geomspace(
return dpnp_geomspace(
start,
stop,
num,
Expand All @@ -2200,9 +2200,6 @@ def geomspace(
axis=axis,
)

dpnp.synchronize_array_data(res)
return res


def identity(
n,
Expand Down Expand Up @@ -2410,7 +2407,7 @@ def linspace(

"""

res = dpnp_linspace(
return dpnp_linspace(
start,
stop,
num,
Expand All @@ -2423,12 +2420,6 @@ def linspace(
axis=axis,
)

if isinstance(res, tuple): # (result, step) is returning
dpnp.synchronize_array_data(res[0])
else:
dpnp.synchronize_array_data(res)
return res


def loadtxt(
fname,
Expand Down Expand Up @@ -2643,7 +2634,7 @@ def logspace(

"""

res = dpnp_logspace(
return dpnp_logspace(
start,
stop,
num=num,
Expand All @@ -2656,9 +2647,6 @@ def logspace(
axis=axis,
)

dpnp.synchronize_array_data(res)
return res


# pylint: disable=redefined-outer-name
def meshgrid(*xi, copy=True, sparse=False, indexing="xy"):
Expand Down Expand Up @@ -2759,9 +2747,7 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"):
if copy:
output = [dpt.copy(x) for x in output]

dpnp.synchronize_array_data(output[0])
output = [dpnp_array._create_from_usm_ndarray(x) for x in output]
return output
return [dpnp_array._create_from_usm_ndarray(x) for x in output]


class MGridClass:
Expand Down
77 changes: 28 additions & 49 deletions dpnp/dpnp_iface_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@
import operator
import warnings

import dpctl.tensor as dpt
import dpctl.utils as dpu
import numpy

import dpnp

from .dpnp_algo.dpnp_arraycreation import (
dpnp_linspace,
)
from .dpnp_array import dpnp_array

__all__ = [
"digitize",
"histogram",
Expand All @@ -63,10 +57,14 @@


def _ravel_check_a_and_weights(a, weights):
"""Check input `a` and `weights` arrays, and ravel both."""
"""
Check input `a` and `weights` arrays, and ravel both.
The returned array have :class:`dpnp.ndarray` type always.

"""

# ensure that `a` array has supported type
a = dpnp.get_usm_ndarray(a)
dpnp.check_supported_arrays_type(a)
usm_type = a.usm_type

# ensure that the array is a "subtractable" dtype
Expand All @@ -77,11 +75,11 @@ def _ravel_check_a_and_weights(a, weights):
RuntimeWarning,
stacklevel=3,
)
a = dpt.astype(a, numpy.uint8)
a = dpnp.astype(a, numpy.uint8)

if weights is not None:
# check that `weights` array has supported type
weights = dpnp.get_usm_ndarray(weights)
dpnp.check_supported_arrays_type(weights)
usm_type = dpu.get_coerced_usm_type([usm_type, weights.usm_type])

# check that arrays have the same allocation queue
Expand All @@ -92,9 +90,9 @@ def _ravel_check_a_and_weights(a, weights):

if weights.shape != a.shape:
raise ValueError("weights should have the same shape as a.")
weights = dpt.reshape(weights, -1)
weights = dpnp.ravel(weights)

a = dpt.reshape(a, -1)
a = dpnp.ravel(a)
return a, weights, usm_type


Expand All @@ -120,7 +118,7 @@ def _get_outer_edges(a, range):
first_edge, last_edge = 0, 1

else:
first_edge, last_edge = dpt.min(a), dpt.max(a)
first_edge, last_edge = a.min(), a.max()
if not (dpnp.isfinite(first_edge) and dpnp.isfinite(last_edge)):
raise ValueError(
f"autodetected range of [{first_edge}, {last_edge}] "
Expand Down Expand Up @@ -164,9 +162,11 @@ def _get_bin_edges(a, bins, range, usm_type):
"a and bins must be allocated on the same SYCL queue"
)

bin_edges = dpnp.as_usm_ndarray(
bins, usm_type=usm_type, sycl_queue=sycl_queue
)
bin_edges = bins
else:
bin_edges = dpnp.asarray(
bins, sycl_queue=sycl_queue, usm_type=usm_type
)

if dpnp.any(bin_edges[:-1] > bin_edges[1:]):
raise ValueError(
Expand All @@ -188,15 +188,15 @@ def _get_bin_edges(a, bins, range, usm_type):
)

# bin edges must be computed
bin_edges = dpnp_linspace(
bin_edges = dpnp.linspace(
first_edge,
last_edge,
n_equal_bins + 1,
endpoint=True,
dtype=bin_type,
sycl_queue=sycl_queue,
usm_type=usm_type,
).get_array()
)
return bin_edges, (first_edge, last_edge, n_equal_bins)
return bin_edges, None

Expand All @@ -209,11 +209,8 @@ def _search_sorted_inclusive(a, v):

"""

return dpt.concat(
(
dpt.searchsorted(a, v[:-1], side="left"),
dpt.searchsorted(a, v[-1:], side="right"),
)
return dpnp.concatenate(
(a.searchsorted(v[:-1], "left"), a.searchsorted(v[-1:], "right"))
)


Expand Down Expand Up @@ -305,14 +302,8 @@ def digitize(x, bins, right=False):
# Use dpnp.searchsorted directly if bins are increasing
return dpnp.searchsorted(bins, x, side=side)

usm_x = dpnp.get_usm_ndarray(x)
usm_bins = dpnp.get_usm_ndarray(bins)

# Reverse bins and adjust indices if bins are decreasing
usm_res = usm_bins.size - dpt.searchsorted(usm_bins[::-1], usm_x, side=side)

dpnp.synchronize_array_data(usm_res)
return dpnp_array._create_from_usm_ndarray(usm_res)
return bins.size - dpnp.searchsorted(bins[::-1], x, side=side)


def histogram(a, bins=10, range=None, density=None, weights=None):
Expand Down Expand Up @@ -426,36 +417,26 @@ def histogram(a, bins=10, range=None, density=None, weights=None):
else:
# Compute via cumulative histogram
if weights is None:
sa = dpt.sort(a)
sa = dpnp.sort(a)
cum_n = _search_sorted_inclusive(sa, bin_edges)
else:
zero = dpt.zeros(
zero = dpnp.zeros(
1, dtype=ntype, sycl_queue=a.sycl_queue, usm_type=usm_type
)
sorting_index = dpt.argsort(a)
sorting_index = dpnp.argsort(a)
sa = a[sorting_index]
sw = weights[sorting_index]
cw = dpt.concat((zero, dpt.cumulative_sum(sw, dtype=ntype)))
cw = dpnp.concatenate((zero, sw.cumsum(dtype=ntype)))
bin_index = _search_sorted_inclusive(sa, bin_edges)
cum_n = cw[bin_index]

n = dpnp.diff(cum_n)

# convert bin_edges to dpnp.ndarray
bin_edges = dpnp_array._create_from_usm_ndarray(bin_edges)

if density:
# pylint: disable=possibly-used-before-assignment
db = dpnp.diff(bin_edges)
db = dpt.astype(db.get_array(), dpnp.default_float_type())
db = dpnp.diff(bin_edges).astype(dpnp.default_float_type())
return n / db / n.sum(), bin_edges

usm_n = n.get_array()
hist = usm_n / db / dpt.sum(usm_n)

dpnp.synchronize_array_data(hist)
return dpnp_array._create_from_usm_ndarray(hist), bin_edges

dpnp.synchronize_array_data(n)
return n, bin_edges


Expand Down Expand Up @@ -541,6 +522,4 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None):

a, weights, usm_type = _ravel_check_a_and_weights(a, weights)
bin_edges, _ = _get_bin_edges(a, bins, range, usm_type)

dpnp.synchronize_array_data(bin_edges)
return dpnp_array._create_from_usm_ndarray(bin_edges)
return bin_edges
Loading
Loading