Skip to content

Commit c6434f3

Browse files
committed
fix review
1 parent 5da8575 commit c6434f3

2 files changed

Lines changed: 32 additions & 37 deletions

File tree

dpnp/dpnp_broadcast.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"""Implementation of broadcast class."""
3030

3131
import dpnp
32-
import dpnp.tensor as dpt
3332
from dpnp.tensor._manipulation_functions import _broadcast_shapes
3433

3534

@@ -41,16 +40,15 @@ class broadcast:
4140
4241
Parameters
4342
----------
44-
*args : object
45-
Input parameters. Every argument must define ``shape`` attribute.
43+
*args : {dpnp.ndarray, usm_ndarray}
44+
Input arrays to broadcast against one another.
4645
4746
Returns
4847
-------
4948
broadcast : broadcast object
5049
Broadcast the input parameters against one another, and
5150
return an object that encapsulates the result.
52-
Amongst others, it has ``shape`` and ``nd`` properties, and
53-
may be used as an iterator.
51+
Amongst others, it has ``shape`` and ``nd`` properties.
5452
5553
See Also
5654
--------
@@ -73,32 +71,22 @@ class broadcast:
7371
>>> b.size
7472
9
7573
74+
Limitations
75+
-----------
76+
Input arrays are not coerced, so array-like objects and scalars are not
77+
supported and ``TypeError`` exception will be raised.
78+
7679
Notes
7780
-----
7881
Iterator functionality is not supported.
7982
8083
"""
8184

8285
def __init__(self, *args):
83-
for i, arg in enumerate(args):
84-
if not hasattr(arg, "shape"):
85-
raise TypeError(
86-
f"Argument at position {i} must define shape attribute"
87-
)
86+
dpnp.check_supported_arrays_type(*args)
8887

8988
self._arrays = tuple(args)
9089

91-
dpnp_arrays = [arg for arg in self._arrays if isinstance(arg, dpnp.ndarray)]
92-
if len(dpnp_arrays) > 1:
93-
exec_q = dpt.get_execution_queue(
94-
tuple(array.sycl_queue for array in dpnp_arrays)
95-
)
96-
if exec_q is None:
97-
raise dpt.ExecutionPlacementError(
98-
"Execution placement can not be unambiguously inferred "
99-
"from input arguments."
100-
)
101-
10290
if len(self._arrays) == 0:
10391
self._shape = ()
10492
self._size = 1

dpnp/tests/test_manipulation.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
get_unsigned_dtypes,
2525
has_support_aspect64,
2626
)
27-
from .third_party.cupy import testing
2827
from .tensor.helper import get_queue_or_skip
28+
from .third_party.cupy import testing
2929

3030

3131
def _compare_results(result, expected):
@@ -307,7 +307,7 @@ def test_no_copy(self):
307307
assert_array_equal(b, a)
308308

309309

310-
class TestBroadcast:
310+
class TestBroadcastShapes:
311311
@pytest.mark.parametrize(
312312
"shape",
313313
[
@@ -2095,12 +2095,14 @@ def test_broadcast_three_arrays(self):
20952095
assert bc.numiter == 3
20962096

20972097
def test_broadcast_ndim_property(self):
2098-
# Test that ndim property equals nd property
2098+
# Test that ndim property matches numpy and equals nd property
20992099
a = dpnp.array([[1, 2], [3, 4]])
21002100
b = dpnp.array([5, 6])
21012101

21022102
bc = dpnp.broadcast(a, b)
2103+
bc_np = numpy.broadcast(a.asnumpy(), b.asnumpy())
21032104

2105+
assert bc.ndim == bc_np.ndim
21042106
assert bc.ndim == bc.nd
21052107

21062108
def test_broadcast_complex_shapes(self):
@@ -2116,13 +2118,19 @@ def test_broadcast_complex_shapes(self):
21162118
assert bc.nd == bc_np.nd
21172119
assert bc.size == bc_np.size
21182120

2119-
def test_broadcast_with_array_like(self):
2120-
# Conversion from array-like inputs is not implemented for broadcast yet.
2121+
@pytest.mark.parametrize(
2122+
"arg",
2123+
[[[1], [2]], 3, numpy.ones((2, 1))],
2124+
ids=["list", "scalar", "numpy"],
2125+
)
2126+
def test_broadcast_unsupported_type(self, arg):
2127+
# unlike numpy, input arrays are not coerced, so array-like objects,
2128+
# scalars and host arrays are rejected the same way as they are by
2129+
# dpnp.broadcast_to and dpnp.broadcast_arrays
21212130
a = dpnp.array([1, 2, 3])
2122-
b = [[1], [2]]
21232131

21242132
with pytest.raises(TypeError):
2125-
dpnp.broadcast(a, b)
2133+
dpnp.broadcast(a, arg)
21262134

21272135
@pytest.mark.parametrize(
21282136
"shapes",
@@ -2171,21 +2179,20 @@ def test_broadcast_no_args(self):
21712179
assert bc.size == 1
21722180
assert bc.numiter == 0
21732181

2174-
def test_broadcast_argument_without_shape(self):
2175-
a = dpnp.array([1, 2, 3])
2176-
2177-
with pytest.raises(TypeError):
2178-
dpnp.broadcast(a, 3)
2179-
2180-
def test_broadcast_compute_follows_data(self):
2182+
def test_broadcast_different_queues(self):
2183+
# Broadcasting is a shape-only query, so inputs are not required
2184+
# to share a common execution placement
21812185
q1 = get_queue_or_skip()
21822186
q2 = get_queue_or_skip()
21832187

21842188
a = dpt.ones((2, 1), sycl_queue=q1)
21852189
b = dpt.ones((1, 2), sycl_queue=q2)
21862190

2187-
with pytest.raises(dpt.ExecutionPlacementError):
2188-
dpnp.broadcast(a, b)
2191+
bc = dpnp.broadcast(a, b)
2192+
2193+
assert bc.shape == (2, 2)
2194+
assert bc.size == 4
2195+
assert bc.nd == 2
21892196

21902197
def test_broadcast_repr(self):
21912198
# Test __repr__ method

0 commit comments

Comments
 (0)