Skip to content

Commit ae7cabd

Browse files
committed
update order
1 parent 178342c commit ae7cabd

File tree

4 files changed

+165
-131
lines changed

4 files changed

+165
-131
lines changed

dpnp/dpnp_iface.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,10 @@ def as_usm_ndarray(a, dtype=None, device=None, usm_type=None, sycl_queue=None):
320320
)
321321

322322

323-
def check_limitations(
324-
order=None, subok=False, like=None, initial=None, where=True
325-
):
323+
def check_limitations(subok=False, like=None, initial=None, where=True):
326324
"""
327325
Checking limitation kwargs for their supported values.
328326
329-
Parameter `order` is only supported with values ``"C"``, ``"F"``,
330-
and ``None``.
331327
Parameter `subok` is only supported with default value ``False``.
332328
Parameter `like` is only supported with default value ``None``.
333329
Parameter `initial` is only supported with default value ``None``.
@@ -340,16 +336,6 @@ def check_limitations(
340336
341337
"""
342338

343-
if order in ("A", "a", "K", "k"):
344-
raise NotImplementedError(
345-
"Keyword argument `order` is supported only with "
346-
f"values 'C' and 'F', but got '{order}'"
347-
)
348-
if order not in ("C", "c", "F", "f", None):
349-
raise ValueError(
350-
"Unrecognized `order` keyword value, expecting "
351-
f"'C' or 'F', but got '{order}'"
352-
)
353339
if like is not None:
354340
raise NotImplementedError(
355341
"Keyword argument `like` is supported only with "

dpnp/dpnp_iface_arraycreation.py

Lines changed: 122 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,78 @@
9797
]
9898

9999

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+
100172
def arange(
101173
start,
102174
/,
@@ -1206,7 +1278,7 @@ def empty_like(
12061278
/,
12071279
*,
12081280
dtype=None,
1209-
order="C",
1281+
order="K",
12101282
subok=False,
12111283
shape=None,
12121284
device=None,
@@ -1227,9 +1299,9 @@ def empty_like(
12271299
The desired dtype for the array, e.g., dpnp.int32.
12281300
Default is the default floating point data type for the device where
12291301
input array is allocated.
1230-
order : {None, "C", "F"}, optional
1302+
order : {None, "C", "F", "A", "K"}, optional
12311303
Memory layout of the newly output array.
1232-
Default: ``"C"``.
1304+
Default: ``"K"``.
12331305
shape : {None, int, sequence of ints}
12341306
Overrides the shape of the result.
12351307
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -1256,8 +1328,6 @@ def empty_like(
12561328
12571329
Limitations
12581330
-----------
1259-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
1260-
``None``.
12611331
Parameter `subok` is supported only with default value ``False``.
12621332
Otherwise, the function raises `NotImplementedError` exception.
12631333
@@ -1295,20 +1365,16 @@ def empty_like(
12951365
"""
12961366

12971367
dpnp.check_supported_arrays_type(a)
1298-
dpnp.check_limitations(order=order, subok=subok)
1368+
dpnp.check_limitations(subok=subok)
12991369

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,
13091373
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,
13121378
)
13131379

13141380

@@ -2063,7 +2129,7 @@ def full_like(
20632129
fill_value,
20642130
*,
20652131
dtype=None,
2066-
order="C",
2132+
order="K",
20672133
subok=False,
20682134
shape=None,
20692135
device=None,
@@ -2088,9 +2154,9 @@ def full_like(
20882154
The desired dtype for the array, e.g., dpnp.int32.
20892155
Default is the default floating point data type for the device where
20902156
input array is allocated.
2091-
order : {None, "C", "F"}, optional
2157+
order : {None, "C", "F", "A", "K"}, optional
20922158
Memory layout of the newly output array.
2093-
Default: ``"C"``.
2159+
Default: ``"K"``.
20942160
shape : {None, int, sequence of ints}
20952161
Overrides the shape of the result.
20962162
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -2117,8 +2183,6 @@ def full_like(
21172183
21182184
Limitations
21192185
-----------
2120-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
2121-
``None``.
21222186
Parameter `subok` is supported only with default value ``False``.
21232187
Otherwise, the function raises `NotImplementedError` exception.
21242188
@@ -2156,23 +2220,19 @@ def full_like(
21562220
"""
21572221

21582222
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)
21672224

2168-
return dpnp_container.full(
2169-
_shape,
2170-
fill_value,
2171-
dtype=_dtype,
2225+
res = _get_empty_array(
2226+
a,
2227+
dtype=dtype,
21722228
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,
21752233
)
2234+
dpnp.copyto(res, fill_value, casting="unsafe")
2235+
return res
21762236

21772237

21782238
def geomspace(
@@ -3112,7 +3172,7 @@ def ones_like(
31123172
/,
31133173
*,
31143174
dtype=None,
3115-
order="C",
3175+
order="K",
31163176
subok=False,
31173177
shape=None,
31183178
device=None,
@@ -3133,9 +3193,9 @@ def ones_like(
31333193
The desired dtype for the array, e.g., dpnp.int32.
31343194
Default is the default floating point data type for the device where
31353195
input array is allocated.
3136-
order : {None, "C", "F"}, optional
3196+
order : {None, "C", "F", "A", "K"}, optional
31373197
Memory layout of the newly output array.
3138-
Default: ``"C"``.
3198+
Default: ``"K"``.
31393199
shape : {None, int, sequence of ints}
31403200
Overrides the shape of the result.
31413201
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3162,8 +3222,6 @@ def ones_like(
31623222
31633223
Limitations
31643224
-----------
3165-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3166-
``None``.
31673225
Parameter `subok` is supported only with default value ``False``.
31683226
Otherwise, the function raises `NotImplementedError` exception.
31693227
@@ -3202,21 +3260,19 @@ def ones_like(
32023260
32033261
"""
32043262
dpnp.check_supported_arrays_type(a)
3205-
dpnp.check_limitations(order=order, subok=subok)
3263+
dpnp.check_limitations(subok=subok)
32063264

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,
32163268
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,
32193273
)
3274+
res.fill(1)
3275+
return res
32203276

32213277

32223278
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
@@ -3759,7 +3815,7 @@ def zeros_like(
37593815
/,
37603816
*,
37613817
dtype=None,
3762-
order="C",
3818+
order="K",
37633819
subok=False,
37643820
shape=None,
37653821
device=None,
@@ -3780,9 +3836,9 @@ def zeros_like(
37803836
The desired dtype for the array, e.g., dpnp.int32.
37813837
Default is the default floating point data type for the device where
37823838
input array is allocated.
3783-
order : {None, "C", "F"}, optional
3839+
order : {None, "C", "F", "A", "K"}, optional
37843840
Memory layout of the newly output array.
3785-
Default: ``"C"``.
3841+
Default: ``"K"``.
37863842
shape : {None, int, sequence of ints}
37873843
Overrides the shape of the result.
37883844
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3809,8 +3865,6 @@ def zeros_like(
38093865
38103866
Limitations
38113867
-----------
3812-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3813-
``None``.
38143868
Parameter `subok` is supported only with default value ``False``.
38153869
Otherwise, the function raises `NotImplementedError` exception.
38163870
@@ -3850,18 +3904,16 @@ def zeros_like(
38503904
"""
38513905

38523906
dpnp.check_supported_arrays_type(a)
3853-
dpnp.check_limitations(order=order, subok=subok)
3907+
dpnp.check_limitations(subok=subok)
38543908

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,
38643912
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,
38673917
)
3918+
res.fill(0)
3919+
return res

0 commit comments

Comments
 (0)