1
1
import math
2
2
import operator
3
3
4
+ import dpctl .tensor as dpt
4
5
import dpctl .utils as dpu
5
6
import numpy
6
7
7
8
import dpnp
8
- import dpnp .dpnp_container as dpnp_container
9
9
import dpnp .dpnp_utils as utils
10
+ from dpnp .dpnp_array import dpnp_array
10
11
11
12
__all__ = [
12
13
"dpnp_geomspace" ,
16
17
]
17
18
18
19
20
+ def _as_usm_ndarray (a , usm_type , sycl_queue ):
21
+ if isinstance (a , dpnp_array ):
22
+ return a .get_array ()
23
+ return dpt .asarray (a , usm_type = usm_type , sycl_queue = sycl_queue )
24
+
25
+
19
26
def dpnp_geomspace (
20
27
start ,
21
28
stop ,
@@ -40,14 +47,8 @@ def dpnp_geomspace(
40
47
else :
41
48
_usm_type = usm_type
42
49
43
- if not dpnp .is_supported_array_type (start ):
44
- start = dpnp .asarray (
45
- start , usm_type = _usm_type , sycl_queue = sycl_queue_normalized
46
- )
47
- if not dpnp .is_supported_array_type (stop ):
48
- stop = dpnp .asarray (
49
- stop , usm_type = _usm_type , sycl_queue = sycl_queue_normalized
50
- )
50
+ start = _as_usm_ndarray (start , _usm_type , sycl_queue_normalized )
51
+ stop = _as_usm_ndarray (stop , _usm_type , sycl_queue_normalized )
51
52
52
53
dt = numpy .result_type (start , stop , float (num ))
53
54
dt = utils .map_dtype_to_device (dt , sycl_queue_normalized .sycl_device )
@@ -57,8 +58,8 @@ def dpnp_geomspace(
57
58
if dpnp .any (start == 0 ) or dpnp .any (stop == 0 ):
58
59
raise ValueError ("Geometric sequence cannot include zero" )
59
60
60
- out_sign = dpnp .ones (
61
- dpnp .broadcast_arrays (start , stop )[0 ].shape ,
61
+ out_sign = dpt .ones (
62
+ dpt .broadcast_arrays (start , stop )[0 ].shape ,
62
63
dtype = dt ,
63
64
usm_type = _usm_type ,
64
65
sycl_queue = sycl_queue_normalized ,
@@ -72,15 +73,15 @@ def dpnp_geomspace(
72
73
stop [all_imag ] = stop [all_imag ].imag
73
74
out_sign [all_imag ] = 1j
74
75
75
- both_negative = (dpnp .sign (start ) == - 1 ) & (dpnp .sign (stop ) == - 1 )
76
+ both_negative = (dpt .sign (start ) == - 1 ) & (dpt .sign (stop ) == - 1 )
76
77
if dpnp .any (both_negative ):
77
- dpnp .negative (start [both_negative ], out = start [both_negative ])
78
- dpnp .negative (stop [both_negative ], out = stop [both_negative ])
79
- dpnp .negative (out_sign [both_negative ], out = out_sign [both_negative ])
78
+ dpt .negative (start [both_negative ], out = start [both_negative ])
79
+ dpt .negative (stop [both_negative ], out = stop [both_negative ])
80
+ dpt .negative (out_sign [both_negative ], out = out_sign [both_negative ])
80
81
81
- log_start = dpnp .log10 (start )
82
- log_stop = dpnp .log10 (stop )
83
- result = dpnp_logspace (
82
+ log_start = dpt .log10 (start )
83
+ log_stop = dpt .log10 (stop )
84
+ res = dpnp_logspace (
84
85
log_start ,
85
86
log_stop ,
86
87
num = num ,
@@ -89,19 +90,20 @@ def dpnp_geomspace(
89
90
dtype = dtype ,
90
91
usm_type = _usm_type ,
91
92
sycl_queue = sycl_queue_normalized ,
92
- )
93
+ ). get_array ()
93
94
94
95
if num > 0 :
95
- result [0 ] = start
96
+ res [0 ] = start
96
97
if num > 1 and endpoint :
97
- result [- 1 ] = stop
98
+ res [- 1 ] = stop
98
99
99
- result = out_sign * result
100
+ res = out_sign * res
100
101
101
102
if axis != 0 :
102
- result = dpnp .moveaxis (result , 0 , axis )
103
+ res = dpt .moveaxis (res , 0 , axis )
103
104
104
- return result .astype (dtype , copy = False )
105
+ res = dpt .astype (res , dtype , copy = False )
106
+ return dpnp_array ._create_from_usm_ndarray (res )
105
107
106
108
107
109
def dpnp_linspace (
@@ -129,14 +131,11 @@ def dpnp_linspace(
129
131
else :
130
132
_usm_type = usm_type
131
133
132
- if not hasattr (start , "dtype" ) and not dpnp .isscalar (start ):
133
- start = dpnp .asarray (
134
- start , usm_type = _usm_type , sycl_queue = sycl_queue_normalized
135
- )
136
- if not hasattr (stop , "dtype" ) and not dpnp .isscalar (stop ):
137
- stop = dpnp .asarray (
138
- stop , usm_type = _usm_type , sycl_queue = sycl_queue_normalized
139
- )
134
+ if not dpnp .isscalar (start ):
135
+ start = _as_usm_ndarray (start , _usm_type , sycl_queue_normalized )
136
+
137
+ if not dpnp .isscalar (stop ):
138
+ stop = _as_usm_ndarray (stop , _usm_type , sycl_queue_normalized )
140
139
141
140
dt = numpy .result_type (start , stop , float (num ))
142
141
dt = utils .map_dtype_to_device (dt , sycl_queue_normalized .sycl_device )
@@ -155,7 +154,7 @@ def dpnp_linspace(
155
154
156
155
if dpnp .isscalar (start ) and dpnp .isscalar (stop ):
157
156
# Call linspace() function for scalars.
158
- res = dpnp_container .linspace (
157
+ usm_res = dpt .linspace (
159
158
start ,
160
159
stop ,
161
160
num ,
@@ -167,17 +166,17 @@ def dpnp_linspace(
167
166
if retstep is True and step_nan is False :
168
167
step = (stop - start ) / step_num
169
168
else :
170
- _start = dpnp .asarray (
169
+ usm_start = dpt .asarray (
171
170
start ,
172
171
dtype = dt ,
173
172
usm_type = _usm_type ,
174
173
sycl_queue = sycl_queue_normalized ,
175
174
)
176
- _stop = dpnp .asarray (
175
+ usm_stop = dpt .asarray (
177
176
stop , dtype = dt , usm_type = _usm_type , sycl_queue = sycl_queue_normalized
178
177
)
179
178
180
- res = dpnp_container .arange (
179
+ usm_res = dpt .arange (
181
180
0 ,
182
181
stop = num ,
183
182
step = 1 ,
@@ -187,28 +186,29 @@ def dpnp_linspace(
187
186
)
188
187
189
188
if step_nan is False :
190
- step = (_stop - _start ) / step_num
191
- res = res .reshape ((- 1 ,) + (1 ,) * step .ndim )
192
- res = res * step + _start
189
+ step = (usm_stop - usm_start ) / step_num
190
+ usm_res = dpt .reshape (usm_res , (- 1 ,) + (1 ,) * step .ndim , copy = False )
191
+ usm_res = usm_res * step
192
+ usm_res += usm_start
193
193
194
194
if endpoint and num > 1 :
195
- res [- 1 ] = dpnp_container .full (step .shape , _stop )
195
+ usm_res [- 1 ] = dpt .full (step .shape , usm_stop )
196
196
197
197
if axis != 0 :
198
- res = dpnp .moveaxis (res , 0 , axis )
198
+ usm_res = dpt .moveaxis (usm_res , 0 , axis )
199
199
200
200
if numpy .issubdtype (dtype , dpnp .integer ):
201
- dpnp .floor (res , out = res )
201
+ dpt .floor (usm_res , out = usm_res )
202
202
203
- res = res .astype (dtype , copy = False )
203
+ res = dpt .astype (usm_res , dtype , copy = False )
204
+ res = dpnp_array ._create_from_usm_ndarray (res )
204
205
205
206
if retstep is True :
206
207
if dpnp .isscalar (step ):
207
- step = dpnp .asarray (
208
+ step = dpt .asarray (
208
209
step , usm_type = res .usm_type , sycl_queue = res .sycl_queue
209
210
)
210
- return (res , step )
211
-
211
+ return res , dpnp_array ._create_from_usm_ndarray (step )
212
212
return res
213
213
214
214
@@ -239,12 +239,15 @@ def dpnp_logspace(
239
239
usm_type = "device" if usm_type_alloc is None else usm_type_alloc
240
240
else :
241
241
usm_type = usm_type
242
- start = dpnp .asarray (start , usm_type = usm_type , sycl_queue = sycl_queue )
243
- stop = dpnp .asarray (stop , usm_type = usm_type , sycl_queue = sycl_queue )
244
- base = dpnp .asarray (base , usm_type = usm_type , sycl_queue = sycl_queue )
245
- [start , stop , base ] = dpnp .broadcast_arrays (start , stop , base )
246
- base = dpnp .expand_dims (base , axis = axis )
247
242
243
+ start = _as_usm_ndarray (start , usm_type , sycl_queue )
244
+ stop = _as_usm_ndarray (stop , usm_type , sycl_queue )
245
+ base = _as_usm_ndarray (base , usm_type , sycl_queue )
246
+
247
+ [start , stop , base ] = dpt .broadcast_arrays (start , stop , base )
248
+ base = dpt .expand_dims (base , axis = axis )
249
+
250
+ # assume res as not a tuple, because retstep is False
248
251
res = dpnp_linspace (
249
252
start ,
250
253
stop ,
@@ -254,11 +257,12 @@ def dpnp_logspace(
254
257
sycl_queue = sycl_queue ,
255
258
endpoint = endpoint ,
256
259
axis = axis ,
257
- )
260
+ ). get_array ()
258
261
259
- if dtype is None :
260
- return dpnp .power (base , res )
261
- return dpnp .power (base , res ).astype (dtype , copy = False )
262
+ dpt .pow (base , res , out = res )
263
+ if dtype is not None :
264
+ res = dpt .astype (res , dtype , copy = False )
265
+ return dpnp_array ._create_from_usm_ndarray (res )
262
266
263
267
264
268
class dpnp_nd_grid :
0 commit comments