Skip to content

Commit 233fad9

Browse files
gather(): Address indices validation and other algorithm nits
* webmachinelearning#486 points out that indices can't be validated at build-time, and clamping behavior with an implementation note is given instead. * Fix a typo in the steps. * Replace several map-like iterations over lists with list iteration. Fixes webmachinelearning#486
1 parent 1f60107 commit 233fad9

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

index.bs

+9-8
Original file line numberDiff line numberDiff line change
@@ -2714,40 +2714,41 @@ partial interface MLGraphBuilder {
27142714
<div>
27152715
**Arguments:**
27162716
- *input*: an {{MLOperand}}. The input N-D tensor from which the values are gathered.
2717-
- *indices*: an {{MLOperand}}. The indices N-D tensor of the input values to gather. The values must be of type {{MLOperandDataType/"uint32"}} or {{MLOperandDataType/"int64"}} in the range [0, N-1] where N is the size of the input dimension indexed by *options.axis*.
2717+
- *indices*: an {{MLOperand}}. The indices N-D tensor of the input values to gather. The values must be of type {{MLOperandDataType/"uint32"}} or {{MLOperandDataType/"int64"}}, and are clamped to the range -N (inclusive) to N (exclusive) where N is the size of the input dimension indexed by *options.axis*, and a negative index means indexing from the end of the dimension.
27182718
- *options*: an optional {{MLGatherOptions}}. The optional parameters of the operation.
27192719

27202720
**Returns:** an {{MLOperand}}. The output N-D tensor of [=MLOperand/rank=] equal to the [=MLOperand/rank=] of *input* + the [=MLOperand/rank=] of *indices* - 1.
27212721
</div>
27222722

2723+
<div class="note">
2724+
The {{MLGraphBuilder/gather(input, indices, options)/indices}} parameter to {{MLGraphBuilder/gather()}} can not be clamped to the allowed range when the graph is built. Implementations can introduce clamping operands in the compiled graph if the required clamping behavior is not provided by the underlying platform.
2725+
</div>
2726+
27232727
<details open algorithm>
27242728
<summary>
27252729
The <dfn method for=MLGraphBuilder>gather(|input|, |indices|, |options|)</dfn> method steps are:
27262730
</summary>
2727-
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input| abd |indices| returns false, then [=exception/throw=] a {{TypeError}}.
2731+
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input| and |indices| returns false, then [=exception/throw=] a {{TypeError}}.
27282732
1. If |indices|'s [=MLOperand/dataType=] is neither {{MLOperandDataType/"uint32"}} nor {{MLOperandDataType/"int64"}}, then [=exception/throw=] a {{TypeError}}.
27292733
1. Let |shapeInput| be |input|'s [=MLOperand/shape=] and |rankInput| be |shapeInput|'s [=MLOperand/rank=].
27302734
1. Let |shapeIndices| be |indices|'s [=MLOperand/shape=].
27312735
1. Let |axis| be |options|.{{MLGatherOptions/axis}}.
2732-
1. Let |axisSize| be |input|'s [=MLOperand/shape=][|axis|]
27332736
1. If |axis| is greater than or equal to |rankInput|, then [=exception/throw=] a {{TypeError}}.
2734-
1. [=map/For each=] |index| → |value| of |indices|:
2735-
1. If |index| is greater than or equal to |axisSize|, then [=exception/throw=] a {{TypeError}}.
27362737
1. Let |dimCount| be zero.
27372738
1. Let |rankOutput| be zero.
27382739
1. Let |shapeOutput| be an empty list.
2739-
1. [=map/For each=] |size| → |value| of |shapeInput|:
2740+
1. [=list/For each=] |size| of |shapeInput|:
27402741
1. If |dimCount| is equal to |axis| then [=iteration/break=].
27412742
1. Set |shapeOutput|[|dimCount|] to |size|.
27422743
1. Increment |dimCount| by one.
27432744
1. Set |rankOutput| to |dimCount|.
27442745
1. Let |dimCount| be zero.
2745-
1. [=map/For each=] |size| → |value| of |shapeIndices|:
2746+
1. [=list/For each=] |size| of |shapeIndices|:
27462747
1. Set |shapeOutput|[|rankOutput| + |dimCount|] to |size|.
27472748
1. Increment |dimCount| by one.
27482749
1. Set |rankOutput| to |rankOutput| + |dimCount|.
27492750
1. Let |dimCount| be zero.
2750-
1. [=map/For each=] |size| → |value| of |shapeInput|:
2751+
1. [=list/For each=] |size| of |shapeInput|:
27512752
1. If |dimCount| is less than or equal to |axis| then [=iteration/continue=].
27522753
1. Set |shapeOutput|[|rankOutput| + |dimCount| - |axis| - 1] to |size|.
27532754
1. Increment |dimCount| by one.

0 commit comments

Comments
 (0)