41
41
42
42
43
43
import collections .abc
44
- import numpy
45
44
46
45
from dpnp .dpnp_algo import *
47
46
from dpnp .dparray import dparray
48
47
from dpnp .dpnp_utils import *
49
- import dpnp
50
48
from dpnp .dpnp_iface_arraycreation import array
51
49
50
+ import dpnp
51
+ import numpy
52
+
52
53
53
54
__all__ = [
54
55
"asfarray" ,
67
68
]
68
69
69
70
70
- def asfarray (a , dtype = numpy .float64 ):
71
+ def asfarray (x1 , dtype = numpy .float64 ):
71
72
"""
72
73
Return an array converted to a float type.
73
74
@@ -79,18 +80,19 @@ def asfarray(a, dtype=numpy.float64):
79
80
80
81
"""
81
82
82
- if not use_origin_backend (a ):
83
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
84
+ if x1_desc :
83
85
# behavior of original function: int types replaced with float64
84
86
if numpy .issubdtype (dtype , numpy .integer ):
85
87
dtype = numpy .float64
86
88
87
89
# if type is the same then same object should be returned
88
- if isinstance ( a , dpnp . ndarray ) and a .dtype == dtype :
89
- return a
90
+ if x1_desc .dtype == dtype :
91
+ return x1
90
92
91
- return array (a , dtype = dtype )
93
+ return array (x1 , dtype = dtype )
92
94
93
- return call_origin (numpy .asfarray , a , dtype )
95
+ return call_origin (numpy .asfarray , x1 , dtype )
94
96
95
97
96
98
def atleast_1d (* arys ):
@@ -188,32 +190,31 @@ def copyto(dst, src, casting='same_kind', where=True):
188
190
Input array data types are limited by supported DPNP :ref:`Data types`.
189
191
190
192
"""
191
- if not use_origin_backend (dst ):
192
- if not isinstance (dst , dparray ):
193
- pass
194
- elif not isinstance (src , dparray ):
195
- pass
196
- elif casting != 'same_kind' :
193
+
194
+ dst_desc = dpnp .get_dpnp_descriptor (dst )
195
+ src_desc = dpnp .get_dpnp_descriptor (src )
196
+ if dst_desc and src_desc :
197
+ if casting != 'same_kind' :
197
198
pass
198
- elif (dst .dtype == dpnp .bool and # due to 'same_kind' casting
199
- src .dtype in [dpnp .int32 , dpnp .int64 , dpnp .float32 , dpnp .float64 , dpnp .complex128 ]):
199
+ elif (dst_desc .dtype == dpnp .bool and # due to 'same_kind' casting
200
+ src_desc .dtype in [dpnp .int32 , dpnp .int64 , dpnp .float32 , dpnp .float64 , dpnp .complex128 ]):
200
201
pass
201
- elif (dst .dtype in [dpnp .int32 , dpnp .int64 ] and # due to 'same_kind' casting
202
- src .dtype in [dpnp .float32 , dpnp .float64 , dpnp .complex128 ]):
202
+ elif (dst_desc .dtype in [dpnp .int32 , dpnp .int64 ] and # due to 'same_kind' casting
203
+ src_desc .dtype in [dpnp .float32 , dpnp .float64 , dpnp .complex128 ]):
203
204
pass
204
- elif dst .dtype in [dpnp .float32 , dpnp .float64 ] and src .dtype == dpnp .complex128 : # due to 'same_kind' casting
205
+ elif dst_desc .dtype in [dpnp .float32 , dpnp .float64 ] and src_desc .dtype == dpnp .complex128 : # due to 'same_kind' casting
205
206
pass
206
207
elif where is not True :
207
208
pass
208
- elif dst .shape != src .shape :
209
+ elif dst_desc .shape != src_desc .shape :
209
210
pass
210
211
else :
211
- return dpnp_copyto (dst , src , where = where )
212
+ return dpnp_copyto (dst_desc , src_desc , where = where )
212
213
213
214
return call_origin (numpy .copyto , dst , src , casting , where )
214
215
215
216
216
- def expand_dims (a , axis ):
217
+ def expand_dims (x1 , axis ):
217
218
"""
218
219
Expand the shape of an array.
219
220
@@ -271,13 +272,11 @@ def expand_dims(a, axis):
271
272
272
273
"""
273
274
274
- if not use_origin_backend (a ):
275
- if not isinstance (a , dpnp .ndarray ):
276
- pass
277
- else :
278
- return dpnp_expand_dims (a , axis )
275
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
276
+ if x1_desc :
277
+ return dpnp_expand_dims (x1 , axis )
279
278
280
- return call_origin (numpy .expand_dims , a , axis )
279
+ return call_origin (numpy .expand_dims , x1 , axis )
281
280
282
281
283
282
def moveaxis (x1 , source , destination ):
@@ -309,45 +308,33 @@ def moveaxis(x1, source, destination):
309
308
310
309
"""
311
310
312
- if (use_origin_backend (x1 )):
313
- return numpy .swapaxes (x1 , source , destination )
314
-
315
- if (not isinstance (x1 , dparray )):
316
- return numpy .swapaxes (x1 , source , destination )
317
-
318
- if not isinstance (source , collections .abc .Sequence ): # assume scalar
319
- source = (source ,)
320
-
321
- if not isinstance (destination , collections .abc .Sequence ): # assume scalar
322
- destination = (destination ,)
323
-
324
- source_norm = normalize_axis (source , x1 .ndim )
325
- destination_norm = normalize_axis (destination , x1 .ndim )
311
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
312
+ if x1_desc :
313
+ source_norm = normalize_axis (source , x1_desc .ndim )
314
+ destination_norm = normalize_axis (destination , x1_desc .ndim )
326
315
327
- if len (source_norm ) != len (destination_norm ):
328
- checker_throw_axis_error (
329
- "swapaxes" ,
330
- "source_norm.size() != destination_norm.size()" ,
331
- source_norm ,
332
- destination_norm )
316
+ if len (source_norm ) != len (destination_norm ):
317
+ pass
318
+ else :
319
+ # 'do nothing' pattern for transpose() with no elements in 'source'
320
+ input_permute = []
321
+ for i in range (x1_desc .ndim ):
322
+ if i not in source_norm :
323
+ input_permute .append (i )
333
324
334
- # 'do nothing' pattern for transpose() with no elements in 'source'
335
- input_permute = []
336
- for i in range (x1 .ndim ):
337
- if i not in source_norm :
338
- input_permute .append (i )
325
+ # insert moving axes into proper positions
326
+ for destination_id , source_id in sorted (zip (destination_norm , source_norm )):
327
+ # if destination_id in input_permute:
328
+ # pytest tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_3
329
+ # checker_throw_value_error("swapaxes", "source_id exists", source_id, input_permute)
330
+ input_permute .insert (destination_id , source_id )
339
331
340
- # insert moving axes into proper positions
341
- for destination_id , source_id in sorted (zip (destination_norm , source_norm )):
342
- # if destination_id in input_permute:
343
- # pytest tests/third_party/cupy/manipulation_tests/test_transpose.py::TestTranspose::test_moveaxis_invalid5_3
344
- # checker_throw_value_error("swapaxes", "source_id exists", source_id, input_permute)
345
- input_permute .insert (destination_id , source_id )
332
+ return transpose (x1_desc , axes = input_permute )
346
333
347
- return transpose ( x1 , axes = input_permute )
334
+ return call_origin ( numpy . moveaxis , x1 , source , destination )
348
335
349
336
350
- def ravel (a , order = 'C' ):
337
+ def ravel (x1 , order = 'C' ):
351
338
"""
352
339
Return a contiguous flattened array.
353
340
@@ -369,12 +356,11 @@ def ravel(a, order='C'):
369
356
370
357
"""
371
358
372
- if not use_origin_backend (a ) and isinstance (a , dparray ):
373
- return a .ravel (order = order )
359
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
360
+ if x1_desc :
361
+ return dpnp_flatten (x1 )
374
362
375
- result = numpy .rollaxis (dp2nd_array (a ), order = order )
376
-
377
- return nd2dp_array (result )
363
+ return call_origin (numpy .ravel , x1 , order = order )
378
364
379
365
380
366
def repeat (x1 , repeats , axis = None ):
@@ -403,23 +389,22 @@ def repeat(x1, repeats, axis=None):
403
389
404
390
"""
405
391
406
- if not use_origin_backend (x1 ):
407
- if not isinstance (x1 , dparray ):
408
- pass
409
- elif axis is not None and axis != 0 :
392
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
393
+ if x1_desc :
394
+ if axis is not None and axis != 0 :
410
395
pass
411
- elif x1 .ndim >= 2 :
396
+ elif x1_desc .ndim >= 2 :
412
397
pass
413
398
elif not dpnp .isscalar (repeats ) and len (repeats ) > 1 :
414
399
pass
415
400
else :
416
401
repeat_val = repeats if dpnp .isscalar (repeats ) else repeats [0 ]
417
- return dpnp_repeat (x1 , repeat_val , axis )
402
+ return dpnp_repeat (x1_desc , repeat_val , axis )
418
403
419
404
return call_origin (numpy .repeat , x1 , repeats , axis )
420
405
421
406
422
- def rollaxis (a , axis , start = 0 ):
407
+ def rollaxis (x1 , axis , start = 0 ):
423
408
"""
424
409
Roll the specified axis backwards, until it lies in a given position.
425
410
@@ -452,25 +437,22 @@ def rollaxis(a, axis, start=0):
452
437
453
438
"""
454
439
455
- if not use_origin_backend (a ):
456
- if not isinstance (a , dparray ):
440
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
441
+ if x1_desc :
442
+ if not isinstance (axis , int ):
457
443
pass
458
- elif not isinstance (axis , int ):
459
- pass
460
- elif start < - a .ndim or start > a .ndim :
444
+ elif start < - x1_desc .ndim or start > x1_desc .ndim :
461
445
pass
462
446
else :
463
- start_norm = start + a .ndim if start < 0 else start
447
+ start_norm = start + x1_desc .ndim if start < 0 else start
464
448
destination = start_norm - 1 if start_norm > axis else start_norm
465
449
466
- return dpnp .moveaxis (a , axis , destination )
467
-
468
- result = numpy .rollaxis (dp2nd_array (a ), axis , start )
450
+ return dpnp .moveaxis (x1_desc , axis , destination )
469
451
470
- return nd2dp_array ( result )
452
+ return call_origin ( numpy . rollaxis , x1 , axis , start )
471
453
472
454
473
- def squeeze (a , axis = None ):
455
+ def squeeze (x1 , axis = None ):
474
456
"""
475
457
Remove single-dimensional entries from the shape of an array.
476
458
@@ -504,13 +486,11 @@ def squeeze(a, axis=None):
504
486
505
487
"""
506
488
507
- if not use_origin_backend (a ):
508
- if not isinstance (a , dpnp .ndarray ):
509
- pass
510
- else :
511
- return dpnp_squeeze (a , axis )
489
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
490
+ if x1_desc :
491
+ return dpnp_squeeze (x1 , axis )
512
492
513
- return call_origin (numpy .squeeze , a , axis )
493
+ return call_origin (numpy .squeeze , x1 , axis )
514
494
515
495
516
496
def swapaxes (x1 , axis1 , axis2 ):
@@ -539,24 +519,21 @@ def swapaxes(x1, axis1, axis2):
539
519
540
520
"""
541
521
542
- if ( use_origin_backend ( x1 )):
543
- return numpy . swapaxes ( x1 , axis1 , axis2 )
544
-
545
- if ( not isinstance ( x1 , dparray )):
546
- return numpy . swapaxes ( x1 , axis1 , axis2 )
547
-
548
- if not ( axis1 < x1 . ndim ) :
549
- checker_throw_value_error ( "swapaxes" , "axis1" , axis1 , x1 . ndim - 1 )
550
-
551
- if not ( axis2 < x1 . ndim ):
552
- checker_throw_value_error ( "swapaxes" , " axis2" , axis2 , x1 . ndim - 1 )
522
+ x1_desc = dpnp . get_dpnp_descriptor ( x1 )
523
+ if x1_desc :
524
+ if axis1 >= x1_desc . ndim :
525
+ pass
526
+ elif axis2 >= x1_desc . ndim :
527
+ pass
528
+ else :
529
+ # 'do nothing' pattern for transpose( )
530
+ input_permute = [ i for i in range ( x1 . ndim )]
531
+ # swap axes
532
+ input_permute [ axis1 ], input_permute [ axis2 ] = input_permute [ axis2 ], input_permute [ axis1 ]
553
533
554
- # 'do nothing' pattern for transpose()
555
- input_permute = [i for i in range (x1 .ndim )]
556
- # swap axes
557
- input_permute [axis1 ], input_permute [axis2 ] = input_permute [axis2 ], input_permute [axis1 ]
534
+ return transpose (x1_desc , axes = input_permute )
558
535
559
- return transpose ( x1 , axes = input_permute )
536
+ return call_origin ( numpy . swapaxes , x1 , axis1 , axis2 )
560
537
561
538
562
539
def transpose (x1 , axes = None ):
@@ -593,17 +570,17 @@ def transpose(x1, axes=None):
593
570
594
571
"""
595
572
596
- if (use_origin_backend (x1 )):
597
- return numpy .transpose (x1 , axes = axes )
573
+ x1_desc = dpnp .get_dpnp_descriptor (x1 )
574
+ if x1_desc :
575
+ if axes is not None :
576
+ if not any (axes ):
577
+ """
578
+ pytest tests/third_party/cupy/manipulation_tests/test_transpose.py
579
+ """
580
+ axes = None
598
581
599
- if (not isinstance (x1 , dparray )):
600
- return numpy .transpose (x1 , axes = axes )
582
+ result = dpnp_transpose (x1_desc , axes )
601
583
602
- if (axes is not None ):
603
- if (not any (axes )):
604
- """
605
- pytest tests/third_party/cupy/manipulation_tests/test_transpose.py
606
- """
607
- axes = None
584
+ return result
608
585
609
- return dpnp_transpose ( x1 , axes = axes )
586
+ return call_origin ( numpy . transpose , x1 , axes = axes )
0 commit comments