-
Notifications
You must be signed in to change notification settings - Fork 1.1k
perf: optimize MXFP4xBF16 & INT4xFP8 CUTLASS MoE backend for SM90 #3084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samuellees
merged 33 commits into
flashinfer-ai:main
from
samuellees:feat/w4a16-moe-kernel
Apr 23, 2026
Merged
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 ac0e82e
chore: address review feedback on W4A16 MoE PR
samuellees 931b87a
style: apply clang-format
samuellees 9c4d204
feat: migrate weight interleaving and scheduler config from TRTLLM PR…
samuellees 31f80e2
feat: migrate CUTLASS mixed-input kernel optimizations from TRTLLM PR…
samuellees 1687648
test: add W4A16 MoE benchmark script
samuellees e43b3e6
fix: pass device arg to is_sm90a_supported in test
samuellees be83fd0
revert: restore original CUTLASS extension files, add reference corre…
samuellees f1ae825
fix: use small configs for strict correctness, large configs for sani…
samuellees 50de6f8
fix: limit correctness configs to h=128 matching upstream test scale
samuellees 9c251a4
fix: use percent-based accuracy check matching TRTLLM methodology
samuellees 7446660
fix: revert test scope to h=128 correctness (matches upstream W4A16 c…
samuellees f57bf38
style: apply pre-commit hooks (clang-format, ruff)
samuellees 1461814
feat: port TRTLLM PR #12451 mixed-input CUTLASS extensions
samuellees 4ed56ff
feat: wire up W4A16 weight + scale interleave for SM90 mixed-input MoE
samuellees c4002f1
test: extend W4A16 MoE correctness to K=768/2048/4096 with interleave…
samuellees 31ff00e
test: use 99.9% percent-based check for W4A16 coverage sweep
samuellees e355831
test: speed up W4A16 reference, trim coverage configs, preprocess ben…
samuellees 2fac475
test: apply W4A16 interleave preprocessing in upstream MoE test
samuellees 4223e45
style: apply pre-commit hooks (clang-format, ruff format)
samuellees eabea6d
feat: align W4A16 heuristic with TRTLLM PR #12451
samuellees 0e4ba76
fix(test): fix test_moe_w4a8 weight interleaving, act-scale dtype and…
StudyingShao dcafd6a
remove redundant W4A16 MoE test and benchmark covered by test_trtllm_…
StudyingShao efd9aff
test: add dedicated W4A16 / W4A8 MoE test files, keep upstream test u…
samuellees 21b5d44
test: trim W4A8 test to supported envelope (h==inter==512, e==2)
samuellees d3a6190
test: inline SM90 mixed-input W4A16/W4A8 tests into upstream file
samuellees fc3bb6d
test: add batch_size=1 to W4A8 Hopper correctness
samuellees 77746b8
test: drop m=16 from Hopper mixed-input MoE tests
samuellees cb90611
refactor: rename interleave_moe_*_for_{Hopper,hopper}_mixed_gemm → sm90
samuellees 335345b
style: apply pre-commit auto-fixes + document sm90 interleave helpers
samuellees 17a9c54
Merge branch 'main' into feat/w4a16-moe-kernel
samuellees 7c2ab34
fix(test): apply sm90 weight/scale interleave in upstream MoE tests
samuellees 1a5b242
Merge branch 'main' into feat/w4a16-moe-kernel
samuellees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
csrc/nv_internal/tensorrt_llm/kernels/cutlass_kernels/moe_gemm/moe_gemm_mixed_utils.cu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| } // namespace cutlass_kernels | ||
| } // namespace kernels | ||
| } // namespace tensorrt_llm | ||
36 changes: 36 additions & 0 deletions
36
csrc/nv_internal/tensorrt_llm/kernels/cutlass_kernels/moe_gemm/moe_gemm_mixed_utils.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| } // namespace cutlass_kernels | ||
| } // namespace kernels | ||
| } // namespace tensorrt_llm | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fail fast on unsupported matrix shapes.
These kernels only work when
rowsis a multiple of 16 andcolsis a multiple of 64. On Line 42 and Line 80,row_id + 8can read past the last tile whenrowshas a tail, and thepartition_id < cols / 64loops 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