Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 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 @@ -29,6 +29,7 @@
#include "moe_gemm_kernels.h"
#include "tensorrt_llm/common/workspace.h"
#include "tensorrt_llm/kernels/cutlass_kernels/fp8_blockscale_gemm/fp8_blockscale_gemm.h"
#include "tensorrt_llm/kernels/cutlass_kernels/moe_gemm/moe_gemm_mixed_utils.h"

namespace common = tensorrt_llm::common;
namespace kernels = CUTLASS_MOE_GEMM_KERNELS_NAMESPACE;
Expand Down Expand Up @@ -1274,4 +1275,48 @@ tvm::ffi::Module init(DLDataType activation_dtype, DLDataType weight_dtype, DLDa
return tvm::ffi::Module(ptr);
}

// Interleave a 4-bit packed weight tensor into the layout required by the
// SM90 mixed-input Hopper MoE GEMM. Expected input shape (num_experts, n,
Comment thread
samuellees marked this conversation as resolved.
Outdated
// k / 2) uint8 on CUDA. Writes into an output tensor of the same shape.
// quant_type: 0 for INT4 (W4A8), 1 for FP4 (W4A16 / MXFP4).
void interleave_moe_weights_for_Hopper_mixed_gemm(TensorView weight, TensorView weight_interleaved,
Comment thread
samuellees marked this conversation as resolved.
Outdated
int64_t quant_type) {
CHECK_INPUT_TYPE(weight, dl_uint8);
CHECK_INPUT_TYPE(weight_interleaved, dl_uint8);
CHECK_CONTIGUOUS(weight);
CHECK_CONTIGUOUS(weight_interleaved);
CHECK_DIM(3, weight);
CHECK_DIM(3, weight_interleaved);
TVM_FFI_ICHECK_EQ(weight.size(0), weight_interleaved.size(0))
<< "weight and weight_interleaved must share num_experts dim";
TVM_FFI_ICHECK_EQ(weight.size(1), weight_interleaved.size(1))
<< "weight and weight_interleaved must share n dim";
TVM_FFI_ICHECK_EQ(weight.size(2), weight_interleaved.size(2))
<< "weight and weight_interleaved must share packed-k dim";
TVM_FFI_ICHECK(quant_type == 0 || quant_type == 1)
<< "quant_type must be 0 (INT4) or 1 (FP4), got " << quant_type;

int64_t const num_experts = weight.size(0);
int64_t const n = weight.size(1);
int64_t const k = weight.size(2) * 2;
int64_t const per_expert_bytes = n * (k / 2);

auto stream = get_stream(weight.device());
auto* src = static_cast<uint8_t*>(weight.data_ptr());
auto* dst = static_cast<uint8_t*>(weight_interleaved.data_ptr());
for (int64_t e = 0; e < num_experts; ++e) {
uint8_t* src_e = src + e * per_expert_bytes;
uint8_t* dst_e = dst + e * per_expert_bytes;
if (quant_type == 1) {
tensorrt_llm::kernels::cutlass_kernels::interleave_fp4_weights_for_Hopper_mixed_gemm(
Comment thread
samuellees marked this conversation as resolved.
Outdated
src_e, dst_e, static_cast<int>(n), static_cast<int>(k), stream);
} else {
tensorrt_llm::kernels::cutlass_kernels::interleave_int4_weights_for_Hopper_mixed_gemm(
src_e, dst_e, static_cast<int>(n), static_cast<int>(k), stream);
}
}
}

TVM_FFI_DLL_EXPORT_TYPED_FUNC(init, init);
TVM_FFI_DLL_EXPORT_TYPED_FUNC(interleave_moe_weights_for_Hopper_mixed_gemm,
interleave_moe_weights_for_Hopper_mixed_gemm);

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,16 @@ std::vector<CutlassGemmConfig> get_candidate_configs_sm90(
(config & CutlassGemmConfig::WEIGHT_ONLY) && (config & CutlassGemmConfig::GROUPED_GEMM);
if (has_w4afp8) {
bool const has_coop_supported = sm90_supports_coop(tile_config);
std::set<MainloopScheduleType> mainloop_schedules{MainloopScheduleType::PINGPONG};
std::set<MainloopScheduleType> mainloop_schedules;
if (has_coop_supported) {
// Due to the limitation on the number of registers on SM,
// cooperative scheduler does not support CtaShape128x128x128B
// for mixed-dtype (W4A16) grouped GEMM. Skip the tile entirely
// to avoid register overflow.
if (tile_config == CutlassTileConfigSM90::CtaShape128x128x128B) continue;
mainloop_schedules.insert(MainloopScheduleType::COOPERATIVE);
} else {
mainloop_schedules.insert(MainloopScheduleType::PINGPONG);
}
auto const epilogue_schedule = EpilogueScheduleType::AUTO;
for (auto const& mainloop_schedule : mainloop_schedules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cutlass/gemm/device/gemm_universal_adapter.h"
#include "cutlass/gemm/dispatch_policy.hpp"
#include "cutlass/gemm/kernel/gemm_universal.hpp"
#include "cutlass/gemm/kernel/tile_scheduler_params.h"
#include "cutlass/util/command_line.h"
#include "cutlass/util/distribution.h"
#include "cutlass/util/host_tensor.h"
Expand Down Expand Up @@ -202,6 +203,12 @@ 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,114 @@
/*
* 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,
Comment thread
samuellees marked this conversation as resolved.
Outdated
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,35 @@
/*
* 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 <cuda_runtime.h>

#include <cstdint>

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);

} // namespace cutlass_kernels
} // namespace kernels
} // namespace tensorrt_llm
4 changes: 4 additions & 0 deletions flashinfer/fused_moe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
WeightLayout,
convert_to_block_layout,
cutlass_fused_moe,
interleave_moe_scales_for_hopper_mixed_gemm,
interleave_moe_weights_for_hopper_mixed_gemm,
gen_cutlass_fused_moe_sm120_module,
gen_cutlass_fused_moe_sm103_module,
gen_cutlass_fused_moe_sm100_module,
Expand Down Expand Up @@ -59,6 +61,8 @@
"WeightLayout",
"convert_to_block_layout",
"cutlass_fused_moe",
"interleave_moe_scales_for_hopper_mixed_gemm",
"interleave_moe_weights_for_hopper_mixed_gemm",
"gen_cutlass_fused_moe_sm120_module",
"gen_cutlass_fused_moe_sm103_module",
"gen_cutlass_fused_moe_sm100_module",
Expand Down
113 changes: 113 additions & 0 deletions flashinfer/fused_moe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,122 @@ def _fake_cutlass_fused_moe(
# Register the module
return SimpleNamespace(
cutlass_fused_moe=cutlass_fused_moe,
interleave_moe_weights_for_Hopper_mixed_gemm=(
module.interleave_moe_weights_for_Hopper_mixed_gemm
),
)


@flashinfer_api
def interleave_moe_scales_for_hopper_mixed_gemm(
scales: torch.Tensor,
group_size: int = 32,
) -> torch.Tensor:
"""Interleave MXFP4 block scales for the SM90 mixed-input Hopper GEMM.

The kernel expects scales in layout
``(num_experts, K // (group_size * 4), rows * 4)`` rather than the natural
``(num_experts, rows, K // group_size)`` produced by the MXFP4 quantizer.
This helper performs the reshape + permute equivalent to TensorRT-LLM's
``WFP4A16FusedMoEMethod.load_quant_scales`` (PR #12451), with the fixed
interleave factor of ``128 // group_size`` used for MXFP4.

Parameters
----------
scales:
``[num_experts, rows, K // group_size]`` uint8 tensor of E8M0 block
scales.
group_size:
MXFP4 quantization group size (default 32).

Returns
-------
torch.Tensor
Contiguous uint8 tensor with shape
``[num_experts, K // (group_size * factor), rows * factor]``
where ``factor = 128 // group_size``.
"""
if scales.dim() != 3:
raise ValueError(
f"scales must be 3D (num_experts, rows, K/group_size); got {tuple(scales.shape)}"
)
if scales.dtype != torch.uint8:
raise ValueError(f"scales must be uint8 (E8M0); got {scales.dtype}")

factor = 128 // group_size
if factor < 1 or 128 % group_size != 0:
raise ValueError(
f"group_size={group_size} must divide 128 (interleave factor = 128 // group_size)"
)
e, rows, kgs = scales.shape
if kgs % factor != 0:
raise ValueError(
f"K/group_size={kgs} must be divisible by interleave factor {factor}"
)
tmp = (
scales.reshape(e, rows, kgs // factor, factor).permute(0, 2, 1, 3).contiguous()
)
return tmp.reshape(e, kgs // factor, rows * factor)


@flashinfer_api
def interleave_moe_weights_for_hopper_mixed_gemm(
weight: torch.Tensor,
quant_type: str = "fp4",
) -> torch.Tensor:
"""Interleave 4-bit packed MoE weights for the SM90 mixed-input GEMM.

The Hopper mixed-dtype MoE GEMM (used by ``cutlass_fused_moe`` with
``use_w4_group_scaling=True``) expects weights in a specific interleaved
layout; without preprocessing, the LUT-based FP4→BF16 conversion reads
bytes from the wrong positions and the output diverges from a dequantized
reference for any K > 128. TensorRT-LLM's W4A16 MoE runs the equivalent
preprocessing at weight-load time (see ``trtllm::
interleave_4bit_weights_for_Hopper_mixed_gemm`` in PR #12451).

Parameters
----------
weight:
``[num_experts, n, k // 2]`` uint8 CUDA tensor (4-bit values packed
two-per-byte).
quant_type:
``"fp4"`` for MXFP4 (the W4A16 path) or ``"int4"`` for INT4 (the
W4A8 path).

Returns
-------
torch.Tensor
A new uint8 tensor with the same shape as ``weight`` holding the
interleaved layout. Feed this directly as ``fc1_expert_weights`` /
``fc2_expert_weights`` to :func:`cutlass_fused_moe`.
"""
if weight.dim() != 3:
raise ValueError(
f"weight must be 3D (num_experts, n, k/2); got shape {tuple(weight.shape)}"
)
if weight.dtype != torch.uint8:
raise ValueError(f"weight must be uint8 (packed 4-bit); got {weight.dtype}")
if not weight.is_cuda:
raise ValueError("weight must live on CUDA")

qtype_map = {"fp4": 1, "int4": 0}
if quant_type not in qtype_map:
raise ValueError(
f"quant_type must be one of {list(qtype_map)}; got {quant_type!r}"
)

weight = weight.contiguous()
out = torch.empty_like(weight)

major, minor = get_compute_capability(weight.device)
device_arch = f"{major * 10 + minor}"
module = get_cutlass_fused_moe_module(device_arch)
module.interleave_moe_weights_for_Hopper_mixed_gemm(
weight, out, qtype_map[quant_type]
)
return out


# ref: https://github.com/NVIDIA/TensorRT-LLM/blob/main/tensorrt_llm/_torch/custom_ops/torch_custom_ops.py#L121
@flashinfer_api
def cutlass_fused_moe(
Expand Down
2 changes: 2 additions & 0 deletions flashinfer/jit/fused_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def gen_cutlass_fused_moe_module(
jit_env.FLASHINFER_CSRC_DIR
/ "nv_internal/tensorrt_llm/kernels/cutlass_kernels/fp8_blockscale_gemm/fp8_blockscale_gemm.cu",
jit_env.FLASHINFER_CSRC_DIR
/ "nv_internal/tensorrt_llm/kernels/cutlass_kernels/moe_gemm/moe_gemm_mixed_utils.cu",
jit_env.FLASHINFER_CSRC_DIR
/ "fused_moe/cutlass_backend/flashinfer_cutlass_fused_moe_binding.cu",
jit_env.FLASHINFER_CSRC_DIR
/ "fused_moe/cutlass_backend/deepgemm_jit_setup.cu",
Expand Down
Loading
Loading