Skip to content

Commit 440b1a1

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 c9d2dd7 commit 440b1a1

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
@@ -2387,8 +2387,8 @@ partial interface MLGraphBuilder {
23872387
1. If |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
23882388
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
23892389
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
2390-
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}}.
2391-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
2390+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2391+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
23922392
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
23932393
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
23942394
1. Make a request to the underlying platform to:
@@ -2402,21 +2402,6 @@ partial interface MLGraphBuilder {
24022402
</div>
24032403
</details>
24042404

2405-
<details open algorithm>
2406-
<summary>
2407-
To <dfn for="MLGraphBuilder">broadcast-shapes</dfn> given [=/list=] |shape1| and [=/list=] |shape2|, run the following steps:
2408-
</summary>
2409-
<div class=algorithm-steps>
2410-
1. [=Assert=]: The type of |shape1| and |shape2| is `sequence of unsigned long`.
2411-
1. Let |output| be the result of invoking the [=implementation-defined=] shape broadcast on |shape1| and |shape2|.
2412-
1. If that fails, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
2413-
1. Return |output|.
2414-
<div class = "note">
2415-
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.
2416-
</div>
2417-
</div>
2418-
</details>
2419-
24202405
<details open>
24212406
<summary>
24222407
The element-wise binary operation algorithms invoke the [=MLGraphBuilder/element-wise-binary-op | create element-wise binary operation=] steps as follows.
@@ -2523,8 +2508,8 @@ Although operations *greaterOrEqual* and *lesserOrEqual* can each be implemented
25232508
1. If |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
25242509
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
25252510
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to {{MLOperandDataType/"uint8"}}.
2526-
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}}.
2527-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
2511+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2512+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
25282513
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
25292514
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
25302515
1. Make a request to the underlying platform to:
@@ -2862,6 +2847,7 @@ partial interface MLGraphBuilder {
28622847
1. If the sequence length of |newShape| is not equal to the [=rank=] of |inputDesc|, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
28632848
1. Let |outputDesc| be a copy of |inputDesc|.
28642849
1. [=list/For each=] |index| in [=the range=] 0 to the [=rank=] of |input|, exclusive:
2850+
Issue: Can this be replaced with [=unidirectionally broadcasting the shapes=] |inputDesc| and |newShape|.
28652851
1. Let |size| be the |input|.{{MLOperand/shape()}}[|index|].
28662852
1. If |size| is not equal to 1 and not equal to |newShape|[index], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
28672853
1. If |size| is equal to 1, then let |outputDesc|.{{MLOperandDescriptor/dimensions}}[|index|] be |newShape|[|index|].
@@ -3009,7 +2995,7 @@ partial interface MLGraphBuilder {
30092995
</div>
30102996

30112997
### gemm ### {#api-mlgraphbuilder-gemm}
3012-
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.
2998+
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.
30132999

30143000
<script type=idl>
30153001
dictionary MLGemmOptions {
@@ -3029,7 +3015,7 @@ partial interface MLGraphBuilder {
30293015
<dl dfn-type=dict-member dfn-for=MLGemmOptions>
30303016
: <dfn>c</dfn>
30313017
::
3032-
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.
3018+
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.
30333019

30343020
: <dfn>alpha</dfn>
30353021
::
@@ -3069,7 +3055,7 @@ partial interface MLGraphBuilder {
30693055
1. If |options|.{{MLGemmOptions/aTranspose}} is true, then let |shapeA| be the reverse array of |shapeA|.
30703056
1. If |options|.{{MLGemmOptions/bTranspose}} is true, then let |shapeB| be the reverse array of |shapeB|.
30713057
1. If |shapeA|[1] is not equal to |shapeB|[0], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
3072-
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}}.
3058+
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}}.
30733059
<div class="note">
30743060
Type compatibility between |a|, |b| and |options|.{{MLGemmOptions/c}} can be also checked.
30753061
</div>
@@ -4869,7 +4855,7 @@ partial interface MLGraphBuilder {
48694855
<div>
48704856
**Arguments:**
48714857
- *input*: an {{MLOperand}}. The input tensor.
4872-
- *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]].
4858+
- *slope*: an {{MLOperand}}. The slope tensor. Its shape is either the same as, or [=unidirectionally broadcastable=] to the shape of input tensor *input*.
48734859

48744860
**Returns:**
48754861
- an {{MLOperand}}. The output tensor of the same shape as *input*.
@@ -4883,8 +4869,8 @@ partial interface MLGraphBuilder {
48834869
<div class=algorithm-steps>
48844870
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
48854871
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
4886-
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}}.
4887-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
4872+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=unidirectionally broadcasting the shapes=] |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |slope|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
4873+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
48884874
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
48894875
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
48904876
1. Make a request to the underlying platform to:
@@ -6007,9 +5993,9 @@ partial interface MLGraphBuilder {
60075993
1. If |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
60085994
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
60095995
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
6010-
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}}.
6011-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
6012-
1. If |condition| is not unidirectionally broadcastable to |descriptor|.{{MLOperandDescriptor/dimensions}} according to the [[!numpy-broadcasting-rule]], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
5996+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
5997+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
5998+
1. If |condition| is not [=unidirectionally broadcastable=] to |descriptor|.{{MLOperandDescriptor/dimensions}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
60135999
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
60146000
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
60156001
1. Make a request to the underlying platform to:
@@ -6222,6 +6208,48 @@ dictionary MLOperandDescriptor {
62226208
</div>
62236209
</details>
62246210

6211+
Algorithms {#algorithms}
6212+
=====================
6213+
6214+
## Broadcasting ## {#algorithms-broadcasting}
6215+
6216+
Broadcasting refers to how operations treat tensors with different shapes, and follow the precedent set by [[!numpy-broadcasting-rule]].
6217+
6218+
<div algorithm>
6219+
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.
6220+
6221+
1. Let |sizeA| be the [=list/size=] of |A|.
6222+
1. Let |sizeB| be the [=list/size=] of |B|.
6223+
1. Let |output| be a new [=/list=].
6224+
1. [=list/For each=] |index| in [=the range=] 0 to |sizeA|, exclusive:
6225+
1. Let |dimA| be |A|[|sizeA| - |index| - 1] if |index| < |sizeA|, or 1 otherwise.
6226+
1. Let |dimB| be |B|[|sizeB| - |index| - 1] if |index| < |sizeB|, or 1 otherwise.
6227+
1. If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
6228+
1. [=list/Prepend=] |dimA| to |output|.
6229+
1. Return |output|.
6230+
6231+
</div>
6232+
6233+
<div algorithm>
6234+
|A| is <dfn>unidirectionally broadcastable</dfn> to |B| if [=unidirectionally broadcasting the shapes=] |A| and |B| does not result in failure.
6235+
</div>
6236+
6237+
<div algorithm>
6238+
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.
6239+
6240+
1. Let |sizeA| be the [=list/size=] of |A|.
6241+
1. Let |sizeB| be the [=list/size=] of |B|.
6242+
1. Let |outputSize| be the maximum of |sizeA| and |sizeB|.
6243+
1. Let |output| be a new [=/list=].
6244+
1. [=list/For each=] |index| in [=the range=] 0 to |outputSize|, exclusive:
6245+
1. Let |dimA| be |A|[|sizeA| - |index| - 1] if |index| < |sizeA|, or 1 otherwise.
6246+
1. Let |dimB| be |B|[|sizeB| - |index| - 1] if |index| < |sizeB|, or 1 otherwise.
6247+
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.
6248+
1. [=list/Prepend=] the maximum of |dimA| and |dimB| to |output|.
6249+
1. Return |output|.
6250+
6251+
</div>
6252+
62256253
Examples {#examples}
62266254
=====================
62276255

0 commit comments

Comments
 (0)