Skip to content

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 68 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
83fc81f
Add function interfaces
kgryte Jul 30, 2020
22a0f95
Add functions
kgryte Jul 30, 2020
2753c1c
Fix descriptions
kgryte Jul 30, 2020
f3d5cd6
Add rounding functions
kgryte Jul 30, 2020
5fba6f1
Add links
kgryte Jul 30, 2020
0f90c8f
Add heading links
kgryte Jul 30, 2020
13e3758
Move notes up in document
kgryte Jul 30, 2020
3b512d9
Add arithmetic operations
kgryte Jul 30, 2020
4491793
Add domain and codomain
kgryte Jul 30, 2020
668e9e2
Make positional-parameters positional-only
kgryte Jul 30, 2020
ddca794
Add domains and codomains
kgryte Jul 30, 2020
d7df3fd
Rename file and add definition
kgryte Jul 30, 2020
c707910
Experiment with subscripts
kgryte Jul 30, 2020
f5de1c4
Experiment with subscripts
kgryte Jul 30, 2020
6e2d1f2
Use HTML subscripts
kgryte Jul 30, 2020
7f37fcc
Update phrasing
kgryte Jul 30, 2020
63ef729
Revert use of HTML markup for subscripts
kgryte Aug 3, 2020
991eca6
Move terms, references, and conformance to purpose and scope document
kgryte Aug 3, 2020
f1fb7a5
Remove heading
kgryte Aug 3, 2020
f009e50
Add definition for broadcasting
kgryte Aug 3, 2020
15422fe
Define compatible
kgryte Aug 3, 2020
2f653ec
Add broadcasting document
kgryte Aug 4, 2020
9caaff9
Fix markup
kgryte Aug 4, 2020
f200206
Update phrasing
kgryte Aug 4, 2020
02de24e
Fix algorithm
kgryte Aug 4, 2020
347973a
Update definition
kgryte Aug 4, 2020
b813d9b
Expand overview and add note regarding in-place semantics
kgryte Aug 4, 2020
33cd2b7
Rename arrays
kgryte Aug 4, 2020
5df7549
Rephrase
kgryte Aug 4, 2020
3dd7288
Add clause
kgryte Aug 4, 2020
f599e2e
Add initial typing
kgryte Aug 5, 2020
81ea06a
Merge branch 'master' of https://github.com/pydata-apis/array-api int…
kgryte Aug 5, 2020
7c34785
Remove typing from signature
kgryte Aug 5, 2020
11fa322
Update typing
kgryte Aug 5, 2020
8701e7d
Add markup
kgryte Aug 5, 2020
0c27737
Update typing
kgryte Aug 5, 2020
7f00d03
Update formatting
kgryte Aug 5, 2020
21ee999
Remove spaces
kgryte Aug 5, 2020
8883026
Add types
kgryte Aug 5, 2020
be488d5
Update `out` requirements
kgryte Aug 6, 2020
31f69db
Update types and descriptions
kgryte Aug 6, 2020
8194b38
Update descriptions
kgryte Aug 6, 2020
6100447
Update conventions
kgryte Aug 6, 2020
8890def
Remove markup and update descriptions
kgryte Aug 6, 2020
b489749
Document broadcasting behavior
kgryte Aug 6, 2020
4af802c
Update wording
kgryte Aug 6, 2020
f8ef97f
Rephrase
kgryte Aug 6, 2020
c4847d1
Add example
kgryte Aug 6, 2020
18738cf
Update `out` type
kgryte Aug 7, 2020
29a6a5d
Update descriptions
kgryte Aug 7, 2020
d1bb2ba
Update conventions
kgryte Aug 7, 2020
d9adebc
Link to broadcasting document
kgryte Aug 7, 2020
16a0727
Add reference
kgryte Aug 7, 2020
2e3a597
Add document specifying the behavior of the `out` keyword argument
kgryte Aug 7, 2020
a2da7d9
Update conventions
kgryte Aug 7, 2020
076a6d5
Add references
kgryte Aug 7, 2020
be2a636
Add explainer for arrays of unequal rank
kgryte Aug 7, 2020
bbeae1f
Fix reference
kgryte Aug 7, 2020
6d06f13
Update comment
kgryte Aug 7, 2020
22ee3b0
Add link to Python documentation for reading type annotations
kgryte Aug 7, 2020
7818687
Add convention regarding shape immutatibility for output arrays
kgryte Aug 8, 2020
e2ee01b
Update descriptions
kgryte Aug 8, 2020
6f1274b
Add data types document
kgryte Aug 8, 2020
ca6e87e
Update index
kgryte Aug 8, 2020
9ddb80a
Update descriptions
kgryte Aug 8, 2020
0e272d9
Add type promotion rules
kgryte Aug 8, 2020
f1f8490
Update index
kgryte Aug 8, 2020
38db149
Fix reference
kgryte Aug 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions spec/API_specification/broadcasting.md
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
```

## 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.
53 changes: 53 additions & 0 deletions spec/API_specification/data_types.md
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).
Loading