-
Notifications
You must be signed in to change notification settings - Fork 53
Add element-wise function specifications, incl. type casting and broadcasting rules #12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
83fc81f
Add function interfaces
kgryte 22a0f95
Add functions
kgryte 2753c1c
Fix descriptions
kgryte f3d5cd6
Add rounding functions
kgryte 5fba6f1
Add links
kgryte 0f90c8f
Add heading links
kgryte 13e3758
Move notes up in document
kgryte 3b512d9
Add arithmetic operations
kgryte 4491793
Add domain and codomain
kgryte 668e9e2
Make positional-parameters positional-only
kgryte ddca794
Add domains and codomains
kgryte d7df3fd
Rename file and add definition
kgryte c707910
Experiment with subscripts
kgryte f5de1c4
Experiment with subscripts
kgryte 6e2d1f2
Use HTML subscripts
kgryte 7f37fcc
Update phrasing
kgryte 63ef729
Revert use of HTML markup for subscripts
kgryte 991eca6
Move terms, references, and conformance to purpose and scope document
kgryte f1fb7a5
Remove heading
kgryte f009e50
Add definition for broadcasting
kgryte 15422fe
Define compatible
kgryte 2f653ec
Add broadcasting document
kgryte 9caaff9
Fix markup
kgryte f200206
Update phrasing
kgryte 02de24e
Fix algorithm
kgryte 347973a
Update definition
kgryte b813d9b
Expand overview and add note regarding in-place semantics
kgryte 33cd2b7
Rename arrays
kgryte 5df7549
Rephrase
kgryte 3dd7288
Add clause
kgryte f599e2e
Add initial typing
kgryte 81ea06a
Merge branch 'master' of https://github.com/pydata-apis/array-api int…
kgryte 7c34785
Remove typing from signature
kgryte 11fa322
Update typing
kgryte 8701e7d
Add markup
kgryte 0c27737
Update typing
kgryte 7f00d03
Update formatting
kgryte 21ee999
Remove spaces
kgryte 8883026
Add types
kgryte be488d5
Update `out` requirements
kgryte 31f69db
Update types and descriptions
kgryte 8194b38
Update descriptions
kgryte 6100447
Update conventions
kgryte 8890def
Remove markup and update descriptions
kgryte b489749
Document broadcasting behavior
kgryte 4af802c
Update wording
kgryte f8ef97f
Rephrase
kgryte c4847d1
Add example
kgryte 18738cf
Update `out` type
kgryte 29a6a5d
Update descriptions
kgryte d1bb2ba
Update conventions
kgryte d9adebc
Link to broadcasting document
kgryte 16a0727
Add reference
kgryte 2e3a597
Add document specifying the behavior of the `out` keyword argument
kgryte a2da7d9
Update conventions
kgryte 076a6d5
Add references
kgryte be2a636
Add explainer for arrays of unequal rank
kgryte bbeae1f
Fix reference
kgryte 6d06f13
Update comment
kgryte 22ee3b0
Add link to Python documentation for reading type annotations
kgryte 7818687
Add convention regarding shape immutatibility for output arrays
kgryte e2ee01b
Update descriptions
kgryte 6f1274b
Add data types document
kgryte ca6e87e
Update index
kgryte 9ddb80a
Update descriptions
kgryte 0e272d9
Add type promotion rules
kgryte f1f8490
Update index
kgryte 38db149
Fix reference
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
.. _broadcasting: | ||
|
||
# Broadcasting | ||
|
||
> Array API specification for broadcasting semantics. | ||
|
||
## Overview | ||
|
||
**Broadcasting** refers to the automatic (implicit) expansion of array dimensions to be of equal sizes without copying array data for the purpose of making arrays with different shapes have compatible shapes for element-wise operations. | ||
|
||
Broadcasting facilitates user ergonomics by encouraging users to avoid unnecessary copying of array data and can **potentially** enable more memory-efficient element-wise operations through vectorization, reduced memory consumption, and cache locality. | ||
|
||
## Algorithm | ||
|
||
Given an element-wise operation involving two compatible arrays, an array having a singleton dimension (i.e., a dimension whose size is one) is broadcast (i.e., virtually repeated) across an array having a corresponding non-singleton dimension. | ||
|
||
If two arrays are of unequal rank, the array having a lower rank is promoted to a higher rank by (virtually) prepending singleton dimensions until the number of dimensions matches that of the array having a higher rank. | ||
|
||
The results of the element-wise operation must be stored in an array having a shape determined by the following algorithm. | ||
|
||
1. Let `A` and `B` both be arrays. | ||
|
||
1. Let `shape1` be a tuple describing the shape of array `A`. | ||
|
||
1. Let `shape2` be a tuple describing the shape of array `B`. | ||
|
||
1. Let `N1` be the number of dimensions of array `A` (i.e., the result of `len(shape1)`). | ||
|
||
1. Let `N2` be the number of dimensions of array `B` (i.e., the result of `len(shape2)`). | ||
|
||
1. Let `N` be the maximum value of `N1` and `N2` (i.e., the result of `max(N1, N2)`). | ||
|
||
1. Let `shape` be a temporary list of length `N` for storing the shape of the result array. | ||
|
||
1. Let `i` be `N-1`. | ||
|
||
1. Repeat, while `i >= 0` | ||
|
||
1. If `N1-N+i >= 0`, let `d1` be the size of dimension `n` for array `A` (i.e., the result of `shape1[i]`); else, let `d1` be `1`. | ||
|
||
1. If `N2-N+i >= 0`, let `d2` be the size of dimension `n` for array `B` (i.e., the result of `shape2[i]`); else, let `d2` be `1`. | ||
|
||
1. If `d1 == 1`, then | ||
|
||
- set the `i`th element of `shape` to `d2`. | ||
|
||
1. Else, if `d2 == 1`, then | ||
|
||
- set the `i`th element of `shape` to `d1`. | ||
|
||
1. Else, if `d1 == d2`, then | ||
|
||
- set the `i`th element of `shape` to `d1`. | ||
|
||
1. Else, throw an exception. | ||
|
||
1. Set `i` to `i-1`. | ||
|
||
1. Let `tuple(shape)` be the shape of the result array. | ||
|
||
### Examples | ||
|
||
The following examples demonstrate the application of the broadcasting algorithm for two compatible arrays. | ||
|
||
```text | ||
A (4d array): 8 x 1 x 6 x 1 | ||
B (3d array): 7 x 1 x 5 | ||
--------------------------------- | ||
Result (4d array): 8 x 7 x 6 x 5 | ||
|
||
A (2d array): 5 x 4 | ||
B (1d array): 1 | ||
------------------------- | ||
Result (2d array): 5 x 4 | ||
|
||
A (2d array): 5 x 4 | ||
B (1d array): 4 | ||
------------------------- | ||
Result (2d array): 5 x 4 | ||
|
||
A (3d array): 15 x 3 x 5 | ||
B (3d array): 15 x 1 x 5 | ||
------------------------------ | ||
Result (3d array): 15 x 3 x 5 | ||
|
||
A (3d array): 15 x 3 x 5 | ||
B (2d array): 3 x 5 | ||
------------------------------ | ||
Result (3d array): 15 x 3 x 5 | ||
|
||
A (3d array): 15 x 3 x 5 | ||
B (2d array): 3 x 1 | ||
------------------------------ | ||
Result (3d array): 15 x 3 x 5 | ||
``` | ||
|
||
The following examples demonstrate array shapes which do **not** broadcast. | ||
|
||
```text | ||
A (1d array): 3 | ||
B (1d array): 4 # dimension does not match | ||
|
||
A (2d array): 2 x 1 | ||
B (3d array): 8 x 4 x 3 # second dimension does not match | ||
|
||
A (3d array): 15 x 3 x 5 | ||
B (2d array): 15 x 3 # singleton dimensions can only be prepended, not appended | ||
``` | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## In-place Semantics | ||
|
||
As implied by the broadcasting algorithm, in-place element-wise operations must not change the shape of the in-place array as a result of broadcasting. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
.. _data-types: | ||
|
||
# Data Types | ||
|
||
> Array API specification for supported data types. | ||
|
||
A conforming implementation of the array API standard must provide and support the following data types. | ||
|
||
A conforming implementation of the array API standard may provide and support additional data types beyond those described in this specification. | ||
|
||
## bool | ||
|
||
Boolean (`True` or `False`) stored as a byte. | ||
|
||
## int8 | ||
|
||
An 8-bit signed integer whose values exist on the interval `[-128, +127]`. | ||
|
||
## int16 | ||
|
||
A 16-bit signed integer whose values exist on the interval `[−32,767, +32,767]`. | ||
|
||
## int32 | ||
|
||
A 32-bit signed integer whose values exist on the interval `[−2,147,483,647, +2,147,483,647]`. | ||
|
||
## int64 | ||
|
||
A 64-bit signed integer whose values exist on the interval `[−9,223,372,036,854,775,807, +9,223,372,036,854,775,807]`. | ||
|
||
## uint8 | ||
|
||
An 8-bit unsigned integer whose values exist on the interval `[0, +255]`. | ||
|
||
## uint16 | ||
|
||
A 16-bit unsigned integer whose values exist on the interval `[0, +65,535]`. | ||
|
||
## uint32 | ||
|
||
A 32-bit unsigned integer whose values exist on the interval `[0, +4,294,967,295]`. | ||
|
||
## uint64 | ||
|
||
A 64-bit unsigned integer whose values exist on the interval `[0, +18,446,744,073,709,551,615]`. | ||
|
||
## float32 | ||
|
||
IEEE 754 single-precision (32-bit) binary floating-point number (see IEEE 754-2019). | ||
|
||
## float64 | ||
|
||
IEEE 754 double-precision (64-bit) binary floating-point number (see IEEE 754-2019). |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.