Skip to content

Commit e52f163

Browse files
authored
Merge pull request #627 from inexorabletash/conventions-rank-not-size
Conventions: Use "rank" for variable names, when appropriate
2 parents fe6e88b + f4535ca commit e52f163

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

docs/SpecCodingConventions.md

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Example:
110110
* Use "let" to introduce a variable and "set" to update a variable or assign to a property.
111111
* Use « » notation for literal lists, which helps make it clear that they are not JavaScript arrays.
112112
* When referring to abstract properties, use the short possessive form `|object|'s [=property=]`. Avoid the wordier `the [=property=] of |object|` form.
113+
* Use "rank" when describing the number of dimensions of a tensor (e.g. in variable names) rather than the ambiguous "size".
114+
* Only use single capital letters as variable names when referring to tensors; i.e. prefer `|shapeA|` to `|A|`, but tensor `|T|` is okay.
113115

114116

115117
### Method Definitions

index.bs

+32-38
Original file line numberDiff line numberDiff line change
@@ -1562,10 +1562,10 @@ partial interface MLGraphBuilder {
15621562
1. If |variance|'s [=MLOperand/rank=] is not 1, then [=exception/throw=] a {{TypeError}}.
15631563
1. If |variance|'s [=MLOperand/shape=][0] is not equal to |input|'s [=MLOperand/shape=][|options|.{{MLBatchNormalizationOptions/axis}}], then [=exception/throw=] a {{TypeError}}.
15641564
1. If |options|.{{MLBatchNormalizationOptions/scale}} [=map/exists=]:
1565-
1. If its [=list/size=] is not 1, then [=exception/throw=] a {{TypeError}}.
1565+
1. If its [=MLOperand/rank=] is not 1, then [=exception/throw=] a {{TypeError}}.
15661566
1. If |options|.{{MLBatchNormalizationOptions/scale}}'s [=MLOperand/shape=][0] is not equal to |input|'s [=MLOperand/shape=][|options|.{{MLBatchNormalizationOptions/axis}}], then [=exception/throw=] a {{TypeError}}.
15671567
1. If |options|.{{MLBatchNormalizationOptions/bias}} [=map/exists=]:
1568-
1. If its [=list/size=] is not 1, then [=exception/throw=] a {{TypeError}}.
1568+
1. If its [=MLOperand/rank=] is not 1, then [=exception/throw=] a {{TypeError}}.
15691569
1. If |options|.{{MLBatchNormalizationOptions/bias}}'s [=MLOperand/shape=][0] is not equal to |input|'s [=MLOperand/shape=][|options|.{{MLBatchNormalizationOptions/axis}}], then [=exception/throw=] a {{TypeError}}.
15701570
1. *Make graph connections:*
15711571
1. Let |operator| be an [=operator=] for the batchNormalization operation, given |input|, |mean|, |variance| and |options|.
@@ -1901,10 +1901,8 @@ partial interface MLGraphBuilder {
19011901
</summary>
19021902
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input|, |filter|, and |options|.{{MLConv2dOptions/bias}} (if it [=map/exists=]) returns false, then [=exception/throw=] a {{TypeError}}.
19031903
1. If |options|.{{MLConv2dOptions/activation}} [=map/exists=], and [=MLGraphBuilder/validating activation=] with [=this=] and it returns false, then [=exception/throw=] a {{TypeError}}.
1904-
1. Let |inputSize| be |input|'s [=MLOperand/rank=].
1905-
1. Let |filterSize| be |filter|'s [=MLOperand/rank=].
1906-
1. If |inputSize| is not 4, then [=exception/throw=] a {{TypeError}}.
1907-
1. If |filterSize| is not 4, then [=exception/throw=] a {{TypeError}}.
1904+
1. If |input|'s [=MLOperand/rank=] is not 4, then [=exception/throw=] a {{TypeError}}.
1905+
1. If |filter|'s [=MLOperand/rank=] is not 4, then [=exception/throw=] a {{TypeError}}.
19081906
1. If |input|'s [=MLOperand/dataType=] is not the same as |filter|'s [=MLOperand/dataType=], then [=exception/throw=] a {{TypeError}}.
19091907
1. If |options|.{{MLConv2dOptions/padding}} does not [=map/exist=], set it to the [=/list=] « 0, 0, 0, 0 ».
19101908
1. Else if |options|.{{MLConv2dOptions/padding}}'s [=list/size=] is not 4, then [=exception/throw=] a {{TypeError}}.
@@ -2117,10 +2115,8 @@ partial interface MLGraphBuilder {
21172115
</summary>
21182116
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input|, |filter|, and |options|.{{MLConvTranspose2dOptions/bias}} (if it [=map/exists=]) returns false, then [=exception/throw=] a {{TypeError}}.
21192117
1. If |options|.{{MLConvTranspose2dOptions/activation}} [=map/exists=], and [=MLGraphBuilder/validating activation=] with [=this=] and it returns false, then [=exception/throw=] a {{TypeError}}.
2120-
1. Let |inputSize| be |input|'s [=MLOperand/rank=].
2121-
1. Let |filterSize| be |filter|'s [=MLOperand/rank=].
2122-
1. If |inputSize| is not 4, then [=exception/throw=] a {{TypeError}}.
2123-
1. If |filterSize| is not 4, then [=exception/throw=] a {{TypeError}}.
2118+
1. If |input|'s [=MLOperand/rank=] is not 4, then [=exception/throw=] a {{TypeError}}.
2119+
1. If |filter|'s [=MLOperand/rank=] is not 4, then [=exception/throw=] a {{TypeError}}.
21242120
1. If |input|'s [=MLOperand/dataType=] is not the same as |filter|'s [=MLOperand/dataType=], then [=exception/throw=] a {{TypeError}}.
21252121
1. If |options|.{{MLConvTranspose2dOptions/padding}} does not [=map/exist=], set it to the [=/list=] « 0, 0, 0, 0 ».
21262122
1. Else if |options|.{{MLConvTranspose2dOptions/padding}}'s [=list/size=] is not 4, then [=exception/throw=] a {{TypeError}}.
@@ -2876,11 +2872,9 @@ partial interface MLGraphBuilder {
28762872
The <dfn method for=MLGraphBuilder>gemm(|a|, |b|, |options|)</dfn> method steps are:
28772873
</summary>
28782874
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |a| and |b| returns false, then [=exception/throw=] a {{TypeError}}.
2875+
1. If |a|'s [=MLOperand/rank=] is not 2 or |b|'s [=MLOperand/rank=] is not 2, then [=exception/throw=] a {{TypeError}}.
28792876
1. Let |shapeA| be a [=list/clone=] of |a|'s [=MLOperand/shape=].
2880-
1. Let |sizeA| be |shapeA|'s [=list/size=].
28812877
1. Let |shapeB| be a [=list/clone=] of |b|'s [=MLOperand/shape=].
2882-
1. Let |sizeB| be |shapeB|'s [=list/size=].
2883-
1. If |sizeA| is not 2 or |sizeB| is not 2, then [=exception/throw=] a {{TypeError}}.
28842878
1. If |options|.{{MLGemmOptions/aTranspose}} is true, then reverse the order of the items in |shapeA|.
28852879
1. If |options|.{{MLGemmOptions/bTranspose}} is true, then reverse the order of the items in |shapeB|.
28862880
1. If |shapeA|[1] is not equal to |shapeB|[0], then [=exception/throw=] a {{TypeError}}.
@@ -4286,17 +4280,17 @@ partial interface MLGraphBuilder {
42864280

42874281
<details open algorithm>
42884282
<summary>
4289-
To <dfn dfn-for=MLGraphBuilder>calculate matmul output sizes</dfn>, given |a| and |b| run the following steps:
4283+
To <dfn dfn-for=MLGraphBuilder>calculate matmul output sizes</dfn>, given {{MLOperand}} |a| and {{MLOperand}} |b| run the following steps:
42904284
</summary>
42914285
1. Let |shapeA| be a [=list/clone=] of |a|'s [=MLOperand/shape=]
4292-
1. Let |sizeA| be |shapeA|'s [=list/size=].
4286+
1. Let |rankA| be |a|'s [=MLOperand/rank=].
42934287
1. Let |shapeB| be a [=list/clone=] of |b|'s [=MLOperand/shape=]
4294-
1. Let |sizeB| be |shapeB|'s [=list/size=].
4295-
1. If either |sizeA| or |sizeB| is less than 2, then [=exception/throw=] a {{TypeError}}.
4296-
1. Let |colsA| be |shapeA|[|sizeA| - 1].
4297-
1. Let |rowsA| be |shapeA|[|sizeA| - 2].
4298-
1. Let |colsB| be |shapeB|[|sizeB| - 1].
4299-
1. Let |rowsB| be |shapeB|[|sizeB| - 2].
4288+
1. Let |rankB| be |b|'s [=MLOperand/rank=].
4289+
1. If either |rankA| or |rankB| is less than 2, then [=exception/throw=] a {{TypeError}}.
4290+
1. Let |colsA| be |shapeA|[|rankA| - 1].
4291+
1. Let |rowsA| be |shapeA|[|rankA| - 2].
4292+
1. Let |colsB| be |shapeB|[|rankB| - 1].
4293+
1. Let |rowsB| be |shapeB|[|rankB| - 2].
43004294
1. If |colsA| is not equal to |rowsB|, then [=exception/throw=] a {{TypeError}}.
43014295
1. Let |batchShapeA| be a [=list/clone=] of |shapeA| with the spatial dimensions (last 2 items) [=list/removed=].
43024296
1. Let |batchShapeB| be a [=list/clone=] of |shapeB| with the spatial dimensions (last 2 items) [=list/removed=].
@@ -4730,7 +4724,7 @@ partial interface MLGraphBuilder {
47304724

47314725
: <dfn>keepDimensions</dfn>
47324726
::
4733-
If true, retains reduced dimensions with [=list/size=] 1.
4727+
If true, the output has the same rank as the input, setting any reduced dimensions to size 1.
47344728
</dl>
47354729

47364730
<div>
@@ -4759,15 +4753,15 @@ partial interface MLGraphBuilder {
47594753
<summary>
47604754
To <dfn for="MLGraphBuilder">calculate reduction output sizes</dfn>, given a [=/list=] of unsigned integers |inputShape|, a optional [=/list=] of unsigned integers |axes|, and [=/boolean=] |keepDimensions|, perform the following steps. They return a new [=/list=] of unsigned integers.
47614755
</summary>
4762-
1. Let |inputSize| be |inputShape|'s [=list/size=].
4763-
1. If |axes| is not given, let |axes| be [=the range=] 0 to |inputSize|, exclusive.
4756+
1. Let |inputRank| be |inputShape|'s [=list/size=].
4757+
1. If |axes| is not given, let |axes| be [=the range=] 0 to |inputRank|, exclusive.
47644758
1. If |keepDimensions| is true, then:
47654759
1. Let |outputShape| be a [=list/clone=] of |inputShape|.
47664760
1. [=list/For each=] |axis| of |axes|:
47674761
1. Set |outputShape|[|axis|] to 1.
47684762
1. Otherwise:
47694763
1. Let |outputShape| be an empty [=/list=].
4770-
1. [=list/For each=] |index| in [=the range=] 0 to |inputSize|, exclusive:
4764+
1. [=list/For each=] |index| in [=the range=] 0 to |inputRank|, exclusive:
47714765
1. If |axes| does not [=list/contain=] |index|, then [=list/append=] |inputShape|[|index|].
47724766
1. Return |outputShape|.
47734767
</details>
@@ -5189,7 +5183,7 @@ partial interface MLGraphBuilder {
51895183
The <dfn method for=MLGraphBuilder>slice(|input|, |starts|, |sizes|)</dfn> method steps are:
51905184
</summary>
51915185
1. If [=MLGraphBuilder/validating operand=] with [=this=] and |input| returns false, then [=exception/throw=] a {{TypeError}}.
5192-
1. If |sizes|'s [=list/size=] is 0, then [=exception/throw=] a {{TypeError}}.
5186+
1. If any of |sizes|'s [=list/items=] are 0, then [=exception/throw=] a {{TypeError}}.
51935187
1. If |starts|'s [=list/size=] and |sizes|'s [=list/size=] are not both equal to |input|'s [=MLOperand/rank=], then [=exception/throw=] a {{TypeError}}.
51945188
1. *Make graph connections:*
51955189
1. Let |output| be the result of [=copying an MLOperand=] given |input|.
@@ -5787,17 +5781,17 @@ Broadcasting refers to how operations treat tensors with different shapes, and f
57875781

57885782
<details open algorithm>
57895783
<summary>
5790-
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.
5784+
To <dfn data-lt="unidirectionally broadcasting the shapes">unidirectionally broadcast the shapes</dfn> |shapeA| and |shapeB|, perform the following steps. |shapeA| and |shapeB| are [=/lists=] of positive integers, representing the dimensions of tensors, and the steps return a new [=/list=] of positive integers, or failure.
57915785
</summary>
57925786

5793-
1. Let |sizeA| be |A|'s [=list/size=].
5794-
1. Let |sizeB| be |B|'s [=list/size=].
5787+
1. Let |sizeA| be |shapeA|'s [=list/size=].
5788+
1. Let |sizeB| be |shapeB|'s [=list/size=].
57955789
1. If |sizeB| > |sizeA|, then return failure.
5796-
1. Let |paddedB| be a [=list/clone=] of |B|.
5790+
1. Let |paddedB| be a [=list/clone=] of |shapeB|.
57975791
1. While |paddedB|'s [=list/size=] is less than |sizeA|, [=list/prepend=] 1 to |paddedB|.
57985792
1. Let |outputShape| be a new [=/list=].
57995793
1. [=list/For each=] |index| in [=the range=] 0 to |sizeA|, exclusive:
5800-
1. Let |dimA| be |A|[|index|].
5794+
1. Let |dimA| be |shapeA|[|index|].
58015795
1. Let |dimB| be |paddedB|[|index|].
58025796
1. If |dimA| is not equal to |dimB| and |dimA| is not equal to 1, then return failure.
58035797
1. [=list/Append=] |dimA| to |outputShape|.
@@ -5806,20 +5800,20 @@ To <dfn data-lt="unidirectionally broadcasting the shapes">unidirectionally broa
58065800
</details>
58075801

58085802
<p algorithm>
5809-
|A| is <dfn>unidirectionally broadcastable</dfn> to |B| if [=unidirectionally broadcasting the shapes=] |A| and |B| does not result in failure.
5803+
|shapeA| is <dfn>unidirectionally broadcastable</dfn> to |shapeB| if [=unidirectionally broadcasting the shapes=] |shapeA| and |shapeB| does not result in failure.
58105804
</p>
58115805

58125806
<details open algorithm>
58135807
<summary>
5814-
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.
5808+
To <dfn data-lt="bidirectionally broadcasting the shapes">bidirectionally broadcast the shapes</dfn> |shapeA| and |shapeB|, perform the following steps. |shapeA| and |shapeB| are [=/lists=] of positive integers, representing the dimensions of tensors, and the steps return a new [=/list=] of positive integers, or failure.
58155809
</summary>
58165810

5817-
1. Let |sizeA| be |A|'s [=list/size=].
5818-
1. Let |sizeB| be |B|'s [=list/size=].
5811+
1. Let |sizeA| be |shapeA|'s [=list/size=].
5812+
1. Let |sizeB| be |shapeB|'s [=list/size=].
58195813
1. Let |outputSize| be the maximum of |sizeA| and |sizeB|.
5820-
1. Let |paddedA| be a [=list/clone=] of |A|.
5814+
1. Let |paddedA| be a [=list/clone=] of |shapeA|.
58215815
1. While |paddedA|'s [=list/size=] is less than |outputSize|, [=list/prepend=] 1 to |paddedA|.
5822-
1. Let |paddedB| be a [=list/clone=] of |B|.
5816+
1. Let |paddedB| be a [=list/clone=] of |shapeB|.
58235817
1. While |paddedB|'s [=list/size=] is less than |outputSize|, [=list/prepend=] 1 to |paddedB|.
58245818
1. Let |outputShape| be a new [=/list=].
58255819
1. [=list/For each=] |index| in [=the range=] 0 to |outputSize|, exclusive:
@@ -5832,7 +5826,7 @@ To <dfn data-lt="bidirectionally broadcasting the shapes">bidirectionally broadc
58325826
</details>
58335827

58345828
<p algorithm>
5835-
|A| is <dfn>bidirectionally broadcastable</dfn> to |B| if [=bidirectionally broadcasting the shapes=] |A| and |B| does not result in failure.
5829+
|shapeA| is <dfn>bidirectionally broadcastable</dfn> to |shapeB| if [=bidirectionally broadcasting the shapes=] |shapeA| and |shapeB| does not result in failure.
58365830
</p>
58375831

58385832
Examples {#examples}

0 commit comments

Comments
 (0)