Skip to content

Commit d846723

Browse files
inexorabletashhuningxinfdwr
authored
gather(): Address indices validation and other algorithm nits (#642)
* gather(): Address indices validation and other algorithm nits * #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 #486 * Update index.bs Co-authored-by: Ningxin Hu <[email protected]> * Add note about negative indices. fixes #484 * Update index.bs Co-authored-by: Dwayne Robinson <[email protected]> * Update index.bs Co-authored-by: Dwayne Robinson <[email protected]> * Fix grammar glitch --------- Co-authored-by: Ningxin Hu <[email protected]> Co-authored-by: Dwayne Robinson <[email protected]>
1 parent 05b15b1 commit d846723

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

index.bs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,40 +2715,41 @@ partial interface MLGraphBuilder {
27152715
<div>
27162716
**Arguments:**
27172717
- *input*: an {{MLOperand}}. The input N-D tensor from which the values are gathered.
2718-
- *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*.
2718+
- *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 must be in 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.
27192719
- *options*: an optional {{MLGatherOptions}}. The optional parameters of the operation.
27202720

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

2724+
<div class="note">
2725+
The {{MLGraphBuilder/gather(input, indices, options)/indices}} parameter to {{MLGraphBuilder/gather()}} can not be clamped to the allowed range when the graph is built because the inputs are not known until execution. Implementations can introduce {{MLGraphBuilder/clamp()}} in the compiled graph if the required clamping behavior is not provided by the underlying platform. Similarly, if the underlying platform does not support negative indices, the implementation can introduce operations in the compiled graph to transform a negative index from the end of the dimension into a positive index.
2726+
</div>
2727+
27242728
<details open algorithm>
27252729
<summary>
27262730
The <dfn method for=MLGraphBuilder>gather(|input|, |indices|, |options|)</dfn> method steps are:
27272731
</summary>
2728-
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input| abd |indices| returns false, then [=exception/throw=] a {{TypeError}}.
2732+
1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input| and |indices| returns false, then [=exception/throw=] a {{TypeError}}.
27292733
1. If |indices|'s [=MLOperand/dataType=] is neither {{MLOperandDataType/"uint32"}} nor {{MLOperandDataType/"int64"}}, then [=exception/throw=] a {{TypeError}}.
27302734
1. Let |shapeInput| be |input|'s [=MLOperand/shape=] and |rankInput| be |shapeInput|'s [=MLOperand/rank=].
27312735
1. Let |shapeIndices| be |indices|'s [=MLOperand/shape=].
27322736
1. Let |axis| be |options|.{{MLGatherOptions/axis}}.
2733-
1. Let |axisSize| be |input|'s [=MLOperand/shape=][|axis|]
27342737
1. If |axis| is greater than or equal to |rankInput|, then [=exception/throw=] a {{TypeError}}.
2735-
1. [=map/For each=] |index| → |value| of |indices|:
2736-
1. If |index| is greater than or equal to |axisSize|, then [=exception/throw=] a {{TypeError}}.
27372738
1. Let |dimCount| be zero.
27382739
1. Let |rankOutput| be zero.
27392740
1. Let |shapeOutput| be an empty list.
2740-
1. [=map/For each=] |size| → |value| of |shapeInput|:
2741+
1. [=list/For each=] |size| of |shapeInput|:
27412742
1. If |dimCount| is equal to |axis| then [=iteration/break=].
27422743
1. Set |shapeOutput|[|dimCount|] to |size|.
27432744
1. Increment |dimCount| by one.
27442745
1. Set |rankOutput| to |dimCount|.
27452746
1. Let |dimCount| be zero.
2746-
1. [=map/For each=] |size| → |value| of |shapeIndices|:
2747+
1. [=list/For each=] |size| of |shapeIndices|:
27472748
1. Set |shapeOutput|[|rankOutput| + |dimCount|] to |size|.
27482749
1. Increment |dimCount| by one.
27492750
1. Set |rankOutput| to |rankOutput| + |dimCount|.
27502751
1. Let |dimCount| be zero.
2751-
1. [=map/For each=] |size| → |value| of |shapeInput|:
2752+
1. [=list/For each=] |size| of |shapeInput|:
27522753
1. If |dimCount| is less than or equal to |axis| then [=iteration/continue=].
27532754
1. Set |shapeOutput|[|rankOutput| + |dimCount| - |axis| - 1] to |size|.
27542755
1. Increment |dimCount| by one.

0 commit comments

Comments
 (0)