Skip to content

Let PyTorch choose the C++ standard when building ops - #341

Merged
jithunnair-amd merged 1 commit into
ROCm:masterfrom
leconcio:cxx20-for-pytorch-2.14
Oct 19, 2026
Merged

jithunnair-amd merged 1 commit into
ROCm:masterfrom
leconcio:cxx20-for-pytorch-2.14

Conversation

@leconcio

@leconcio leconcio commented Oct 16, 2026 •

Copy link
Copy Markdown

Why

PyTorch 2.14 is now an official ROCm Docker release for ROCm 10.1 and above, and on it apex ops fail to build, whether they are JIT-built on first use or prebuilt with APEX_BUILD_*=1.

torch.utils.cpp_extension compiles extensions with -std=c++20 since PyTorch 2.12, but CUDAOpBuilder appends -std=c++17 to the host C++ flags (cxx_args()) and to the ROCm hipcc flags (nvcc_args()). The later flag wins, so every op is compiled as C++17. That still worked on 2.12 and 2.13, whose headers accept C++17, but the PyTorch 2.14 headers require C++20:

ATen/ATen.h: error: #error C++20 or later compatible compiler is required to use ATen.
c10/util/intrusive_ptr.h:775:27: error: 'strong_ordering' in namespace 'std' does not name a type
ATen/core/TensorBase.h:1031:5: error: 'requires' does not name a type

How

Remove every hard-coded -std flag from CUDAOpBuilder, and let torch.utils.cpp_extension choose the standard that matches the installed PyTorch headers:

  • cxx_args(): drop -std=c++17.
  • ROCm branch of nvcc_args(): drop -std=c++17.
  • CUDA branch of nvcc_args(): drop the CUDA-version-based selection (c++20 on CUDA 12.5+, c++17 on CUDA 11 to 12.4, c++14 on older).

torch.utils.cpp_extension adds its own -std whenever none is passed, on both the JIT path and the setup.py path. This is the approach upstream apex took in NVIDIA#2006, and apex no longer needs updating each time PyTorch raises its required standard.

After this change apex sets no -std anywhere. Builders that override cxx_args() or nvcc_args() without calling super() (bnp, and the hipcc flags of amp_C, fused_adam, syncbn, ...) already passed none.

Testing

All runs on MI300X (gfx942) with ROCm images, comparing master (2dfd342) with this branch.

Setup master This PR
PyTorch 2.14, AOT: APEX_BUILD_CPP_OPS=1 APEX_BUILD_CUDA_OPS=1 python setup.py build_ext Fails with the errors above, 0 of 28 ops built 28 of 28 ops built, 0 errors; all 77 compile commands use PyTorch's -std=c++20
PyTorch 2.14, JIT: pip install ., then jit_load() on every builder 27 of 28 fail with the errors above (only bnp, which passes no -std, builds) 28 of 28 ops built; the generated build.ninja files contain only -std=c++20
amp_C.multi_tensor_l2norm on a 1024-element tensor of ones, PyTorch 2.14 Doesn't build Returns 32.0 (expected)

L0 tests on PyTorch 2.14 with this branch (JIT install, APEX_TEST_WITH_ROCM=1 APEX_SKIP_FLAKY_TEST=1):

Suite Result
run_amp 70 ran, OK (9 skipped)
run_fp16util 2 ran, OK
run_optimizers 39 ran, OK (9 skipped)
run_fused_layer_norm 84 of 84 non-ONNX tests passed
run_mlp 6 ran, OK (4 skipped)
run_fused_dense 2 ran, OK
run_transformer 102 ran, OK (44 skipped), including the GPT, BERT and dynamic batch size minimal tests on 8 GPUs

Image: rocm/frameworks-internal:rocm10.2.0a20261008_ubuntu24.04_py3.12_pytorch_2.14.0_5d7f2f0 (torch 2.14.0+rocm10.2.0a20261008, HIP 7.17).

Not tested: CUDA builds. On CUDA 12.0 to 12.4 this changes the standard from c++17 to whatever the installed PyTorch defaults to.

🤖 Generated with Claude Code

Cherry-picked to release/1.14.0 branch via #342

Cherry-picked to release/1.15.0 branch via #343

torch.utils.cpp_extension compiles extensions with -std=c++20 since
PyTorch 2.12, but the op builder appended -std=c++17 to the host C++
flags and to the ROCm hipcc flags. The later flag wins, so every op was
compiled as C++17. PyTorch 2.14 headers require C++20 (ATen.h raises
"C++20 or later compatible compiler is required", and c10 uses
std::strong_ordering and requires-clauses), so every op fails to build,
both when JIT-built on first use and when prebuilt with APEX_BUILD_*=1.

Remove every hard-coded -std flag from the op builder: from cxx_args(),
from the ROCm branch of nvcc_args(), and the CUDA-version-based selection
in the CUDA branch. torch.utils.cpp_extension adds the standard that
matches the installed PyTorch headers whenever no -std flag is passed,
on both the JIT and setup.py paths. This follows NVIDIA#2006, which
made the same change upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leconcio
leconcio force-pushed the cxx20-for-pytorch-2.14 branch from f4e5605 to 8837f88 Compare October 16, 2026 20:08
@leconcio leconcio changed the title Build ops with C++20 on PyTorch 2.12+ Let PyTorch choose the C++ standard when building ops Oct 16, 2026
@jithunnair-amd
jithunnair-amd merged commit 9e2b89f into ROCm:master Oct 19, 2026
2 of 3 checks passed
@jithunnair-amd

Copy link
Copy Markdown
Collaborator

! cherry-pick --onto release/1.14.0,release/1.15.0

@rocm-repo-management-api-6

Copy link
Copy Markdown

Target branch release/1.14.0,release/1.15.0 doesn't exist

Comment processed by Build

@jithunnair-amd

Copy link
Copy Markdown
Collaborator

! cherry-pick --onto release/1.14.0 release/1.15.0

@rocm-repo-management-api-6

Copy link
Copy Markdown

Created branch autogenerated/release/1.14.0_cherry-pick_pr-341 and #342

Created branch autogenerated/release/1.15.0_cherry-pick_pr-341 and #343

Comment processed by Build

leconcio added a commit to ROCm/pytorch that referenced this pull request Oct 21, 2026
Move ROCm/apex release/1.14.0 from a7f8fded to 172d2689, the
release/1.14.0 backport of ROCm/apex#341, which lets PyTorch choose the
C++ standard when building apex ops (PyTorch 2.14+ requires C++20).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leconcio added a commit to ROCm/pytorch that referenced this pull request Oct 21, 2026
Move ROCm/apex release/1.15.0 from 3310832c to 11867be7, the
release/1.15.0 backport of ROCm/apex#341, which lets PyTorch choose the
C++ standard when building apex ops (PyTorch 2.14+ requires C++20).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jithunnair-amd pushed a commit to ROCm/pytorch that referenced this pull request Oct 24, 2026
## Summary
- Update the apex pin in `related_commits` for the 2.14 release.
- Move `ROCm/apex` `release/1.14.0` from `a7f8fded` to `172d2689`, the
`release/1.14.0` backport of
[ROCm/apex#341](ROCm/apex#341). It is the only
commit between the two.
- ROCm/apex#341 removes the hard-coded `-std=c++17` from apex's
`op_builder/builder.py`, so PyTorch's `cpp_extension` picks the C++
standard. PyTorch 2.14+ headers require C++20, so with the old pin the
apex ops (`amp_C`, `fused_layer_norm_cuda`, ...) fail to build, both
when prebuilt and when JIT-compiled on first use.
- The torchvision and torchaudio pins in `related_commits` are
unchanged.

Apex release commit: 172d26895969173213b2b4c206ed074103efe602

## Test plan
- [x] Validate the PyTorch build through the Linux and Windows build CI
jobs: https://github.com/ROCm/TheRock/actions/runs/39976925637

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
jithunnair-amd pushed a commit to ROCm/pytorch that referenced this pull request Oct 24, 2026
## Summary
- Update the apex pin in `related_commits` for the 2.15 release.
- Move `ROCm/apex` `release/1.15.0` from `3310832c` to `11867be7`, the
`release/1.15.0` backport of
[ROCm/apex#341](ROCm/apex#341). It is the only
commit between the two.
- ROCm/apex#341 removes the hard-coded `-std=c++17` from apex's
`op_builder/builder.py`, so PyTorch's `cpp_extension` picks the C++
standard. PyTorch 2.14+ headers require C++20, so with the old pin the
apex ops (`amp_C`, `fused_layer_norm_cuda`, ...) fail to build, both
when prebuilt and when JIT-compiled on first use.
- The torchvision and torchaudio pins in `related_commits` are
unchanged.

Apex release commit: 11867be79cb16fe53bd8b1b898ea38b7508721ff

## Test plan
- [x] Validate the PyTorch build through the Linux and Windows build CI
jobs: https://github.com/ROCm/TheRock/actions/runs/40002541100

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.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.

2 participants