Skip to content

Conversation

@ev-br
Copy link
Member

@ev-br ev-br commented Jan 9, 2026

Work around pytorch/pytorch#172067 :

>>> torch.clamp(torch.ones(3), None, torch.as_tensor(float('nan')))
tensor([nan, nan, nan])
>>> torch.clamp(torch.ones(3), None, float('nan'))     # oops
tensor([1., 1., 1.])

Tested locally with (yes, $10^5$ examples)

$ ARRAY_API_TESTS_SKIP_DTYPES=uint16,uint32,uint64 ARRAY_API_TESTS_MODULE=array_api_compat.torch pytest array_api_tests/test_operators_and_elementwise_functions.py::test_clip -v --max-examples 100_000 -s

which passes.

Copilot AI review requested due to automatic review settings January 9, 2026 12:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a PyTorch bug where torch.clamp incorrectly handles NaN values differently depending on whether they are passed as scalars or tensors (pytorch/pytorch#172067). The fix adds special handling for NaN values when both min and max are scalars.

Key changes:

  • Added import math for NaN detection
  • Refactored clip function logic to handle mixed scalar/tensor signatures better
  • Added special case to return NaN-filled tensors when NaN bounds are provided as scalars

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

min: int | float | Array | None = None,
max: int | float | Array | None = None,
**kwargs
) -> Array:
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

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

Removing **kwargs from the function signature breaks support for the out= parameter. The test_clip_out test in tests/test_common.py expects torch's clip to support out=, which torch.clamp does support. The **kwargs should be retained in the signature and passed to torch.clamp calls at lines 865 and 877 to maintain backward compatibility.

Copilot uses AI. Check for mistakes.
@ev-br
Copy link
Member Author

ev-br commented Jan 9, 2026

Merging as a follow-up to gh-353 to fix an edge case which surfaced in more thorough testing.

@ev-br ev-br added the bug Something isn't working label Jan 9, 2026
@ev-br ev-br added this to the 1.14 milestone Jan 9, 2026
@ev-br ev-br merged commit 1a077c4 into data-apis:main Jan 9, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant