Skip to content

task: add broadcast class implementation - #2901

Open
jharlow-intel wants to merge 5 commits into
masterfrom
task/SAT-7028
Open

task: add broadcast class implementation#2901
jharlow-intel wants to merge 5 commits into
masterfrom
task/SAT-7028

Conversation

@jharlow-intel

@jharlow-intel jharlow-intel commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Adds a broadcast class implementation

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@jharlow-intel jharlow-intel self-assigned this May 6, 2026
@jharlow-intel jharlow-intel added the enhancement New feature or request label May 6, 2026
@jharlow-intel jharlow-intel changed the title task: add boradcast class implementation task: add broadcast class implementation May 6, 2026
@jharlow-intel
jharlow-intel marked this pull request as draft May 6, 2026 19:53
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/2901/index.html

Comment thread dpnp/dpnp_broadcast.py Outdated
Comment thread dpnp/dpnp_broadcast.py Outdated
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev3=py314h509198e_41 ran successfully.
Passed: 1377
Failed: 0
Skipped: 5

@jharlow-intel
jharlow-intel requested a review from antonwolfy May 26, 2026 19:25
Comment thread dpnp/dpnp_broadcast.py Outdated
Comment on lines +92 to +100
if len(dpnp_arrays) > 1:
exec_q = dpt.get_execution_queue(
tuple(array.sycl_queue for array in dpnp_arrays)
)
if exec_q is None:
raise dpt.ExecutionPlacementError(
"Execution placement can not be unambiguously inferred "
"from input arguments."
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to check for compute follows data here? The arrays can be broadcast even if they aren't, it just means device routines can't be run with both as inputs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do keep this check, we would need to check if they are dpnp.tensor.usm_ndarray as well

Comment thread dpnp/dpnp_broadcast.py
The number of iterators.

"""
return len(self._arrays)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

primarily a question for @antonwolfy and @vlad-perevezentsev since it's design-related, but NumPy and CuPy differ drastically in this class implementation

https://numpy.org/doc/2.1/reference/generated/numpy.broadcast.html
https://docs.cupy.dev/en/latest/reference/generated/cupy.broadcast.html

do we want more of the CuPy or NumPy behavior? What is the intended use-case of this class to users?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we are not going to fully align with NumPy here, aligning with CuPy looks more preferable as for me, because might help in case of CuPy to DPNP migration for some users.

In that perspective, I'd keep the current implement as it is for now, plus adding values property, which mimics CuPy.

@jharlow-intel
jharlow-intel marked this pull request as ready for review August 7, 2026 14:21
@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.434% (+0.2%) from 78.242% — task/SAT-7028 into master

Comment thread dpnp/dpnp_broadcast.py
Comment on lines +100 to +102
self._size = 1
for dim in self._shape:
self._size *= dim

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use math.prod:

Suggested change
self._size = 1
for dim in self._shape:
self._size *= dim
self._size = math.prod(self._shape)

Comment thread dpnp/dpnp_broadcast.py
The number of iterators.

"""
return len(self._arrays)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we are not going to fully align with NumPy here, aligning with CuPy looks more preferable as for me, because might help in case of CuPy to DPNP migration for some users.

In that perspective, I'd keep the current implement as it is for now, plus adding values property, which mimics CuPy.

Comment thread dpnp/dpnp_broadcast.py

See Also
--------
:obj:`dpnp.broadcast_arrays` : Broadcast any number of arrays against

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing back-lins on dpnp.broadcast in docstrings of all three functions below.

Comment thread CHANGELOG.md
Comment on lines +12 to 13
* Added `dpnp.broadcast` class implementation [#2901](https://github.com/IntelPython/dpnp/pull/2901)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank empty line:

Suggested change
* Added `dpnp.broadcast` class implementation [#2901](https://github.com/IntelPython/dpnp/pull/2901)
* Added `dpnp.broadcast` class implementation [#2901](https://github.com/IntelPython/dpnp/pull/2901)

Comment thread dpnp/dpnp_broadcast.py
>>> b.size
9

Limitations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limitations and Notes should be before Examples

Comment thread dpnp/dpnp_broadcast.py
self._size *= dim
self._nd = len(self._shape)

@property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to add members:

   broadcast.shape
   broadcast.size
   broadcast.nd
   broadcast.ndim
   broadcast.numiter

to array-manipulation.rst to make them rendered

Comment thread dpnp/dpnp_broadcast.py
from dpnp.tensor._manipulation_functions import _broadcast_shapes


class broadcast:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a new file dpnp/dpnp_broadcast.py. The class can be a part of existing dpnp/dpnp_iface_manipulation.py which currently holds all the broadcast functions

Comment thread dpnp/dpnp_broadcast.py
return self._size

@property
def nd(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need nd method, because NumPy states that ndim is preferable:

Number of dimensions of broadcasted result. For code intended for NumPy 1.12.0 and later the more consistent ndim is preferred.

Comment thread dpnp/dpnp_broadcast.py
-------
out : tuple
A tuple containing the shape of the broadcasted result.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Examples sections for the methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants