Skip to content

Use std::in_range for integer range checking#179400

Open
lakshayg wants to merge 2 commits into
pytorch:mainfrom
lakshayg:cpp20-in-range
Open

Use std::in_range for integer range checking#179400
lakshayg wants to merge 2 commits into
pytorch:mainfrom
lakshayg:cpp20-in-range

Conversation

@lakshayg

@lakshayg lakshayg commented Apr 5, 2026

Copy link
Copy Markdown
Collaborator

C++20 provides std::in_range which allows checking
if an integer value can fit in a given integer type. This PR
replaces such checks with in_range.

std::in_range is safer than direct comparisons
because it avoids implicit casts.

The call sites to update were obtained by grepping
for "numeric_limits.*min".

PyTorch supports C++20 now #176662

I have also proposed a clang-tidy checker to take
care of this automatically going forward llvm/llvm-project#190665

@lakshayg lakshayg self-assigned this Apr 5, 2026
@lakshayg lakshayg added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 5, 2026
@lakshayg lakshayg requested a review from janeyx99 as a code owner April 5, 2026 07:21
@lakshayg lakshayg added the topic: not user facing topic category label Apr 5, 2026
@pytorch-bot

pytorch-bot Bot commented Apr 5, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/179400

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures, 9 Unrelated Failures, 8 Unclassified Failures

As of commit 27907b0 with merge base b9a2479 (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions

github-actions Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

Attention! PyTorch one of the C-stable API file was changed

You MUST NOT change existing function declarations in this, as this header defines a stable C ABI. If you need to change the signature for a function, introduce a new v2 version of the function and modify code generation to target the new version of the function.


Resolved

Comment thread torch/csrc/stable/c/shim_function_versions.txt

@Skylion007 Skylion007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

So the reason this is useful is removing that possibility of accidental implicit casts when reading the doc and it's therefore safer. You should update the PR description to reflect that.

@Skylion007 Skylion007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not sure about the stable API stuff

Comment thread torch/csrc/shim_common.cpp Outdated
@lakshayg

lakshayg commented Apr 5, 2026

Copy link
Copy Markdown
Collaborator Author

Yeah, I am not sure either. I believe @janeyx99 will be able to check if this makes sense or should be reverted.

@lakshayg lakshayg force-pushed the cpp20-in-range branch 2 times, most recently from 96c22f6 to 81764b7 Compare April 7, 2026 07:52
@liangel-02 liangel-02 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 7, 2026
@lakshayg lakshayg force-pushed the cpp20-in-range branch 3 times, most recently from 1b511df to 76b2297 Compare April 13, 2026 22:10
@lakshayg

Copy link
Copy Markdown
Collaborator Author

@pytorchmergebot merge

@pytorch-bot

pytorch-bot Bot commented Apr 28, 2026

Copy link
Copy Markdown

This PR has pending changes requested. Please address the comments and update the PR before merging.

@lakshayg

Copy link
Copy Markdown
Collaborator Author

@Skylion007 can you please re-review? I have reverted the changes related to stable API

@janeyx99 janeyx99 requested a review from Skylion007 April 29, 2026 15:48
@janeyx99 janeyx99 dismissed Skylion007’s stale review April 29, 2026 15:49

PR author addressed by reverting

@janeyx99

Copy link
Copy Markdown
Contributor

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Rebase failed due to Command git -C /home/runner/work/pytorch/pytorch rebase refs/remotes/origin/viable/strict pull/179400/head returned non-zero exit code 1

Rebasing (1/1)
Auto-merging aten/src/ATen/native/Pool.h
CONFLICT (content): Merge conflict in aten/src/ATen/native/Pool.h
Auto-merging torch/csrc/stable/tensor_struct.h
Auto-merging torch/nativert/graph/Serialization.cpp
error: could not apply cf1a505a031... Use std::in_range for integer range checking
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply cf1a505a031... # Use std::in_range for integer range checking

Raised by https://github.com/pytorch/pytorch/actions/runs/26020550472

@cyyever

cyyever commented May 20, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 17 checks: pull / linux-jammy-py3.14-clang18 / test-osdc (openreg, 1, 1, mt-l-x86iavx512-8-64), pull / linux-jammy-py3.14-clang18 / test (openreg, 1, 1, linux.2xlarge), pull / linux-jammy-aarch64-py3.10 / test (openreg, 1, 1, linux.arm64.m8g.4xlarge), pull / linux-jammy-aarch64-py3.10 / test-osdc (openreg, 1, 1, mt-l-arm64g4-16-62), pull / linux-jammy-py3.10-clang18 / test-osdc (openreg, 1, 1, mt-l-x86iavx512-8-64), pull / linux-jammy-py3.10-clang18 / test (onnx, 1, 1, linux.4xlarge), pull / linux-jammy-py3.10-clang18 / test (openreg, 1, 1, linux.2xlarge), pull / linux-jammy-py3.14t-clang18 / test-osdc (openreg, 1, 1, mt-l-x86iavx512-8-64), pull / linux-jammy-py3.10-gcc11 / test (openreg, 1, 1, linux.2xlarge), pull / linux-jammy-py3.10-gcc11 / test-osdc (openreg, 1, 1, mt-l-x86iavx512-8-64), pull / linux-jammy-py3.14t-clang18 / test (openreg, 1, 1, linux.2xlarge), pull / linux-jammy-py3.10-clang18-asan / test (openreg, 1, 1, linux.4xlarge), pull / linux-jammy-py3.10-clang18-asan / test-osdc (openreg, 1, 1, mt-l-x86iavx512-16-128), trunk / linux-jammy-aarch64-py3.10 / test-osdc (default, 4, 5, mt-l-arm64g3-16-62), trunk / linux-jammy-aarch64-py3.10 / test (default, 2, 5, linux.arm64.m7g.4xlarge), trunk / win-vs2022-cpu-py3 / test (openreg, 1, 1, windows.4xlarge.nonephemeral), trunk / macos-py3-arm64 / test (openreg, 1, 1, macos-m1-stable)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit to khushi-411/pytorch that referenced this pull request May 20, 2026
C++20 provides [`std::in_range`](https://en.cppreference.com/w/cpp/utility/in_range.html) which allows checking
if an integer value can fit in a given integer type. This PR
replaces such checks with in_range.

std::in_range is safer than direct comparisons
because it avoids implicit casts.

The call sites to update were obtained by grepping
for `"numeric_limits.*min"`.

PyTorch supports C++20 now pytorch#176662

I have also proposed a clang-tidy checker to take
care of this automatically going forward llvm/llvm-project#190665
Pull Request resolved: pytorch#179400
Approved by: https://github.com/janeyx99
pytorchmergebot pushed a commit to weishi-deng/pytorch that referenced this pull request May 21, 2026
C++20 provides [`std::in_range`](https://en.cppreference.com/w/cpp/utility/in_range.html) which allows checking
if an integer value can fit in a given integer type. This PR
replaces such checks with in_range.

std::in_range is safer than direct comparisons
because it avoids implicit casts.

The call sites to update were obtained by grepping
for `"numeric_limits.*min"`.

PyTorch supports C++20 now pytorch#176662

I have also proposed a clang-tidy checker to take
care of this automatically going forward llvm/llvm-project#190665
Pull Request resolved: pytorch#179400
Approved by: https://github.com/janeyx99
@atalman

atalman commented May 21, 2026

Copy link
Copy Markdown
Collaborator

@claude review if this PR caused following failures: https://github.com/pytorch/pytorch/actions/runs/26198707681/job/77106554779

 File "/pytorch/.ci/pytorch/smoke_test/check_binary_symbols.py", line 494, in <module>
    main()
  File "/pytorch/.ci/pytorch/smoke_test/check_binary_symbols.py", line 487, in main
    check_stable_api_symbols(install_root)
  File "/pytorch/.ci/pytorch/smoke_test/check_binary_symbols.py", line 293, in check_stable_api_symbols
    symbols_stable = _compile_and_extract_symbols(
  File "/pytorch/.ci/pytorch/smoke_test/check_binary_symbols.py", line 140, in _compile_and_extract_symbols
    raise RuntimeError(f"Compilation failed: {result.stderr}")
RuntimeError: Compilation failed: In file included from /opt/python/cp310-cp310/bin/../lib/python3.10/site-packages/torch/include/torch/headeronly/util/Exception.h:4,
                 from /opt/python/cp310-cp310/bin/../lib/python3.10/site-packages/torch/include/torch/csrc/inductor/aoti_torch/c/shim.h:6,
                 from /opt/python/cp310-cp310/bin/../lib/python3.10/site-packages/torch/include/torch/csrc/stable/accelerator.h:3,
                 from /tmp/tmptdpe3lcy/test.cpp:2:
/opt/python/cp310-cp310/bin/../lib/python3.10/site-packages/torch/include/torch/csrc/stable/tensor_struct.h: In member function ‘int8_t torch::stable::Tensor::get_device() const’:
/opt/python/cp310-cp310/bin/../lib/python3.10/site-packages/torch/include/torch/csrc/stable/tensor_struct.h:309:14: error: ‘in_range’ is not a member of ‘std’
  309 |         std::in_range<int8_t>(device_index),
      |              ^~~~~~~~
/opt/python/cp310-cp310/bin/../lib/python3.10/site-packages/torch/include/torch/headeronly/macros/Macros.h:202:64: note: in definition of macro ‘C10_UNLIKELY’
  202 | #define C10_UNLIKELY(expr) (__builtin_expect(static_cast<bool>(expr), 0))
      |                                                                ^~~~

@claude

claude Bot commented May 21, 2026

Copy link
Copy Markdown

Claude finished @atalman's task in 49s —— View job


CI Failure Diagnosis for PR #179400

  • Read the failing file (torch/csrc/stable/tensor_struct.h)
  • Diagnose the root cause
  • Provide fix guidance

Root Cause

This is the same issue that caused the original revert. The fix was not applied before re-merging.

At torch/csrc/stable/tensor_struct.h:309:

std::in_range<int8_t>(device_index),

std::in_range is a C++20 feature (<utility>). The stable API headers in torch/csrc/stable/ are consumed by external projects (like vllm) that compile with C++17 (-std=gnu++17). The check_stable_api_symbols smoke test validates this by compiling stable headers with C++17, and it correctly catches the failure:

error: 'in_range' is not a member of 'std'

Fix

Revert line 309 in torch/csrc/stable/tensor_struct.h back to the C++17-compatible bounds check:

STD_TORCH_CHECK(
    device_index >= std::numeric_limits<int8_t>::min() &&
        device_index <= std::numeric_limits<int8_t>::max(),
    "Device index is out of range of return type int8_t, please use get_device_index() instead.");

The rest of the std::in_range changes (in non-stable headers/source files) are fine since PyTorch itself compiles with C++20. Only torch/csrc/stable/ must remain C++17-compatible because external consumers compile these headers with older standards.

TL;DR: The torch/csrc/stable/ directory must not use C++20 features. This file needs to be excluded from the std::in_range refactoring.


@atalman

atalman commented May 21, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot revert -c nosignal -m "Broke nightly builds. Please test with ciflow/binaries when remerging"

@atalman atalman added the ciflow/binaries Trigger all binary build and upload jobs on the PR label May 21, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@lakshayg your PR has been successfully reverted.

pytorchmergebot added a commit that referenced this pull request May 21, 2026
This reverts commit d7e3761.

Reverted #179400 on behalf of https://github.com/atalman due to Broke nightly builds. Please test with ciflow/binaries when remerging ([comment](#179400 (comment)))
@atalman atalman mentioned this pull request May 21, 2026
1 task
@lakshayg

Copy link
Copy Markdown
Collaborator Author

@pytorchmergebot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Rebase failed due to

Aborting rebase because rebasing the branch resulted in the same sha as the target branch.
This usually happens because the PR has already been merged.  Please rebase locally and push.

Raised by https://github.com/pytorch/pytorch/actions/runs/26299810141

@lakshayg lakshayg force-pushed the cpp20-in-range branch 2 times, most recently from 5a92e3d to dba62d8 Compare May 26, 2026 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/binaries Trigger all binary build and upload jobs on the PR ciflow/trunk Trigger trunk jobs on your pull request ciflow/vllm Merged open source Reverted topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants