Skip to content

Add isNaN and isInfinite operators #858

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,8 @@ partial interface MLGraphBuilder {
MLOperand logicalXor(MLOperand a,
MLOperand b,
optional MLOperatorOptions options = {});
MLOperand isNaN(MLOperand a, optional MLOperatorOptions options = {});
MLOperand isInfinite(MLOperand a, optional MLOperatorOptions options = {});
};

dictionary MLLogicalNotSupportLimits {
Expand All @@ -3380,10 +3382,12 @@ partial dictionary MLOpSupportLimits {
MLBinarySupportLimits logicalAnd;
MLBinarySupportLimits logicalOr;
MLBinarySupportLimits logicalXor;
MLLogicalNotSupportLimits isNaN;
MLLogicalNotSupportLimits isInfinite;
};
</script>

<div dfn-for="MLGraphBuilder/equal(a, b, options), MLGraphBuilder/notEqual(a, b, options), MLGraphBuilder/greater(a, b, options), MLGraphBuilder/greaterOrEqual(a, b, options), MLGraphBuilder/lesser(a, b, options), MLGraphBuilder/lesserOrEqual(a, b, options), MLGraphBuilder/logicalNot(a, options), MLGraphBuilder/logicalAnd(a, b, options), MLGraphBuilder/logicalOr(a, b, options), MLGraphBuilder/logicalXor(a, b, options)" dfn-type=argument>
<div dfn-for="MLGraphBuilder/equal(a, b, options), MLGraphBuilder/notEqual(a, b, options), MLGraphBuilder/greater(a, b, options), MLGraphBuilder/greaterOrEqual(a, b, options), MLGraphBuilder/lesser(a, b, options), MLGraphBuilder/lesserOrEqual(a, b, options), MLGraphBuilder/logicalNot(a, options), MLGraphBuilder/logicalAnd(a, b, options), MLGraphBuilder/logicalOr(a, b, options), MLGraphBuilder/logicalXor(a, b, options), MLGraphBuilder/isNaN(a, options), MLGraphBuilder/isInfinite(a, options)" dfn-type=argument>
**Arguments:**
- <dfn>a</dfn>: an {{MLOperand}}. The first input tensor.
- <dfn>b</dfn>: an {{MLOperand}}. The second input tensor when specified.
Expand All @@ -3392,7 +3396,7 @@ partial dictionary MLOpSupportLimits {
**Returns:** an {{MLOperand}}. The output tensor that contains the result of element-wise comparison of the two input tensors.
</div>

<table id=constraints-elementwise-logical class='data' link-for="MLGraphBuilder/equal(a, b, options), MLGraphBuilder/notEqual(a, b, options), MLGraphBuilder/greater(a, b, options), MLGraphBuilder/greaterOrEqual(a, b, options), MLGraphBuilder/lesser(a, b, options), MLGraphBuilder/lesserOrEqual(a, b, options), MLGraphBuilder/logicalNot(a, options), MLGraphBuilder/logicalAnd(a, b, options), MLGraphBuilder/logicalOr(a, b, options), MLGraphBuilder/logicalXor(a, b, options)">
<table id=constraints-elementwise-logical class='data' link-for="MLGraphBuilder/equal(a, b, options), MLGraphBuilder/notEqual(a, b, options), MLGraphBuilder/greater(a, b, options), MLGraphBuilder/greaterOrEqual(a, b, options), MLGraphBuilder/lesser(a, b, options), MLGraphBuilder/lesserOrEqual(a, b, options), MLGraphBuilder/logicalNot(a, options), MLGraphBuilder/logicalAnd(a, b, options), MLGraphBuilder/logicalOr(a, b, options), MLGraphBuilder/logicalXor(a, b, options), MLGraphBuilder/isNaN(a, options), MLGraphBuilder/isInfinite(a, options)">
<caption>Constraints for element-wise logical options</caption>
<thead>
<tr>
Expand Down Expand Up @@ -3449,6 +3453,10 @@ partial dictionary MLOpSupportLimits {
:: Support limits for operator {{MLGraphBuilder/logicalOr()}}.
: <dfn>logicalXor</dfn>
:: Support limits for operator {{MLGraphBuilder/logicalXor()}}.
: <dfn>isNaN</dfn>
:: Support limits for operator {{MLGraphBuilder/isNaN()}}.
: <dfn>isInfinite</dfn>
:: Support limits for operator {{MLGraphBuilder/isInfinite()}}.
</dl>

<div>
Expand All @@ -3463,6 +3471,8 @@ partial dictionary MLOpSupportLimits {
- *logicalAnd*: Compute the logical *and* of the two input tensors, element-wise, treating any non-zero value as true and returning elements of 0 or 1.
- *logicalOr*: Compute the logical *or* of the two input tensors, element-wise, treating any non-zero value as true and returning elements of 0 or 1.
- *logicalXor*: Compute the logical *xor* of the two input tensors, element-wise, treating any non-zero value as true and returning elements of 0 or 1.
- *isNaN*: Check if the values of the input tensor are invalid numeric representations (NaN's), element-wise, returning 1's for NaN's and 0 otherwise.
- *isInfinite*: Check if the values of the input tensor are infinite, element-wise, returning 1's for positive or negative infinity and 0 otherwise.
</div>

<div class="note">
Expand All @@ -3473,7 +3483,7 @@ Although operations {{MLGraphBuilder/greaterOrEqual()}} and {{MLGraphBuilder/les
<summary>
To <dfn for="MLGraphBuilder" data-lt="element-wise-logical-op">create an element-wise logical operation</dfn> given [=string=] |op|, {{MLOperand}} |a|, an optional {{MLOperand}} |b|, and {{MLOperatorOptions}} |options|, run the following steps:
</summary>
1. [=Assert=]: |op| is one of "equal", "notEqual", "greater", "greaterOrEqual", "lesser", "lesserOrEqual", "logicalNot", "logicalAnd", "logicalOr", "logicalXor".
1. [=Assert=]: |op| is one of "equal", "notEqual", "greater", "greaterOrEqual", "lesser", "lesserOrEqual", "logicalNot", "logicalAnd", "logicalOr", "logicalXor", "isNaN", "isInfinite".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose isNan and isInfinite should only support "float32" and "float16" data types. You may want to add step to validate that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. The alternative is that we take all data types, and for any non-float data types (int32, uint8...), WebNN just returns a tensor of all false 0's, which makes the operator more cleanly generic, but then I don't know any ML library that accepts integers to isNaN anyway, making it unlikely to reach WebNN.

Hmm, I don't like what we currently have where most operators visible show their data types in a table, but some operators instead bury data types inside algorithmic steps. Maybe I can cleanly promote this up into the existing table instead "Constraints for element-wise logical options"... ⏳

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then I don't know any ML library that accepts integers to isNaN anyway, making it unlikely to reach WebNN.

Right. And the native framework may also reject integers for isNan and isInfinite, such as ONNX Runtime: IsNaN, IsInf

Hmm, I don't like what we currently have where most operators visible show their data types in a table, but some operators instead bury data types inside algorithmic steps. Maybe I can cleanly promote this up into the existing table instead "Constraints for element-wise logical options"

That's true. The latter was introduced for op categories with the intention of minimum change, see discussion at original PR: #657 (comment).

For this PR, I'd recommend to keep the change minimum with two options:

Option 1: Add a step into the "create an element-wise logical operation" algorithm, similar to validation step for "logicalNot":

  1. If op is one of "isNan" and "isIfinite, then:
    1. If a ’s dataType is neither "float32" nor "float16 , then throw a TypeError .

Option 2: Add an optional allowedDataTyeps argument into "create an element-wise logical operation" algorithm (similar to "create an element-wise unary operation").

And add a step:

If allowedDataTypes is given and it does not contain input ’s dataType , then throw a TypeError .

With that, we can call "create an element-wise logical operation" with « "float32" , "float16" » for "isNan" and "IsInfinite". The validation step of logicalNot can be removed, we can call "create an element-wise logical operation" with « "uint8" » instead.

1. If [=this=] [=MLGraphBuilder/can not build=], then [=exception/throw=] an "{{InvalidStateError}}" {{DOMException}}.
1. If [=MLGraphBuilder/validating operand=] with [=this=] and |a| returns false, then [=exception/throw=] a {{TypeError}}.
1. If |op| is one of "logicalNot", "logicalAnd", "logicalOr", "logicalXor", then:
Expand Down Expand Up @@ -3567,6 +3577,20 @@ Although operations {{MLGraphBuilder/greaterOrEqual()}} and {{MLGraphBuilder/les
1. If that [=exception/throws=] an error, then re-[=exception/throw=] the error.
1. Return |output|.
</div>

<div algorithm>
The <dfn method for=MLGraphBuilder>isNaN(|a|, |options|)</dfn> method steps are:
1. Let |output| be the result of [=MLGraphBuilder/element-wise-logical-op|creating an element-wise logical operation=] given "isNaN", |a|, and |options|.
1. If that [=exception/throws=] an error, then re-[=exception/throw=] the error.
1. Return |output|.
</div>

<div algorithm>
The <dfn method for=MLGraphBuilder>isInfinite(|a|, |options|)</dfn> method steps are:
1. Let |output| be the result of [=MLGraphBuilder/element-wise-logical-op|creating an element-wise logical operation=] given "isInfinite", |a|, and |options|.
1. If that [=exception/throws=] an error, then re-[=exception/throw=] the error.
1. Return |output|.
</div>
</details>

### Element-wise unary operations ### {#api-mlgraphbuilder-unary}
Expand Down