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

Conversation

fdwr
Copy link
Collaborator

@fdwr fdwr commented Jun 5, 2025

Fixes #811. Since NaN and infinity are closely related concepts, I'm introducing both logical unary operators together for completeness.

partial interface MLGraphBuilder
{
    ...
+   MLOperand isNaN(MLOperand a, optional MLOperatorOptions options = {})
+   MLOperand isInfinite(MLOperand a, optional MLOperatorOptions options = {})
    ...
};

Mappings to backend functions:

Is not-a-number:

  • numpy.isnan
  • tf.math.is_nan
  • torch.isnan
  • onnx.ai.IsNaN
  • DML_OPERATOR_ELEMENT_WISE_IS_NAN
  • mil.ops.defs.iOS15.elementwise_binary.not_equal(a, a)

Is infinite:

  • numpy.isinf
  • tfm.optimization.math.isinf
  • torch.isinf
  • onnx.ai.IsInf
  • DML_OPERATOR_ELEMENT_WISE_IS_INFINITY
  • mil.ops.defs.iOS15.elementwise_unary.abs(..) &&
    mil.ops.defs.iOS15.elementwise_binary.equal(.., Infinity)

Possible question answers:

  • Why name it "isInfinite" rather than "isinf" like Python API's? This isn't Python, it's closer kin to Javascript's isFinite function and Infinite constant, and general API naming guidelines favor whole words for readability.
  • Why not support specific tests for positive infinity or negative infinity like ONNX? I'm not opposed to it, but I'm unsure the use case, most backends don't support it, and one could easily clamp the values before calling isInfinite to emulate it.
  • Do we really need these functions? No - technically they can be emulated via existing logical operators, but backends may have more optimized forms of them, and their existence leads to better discoverability. One compromise would be to instead document a decomposition in the specification (similar to flatten and unsqueeze) in the Operator Emulation section.

Here are some example emulations:

function isNaN(value: Number): Boolean
{
    return value != value;
    // graphBuilder.notEqual(value, value)
    // low level equivalent: return value.reinterpretAs(uint32) & 0x7FFFFFFF) > 0x7F800000;
}

function isNotNaN(value: Number): Boolean
{
    return value == value;
    // graphBuilder.equal(value, value)
}

function isNaNOrInfinity(value: Number): Boolean
{
    return (value * 0) != 0;
    // graphBuilder.notEqual(graphBuilder.mul(x, zeroTensor), zeroTensor)
}

function isNotNaNOrInfinity(value: Number): Boolean
{
    return (value * 0) == 0;
    // graphBuilder.equal(graphBuilder.mul(x, zeroTensor), zeroTensor)
}

Preview | Diff

@fdwr fdwr requested a review from huningxin June 5, 2025 05:41
@fdwr
Copy link
Collaborator Author

fdwr commented Jun 5, 2025

FYI @philloooo.

@fdwr fdwr mentioned this pull request Jun 5, 2025
Copy link
Contributor

@huningxin huningxin left a comment

Choose a reason for hiding this comment

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

LGTM

@fdwr fdwr merged commit 5d157b4 into webmachinelearning:main Aug 11, 2025
2 checks passed
github-actions bot added a commit that referenced this pull request Aug 11, 2025
SHA: 5d157b4
Reason: push, by fdwr

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Behavior when there are NaNs in argmin/max inputs
2 participants