Skip to content
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

Update dpnp.geomspace and dpnp.logspace functions #1603

Merged
merged 18 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
113 changes: 0 additions & 113 deletions dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ and the rest of the library
__all__ += [
"dpnp_copy",
"dpnp_diag",
"dpnp_geomspace",
"dpnp_linspace",
"dpnp_logspace",
"dpnp_ptp",
"dpnp_trace",
"dpnp_vander",
Expand Down Expand Up @@ -138,116 +135,6 @@ cpdef utils.dpnp_descriptor dpnp_diag(utils.dpnp_descriptor v, int k):
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)

if endpoint:
steps_count = num - 1
else:
steps_count = num

# if there are steps, then fill values
if steps_count > 0:
step = dpnp.power(dpnp.float64(stop) / start, 1.0 / steps_count)
mult = step
for i in range(1, result.size):
result.get_pyobj()[i] = start * mult
mult = mult * step
else:
step = dpnp.nan

# 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

return result


def dpnp_linspace(start, stop, num, dtype=None, device=None, usm_type=None, sycl_queue=None, endpoint=True, retstep=False, axis=0):
usm_type_alloc, sycl_queue_alloc = utils_py.get_usm_allocations([start, stop])

# Get sycl_queue.
if sycl_queue is None and device is None:
sycl_queue = sycl_queue_alloc
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)

# Get temporary usm_type for getting dtype.
if usm_type is None:
_usm_type = "device" if usm_type_alloc is None else usm_type_alloc
else:
_usm_type = usm_type

# Get dtype.
if not hasattr(start, "dtype") and not dpnp.isscalar(start):
start = dpnp.asarray(start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized)
if not hasattr(stop, "dtype") and not dpnp.isscalar(stop):
stop = dpnp.asarray(stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized)
dt = numpy.result_type(start, stop, float(num))
dt = utils_py.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device)
if dtype is None:
dtype = dt

if dpnp.isscalar(start) and dpnp.isscalar(stop):
# Call linspace() function for scalars.
res = dpnp_container.linspace(start,
stop,
num,
dtype=dt,
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")

# Get final usm_type and copy arrays if needed with current dtype, usm_type and sycl_queue.
# Do not need to copy usm_ndarray by usm_type if it is not explicitly stated.
if usm_type is None:
usm_type = _usm_type
if not hasattr(start, "usm_type"):
_start = dpnp.asarray(start, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
else:
_start = dpnp.asarray(start, dtype=dt, sycl_queue=sycl_queue_normalized)
if not hasattr(stop, "usm_type"):
_stop = dpnp.asarray(stop, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
else:
_stop = dpnp.asarray(stop, dtype=dt, sycl_queue=sycl_queue_normalized)
else:
_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)

# FIXME: issue #1304. Mathematical operations with scalar don't follow data type.
_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 numpy.issubdtype(dtype, dpnp.integer):
dpnp.floor(res, out=res)
return res.astype(dtype)


cpdef utils.dpnp_descriptor dpnp_logspace(start, stop, num, endpoint, base, dtype, axis):
temp = dpnp.linspace(start, stop, num=num, endpoint=endpoint)
return dpnp.get_dpnp_descriptor(dpnp.astype(dpnp.power(base, temp), 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
Loading
Loading