-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path_copy_utils.py
More file actions
1108 lines (1007 loc) · 36.5 KB
/
_copy_utils.py
File metadata and controls
1108 lines (1007 loc) · 36.5 KB
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
# Data Parallel Control (dpctl)
#
# Copyright 2020-2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import builtins
import operator
from numbers import Integral
import numpy as np
import dpctl
import dpctl.memory as dpm
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti
import dpctl.utils
from dpctl.tensor._data_types import _get_dtype
from dpctl.tensor._device import normalize_queue_device
from dpctl.tensor._type_utils import _dtype_supported_by_device_impl
from ._numpy_helper import normalize_axis_index
__doc__ = (
"Implementation module for copy- and cast- operations on "
":class:`dpctl.tensor.usm_ndarray`."
)
int32_t_max = 1 + np.iinfo(np.int32).max
def _copy_to_numpy(ary):
if not isinstance(ary, dpt.usm_ndarray):
raise TypeError(f"Expected dpctl.tensor.usm_ndarray, got {type(ary)}")
nb = ary.usm_data.nbytes
q = ary.sycl_queue
hh = dpm.MemoryUSMHost(nb, queue=q)
h = np.ndarray(nb, dtype="u1", buffer=hh).view(ary.dtype)
itsz = ary.itemsize
strides_bytes = tuple(si * itsz for si in ary.strides)
offset = ary._element_offset * itsz
# ensure that content of ary.usm_data is final
q.wait()
hh.copy_from_device(ary.usm_data)
return np.ndarray(
ary.shape,
dtype=ary.dtype,
buffer=h,
strides=strides_bytes,
offset=offset,
)
def _copy_from_numpy(np_ary, usm_type="device", sycl_queue=None):
"Copies numpy array `np_ary` into a new usm_ndarray"
# This may perform a copy to meet stated requirements
Xnp = np.require(np_ary, requirements=["A", "E"])
alloc_q = normalize_queue_device(sycl_queue=sycl_queue, device=None)
dt = Xnp.dtype
if dt.char in "dD" and alloc_q.sycl_device.has_aspect_fp64 is False:
Xusm_dtype = (
dpt.dtype("float32") if dt.char == "d" else dpt.dtype("complex64")
)
else:
Xusm_dtype = dt
Xusm = dpt.empty(
Xnp.shape, dtype=Xusm_dtype, usm_type=usm_type, sycl_queue=sycl_queue
)
_copy_from_numpy_into(Xusm, Xnp)
return Xusm
def _copy_from_numpy_into(dst, np_ary):
"Copies `np_ary` into `dst` of type :class:`dpctl.tensor.usm_ndarray"
if not isinstance(np_ary, np.ndarray):
raise TypeError(f"Expected numpy.ndarray, got {type(np_ary)}")
if not isinstance(dst, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(dst)}")
if np_ary.flags["OWNDATA"]:
Xnp = np_ary
else:
# Determine base of input array
base = np_ary.base
while isinstance(base, np.ndarray):
base = base.base
if isinstance(base, dpm._memory._Memory):
# we must perform a copy, since subsequent
# _copy_numpy_ndarray_into_usm_ndarray is implemented using
# sycl::buffer, and using USM-pointers with sycl::buffer
# results is undefined behavior
Xnp = np_ary.copy()
else:
Xnp = np_ary
src_ary = np.broadcast_to(Xnp, dst.shape)
copy_q = dst.sycl_queue
if copy_q.sycl_device.has_aspect_fp64 is False:
src_ary_dt_c = src_ary.dtype.char
if src_ary_dt_c == "d":
src_ary = src_ary.astype(np.float32)
elif src_ary_dt_c == "D":
src_ary = src_ary.astype(np.complex64)
_manager = dpctl.utils.SequentialOrderManager[copy_q]
dep_ev = _manager.submitted_events
# synchronizing call
ti._copy_numpy_ndarray_into_usm_ndarray(
src=src_ary, dst=dst, sycl_queue=copy_q, depends=dep_ev
)
def from_numpy(np_ary, /, *, device=None, usm_type="device", sycl_queue=None):
"""
from_numpy(arg, device=None, usm_type="device", sycl_queue=None)
Creates :class:`dpctl.tensor.usm_ndarray` from instance of
:class:`numpy.ndarray`.
Args:
arg:
Input convertible to :class:`numpy.ndarray`
device (object): array API specification of device where the
output array is created. Device can be specified by
a filter selector string, an instance of
:class:`dpctl.SyclDevice`, an instance of
:class:`dpctl.SyclQueue`, or an instance of
:class:`dpctl.tensor.Device`. If the value is ``None``,
returned array is created on the default-selected device.
Default: ``None``
usm_type (str): The requested USM allocation type for the
output array. Recognized values are ``"device"``,
``"shared"``, or ``"host"``
sycl_queue (:class:`dpctl.SyclQueue`, optional):
A SYCL queue that determines output array allocation device
as well as execution placement of data movement operations.
The ``device`` and ``sycl_queue`` arguments
are equivalent. Only one of them should be specified. If both
are provided, they must be consistent and result in using the
same execution queue. Default: ``None``
The returned array has the same shape, and the same data type kind.
If the device does not support the data type of input array, a
closest support data type of the same kind may be returned, e.g.
input array of type ``float16`` may be upcast to ``float32`` if the
target device does not support 16-bit floating point type.
"""
q = normalize_queue_device(sycl_queue=sycl_queue, device=device)
return _copy_from_numpy(np_ary, usm_type=usm_type, sycl_queue=q)
def to_numpy(usm_ary, /):
"""
to_numpy(usm_ary)
Copies content of :class:`dpctl.tensor.usm_ndarray` instance ``usm_ary``
into :class:`numpy.ndarray` instance of the same shape and same data type.
Args:
usm_ary (usm_ndarray):
Input array
Returns:
:class:`numpy.ndarray`:
An instance of :class:`numpy.ndarray` populated with content of
``usm_ary``
"""
return _copy_to_numpy(usm_ary)
def asnumpy(usm_ary):
"""
asnumpy(usm_ary)
Copies content of :class:`dpctl.tensor.usm_ndarray` instance ``usm_ary``
into :class:`numpy.ndarray` instance of the same shape and same data
type.
Args:
usm_ary (usm_ndarray):
Input array
Returns:
:class:`numpy.ndarray`:
An instance of :class:`numpy.ndarray` populated with content
of ``usm_ary``
"""
return _copy_to_numpy(usm_ary)
class Dummy:
"""
Helper class with specified ``__sycl_usm_array_interface__`` attribute
"""
def __init__(self, iface):
self.__sycl_usm_array_interface__ = iface
def _copy_overlapping(dst, src):
"""Assumes src and dst have the same shape."""
q = normalize_queue_device(sycl_queue=dst.sycl_queue)
tmp = dpt.usm_ndarray(
src.shape,
dtype=src.dtype,
buffer="device",
order="C",
buffer_ctor_kwargs={"queue": q},
)
_manager = dpctl.utils.SequentialOrderManager[q]
dep_evs = _manager.submitted_events
hcp1, cp1 = ti._copy_usm_ndarray_into_usm_ndarray(
src=src, dst=tmp, sycl_queue=q, depends=dep_evs
)
_manager.add_event_pair(hcp1, cp1)
hcp2, cp2 = ti._copy_usm_ndarray_into_usm_ndarray(
src=tmp, dst=dst, sycl_queue=q, depends=[cp1]
)
_manager.add_event_pair(hcp2, cp2)
def _copy_same_shape(dst, src):
"""Assumes src and dst have the same shape."""
# check that memory regions do not overlap
if ti._array_overlap(dst, src):
if src._pointer == dst._pointer and (
src is dst
or (src.strides == dst.strides and src.dtype == dst.dtype)
):
return
_copy_overlapping(src=src, dst=dst)
return
copy_q = dst.sycl_queue
_manager = dpctl.utils.SequentialOrderManager[copy_q]
dep_evs = _manager.submitted_events
hev, cpy_ev = ti._copy_usm_ndarray_into_usm_ndarray(
src=src, dst=dst, sycl_queue=copy_q, depends=dep_evs
)
_manager.add_event_pair(hev, cpy_ev)
if hasattr(np, "broadcast_shapes"):
def _broadcast_shapes(sh1, sh2):
return np.broadcast_shapes(sh1, sh2)
else:
def _broadcast_shapes(sh1, sh2):
# use arrays with zero strides, whose memory footprint
# is independent of the number of array elements
return np.broadcast(
np.empty(sh1, dtype=[]),
np.empty(sh2, dtype=[]),
).shape
def _broadcast_strides(X_shape, X_strides, res_ndim):
"""
Broadcasts strides to match the given dimensions;
returns tuple type strides.
"""
out_strides = [0] * res_ndim
X_shape_len = len(X_shape)
str_dim = -X_shape_len
for i in range(X_shape_len):
shape_value = X_shape[i]
if not shape_value == 1:
out_strides[str_dim] = X_strides[i]
str_dim += 1
return tuple(out_strides)
def _copy_from_usm_ndarray_to_usm_ndarray(dst, src):
if any(
not isinstance(arg, dpt.usm_ndarray)
for arg in (
dst,
src,
)
):
raise TypeError(
"Both types are expected to be dpctl.tensor.usm_ndarray, "
f"got {type(dst)} and {type(src)}."
)
if dst.ndim == src.ndim and dst.shape == src.shape:
_copy_same_shape(dst, src)
return
try:
common_shape = _broadcast_shapes(dst.shape, src.shape)
except ValueError as exc:
raise ValueError("Shapes of two arrays are not compatible") from exc
if dst.size < src.size and dst.size < np.prod(common_shape):
raise ValueError("Destination is smaller ")
if len(common_shape) > dst.ndim:
ones_count = len(common_shape) - dst.ndim
for k in range(ones_count):
if common_shape[k] != 1:
raise ValueError
common_shape = common_shape[ones_count:]
if src.ndim < len(common_shape):
new_src_strides = _broadcast_strides(
src.shape, src.strides, len(common_shape)
)
src_same_shape = dpt.usm_ndarray(
common_shape,
dtype=src.dtype,
buffer=src,
strides=new_src_strides,
offset=src._element_offset,
)
elif src.ndim == len(common_shape):
new_src_strides = _broadcast_strides(
src.shape, src.strides, len(common_shape)
)
src_same_shape = dpt.usm_ndarray(
common_shape,
dtype=src.dtype,
buffer=src,
strides=new_src_strides,
offset=src._element_offset,
)
else:
# since broadcasting succeeded, src.ndim is greater because of
# leading sequence of ones, so we trim it
n = len(common_shape)
new_src_strides = _broadcast_strides(
src.shape[-n:], src.strides[-n:], n
)
src_same_shape = dpt.usm_ndarray(
common_shape,
dtype=src.dtype,
buffer=src.usm_data,
strides=new_src_strides,
offset=src._element_offset,
)
_copy_same_shape(dst, src_same_shape)
def _make_empty_like_orderK(x, dt, usm_type, dev):
"""
Returns empty array with shape and strides like `x`, with dtype `dt`,
USM type `usm_type`, on device `dev`.
"""
st = list(x.strides)
perm = sorted(
range(x.ndim),
key=lambda d: builtins.abs(st[d]) if x.shape[d] > 1 else 0,
reverse=True,
)
inv_perm = sorted(range(x.ndim), key=lambda i: perm[i])
sh = x.shape
sh_sorted = tuple(sh[i] for i in perm)
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if min(st) < 0:
st_sorted = [st[i] for i in perm]
sl = tuple(
(
slice(None, None, -1)
if st_sorted[i] < 0
else slice(None, None, None)
)
for i in range(x.ndim)
)
R = R[sl]
return dpt.permute_dims(R, inv_perm)
def _empty_like_orderK(x, dt, usm_type=None, dev=None):
"""
Returns empty array like `x`, using order='K'
For an array `x` that was obtained by permutation of a contiguous
array the returned array will have the same shape and the same
strides as `x`.
"""
if not isinstance(x, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(x)}")
if usm_type is None:
usm_type = x.usm_type
if dev is None:
dev = x.device
fl = x.flags
if fl["C"] or x.size <= 1:
return dpt.empty_like(
x, dtype=dt, usm_type=usm_type, device=dev, order="C"
)
elif fl["F"]:
return dpt.empty_like(
x, dtype=dt, usm_type=usm_type, device=dev, order="F"
)
return _make_empty_like_orderK(x, dt, usm_type, dev)
def _from_numpy_empty_like_orderK(x, dt, usm_type, dev):
"""
Returns empty usm_ndarray like NumPy array `x`, using order='K'
For an array `x` that was obtained by permutation of a contiguous
array the returned array will have the same shape and the same
strides as `x`.
"""
if not isinstance(x, np.ndarray):
raise TypeError(f"Expected numpy.ndarray, got {type(x)}")
fl = x.flags
if fl["C"] or x.size <= 1:
return dpt.empty(
x.shape, dtype=dt, usm_type=usm_type, device=dev, order="C"
)
elif fl["F"]:
return dpt.empty(
x.shape, dtype=dt, usm_type=usm_type, device=dev, order="F"
)
return _make_empty_like_orderK(x, dt, usm_type, dev)
def _empty_like_pair_orderK(X1, X2, dt, res_shape, usm_type, dev):
if not isinstance(X1, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(X1)}")
if not isinstance(X2, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(X2)}")
nd1 = X1.ndim
nd2 = X2.ndim
if nd1 > nd2 and X1.shape == res_shape:
return _empty_like_orderK(X1, dt, usm_type, dev)
elif nd1 < nd2 and X2.shape == res_shape:
return _empty_like_orderK(X2, dt, usm_type, dev)
fl1 = X1.flags
fl2 = X2.flags
if fl1["C"] or fl2["C"]:
return dpt.empty(
res_shape, dtype=dt, usm_type=usm_type, device=dev, order="C"
)
if fl1["F"] and fl2["F"]:
return dpt.empty(
res_shape, dtype=dt, usm_type=usm_type, device=dev, order="F"
)
st1 = list(X1.strides)
st2 = list(X2.strides)
max_ndim = max(nd1, nd2)
st1 += [0] * (max_ndim - len(st1))
st2 += [0] * (max_ndim - len(st2))
sh1 = list(X1.shape) + [0] * (max_ndim - nd1)
sh2 = list(X2.shape) + [0] * (max_ndim - nd2)
perm = sorted(
range(max_ndim),
key=lambda d: (
builtins.abs(st1[d]) if sh1[d] > 1 else 0,
builtins.abs(st2[d]) if sh2[d] > 1 else 0,
),
reverse=True,
)
inv_perm = sorted(range(max_ndim), key=lambda i: perm[i])
st1_sorted = [st1[i] for i in perm]
st2_sorted = [st2[i] for i in perm]
sh = res_shape
sh_sorted = tuple(sh[i] for i in perm)
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if max(min(st1_sorted), min(st2_sorted)) < 0:
sl = tuple(
(
slice(None, None, -1)
if (st1_sorted[i] < 0 and st2_sorted[i] < 0)
else slice(None, None, None)
)
for i in range(nd1)
)
R = R[sl]
return dpt.permute_dims(R, inv_perm)
def _empty_like_triple_orderK(X1, X2, X3, dt, res_shape, usm_type, dev):
if not isinstance(X1, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(X1)}")
if not isinstance(X2, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(X2)}")
if not isinstance(X3, dpt.usm_ndarray):
raise TypeError(f"Expected usm_ndarray, got {type(X3)}")
nd1 = X1.ndim
nd2 = X2.ndim
nd3 = X3.ndim
if X1.shape == res_shape and X2.shape == res_shape and len(res_shape) > nd3:
return _empty_like_pair_orderK(X1, X2, dt, res_shape, usm_type, dev)
elif (
X2.shape == res_shape and X3.shape == res_shape and len(res_shape) > nd1
):
return _empty_like_pair_orderK(X2, X3, dt, res_shape, usm_type, dev)
elif (
X1.shape == res_shape and X3.shape == res_shape and len(res_shape) > nd2
):
return _empty_like_pair_orderK(X1, X3, dt, res_shape, usm_type, dev)
fl1 = X1.flags
fl2 = X2.flags
fl3 = X3.flags
if fl1["C"] or fl2["C"] or fl3["C"]:
return dpt.empty(
res_shape, dtype=dt, usm_type=usm_type, device=dev, order="C"
)
if fl1["F"] and fl2["F"] and fl3["F"]:
return dpt.empty(
res_shape, dtype=dt, usm_type=usm_type, device=dev, order="F"
)
st1 = list(X1.strides)
st2 = list(X2.strides)
st3 = list(X3.strides)
max_ndim = max(nd1, nd2, nd3)
st1 += [0] * (max_ndim - len(st1))
st2 += [0] * (max_ndim - len(st2))
st3 += [0] * (max_ndim - len(st3))
sh1 = list(X1.shape) + [0] * (max_ndim - nd1)
sh2 = list(X2.shape) + [0] * (max_ndim - nd2)
sh3 = list(X3.shape) + [0] * (max_ndim - nd3)
perm = sorted(
range(max_ndim),
key=lambda d: (
builtins.abs(st1[d]) if sh1[d] > 1 else 0,
builtins.abs(st2[d]) if sh2[d] > 1 else 0,
builtins.abs(st3[d]) if sh3[d] > 1 else 0,
),
reverse=True,
)
inv_perm = sorted(range(max_ndim), key=lambda i: perm[i])
st1_sorted = [st1[i] for i in perm]
st2_sorted = [st2[i] for i in perm]
st3_sorted = [st3[i] for i in perm]
sh = res_shape
sh_sorted = tuple(sh[i] for i in perm)
R = dpt.empty(sh_sorted, dtype=dt, usm_type=usm_type, device=dev, order="C")
if max(min(st1_sorted), min(st2_sorted), min(st3_sorted)) < 0:
sl = tuple(
(
slice(None, None, -1)
if (
st1_sorted[i] < 0
and st2_sorted[i] < 0
and st3_sorted[i] < 0
)
else slice(None, None, None)
)
for i in range(nd1)
)
R = R[sl]
return dpt.permute_dims(R, inv_perm)
def copy(usm_ary, /, *, order="K"):
"""copy(ary, order="K")
Creates a copy of given instance of :class:`dpctl.tensor.usm_ndarray`.
Args:
ary (usm_ndarray):
Input array
order (``"C"``, ``"F"``, ``"A"``, ``"K"``, optional):
Controls the memory layout of the output array
Returns:
usm_ndarray:
A copy of the input array.
Memory layout of the copy is controlled by ``order`` keyword,
following NumPy's conventions. The ``order`` keywords can be
one of the following:
.. list-table::
* - ``"C"``
- C-contiguous memory layout
* - ``"F"``
- Fortran-contiguous memory layout
* - ``"A"``
- Fortran-contiguous if the input array is also Fortran-contiguous,
otherwise C-contiguous
* - ``"K"``
- match the layout of ``usm_ary`` as closely as possible.
"""
if len(order) == 0 or order[0] not in "KkAaCcFf":
raise ValueError(
"Unrecognized order keyword value, expecting 'K', 'A', 'F', or 'C'."
)
order = order[0].upper()
if not isinstance(usm_ary, dpt.usm_ndarray):
raise TypeError(
f"Expected object of type dpt.usm_ndarray, got {type(usm_ary)}"
)
copy_order = "C"
if order == "C":
pass
elif order == "F":
copy_order = order
elif order == "A":
if usm_ary.flags.f_contiguous:
copy_order = "F"
elif order == "K":
if usm_ary.flags.f_contiguous:
copy_order = "F"
else:
raise ValueError(
"Unrecognized value of the order keyword. "
"Recognized values are 'A', 'C', 'F', or 'K'"
)
if order == "K":
R = _empty_like_orderK(usm_ary, usm_ary.dtype)
else:
R = dpt.usm_ndarray(
usm_ary.shape,
dtype=usm_ary.dtype,
buffer=usm_ary.usm_type,
order=copy_order,
buffer_ctor_kwargs={"queue": usm_ary.sycl_queue},
)
_copy_same_shape(R, usm_ary)
return R
def astype(
usm_ary, newdtype, /, *, order="K", casting="unsafe", copy=True, device=None
):
""" astype(array, new_dtype, order="K", casting="unsafe", \
copy=True, device=None)
Returns a copy of the :class:`dpctl.tensor.usm_ndarray`, cast to a
specified type.
Args:
array (usm_ndarray):
An input array.
new_dtype (dtype):
The data type of the resulting array. If `None`, gives default
floating point type supported by device where the resulting array
will be located.
order ({"C", "F", "A", "K"}, optional):
Controls memory layout of the resulting array if a copy
is returned.
casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional):
Controls what kind of data casting may occur. Please see
:meth:`numpy.ndarray.astype` for description of casting modes.
copy (bool, optional):
By default, `astype` always returns a newly allocated array.
If this keyword is set to `False`, a view of the input array
may be returned when possible.
device (object): array API specification of device where the
output array is created. Device can be specified by
a filter selector string, an instance of
:class:`dpctl.SyclDevice`, an instance of
:class:`dpctl.SyclQueue`, or an instance of
:class:`dpctl.tensor.Device`. If the value is `None`,
returned array is created on the same device as `array`.
Default: `None`.
Returns:
usm_ndarray:
An array with requested data type.
A view can be returned, if possible, when `copy=False` is used.
"""
if not isinstance(usm_ary, dpt.usm_ndarray):
return TypeError(
f"Expected object of type dpt.usm_ndarray, got {type(usm_ary)}"
)
if len(order) == 0 or order[0] not in "KkAaCcFf":
raise ValueError(
"Unrecognized order keyword value, expecting 'K', 'A', 'F', or 'C'."
)
order = order[0].upper()
ary_dtype = usm_ary.dtype
if device is not None:
if not isinstance(device, dpctl.SyclQueue):
if isinstance(device, dpt.Device):
device = device.sycl_queue
else:
device = dpt.Device.create_device(device).sycl_queue
d = device.sycl_device
target_dtype = _get_dtype(newdtype, device)
if not _dtype_supported_by_device_impl(
target_dtype, d.has_aspect_fp16, d.has_aspect_fp64
):
raise ValueError(
f"Requested dtype '{target_dtype}' is not supported by the "
"target device"
)
usm_ary = usm_ary.to_device(device)
else:
target_dtype = _get_dtype(newdtype, usm_ary.sycl_queue)
if not dpt.can_cast(ary_dtype, target_dtype, casting=casting):
raise TypeError(
f"Can not cast from {ary_dtype} to {newdtype} "
f"according to rule {casting}."
)
c_contig = usm_ary.flags.c_contiguous
f_contig = usm_ary.flags.f_contiguous
needs_copy = copy or not ary_dtype == target_dtype
if not needs_copy and (order != "K"):
# ensure that order="F" for C-contig input triggers copy,
# and order="C" for F-contig input triggers copy too.
# 1D arrays which are both C- and F- contig should not
# force copying for neither order="F", nor order="C", see gh-1926
needs_copy = (
c_contig and not f_contig and order not in ["A", "C"]
) or (not c_contig and f_contig and order not in ["A", "F"])
if not needs_copy:
return usm_ary
copy_order = "C"
if order == "C":
pass
elif order == "F":
copy_order = order
elif order == "A":
if usm_ary.flags.f_contiguous:
copy_order = "F"
elif order == "K":
if usm_ary.flags.f_contiguous:
copy_order = "F"
else:
raise ValueError(
"Unrecognized value of the order keyword. "
"Recognized values are 'A', 'C', 'F', or 'K'"
)
if order == "K":
R = _empty_like_orderK(usm_ary, target_dtype)
else:
R = dpt.usm_ndarray(
usm_ary.shape,
dtype=target_dtype,
buffer=usm_ary.usm_type,
order=copy_order,
buffer_ctor_kwargs={"queue": usm_ary.sycl_queue},
)
# see #2121
if ary_dtype == dpt.bool:
usm_ary = dpt.not_equal(usm_ary, 0, order=order)
_copy_from_usm_ndarray_to_usm_ndarray(R, usm_ary)
return R
def _extract_impl(ary, ary_mask, axis=0):
"""Extract elements of ary by applying mask starting from slot
dimension axis"""
if not isinstance(ary, dpt.usm_ndarray):
raise TypeError(
f"Expecting type dpctl.tensor.usm_ndarray, got {type(ary)}"
)
if not isinstance(ary_mask, dpt.usm_ndarray):
raise TypeError(
f"Expecting type dpctl.tensor.usm_ndarray, got {type(ary_mask)}"
)
dst_usm_type = dpctl.utils.get_coerced_usm_type(
(ary.usm_type, ary_mask.usm_type)
)
exec_q = dpctl.utils.get_execution_queue(
(ary.sycl_queue, ary_mask.sycl_queue)
)
if exec_q is None:
raise dpctl.utils.ExecutionPlacementError(
"arrays have different associated queues. "
"Use `y.to_device(x.device)` to migrate."
)
ary_nd = ary.ndim
pp = normalize_axis_index(operator.index(axis), ary_nd)
mask_nd = ary_mask.ndim
if pp < 0 or pp + mask_nd > ary_nd:
raise ValueError(
"Parameter p is inconsistent with input array dimensions"
)
mask_nelems = ary_mask.size
cumsum_dt = dpt.int32 if mask_nelems < int32_t_max else dpt.int64
cumsum = dpt.empty(mask_nelems, dtype=cumsum_dt, device=ary_mask.device)
exec_q = cumsum.sycl_queue
_manager = dpctl.utils.SequentialOrderManager[exec_q]
dep_evs = _manager.submitted_events
mask_count = ti.mask_positions(
ary_mask, cumsum, sycl_queue=exec_q, depends=dep_evs
)
dst_shape = ary.shape[:pp] + (mask_count,) + ary.shape[pp + mask_nd :]
dst = dpt.empty(
dst_shape, dtype=ary.dtype, usm_type=dst_usm_type, device=ary.device
)
if dst.size == 0:
return dst
hev, ev = ti._extract(
src=ary,
cumsum=cumsum,
axis_start=pp,
axis_end=pp + mask_nd,
dst=dst,
sycl_queue=exec_q,
depends=dep_evs,
)
_manager.add_event_pair(hev, ev)
return dst
def _nonzero_impl(ary):
if not isinstance(ary, dpt.usm_ndarray):
raise TypeError(
f"Expecting type dpctl.tensor.usm_ndarray, got {type(ary)}"
)
exec_q = ary.sycl_queue
usm_type = ary.usm_type
mask_nelems = ary.size
cumsum_dt = dpt.int32 if mask_nelems < int32_t_max else dpt.int64
cumsum = dpt.empty(
mask_nelems, dtype=cumsum_dt, sycl_queue=exec_q, order="C"
)
_manager = dpctl.utils.SequentialOrderManager[exec_q]
dep_evs = _manager.submitted_events
mask_count = ti.mask_positions(
ary, cumsum, sycl_queue=exec_q, depends=dep_evs
)
indexes_dt = ti.default_device_index_type(exec_q.sycl_device)
indexes = dpt.empty(
(ary.ndim, mask_count),
dtype=indexes_dt,
usm_type=usm_type,
sycl_queue=exec_q,
order="C",
)
hev, nz_ev = ti._nonzero(cumsum, indexes, ary.shape, exec_q)
res = tuple(indexes[i, :] for i in range(ary.ndim))
_manager.add_event_pair(hev, nz_ev)
return res
def _validate_indices(inds, queue_list, usm_type_list):
"""
Utility for validating indices are usm_ndarray of integral dtype or Python
integers. At least one must be an array.
For each array, the queue and usm type are appended to `queue_list` and
`usm_type_list`, respectively.
"""
any_usmarray = False
for ind in inds:
if isinstance(ind, dpt.usm_ndarray):
any_usmarray = True
if ind.dtype.kind not in "ui":
raise IndexError(
"arrays used as indices must be of integer (or boolean) "
"type"
)
queue_list.append(ind.sycl_queue)
usm_type_list.append(ind.usm_type)
elif not isinstance(ind, Integral):
raise TypeError(
"all elements of `ind` expected to be usm_ndarrays "
f"or integers, found {type(ind)}"
)
if not any_usmarray:
raise TypeError(
"at least one element of `inds` expected to be a usm_ndarray"
)
return inds
def _prepare_indices_arrays(inds, q, usm_type):
"""
Utility taking a mix of usm_ndarray and possibly Python int scalar indices,
a queue (assumed to be common to arrays in inds), and a usm type.
Python scalar integers are promoted to arrays on the provided queue and
with the provided usm type. All arrays are then promoted to a common
integral type (if possible) before being broadcast to a common shape.
"""
# scalar integers -> arrays
inds = tuple(
map(
lambda ind: (
ind
if isinstance(ind, dpt.usm_ndarray)
else dpt.asarray(ind, usm_type=usm_type, sycl_queue=q)
),
inds,
)
)
# promote to a common integral type if possible
ind_dt = dpt.result_type(*inds)
if ind_dt.kind not in "ui":
raise ValueError(
"cannot safely promote indices to an integer data type"
)
inds = tuple(
map(
lambda ind: (
ind if ind.dtype == ind_dt else dpt.astype(ind, ind_dt)
),
inds,
)
)
# broadcast
inds = dpt.broadcast_arrays(*inds)
return inds
def _take_multi_index(ary, inds, p, mode=0):
if not isinstance(ary, dpt.usm_ndarray):
raise TypeError(
f"Expecting type dpctl.tensor.usm_ndarray, got {type(ary)}"
)
ary_nd = ary.ndim
p = normalize_axis_index(operator.index(p), ary_nd)
mode = operator.index(mode)
if mode not in [0, 1]:
raise ValueError(
"Invalid value for mode keyword, only 0 or 1 is supported"
)
queues_ = [
ary.sycl_queue,
]
usm_types_ = [
ary.usm_type,
]
if not isinstance(inds, (list, tuple)):
inds = (inds,)
_validate_indices(inds, queues_, usm_types_)
res_usm_type = dpctl.utils.get_coerced_usm_type(usm_types_)
exec_q = dpctl.utils.get_execution_queue(queues_)
if exec_q is None:
raise dpctl.utils.ExecutionPlacementError(
"Can not automatically determine where to allocate the "
"result or performance execution. "
"Use `usm_ndarray.to_device` method to migrate data to "
"be associated with the same queue."
)
if len(inds) > 1:
inds = _prepare_indices_arrays(inds, exec_q, res_usm_type)
ind0 = inds[0]
ary_sh = ary.shape
p_end = p + len(inds)
if 0 in ary_sh[p:p_end] and ind0.size != 0:
raise IndexError("cannot take non-empty indices from an empty axis")
res_shape = ary_sh[:p] + ind0.shape + ary_sh[p_end:]
res = dpt.empty(
res_shape, dtype=ary.dtype, usm_type=res_usm_type, sycl_queue=exec_q
)
_manager = dpctl.utils.SequentialOrderManager[exec_q]
dep_ev = _manager.submitted_events
hev, take_ev = ti._take(
src=ary,
ind=inds,
dst=res,
axis_start=p,
mode=mode,
sycl_queue=exec_q,
depends=dep_ev,
)
_manager.add_event_pair(hev, take_ev)
return res
def _place_impl(ary, ary_mask, vals, axis=0):
"""Extract elements of ary by applying mask starting from slot
dimension axis"""
if not isinstance(ary, dpt.usm_ndarray):
raise TypeError(
f"Expecting type dpctl.tensor.usm_ndarray, got {type(ary)}"
)
if not isinstance(ary_mask, dpt.usm_ndarray):
raise TypeError(
f"Expecting type dpctl.tensor.usm_ndarray, got {type(ary_mask)}"
)
exec_q = dpctl.utils.get_execution_queue(
(
ary.sycl_queue,
ary_mask.sycl_queue,
)
)
if exec_q is not None:
if not isinstance(vals, dpt.usm_ndarray):
vals = dpt.asarray(vals, dtype=ary.dtype, sycl_queue=exec_q)
else:
exec_q = dpctl.utils.get_execution_queue((exec_q, vals.sycl_queue))
if exec_q is None:
raise dpctl.utils.ExecutionPlacementError(
"arrays have different associated queues. "
"Use `Y.to_device(X.device)` to migrate."
)
ary_nd = ary.ndim
pp = normalize_axis_index(operator.index(axis), ary_nd)
mask_nd = ary_mask.ndim
if pp < 0 or pp + mask_nd > ary_nd:
raise ValueError(