Skip to content

Implement call of arange() function from dpctl.tensor #1202

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 2, 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
8 changes: 3 additions & 5 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ jobs:
strategy:
matrix:
python: ['3.8', '3.9']
dpctl: ['0.13.0']
experimental: [false]

continue-on-error: ${{ matrix.experimental }}
Expand Down Expand Up @@ -239,7 +238,7 @@ jobs:
${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-

- name: Install dpnp
run: conda install ${{ env.PACKAGE_NAME }}=${{ env.PACKAGE_VERSION }} dpctl=${{ matrix.dpctl }} pytest python=${{ matrix.python }} ${{ env.TEST_CHANNELS }}
run: conda install ${{ env.PACKAGE_NAME }}=${{ env.PACKAGE_VERSION }} pytest python=${{ matrix.python }} ${{ env.TEST_CHANNELS }}
env:
TEST_CHANNELS: '-c ${{ env.channel-path }} ${{ env.CHANNELS }}'

Expand Down Expand Up @@ -270,7 +269,6 @@ jobs:
strategy:
matrix:
python: ['3.8', '3.9']
dpctl: ['0.13.0']
experimental: [false]

continue-on-error: ${{ matrix.experimental }}
Expand Down Expand Up @@ -344,7 +342,7 @@ jobs:
echo PACKAGE_VERSION: %PACKAGE_VERSION%
(echo PACKAGE_VERSION=%PACKAGE_VERSION%) >> %GITHUB_ENV%

conda install ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% dpctl=${{ matrix.dpctl }} python=${{ matrix.python }} ${{ env.TEST_CHANNELS }} --only-deps --dry-run > lockfile
conda install ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% python=${{ matrix.python }} ${{ env.TEST_CHANNELS }} --only-deps --dry-run > lockfile
env:
TEST_CHANNELS: '-c ${{ env.channel-path }} ${{ env.CHANNELS }}'

Expand All @@ -369,7 +367,7 @@ jobs:
- name: Install dpnp
run: |
@echo on
conda install ${{ env.PACKAGE_NAME }}=${{ env.PACKAGE_VERSION }} dpctl=${{ matrix.dpctl }} pytest python=${{ matrix.python }} ${{ env.TEST_CHANNELS }}
conda install ${{ env.PACKAGE_NAME }}=${{ env.PACKAGE_VERSION }} pytest python=${{ matrix.python }} ${{ env.TEST_CHANNELS }}
env:
TEST_CHANNELS: '-c ${{ env.channel-path }} ${{ env.CHANNELS }}'

Expand Down
3 changes: 1 addition & 2 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//*****************************************************************************
// 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 @@ -69,7 +69,6 @@ enum class DPNPFuncName : size_t
DPNP_FN_ANY, /**< Used in numpy.any() impl */
DPNP_FN_ANY_EXT, /**< Used in numpy.any() impl, requires extra parameters */
DPNP_FN_ARANGE, /**< Used in numpy.arange() impl */
DPNP_FN_ARANGE_EXT, /**< Used in numpy.arange() impl, requires extra parameters */
DPNP_FN_ARCCOS, /**< Used in numpy.arccos() impl */
DPNP_FN_ARCCOS_EXT, /**< Used in numpy.arccos() impl, requires extra parameters */
DPNP_FN_ARCCOSH, /**< Used in numpy.arccosh() impl */
Expand Down
44 changes: 31 additions & 13 deletions dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//*****************************************************************************
// 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 @@ -90,18 +90,41 @@ void dpnp_arange_c(size_t start, size_t step, void* result1, size_t size)
size,
dep_event_vec_ref);
DPCTLEvent_WaitAndThrow(event_ref);
DPCTLEvent_Delete(event_ref);
}

template <typename _DataType>
void (*dpnp_arange_default_c)(size_t, size_t, void*, size_t) = dpnp_arange_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef (*dpnp_arange_ext_c)(DPCTLSyclQueueRef,
size_t,
size_t,
void*,
size_t,
const DPCTLEventVectorRef) = dpnp_arange_c<_DataType>;
// Explicit instantiation of the function, since dpnp_arange_c() is used by other template functions,
// but implicit instantiation is not applied anymore.
template DPCTLSyclEventRef dpnp_arange_c<int32_t>(DPCTLSyclQueueRef,
size_t,
size_t,
void*,
size_t,
const DPCTLEventVectorRef);

template DPCTLSyclEventRef dpnp_arange_c<int64_t>(DPCTLSyclQueueRef,
size_t,
size_t,
void*,
size_t,
const DPCTLEventVectorRef);

template DPCTLSyclEventRef dpnp_arange_c<float>(DPCTLSyclQueueRef,
size_t,
size_t,
void*,
size_t,
const DPCTLEventVectorRef);

template DPCTLSyclEventRef dpnp_arange_c<double>(DPCTLSyclQueueRef,
size_t,
size_t,
void*,
size_t,
const DPCTLEventVectorRef);

template <typename _DataType>
DPCTLSyclEventRef dpnp_diag_c(DPCTLSyclQueueRef q_ref,
Expand Down Expand Up @@ -1287,11 +1310,6 @@ void func_map_init_arraycreation(func_map_t& fmap)
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_arange_default_c<float>};
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_arange_default_c<double>};

fmap[DPNPFuncName::DPNP_FN_ARANGE_EXT][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_arange_ext_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_ARANGE_EXT][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_arange_ext_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_ARANGE_EXT][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_arange_ext_c<float>};
fmap[DPNPFuncName::DPNP_FN_ARANGE_EXT][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_arange_ext_c<double>};

fmap[DPNPFuncName::DPNP_FN_DIAG][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_diag_default_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_DIAG][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_diag_default_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_DIAG][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_diag_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 @@ -45,7 +45,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_ANY
DPNP_FN_ANY_EXT
DPNP_FN_ARANGE
DPNP_FN_ARANGE_EXT
DPNP_FN_ARCCOS
DPNP_FN_ARCCOS_EXT
DPNP_FN_ARCCOSH
Expand Down Expand Up @@ -519,7 +518,6 @@ cpdef dpnp_descriptor dpnp_matmul(dpnp_descriptor in_array1, dpnp_descriptor in_
"""
Array creation routines
"""
cpdef dpnp_descriptor dpnp_arange(start, stop, step, dtype)
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
39 changes: 1 addition & 38 deletions dpnp/dpnp_algo/dpnp_algo.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 @@ -50,7 +50,6 @@ import numpy


__all__ = [
"dpnp_arange",
"dpnp_astype",
"dpnp_flatten",
"dpnp_init_val",
Expand All @@ -74,9 +73,6 @@ include "dpnp_algo_statistics.pyx"
include "dpnp_algo_trigonometric.pyx"


ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_arange_t)(c_dpctl.DPCTLSyclQueueRef,
size_t, size_t, void *, size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_astype_t)(c_dpctl.DPCTLSyclQueueRef,
const void *, void * , const size_t,
const c_dpctl.DPCTLEventVectorRef)
Expand All @@ -92,39 +88,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_initval_t)(c_dpctl.DPCTLSyclQueueR
const c_dpctl.DPCTLEventVectorRef)


cpdef utils.dpnp_descriptor dpnp_arange(start, stop, step, dtype):
obj_len = int(numpy.ceil((stop - start) / step))
if obj_len < 0:
raise ValueError(f"DPNP dpnp_arange(): Negative array size (start={start},stop={stop},step={step})")

cdef tuple obj_shape = utils._object_to_tuple(obj_len)

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ARANGE_EXT, param1_type, param1_type)

cdef utils.dpnp_descriptor result = utils.create_output_descriptor(obj_shape, kernel_data.return_type, None)

# for i in range(result.size):
# result[i] = start + i

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_dpnp_arange_t func = <fptr_dpnp_arange_t > kernel_data.ptr
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
start,
step,
result.get_data(),
result.size,
NULL) # dep_events_ref)
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef utils.dpnp_descriptor dpnp_astype(utils.dpnp_descriptor x1, dtype):
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(dtype)
Expand Down
25 changes: 25 additions & 0 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,43 @@
"""


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

from dpnp.dpnp_array import dpnp_array
import dpnp


__all__ = [
"arange",
"asarray",
"empty",
]


def arange(start,
/,
stop=None,
step=1,
*,
dtype=None,
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)

array_obj = dpt.arange(start,
stop=stop,
step=step,
dtype=dtype,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized)

return dpnp_array(array_obj.shape, buffer=array_obj)


def asarray(x1,
dtype=None,
copy=False,
Expand Down
56 changes: 22 additions & 34 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# distutils: language = c++
# -*- 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 @@ -90,7 +90,16 @@
]


def arange(start, stop=None, step=1, dtype=None):
def arange(start,
/,
stop=None,
step=1,
*,
dtype=None,
like=None,
device=None,
usm_type="device",
sycl_queue=None):
"""
Returns an array with evenly spaced values within a given interval.

Expand All @@ -99,12 +108,11 @@ def arange(start, stop=None, step=1, dtype=None):
Returns
-------
arange : :obj:`dpnp.ndarray`
The 1-D array of range values.
The 1-D array containing evenly spaced values.

Limitations
-----------
Parameter ``start`` is supported as integer only.
Parameters ``stop`` and ``step`` are supported as either integer or ``None``.
Parameter ``like`` is supported only with default value ``None``.
Otherwise the function will be executed sequentially on CPU.

See Also
Expand All @@ -113,7 +121,6 @@ def arange(start, stop=None, step=1, dtype=None):

Examples
--------

>>> import dpnp as np
>>> [i for i in np.arange(3)]
[0, 1, 2]
Expand All @@ -123,36 +130,17 @@ def arange(start, stop=None, step=1, dtype=None):
[3, 5]

"""
if not use_origin_backend():
if not isinstance(start, int):
pass
elif (stop is not None) and (not isinstance(stop, int)):
pass
elif (step is not None) and (not isinstance(step, int)):
pass
# TODO: modify native implementation to accept negative values
elif (step is not None) and (step < 0):
pass
elif (start is not None) and (start < 0):
pass
elif (start is not None) and (stop is not None) and (start > stop):
pass
elif (dtype is not None) and (dtype not in [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64]):
pass
else:
if dtype is None:
dtype = numpy.int64

if stop is None:
stop = start
start = 0

if step is None:
step = 1

return dpnp_arange(start, stop, step, dtype).get_pyobj()
if like is None:
return dpnp_container.arange(start,
stop=stop,
step=step,
dtype=dtype,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue)

return call_origin(numpy.arange, start, stop=stop, step=step, dtype=dtype)
return call_origin(numpy.arange, start, stop=stop, step=step, dtype=dtype, like=like)


def array(x1,
Expand Down
2 changes: 1 addition & 1 deletion tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ tests/test_indexing.py::test_nonzero[[[0, 1, 2], [3, 0, 5], [6, 7, 0]]]
tests/test_indexing.py::test_nonzero[[[0, 1, 0, 3, 0], [5, 0, 7, 0, 9]]]
tests/test_indexing.py::test_nonzero[[[[1, 2], [0, 4]], [[0, 2], [0, 1]], [[0, 0], [3, 1]]]]
tests/test_indexing.py::test_nonzero[[[[[1, 2, 3], [3, 4, 5]], [[1, 2, 3], [2, 1, 0]]], [[[1, 3, 5], [3, 1, 0]], [[0, 1, 2], [1, 3, 4]]]]]
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int

tests/third_party/cupy/indexing_tests/test_indexing.py::TestIndexing::test_take_no_axis
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_3_{n_vals=1, shape=(7,)}::test_place
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_4_{n_vals=1, shape=(2, 3)}::test_place
Expand Down
Loading