From 233fad99cf7c863e842614ad28720f27ced6d290 Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Mon, 8 Apr 2024 16:27:51 -0700 Subject: [PATCH 1/6] 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 --- index.bs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/index.bs b/index.bs index d124fd9f..ae731fa1 100644 --- a/index.bs +++ b/index.bs @@ -2714,40 +2714,41 @@ partial interface MLGraphBuilder {
**Arguments:** - *input*: an {{MLOperand}}. The input N-D tensor from which the values are gathered. - - *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*. + - *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. - *options*: an optional {{MLGatherOptions}}. The optional parameters of the operation. **Returns:** an {{MLOperand}}. The output N-D tensor of [=MLOperand/rank=] equal to the [=MLOperand/rank=] of *input* + the [=MLOperand/rank=] of *indices* - 1.
+
+ 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. +
+
The gather(|input|, |indices|, |options|) method steps are: - 1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input| abd |indices| returns false, then [=exception/throw=] a {{TypeError}}. + 1. If [=MLGraphBuilder/validating operand=] with [=this=] and any of |input| and |indices| returns false, then [=exception/throw=] a {{TypeError}}. 1. If |indices|'s [=MLOperand/dataType=] is neither {{MLOperandDataType/"uint32"}} nor {{MLOperandDataType/"int64"}}, then [=exception/throw=] a {{TypeError}}. 1. Let |shapeInput| be |input|'s [=MLOperand/shape=] and |rankInput| be |shapeInput|'s [=MLOperand/rank=]. 1. Let |shapeIndices| be |indices|'s [=MLOperand/shape=]. 1. Let |axis| be |options|.{{MLGatherOptions/axis}}. - 1. Let |axisSize| be |input|'s [=MLOperand/shape=][|axis|] 1. If |axis| is greater than or equal to |rankInput|, then [=exception/throw=] a {{TypeError}}. - 1. [=map/For each=] |index| → |value| of |indices|: - 1. If |index| is greater than or equal to |axisSize|, then [=exception/throw=] a {{TypeError}}. 1. Let |dimCount| be zero. 1. Let |rankOutput| be zero. 1. Let |shapeOutput| be an empty list. - 1. [=map/For each=] |size| → |value| of |shapeInput|: + 1. [=list/For each=] |size| of |shapeInput|: 1. If |dimCount| is equal to |axis| then [=iteration/break=]. 1. Set |shapeOutput|[|dimCount|] to |size|. 1. Increment |dimCount| by one. 1. Set |rankOutput| to |dimCount|. 1. Let |dimCount| be zero. - 1. [=map/For each=] |size| → |value| of |shapeIndices|: + 1. [=list/For each=] |size| of |shapeIndices|: 1. Set |shapeOutput|[|rankOutput| + |dimCount|] to |size|. 1. Increment |dimCount| by one. 1. Set |rankOutput| to |rankOutput| + |dimCount|. 1. Let |dimCount| be zero. - 1. [=map/For each=] |size| → |value| of |shapeInput|: + 1. [=list/For each=] |size| of |shapeInput|: 1. If |dimCount| is less than or equal to |axis| then [=iteration/continue=]. 1. Set |shapeOutput|[|rankOutput| + |dimCount| - |axis| - 1] to |size|. 1. Increment |dimCount| by one. From c0d34698474c45ee2632736ef80b6bdf6acd3853 Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Mon, 8 Apr 2024 17:37:40 -0700 Subject: [PATCH 2/6] Update index.bs Co-authored-by: Ningxin Hu --- index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.bs b/index.bs index ae731fa1..f649af6c 100644 --- a/index.bs +++ b/index.bs @@ -2721,7 +2721,7 @@ partial interface MLGraphBuilder {
- 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. + 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 {{MLGraphBuilder/clamp()}} in the compiled graph if the required clamping behavior is not provided by the underlying platform.
From 446bc773590c1e8814e7b593c6913da7fd3f4b09 Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Wed, 10 Apr 2024 10:35:58 -0700 Subject: [PATCH 3/6] Add note about negative indices. fixes #484 --- index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.bs b/index.bs index f649af6c..1dd8c759 100644 --- a/index.bs +++ b/index.bs @@ -2721,7 +2721,7 @@ partial interface MLGraphBuilder {
- 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 {{MLGraphBuilder/clamp()}} in the compiled graph if the required clamping behavior is not provided by the underlying platform. + 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 {{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.
From 89e59f390352499760149f876130245911b8566f Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Tue, 16 Apr 2024 09:01:22 -0700 Subject: [PATCH 4/6] Update index.bs Co-authored-by: Dwayne Robinson --- index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.bs b/index.bs index 1dd8c759..7e076bd7 100644 --- a/index.bs +++ b/index.bs @@ -2714,7 +2714,7 @@ partial interface MLGraphBuilder {
**Arguments:** - *input*: an {{MLOperand}}. The input N-D tensor from which the values are gathered. - - *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. + - *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 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. - *options*: an optional {{MLGatherOptions}}. The optional parameters of the operation. **Returns:** an {{MLOperand}}. The output N-D tensor of [=MLOperand/rank=] equal to the [=MLOperand/rank=] of *input* + the [=MLOperand/rank=] of *indices* - 1. From 24ba1262a05aac0e7f979942bd16dc82d916bb11 Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Tue, 16 Apr 2024 09:01:32 -0700 Subject: [PATCH 5/6] Update index.bs Co-authored-by: Dwayne Robinson --- index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.bs b/index.bs index 7e076bd7..af8b7ac7 100644 --- a/index.bs +++ b/index.bs @@ -2721,7 +2721,7 @@ partial interface MLGraphBuilder {
- 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 {{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. + 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.
From cc0cf8a15bae63eb3d274d311b085826b0c3b4d5 Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Tue, 16 Apr 2024 09:03:25 -0700 Subject: [PATCH 6/6] Fix grammar glitch --- index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.bs b/index.bs index 6ece1c3e..ff58ae67 100644 --- a/index.bs +++ b/index.bs @@ -2715,7 +2715,7 @@ partial interface MLGraphBuilder {
**Arguments:** - *input*: an {{MLOperand}}. The input N-D tensor from which the values are gathered. - - *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 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. + - *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. - *options*: an optional {{MLGatherOptions}}. The optional parameters of the operation. **Returns:** an {{MLOperand}}. The output N-D tensor of [=MLOperand/rank=] equal to the [=MLOperand/rank=] of *input* + the [=MLOperand/rank=] of *indices* - 1.