Skip to content

Use full() function from dpctl.tensor #1218

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 13 commits into from
Dec 9, 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 @@ -178,9 +178,7 @@ enum class DPNPFuncName : size_t
DPNP_FN_FMOD, /**< Used in numpy.fmod() impl */
DPNP_FN_FMOD_EXT, /**< Used in numpy.fmod() impl, requires extra parameters */
DPNP_FN_FULL, /**< Used in numpy.full() impl */
DPNP_FN_FULL_EXT, /**< Used in numpy.full() impl, requires extra parameters */
DPNP_FN_FULL_LIKE, /**< Used in numpy.full_like() impl */
DPNP_FN_FULL_LIKE_EXT, /**< Used in numpy.full_like() impl, requires extra parameters */
DPNP_FN_HYPOT, /**< Used in numpy.hypot() impl */
DPNP_FN_HYPOT_EXT, /**< Used in numpy.hypot() impl, requires extra parameters */
DPNP_FN_IDENTITY, /**< Used in numpy.identity() impl */
Expand Down
32 changes: 2 additions & 30 deletions dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,12 @@ void dpnp_full_c(void* array_in, void* result, const size_t size)
size,
dep_event_vec_ref);
DPCTLEvent_WaitAndThrow(event_ref);
DPCTLEvent_Delete(event_ref);
}

template <typename _DataType>
void (*dpnp_full_default_c)(void*, void*, const size_t) = dpnp_full_c<_DataType>;

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

template <typename _DataType>
DPCTLSyclEventRef dpnp_full_like_c(DPCTLSyclQueueRef q_ref,
void* array_in,
Expand All @@ -346,18 +340,12 @@ void dpnp_full_like_c(void* array_in, void* result, const size_t size)
size,
dep_event_vec_ref);
DPCTLEvent_WaitAndThrow(event_ref);
DPCTLEvent_Delete(event_ref);
}

template <typename _DataType>
void (*dpnp_full_like_default_c)(void*, void*, const size_t) = dpnp_full_like_c<_DataType>;

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

template <typename _KernelNameSpecialization>
class dpnp_identity_c_kernel;

Expand Down Expand Up @@ -1338,14 +1326,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
fmap[DPNPFuncName::DPNP_FN_FULL][eft_C128][eft_C128] = {eft_C128,
(void*)dpnp_full_default_c<std::complex<double>>};

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

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

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

fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_identity_default_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_identity_default_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_IDENTITY][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_identity_default_c<float>};
Expand Down
3 changes: 0 additions & 3 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_FMOD
DPNP_FN_FMOD_EXT
DPNP_FN_FULL
DPNP_FN_FULL_EXT
DPNP_FN_FULL_LIKE
DPNP_FN_FULL_LIKE_EXT
DPNP_FN_HYPOT
DPNP_FN_HYPOT_EXT
DPNP_FN_IDENTITY
Expand Down Expand Up @@ -518,7 +516,6 @@ cpdef dpnp_descriptor dpnp_matmul(dpnp_descriptor in_array1, dpnp_descriptor in_
Array creation routines
"""
cpdef dpnp_descriptor dpnp_init_val(shape, dtype, value)
cpdef dpnp_descriptor dpnp_full(result_shape, value_in, result_dtype) # same as dpnp_init_val
cpdef dpnp_descriptor dpnp_copy(dpnp_descriptor x1)

"""
Expand Down
77 changes: 1 addition & 76 deletions dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# cython: language_level=3
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2016-2020, Intel Corporation
# Copyright (c) 2016-2022, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,8 +38,6 @@ __all__ += [
"dpnp_copy",
"dpnp_diag",
"dpnp_eye",
"dpnp_full",
"dpnp_full_like",
"dpnp_geomspace",
"dpnp_identity",
"dpnp_linspace",
Expand Down Expand Up @@ -183,79 +181,6 @@ cpdef utils.dpnp_descriptor dpnp_eye(N, M=None, k=0, dtype=None):
return result


cpdef utils.dpnp_descriptor dpnp_full(result_shape, value_in, result_dtype):
# Convert string type names (array.dtype) 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(DPNP_FN_FULL_EXT, dtype_in, DPNP_FT_NONE)

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

result_obj = result.get_array()

# Create single-element input fill array with type given by FPTR data
cdef shape_type_c shape_in = (1,)
cdef utils.dpnp_descriptor array_fill = utils.create_output_descriptor(shape_in,
kernel_data.return_type,
None,
device=result_obj.sycl_device,
usm_type=result_obj.usm_type,
sycl_queue=result_obj.sycl_queue)
array_fill.get_pyobj()[0] = value_in

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

cdef fptr_1in_1out_t func = <fptr_1in_1out_t > kernel_data.ptr
# Call FPTR function

cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, array_fill.get_data(), result.get_data(), result.size, NULL)

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

return result


cpdef utils.dpnp_descriptor dpnp_full_like(result_shape, value_in, result_dtype):
# Convert string type names (array.dtype) 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(DPNP_FN_FULL_LIKE_EXT, dtype_in, DPNP_FT_NONE)

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

result_obj = result.get_array()

# Create single-element input fill array with type given by FPTR data
cdef shape_type_c shape_in = (1,)
cdef utils.dpnp_descriptor array_fill = utils.create_output_descriptor(shape_in,
kernel_data.return_type,
None,
device=result_obj.sycl_device,
usm_type=result_obj.usm_type,
sycl_queue=result_obj.sycl_queue)
array_fill.get_pyobj()[0] = value_in

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

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

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

return result


cpdef utils.dpnp_descriptor dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
cdef shape_type_c obj_shape = utils._object_to_tuple(num)
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(obj_shape, dtype, None)
Expand Down
26 changes: 26 additions & 0 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"arange",
"asarray",
"empty",
"full",
]


Expand Down Expand Up @@ -111,3 +112,28 @@ def empty(shape,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


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

if isinstance(fill_value, dpnp_array):
fill_value = fill_value.get_array()

"""Creates `dpnp_array` having a specified shape, filled with fill_value."""
array_obj = dpt.full(shape,
fill_value,
dtype=dtype,
order=order,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
89 changes: 60 additions & 29 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,25 @@ def fromstring(string, **kwargs):
return call_origin(numpy.fromstring, string, **kwargs)


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

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

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 @@ -685,29 +695,44 @@ def full(shape, fill_value, dtype=None, order='C'):
[10, 10, 10, 10]

"""
if not use_origin_backend():
if order not in ('C', 'c', None):
pass
else:
if dtype is None:
dtype = numpy.array(fill_value).dtype.type # TODO simplify

return dpnp_full(shape, fill_value, dtype).get_pyobj()

return call_origin(numpy.full, shape, fill_value, dtype, order)


# numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)
def full_like(x1, fill_value, dtype=None, order='C', subok=False, shape=None):
if like is not None:
pass
elif not isinstance(order, str) or len(order) != 1 or order not in "CcFf":
pass
else:
return dpnp_container.full(shape,
fill_value,
dtype=dtype,
order=order,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue)

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


def full_like(x1,
/,
fill_value,
*,
dtype=None,
order='C',
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None):
"""
Return a full array with the same shape and type as a given array.

For full documentation refer to :obj:`numpy.full_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 only with values ``"C"`` and ``"F"``.
Parameter ``subok`` is supported only with default value ``False``.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
Expand All @@ -725,18 +750,24 @@ def full_like(x1, fill_value, dtype=None, order='C', subok=False, shape=None):
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

"""

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

return dpnp_full_like(_shape, fill_value, _dtype).get_pyobj()

if not isinstance(x1, dpnp.ndarray):
pass
elif not isinstance(order, str) or len(order) != 1 or order not in "CcFf":
pass
elif subok is not False:
pass
else:
_shape = shape if shape is not None else x1.shape
_dtype = dtype if dtype is not None else x1.dtype
_usm_type = usm_type if usm_type is not None else x1.usm_type
_sycl_queue = dpnp.get_normalized_queue_device(x1, sycl_queue=sycl_queue, device=device)

return dpnp_container.full(_shape,
fill_value,
dtype=_dtype,
order=order,
usm_type=_usm_type,
sycl_queue=_sycl_queue)
return numpy.full_like(x1, fill_value, dtype, order, subok, shape)


Expand Down
Loading