-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add broadcast_shapes to the specification
#983
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
base: main
Are you sure you want to change the base?
Conversation
rgommers
left a comment
There was a problem hiding this 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
the array-api-extra docs just say |
ev-br
left a comment
There was a problem hiding this 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.
| ----- | ||
| - If not provided one or more arguments, the function **must** return an empty tuple. | ||
| - 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), array-conforming libraries **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``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've to admit I'm not exactly sure what this clause means for a library that does not support a sentinel value. E.g., what must numpy do for inputs of (1, 2, None) and (1, 2, 1)?
- does it raise a ValueError since the
broadcastingsection does not mentionNone(hence shapes are not broadcast-compatible), per theRaisessection? - or does it propagate the
None, per this clause?
Maybe a way out is to phrase it, roughly, "An array library may accept a value other than integer (e.g. None) for one or multiple dimensions. This is common for libraries that build computational graphs, and is used to indicate a dimension of unknown size. Array-conforming libraries which accept non-integer values for unknown dimensions must propagate such values...."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numpy will raise ValueError. Your proposed phrasing looks good to me.

This PR
broadcast_shapesto the specification #893 by adding support forbroadcast_shapesto the specification.Nonefor a dimension of unknown size, behavior is left unspecified and thus implementation-defined.