Skip to content

Conversation

@kgryte
Copy link
Contributor

@kgryte kgryte commented Dec 8, 2025

This PR

  • resolves RFC: add broadcast_shapes to the specification #893 by adding support for broadcast_shapes to the specification.
  • follows NumPy et al in supporting an arbitrary number of input shapes to be broadcasted.
  • specifies that only shapes which contain integers are explicitly supported. For shapes containing sentinel values such as None for a dimension of unknown size, behavior is left unspecified and thus implementation-defined.

@kgryte kgryte added API extension Adds new functions or objects to the API. topic: Manipulation Array manipulation and transformation. labels Dec 8, 2025
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @kgryte! Overall LGTM and seems good to add this function to the standard. A few comments with the "unknown shape" one the key thing to discuss.

- If not provided one or more arguments, the function **must** return an empty tuple.

.. note::
Array libraries which build computation graphs (e.g., ndonnx and Dask) commonly support shapes having dimensions of unknown size. If a shape contains a value other than an integer (e.g., ``None`` for a dimension of unknown size), behavior is unspecified and thus implementation-defined. Array-conforming libraries **may** choose to propagate such values (e.g., if a shape contains a dimension size of ``None``, the returned broadcasted shape also has a corresponding dimension having a size equal to ``None``) or raise an exception.
Copy link
Member

Choose a reason for hiding this comment

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

This can be phrased such that it works for computation graphs in the expected manner I believe, since the broadcasting math is abstract anyway and any sentinels can propagate correctly. To be discussed in the next call I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the copy to now be normative with regard to sentinel propagation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is the question regarding what happens when two shapes use different sentinels, but we can likely resolve this ambiguity in a future PR when someone asks for explicit clarification.

@lucascolley
Copy link
Member

Copy link
Member

@ev-br ev-br left a comment

Choose a reason for hiding this comment

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

One small suggestion about the unknown dimensions, LGTM otherwise.

@kgryte kgryte requested a review from ev-br February 2, 2026 07:56
-----

- If not provided one or more arguments, the function **must** return an empty tuple.
- An array-conforming library **may** accept a non-integer value (e.g., ``None``) for one or more dimensions. This is common for array libraries which build computation graphs (e.g., ndonnx and Dask) and which need to provide a mechanism for indicating dimensions of unknown size. Array-conforming libraries which accept non-integer values for dimensions of unknown size **must** propagate such values (e.g., if a shape contains a dimension size of ``None``, the returned broadcasted shape **must** also have a corresponding dimension having a size equal to ``None``); otherwise, an array-conforming library **must** raise a ``ValueError``.
Copy link
Member

Choose a reason for hiding this comment

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

Is this deliberately less strict than https://data-apis.org/array-api/draft/API_specification/generated/array_api.array.shape.html#shape, which seems to imply that None is the only acceptable non-integer value in a shape (at least where that value is supposed to represent an unknown dimension)?

E.g. this appears to be accommodating Dask's math.nan, suggesting that it should propagate that math.nan? That would differ from the existing behaviour in https://data-apis.org/array-api-extra/generated/array_api_extra.broadcast_shapes.html which replaces that non-standard math.nan with a None.

Suggested change
- An array-conforming library **may** accept a non-integer value (e.g., ``None``) for one or more dimensions. This is common for array libraries which build computation graphs (e.g., ndonnx and Dask) and which need to provide a mechanism for indicating dimensions of unknown size. Array-conforming libraries which accept non-integer values for dimensions of unknown size **must** propagate such values (e.g., if a shape contains a dimension size of ``None``, the returned broadcasted shape **must** also have a corresponding dimension having a size equal to ``None``); otherwise, an array-conforming library **must** raise a ``ValueError``.
- An array-conforming library **may** accept a ``None`` value for one or more shape dimensions. This is common for array libraries which build computation graphs (e.g., ndonnx and Dask) and which need to provide a mechanism for indicating dimensions of unknown size. Array-conforming libraries which accept ``None`` values for dimensions of unknown size **must** propagate those values (i.e., if a shape contains a dimension size of ``None``, the returned broadcasted shape **must** also have a corresponding dimension having a size equal to ``None``); otherwise, an array-conforming library **must** raise a ``ValueError``.

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

Labels

API extension Adds new functions or objects to the API. topic: Manipulation Array manipulation and transformation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: add broadcast_shapes to the specification

4 participants