Skip to content

Use ones() function from dpctl.tensor. #1256

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 5 commits into from
Dec 23, 2022
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
2 changes: 0 additions & 2 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ enum class DPNPFuncName : size_t
DPNP_FN_NONZERO, /**< Used in numpy.nonzero() impl */
DPNP_FN_NONZERO_EXT, /**< Used in numpy.nonzero() impl, requires extra parameters */
DPNP_FN_ONES, /**< Used in numpy.ones() impl */
DPNP_FN_ONES_EXT, /**< Used in numpy.ones() impl, requires extra parameters */
DPNP_FN_ONES_LIKE, /**< Used in numpy.ones_like() impl */
DPNP_FN_ONES_LIKE_EXT, /**< Used in numpy.ones_like() impl, requires extra parameters */
DPNP_FN_PARTITION, /**< Used in numpy.partition() impl */
DPNP_FN_PARTITION_EXT, /**< Used in numpy.partition() impl, requires extra parameters */
DPNP_FN_PLACE, /**< Used in numpy.place() impl */
Expand Down
28 changes: 0 additions & 28 deletions dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,6 @@ void dpnp_ones_c(void* result, size_t size)
template <typename _DataType>
void (*dpnp_ones_default_c)(void*, size_t) = dpnp_ones_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef (*dpnp_ones_ext_c)(DPCTLSyclQueueRef,
void*,
size_t,
const DPCTLEventVectorRef) = dpnp_ones_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef dpnp_ones_like_c(DPCTLSyclQueueRef q_ref,
void* result,
Expand All @@ -488,12 +482,6 @@ void dpnp_ones_like_c(void* result, size_t size)
template <typename _DataType>
void (*dpnp_ones_like_default_c)(void*, size_t) = dpnp_ones_like_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef (*dpnp_ones_like_ext_c)(DPCTLSyclQueueRef,
void*,
size_t,
const DPCTLEventVectorRef) = dpnp_ones_like_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef dpnp_ptp_c(DPCTLSyclQueueRef q_ref,
void* result1_out,
Expand Down Expand Up @@ -1378,14 +1366,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
fmap[DPNPFuncName::DPNP_FN_ONES][eft_C128][eft_C128] = {eft_C128,
(void*)dpnp_ones_default_c<std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ones_ext_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ones_ext_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ones_ext_c<float>};
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_ones_ext_c<double>};
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_ones_ext_c<bool>};
fmap[DPNPFuncName::DPNP_FN_ONES_EXT][eft_C128][eft_C128] = {eft_C128,
(void*)dpnp_ones_ext_c<std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ones_like_default_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ones_like_default_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ones_like_default_c<float>};
Expand All @@ -1394,14 +1374,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_C128][eft_C128] = {
eft_C128, (void*)dpnp_ones_like_default_c<std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ones_like_ext_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ones_like_ext_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ones_like_ext_c<float>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_ones_like_ext_c<double>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_ones_like_ext_c<bool>};
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE_EXT][eft_C128][eft_C128] = {
eft_C128, (void*)dpnp_ones_like_ext_c<std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_PTP][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ptp_default_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_PTP][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ptp_default_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_PTP][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ptp_default_c<float>};
Expand Down
2 changes: 0 additions & 2 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_NONZERO
DPNP_FN_NONZERO_EXT
DPNP_FN_ONES
DPNP_FN_ONES_EXT
DPNP_FN_ONES_LIKE
DPNP_FN_ONES_LIKE_EXT
DPNP_FN_PARTITION
DPNP_FN_PARTITION_EXT
DPNP_FN_PLACE
Expand Down
28 changes: 0 additions & 28 deletions dpnp/dpnp_algo/dpnp_algo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -281,34 +281,6 @@ cdef dpnp_DPNPFuncType_to_dtype(size_t type):
utils.checker_throw_type_error("dpnp_DPNPFuncType_to_dtype", type)


cdef utils.dpnp_descriptor call_fptr_1out(DPNPFuncName fptr_name,
shape_type_c result_shape,
result_dtype):

# Convert type to C enum DPNPFuncType
cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype)

# get the FPTR data structure
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(fptr_name, dtype_in, dtype_in)

# Create result array with type given by FPTR data
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef fptr_1out_t func = <fptr_1out_t > kernel_data.ptr
# Call FPTR function
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, result.get_data(), result.size, NULL)

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cdef utils.dpnp_descriptor call_fptr_1in_1out(DPNPFuncName fptr_name,
utils.dpnp_descriptor x1,
shape_type_c result_shape,
Expand Down
10 changes: 0 additions & 10 deletions dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ __all__ += [
"dpnp_linspace",
"dpnp_logspace",
"dpnp_meshgrid",
"dpnp_ones",
"dpnp_ones_like",
"dpnp_ptp",
"dpnp_trace",
"dpnp_tri",
Expand Down Expand Up @@ -307,14 +305,6 @@ cpdef list dpnp_meshgrid(xi, copy, sparse, indexing):
return result


cpdef utils.dpnp_descriptor dpnp_ones(result_shape, result_dtype):
return call_fptr_1out(DPNP_FN_ONES_EXT, utils._object_to_tuple(result_shape), result_dtype)


cpdef utils.dpnp_descriptor dpnp_ones_like(result_shape, result_dtype):
return call_fptr_1out(DPNP_FN_ONES_LIKE_EXT, utils._object_to_tuple(result_shape), result_dtype)


cpdef dpnp_ptp(utils.dpnp_descriptor arr, axis=None):
cdef shape_type_c shape_arr = arr.shape
cdef shape_type_c output_shape
Expand Down
23 changes: 23 additions & 0 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"asarray",
"empty",
"full",
"ones"
"zeros",
]

Expand Down Expand Up @@ -144,6 +145,28 @@ def full(shape,
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


def ones(shape,
*,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None):
"""Validate input parameters before passing them into `dpctl.tensor` module"""
dpu.validate_usm_type(usm_type, allow_none=False)
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
if order is None:
order = 'C'

"""Creates `dpnp_array` of ones with the given shape, dtype, and order."""
array_obj = dpt.ones(shape,
dtype=dtype,
order=order,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


def zeros(shape,
*,
dtype=None,
Expand Down
78 changes: 53 additions & 25 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,15 +1087,24 @@ def __getitem__(self, key):
ogrid = OGridClass()


def ones(shape, dtype=None, order='C'):
def ones(shape,
*,
dtype=None,
order="C",
like=None,
device=None,
usm_type="device",
sycl_queue=None):
"""
Return a new array of given shape and type, filled with ones.

For full documentation refer to :obj:`numpy.ones`.

Limitations
-----------
Parameter ``order`` is supported only with default value ``"C"``.
Parameter ``order`` is supported only with values ``"C"`` and ``"F"``.
Parameter ``like`` is supported only with default value ``None``.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
Expand All @@ -1117,28 +1126,42 @@ def ones(shape, dtype=None, order='C'):

"""

if (not use_origin_backend()):
if order not in ('C', 'c', None):
pass
else:
_dtype = dtype if dtype is not None else dpnp.float64

return dpnp_ones(shape, _dtype).get_pyobj()
if like is not None:
pass
elif order not in ('C', 'c', 'F', 'f', None):
pass
else:
return dpnp_container.ones(shape,
dtype=dtype,
order=order,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue)

return call_origin(numpy.ones, shape, dtype=dtype, order=order)
return call_origin(numpy.ones, shape, dtype=dtype, order=order, like=like)


# numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None)
def ones_like(x1, dtype=None, order='C', subok=False, shape=None):
def ones_like(x1,
/,
*,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None):
"""
Return an array of ones with the same shape and type as a given array.

For full documentation refer to :obj:`numpy.ones_like`.

Limitations
-----------
Parameter ``order`` is supported only with default value ``"C"``.
Parameters ``x1`` is supported only as :class:`dpnp.dpnp_array`.
Parameter ``order`` is supported with values ``"C"`` or ``"F"``.
Parameter ``subok`` is supported only with default value ``False``.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
Expand All @@ -1157,18 +1180,22 @@ def ones_like(x1, dtype=None, order='C', subok=False, shape=None):
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

"""

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
if order not in ('C', 'c', None):
pass
elif subok is not False:
pass
else:
_shape = shape if shape is not None else x1_desc.shape
_dtype = dtype if dtype is not None else x1_desc.dtype

return dpnp_ones_like(_shape, _dtype).get_pyobj()
if not isinstance(x1, dpnp.ndarray):
pass
elif order not in ('C', 'c', 'F', 'f', None):
pass
elif subok is not False:
pass
else:
_shape = x1.shape if shape is None else shape
_dtype = x1.dtype if dtype is None else dtype
_usm_type = x1.usm_type if usm_type is None else usm_type
_sycl_queue = dpnp.get_normalized_queue_device(x1, sycl_queue=sycl_queue, device=device)
return dpnp_container.ones(_shape,
dtype=_dtype,
order=order,
usm_type=_usm_type,
sycl_queue=_sycl_queue)

return call_origin(numpy.ones_like, x1, dtype, order, subok, shape)

Expand Down Expand Up @@ -1430,6 +1457,7 @@ def zeros(shape,


def zeros_like(x1,
/,
*,
dtype=None,
order="C",
Expand Down
44 changes: 44 additions & 0 deletions tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,47 @@ def test_zeros_like(array, dtype, order):
result = dpnp.zeros_like(ia, dtype=dtype, order=order)

assert_array_equal(expected, result)


@pytest.mark.parametrize("shape",
[(), 0, (0,), (2, 0, 3), (3, 2)],
ids=['()', '0', '(0,)', '(2, 0, 3)', '(3, 2)'])
@pytest.mark.parametrize("dtype",
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
'float16', 'int64', 'int32', 'bool'])
@pytest.mark.parametrize("order",
[None, "C", "F"],
ids=['None', 'C', 'F'])
def test_ones(shape, dtype, order):
func = lambda xp: xp.ones(shape, dtype=dtype, order=order)

if shape != 0 and not 0 in shape and not is_dtype_supported(dtype, no_complex_check=True):
assert_raises(RuntimeError, func, dpnp)
return

assert_array_equal(func(numpy), func(dpnp))


@pytest.mark.parametrize("array",
[[], 0, [1, 2, 3], [[1, 2], [3, 4]]],
ids=['[]', '0', '[1, 2, 3]', '[[1, 2], [3, 4]]'])
@pytest.mark.parametrize("dtype",
[None, numpy.complex128, numpy.complex64, numpy.float64, numpy.float32,
numpy.float16, numpy.int64, numpy.int32, numpy.bool],
ids=['None', 'complex128', 'complex64', 'float64', 'float32',
'float16', 'int64', 'int32', 'bool'])
@pytest.mark.parametrize("order",
[None, "C", "F"],
ids=['None', 'C', 'F'])
def test_ones_like(array, dtype, order):
a = numpy.array(array)
ia = dpnp.array(array)
func = lambda xp, x: xp.ones_like(x, dtype=dtype, order=order)

if ia.size and not is_dtype_supported(dtype, no_complex_check=True):
assert_raises(RuntimeError, func, dpnp, ia)
return

assert_array_equal(func(numpy, a), func(dpnp, ia))
5 changes: 5 additions & 0 deletions tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def vvsort(val, vec, size, xp):
pytest.param("full",
(2,2),
{'fill_value': 5}),
pytest.param("ones",
(2,2),
{}),
pytest.param("zeros",
(2,2),
{})
Expand All @@ -104,6 +107,8 @@ def test_array_creation(func, arg, kwargs, device):
[
pytest.param("full_like",
{'fill_value': 5}),
pytest.param("ones_like",
{}),
pytest.param("zeros_like",
{})
])
Expand Down
Loading