43
43
import dpctl
44
44
import dpctl .tensor as dpt
45
45
import numpy
46
+ from dpctl .tensor ._device import normalize_queue_device
46
47
47
48
import dpnp
48
49
from dpnp .dpnp_algo import *
49
50
from dpnp .dpnp_array import dpnp_array
50
- from dpnp .dpnp_utils import *
51
51
from dpnp .fft import *
52
52
from dpnp .linalg import *
53
53
from dpnp .random import *
59
59
"check_supported_arrays_type" ,
60
60
"convert_single_elem_array_to_scalar" ,
61
61
"default_float_type" ,
62
- "dpnp_queue_initialize" ,
63
62
"from_dlpack" ,
64
63
"get_dpnp_descriptor" ,
65
64
"get_include" ,
101
100
from dpnp .dpnp_iface_trigonometric import *
102
101
from dpnp .dpnp_iface_trigonometric import __all__ as __all__trigonometric
103
102
103
+ # pylint: disable=no-name-in-module
104
+ from .dpnp_utils import (
105
+ dpnp_descriptor ,
106
+ map_dtype_to_device ,
107
+ use_origin_backend ,
108
+ )
109
+
104
110
__all__ += __all__arraycreation
105
111
__all__ += __all__bitwise
106
112
__all__ += __all__counting
@@ -132,27 +138,44 @@ def array_equal(a1, a2, equal_nan=False):
132
138
133
139
"""
134
140
135
- return numpy .array_equal (a1 , a2 )
141
+ return numpy .array_equal (a1 , a2 , equal_nan = equal_nan )
136
142
137
143
138
- def asnumpy (input , order = "C" ):
144
+ def asnumpy (a , order = "C" ):
139
145
"""
140
146
Returns the NumPy array with input data.
141
147
148
+ Parameters
149
+ ----------
150
+ a : {array_like}
151
+ Arbitrary object that can be converted to :obj:`numpy.ndarray`.
152
+ order : {'C', 'F', 'A', 'K'}
153
+ The desired memory layout of the converted array.
154
+ When `order` is ``A``, it uses ``F`` if `a` is column-major and uses
155
+ ``C`` otherwise. And when `order` is ``K``, it keeps strides as closely
156
+ as possible.
157
+
158
+ Returns
159
+ -------
160
+ out : numpy.ndarray
161
+ NumPy interpretation of input array `a`.
162
+
142
163
Notes
143
164
-----
144
165
This function works exactly the same as :obj:`numpy.asarray`.
145
166
146
167
"""
147
- if isinstance (input , dpnp_array ):
148
- return input .asnumpy ()
149
168
150
- if isinstance (input , dpt .usm_ndarray ):
151
- return dpt .asnumpy (input )
169
+ if isinstance (a , dpnp_array ):
170
+ return a .asnumpy ()
171
+
172
+ if isinstance (a , dpt .usm_ndarray ):
173
+ return dpt .asnumpy (a )
152
174
153
- return numpy .asarray (input , order = order )
175
+ return numpy .asarray (a , order = order )
154
176
155
177
178
+ # pylint: disable=redefined-outer-name
156
179
def astype (x1 , dtype , order = "K" , casting = "unsafe" , copy = True ):
157
180
"""
158
181
Copy the array with data type casting.
@@ -165,28 +188,32 @@ def astype(x1, dtype, order="K", casting="unsafe", copy=True):
165
188
Target data type.
166
189
order : {'C', 'F', 'A', 'K'}
167
190
Row-major (C-style) or column-major (Fortran-style) order.
168
- When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
169
- And when ``order`` is 'K', it keeps strides as closely as possible.
191
+ When `order` is ``A``, it uses ``F`` if `a` is column-major and uses
192
+ ``C`` otherwise. And when `order` is ``K``, it keeps strides as closely
193
+ as possible.
170
194
copy : bool
171
- If it is False and no cast happens, then this method returns the array itself.
172
- Otherwise, a copy is returned.
195
+ If it is `` False`` and no cast happens, then this method returns
196
+ the array itself. Otherwise, a copy is returned.
173
197
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
174
- Controls what kind of data casting may occur. Defaults to 'unsafe' for backwards compatibility.
175
- 'no' means the data types should not be cast at all.
176
- 'equiv' means only byte-order changes are allowed.
177
- 'safe' means only casts which can preserve values are allowed.
178
- 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed.
179
- 'unsafe' means any data conversions may be done.
198
+ Controls what kind of data casting may occur. Defaults to ``unsafe``
199
+ for backwards compatibility.
200
+ - 'no' means the data types should not be cast at all.
201
+ - 'equiv' means only byte-order changes are allowed.
202
+ - 'safe' means only casts which can preserve values are allowed.
203
+ - 'same_kind' means only safe casts or casts within a kind, like
204
+ float64 to float32, are allowed.
205
+ - 'unsafe' means any data conversions may be done.
180
206
copy : bool, optional
181
- By default, astype always returns a newly allocated array. If this is set to false, and the dtype,
182
- order, and subok requirements are satisfied, the input array is returned instead of a copy.
207
+ By default, astype always returns a newly allocated array. If this
208
+ is set to ``False``, and the dtype, order, and subok requirements
209
+ are satisfied, the input array is returned instead of a copy.
183
210
184
211
Returns
185
212
-------
186
213
arr_t : dpnp.ndarray
187
- Unless `copy` is ``False`` and the other conditions for returning the input array
188
- are satisfied, `arr_t` is a new array of the same shape as the input array,
189
- with dtype, order given by dtype, order.
214
+ Unless `copy` is ``False`` and the other conditions for returning
215
+ the input array are satisfied, `arr_t` is a new array of the same shape
216
+ as the input array, with dtype, order given by dtype, order.
190
217
191
218
"""
192
219
@@ -238,16 +265,18 @@ def check_supported_arrays_type(*arrays, scalar_type=False, all_scalars=False):
238
265
if is_supported_array_type (a ):
239
266
any_is_array = True
240
267
continue
241
- elif scalar_type and dpnp .isscalar (a ):
268
+
269
+ if scalar_type and dpnp .isscalar (a ):
242
270
continue
243
271
244
272
raise TypeError (
245
- "An array must be any of supported type, but got {}" . format ( type (a ))
273
+ f "An array must be any of supported type, but got { type (a )} "
246
274
)
247
275
248
276
if len (arrays ) > 1 and not (all_scalars or any_is_array ):
249
277
raise TypeError (
250
- "At least one input must be of supported array type, but got all scalars."
278
+ "At least one input must be of supported array type, "
279
+ "but got all scalars."
251
280
)
252
281
return True
253
282
@@ -263,21 +292,24 @@ def convert_single_elem_array_to_scalar(obj, keepdims=False):
263
292
264
293
def default_float_type (device = None , sycl_queue = None ):
265
294
"""
266
- Return a floating type used by default in DPNP depending on device capabilities.
295
+ Return a floating type used by default in DPNP depending on device
296
+ capabilities.
267
297
268
298
Parameters
269
299
----------
270
300
device : {None, string, SyclDevice, SyclQueue}, optional
271
- An array API concept of device where an array of default floating type might be created.
272
- The `device` can be ``None`` (the default), an OneAPI filter selector string,
273
- an instance of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL device,
274
- an instance of :class:`dpctl.SyclQueue`, or a `Device` object returned by
301
+ An array API concept of device where an array of default floating type
302
+ might be created. The `device` can be ``None`` (the default), an OneAPI
303
+ filter selector string, an instance of :class:`dpctl.SyclDevice`
304
+ corresponding to a non-partitioned SYCL device, an instance of
305
+ :class:`dpctl.SyclQueue`, or a `Device` object returned by
275
306
:obj:`dpnp.dpnp_array.dpnp_array.device` property.
276
307
The value ``None`` is interpreted as to use a default device.
277
308
sycl_queue : {None, SyclQueue}, optional
278
- A SYCL queue which might be used to create an array of default floating type.
279
- The `sycl_queue` can be ``None`` (the default), which is interpreted as
280
- to get the SYCL queue from `device` keyword if present or to use a default queue.
309
+ A SYCL queue which might be used to create an array of default floating
310
+ type. The `sycl_queue` can be ``None`` (the default), which is
311
+ interpreted as to get the SYCL queue from `device` keyword if present
312
+ or to use a default queue.
281
313
282
314
Returns
283
315
-------
@@ -336,19 +368,16 @@ def get_dpnp_descriptor(
336
368
2. We can not handle with input data object
337
369
"""
338
370
339
- # TODO need to allow "import dpnp" with no build procedure
340
- # if no_modules_load_doc_build();
341
- # return False
342
-
343
371
if use_origin_backend ():
344
372
return False
345
373
346
- # It's required to keep track of input object if a non-strided copy is going to be created.
347
- # Thus there will be an extra descriptor allocated to refer on original input.
374
+ # It's required to keep track of input object if a non-strided copy is
375
+ # going to be created. Thus there will be an extra descriptor allocated
376
+ # to refer on original input.
348
377
orig_desc = None
349
378
350
379
# If input object is a scalar, it means it was allocated on host memory.
351
- # We need to copy it to USM memory according to compute follows data paradigm .
380
+ # We need to copy it to USM memory according to compute follows data.
352
381
if isscalar (ext_obj ):
353
382
ext_obj = array (
354
383
ext_obj ,
@@ -362,7 +391,8 @@ def get_dpnp_descriptor(
362
391
# if function get implementation for strides case
363
392
# then this behavior can be disabled with setting "copy_when_strides"
364
393
if copy_when_strides and getattr (ext_obj , "strides" , None ) is not None :
365
- # TODO: replace this workaround when usm_ndarray will provide such functionality
394
+ # TODO: replace this workaround when usm_ndarray will provide
395
+ # such functionality
366
396
shape_offsets = tuple (
367
397
numpy .prod (ext_obj .shape [i + 1 :], dtype = numpy .int64 )
368
398
for i in range (ext_obj .ndim )
@@ -382,7 +412,8 @@ def get_dpnp_descriptor(
382
412
# while dpnp functions are based on DPNP_QUEUE
383
413
# we need to create a copy on device associated with DPNP_QUEUE
384
414
# if function get implementation for different queue
385
- # then this behavior can be disabled with setting "copy_when_nondefault_queue"
415
+ # then this behavior can be disabled with setting
416
+ # "copy_when_nondefault_queue"
386
417
queue = getattr (ext_obj , "sycl_queue" , None )
387
418
if queue is not None and copy_when_nondefault_queue :
388
419
default_queue = dpctl .SyclQueue ()
@@ -393,7 +424,7 @@ def get_dpnp_descriptor(
393
424
ext_obj = array (ext_obj , sycl_queue = default_queue )
394
425
395
426
dpnp_desc = dpnp_descriptor (ext_obj , orig_desc )
396
- if dpnp_desc .is_valid :
427
+ if dpnp_desc .is_valid : # pylint: disable=using-constant-test
397
428
return dpnp_desc
398
429
399
430
return False
@@ -418,8 +449,8 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
418
449
419
450
If both arguments 'device' and 'sycl_queue' have default value ``None``
420
451
and 'obj' has `sycl_queue` attribute, it assumes that Compute Follows Data
421
- approach has to be applied and so the resulting SYCL queue will be normalized
422
- based on the queue value from 'obj'.
452
+ approach has to be applied and so the resulting SYCL queue will be
453
+ normalized based on the queue value from 'obj'.
423
454
424
455
Parameters
425
456
----------
@@ -443,15 +474,11 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
443
474
Returns
444
475
-------
445
476
sycl_queue: dpctl.SyclQueue
446
- A :class:`dpctl.SyclQueue` object normalized by `normalize_queue_device` call
447
- of `dpctl.tensor` module invoked with 'device' and 'sycl_queue' values.
448
- If both incoming 'device' and 'sycl_queue' are None and 'obj' has `sycl_queue` attribute,
449
- the normalization will be performed for 'obj.sycl_queue' value.
450
-
451
- Raises
452
- ------
453
- TypeError
454
- If argument is not of the expected type, or keywords imply incompatible queues.
477
+ A :class:`dpctl.SyclQueue` object normalized by
478
+ `normalize_queue_device` call of `dpctl.tensor` module invoked with
479
+ `device` and `sycl_queue` values. If both incoming `device` and
480
+ `sycl_queue` are ``None`` and `obj` has `sycl_queue` attribute,
481
+ the normalization will be performed for `obj.sycl_queue` value.
455
482
456
483
"""
457
484
@@ -463,9 +490,7 @@ def get_normalized_queue_device(obj=None, device=None, sycl_queue=None):
463
490
):
464
491
sycl_queue = obj .sycl_queue
465
492
466
- return dpt ._device .normalize_queue_device (
467
- sycl_queue = sycl_queue , device = device
468
- )
493
+ return normalize_queue_device (sycl_queue = sycl_queue , device = device )
469
494
470
495
471
496
def get_result_array (a , out = None , casting = "safe" ):
@@ -494,21 +519,21 @@ def get_result_array(a, out=None, casting="safe"):
494
519
495
520
if out is None :
496
521
return a
497
- else :
498
- if a is out :
499
- return out
500
- else :
501
- dpnp .check_supported_arrays_type (out )
502
- if out .shape != a .shape :
503
- raise ValueError (
504
- f"Output array of shape { a .shape } is needed, got { out .shape } ."
505
- )
506
- elif isinstance (out , dpt .usm_ndarray ):
507
- out = dpnp_array ._create_from_usm_ndarray (out )
508
522
509
- dpnp .copyto (out , a , casting = casting )
523
+ if a is out :
524
+ return out
525
+
526
+ dpnp .check_supported_arrays_type (out )
527
+ if out .shape != a .shape :
528
+ raise ValueError (
529
+ f"Output array of shape { a .shape } is needed, got { out .shape } ."
530
+ )
531
+
532
+ if isinstance (out , dpt .usm_ndarray ):
533
+ out = dpnp_array ._create_from_usm_ndarray (out )
510
534
511
- return out
535
+ dpnp .copyto (out , a , casting = casting )
536
+ return out
512
537
513
538
514
539
def get_usm_ndarray (a ):
@@ -538,7 +563,7 @@ def get_usm_ndarray(a):
538
563
if isinstance (a , dpt .usm_ndarray ):
539
564
return a
540
565
raise TypeError (
541
- "An array must be any of supported type, but got {}" . format ( type (a ))
566
+ f "An array must be any of supported type, but got { type (a )} "
542
567
)
543
568
544
569
0 commit comments