Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
112 changes: 91 additions & 21 deletions dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,101 @@ cpdef utils.dpnp_descriptor dpnp_identity(n, result_dtype):
return result


# TODO this function should work through dpnp_arange_c
cpdef tuple dpnp_linspace(start, stop, num, endpoint, retstep, 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)
cpdef utils.dpnp_descriptor dpnp_linspace(start, stop, num, dtype=None, device=None, usm_type=None, sycl_queue=None, endpoint=True, retstep=False, axis=0):
start_isarray = isinstance(start, (dpnp.ndarray, dpctl.tensor.usm_ndarray))
stop_isarray = isinstance(stop, (dpnp.ndarray, dpctl.tensor.usm_ndarray))

if sycl_queue is None and device is None:
sycl_queue = utils_py.get_common_allocation_queue([start, stop])
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)

if usm_type is None:
if start_isarray and stop_isarray:
usm_type = dpctl.utils.get_coerced_usm_type([start.usm_type, stop.usm_type])
elif start_isarray:
usm_type = start.usm_type
elif stop_isarray:
usm_type = stop.usm_type
else:
usm_type = "device"

if endpoint:
steps_count = num - 1
else:
steps_count = num
start_isarray = start_isarray or isinstance(start, numpy.ndarray)
stop_isarray = stop_isarray or isinstance(stop, numpy.ndarray)

# if there are steps, then fill values
if steps_count > 0:
step = (dpnp.float64(stop) - start) / steps_count
for i in range(1, result.size):
result.get_pyobj()[i] = start + step * i
else:
step = dpnp.nan
dt = None
if start_isarray and stop_isarray:
dt = numpy.result_type(start.dtype, stop.dtype)
elif start_isarray:
dt = start.dtype
elif stop_isarray:
dt = stop.dtype

# if result is not empty, then fiil first and last elements
if num > 0:
result.get_pyobj()[0] = start
if endpoint and result.size > 1:
result.get_pyobj()[result.size - 1] = stop
if dt == None or numpy.issubdtype(dt, dpnp.integer):
dt = numpy.result_type(float(num), dt)

dt = utils_py.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device)

return (result.get_pyobj(), step)
if dtype is None:
dtype = dt

if dpnp.isscalar(start) and dpnp.isscalar(stop):
res = dpnp_container.linspace(start,
stop,
num,
dtype=dt,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
endpoint=endpoint)
else:
#num = operator.index(num)
if num < 0:
raise ValueError("Number of points must be non-negative")

#FIXME: When subtraction with scalar will be implemented
start_isscalar = dpnp.isscalar(start)
if start_isscalar:
start = [start]
elif start_isarray:
if start.ndim == 0:
start = start.reshape((1))
start_isscalar = True
stop_isscalar = dpnp.isscalar(stop)
if stop_isscalar:
stop = [stop]
elif stop_isarray:
if stop.ndim == 0:
stop = stop.reshape((1))
stop_isscalar = True

_start = dpnp.asarray(start, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
_stop = dpnp.asarray(stop, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
Copy link
Contributor

@antonwolfy antonwolfy Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case start and stop are both dpnp_array or usm_ndarray, we might change usm_type type here for resulting _start and _stop and so zero copy will not be applied (i.e. extra memory copy might take place).


_num = dpnp.asarray([(num - 1) if endpoint else num], dtype=dt,
usm_type=usm_type, sycl_queue=sycl_queue_normalized)

step = (_stop - _start) / _num

res = dpnp_container.arange(0,
stop=num,
step=1,
dtype=dt,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized)

res = res.reshape((-1,) + (1,) * step.ndim)
res = res * step + _start

if endpoint and num > 1:
res[-1] = dpnp_container.full(step.shape, _stop)

if stop_isscalar and start_isscalar:
res = res.reshape(-1)

if numpy.issubdtype(dtype, dpnp.integer):
res = res + dpnp.asarray([0.000000001], dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
dpnp.floor(res, out=res)
return dpnp.get_dpnp_descriptor(res.astype(dtype), copy_when_nondefault_queue=False)


cpdef utils.dpnp_descriptor dpnp_logspace(start, stop, num, endpoint, base, dtype, axis):
Expand Down
68 changes: 47 additions & 21 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"empty",
"eye",
"full",
"linspace",
"ones"
"zeros",
]
Expand Down Expand Up @@ -124,6 +125,33 @@ def empty(shape,
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


def eye(N,
M=None,
/,
*,
k=0,
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` with ones on the `k`th diagonal."""
array_obj = dpt.eye(N,
M,
k=k,
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 full(shape,
fill_value,
*,
Expand Down Expand Up @@ -151,31 +179,29 @@ def full(shape,
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)


def eye(N,
M=None,
/,
*,
k=0,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None):
def linspace(start,
stop,
/,
num,
*,
dtype=None,
device=None,
usm_type="device",
sycl_queue=None,
endpoint=True):
"""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` with ones on the `k`th diagonal."""
array_obj = dpt.eye(N,
M,
k=k,
dtype=dtype,
order=order,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
"""Creates `dpnp_array` with evenly spaced numbers of specified interval."""
array_obj = dpt.linspace(start,
stop,
num,
dtype=dtype,
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
endpoint=endpoint)
return dpnp_array(array_obj.shape, buffer=array_obj)


def ones(shape,
Expand Down
39 changes: 28 additions & 11 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import dpnp.dpnp_container as dpnp_container
import dpctl.tensor as dpt
import dpctl


__all__ = [
Expand Down Expand Up @@ -878,7 +879,18 @@ def identity(n, dtype=None, *, like=None):
return call_origin(numpy.identity, n, dtype=dtype, like=like)


def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0):
def linspace(start,
stop,
/,
num,
*,
dtype=None,
device=None,
usm_type=None,
sycl_queue=None,
endpoint=True,
retstep=False,
axis=0):
"""
Return evenly spaced numbers over a specified interval.

Expand All @@ -887,6 +899,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis
Limitations
-----------
Parameter ``axis`` is supported only with default value ``0``.
Parameter ``retstep`` is supported only with default value ``False``.
Otherwise the function will be executed sequentially on CPU.

See Also
--------
Expand All @@ -912,16 +926,19 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis

"""

if not use_origin_backend():
if axis != 0:
checker_throw_value_error("linspace", "axis", axis, 0)

res = dpnp_linspace(start, stop, num, endpoint, retstep, dtype, axis)

if retstep:
return res
else:
return res[0]
if retstep is not False:
pass
elif axis != 0:
pass
else:
return dpnp_linspace(start,
stop,
num,
dtype=dtype,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue,
endpoint=endpoint).get_pyobj()

return call_origin(numpy.linspace, start, stop, num, endpoint, retstep, dtype, axis)

Expand Down
2 changes: 1 addition & 1 deletion dpnp/dpnp_utils/dpnp_algo_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def get_common_allocation_queue(objects):
elif len(queues_in_use) == 1:
return queues_in_use[0]

common_queue = dpt.get_execution_queue(queues_in_use)
common_queue = dpctl.utils.get_execution_queue(queues_in_use)
if common_queue is None:
raise ValueError("Input arrays must be allocated on the same SYCL queue")
return common_queue
Expand Down
8 changes: 3 additions & 5 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,10 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid3
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_float_underflow
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop2
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_start_stop_list
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_one_num_no_endopoint_with_retstep
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_with_retstep
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_zero_num_no_endopoint_with_retstep
tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1
tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len
tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1
Expand Down
14 changes: 4 additions & 10 deletions tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -602,19 +602,13 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid5
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4
tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5

tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_negative_size
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int

tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_float_underflow
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_mixed_start_stop2
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_start_stop_list

tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_array_start_stop_axis1
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_one_num_no_endopoint_with_retstep
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_with_retstep
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_linspace_zero_num_no_endopoint_with_retstep
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_logspace_zero_num

tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_1_{axes=None, norm=None, s=(1, None), shape=(3, 4)}::test_fft2
tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_7_{axes=(), norm=None, s=None, shape=(3, 4)}::test_fft2
tests/third_party/cupy/fft_tests/test_fft.py::TestFft2_param_7_{axes=(), norm=None, s=None, shape=(3, 4)}::test_ifft2
Expand Down
45 changes: 45 additions & 0 deletions tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,48 @@ def test_dpctl_tensor_input(func, args):
assert X.shape == Y.shape
else:
assert_array_equal(X, Y)


@pytest.mark.parametrize("start",
[0, -5, 10, -2.5, 9.7],
ids=['0', '-5', '10', '-2.5', '9.7'])
@pytest.mark.parametrize("stop",
[0, 10, -2, 20.5, 1000],
ids=['0', '10', '-2', '20.5', '1000'])
@pytest.mark.parametrize("num",
[5, 10, 17, 100],
ids=['5', '10', '17', '100'])
# @pytest.mark.parametrize("num",
# [5, numpy.array(10), dpnp.array(17), dpt.asarray(100)],
# ids=['5', 'numpy.array(10)', 'dpnp.array(17)', 'dpt.asarray(100)'])
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_float16=False))
def test_linspace(start, stop, num, dtype):
func = lambda xp: xp.linspace(start, stop, num, dtype=dtype)

if numpy.issubdtype(dtype, numpy.floating) or numpy.issubdtype(dtype, numpy.complexfloating):
assert_allclose(func(numpy), func(dpnp), atol=numpy.finfo(dtype).eps)
else:
assert_array_equal(func(numpy), func(dpnp))


@pytest.mark.parametrize("start_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
@pytest.mark.parametrize("stop_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_linspace_dtype(start_dtype, stop_dtype):
start = dpnp.asarray([1, 2, 3], dtype=start_dtype)
stop = dpnp.asarray([11, 7, -2], dtype=stop_dtype)
dpnp.linspace(start, stop, 10)


@pytest.mark.parametrize("start",
[dpnp.array(1), dpnp.array([2.6]), numpy.array([[-6.7, 3]]), [1, -4], (3, 5)])
@pytest.mark.parametrize("stop",
[dpnp.array([-4]), dpnp.array([[2.6], [- 4]]), numpy.array(2), [[-4.6]], (3,)])
def test_linspace_arrays(start, stop):
func = lambda xp: xp.linspace(start, stop, 10)
func(dpnp)
#assert func(numpy).shape == func(dpnp).shape
#assert_array_equal(func(numpy), func(dpnp))
Loading