Skip to content

Commit abc4c4a

Browse files
authored
Merge branch 'master' into divide_in_place
2 parents 88c113d + 247a266 commit abc4c4a

9 files changed

+614
-247
lines changed

dpnp/dpnp_algo/dpnp_algo_arraycreation.pxi

-113
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ and the rest of the library
3838
__all__ += [
3939
"dpnp_copy",
4040
"dpnp_diag",
41-
"dpnp_geomspace",
42-
"dpnp_linspace",
43-
"dpnp_logspace",
4441
"dpnp_ptp",
4542
"dpnp_trace",
4643
"dpnp_vander",
@@ -138,116 +135,6 @@ cpdef utils.dpnp_descriptor dpnp_diag(utils.dpnp_descriptor v, int k):
138135
return result
139136

140137

141-
cpdef utils.dpnp_descriptor dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
142-
cdef shape_type_c obj_shape = utils._object_to_tuple(num)
143-
cdef utils.dpnp_descriptor result = utils_py.create_output_descriptor_py(obj_shape, dtype, None)
144-
145-
if endpoint:
146-
steps_count = num - 1
147-
else:
148-
steps_count = num
149-
150-
# if there are steps, then fill values
151-
if steps_count > 0:
152-
step = dpnp.power(dpnp.float64(stop) / start, 1.0 / steps_count)
153-
mult = step
154-
for i in range(1, result.size):
155-
result.get_pyobj()[i] = start * mult
156-
mult = mult * step
157-
else:
158-
step = dpnp.nan
159-
160-
# if result is not empty, then fiil first and last elements
161-
if num > 0:
162-
result.get_pyobj()[0] = start
163-
if endpoint and result.size > 1:
164-
result.get_pyobj()[result.size - 1] = stop
165-
166-
return result
167-
168-
169-
def dpnp_linspace(start, stop, num, dtype=None, device=None, usm_type=None, sycl_queue=None, endpoint=True, retstep=False, axis=0):
170-
usm_type_alloc, sycl_queue_alloc = utils_py.get_usm_allocations([start, stop])
171-
172-
# Get sycl_queue.
173-
if sycl_queue is None and device is None:
174-
sycl_queue = sycl_queue_alloc
175-
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
176-
177-
# Get temporary usm_type for getting dtype.
178-
if usm_type is None:
179-
_usm_type = "device" if usm_type_alloc is None else usm_type_alloc
180-
else:
181-
_usm_type = usm_type
182-
183-
# Get dtype.
184-
if not hasattr(start, "dtype") and not dpnp.isscalar(start):
185-
start = dpnp.asarray(start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized)
186-
if not hasattr(stop, "dtype") and not dpnp.isscalar(stop):
187-
stop = dpnp.asarray(stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized)
188-
dt = numpy.result_type(start, stop, float(num))
189-
dt = utils_py.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device)
190-
if dtype is None:
191-
dtype = dt
192-
193-
if dpnp.isscalar(start) and dpnp.isscalar(stop):
194-
# Call linspace() function for scalars.
195-
res = dpnp_container.linspace(start,
196-
stop,
197-
num,
198-
dtype=dt,
199-
usm_type=_usm_type,
200-
sycl_queue=sycl_queue_normalized,
201-
endpoint=endpoint)
202-
else:
203-
num = operator.index(num)
204-
if num < 0:
205-
raise ValueError("Number of points must be non-negative")
206-
207-
# Get final usm_type and copy arrays if needed with current dtype, usm_type and sycl_queue.
208-
# Do not need to copy usm_ndarray by usm_type if it is not explicitly stated.
209-
if usm_type is None:
210-
usm_type = _usm_type
211-
if not hasattr(start, "usm_type"):
212-
_start = dpnp.asarray(start, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
213-
else:
214-
_start = dpnp.asarray(start, dtype=dt, sycl_queue=sycl_queue_normalized)
215-
if not hasattr(stop, "usm_type"):
216-
_stop = dpnp.asarray(stop, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
217-
else:
218-
_stop = dpnp.asarray(stop, dtype=dt, sycl_queue=sycl_queue_normalized)
219-
else:
220-
_start = dpnp.asarray(start, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
221-
_stop = dpnp.asarray(stop, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
222-
223-
# FIXME: issue #1304. Mathematical operations with scalar don't follow data type.
224-
_num = dpnp.asarray((num - 1) if endpoint else num, dtype=dt, usm_type=usm_type, sycl_queue=sycl_queue_normalized)
225-
226-
step = (_stop - _start) / _num
227-
228-
res = dpnp_container.arange(0,
229-
stop=num,
230-
step=1,
231-
dtype=dt,
232-
usm_type=usm_type,
233-
sycl_queue=sycl_queue_normalized)
234-
235-
res = res.reshape((-1,) + (1,) * step.ndim)
236-
res = res * step + _start
237-
238-
if endpoint and num > 1:
239-
res[-1] = dpnp_container.full(step.shape, _stop)
240-
241-
if numpy.issubdtype(dtype, dpnp.integer):
242-
dpnp.floor(res, out=res)
243-
return res.astype(dtype)
244-
245-
246-
cpdef utils.dpnp_descriptor dpnp_logspace(start, stop, num, endpoint, base, dtype, axis):
247-
temp = dpnp.linspace(start, stop, num=num, endpoint=endpoint)
248-
return dpnp.get_dpnp_descriptor(dpnp.astype(dpnp.power(base, temp), dtype))
249-
250-
251138
cpdef dpnp_ptp(utils.dpnp_descriptor arr, axis=None):
252139
cdef shape_type_c shape_arr = arr.shape
253140
cdef shape_type_c output_shape

dpnp/dpnp_algo/dpnp_arraycreation.py

+258
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
import operator
2+
3+
import numpy
4+
5+
import dpnp
6+
import dpnp.dpnp_container as dpnp_container
7+
import dpnp.dpnp_utils as utils
8+
9+
__all__ = [
10+
"dpnp_geomspace",
11+
"dpnp_linspace",
12+
"dpnp_logspace",
13+
]
14+
15+
16+
def dpnp_geomspace(
17+
start,
18+
stop,
19+
num,
20+
dtype=None,
21+
device=None,
22+
usm_type=None,
23+
sycl_queue=None,
24+
endpoint=True,
25+
axis=0,
26+
):
27+
usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations([start, stop])
28+
29+
if sycl_queue is None and device is None:
30+
sycl_queue = sycl_queue_alloc
31+
sycl_queue_normalized = dpnp.get_normalized_queue_device(
32+
sycl_queue=sycl_queue, device=device
33+
)
34+
35+
if usm_type is None:
36+
_usm_type = "device" if usm_type_alloc is None else usm_type_alloc
37+
else:
38+
_usm_type = usm_type
39+
40+
if not dpnp.is_supported_array_type(start):
41+
start = dpnp.asarray(
42+
start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized
43+
)
44+
if not dpnp.is_supported_array_type(stop):
45+
stop = dpnp.asarray(
46+
stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized
47+
)
48+
49+
dt = numpy.result_type(start, stop, float(num))
50+
dt = utils.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device)
51+
if dtype is None:
52+
dtype = dt
53+
54+
if dpnp.any(start == 0) or dpnp.any(stop == 0):
55+
raise ValueError("Geometric sequence cannot include zero")
56+
57+
out_sign = dpnp.ones(
58+
dpnp.broadcast_arrays(start, stop)[0].shape,
59+
dtype=dt,
60+
usm_type=_usm_type,
61+
sycl_queue=sycl_queue_normalized,
62+
)
63+
# Avoid negligible real or imaginary parts in output by rotating to
64+
# positive real, calculating, then undoing rotation
65+
if dpnp.issubdtype(dt, dpnp.complexfloating):
66+
all_imag = (start.real == 0.0) & (stop.real == 0.0)
67+
if dpnp.any(all_imag):
68+
start[all_imag] = start[all_imag].imag
69+
stop[all_imag] = stop[all_imag].imag
70+
out_sign[all_imag] = 1j
71+
72+
both_negative = (dpnp.sign(start) == -1) & (dpnp.sign(stop) == -1)
73+
if dpnp.any(both_negative):
74+
dpnp.negative(start[both_negative], out=start[both_negative])
75+
dpnp.negative(stop[both_negative], out=stop[both_negative])
76+
dpnp.negative(out_sign[both_negative], out=out_sign[both_negative])
77+
78+
log_start = dpnp.log10(start)
79+
log_stop = dpnp.log10(stop)
80+
result = dpnp_logspace(
81+
log_start,
82+
log_stop,
83+
num=num,
84+
endpoint=endpoint,
85+
base=10.0,
86+
dtype=dtype,
87+
usm_type=_usm_type,
88+
sycl_queue=sycl_queue_normalized,
89+
)
90+
91+
if num > 0:
92+
result[0] = start
93+
if num > 1 and endpoint:
94+
result[-1] = stop
95+
96+
result = out_sign * result
97+
98+
if axis != 0:
99+
result = dpnp.moveaxis(result, 0, axis)
100+
101+
return result.astype(dtype, copy=False)
102+
103+
104+
def dpnp_linspace(
105+
start,
106+
stop,
107+
num,
108+
dtype=None,
109+
device=None,
110+
usm_type=None,
111+
sycl_queue=None,
112+
endpoint=True,
113+
retstep=False,
114+
axis=0,
115+
):
116+
usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations([start, stop])
117+
118+
if sycl_queue is None and device is None:
119+
sycl_queue = sycl_queue_alloc
120+
sycl_queue_normalized = dpnp.get_normalized_queue_device(
121+
sycl_queue=sycl_queue, device=device
122+
)
123+
124+
if usm_type is None:
125+
_usm_type = "device" if usm_type_alloc is None else usm_type_alloc
126+
else:
127+
_usm_type = usm_type
128+
129+
if not hasattr(start, "dtype") and not dpnp.isscalar(start):
130+
start = dpnp.asarray(
131+
start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized
132+
)
133+
if not hasattr(stop, "dtype") and not dpnp.isscalar(stop):
134+
stop = dpnp.asarray(
135+
stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized
136+
)
137+
138+
dt = numpy.result_type(start, stop, float(num))
139+
dt = utils.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device)
140+
if dtype is None:
141+
dtype = dt
142+
143+
num = operator.index(num)
144+
if num < 0:
145+
raise ValueError("Number of points must be non-negative")
146+
step_num = (num - 1) if endpoint else num
147+
148+
step_nan = False
149+
if step_num == 0:
150+
step_nan = True
151+
step = dpnp.nan
152+
153+
if dpnp.isscalar(start) and dpnp.isscalar(stop):
154+
# Call linspace() function for scalars.
155+
res = dpnp_container.linspace(
156+
start,
157+
stop,
158+
num,
159+
dtype=dt,
160+
usm_type=_usm_type,
161+
sycl_queue=sycl_queue_normalized,
162+
endpoint=endpoint,
163+
)
164+
if retstep is True and step_nan is False:
165+
step = (stop - start) / step_num
166+
else:
167+
_start = dpnp.asarray(
168+
start,
169+
dtype=dt,
170+
usm_type=_usm_type,
171+
sycl_queue=sycl_queue_normalized,
172+
)
173+
_stop = dpnp.asarray(
174+
stop, dtype=dt, usm_type=_usm_type, sycl_queue=sycl_queue_normalized
175+
)
176+
177+
res = dpnp_container.arange(
178+
0,
179+
stop=num,
180+
step=1,
181+
dtype=dt,
182+
usm_type=_usm_type,
183+
sycl_queue=sycl_queue_normalized,
184+
)
185+
186+
if step_nan is False:
187+
step = (_stop - _start) / step_num
188+
res = res.reshape((-1,) + (1,) * step.ndim)
189+
res = res * step + _start
190+
191+
if endpoint and num > 1:
192+
res[-1] = dpnp_container.full(step.shape, _stop)
193+
194+
if axis != 0:
195+
res = dpnp.moveaxis(res, 0, axis)
196+
197+
if numpy.issubdtype(dtype, dpnp.integer):
198+
dpnp.floor(res, out=res)
199+
200+
res = res.astype(dtype, copy=False)
201+
202+
if retstep is True:
203+
if dpnp.isscalar(step):
204+
step = dpnp.asarray(
205+
step, usm_type=res.usm_type, sycl_queue=res.sycl_queue
206+
)
207+
return (res, step)
208+
209+
return res
210+
211+
212+
def dpnp_logspace(
213+
start,
214+
stop,
215+
num=50,
216+
device=None,
217+
usm_type=None,
218+
sycl_queue=None,
219+
endpoint=True,
220+
base=10.0,
221+
dtype=None,
222+
axis=0,
223+
):
224+
if not dpnp.isscalar(base):
225+
usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations(
226+
[start, stop, base]
227+
)
228+
229+
if sycl_queue is None and device is None:
230+
sycl_queue = sycl_queue_alloc
231+
sycl_queue = dpnp.get_normalized_queue_device(
232+
sycl_queue=sycl_queue, device=device
233+
)
234+
235+
if usm_type is None:
236+
usm_type = "device" if usm_type_alloc is None else usm_type_alloc
237+
else:
238+
usm_type = usm_type
239+
start = dpnp.asarray(start, usm_type=usm_type, sycl_queue=sycl_queue)
240+
stop = dpnp.asarray(stop, usm_type=usm_type, sycl_queue=sycl_queue)
241+
base = dpnp.asarray(base, usm_type=usm_type, sycl_queue=sycl_queue)
242+
[start, stop, base] = dpnp.broadcast_arrays(start, stop, base)
243+
base = dpnp.expand_dims(base, axis=axis)
244+
245+
res = dpnp_linspace(
246+
start,
247+
stop,
248+
num=num,
249+
device=device,
250+
usm_type=usm_type,
251+
sycl_queue=sycl_queue,
252+
endpoint=endpoint,
253+
axis=axis,
254+
)
255+
256+
if dtype is None:
257+
return dpnp.power(base, res)
258+
return dpnp.power(base, res).astype(dtype, copy=False)

0 commit comments

Comments
 (0)