Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4c2372b
feat: optimize W4A16 (wMXFP4 x BF16) MoE kernel from TRTLLM PR #12451
samuellees Apr 15, 2026
ac0e82e
chore: address review feedback on W4A16 MoE PR
samuellees Apr 16, 2026
931b87a
style: apply clang-format
samuellees Apr 16, 2026
9c4d204
feat: migrate weight interleaving and scheduler config from TRTLLM PR…
samuellees Apr 16, 2026
31f80e2
feat: migrate CUTLASS mixed-input kernel optimizations from TRTLLM PR…
samuellees Apr 16, 2026
1687648
test: add W4A16 MoE benchmark script
samuellees Apr 17, 2026
e43b3e6
fix: pass device arg to is_sm90a_supported in test
samuellees Apr 18, 2026
be83fd0
revert: restore original CUTLASS extension files, add reference corre…
samuellees Apr 18, 2026
f1ae825
fix: use small configs for strict correctness, large configs for sani…
samuellees Apr 18, 2026
50de6f8
fix: limit correctness configs to h=128 matching upstream test scale
samuellees Apr 18, 2026
9c251a4
fix: use percent-based accuracy check matching TRTLLM methodology
samuellees Apr 19, 2026
7446660
fix: revert test scope to h=128 correctness (matches upstream W4A16 c…
samuellees Apr 19, 2026
f57bf38
style: apply pre-commit hooks (clang-format, ruff)
samuellees Apr 19, 2026
1461814
feat: port TRTLLM PR #12451 mixed-input CUTLASS extensions
samuellees Apr 19, 2026
4ed56ff
feat: wire up W4A16 weight + scale interleave for SM90 mixed-input MoE
samuellees Apr 19, 2026
c4002f1
test: extend W4A16 MoE correctness to K=768/2048/4096 with interleave…
samuellees Apr 19, 2026
31ff00e
test: use 99.9% percent-based check for W4A16 coverage sweep
samuellees Apr 19, 2026
e355831
test: speed up W4A16 reference, trim coverage configs, preprocess ben…
samuellees Apr 19, 2026
2fac475
test: apply W4A16 interleave preprocessing in upstream MoE test
samuellees Apr 19, 2026
4223e45
style: apply pre-commit hooks (clang-format, ruff format)
samuellees Apr 19, 2026
eabea6d
feat: align W4A16 heuristic with TRTLLM PR #12451
samuellees Apr 19, 2026
0e4ba76
fix(test): fix test_moe_w4a8 weight interleaving, act-scale dtype and…
StudyingShao Apr 20, 2026
dcafd6a
remove redundant W4A16 MoE test and benchmark covered by test_trtllm_…
StudyingShao Apr 20, 2026
efd9aff
test: add dedicated W4A16 / W4A8 MoE test files, keep upstream test u…
samuellees Apr 21, 2026
21b5d44
test: trim W4A8 test to supported envelope (h==inter==512, e==2)
samuellees Apr 21, 2026
d3a6190
test: inline SM90 mixed-input W4A16/W4A8 tests into upstream file
samuellees Apr 21, 2026
fc3bb6d
test: add batch_size=1 to W4A8 Hopper correctness
samuellees Apr 21, 2026
77746b8
test: drop m=16 from Hopper mixed-input MoE tests
samuellees Apr 21, 2026
cb90611
refactor: rename interleave_moe_*_for_{Hopper,hopper}_mixed_gemm → sm90
samuellees Apr 21, 2026
335345b
style: apply pre-commit auto-fixes + document sm90 interleave helpers
samuellees Apr 21, 2026
17a9c54
Merge branch 'main' into feat/w4a16-moe-kernel
samuellees Apr 21, 2026
7c2ab34
fix(test): apply sm90 weight/scale interleave in upstream MoE tests
samuellees Apr 22, 2026
1a5b242
Merge branch 'main' into feat/w4a16-moe-kernel
samuellees Apr 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ std::vector<CutlassGemmConfig> get_candidate_configs_sm90(
if (has_w4afp8) {
bool const has_coop_supported = sm90_supports_coop(tile_config);
std::set<MainloopScheduleType> mainloop_schedules{MainloopScheduleType::PINGPONG};
if (has_coop_supported) {
if (has_coop_supported && tile_config != CutlassTileConfigSM90::CtaShape128x128x128B) {
// Due to register pressure on SM90, cooperative scheduler does not
// work with the 128x128x128B tile for mixed-dtype (W4A16) GEMM.
// Only PINGPONG is used for that tile to avoid register overflow.
mainloop_schedules.insert(MainloopScheduleType::COOPERATIVE);
}
auto const epilogue_schedule = EpilogueScheduleType::AUTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "cutlass_extensions/epilogue_helpers.h"
#include "cutlass_extensions/gemm/collective/collective_builder_mixed_input.hpp"
#include "cutlass_extensions/gemm_configs.h"
#include "cutlass/gemm/kernel/tile_scheduler_params.h"

#ifdef __GNUC__ // Check if the compiler is GCC or Clang
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -202,6 +203,11 @@ void sm90_generic_mixed_moe_gemm_kernelLauncher(
reinterpret_cast<StrideD*>(hopper_inputs.stride_d)},
hw_info};

// Optimize tile scheduling for better L2 locality
using RasterOrderOptions = typename cutlass::gemm::kernel::detail::PersistentTileSchedulerSm90Params::RasterOrderOptions;
arguments.scheduler.max_swizzle_size = 2;
arguments.scheduler.raster_order = RasterOrderOptions::Heuristic;

assert(group_size == int(inputs.groupwise_quant_group_size));
if (workspace_size != nullptr) {
*workspace_size = gemm.get_workspace_size(arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2020-2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "moe_gemm_mixed_utils.h"

namespace tensorrt_llm {
namespace kernels {
namespace cutlass_kernels {

/////////////////////////////////////////////////////////////////////////////////////////////////////////

__global__ void interleave_fp4_weights_for_Hopper_mixed_gemm_kernel(
uint8_t* fp4_weight, uint8_t* fp4_weight_interleaved, int const rows, int const cols) {
for (int block_id = blockIdx.x; block_id < rows / 2; block_id += gridDim.x) {
for (int partition_id = threadIdx.y; partition_id < cols / 64; partition_id += blockDim.y) {
Comment on lines +28 to +29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fail fast on unsupported matrix shapes.

These kernels only work when rows is a multiple of 16 and cols is a multiple of 64. On Line 42 and Line 80, row_id + 8 can read past the last tile when rows has a tail, and the partition_id < cols / 64 loops silently drop remainder columns. Please add a host-side check or fallback before launch so unsupported shapes do not corrupt the interleaved buffer.

Also applies to: 41-56, 66-67, 79-88, 95-108

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@csrc/nv_internal/tensorrt_llm/kernels/cutlass_kernels/moe_gemm/moe_gemm_mixed_utils.cu`
around lines 27 - 28, The kernels in moe_gemm_mixed_utils.cu assume rows % 16 ==
0 and cols % 64 == 0, but the current launch loops (using block_id from
blockIdx.x and partition_id from threadIdx.y) can read past bounds (e.g.,
accesses like row_id + 8) and silently drop column remainders; add a host-side
validation before launching these kernels that checks the input dimensions (rows
and cols) and either (a) returns/throws an error for unsupported shapes or (b)
pads/rounds up the buffers to multiples of 16 (rows) and 64 (cols) and documents
that fallback behavior; ensure this check is performed wherever these kernels
are invoked so the loops governed by block_id/partition_id never encounter tails
that would corrupt the interleaved buffer.

int lane_id = threadIdx.x;
int row_id = block_id / 8 * 16 + block_id % 8;

int mma_id = lane_id / 8;
int dst_row_id = row_id + (mma_id % 2) * 8;

int interleaved_lane_id =
lane_id / 16 * 16 + (lane_id % 4) * 4 + (lane_id % 8) / 4 * 2;

int col_id = partition_id * 32 + lane_id;
int dst_col_id = partition_id * 32 + interleaved_lane_id;

int index_a = row_id * cols / 2 + col_id;
int index_b = (row_id + 8) * cols / 2 + col_id;

uint8_t fp4x2_a = fp4_weight[index_a];
uint8_t fp4x2_b = fp4_weight[index_b];

uint8_t fp4_temp_a = (fp4x2_a & 0xF0U) >> 4;
uint8_t fp4_temp_b = (fp4x2_b & 0x0FU) << 4;

fp4x2_a = (fp4x2_a & 0x0FU) | fp4_temp_b;
fp4x2_b = (fp4x2_b & 0xF0U) | fp4_temp_a;

int dst_id = dst_row_id * cols / 2 + dst_col_id;

fp4_weight_interleaved[dst_id] = fp4x2_a;
fp4_weight_interleaved[dst_id + 1] = fp4x2_b;
}
}
}

__global__ void interleave_int4_weights_for_Hopper_mixed_gemm_kernel(
uint8_t* int4_weight, uint8_t* int4_weight_interleaved, int const rows, int const cols) {
uint16_t* uint16_ptr = reinterpret_cast<uint16_t*>(int4_weight);
uint16_t* uint16_interleaved_ptr = reinterpret_cast<uint16_t*>(int4_weight_interleaved);

for (int block_id = blockIdx.x; block_id < rows / 2; block_id += gridDim.x) {
for (int partition_id = threadIdx.y; partition_id < cols / 64; partition_id += blockDim.y) {
int lane_id = threadIdx.x;

int row_id = block_id / 8 * 16 + block_id % 8;
int dst_row_id = row_id + (lane_id % 8) / 4 * 8;

int mma_id = lane_id / 8;
int interleaved_lane_id = mma_id * 8 + lane_id % 4 * 2;

int col_id = partition_id * 16 + lane_id;
int dst_col_id = partition_id * 16 + interleaved_lane_id;

int src_id_a = row_id * cols / 4 + col_id;
int src_id_b = (row_id + 8) * cols / 4 + col_id;

uint16_t int4x2_a = uint16_ptr[src_id_a];
uint16_t int4x2_b = uint16_ptr[src_id_b];

int dst_id = dst_row_id * cols / 4 + dst_col_id;

uint16_interleaved_ptr[dst_id] = int4x2_a;
uint16_interleaved_ptr[dst_id + 1] = int4x2_b;
}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

void interleave_fp4_weights_for_Hopper_mixed_gemm(
uint8_t* fp4_weight, uint8_t* fp4_weight_interleaved, int const rows, int const cols,
cudaStream_t stream) {
dim3 block(32, 32);
interleave_fp4_weights_for_Hopper_mixed_gemm_kernel<<<1024, block, 0, stream>>>(
fp4_weight, fp4_weight_interleaved, rows, cols);
}

void interleave_int4_weights_for_Hopper_mixed_gemm(
uint8_t* int4_weight, uint8_t* int4_weight_interleaved, int const rows, int const cols,
cudaStream_t stream) {
dim3 block(16, 32);
interleave_int4_weights_for_Hopper_mixed_gemm_kernel<<<1024, block, 0, stream>>>(
int4_weight, int4_weight_interleaved, rows, cols);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

} // namespace cutlass_kernels
} // namespace kernels
} // namespace tensorrt_llm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cstdint>
#include <cuda_runtime.h>

namespace tensorrt_llm {
namespace kernels {
namespace cutlass_kernels {

void interleave_fp4_weights_for_Hopper_mixed_gemm(
uint8_t* weight, uint8_t* weight_interleaved, int rows, int cols,
cudaStream_t stream = 0);

void interleave_int4_weights_for_Hopper_mixed_gemm(
uint8_t* weight, uint8_t* weight_interleaved, int rows, int cols,
cudaStream_t stream = 0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

} // namespace cutlass_kernels
} // namespace kernels
} // namespace tensorrt_llm
Loading
Loading