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 = "C"
131
+ elif order == "A" :
132
+ if a .flags .fnc :
133
+ order = "F"
134
+ else :
135
+ order = "C"
136
+ elif order == "K" :
137
+ if len (_shape ) != a .ndim :
138
+ order = "C"
139
+ elif a .flags .f_contiguous :
140
+ order = "F"
141
+ elif a .flags .f_contiguous :
142
+ order = "C"
143
+ else :
144
+ strides = _get_strides_for_order_k (a , _shape )
145
+ order = "C"
146
+
147
+ return dpnp_array (
148
+ _shape ,
149
+ dtype = _dtype ,
150
+ strides = strides ,
151
+ order = order ,
152
+ usm_type = _usm_type ,
153
+ sycl_queue = _sycl_queue ,
154
+ )
155
+
156
+
157
+ def _get_strides_for_order_k (x , shape = None ):
158
+ """
159
+ Calculate strides when order='K' for empty_like, ones_like, zeros_like,
160
+ and full_like where `shape` is ``None`` or len(shape) == x.ndim.
161
+
162
+ """
163
+ stride_and_index = sorted ([(abs (s ), - i ) for i , s in enumerate (x .strides )])
164
+ strides = [0 ] * x .ndim
165
+ stride = 1
166
+ for _ , i in stride_and_index :
167
+ strides [- i ] = stride
168
+ stride *= shape [- i ] if shape else x .shape [- i ]
169
+ return strides
170
+
171
+
100
172
def arange (
101
173
start ,
102
174
/ ,
@@ -1206,7 +1278,7 @@ def empty_like(
1206
1278
/ ,
1207
1279
* ,
1208
1280
dtype = None ,
1209
- order = "C " ,
1281
+ order = "K " ,
1210
1282
subok = False ,
1211
1283
shape = None ,
1212
1284
device = None ,
@@ -1227,9 +1299,9 @@ def empty_like(
1227
1299
The desired dtype for the array, e.g., dpnp.int32.
1228
1300
Default is the default floating point data type for the device where
1229
1301
input array is allocated.
1230
- order : {None, "C", "F"}, optional
1302
+ order : {None, "C", "F", "A", "K" }, optional
1231
1303
Memory layout of the newly output array.
1232
- Default: ``"C "``.
1304
+ Default: ``"K "``.
1233
1305
shape : {None, int, sequence of ints}
1234
1306
Overrides the shape of the result.
1235
1307
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -1256,8 +1328,6 @@ def empty_like(
1256
1328
1257
1329
Limitations
1258
1330
-----------
1259
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
1260
- ``None``.
1261
1331
Parameter `subok` is supported only with default value ``False``.
1262
1332
Otherwise, the function raises `NotImplementedError` exception.
1263
1333
@@ -1295,20 +1365,16 @@ def empty_like(
1295
1365
"""
1296
1366
1297
1367
dpnp .check_supported_arrays_type (a )
1298
- dpnp .check_limitations (order = order , subok = subok )
1368
+ dpnp .check_limitations (subok = subok )
1299
1369
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 ,
1370
+ return _get_empty_array (
1371
+ a ,
1372
+ dtype = dtype ,
1309
1373
order = order ,
1310
- usm_type = _usm_type ,
1311
- sycl_queue = _sycl_queue ,
1374
+ shape = shape ,
1375
+ device = device ,
1376
+ usm_type = usm_type ,
1377
+ sycl_queue = sycl_queue ,
1312
1378
)
1313
1379
1314
1380
@@ -2063,7 +2129,7 @@ def full_like(
2063
2129
fill_value ,
2064
2130
* ,
2065
2131
dtype = None ,
2066
- order = "C " ,
2132
+ order = "K " ,
2067
2133
subok = False ,
2068
2134
shape = None ,
2069
2135
device = None ,
@@ -2088,9 +2154,9 @@ def full_like(
2088
2154
The desired dtype for the array, e.g., dpnp.int32.
2089
2155
Default is the default floating point data type for the device where
2090
2156
input array is allocated.
2091
- order : {None, "C", "F"}, optional
2157
+ order : {None, "C", "F", "A", "K" }, optional
2092
2158
Memory layout of the newly output array.
2093
- Default: ``"C "``.
2159
+ Default: ``"K "``.
2094
2160
shape : {None, int, sequence of ints}
2095
2161
Overrides the shape of the result.
2096
2162
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -2117,8 +2183,6 @@ def full_like(
2117
2183
2118
2184
Limitations
2119
2185
-----------
2120
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
2121
- ``None``.
2122
2186
Parameter `subok` is supported only with default value ``False``.
2123
2187
Otherwise, the function raises `NotImplementedError` exception.
2124
2188
@@ -2156,23 +2220,19 @@ def full_like(
2156
2220
"""
2157
2221
2158
2222
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
- )
2223
+ dpnp .check_limitations (subok = subok )
2167
2224
2168
- return dpnp_container .full (
2169
- _shape ,
2170
- fill_value ,
2171
- dtype = _dtype ,
2225
+ res = _get_empty_array (
2226
+ a ,
2227
+ dtype = dtype ,
2172
2228
order = order ,
2173
- usm_type = _usm_type ,
2174
- sycl_queue = _sycl_queue ,
2229
+ shape = shape ,
2230
+ device = device ,
2231
+ usm_type = usm_type ,
2232
+ sycl_queue = sycl_queue ,
2175
2233
)
2234
+ dpnp .copyto (res , fill_value , casting = "unsafe" )
2235
+ return res
2176
2236
2177
2237
2178
2238
def geomspace (
@@ -3112,7 +3172,7 @@ def ones_like(
3112
3172
/ ,
3113
3173
* ,
3114
3174
dtype = None ,
3115
- order = "C " ,
3175
+ order = "K " ,
3116
3176
subok = False ,
3117
3177
shape = None ,
3118
3178
device = None ,
@@ -3133,9 +3193,9 @@ def ones_like(
3133
3193
The desired dtype for the array, e.g., dpnp.int32.
3134
3194
Default is the default floating point data type for the device where
3135
3195
input array is allocated.
3136
- order : {None, "C", "F"}, optional
3196
+ order : {None, "C", "F", "A", "K" }, optional
3137
3197
Memory layout of the newly output array.
3138
- Default: ``"C "``.
3198
+ Default: ``"K "``.
3139
3199
shape : {None, int, sequence of ints}
3140
3200
Overrides the shape of the result.
3141
3201
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3162,8 +3222,6 @@ def ones_like(
3162
3222
3163
3223
Limitations
3164
3224
-----------
3165
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3166
- ``None``.
3167
3225
Parameter `subok` is supported only with default value ``False``.
3168
3226
Otherwise, the function raises `NotImplementedError` exception.
3169
3227
@@ -3202,21 +3260,19 @@ def ones_like(
3202
3260
3203
3261
"""
3204
3262
dpnp .check_supported_arrays_type (a )
3205
- dpnp .check_limitations (order = order , subok = subok )
3263
+ dpnp .check_limitations (subok = subok )
3206
3264
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 ,
3265
+ res = _get_empty_array (
3266
+ a ,
3267
+ dtype = dtype ,
3216
3268
order = order ,
3217
- usm_type = _usm_type ,
3218
- sycl_queue = _sycl_queue ,
3269
+ shape = shape ,
3270
+ device = device ,
3271
+ usm_type = usm_type ,
3272
+ sycl_queue = sycl_queue ,
3219
3273
)
3274
+ res .fill (1 )
3275
+ return res
3220
3276
3221
3277
3222
3278
def trace (a , offset = 0 , axis1 = 0 , axis2 = 1 , dtype = None , out = None ):
@@ -3759,7 +3815,7 @@ def zeros_like(
3759
3815
/ ,
3760
3816
* ,
3761
3817
dtype = None ,
3762
- order = "C " ,
3818
+ order = "K " ,
3763
3819
subok = False ,
3764
3820
shape = None ,
3765
3821
device = None ,
@@ -3780,9 +3836,9 @@ def zeros_like(
3780
3836
The desired dtype for the array, e.g., dpnp.int32.
3781
3837
Default is the default floating point data type for the device where
3782
3838
input array is allocated.
3783
- order : {None, "C", "F"}, optional
3839
+ order : {None, "C", "F", "A", "K" }, optional
3784
3840
Memory layout of the newly output array.
3785
- Default: ``"C "``.
3841
+ Default: ``"K "``.
3786
3842
shape : {None, int, sequence of ints}
3787
3843
Overrides the shape of the result.
3788
3844
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3809,8 +3865,6 @@ def zeros_like(
3809
3865
3810
3866
Limitations
3811
3867
-----------
3812
- Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3813
- ``None``.
3814
3868
Parameter `subok` is supported only with default value ``False``.
3815
3869
Otherwise, the function raises `NotImplementedError` exception.
3816
3870
@@ -3850,18 +3904,16 @@ def zeros_like(
3850
3904
"""
3851
3905
3852
3906
dpnp .check_supported_arrays_type (a )
3853
- dpnp .check_limitations (order = order , subok = subok )
3907
+ dpnp .check_limitations (subok = subok )
3854
3908
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 ,
3909
+ res = _get_empty_array (
3910
+ a ,
3911
+ dtype = dtype ,
3864
3912
order = order ,
3865
- usm_type = _usm_type ,
3866
- sycl_queue = _sycl_queue ,
3913
+ shape = shape ,
3914
+ device = device ,
3915
+ usm_type = usm_type ,
3916
+ sycl_queue = sycl_queue ,
3867
3917
)
3918
+ res .fill (0 )
3919
+ return res
0 commit comments