Skip to content

Commit 74aef99

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 ce7deb3 commit 74aef99

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
@@ -2439,8 +2439,8 @@ partial interface MLGraphBuilder {
24392439
1. If |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
24402440
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
24412441
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
2442-
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}}.
2443-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
2442+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2443+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
24442444
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
24452445
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
24462446
1. Make a request to the underlying platform to:
@@ -2454,21 +2454,6 @@ partial interface MLGraphBuilder {
24542454
</div>
24552455
</details>
24562456

2457-
<details open algorithm>
2458-
<summary>
2459-
To <dfn for="MLGraphBuilder">broadcast-shapes</dfn> given |shape1| and |shape2|, run the following steps:
2460-
</summary>
2461-
<div class=algorithm-steps>
2462-
1. [=Assert=]: The type of |shape1| and |shape2| is `sequence of unsigned long`.
2463-
1. Let |output| be the result of invoking the [=implementation-defined=] shape broadcast on |shape1| and |shape2|.
2464-
1. If that fails, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
2465-
1. Return |output|.
2466-
<div class = "note">
2467-
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.
2468-
</div>
2469-
</div>
2470-
</details>
2471-
24722457
<details open>
24732458
<summary>
24742459
The element-wise binary operation algorithms invoke the [=MLGraphBuilder/element-wise-binary-op | create element-wise binary operation=] steps as follows.
@@ -2576,8 +2561,8 @@ Although operations *greaterOrEqual* and *lesserOrEqual* can each be implemented
25762561
1. If |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
25772562
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
25782563
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to {{MLOperandDataType/"uint8"}}.
2579-
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}}.
2580-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
2564+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |a|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |b|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
2565+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
25812566
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
25822567
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
25832568
1. Make a request to the underlying platform to:
@@ -2918,6 +2903,7 @@ partial interface MLGraphBuilder {
29182903
1. If the sequence length of |newShape| is not equal to the [=rank=] of |inputDesc|, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
29192904
1. Let |outputDesc| be a copy of |inputDesc|.
29202905
1. [=map/For each=] |index| in [=the range=] 0 to the [=rank=] of |input|, exclusive:
2906+
Issue: Can this be replaced with [=unidirectionally broadcasting the shapes=] |inputDesc| and |newShape|.
29212907
1. Let |size| be the |input|.{{MLOperand/shape()}}[|index|].
29222908
1. If |size| is not equal to 1 and not equal to |newShape|[index], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
29232909
1. If |size| is equal to 1, then let |outputDesc|.{{MLOperandDescriptor/dimensions}}[|index|] be |newShape|[|index|].
@@ -3066,7 +3052,7 @@ partial interface MLGraphBuilder {
30663052
</div>
30673053

30683054
### gemm ### {#api-mlgraphbuilder-gemm}
3069-
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.
3055+
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.
30703056

30713057
<script type=idl>
30723058
dictionary MLGemmOptions {
@@ -3086,7 +3072,7 @@ partial interface MLGraphBuilder {
30863072
<dl dfn-type=dict-member dfn-for=MLGemmOptions>
30873073
: <dfn>c</dfn>
30883074
::
3089-
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.
3075+
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.
30903076

30913077
: <dfn>alpha</dfn>
30923078
::
@@ -3127,7 +3113,7 @@ partial interface MLGraphBuilder {
31273113
1. If |options|.{{MLGemmOptions/aTranspose}} is true, then let |shapeA| be the reverse array of |shapeA|.
31283114
1. If |options|.{{MLGemmOptions/bTranspose}} is true, then let |shapeB| be the reverse array of |shapeB|.
31293115
1. If |shapeA|[1] is not equal to |shapeB|[0], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
3130-
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}}.
3116+
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}}.
31313117
<div class="note">
31323118
Type compatibility between |a|, |b| and |options|.{{MLGemmOptions/c}} can be also checked.
31333119
</div>
@@ -4971,7 +4957,7 @@ partial interface MLGraphBuilder {
49714957
<div>
49724958
**Arguments:**
49734959
- *input*: an {{MLOperand}}. The input tensor.
4974-
- *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]].
4960+
- *slope*: an {{MLOperand}}. The slope tensor. Its shape is either the same as, or [=unidirectionally broadcastable=] to the shape of input tensor *input*.
49754961

49764962
**Returns:**
49774963
- an {{MLOperand}}. The output tensor of the same shape as *input*.
@@ -4986,8 +4972,8 @@ partial interface MLGraphBuilder {
49864972
1. [=Assert=]: the type of |input| and |slope| is {{MLOperand}}.
49874973
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
49884974
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
4989-
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}}.
4990-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
4975+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=unidirectionally broadcasting the shapes=] |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |slope|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
4976+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
49914977
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
49924978
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
49934979
1. Make a request to the underlying platform to:
@@ -6127,9 +6113,9 @@ partial interface MLGraphBuilder {
61276113
1. If |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}} is not equal to |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
61286114
1. Let |descriptor| be a new {{MLOperandDescriptor}}.
61296115
1. Set |descriptor|.{{MLOperandDescriptor/dataType}} to |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dataType}}.
6130-
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}}.
6131-
1. If that [=exception/throws=] an error, re-[=exception/throw=] the error.
6132-
1. If |condition| is not unidirectionally broadcastable to |descriptor|.{{MLOperandDescriptor/dimensions}} according to the [[!numpy-broadcasting-rule]], then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
6116+
1. Set |descriptor|.{{MLOperandDescriptor/dimensions}} to the result of [=bidirectionally broadcasting the shapes=] |input|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}} and |other|.{{MLOperand/[[descriptor]]}}.{{MLOperandDescriptor/dimensions}}.
6117+
1. If that returns failure, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
6118+
1. If |condition| is not [=unidirectionally broadcastable=] to |descriptor|.{{MLOperandDescriptor/dimensions}}, then [=exception/throw=] a "{{DataError}}" {{DOMException}}.
61336119
1. If any of the following sub-steps fail, [=exception/throw=] an "{{OperationError}}" {{DOMException}}.
61346120
1. Let |output| be the result of [=creating an MLOperand=] given [=this=] and |descriptor|.
61356121
1. Make a request to the underlying platform to:
@@ -6346,6 +6332,48 @@ dictionary MLOperandDescriptor {
63466332
</div>
63476333
</details>
63486334

6335+
Algorithms {#algorithms}
6336+
=====================
6337+
6338+
## Broadcasting ## {#algorithms-broadcasting}
6339+
6340+
Broadcasting refers to how operations treat tensors with different shapes, and follow the precedent set by [[!numpy-broadcasting-rule]].
6341+
6342+
<div algorithm>
6343+
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.
6344+
6345+
1. Let |sizeA| be the [=list/size=] of |A|.
6346+
1. Let |sizeB| be the [=list/size=] of |B|.
6347+
1. Let |output| be a new [=/list=].
6348+
1. [=list/For each=] |index| in [=the range=] 0 to |sizeA|, exclusive:
6349+
1. Let |dimA| be |A|[|sizeA| - |index| - 1] if |index| < |sizeA|, or 1 otherwise.
6350+
1. Let |dimB| be |B|[|sizeB| - |index| - 1] if |index| < |sizeB|, or 1 otherwise.
6351+
1. If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
6352+
1. [=list/Prepend=] |dimA| to |output|.
6353+
1. Return |output|.
6354+
6355+
</div>
6356+
6357+
<div algorithm>
6358+
|A| is <dfn>unidirectionally broadcastable</dfn> to |B| if [=unidirectionally broadcasting the shapes=] |A| and |B| does not result in failure.
6359+
</div>
6360+
6361+
<div algorithm>
6362+
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.
6363+
6364+
1. Let |sizeA| be the [=list/size=] of |A|.
6365+
1. Let |sizeB| be the [=list/size=] of |B|.
6366+
1. Let |outputSize| be the maximum of |sizeA| and |sizeB|.
6367+
1. Let |output| be a new [=/list=].
6368+
1. [=list/For each=] |index| in [=the range=] 0 to |outputSize|, exclusive:
6369+
1. Let |dimA| be |A|[|sizeA| - |index| - 1] if |index| < |sizeA|, or 1 otherwise.
6370+
1. Let |dimB| be |B|[|sizeB| - |index| - 1] if |index| < |sizeB|, or 1 otherwise.
6371+
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.
6372+
1. [=list/Prepend=] the maximum of |dimA| and |dimB| to |output|.
6373+
1. Return |output|.
6374+
6375+
</div>
6376+
63496377
Examples {#examples}
63506378
=====================
63516379

0 commit comments

Comments
 (0)