[Bug]: C++17 build cannot compile torch 2.12 headers with nvcc, but C++20 breaks float2(fp8) functional casts
System Info
- CPU architecture: x86_64
- GPU: not required to reproduce (compile-only)
- TensorRT-LLM commit:
4476ee3eacc1639c28f7e3e845b67ec246f2e482(main) - CUDA: 13.4, nvcc V13.4.59
- Host compiler: gcc 13.3.0 (Ubuntu 24.04)
- Container:
nvcr.io/nvidia/cuda-dl-base:26.08-cuda13.4-devel-ubuntu24.04 - torch: 2.12.0+cu130 (from
--extra-index-url https://download.pytorch.org/whl/cu130, as pinned byrequirements.txt)
Who can help?
Build / C++ runtime.
Information about the problem
Building outside the prebuilt NGC container hits a C++ standard conflict that originates entirely inside this repository:
requirements.txt:29pinstorch>=2.12.0a0,<=2.13.0a0.- torch 2.12 ships
share/cmake/Torch/TorchConfig.cmake:158withCXX_STANDARD 20, andshare/cmake/Caffe2/public/utils.cmake:340sets the same property on its targets. torch 2.12 therefore requires C++20. cpp/CMakeLists.txt:343setsCMAKE_CXX_STANDARD 17, and line 346 derivesCMAKE_CUDA_STANDARDfrom it.
The two requirements cannot both be satisfied:
At C++17, nvcc cannot compile torch 2.12 headers. Any .cu that transitively
includes ATen/core/function_schema.h fails:
ATen/core/List_inl.h:202:49: error: need 'typename' before
'decltype(((c10::List<T>*)this)->c10::List<T>::impl_->list)::difference_type'
because '...' is a dependent scope
This reaches several targets through pgUtils.h and thUtils.h, for example
tensorrt_llm/kernels/trtllmGenKernels/blockScaleMoe/runner.cu.
At C++20, torch compiles, but 45 functional casts in
cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttentionUtils.h break, producing
over 150 diagnostics:
decoderMaskedMultiheadAttentionUtils.h(1210): error: no suitable conversion function
from "const __nv_fp8x2_e4m3" to "float" exists
The cause is parenthesized aggregate initialization, added in C++20. float2(x) is a
functional cast in C++17 and selects __nv_fp8x2_e4m3::operator float2(). In C++20 the
same expression is an aggregate initialization of float2, so the compiler tries to
initialize the first member float x from the fp8 value and fails.
Reproduction
Three lines, no GPU and no TensorRT-LLM checkout required:
#include <cuda_fp8.h>
__global__ void k(const __nv_fp8x2_e4m3 a, float2* o) { *o = float2(a); }
nvcc -std=c++17 -arch=sm_90 -c repro.cu # succeeds
nvcc -std=c++20 -arch=sm_90 -c repro.cu # fails with the error above
The full build was run as:
python scripts/build_wheel.py --cpp_only --use_ccache \
-a "90-real;100-real;103-real" -j 48 --no-venv
Suggested fix
Replacing the functional cast with an explicit static_cast compiles under both
standards and keeps the same conversion operator, so the generated code is unchanged:
- float2 fb0 = float2(fp8_2[0]);
+ float2 fb0 = static_cast<float2>(fp8_2[0]);
Verified:
| expression | C++17 | C++20 |
|---|---|---|
float2(a) |
compiles | fails |
static_cast<float2>(a) |
compiles | compiles |
45 lines in decoderMaskedMultiheadAttentionUtils.h use the affected pattern. The
change is mechanical and safe at C++17, so it does not require the project to move to
C++20; it only removes the blocker if and when the standard is raised.
I am happy to open a PR with this change if the approach looks right.
Some follow-up measurements, in case they help scope this.
The affected construct is not limited to float2. In
decoderMaskedMultiheadAttentionUtils.h the same C++20 problem appears with three
vector types:
| construct | occurrences |
|---|---|
float2(x) |
45 |
float4(x) |
1 |
half2(x) |
1 |
Effect on a full build. Same tree and flags, only CMAKE_CUDA_STANDARD raised
to 20:
| C++17 | C++20, before #19060 | C++20, with #19060 | |
|---|---|---|---|
torch header errors (ATen/core/List_inl.h:202) |
fails every .cu that includes it |
0 | 0 |
no suitable conversion from const __nv_fp8* |
0 | 368 | 0 |
| build progress reached | blocked early | 80% | 94% |
So raising the standard resolves the torch side, and #19060 resolves the fp8 side.
Together they take a --cpp_only build from blocked to 94%.
One unrelated blocker remains after both, at
cpp/tensorrt_llm/kernels/cutlass_kernels/moe_gemm/launchers/moe_gemm_tma_ws_launcher.inl:78:
return ReturnType{std::forward<Args>(args)...};
error: a value of type "const float **" cannot be used to initialize an entity of type "float"
This is a different construct from the functional casts and I have not investigated it. Happy to open a separate issue if that is preferred.
Note on scope. I am not proposing that the project move to C++20. The
observation is only that requirements.txt already pins a torch release that
declares CXX_STANDARD 20, so the two settings in this repository are currently
inconsistent for anyone building outside the prebuilt container. Whether the fix is
to raise the standard, pin an older torch, or document the supported combination is
your call.
Follow-up: the remaining blocker I mentioned above is now filed separately as #19063.
With #19060 applied, a --cpp_only build at C++20 reaches 95% and #19063 is the only
file left producing errors (24 diagnostics, all from one line instantiated 24 times).
So the C++20 picture is:
| blocker | status |
|---|---|
| torch 2.12 headers need C++20 under nvcc | this issue (#19059) |
float2 / float4 / half2 functional casts break at C++20 |
#19060 |
construct_if_true braced init breaks at C++20 |
#19063 |
Status update, since the picture changed since my last comment and the remaining step has not been stated anywhere yet.
Where each blocker stands
| blocker | fix | state |
|---|---|---|
| torch headers need C++20 under nvcc | this issue | open |
float2 / float4 / half2 functional casts break at C++20 |
#19060 | CI green, awaiting review |
construct_if_true braced init breaks at C++20 (#19063) |
#19065 | approved, not merged |
CMAKE_CUDA_STANDARD still follows CMAKE_CXX_STANDARD (17) |
not filed yet | see below |
The remaining change
cpp/CMakeLists.txt:346 ties the CUDA standard to the C++ standard:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_STANDARD ${CMAKE_CXX_STANDARD})
so nvcc still gets -std=c++17 even though TorchConfig.cmake asks for CXX_STANDARD 20.
Fixing the two compile errors above is necessary but not sufficient; this line is what
actually selects the standard.
I have not filed a PR for it because it would fail CI on today's main: raising the
standard without #19060 and #19065 in place reintroduces exactly the errors those two
remove. The sequence that works is #19060 → #19065 → this one-liner. I will file it once
the first two land, or sooner if you would rather review them as a stack.
End-to-end result with all three applied
For what it is worth as validation rather than as a request: with #19060, #19065 and the
CMAKE_CUDA_STANDARD 20 change applied to 4476ee3e, a full build at C++20 completes,
and the resulting tensorrt_llm (1.3.0rc26) ran an MLPerf Inference v6.0 llama2-70b
Offline workload to a VALID result on B300 hardware. So the three changes together do not
just get the build past the errors; the library they produce works.
Happy to split, rebase or re-scope any of these if a different shape is easier to review.