Skip to content

Commit 87ba8c6

Browse files
authored
Merge branch 'master' into update_reshape
2 parents da31797 + 48515c8 commit 87ba8c6

File tree

5 files changed

+167
-131
lines changed

5 files changed

+167
-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: 130 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,82 @@
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 = "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+
100176
def arange(
101177
start,
102178
/,
@@ -1206,7 +1282,7 @@ def empty_like(
12061282
/,
12071283
*,
12081284
dtype=None,
1209-
order="C",
1285+
order="K",
12101286
subok=False,
12111287
shape=None,
12121288
device=None,
@@ -1227,9 +1303,10 @@ def empty_like(
12271303
The desired dtype for the array, e.g., dpnp.int32.
12281304
Default is the default floating point data type for the device where
12291305
input array is allocated.
1230-
order : {None, "C", "F"}, optional
1306+
order : {None, "C", "F", "A", "K"}, optional
12311307
Memory layout of the newly output array.
1232-
Default: ``"C"``.
1308+
``order=None`` is an alias for ``order="K"``.
1309+
Default: ``"K"``.
12331310
shape : {None, int, sequence of ints}
12341311
Overrides the shape of the result.
12351312
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -1256,8 +1333,6 @@ def empty_like(
12561333
12571334
Limitations
12581335
-----------
1259-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
1260-
``None``.
12611336
Parameter `subok` is supported only with default value ``False``.
12621337
Otherwise, the function raises `NotImplementedError` exception.
12631338
@@ -1295,20 +1370,16 @@ def empty_like(
12951370
"""
12961371

12971372
dpnp.check_supported_arrays_type(a)
1298-
dpnp.check_limitations(order=order, subok=subok)
1373+
dpnp.check_limitations(subok=subok)
12991374

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,
13091378
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,
13121383
)
13131384

13141385

@@ -2063,7 +2134,7 @@ def full_like(
20632134
fill_value,
20642135
*,
20652136
dtype=None,
2066-
order="C",
2137+
order="K",
20672138
subok=False,
20682139
shape=None,
20692140
device=None,
@@ -2088,9 +2159,10 @@ def full_like(
20882159
The desired dtype for the array, e.g., dpnp.int32.
20892160
Default is the default floating point data type for the device where
20902161
input array is allocated.
2091-
order : {None, "C", "F"}, optional
2162+
order : {None, "C", "F", "A", "K"}, optional
20922163
Memory layout of the newly output array.
2093-
Default: ``"C"``.
2164+
``order=None`` is an alias for ``order="K"``.
2165+
Default: ``"K"``.
20942166
shape : {None, int, sequence of ints}
20952167
Overrides the shape of the result.
20962168
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -2117,8 +2189,6 @@ def full_like(
21172189
21182190
Limitations
21192191
-----------
2120-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
2121-
``None``.
21222192
Parameter `subok` is supported only with default value ``False``.
21232193
Otherwise, the function raises `NotImplementedError` exception.
21242194
@@ -2156,23 +2226,19 @@ def full_like(
21562226
"""
21572227

21582228
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)
21672230

2168-
return dpnp_container.full(
2169-
_shape,
2170-
fill_value,
2171-
dtype=_dtype,
2231+
res = _get_empty_array(
2232+
a,
2233+
dtype=dtype,
21722234
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,
21752239
)
2240+
dpnp.copyto(res, fill_value, casting="unsafe")
2241+
return res
21762242

21772243

21782244
def geomspace(
@@ -3112,7 +3178,7 @@ def ones_like(
31123178
/,
31133179
*,
31143180
dtype=None,
3115-
order="C",
3181+
order="K",
31163182
subok=False,
31173183
shape=None,
31183184
device=None,
@@ -3133,9 +3199,10 @@ def ones_like(
31333199
The desired dtype for the array, e.g., dpnp.int32.
31343200
Default is the default floating point data type for the device where
31353201
input array is allocated.
3136-
order : {None, "C", "F"}, optional
3202+
order : {None, "C", "F", "A", "K"}, optional
31373203
Memory layout of the newly output array.
3138-
Default: ``"C"``.
3204+
``order=None`` is an alias for ``order="K"``.
3205+
Default: ``"K"``.
31393206
shape : {None, int, sequence of ints}
31403207
Overrides the shape of the result.
31413208
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3162,8 +3229,6 @@ def ones_like(
31623229
31633230
Limitations
31643231
-----------
3165-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3166-
``None``.
31673232
Parameter `subok` is supported only with default value ``False``.
31683233
Otherwise, the function raises `NotImplementedError` exception.
31693234
@@ -3202,21 +3267,19 @@ def ones_like(
32023267
32033268
"""
32043269
dpnp.check_supported_arrays_type(a)
3205-
dpnp.check_limitations(order=order, subok=subok)
3270+
dpnp.check_limitations(subok=subok)
32063271

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,
32163275
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,
32193280
)
3281+
res.fill(1)
3282+
return res
32203283

32213284

32223285
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
@@ -3759,7 +3822,7 @@ def zeros_like(
37593822
/,
37603823
*,
37613824
dtype=None,
3762-
order="C",
3825+
order="K",
37633826
subok=False,
37643827
shape=None,
37653828
device=None,
@@ -3780,9 +3843,10 @@ def zeros_like(
37803843
The desired dtype for the array, e.g., dpnp.int32.
37813844
Default is the default floating point data type for the device where
37823845
input array is allocated.
3783-
order : {None, "C", "F"}, optional
3846+
order : {None, "C", "F", "A", "K"}, optional
37843847
Memory layout of the newly output array.
3785-
Default: ``"C"``.
3848+
``order=None`` is an alias for ``order="K"``.
3849+
Default: ``"K"``.
37863850
shape : {None, int, sequence of ints}
37873851
Overrides the shape of the result.
37883852
device : {None, string, SyclDevice, SyclQueue}, optional
@@ -3809,8 +3873,6 @@ def zeros_like(
38093873
38103874
Limitations
38113875
-----------
3812-
Parameter `order` is supported only with values ``"C"``, ``"F"`` and
3813-
``None``.
38143876
Parameter `subok` is supported only with default value ``False``.
38153877
Otherwise, the function raises `NotImplementedError` exception.
38163878
@@ -3850,18 +3912,16 @@ def zeros_like(
38503912
"""
38513913

38523914
dpnp.check_supported_arrays_type(a)
3853-
dpnp.check_limitations(order=order, subok=subok)
3915+
dpnp.check_limitations(subok=subok)
38543916

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,
38643920
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,
38673925
)
3926+
res.fill(0)
3927+
return res

0 commit comments

Comments
 (0)