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

Merged
merged 6 commits into from
Aug 11, 2025
Merged
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
32 changes: 29 additions & 3 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -3544,6 +3544,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 @@ -3562,10 +3564,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 @@ -3574,7 +3578,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 @@ -3631,6 +3635,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 @@ -3645,6 +3653,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 @@ -3655,11 +3665,13 @@ 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".
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:
1. If |a|'s [=MLOperand/dataType=] is not {{MLOperandDataType/"uint8"}}, then [=exception/throw=] a {{TypeError}}.
1. If |op| is one of "isNaN", "isInfinite", then:
1. If |a|'s [=MLOperand/dataType=] is not one of « {{MLOperandDataType/"float32"}}, {{MLOperandDataType/"float16"}} », then [=exception/throw=] a {{TypeError}}.
1. If |b| is passed, then:
1. If [=MLGraphBuilder/validating operand=] with [=this=] and |b| returns false, then [=exception/throw=] a {{TypeError}}.
1. If |a|'s [=MLOperand/dataType=] is not equal to |b|'s [=MLOperand/dataType=], then [=exception/throw=] a {{TypeError}}.
Expand Down Expand Up @@ -3749,6 +3761,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