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 4 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
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
54 changes: 21 additions & 33 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,34 +130,15 @@ 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)

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
41 changes: 41 additions & 0 deletions tests/test_arraycreation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
import pytest

import dpnp
import dpctl.tensor as dpt

import numpy

import tempfile


@pytest.mark.parametrize("start",
[0, -5, 10, -2.5, 9.7],
ids=['0', '-5', '10', '-2.5', '9.7'])
@pytest.mark.parametrize("stop",
[None, 10, -2, 20.5, 10**5],
ids=['None', '10', '-2', '20.5', '10**5'])
@pytest.mark.parametrize("step",
[None, 1, 2.7, -1.6, 100],
ids=['None', '1', '2.7', '-1.6', '100'])
@pytest.mark.parametrize("dtype",
[numpy.complex128, numpy.complex64, numpy.float64, numpy.float32, numpy.float16, numpy.int64, numpy.int32],
ids=['complex128', 'complex64', 'float64', 'float32', 'float16', 'int64', 'int32'])
def test_arange(start, stop, step, dtype):
rtol_mult = 2
if numpy.issubdtype(dtype, numpy.float16):
# numpy casts to float32 type when computes float16 data
rtol_mult = 4

# secure there is no 'inf' elements in resulting array
max = numpy.finfo(dtype).max
if stop is not None and stop > max:
# consider comulative accuracy while generating array
# to calculate maximum allowed 'stop' value for dtype=float16
arr_len = (max - start) / (step if step is not None else 1)
arr_ilen = int(arr_len)
arr_len = (arr_ilen + 1) if float(arr_ilen) < arr_len else arr_ilen
acc = rtol_mult * numpy.finfo(dtype).eps
stop = max - acc * arr_len

exp_array = numpy.arange(start, stop=stop, step=step, dtype=dtype)

dpnp_array = dpnp.arange(start, stop=stop, step=step, dtype=dtype)
res_array = dpnp.asnumpy(dpnp_array)

if numpy.issubdtype(dtype, numpy.floating) or numpy.issubdtype(dtype, numpy.complexfloating):
numpy.testing.assert_allclose(exp_array, res_array, rtol=rtol_mult*numpy.finfo(dtype).eps)
else:
numpy.testing.assert_array_equal(exp_array, res_array)


@pytest.mark.parametrize("k",
[-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6],
ids=['-6', '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5', '6'])
Expand Down
21 changes: 21 additions & 0 deletions tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ def vvsort(val, vec, size, xp):
vec[:, imax] = temp


@pytest.mark.parametrize(
"func, arg, kwargs",
[
pytest.param("arange",
-25.7,
{'stop': 10**8, 'step': 15})
])
@pytest.mark.parametrize("device",
valid_devices,
ids=[device.filter_string for device in valid_devices])
def test_array_creation(func, arg, kwargs, device):
numpy_array = getattr(numpy, func)(arg, **kwargs)

dpnp_kwargs = dict(kwargs)
dpnp_kwargs['device'] = device
dpnp_array = getattr(dpnp, func)(arg, **dpnp_kwargs)

numpy.testing.assert_array_equal(numpy_array, dpnp_array)
assert dpnp_array.sycl_device == device


@pytest.mark.parametrize(
"func,data",
[
Expand Down