-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "implementation consideration" about how out-of-bound indices of Gather/Scatter should be handled #486
Comments
This issue came up again in this code review. I'll summarize my thoughts here. To review, this constraint is included in non-normative text:
And there are comments in the mojom file like this:
The currently-specified behavior of validating the values of That being said, On backends which guarantee this clamping is performed internally (e.g. DML), user agents may be able to elide this step, whereas for other backends the user agent may need to explicitly clamp the values, similar to how user agents need to insert transposes as needed for certain operations. But of course those are just implementation details at that point I propose we specify that values in
Thoughts? |
* webmachinelearning#483 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
* 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
👍 In lieu of any platform mitigation, that sounds a reasonable default to me. Though, I wouldn't encourage people to rely on that behavior as part of the algorithm itself and still emphasize the passed indices should be valid.
Note the clamping there is just a security mitigation for buffer reads/writes, which ensures the element read is within buffer bounds, and it doesn't clamp garbage indices to the dimension size (like recommended above), but rather clamps the final computed offset to stay within the linear buffer, which is a bit different. One could debate why this choice was made, but it's mainly because it's general logic that applies to multiple different operators that all share the same possibility of reading/writing to arbitrary offsets due to potentially invalid indices, and because it's not really worth optimizing for a degenerate case. |
* 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]>
There are other gather and scatter operators have been proposed to support new transformer models, #375 (comment), including The out-of-bounds indices issue came up again, for example Like the "implementation consideration" of
we may need to add similar note or extend the existing one for the new gather and scatter operators. |
Another open raised by @quidity in code review is whether the safe backend behavior should be specified. Different platform APIs may have different behaviors: As @fdwr commented in previous comment and in code review, DML's gather
for scatter,
CoreML's scatter and gather operators support TensorFlow's
User agent may unify the out-of-bounds indices handling by "introducing
Given that, should we expect the safe backend behavior as "a backend implementation must guarantee index values do not cause invalid reads outside the input tensor (gather) or writes outside the output tensor (scatter)"? (by extending @fdwr 's proposal) |
+1 to @quidity's comment:
I think we're looking at this issue a bit backwards. To quote myself over on #754 (comment):
Our goal should be to specify (unsurprising) behavior that can be implemented consistently across backends. How the backends comply with this spec (e.g. by adding a If we specify that indices must be clamped, then DML's default "ignore the index" approach would not be compliant with the spec. Have we considered specifying the "ignore the index" approach? This seems like it should be implementable on all platforms using the
The issue here isn't with clamping, but with collisions (which may occur as a result of clamping). To riff off of @fdwr's example, what is the expected behavior of the following?
Possible outcomes:
|
In proposal Add support for operations needed for well-known transformers , the validation steps of
indices
operand are defined as:The index value of
indices
operand may not be available at the graph build time, e.g., whenindices
is an input operand or an intermediate operand produced by another operator. It should define the run-time behavior if the index value of indices is greater than or equal to axisSize.In that proposal, @wacky6 also recommended to add a "implementation consideration" (i.e. an informative section) mentioning how out-of-bound indices should be handled by browser vendor and platform implementation with some bullet points:
/cc @anssiko @wchao1115 @fdwr
The text was updated successfully, but these errors were encountered: