Skip to content

Leverage dpctl.tensor.copy() implementation #1540

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 4 commits into from
Aug 28, 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
1 change: 1 addition & 0 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
test_arraycreation.py
test_dot.py
test_dparray.py
test_copy.py
test_fft.py
test_linalg.py
test_logic.py
Expand Down
2 changes: 0 additions & 2 deletions dpnp/backend/kernels/dpnp_krnl_elemwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,8 +1326,6 @@ static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
{ \
constexpr size_t lws = 64; \
constexpr unsigned int vec_sz = 8; \
constexpr sycl::access::address_space global_space = \
sycl::access::address_space::global_space; \
\
auto gws_range = sycl::range<1>( \
((result_size + lws * vec_sz - 1) / (lws * vec_sz)) * \
Expand Down
2 changes: 0 additions & 2 deletions dpnp/backend/kernels/dpnp_krnl_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,6 @@ DPCTLSyclEventRef (*dpnp_any_ext_c)(DPCTLSyclQueueRef,
else { \
constexpr size_t lws = 64; \
constexpr unsigned int vec_sz = 8; \
constexpr sycl::access::address_space global_space = \
sycl::access::address_space::global_space; \
\
auto gws_range = sycl::range<1>( \
((result_size + lws * vec_sz - 1) / (lws * vec_sz)) * lws); \
Expand Down
4 changes: 2 additions & 2 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ cpdef tuple dpnp_modf(utils.dpnp_descriptor x1):


cpdef utils.dpnp_descriptor dpnp_nancumprod(utils.dpnp_descriptor x1):
cur_x1 = dpnp_copy(x1).get_pyobj()
cur_x1 = x1.get_pyobj().copy()

cur_x1_flatiter = cur_x1.flat

Expand All @@ -365,7 +365,7 @@ cpdef utils.dpnp_descriptor dpnp_nancumprod(utils.dpnp_descriptor x1):


cpdef utils.dpnp_descriptor dpnp_nancumsum(utils.dpnp_descriptor x1):
cur_x1 = dpnp_copy(x1).get_pyobj()
cur_x1 = x1.get_pyobj().copy()

cur_x1_flatiter = cur_x1.flat

Expand Down
52 changes: 50 additions & 2 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ def __complex__(self):
return self._array_obj.__complex__()

# '__contains__',
# '__copy__',

def __copy__(self):
"""
Used if :func:`copy.copy` is called on an array. Returns a copy of the array.

Equivalent to ``a.copy(order="K")``.
"""
return self.copy(order="K")

# '__deepcopy__',
# '__delattr__',
# '__delitem__',
Expand Down Expand Up @@ -651,7 +659,47 @@ def conjugate(self):
else:
return dpnp.conjugate(self)

# 'copy',
def copy(self, order="C"):
"""
Return a copy of the array.

Returns
-------
out : dpnp.ndarray
A copy of the array.

See also
--------
:obj:`dpnp.copy` : Similar function with different default behavior
:obj:`dpnp.copyto` : Copies values from one array to another.

Notes
-----
This function is the preferred method for creating an array copy. The
function :func:`dpnp.copy` is similar, but it defaults to using order 'K'.

Examples
--------
>>> import dpnp as np
>>> x = np.array([[1, 2, 3], [4, 5, 6]], order='F')
>>> y = x.copy()
>>> x.fill(0)

>>> x
array([[0, 0, 0],
[0, 0, 0]])

>>> y
array([[1, 2, 3],
[4, 5, 6]])

>>> y.flags['C_CONTIGUOUS']
True

"""

return dpnp.copy(self, order=order)

# 'ctypes',
# 'cumprod',

Expand Down
22 changes: 13 additions & 9 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
__all__ = [
"arange",
"asarray",
"copy",
"empty",
"eye",
"full",
Expand Down Expand Up @@ -135,6 +136,15 @@ def asarray(
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


def copy(x1, /, *, order="K"):
"""Creates `dpnp_array` as a copy of given instance of input array."""
if order is None:
order = "K"

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


def empty(
shape,
*,
Expand Down Expand Up @@ -264,9 +274,7 @@ def meshgrid(*xi, indexing="xy"):
"""Creates list of `dpnp_array` coordinate matrices from vectors."""
if len(xi) == 0:
return []
arrays = tuple(
x.get_array() if isinstance(x, dpnp_array) else x for x in xi
)
arrays = tuple(dpnp.get_usm_ndarray(x) for x in xi)
arrays_obj = dpt.meshgrid(*arrays, indexing=indexing)
return [
dpnp_array._create_from_usm_ndarray(array_obj)
Expand Down Expand Up @@ -304,17 +312,13 @@ def ones(

def tril(x1, /, *, k=0):
"""Creates `dpnp_array` as lower triangular part of an input array."""
array_obj = dpt.tril(
x1.get_array() if isinstance(x1, dpnp_array) else x1, k
)
array_obj = dpt.tril(dpnp.get_usm_ndarray(x1), k)
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(
x1.get_array() if isinstance(x1, dpnp_array) else x1, k
)
array_obj = dpt.triu(dpnp.get_usm_ndarray(x1), k)
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")


Expand Down
Loading