Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC]: add broadcast_shapes to the specification #893

Open
crusaderky opened this issue Feb 5, 2025 · 5 comments
Open

[RFC]: add broadcast_shapes to the specification #893

crusaderky opened this issue Feb 5, 2025 · 5 comments
Labels
Needs Discussion Needs further discussion. RFC Request for comments. Feature requests and proposed changes. topic: Broadcasting Array broadcasting.
Milestone

Comments

@crusaderky
Copy link

crusaderky commented Feb 5, 2025

This RFC proposes adding an API to the specification for explicitly broadcasting a list of shapes to a single shape.

Overview

Based on array API comparison data, this API, or some variation of it, is commonly implemented across array libraries.

Currently, the Array API specification only includes broadcast_arrays and broadcast_to which both require array input. The specification lacks APIs for working directly with shapes without needing to create new array instances.

Prior Art

Proposal

This RFC proposes adding the following API to the specification:

def broadcast_shapes(*shapes: tuple[int | None, ...]) → tuple[int | None, ...]

in which one or more shapes are broadcasted together according to broadcasting rules as enumerated in the specification.

Questions

  • How to handle shapes having unknown dimensions?

    • dask.array.core.broadcast_shapes sets the output size to nan if any of the input shapes are nan on the same axis
    • ndonnx.broadcast_arrays(a, b) returns arrays with material shapes.

    Note that shape materialization can be a very expensive operation, as it requires materializing the whole graph until that point. In the case of Dask, which doesn't cache intermediate results as a deliberate memory management policy, this means computing everything at least twice.

Notes

The top-level page on broadcasting mentions on the first line, using non-prescriptive language, that broadcasting allows creating views of the inputs:

Broadcasting refers to the automatic (implicit) expansion of array dimensions to be of equal sizes without copying array data

However, no mention of sharing memory is made in broadcast_to or broadcast_arrays.

For the sake of comparison, see the verbiage in asarray(copy=False).

The problem with this ambiguity is that one can work around the lack of broadcast_shapes by calling xp.broadcast_arrays(*args)[0].shape, but there is no strong guarantee that the backend won't deep-copy the inputs.

Note that numpy.broadcast_shapes doesn't work with shapes containing None (ndonnx and hopefully in the future JAX too) or NaN (Dask; non-standard).

I suggest to either

  • Add prescriptive verbiage to broadcast_to and broadcast_arrays that the output must share memory with the input, or in other words the operation must be O(1), or
  • Add broadcast_shapes to the standard, and change the verbiage of the broadcasting high level page to "typically without copying array data"

For the time being I am adding the function to array_api_extra:

@kgryte
Copy link
Contributor

kgryte commented Feb 6, 2025

@crusaderky Would you mind sharing your use case for needing broadcast_shapes? (i.e., where you want to determine a shape, without actually broadcasting an array)

@kgryte kgryte added Needs Discussion Needs further discussion. topic: Broadcasting Array broadcasting. labels Feb 6, 2025
@crusaderky
Copy link
Author

crusaderky commented Feb 6, 2025

@kgryte I'm writing a backend-agnostic wrapper around jax.pure_callback / dask.blockwise / equivalent functions (ndonnx?) that let you apply an arbitrary eager callback to a lazy array: data-apis/array-api-extra#86
This requires me to know the output shape well before the inputs will be used for anything.

@kgryte kgryte changed the title broadcast_shapes; broadcasting guarantees about views [RFC]: add broadcast_shapes to the specification Feb 6, 2025
@kgryte kgryte added the RFC Request for comments. Feature requests and proposed changes. label Feb 6, 2025
@rgommers
Copy link
Member

rgommers commented Feb 6, 2025

It would be useful to add to the issue description which array libraries do or don't support broadcast_shapes, and if they do whether their signatures and behavior are all compatible.

@rgommers
Copy link
Member

rgommers commented Feb 6, 2025

Add prescriptive verbiage to broadcast_to and broadcast_arrays that the output must share memory with the input, or in other words the operation must be O(1), or

That's a performance/implementation question - as a rule we never specify that, only syntax and semantics. It might not be true for all implementations (e.g., JAX in eager mode).

there is no strong guarantee that the backend won't deep-copy the inputs.

One shouldn't rely on such an assumption.

The problem with this ambiguity is that one can work around the lack of broadcast_shapes by calling xp.broadcast_arrays(*args)[0].shape, there is no strong guarantee that the backend won't deep-copy the inputs.

This syntax in itself is a good indication that broadcast_shapes is a useful thing to have. xp.broadcast_arrays(*args)[0].shape is a pretty clunky workaround - I wouldn't worry about performance here (libraries where performance matters will do the fast thing), but about the maintainability/readability of code.

For the time being I am adding the function to array_api_extra:

That sounds like a good idea to me. Based on usage frequency and library support it can be "promoted" to a standardized function afterwards.

@kgryte
Copy link
Contributor

kgryte commented Feb 6, 2025

@rgommers I've updated the OP with a comparison across libraries.

@kgryte kgryte added this to the v2025 milestone Feb 6, 2025
@kgryte kgryte added this to Proposals Feb 6, 2025
@kgryte kgryte moved this to Stage 0 in Proposals Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Discussion Needs further discussion. RFC Request for comments. Feature requests and proposed changes. topic: Broadcasting Array broadcasting.
Projects
Status: Stage 0
Development

No branches or pull requests

3 participants