-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdpnp_elementwise_common.py
More file actions
1434 lines (1258 loc) · 48.3 KB
/
dpnp_elementwise_common.py
File metadata and controls
1434 lines (1258 loc) · 48.3 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
# *****************************************************************************
# Copyright (c) 2023, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
import warnings
from functools import wraps
import dpctl.utils as dpu
import numpy
import dpnp
import dpnp.backend.extensions.vm._vm_impl as vmi
# pylint: disable=no-name-in-module
import dpnp.tensor as dpt
import dpnp.tensor._copy_utils as dtc
import dpnp.tensor._tensor_impl as dti
import dpnp.tensor._type_utils as dtu
from dpnp.dpnp_array import dpnp_array
from dpnp.dpnp_utils import get_usm_allocations
from dpnp.dpnp_utils.dpnp_utils_common import (
find_buf_dtype_3out,
find_buf_dtype_4out,
)
from dpnp.tensor._elementwise_common import (
BinaryElementwiseFunc,
UnaryElementwiseFunc,
)
from dpnp.tensor._scalar_utils import (
_get_dtype,
_get_shape,
_validate_dtype,
)
__all__ = [
"DPNPI0",
"DPNPAngle",
"DPNPBinaryFunc",
"DPNPBinaryFuncOutKw",
"DPNPBinaryTwoOutputsFunc",
"DPNPDeprecatedUnaryFunc",
"DPNPImag",
"DPNPReal",
"DPNPRound",
"DPNPSinc",
"DPNPUnaryFunc",
"DPNPUnaryTwoOutputsFunc",
"acceptance_fn_gcd_lcm",
"acceptance_fn_negative",
"acceptance_fn_positive",
"acceptance_fn_sign",
"acceptance_fn_subtract",
"resolve_weak_types_2nd_arg_int",
]
class DPNPUnaryFunc(UnaryElementwiseFunc):
"""
Class that implements unary element-wise functions.
Parameters
----------
name : {str}
Name of the unary function
result_type_resolver_fn : {callable}
Function that takes dtype of the input and returns the dtype of
the result if the implementation functions supports it, or
returns `None` otherwise.
unary_dp_impl_fn : {callable}
Data-parallel implementation function with signature
`impl_fn(src: usm_ndarray, dst: usm_ndarray,
sycl_queue: SyclQueue, depends: Optional[List[SyclEvent]])`
where the `src` is the argument array, `dst` is the
array to be populated with function values, effectively
evaluating `dst = func(src)`.
The `impl_fn` is expected to return a 2-tuple of `SyclEvent`s.
The first event corresponds to data-management host tasks,
including lifetime management of argument Python objects to ensure
that their associated USM allocation is not freed before offloaded
computational tasks complete execution, while the second event
corresponds to computational tasks associated with function evaluation.
docs : {str}
Documentation string for the unary function.
mkl_fn_to_call : {None, str}
Check input arguments to answer if function from OneMKL VM library
can be used.
mkl_impl_fn : {None, str}
Function from OneMKL VM library to call.
acceptance_fn : {None, callable}, optional
Function to influence type promotion behavior of this unary
function. The function takes 4 arguments:
arg_dtype - Data type of the first argument
buf_dtype - Data type the argument would be cast to
res_dtype - Data type of the output array with function values
sycl_dev - The :class:`dpctl.SyclDevice` where the function
evaluation is carried out.
The function is invoked when the argument of the unary function
requires casting, e.g. the argument of `dpctl.tensor.log` is an
array with integral data type.
"""
def __init__(
self,
name,
result_type_resolver_fn,
unary_dp_impl_fn,
docs,
mkl_fn_to_call=None,
mkl_impl_fn=None,
acceptance_fn=None,
):
def _call_func(src, dst, sycl_queue, depends=None):
"""
A callback to register in UnaryElementwiseFunc class of
dpctl.tensor
"""
if depends is None:
depends = []
if vmi._is_available() and not (
mkl_impl_fn is None or mkl_fn_to_call is None
):
if getattr(vmi, mkl_fn_to_call)(sycl_queue, src, dst):
# call pybind11 extension for unary function from OneMKL VM
return getattr(vmi, mkl_impl_fn)(
sycl_queue, src, dst, depends
)
return unary_dp_impl_fn(src, dst, sycl_queue, depends)
super().__init__(
name,
result_type_resolver_fn,
_call_func,
docs,
acceptance_fn=acceptance_fn,
)
self.__name__ = "DPNPUnaryFunc"
def __call__(
self,
x,
/,
out=None,
*,
where=True,
order="K",
dtype=None,
subok=True,
**kwargs,
):
if kwargs:
raise NotImplementedError(
f"Requested function={self.name_} with kwargs={kwargs} "
"isn't currently supported."
)
elif where is not True:
raise NotImplementedError(
f"Requested function={self.name_} with where={where} "
"isn't currently supported."
)
elif subok is not True:
raise NotImplementedError(
f"Requested function={self.name_} with subok={subok} "
"isn't currently supported."
)
elif not dpnp.is_supported_array_type(x):
raise TypeError(
"Input array must be any of supported type, "
f"but got {type(x)}"
)
elif dtype is not None and out is not None:
raise TypeError(
f"Requested function={self.name_} only takes `out` or `dtype` "
"as an argument, but both were provided."
)
if order is None:
order = "K"
elif order in "afkcAFKC":
order = order.upper()
else:
raise ValueError(
"order must be one of 'C', 'F', 'A', or 'K' " f"(got '{order}')"
)
x_usm = dpnp.get_usm_ndarray(x)
if dtype is not None:
x_usm = dpt.astype(x_usm, dtype, copy=False)
out = self._unpack_out_kw(out)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
res_usm = super().__call__(x_usm, out=out_usm, order=order)
if out is not None and isinstance(out, dpnp_array):
return out
return dpnp_array._create_from_usm_ndarray(res_usm)
def _unpack_out_kw(self, out):
"""Unpack `out` keyword if passed as a tuple."""
if isinstance(out, tuple):
if len(out) != self.nout:
raise ValueError(
"'out' tuple must have exactly one entry per ufunc output"
)
return out[0]
return out
class DPNPDeprecatedUnaryFunc(DPNPUnaryFunc):
"""
Class that implements a deprecated unary element-wise function.
Parameters
----------
deprecated_msg : {str, None}, optional
Warning message to emit. If None, no warning is issued.
Default: ``None``.
"""
def __init__(self, *args, deprecated_msg=None, **kwargs):
super().__init__(*args, **kwargs)
self._deprecated_msg = deprecated_msg
@wraps(DPNPUnaryFunc.__call__)
def __call__(self, *args, **kwargs):
if self._deprecated_msg:
warnings.warn(
self._deprecated_msg, DeprecationWarning, stacklevel=2
)
return super().__call__(*args, **kwargs)
class DPNPUnaryTwoOutputsFunc(UnaryElementwiseFunc):
"""
Class that implements unary element-wise functions with two output arrays.
Parameters
----------
name : {str}
Name of the unary function
result_type_resolver_fn : {callable}
Function that takes dtype of the input and returns the dtype of
the result if the implementation functions supports it, or
returns `None` otherwise.
unary_dp_impl_fn : {callable}
Data-parallel implementation function with signature
`impl_fn(src: usm_ndarray, dst: usm_ndarray,
sycl_queue: SyclQueue, depends: Optional[List[SyclEvent]])`
where the `src` is the argument array, `dst` is the
array to be populated with function values, effectively
evaluating `dst = func(src)`.
The `impl_fn` is expected to return a 2-tuple of `SyclEvent`s.
The first event corresponds to data-management host tasks,
including lifetime management of argument Python objects to ensure
that their associated USM allocation is not freed before offloaded
computational tasks complete execution, while the second event
corresponds to computational tasks associated with function evaluation.
docs : {str}
Documentation string for the unary function.
mkl_fn_to_call : {None, str}
Check input arguments to answer if function from OneMKL VM library
can be used.
mkl_impl_fn : {None, str}
Function from OneMKL VM library to call.
"""
def __init__(
self,
name,
result_type_resolver_fn,
unary_dp_impl_fn,
docs,
mkl_fn_to_call=None,
mkl_impl_fn=None,
):
def _call_func(src, dst1, dst2, sycl_queue, depends=None):
"""A callback to register in UnaryElementwiseFunc class."""
if depends is None:
depends = []
if vmi._is_available() and not (
mkl_impl_fn is None or mkl_fn_to_call is None
):
if getattr(vmi, mkl_fn_to_call)(sycl_queue, src, dst1, dst2):
# call pybind11 extension for unary function from OneMKL VM
return getattr(vmi, mkl_impl_fn)(
sycl_queue, src, dst1, dst2, depends
)
return unary_dp_impl_fn(src, dst1, dst2, sycl_queue, depends)
super().__init__(
name,
result_type_resolver_fn,
_call_func,
docs,
)
self.__name__ = "DPNPUnaryTwoOutputsFunc"
@property
def nout(self):
"""Returns the number of arguments treated as outputs."""
return 2
@property
def types(self):
"""
Returns information about types supported by implementation function,
using NumPy's character encoding for data type.
Examples
--------
>>> import dpnp as np
>>> np.frexp.types
['e->ei', 'f->fi', 'd->di']
"""
types = self.types_
if not types:
types = []
for dt1 in dtu._all_data_types(True, True):
dt2 = self.result_type_resolver_fn_(dt1)
if all(dt for dt in dt2):
types.append(f"{dt1.char}->{dt2[0].char}{dt2[1].char}")
self.types_ = types
return types
def __call__(
self,
x,
out1=None,
out2=None,
/,
*,
out=(None, None),
where=True,
order="K",
dtype=None,
subok=True,
**kwargs,
):
if kwargs:
raise NotImplementedError(
f"Requested function={self.name_} with kwargs={kwargs} "
"isn't currently supported."
)
elif where is not True:
raise NotImplementedError(
f"Requested function={self.name_} with where={where} "
"isn't currently supported."
)
elif dtype is not None:
raise NotImplementedError(
f"Requested function={self.name_} with dtype={dtype} "
"isn't currently supported."
)
elif subok is not True:
raise NotImplementedError(
f"Requested function={self.name_} with subok={subok} "
"isn't currently supported."
)
x = dpnp.get_usm_ndarray(x)
exec_q = x.sycl_queue
if order is None:
order = "K"
elif order in "afkcAFKC":
order = order.upper()
if order == "A":
order = "F" if x.flags.f_contiguous else "C"
else:
raise ValueError(
"order must be one of 'C', 'F', 'A', or 'K' " f"(got '{order}')"
)
buf_dt, res1_dt, res2_dt = find_buf_dtype_3out(
x.dtype,
self.get_type_result_resolver_function(),
x.sycl_device,
)
if res1_dt is None or res2_dt is None:
raise ValueError(
f"function '{self.name_}' does not support input type "
f"({x.dtype}), "
"and the input could not be safely coerced to any "
"supported types according to the casting rule ''safe''."
)
if not isinstance(out, tuple):
raise TypeError("'out' must be a tuple of arrays")
if len(out) != self.nout:
raise ValueError(
"'out' tuple must have exactly one entry per ufunc output"
)
if not (out1 is None and out2 is None):
if all(res is None for res in out):
out = (out1, out2)
else:
raise TypeError(
"cannot specify 'out' as both a positional and keyword argument"
)
orig_out, out = list(out), list(out)
res_dts = [res1_dt, res2_dt]
for i in range(self.nout):
if out[i] is None:
continue
res = dpnp.get_usm_ndarray(out[i])
if not res.flags.writable:
raise ValueError("output array is read-only")
if res.shape != x.shape:
raise ValueError(
"The shape of input and output arrays are inconsistent. "
f"Expected output shape is {x.shape}, got {res.shape}"
)
if dpt.get_execution_queue((exec_q, res.sycl_queue)) is None:
raise dpnp.exceptions.ExecutionPlacementError(
"Input and output allocation queues are not compatible"
)
res_dt = res_dts[i]
if res_dt != res.dtype:
if not dpnp.can_cast(res_dt, res.dtype, casting="same_kind"):
raise TypeError(
f"Cannot cast ufunc '{self.name_}' output {i + 1} from "
f"{res_dt} to {res.dtype} with casting rule 'same_kind'"
)
# Allocate a temporary buffer with the required dtype
out[i] = dpt.empty_like(res, dtype=res_dt)
elif (
buf_dt is None
and dti._array_overlap(x, res)
and not dti._same_logical_tensors(x, res)
):
# Allocate a temporary buffer to avoid memory overlapping.
# Note if `buf_dt` is not None, a temporary copy of `x` will be
# created, so the array overlap check isn't needed.
out[i] = dpt.empty_like(res)
_manager = dpu.SequentialOrderManager[exec_q]
dep_evs = _manager.submitted_events
# Cast input array to the supported type if needed
if buf_dt is not None:
if order == "K":
buf = dtc._empty_like_orderK(x, buf_dt)
else:
buf = dpt.empty_like(x, dtype=buf_dt, order=order)
ht_copy_ev, copy_ev = dti._copy_usm_ndarray_into_usm_ndarray(
src=x, dst=buf, sycl_queue=exec_q, depends=dep_evs
)
_manager.add_event_pair(ht_copy_ev, copy_ev)
x = buf
dep_evs = copy_ev
# Allocate a buffer for the output arrays if needed
for i in range(self.nout):
if out[i] is None:
res_dt = res_dts[i]
if order == "K":
out[i] = dtc._empty_like_orderK(x, res_dt)
else:
out[i] = dpt.empty_like(x, dtype=res_dt, order=order)
# Call the unary function with input and output arrays
ht_unary_ev, unary_ev = self.get_implementation_function()(
x,
dpnp.get_usm_ndarray(out[0]),
dpnp.get_usm_ndarray(out[1]),
sycl_queue=exec_q,
depends=_manager.submitted_events,
)
_manager.add_event_pair(ht_unary_ev, unary_ev)
for i in range(self.nout):
orig_res, res = orig_out[i], out[i]
if not (orig_res is None or orig_res is res):
# Copy the out data from temporary buffer to original memory
ht_copy_ev, copy_ev = dti._copy_usm_ndarray_into_usm_ndarray(
src=res,
dst=dpnp.get_usm_ndarray(orig_res),
sycl_queue=exec_q,
depends=[unary_ev],
)
_manager.add_event_pair(ht_copy_ev, copy_ev)
res = out[i] = orig_res
if not isinstance(res, dpnp_array):
# Always return dpnp.ndarray
out[i] = dpnp_array._create_from_usm_ndarray(res)
return tuple(out)
class DPNPBinaryFunc(BinaryElementwiseFunc):
"""
Class that implements binary element-wise functions.
Args:
name : {str}
Name of the binary function
result_type_resovle_fn : {callable}
Function that takes dtype of the input and returns the dtype of
the result if the implementation functions supports it, or
returns `None` otherwise..
binary_dp_impl_fn : {callable}
Data-parallel implementation function with signature
`impl_fn(src1: usm_ndarray, src2: usm_ndarray, dst: usm_ndarray,
sycl_queue: SyclQueue, depends: Optional[List[SyclEvent]])`
where the `src1` and `src2` are the argument arrays, `dst` is the
array to be populated with function values,
i.e. `dst=func(src1, src2)`.
The `impl_fn` is expected to return a 2-tuple of `SyclEvent`s.
The first event corresponds to data-management host tasks,
including lifetime management of argument Python objects to ensure
that their associated USM allocation is not freed before offloaded
computational tasks complete execution, while the second event
corresponds to computational tasks associated with function
evaluation.
docs : {str}
Documentation string for the binary function.
mkl_fn_to_call : {None, str}
Check input arguments to answer if function from OneMKL VM library
can be used.
mkl_impl_fn : {None, str}
Function from OneMKL VM library to call.
binary_inplace_fn : {None, callable}, optional
Data-parallel implementation function with signature
`impl_fn(src: usm_ndarray, dst: usm_ndarray,
sycl_queue: SyclQueue, depends: Optional[List[SyclEvent]])`
where the `src` is the argument array, `dst` is the
array to be populated with function values,
i.e. `dst=func(dst, src)`.
The `impl_fn` is expected to return a 2-tuple of `SyclEvent`s.
The first event corresponds to data-management host tasks,
including async lifetime management of Python arguments,
while the second event corresponds to computational tasks
associated with function evaluation.
acceptance_fn : {None, callable}, optional
Function to influence type promotion behavior of this binary
function. The function takes 6 arguments:
arg1_dtype - Data type of the first argument
arg2_dtype - Data type of the second argument
ret_buf1_dtype - Data type the first argument would be cast to
ret_buf2_dtype - Data type the second argument would be cast to
res_dtype - Data type of the output array with function values
sycl_dev - The :class:`dpctl.SyclDevice` where the function
evaluation is carried out.
The function is only called when both arguments of the binary
function require casting, e.g. both arguments of
`dpctl.tensor.logaddexp` are arrays with integral data type.
weak_type_resolver : {None, callable}, optional
Function to influence type promotion behavior for Python scalar types
of this binary function. The function takes 3 arguments:
o1_dtype - Data type or Python scalar type of the first argument
o2_dtype - Data type or Python scalar type of of the second argument
sycl_dev - The :class:`dpctl.SyclDevice` where the function
evaluation is carried out.
One of `o1_dtype` and `o2_dtype` must be a ``dtype`` instance.
"""
def __init__(
self,
name,
result_type_resolver_fn,
binary_dp_impl_fn,
docs,
mkl_fn_to_call=None,
mkl_impl_fn=None,
binary_inplace_fn=None,
acceptance_fn=None,
weak_type_resolver=None,
):
def _call_func(src1, src2, dst, sycl_queue, depends=None):
"""
A callback to register in UnaryElementwiseFunc class of
dpctl.tensor
"""
if depends is None:
depends = []
if vmi._is_available() and not (
mkl_impl_fn is None or mkl_fn_to_call is None
):
if getattr(vmi, mkl_fn_to_call)(sycl_queue, src1, src2, dst):
# call pybind11 extension for binary function from OneMKL VM
return getattr(vmi, mkl_impl_fn)(
sycl_queue, src1, src2, dst, depends
)
return binary_dp_impl_fn(src1, src2, dst, sycl_queue, depends)
super().__init__(
name,
result_type_resolver_fn,
_call_func,
docs,
binary_inplace_fn,
acceptance_fn=acceptance_fn,
weak_type_resolver=weak_type_resolver,
)
self.__name__ = "DPNPBinaryFunc"
def __call__(
self,
x1,
x2,
/,
out=None,
*,
where=True,
order="K",
dtype=None,
subok=True,
**kwargs,
):
dpnp.check_supported_arrays_type(
x1, x2, scalar_type=True, all_scalars=False
)
if kwargs:
raise NotImplementedError(
f"Requested function={self.name_} with kwargs={kwargs} "
"isn't currently supported."
)
elif where is not True:
raise NotImplementedError(
f"Requested function={self.name_} with where={where} "
"isn't currently supported."
)
elif subok is not True:
raise NotImplementedError(
f"Requested function={self.name_} with subok={subok} "
"isn't currently supported."
)
elif dtype is not None and out is not None:
raise TypeError(
f"Requested function={self.name_} only takes `out` or `dtype` "
"as an argument, but both were provided."
)
x1_usm = dpnp.get_usm_ndarray_or_scalar(x1)
x2_usm = dpnp.get_usm_ndarray_or_scalar(x2)
if isinstance(out, tuple):
if len(out) != self.nout:
raise ValueError(
"'out' tuple must have exactly one entry per ufunc output"
)
out = out[0]
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
if (
isinstance(x1, dpnp_array)
and x1 is out
and order == "K"
and dtype is None
):
# in-place operation
super()._inplace_op(x1_usm, x2_usm)
return x1
if order is None:
order = "K"
elif order in "afkcAFKC":
order = order.upper()
else:
raise ValueError(
"order must be one of 'C', 'F', 'A', or 'K' (got '{order}')"
)
if dtype is not None:
if dpnp.isscalar(x1):
x1_usm = dpt.asarray(
x1,
dtype=dtype,
sycl_queue=x2.sycl_queue,
usm_type=x2.usm_type,
)
x2_usm = dpt.astype(x2_usm, dtype, copy=False)
elif dpnp.isscalar(x2):
x1_usm = dpt.astype(x1_usm, dtype, copy=False)
x2_usm = dpt.asarray(
x2,
dtype=dtype,
sycl_queue=x1.sycl_queue,
usm_type=x1.usm_type,
)
else:
x1_usm = dpt.astype(x1_usm, dtype, copy=False)
x2_usm = dpt.astype(x2_usm, dtype, copy=False)
res_usm = super().__call__(x1_usm, x2_usm, out=out_usm, order=order)
if out is not None and isinstance(out, dpnp_array):
return out
return dpnp_array._create_from_usm_ndarray(res_usm)
def outer(
self,
x1,
x2,
out=None,
where=True,
order="K",
dtype=None,
subok=True,
**kwargs,
):
"""
Apply the ufunc op to all pairs (a, b) with a in A and b in B.
Parameters
----------
x1 : {dpnp.ndarray, usm_ndarray}
First input array.
x2 : {dpnp.ndarray, usm_ndarray}
Second input array.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Output array to populate.
Array must have the correct shape and the expected data type.
order : {None, "C", "F", "A", "K"}, optional
Memory layout of the newly output array, Cannot be provided
together with `out`. Default: ``"K"``.
dtype : {None, str, dtype object}, optional
If provided, the destination array will have this dtype. Cannot be
provided together with `out`. Default: ``None``.
Returns
-------
out : dpnp.ndarray
Output array. The data type of the returned array is determined by
the Type Promotion Rules.
Limitations
-----------
Parameters `where` and `subok` are supported with their default values.
Keyword argument `kwargs` is currently unsupported.
Otherwise ``NotImplementedError`` exception will be raised.
See also
--------
:obj:`dpnp.outer` : A less powerful version of dpnp.multiply.outer
that ravels all inputs to 1D. This exists primarily
for compatibility with old code.
:obj:`dpnp.tensordot` : dpnp.tensordot(a, b, axes=((), ())) and
dpnp.multiply.outer(a, b) behave same for all
dimensions of a and b.
Examples
--------
>>> import dpnp as np
>>> A = np.array([1, 2, 3])
>>> B = np.array([4, 5, 6])
>>> np.multiply.outer(A, B)
array([[ 4, 5, 6],
[ 8, 10, 12],
[12, 15, 18]])
A multi-dimensional example:
>>> A = np.array([[1, 2, 3], [4, 5, 6]])
>>> A.shape
(2, 3)
>>> B = np.array([[1, 2, 3, 4]])
>>> B.shape
(1, 4)
>>> C = np.multiply.outer(A, B)
>>> C.shape; C
(2, 3, 1, 4)
array([[[[ 1, 2, 3, 4]],
[[ 2, 4, 6, 8]],
[[ 3, 6, 9, 12]]],
[[[ 4, 8, 12, 16]],
[[ 5, 10, 15, 20]],
[[ 6, 12, 18, 24]]]])
"""
dpnp.check_supported_arrays_type(
x1, x2, scalar_type=True, all_scalars=False
)
if dpnp.isscalar(x1) or dpnp.isscalar(x2):
_x1 = x1
_x2 = x2
else:
_x1 = x1[(Ellipsis,) + (None,) * x2.ndim]
_x2 = x2[(None,) * x1.ndim + (Ellipsis,)]
return self.__call__(
_x1,
_x2,
out=out,
where=where,
order=order,
dtype=dtype,
subok=subok,
**kwargs,
)
class DPNPBinaryFuncOutKw(DPNPBinaryFunc):
"""DPNPBinaryFunc that deprecates positional `out` argument."""
@wraps(DPNPBinaryFunc.__call__)
def __call__(self, *args, **kwargs):
if len(args) > self.nin:
warnings.warn(
"Passing more than 2 positional arguments is deprecated. "
"If you meant to use the third argument as an output, "
"use the `out` keyword argument instead.",
DeprecationWarning,
stacklevel=2,
)
return super().__call__(*args, **kwargs)
class DPNPBinaryTwoOutputsFunc(BinaryElementwiseFunc):
"""
Class that implements binary element-wise functions with two output arrays.
Parameters
----------
name : {str}
Name of the binary function
result_type_resolver_fn : {callable}
Function that takes dtype of the input and returns the dtype of
the result if the implementation functions supports it, or
returns `None` otherwise.
binary_dp_impl_fn : {callable}
Data-parallel implementation function with signature
`impl_fn(src: usm_ndarray, dst: usm_ndarray,
sycl_queue: SyclQueue, depends: Optional[List[SyclEvent]])`
where the `src` is the argument array, `dst` is the
array to be populated with function values, effectively
evaluating `dst = func(src)`.
The `impl_fn` is expected to return a 2-tuple of `SyclEvent`s.
The first event corresponds to data-management host tasks,
including lifetime management of argument Python objects to ensure
that their associated USM allocation is not freed before offloaded
computational tasks complete execution, while the second event
corresponds to computational tasks associated with function evaluation.
docs : {str}
Documentation string for the binary function.
"""
def __init__(
self,
name,
result_type_resolver_fn,
binary_dp_impl_fn,
docs,
):
super().__init__(
name,
result_type_resolver_fn,
binary_dp_impl_fn,
docs,
)
self.__name__ = "DPNPBinaryTwoOutputsFunc"
@property
def nout(self):
"""Returns the number of arguments treated as outputs."""
return 2
@property
def types(self):
"""
Returns information about types supported by implementation function,
using NumPy's character encoding for data types, e.g.
Examples
--------
>>> import dpnp as np
>>> np.divmod.types
['bb->bb', 'BB->BB', 'hh->hh', 'HH->HH', 'ii->ii', 'II->II',
'll->ll', 'LL->LL', 'ee->ee', 'ff->ff', 'dd->dd']
"""
types = self.types_
if not types:
types = []
_all_dtypes = dtu._all_data_types(True, True)
for dt1 in _all_dtypes:
for dt2 in _all_dtypes:
dt3 = self.result_type_resolver_fn_(dt1, dt2)
if all(dt for dt in dt3):
types.append(
f"{dt1.char}{dt2.char}->{dt3[0].char}{dt3[1].char}"
)
self.types_ = types
return types
def __call__(
self,
x1,
x2,
out1=None,
out2=None,
/,
*,
out=(None, None),
where=True,
order="K",
dtype=None,
subok=True,
**kwargs,
):
if kwargs:
raise NotImplementedError(
f"Requested function={self.name_} with kwargs={kwargs} "
"isn't currently supported."
)
elif where is not True:
raise NotImplementedError(
f"Requested function={self.name_} with where={where} "
"isn't currently supported."
)
elif dtype is not None:
raise NotImplementedError(
f"Requested function={self.name_} with dtype={dtype} "
"isn't currently supported."
)
elif subok is not True:
raise NotImplementedError(
f"Requested function={self.name_} with subok={subok} "
"isn't currently supported."
)
dpnp.check_supported_arrays_type(x1, x2, scalar_type=True)
if order is None:
order = "K"
elif order in "afkcAFKC":
order = order.upper()
else:
raise ValueError(
"order must be one of 'C', 'F', 'A', or 'K' " f"(got '{order}')"
)
res_usm_type, exec_q = get_usm_allocations([x1, x2])
x1 = dpnp.get_usm_ndarray_or_scalar(x1)
x2 = dpnp.get_usm_ndarray_or_scalar(x2)
x1_sh = _get_shape(x1)
x2_sh = _get_shape(x2)
try:
res_shape = dpnp.broadcast_shapes(x1_sh, x2_sh)
except ValueError:
raise ValueError(
"operands could not be broadcast together with shapes "
f"{x1_sh} and {x2_sh}"
)
sycl_dev = exec_q.sycl_device
x1_dt = _get_dtype(x1, sycl_dev)
x2_dt = _get_dtype(x2, sycl_dev)
if not all(