Skip to content

Commit 0712df2

Browse files
New content: Add definition for shape broadcasting
This change introduces a new section for Algorithms, following APIs, to collect algorithms referenced throughout the specification. A section for Broadcasting is introduced, which defines broadcasting shapes and gives an explicit algorithm matching WebNN implementations of NumPy's General Broadcasting Rules. Definitions for "broadcastable" and "unidirectionally broadcastable" are introduced. The previous definition of "broadcast-shapes" is removed in favor of these new algorithms. For webmachinelearning#324, webmachinelearning#378, webmachinelearning#462, and potentially webmachinelearning#523.
1 parent 479ce17 commit 0712df2

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

index.bs

+56-28
Original file line numberDiff line numberDiff line change
@@ -2404,8 +2404,8 @@ partial interface MLGraphBuilder {
24042404
1. If |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
24052405
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
24062406
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
2407-
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of running the [=MLGraphBuilder/broadcast-shapes=] steps given |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2408-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
2407+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2408+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
24092409
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
24102410
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
24112411
1. Make a request to the underlying platform to:
@@ -2419,21 +2419,6 @@ partial interface MLGraphBuilder {
24192419
</div>
24202420
</details>
24212421

2422-
<details open algorithm>
2423-
<summary>
2424-
To <dfn for="MLGraphBuilder">broadcast-shapes</dfn> given |shape1| and |shape2|, run the following steps:
2425-
</summary>
2426-
<div class=algorithm-steps>
2427-
1. [=Assert=]: The type of |shape1| and |shape2| is `sequence of unsigned long`.
2428-
1. Let |output| be the result of invoking the [=implementation-defined=] shape broadcast on |shape1| and |shape2|.
2429-
1. If that fails, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
2430-
1. Return |output|.
2431-
<div class = "note">
2432-
The most common implementation is that two shapes are compatible, when each of their corresponding dimensions are equal, or one of them is 1. The output shape consists of the maximum of the corresponding dimensions.
2433-
</div>
2434-
</div>
2435-
</details>
2436-
24372422
<details open>
24382423
<summary>
24392424
The element-wise binary operation algorithms invoke the [=MLGraphBuilder/element-wise-binary-op | create element-wise binary operation=] steps as follows.
@@ -2540,8 +2525,8 @@ Although operations *greaterOrEqual* and *lesserOrEqual* can each be implemented
25402525
1. If |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
25412526
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
25422527
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to {{MLOperandDataType/"uint8"}}.
2543-
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of running the [=MLGraphBuilder/broadcast-shapes=] steps given |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2544-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
2528+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2529+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
25452530
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
25462531
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
25472532
1. Make a request to the underlying platform to:
@@ -2879,6 +2864,7 @@ partial interface MLGraphBuilder {
28792864
1. If the sequence length of |newShape| is not equal to the [=rank=] of |inputDesc|, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
28802865
1. Let |outputDesc| be a copy of |inputDesc|.
28812866
1. [=list/For each=] |index| in [=the range=] 0 to the [=rank=] of |input|, exclusive:
2867+
Issue: Can this be replaced with [=unidirectionally broadcasting the shapes=] |inputDesc| and |newShape|.
28822868
1. Let |size| be the |input|.{{MLOperand/shape()}}[|index|].
28832869
1. If |size| is not equal to 1 and not equal to |newShape|[index], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
28842870
1. If |size| is equal to 1, then let |outputDesc|.{{MLOperandDescriptor/dimensions}}[|index|] be |newShape|[|index|].
@@ -3026,7 +3012,7 @@ partial interface MLGraphBuilder {
30263012
</div>
30273013

30283014
### gemm ### {#api-mlgraphbuilder-gemm}
3029-
Calculate the [general matrix multiplication of the Basic Linear Algebra Subprograms](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3). The calculation follows the expression `alpha * A * B + beta * C`, where `A` is a 2-D tensor with shape [M, K] or [K, M], `B` is a 2-D tensor with shape [K, N] or [N, K], and `C` is broadcastable to the shape [M, N]. `A` and `B` may optionally be transposed prior to the calculation.
3015+
Calculate the [general matrix multiplication of the Basic Linear Algebra Subprograms](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3). The calculation follows the expression `alpha * A * B + beta * C`, where `A` is a 2-D tensor with shape [M, K] or [K, M], `B` is a 2-D tensor with shape [K, N] or [N, K], and `C` is [=unidirectionally broadcastable=] to the shape [M, N]. `A` and `B` may optionally be transposed prior to the calculation.
30303016

30313017
<script type=idl>
30323018
dictionary MLGemmOptions {
@@ -3046,7 +3032,7 @@ partial interface MLGraphBuilder {
30463032
<dl dfn-type=dict-member dfn-for=MLGemmOptions>
30473033
: <dfn>c</dfn>
30483034
::
3049-
An {{MLOperand}}. Specifies the third input tensor. It is either a scalar, or of the shape that is unidirectionally broadcastable to the shape [M, N] according to [[!numpy-broadcasting-rule]]. When it is not specified, the computation is done as if *c* is a scalar 0.0.
3035+
An {{MLOperand}}. Specifies the third input tensor. It is either a scalar, or of the shape that is [=unidirectionally broadcastable=] to the shape [M, N]. When it is not specified, the computation is done as if *c* is a scalar 0.0.
30503036

30513037
: <dfn>alpha</dfn>
30523038
::
@@ -3086,7 +3072,7 @@ partial interface MLGraphBuilder {
30863072
1. If |options|.{{MLGemmOptions/aTranspose}} is true, then let |shapeA| be the reverse array of |shapeA|.
30873073
1. If |options|.{{MLGemmOptions/bTranspose}} is true, then let |shapeB| be the reverse array of |shapeB|.
30883074
1. If |shapeA|[1] is not equal to |shapeB|[0], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
3089-
1. If |options|.{{MLGemmOptions/c}} [=map/exists=] and is not unidirectionally broadcastable to the shape [|shapeA|[0], |shapeB|[1]] according to the [[!numpy-broadcasting-rule]], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
3075+
1. If |options|.{{MLGemmOptions/c}} [=map/exists=] and is not [=unidirectionally broadcastable=] to the shape [|shapeA|[0], |shapeB|[1]], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
30903076
<div class="note">
30913077
Type compatibility between |a|, |b| and |options|.{{MLGemmOptions/c}} can be also checked.
30923078
</div>
@@ -4900,7 +4886,7 @@ partial interface MLGraphBuilder {
49004886
<div>
49014887
**Arguments:**
49024888
- *input*: an {{MLOperand}}. The input tensor.
4903-
- *slope*: an {{MLOperand}}. The slope tensor. Its shape is either the same as, or unidirectionally broadcastable to the shape of input tensor *input* according to [[!numpy-broadcasting-rule]].
4889+
- *slope*: an {{MLOperand}}. The slope tensor. Its shape is either the same as, or [=unidirectionally broadcastable=] to the shape of input tensor *input*.
49044890

49054891
**Returns:**
49064892
- an {{MLOperand}}. The output tensor of the same shape as *input*.
@@ -4914,8 +4900,8 @@ partial interface MLGraphBuilder {
49144900
<div class=algorithm-steps>
49154901
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
49164902
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
4917-
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of running the [=MLGraphBuilder/broadcast-shapes=] steps given |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |slope|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
4918-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
4903+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=unidirectionally broadcasting the shapes=] |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |slope|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
4904+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
49194905
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
49204906
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
49214907
1. Make a request to the underlying platform to:
@@ -6043,9 +6029,9 @@ partial interface MLGraphBuilder {
60436029
1. If |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
60446030
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
60456031
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
6046-
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of running the [=MLGraphBuilder/broadcast-shapes=] steps given |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
6047-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
6048-
1. If |condition| is not unidirectionally broadcastable to |descriptor|.{{MLOperandDescriptor/dimensions}} according to the [[!numpy-broadcasting-rule]], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
6032+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
6033+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
6034+
1. If |condition| is not [=unidirectionally broadcastable=] to |descriptor|.{{MLOperandDescriptor/dimensions}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
60496035
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
60506036
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
60516037
1. Make a request to the underlying platform to:
@@ -6258,6 +6244,48 @@ dictionary MLOperandDescriptor {
62586244
</div>
62596245
</details>
62606246

6247+
Algorithms {#algorithms}
6248+
=====================
6249+
6250+
## Broadcasting ## {#algorithms-broadcasting}
6251+
6252+
Broadcasting refers to how operations treat tensors with different shapes, and follow the precedent set by [[!numpy-broadcasting-rule]].
6253+
6254+
<div algorithm>
6255+
To <dfn data-lt="unidirectionally broadcasting the shapes">unidirectionally broadcast the shapes</dfn> |A| and |B|, perform the following steps. |A| and |B| are [=/lists=] of positive integers, representing the dimensions of tensors, and the steps return a new [=/list=] of positive integers, or failure.
6256+
6257+
1. Let |sizeA| be the [=list/size=] of |A|.
6258+
1. Let |sizeB| be the [=list/size=] of |B|.
6259+
1. Let |output| be a new [=/list=].
6260+
1. [=list/For each=] |index| in [=the range=] 0 to |sizeA|, exclusive:
6261+
1. Let |dimA| be |A|[|sizeA| - |index| - 1] if |index| < |sizeA|, or 1 otherwise.
6262+
1. Let |dimB| be |B|[|sizeB| - |index| - 1] if |index| < |sizeB|, or 1 otherwise.
6263+
1. If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
6264+
1. [=list/Prepend=] |dimA| to |output|.
6265+
1. Return |output|.
6266+
6267+
</div>
6268+
6269+
<div algorithm>
6270+
|A| is <dfn>unidirectionally broadcastable</dfn> to |B| if [=unidirectionally broadcasting the shapes=] |A| and |B| does not result in failure.
6271+
</div>
6272+
6273+
<div algorithm>
6274+
To <dfn data-lt="bidirectionally broadcasting the shapes">bidirectionally broadcast the shapes</dfn> |A| and |B|, perform the following steps. |A| and |B| are [=/lists=] of positive integers, representing the dimensions of tensors, and the steps return a new [=/list=] of positive integers, or failure.
6275+
6276+
1. Let |sizeA| be the [=list/size=] of |A|.
6277+
1. Let |sizeB| be the [=list/size=] of |B|.
6278+
1. Let |outputSize| be the maximum of |sizeA| and |sizeB|.
6279+
1. Let |output| be a new [=/list=].
6280+
1. [=list/For each=] |index| in [=the range=] 0 to |outputSize|, exclusive:
6281+
1. Let |dimA| be |A|[|sizeA| - |index| - 1] if |index| < |sizeA|, or 1 otherwise.
6282+
1. Let |dimB| be |B|[|sizeB| - |index| - 1] if |index| < |sizeB|, or 1 otherwise.
6283+
1. If |dimA| is not equal to |dimB|, and |dimA| is not equal to 1, and |dimB| is not equal to 1, then return failure.
6284+
1. [=list/Prepend=] the maximum of |dimA| and |dimB| to |output|.
6285+
1. Return |output|.
6286+
6287+
</div>
6288+
62616289
Examples {#examples}
62626290
=====================
62636291

0 commit comments

Comments
 (0)