97
97
]
98
98
99
99
100
+ def _get_empty_array (
101
+ a ,
102
+ / ,
103
+ * ,
104
+ dtype = None ,
105
+ order = "K" ,
106
+ shape = None ,
107
+ device = None ,
108
+ usm_type = None ,
109
+ sycl_queue = None ,
110
+ ):
111
+ """
112
+ Get an empty array as the base for empty_like, ones_like, zeros_like,
113
+ and full_like.
114
+
115
+ """
116
+ strides = None
117
+ if shape is None :
118
+ _shape = a .shape
119
+ elif dpnp .isscalar (shape ):
120
+ _shape = (shape ,)
121
+ else :
122
+ _shape = shape
123
+ _dtype = a .dtype if dtype is None else dtype
124
+ _usm_type = a .usm_type if usm_type is None else usm_type
125
+ _sycl_queue = dpnp .get_normalized_queue_device (
126
+ a , sycl_queue = sycl_queue , device = device
127
+ )
128
+
129
+ if order is None :
130
+ order = "K"
131
+ if order in "aA" :
132
+ if a .flags .fnc :
133
+ order = "F"
134
+ else :
135
+ order = "C"
136
+ elif order in "kK" :
137
+ if len (_shape ) != a .ndim :
138
+ order = "C"
139
+ elif a .flags .f_contiguous :
140
+ order = "F"
141
+ elif a .flags .c_contiguous :
142
+ order = "C"
143
+ else :
144
+ strides = _get_strides_for_order_k (a , _shape )
145
+ order = "C"
146
+ elif order not in "cfCF" :
147
+ raise ValueError (
148
+ f"order must be None, 'C', 'F', 'A', or 'K' (got '{ order } ')"
149
+ )
150
+
151
+ return dpnp_array (
152
+ _shape ,
153
+ dtype = _dtype ,
154
+ strides = strides ,
155
+ order = order ,
156
+ usm_type = _usm_type ,
157
+ sycl_queue = _sycl_queue ,
158
+ )
159
+
160
+
161
+ def _get_strides_for_order_k (x , shape = None ):
162
+ """
163
+ Calculate strides when order='K' for empty_like, ones_like, zeros_like,
164
+ and full_like where `shape` is ``None`` or len(shape) == x.ndim.
165
+
166
+ """
167
+ stride_and_index = sorted ([(abs (s ), - i ) for i , s in enumerate (x .strides )])
168
+ strides = [0 ] * x .ndim
169
+ stride = 1
170
+ for _ , i in stride_and_index :
171
+ strides [- i ] = stride
172
+ stride *= shape [- i ] if shape else x .shape [- i ]
173
+ return strides
174
+
175
+
100
176
def arange (
101
177
start ,
102
178
/ ,
@@ -1206,7 +1282,7 @@ def empty_like(
1206
1282
/ ,
1207
1283
* ,
1208
1284
dtype = None ,
1209
- order = "C " ,
1285
+ order = "K " ,
1210
1286
subok = False ,
1211
1287
shape = None ,
1212
1288
device = None ,
@@ -1227,9 +1303,10 @@ def empty_like(
1227
1303
The desired dtype for the array, e.g., dpnp.int32.
1228
1304
Default is the default floating point data type for the device where
1229
1305
input array is allocated.
1230
- order : {None, "C", "F"}, optional
1306
+ order : {None, "C", "F", "A", "K" }, optional
1231
1307
Memory layout of the newly output array.
1232
- Default: ``"C"``.
1308
+ ``order=None`` is an alias for ``order="K"``.
1309
+ Default: ``"K"``.
1233
1310
shape : {None, int, sequence of ints}
1234
1311
Overrides the shape of the result.
1235
1312
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -1256,8 +1333,6 @@ def empty_like(
1256
1333
1257
1334
Limitations
1258
1335
-----------
1259
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
1260
- ``None``.
1261
1336
Parameter `subok` is supported only with default value ``False``.
1262
1337
Otherwise, the function raises `NotImplementedError` exception.
1263
1338
@@ -1295,20 +1370,16 @@ def empty_like(
1295
1370
"""
1296
1371
1297
1372
dpnp .check_supported_arrays_type (a )
1298
- dpnp .check_limitations (order = order , subok = subok )
1373
+ dpnp .check_limitations (subok = subok )
1299
1374
1300
- _shape = a .shape if shape is None else shape
1301
- _dtype = a .dtype if dtype is None else dtype
1302
- _usm_type = a .usm_type if usm_type is None else usm_type
1303
- _sycl_queue = dpnp .get_normalized_queue_device (
1304
- a , sycl_queue = sycl_queue , device = device
1305
- )
1306
- return dpnp_container .empty (
1307
- _shape ,
1308
- dtype = _dtype ,
1375
+ return _get_empty_array (
1376
+ a ,
1377
+ dtype = dtype ,
1309
1378
order = order ,
1310
- usm_type = _usm_type ,
1311
- sycl_queue = _sycl_queue ,
1379
+ shape = shape ,
1380
+ device = device ,
1381
+ usm_type = usm_type ,
1382
+ sycl_queue = sycl_queue ,
1312
1383
)
1313
1384
1314
1385
@@ -2063,7 +2134,7 @@ def full_like(
2063
2134
fill_value ,
2064
2135
* ,
2065
2136
dtype = None ,
2066
- order = "C " ,
2137
+ order = "K " ,
2067
2138
subok = False ,
2068
2139
shape = None ,
2069
2140
device = None ,
@@ -2088,9 +2159,10 @@ def full_like(
2088
2159
The desired dtype for the array, e.g., dpnp.int32.
2089
2160
Default is the default floating point data type for the device where
2090
2161
input array is allocated.
2091
- order : {None, "C", "F"}, optional
2162
+ order : {None, "C", "F", "A", "K" }, optional
2092
2163
Memory layout of the newly output array.
2093
- Default: ``"C"``.
2164
+ ``order=None`` is an alias for ``order="K"``.
2165
+ Default: ``"K"``.
2094
2166
shape : {None, int, sequence of ints}
2095
2167
Overrides the shape of the result.
2096
2168
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -2117,8 +2189,6 @@ def full_like(
2117
2189
2118
2190
Limitations
2119
2191
-----------
2120
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
2121
- ``None``.
2122
2192
Parameter `subok` is supported only with default value ``False``.
2123
2193
Otherwise, the function raises `NotImplementedError` exception.
2124
2194
@@ -2156,23 +2226,19 @@ def full_like(
2156
2226
"""
2157
2227
2158
2228
dpnp .check_supported_arrays_type (a )
2159
- dpnp .check_limitations (order = order , subok = subok )
2160
-
2161
- _shape = a .shape if shape is None else shape
2162
- _dtype = a .dtype if dtype is None else dtype
2163
- _usm_type = a .usm_type if usm_type is None else usm_type
2164
- _sycl_queue = dpnp .get_normalized_queue_device (
2165
- a , sycl_queue = sycl_queue , device = device
2166
- )
2229
+ dpnp .check_limitations (subok = subok )
2167
2230
2168
- return dpnp_container .full (
2169
- _shape ,
2170
- fill_value ,
2171
- dtype = _dtype ,
2231
+ res = _get_empty_array (
2232
+ a ,
2233
+ dtype = dtype ,
2172
2234
order = order ,
2173
- usm_type = _usm_type ,
2174
- sycl_queue = _sycl_queue ,
2235
+ shape = shape ,
2236
+ device = device ,
2237
+ usm_type = usm_type ,
2238
+ sycl_queue = sycl_queue ,
2175
2239
)
2240
+ dpnp .copyto (res , fill_value , casting = "unsafe" )
2241
+ return res
2176
2242
2177
2243
2178
2244
def geomspace (
@@ -3112,7 +3178,7 @@ def ones_like(
3112
3178
/ ,
3113
3179
* ,
3114
3180
dtype = None ,
3115
- order = "C " ,
3181
+ order = "K " ,
3116
3182
subok = False ,
3117
3183
shape = None ,
3118
3184
device = None ,
@@ -3133,9 +3199,10 @@ def ones_like(
3133
3199
The desired dtype for the array, e.g., dpnp.int32.
3134
3200
Default is the default floating point data type for the device where
3135
3201
input array is allocated.
3136
- order : {None, "C", "F"}, optional
3202
+ order : {None, "C", "F", "A", "K" }, optional
3137
3203
Memory layout of the newly output array.
3138
- Default: ``"C"``.
3204
+ ``order=None`` is an alias for ``order="K"``.
3205
+ Default: ``"K"``.
3139
3206
shape : {None, int, sequence of ints}
3140
3207
Overrides the shape of the result.
3141
3208
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3162,8 +3229,6 @@ def ones_like(
3162
3229
3163
3230
Limitations
3164
3231
-----------
3165
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3166
- ``None``.
3167
3232
Parameter `subok` is supported only with default value ``False``.
3168
3233
Otherwise, the function raises `NotImplementedError` exception.
3169
3234
@@ -3202,21 +3267,19 @@ def ones_like(
3202
3267
3203
3268
"""
3204
3269
dpnp .check_supported_arrays_type (a )
3205
- dpnp .check_limitations (order = order , subok = subok )
3270
+ dpnp .check_limitations (subok = subok )
3206
3271
3207
- _shape = a .shape if shape is None else shape
3208
- _dtype = a .dtype if dtype is None else dtype
3209
- _usm_type = a .usm_type if usm_type is None else usm_type
3210
- _sycl_queue = dpnp .get_normalized_queue_device (
3211
- a , sycl_queue = sycl_queue , device = device
3212
- )
3213
- return dpnp_container .ones (
3214
- _shape ,
3215
- dtype = _dtype ,
3272
+ res = _get_empty_array (
3273
+ a ,
3274
+ dtype = dtype ,
3216
3275
order = order ,
3217
- usm_type = _usm_type ,
3218
- sycl_queue = _sycl_queue ,
3276
+ shape = shape ,
3277
+ device = device ,
3278
+ usm_type = usm_type ,
3279
+ sycl_queue = sycl_queue ,
3219
3280
)
3281
+ res .fill (1 )
3282
+ return res
3220
3283
3221
3284
3222
3285
def trace (a , offset = 0 , axis1 = 0 , axis2 = 1 , dtype = None , out = None ):
@@ -3759,7 +3822,7 @@ def zeros_like(
3759
3822
/ ,
3760
3823
* ,
3761
3824
dtype = None ,
3762
- order = "C " ,
3825
+ order = "K " ,
3763
3826
subok = False ,
3764
3827
shape = None ,
3765
3828
device = None ,
@@ -3780,9 +3843,10 @@ def zeros_like(
3780
3843
The desired dtype for the array, e.g., dpnp.int32.
3781
3844
Default is the default floating point data type for the device where
3782
3845
input array is allocated.
3783
- order : {None, "C", "F"}, optional
3846
+ order : {None, "C", "F", "A", "K" }, optional
3784
3847
Memory layout of the newly output array.
3785
- Default: ``"C"``.
3848
+ ``order=None`` is an alias for ``order="K"``.
3849
+ Default: ``"K"``.
3786
3850
shape : {None, int, sequence of ints}
3787
3851
Overrides the shape of the result.
3788
3852
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3809,8 +3873,6 @@ def zeros_like(
3809
3873
3810
3874
Limitations
3811
3875
-----------
3812
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3813
- ``None``.
3814
3876
Parameter `subok` is supported only with default value ``False``.
3815
3877
Otherwise, the function raises `NotImplementedError` exception.
3816
3878
@@ -3850,18 +3912,16 @@ def zeros_like(
3850
3912
"""
3851
3913
3852
3914
dpnp .check_supported_arrays_type (a )
3853
- dpnp .check_limitations (order = order , subok = subok )
3915
+ dpnp .check_limitations (subok = subok )
3854
3916
3855
- _shape = a .shape if shape is None else shape
3856
- _dtype = a .dtype if dtype is None else dtype
3857
- _usm_type = a .usm_type if usm_type is None else usm_type
3858
- _sycl_queue = dpnp .get_normalized_queue_device (
3859
- a , sycl_queue = sycl_queue , device = device
3860
- )
3861
- return dpnp_container .zeros (
3862
- _shape ,
3863
- dtype = _dtype ,
3917
+ res = _get_empty_array (
3918
+ a ,
3919
+ dtype = dtype ,
3864
3920
order = order ,
3865
- usm_type = _usm_type ,
3866
- sycl_queue = _sycl_queue ,
3921
+ shape = shape ,
3922
+ device = device ,
3923
+ usm_type = usm_type ,
3924
+ sycl_queue = sycl_queue ,
3867
3925
)
3926
+ res .fill (0 )
3927
+ return res
0 commit comments