TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

[Bug]: construct_if_true in moe_gemm_tma_ws_launcher.inl fails to compile at C++20

Open 100milliongold opened this issue 2 months ago • 3 comments

System Info

  • CPU architecture: x86_64
  • GPU: not required (compile-only)
  • TensorRT-LLM: main, commit 4476ee3eacc1639c28f7e3e845b67ec246f2e482
  • 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

Who can help?

Build / MoE kernels.

Description

This is a second, separate C++20 blocker found while working on #19059. It is unrelated to the functional-cast problem fixed in #19060, so I am filing it on its own as suggested there.

Building the grouped-GEMM instantiations at C++20 fails in cpp/tensorrt_llm/kernels/cutlass_kernels/moe_gemm/launchers/moe_gemm_tma_ws_launcher.inl:78:

moe_gemm_tma_ws_launcher.inl(78): error: a value of type "const float **" cannot be
used to initialize an entity of type "float"
          return ReturnType{std::forward<Args>(args)...};
                            ^
          detected during instantiation of
          "ReturnType tensorrt_llm::_v1::kernels::cutlass_kernels_oss::construct_if_true<FLAG, ReturnType, Args...>"

The helper is:

// Constructs an object with specific arguments only if flag is true
// This forces the if constexpr branch to properly pruned be when called from in non-template functions
template <bool FLAG, class ReturnType, class... Args>
ReturnType construct_if_true(Args&&... args)
{
    if constexpr (FLAG)
    {
        return ReturnType{std::forward<Args>(args)...};
    }
    else
    {
        return ReturnType{};
    }
}

Call sites are at lines 551, 565, 582, 590 (MainloopArguments) and 610 (EpilogueScalars).

Standard dependence

Same tree, same flags, only -std changed:

target C++17 C++20
build/.../cutlass_instantiations/gemm_grouped/100/cutlass_kernel_file_gemm_grouped_sm100_M128_BS_group0.generated.cu 0 errors 2 errors

In a full --cpp_only build at C++20 (with #19060 applied so the fp8 casts no longer interfere), this is the only file left with errors: 24 diagnostics, all from line 78, which is the same line instantiated 24 times. The build otherwise reaches 95%.

Why C++20 comes up at all

requirements.txt:29 pins torch>=2.12.0a0, and torch 2.12 declares CXX_STANDARD 20 in share/cmake/Torch/TorchConfig.cmake:158. At C++17 nvcc cannot compile the torch headers at all, so raising CMAKE_CUDA_STANDARD is the only way I found to get a build going outside the prebuilt container. Details are in #19059.

What I did not investigate

I have not traced which MainloopArguments / EpilogueScalars member receives the const float **, so I do not know whether the right fix is in the helper, in the call sites, or in the argument structs. I am not proposing a patch for this one, because getting the argument mapping wrong here would silently produce incorrect results rather than a compile error.

Reporting it so the C++20 picture is complete alongside #19059 and #19060.

100milliongold avatar Sep 11 '26 08:09 100milliongold

Traced the root cause. It is the same C++20 feature that caused #19060 (P0960, parenthesized aggregate initialization), but it reaches this code through a type trait rather than through a cast.

The chain

moe_gemm_tma_ws_launcher.inl:604 computes the guard from a trait:

constexpr bool IsSimpleAlphaBeta
    = std::is_constructible_v<EpilogueScalars, ElementAccumulator, ElementAccumulator>;

P0960 makes aggregates constructible from a parenthesized argument list, so this trait flips for aggregate Arguments types:

struct A { float x; float y; };
std::is_constructible_v<A, float, float>
result
C++17 0
C++20 1

Measured with nvcc 13.4 V13.4.59.

When the guard flips to true, if constexpr stops discarding the branch, and the call at line 646 is instantiated for the first time:

else if (tma_ws_input.alpha_scale_ptr_array)
{
    return construct_if_true<(IsSimpleAlphaBeta && !IsFinalizeFusion), EpilogueScalars>(
        tma_ws_input.alpha_scale_ptr_array);      // one argument
}

The instantiation reported in the error is

ReturnType = cutlass::epilogue::fusion::FusionCallbacks<
                 cutlass::epilogue::Sm90PtrArrayTmaWarpSpecialized<...>, ...>::Arguments
Args       = <const float **&>

and that Arguments is declared in cutlass/epilogue/fusion/sm90_callbacks_tma_warpspecialized.hpp as:

struct Arguments {
  ElementScalar alpha = ElementScalar(1);                   // 1
  ElementScalar beta  = ElementScalar(0);                   // 2
  ElementScalar const* alpha_ptr = nullptr;                 // 3
  ElementScalar const* beta_ptr  = nullptr;                 // 4
  ElementScalar const* const* alpha_ptr_array = nullptr;    // 5  <- intended target
  ElementScalar const* const* beta_ptr_array  = nullptr;    // 6
  ...
};

So alpha_scale_ptr_array is the fifth member, but the single-argument call places it in the first, which is float alpha. That is where "const float **" cannot be used to initialize an entity of type "float" comes from.

The sibling call at line 635 in the same lambda passes it in the correct position:

construct_if_true<...>(
    ElementAccumulator(1.f),
    tma_ws_input.ptr_c ? ElementAccumulator(1.f) : ElementAccumulator(0.f),
    nullptr, nullptr,
    tma_ws_input.alpha_scale_ptr_array, nullptr, ...);

So line 646 looks like a latent bug that C++17 never instantiated, rather than a new C++20 incompatibility in that line itself.

A fix I tried and rejected

My first attempt was to restore the C++17 meaning of the guard:

= std::is_constructible_v<EpilogueScalars, ElementAccumulator, ElementAccumulator>
    && !std::is_aggregate_v<EpilogueScalars>;

The trait behaves correctly in isolation (aggregate: 0 under both standards; type with a real constructor: 1 under both). But applied to the real file it produced 100 errors under C++17 as well, which means the guard was already true in the working C++17 build. Recording this so nobody repeats it.

The fix that works

Filling in the four leading members so the pointer lands in alpha_ptr_array:

     return construct_if_true<(IsSimpleAlphaBeta && !IsFinalizeFusion), EpilogueScalars>(
-        tma_ws_input.alpha_scale_ptr_array);
+        ElementAccumulator(1.f),
+        tma_ws_input.ptr_c ? ElementAccumulator(1.f) : ElementAccumulator(0.f),
+        nullptr, nullptr, tma_ws_input.alpha_scale_ptr_array);

The alpha/beta values match the sibling else branch directly below, which is the same case with no alpha_scale_ptr_array.

Verified on cutlass_instantiations/gemm_grouped/100/cutlass_kernel_file_gemm_grouped_sm100_M128_BS_group0.generated.cu:

C++17 C++20 C++20, -arch=sm_107
before 0 errors 2 errors 2 errors
after 0 errors 0 errors 0 errors

PR to follow.

100milliongold avatar Sep 11 '26 08:09 100milliongold

Correction to my earlier comment, and a second rejected fix.

EpilogueScalars is not one type. Instantiations reaching line 646 resolve to at least two shapes with opposite requirements:

type kind how it takes alpha_ptr_array
fusion::FusionCallbacks<Sm90PtrArrayTmaWarpSpecialized<...>, ...>::Arguments aggregate, no constructors 5th member, so a 1-arg braced init lands in float alpha
epilogue::thread::LinearCombination<...>::Params has real constructors Params(ElementCompute const* const*) at linear_combination.h:152 takes it directly

So the original single-argument call is correct for the second type and wrong for the first. My patch in #19060... sorry, in the now-closed #19064, filled in four leading members, which fixed the aggregate case and broke the constructor case:

error: no instance of constructor "cutlass::epilogue::thread::LinearCombination<...>"

Full C++20 build results, same tree, only line 646 differing:

line 646 diagnostics progress
original (1 argument) 24, all const float** -> float 96%
patched (5 arguments) 6, all no instance of constructor 97%

It trades one set of instantiations for the other. I closed that PR.

So the two rejected approaches are:

  1. && !std::is_aggregate_v<EpilogueScalars> on the guard at line 604 — produced 100 errors under C++17 as well, so the guard was already true in the working build.
  2. Filling in the leading members at line 646 — breaks the constructor-bearing type.

A correct fix presumably needs to dispatch on whether EpilogueScalars is an aggregate, or to construct through a named member rather than positionally. I have not verified either, so I am not proposing a patch.

For reference, with #19060 applied and line 646 left untouched, a --cpp_only C++20 build reaches 96% and this is the only remaining failure.

100milliongold avatar Sep 11 '26 08:09 100milliongold

Found the fix, and I have to retract one of my own rejections above.

The !std::is_aggregate_v approach was correct all along. My earlier report that it "produced 100 errors under C++17 as well" was wrong: I split line 604 into two lines without carrying the \ continuation. This .inl is included inside a macro, so every line ends with a backslash aligned at column 317. Dropping it truncated the macro, and the 100 errors came from that, not from the guard.

Re-tested with the continuation preserved:

                 constexpr bool IsSimpleAlphaBeta                                     \
-                    = std::is_constructible_v<EpilogueScalars, ElementAccumulator, ElementAccumulator>; \
+                    = std::is_constructible_v<EpilogueScalars, ElementAccumulator, ElementAccumulator> \
+                        && !std::is_aggregate_v<EpilogueScalars>;                     \
C++17 C++20 C++20, -arch=sm_107
before 0 errors 2 errors 2 errors
after 0 errors 0 errors 0 errors

on cutlass_instantiations/gemm_grouped/100/cutlass_kernel_file_gemm_grouped_sm100_M128_BS_group0.generated.cu, nvcc 13.4 V13.4.59.

Why this is the right shape of fix

The guard's intent appears to be "does this type have a real (alpha, beta) constructor". Under C++17 is_constructible_v answered that, because aggregates were not constructible from a parenthesized list. P0960 removed that distinction, so the guard silently started including aggregates. Spelling the intent out restores the C++17 behaviour for both shapes:

type C++17 C++20 before C++20 after
fusion::FusionCallbacks<...>::Arguments (aggregate) branch discarded branch instantiated, fails branch discarded
thread::LinearCombination<...>::Params (has constructors) branch taken, 1-arg ctor same same

Crucially line 646 stays untouched. It was already correct for the constructor-bearing type, which is why my earlier attempt to "fix" it broke that path. The problem was never the call; it was the guard letting the wrong type reach it.

I will open a PR once the full build confirms it.

100milliongold avatar Sep 11 '26 08:09 100milliongold