@@ -52,6 +52,15 @@ def __call__(self, x, out=None, order="K"):
52
52
if not isinstance (x , dpt .usm_ndarray ):
53
53
raise TypeError (f"Expected dpctl.tensor.usm_ndarray, got { type (x )} " )
54
54
55
+ if order not in ["C" , "F" , "K" , "A" ]:
56
+ order = "K"
57
+ buf_dt , res_dt = _find_buf_dtype (
58
+ x .dtype , self .result_type_resolver_fn_ , x .sycl_device
59
+ )
60
+ if res_dt is None :
61
+ raise RuntimeError
62
+
63
+ orig_out = out
55
64
if out is not None :
56
65
if not isinstance (out , dpt .usm_ndarray ):
57
66
raise TypeError (
@@ -64,8 +73,21 @@ def __call__(self, x, out=None, order="K"):
64
73
f"Expected output shape is { x .shape } , got { out .shape } "
65
74
)
66
75
67
- if ti ._array_overlap (x , out ):
68
- raise TypeError ("Input and output arrays have memory overlap" )
76
+ if res_dt != out .dtype :
77
+ raise TypeError (
78
+ f"Output array of type { res_dt } is needed,"
79
+ f" got { out .dtype } "
80
+ )
81
+
82
+ if (
83
+ buf_dt is None
84
+ and ti ._array_overlap (x , out )
85
+ and not ti ._same_logical_tensors (x , out )
86
+ ):
87
+ # Allocate a temporary buffer to avoid memory overlapping.
88
+ # Note if `buf_dt` is not None, a temporary copy of `x` will be
89
+ # created, so the array overlap check isn't needed.
90
+ out = dpt .empty_like (out )
69
91
70
92
if (
71
93
dpctl .utils .get_execution_queue ((x .sycl_queue , out .sycl_queue ))
@@ -75,13 +97,6 @@ def __call__(self, x, out=None, order="K"):
75
97
"Input and output allocation queues are not compatible"
76
98
)
77
99
78
- if order not in ["C" , "F" , "K" , "A" ]:
79
- order = "K"
80
- buf_dt , res_dt = _find_buf_dtype (
81
- x .dtype , self .result_type_resolver_fn_ , x .sycl_device
82
- )
83
- if res_dt is None :
84
- raise RuntimeError
85
100
exec_q = x .sycl_queue
86
101
if buf_dt is None :
87
102
if out is None :
@@ -91,17 +106,20 @@ def __call__(self, x, out=None, order="K"):
91
106
if order == "A" :
92
107
order = "F" if x .flags .f_contiguous else "C"
93
108
out = dpt .empty_like (x , dtype = res_dt , order = order )
94
- else :
95
- if res_dt != out .dtype :
96
- raise TypeError (
97
- f"Output array of type { res_dt } is needed,"
98
- f" got { out .dtype } "
99
- )
100
109
101
- ht , _ = self .unary_fn_ (x , out , sycl_queue = exec_q )
102
- ht .wait ()
110
+ ht_unary_ev , unary_ev = self .unary_fn_ (x , out , sycl_queue = exec_q )
111
+
112
+ if not (orig_out is None or orig_out is out ):
113
+ # Copy the out data from temporary buffer to original memory
114
+ ht_copy_ev , _ = ti ._copy_usm_ndarray_into_usm_ndarray (
115
+ src = out , dst = orig_out , sycl_queue = exec_q , depends = [unary_ev ]
116
+ )
117
+ ht_copy_ev .wait ()
118
+ out = orig_out
103
119
120
+ ht_unary_ev .wait ()
104
121
return out
122
+
105
123
if order == "K" :
106
124
buf = _empty_like_orderK (x , buf_dt )
107
125
else :
@@ -117,11 +135,6 @@ def __call__(self, x, out=None, order="K"):
117
135
out = _empty_like_orderK (buf , res_dt )
118
136
else :
119
137
out = dpt .empty_like (buf , dtype = res_dt , order = order )
120
- else :
121
- if buf_dt != out .dtype :
122
- raise TypeError (
123
- f"Output array of type { buf_dt } is needed, got { out .dtype } "
124
- )
125
138
126
139
ht , _ = self .unary_fn_ (buf , out , sycl_queue = exec_q , depends = [copy_ev ])
127
140
ht_copy_ev .wait ()
0 commit comments