-
Notifications
You must be signed in to change notification settings - Fork 33
/
arrayobj.py
1131 lines (992 loc) · 39.8 KB
/
arrayobj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
import operator
import dpnp
from numba import errors, types
from numba.core.imputils import impl_ret_borrowed, lower_builtin
from numba.core.types import scalars
from numba.core.types.containers import UniTuple
from numba.core.typing.npydecl import parse_dtype as _ty_parse_dtype
from numba.core.typing.npydecl import parse_shape as _ty_parse_shape
from numba.extending import overload, overload_attribute
from numba.np.arrayobj import _getitem_array_generic as np_getitem_array_generic
from numba.np.arrayobj import make_array
from numba.np.numpy_support import is_nonelike
from numba_dpex.core.kernel_interface.arrayobj import (
_getitem_array_generic as kernel_getitem_array_generic,
)
from numba_dpex.core.types import DpnpNdArray
from numba_dpex.kernel_api_impl.spirv.target import SPIRVTargetContext
from ._intrinsic import (
impl_dpnp_empty,
impl_dpnp_empty_like,
impl_dpnp_full,
impl_dpnp_full_like,
impl_dpnp_ones,
impl_dpnp_ones_like,
impl_dpnp_zeros,
impl_dpnp_zeros_like,
ol_dpnp_nd_array_sycl_queue,
)
# can't import name because of the circular import
DPEX_TARGET_NAME = "dpex"
# =========================================================================
# Helps to parse dpnp constructor arguments
# =========================================================================
def _parse_dim(x1):
if hasattr(x1, "ndim") and x1.ndim:
return x1.ndim
elif isinstance(x1, scalars.Integer):
r = 1
return r
elif isinstance(x1, UniTuple):
r = len(x1)
return r
else:
return 0
def _parse_dtype(dtype):
"""Resolve dtype parameter.
Resolves the dtype parameter based on the given value
or the dtype of the given array.
Args:
dtype (numba.core.types.functions.NumberClass): Numba type
class for number classes (e.g. "np.float64").
data (numba.core.types.npytypes.Array, optional): Numba type
class for nd-arrays. Defaults to None.
Returns:
numba.core.types.functions.NumberClass: Resolved numba type
class for number classes.
"""
_dtype = None
if not is_nonelike(dtype):
_dtype = _ty_parse_dtype(dtype)
return _dtype
def _parse_layout(layout):
if isinstance(layout, types.StringLiteral):
layout_type_str = layout.literal_value
if layout_type_str not in ["C", "F", "A"]:
msg = f"Invalid layout specified: '{layout_type_str}'"
raise errors.NumbaValueError(msg)
return layout_type_str
elif isinstance(layout, str):
if layout not in ["C", "F", "A"]:
msg = f"Invalid layout specified: '{layout}'"
raise errors.NumbaValueError(msg)
return layout
else:
raise TypeError(
"The parameter 'layout' is neither of "
+ "'str' nor 'types.StringLiteral'"
)
def _parse_usm_type(usm_type):
"""Parse usm_type parameter.
Resolves the usm_type parameter based on the type
of the parameter.
Args:
usm_type (str, numba.core.types.misc.StringLiteral):
The type class for the string to specify the usm_type.
Raises:
errors.NumbaValueError: If an invalid usm_type is specified.
TypeError: If the parameter is neither a 'str'
nor a 'types.StringLiteral'.
Returns:
str: The stringized usm_type.
"""
if isinstance(usm_type, types.StringLiteral):
usm_type_str = usm_type.literal_value
if usm_type_str not in ["shared", "device", "host"]:
msg = f"Invalid usm_type specified: '{usm_type_str}'"
raise errors.NumbaValueError(msg)
return usm_type_str
elif isinstance(usm_type, str):
if usm_type not in ["shared", "device", "host"]:
msg = f"Invalid usm_type specified: '{usm_type}'"
raise errors.NumbaValueError(msg)
return usm_type
else:
raise TypeError(
"The parameter 'usm_type' is neither of "
+ "'str' nor 'types.StringLiteral'"
)
def _parse_device_filter_string(device):
"""Parse the device type parameter.
Returns the device filter string,
if it is a string literal.
Args:
device (str, numba.core.types.misc.StringLiteral):
The type class for the string to specify the device.
Raises:
TypeError: If the parameter is neither a 'str'
nor a 'types.StringLiteral'.
Returns:
str: The stringized device.
"""
if isinstance(device, types.StringLiteral):
device_filter_str = device.literal_value
return device_filter_str
elif isinstance(device, str):
return device
elif device is None or isinstance(device, types.NoneType):
return None
else:
raise TypeError(
"The parameter 'device' is neither of "
+ "'str', 'types.StringLiteral' nor 'None'"
)
# =========================================================================
# Dpnp array constructor overloads
# =========================================================================
@overload(dpnp.empty, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_empty(
shape,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None,
):
"""Implementation of an overload to support dpnp.empty() inside
a dpjit function.
Args:
shape (numba.core.types.containers.UniTuple or
numba.core.types.scalars.IntegerLiteral): Dimensions
of the array to be created.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If rank of the ndarray couldn't be inferred.
errors.TypingError: If couldn't parse input types to dpnp.empty().
Returns:
function: Local function `impl_dpnp_empty()`.
"""
_ndim = _ty_parse_shape(shape)
_dtype = _parse_dtype(dtype)
_layout = _parse_layout(order)
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
if _ndim:
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=sycl_queue,
)
if ret_ty:
def impl(
shape,
dtype=None,
order="C",
# like=None, # noqa: E800 see issue https://github.com/IntelPython/numba-dpex/issues/998
device=None,
usm_type="device",
sycl_queue=None,
):
return impl_dpnp_empty(
shape,
_dtype,
order,
# like, # noqa: E800 see issue https://github.com/IntelPython/numba-dpex/issues/998
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.empty({shape}, {dtype}, ...)."
)
else:
raise errors.TypingError("Could not infer the rank of the ndarray.")
@overload(dpnp.zeros, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_zeros(
shape,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None,
):
"""Implementation of an overload to support dpnp.zeros() inside
a dpjit function.
Args:
shape (numba.core.types.containers.UniTuple or
numba.core.types.scalars.IntegerLiteral): Dimensions
of the array to be created.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If rank of the ndarray couldn't be inferred.
errors.TypingError: If couldn't parse input types to dpnp.zeros().
Returns:
function: Local function `impl_dpnp_zeros()`.
"""
_ndim = _ty_parse_shape(shape)
_dtype = _parse_dtype(dtype)
_layout = _parse_layout(order)
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
if _ndim:
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=sycl_queue,
)
if ret_ty:
def impl(
shape,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None,
):
return impl_dpnp_zeros(
shape,
_dtype,
order,
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.zeros({shape}, {dtype}, ...)."
)
else:
raise errors.TypingError("Could not infer the rank of the ndarray.")
@overload(dpnp.ones, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_ones(
shape,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None,
):
"""Implementation of an overload to support dpnp.ones() inside
a dpjit function.
Args:
shape (numba.core.types.containers.UniTuple or
numba.core.types.scalars.IntegerLiteral): Dimensions
of the array to be created.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If rank of the ndarray couldn't be inferred.
errors.TypingError: If couldn't parse input types to dpnp.ones().
Returns:
function: Local function `impl_dpnp_ones()`.
"""
_ndim = _ty_parse_shape(shape)
_dtype = _parse_dtype(dtype)
_layout = _parse_layout(order)
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
if _ndim:
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=sycl_queue,
)
if ret_ty:
def impl(
shape,
dtype=None,
order="C",
device=None,
usm_type="device",
sycl_queue=None,
):
return impl_dpnp_ones(
shape,
_dtype,
order,
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.ones({shape}, {dtype}, ...)."
)
else:
raise errors.TypingError("Could not infer the rank of the ndarray.")
@overload(dpnp.full, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_full(
shape,
fill_value,
dtype=None,
order="C",
like=None,
device=None,
usm_type=None,
sycl_queue=None,
):
"""Implementation of an overload to support dpnp.full() inside
a dpjit function.
Args:
shape (numba.core.types.containers.UniTuple or
numba.core.types.scalars.IntegerLiteral): Dimensions
of the array to be created.
fill_value (numba.core.types.scalars): One of the
numba.core.types.scalar types for the value to
be filled.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
like (numba.core.types.npytypes.Array, optional): A type for
reference object to allow the creation of arrays which are not
`NumPy` arrays. If an array-like passed in as `like` supports the
`__array_function__` protocol, the result will be defined by it.
In this case, it ensures the creation of an array object
compatible with that passed in via this argument.
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If rank of the ndarray couldn't be inferred.
errors.TypingError: If couldn't parse input types to dpnp.full().
Returns:
function: Local function `impl_dpnp_full()`.
"""
_ndim = _ty_parse_shape(shape)
_dtype = _parse_dtype(dtype) if dtype is not None else fill_value
_layout = _parse_layout(order)
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
if _ndim:
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=sycl_queue,
)
if ret_ty:
def impl(
shape,
fill_value,
dtype=None,
order="C",
like=None,
device=None,
usm_type=None,
sycl_queue=None,
):
return impl_dpnp_full(
shape,
fill_value,
_dtype,
order,
like,
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.full({shape}, {fill_value}, {dtype}, ...)."
)
else:
raise errors.TypingError("Could not infer the rank of the ndarray.")
@overload(dpnp.empty_like, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_empty_like(
x1,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
"""Creates `usm_ndarray` from uninitialized USM allocation.
This is an overloaded function implementation for dpnp.empty_like().
Args:
x1 (numba.core.types.npytypes.Array): Input array from which to
derive the output array shape.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
subok ('numba.core.types.scalars.BooleanLiteral', optional): A
boolean literal type for the `subok` parameter defined in
NumPy. If True, then the newly created array will use the
sub-class type of prototype, otherwise it will be a
base-class array. Defaults to False.
shape (numba.core.types.containers.UniTuple, optional): The shape
to override the shape of the given array. Not supported.
Default: `None`
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If couldn't parse input types to dpnp.empty_like().
errors.TypingError: If shape is provided.
errors.TypingError: If `x1` is not an instance of DpnpNdArray
Returns:
function: Local function `impl_dpnp_empty_like()`.
"""
if shape:
raise errors.TypingError(
"The parameter shape is not supported "
+ "inside overloaded dpnp.empty_like() function."
)
if not isinstance(x1, DpnpNdArray):
raise errors.TypingError(
"Only objects of dpnp.dpnp_array type are supported as "
"input array ``x1``."
)
_ndim = _parse_dim(x1)
_dtype = x1.dtype if isinstance(x1, types.Array) else _parse_dtype(dtype)
_layout = x1.layout if order is None else order
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
# If a sycl_queue or device argument was not explicitly provided get the
# queue from the array (x1) argument.
_queue = sycl_queue
if _queue is None and _device is None:
_queue = x1.queue
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=_queue,
)
if ret_ty:
def impl(
x1,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
return impl_dpnp_empty_like(
x1,
_dtype,
_layout,
subok,
shape,
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.empty_like({x1}, {dtype}, ...)."
)
@overload(dpnp.zeros_like, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_zeros_like(
x1,
dtype=None,
order="C",
subok=None,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
"""Creates `usm_ndarray` from USM allocation initialized with zeros.
This is an overloaded function implementation for dpnp.zeros_like().
Args:
x1 (numba.core.types.npytypes.Array): Input array from which to
derive the output array shape.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
subok ('numba.core.types.scalars.BooleanLiteral', optional): A
boolean literal type for the `subok` parameter defined in
NumPy. If True, then the newly created array will use the
sub-class type of prototype, otherwise it will be a
base-class array. Defaults to False.
shape (numba.core.types.containers.UniTuple, optional): The shape
to override the shape of the given array. Not supported.
Default: `None`
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If couldn't parse input types to dpnp.zeros_like().
errors.TypingError: If shape is provided.
errors.TypingError: If `x1` is not an instance of DpnpNdArray
Returns:
function: Local function `impl_dpnp_zeros_like()`.
"""
if shape:
raise errors.TypingError(
"The parameter shape is not supported "
+ "inside overloaded dpnp.zeros_like() function."
)
if not isinstance(x1, DpnpNdArray):
raise errors.TypingError(
"Only objects of dpnp.dpnp_array type are supported as "
"input array ``x1``."
)
_ndim = _parse_dim(x1)
_dtype = x1.dtype if isinstance(x1, types.Array) else _parse_dtype(dtype)
_layout = x1.layout if order is None else order
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
# If a sycl_queue or device argument was not explicitly provided get the
# queue from the array (x1) argument.
_queue = sycl_queue
if _queue is None and _device is None:
_queue = x1.queue
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=_queue,
)
if ret_ty:
def impl(
x1,
dtype=None,
order="C",
subok=None,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
return impl_dpnp_zeros_like(
x1,
_dtype,
_layout,
subok,
shape,
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.empty_like({x1}, {dtype}, ...)."
)
@overload(dpnp.ones_like, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_ones_like(
x1,
dtype=None,
order="C",
subok=None,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
"""Creates `usm_ndarray` from USM allocation initialized with ones.
This is an overloaded function implementation for dpnp.ones_like().
Args:
x1 (numba.core.types.npytypes.Array): Input array from which to
derive the output array shape.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
subok ('numba.core.types.scalars.BooleanLiteral', optional): A
boolean literal type for the `subok` parameter defined in
NumPy. If True, then the newly created array will use the
sub-class type of prototype, otherwise it will be a
base-class array. Defaults to False.
shape (numba.core.types.containers.UniTuple, optional): The shape
to override the shape of the given array. Not supported.
Default: `None`
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If couldn't parse input types to dpnp.ones_like().
errors.TypingError: If shape is provided.
errors.TypingError: If `x1` is not an instance of DpnpNdArray
Returns:
function: Local function `impl_dpnp_ones_like()`.
"""
if shape:
raise errors.TypingError(
"The parameter shape is not supported "
+ "inside overloaded dpnp.ones_like() function."
)
if not isinstance(x1, DpnpNdArray):
raise errors.TypingError(
"Only objects of dpnp.dpnp_array type are supported as "
"input array ``x1``."
)
_ndim = _parse_dim(x1)
_dtype = x1.dtype if isinstance(x1, types.Array) else _parse_dtype(dtype)
_layout = x1.layout if order is None else order
_usm_type = _parse_usm_type(usm_type) if usm_type else "device"
_device = _parse_device_filter_string(device) if device else None
# If a sycl_queue or device argument was not explicitly provided get the
# queue from the array (x1) argument.
_queue = sycl_queue
if _queue is None and _device is None:
_queue = x1.queue
ret_ty = DpnpNdArray(
ndim=_ndim,
layout=_layout,
dtype=_dtype,
usm_type=_usm_type,
device=_device,
queue=_queue,
)
if ret_ty:
def impl(
x1,
dtype=None,
order="C",
subok=None,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
return impl_dpnp_ones_like(
x1,
_dtype,
_layout,
subok,
shape,
_device,
_usm_type,
sycl_queue,
ret_ty,
)
return impl
else:
raise errors.TypingError(
"Cannot parse input types to "
+ f"function dpnp.empty_like({x1}, {dtype}, ...)."
)
@overload(dpnp.full_like, prefer_literal=True, target=DPEX_TARGET_NAME)
def ol_dpnp_full_like(
x1,
fill_value,
dtype=None,
order="C",
subok=None,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
):
"""Creates `usm_ndarray` from USM allocation initialized with values
specified by the `fill_value`.
This is an overloaded function implementation for dpnp.full_like().
Args:
x1 (numba.core.types.npytypes.Array): Input array from which to
derive the output array shape.
fill_value (numba.core.types.scalars): One of the
numba.core.types.scalar types for the value to
be filled.
dtype (numba.core.types.functions.NumberClass, optional):
Data type of the array. Can be typestring, a `numpy.dtype`
object, `numpy` char string, or a numpy scalar type.
Default: None.
order (str, optional): memory layout for the array "C" or "F".
Default: "C".
subok ('numba.core.types.scalars.BooleanLiteral', optional): A
boolean literal type for the `subok` parameter defined in
NumPy. If True, then the newly created array will use the
sub-class type of prototype, otherwise it will be a
base-class array. Defaults to False.
shape (numba.core.types.containers.UniTuple, optional): The shape
to override the shape of the given array. Not supported.
Default: `None`
device (numba.core.types.misc.StringLiteral, optional): array API
concept of device where the output array is created. `device`
can be `None`, a oneAPI filter selector string, an instance of
:class:`dpctl.SyclDevice` corresponding to a non-partitioned
SYCL device, an instance of :class:`dpctl.SyclQueue`, or a
`Device` object returnedby`dpctl.tensor.usm_array.device`.
Default: `None`.
usm_type (numba.core.types.misc.StringLiteral or str, optional):
The type of SYCL USM allocation for the output array.
Allowed values are "device"|"shared"|"host".
Default: `"device"`.
sycl_queue (:class:`numba_dpex.core.types.dpctl_types.DpctlSyclQueue`,
optional): The SYCL queue to use for output array allocation and
copying. sycl_queue and device are exclusive keywords, i.e. use
one or another. If both are specified, a TypeError is raised. If
both are None, a cached queue targeting default-selected device
is used for allocation and copying. Default: `None`.
Raises:
errors.TypingError: If both `device` and `sycl_queue` are provided.
errors.TypingError: If couldn't parse input types to dpnp.full_like().
errors.TypingError: If shape is provided.