-
Notifications
You must be signed in to change notification settings - Fork 472
tuner: Trtllm-gen Fp4 MoE Autotunner #1475
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
yzh119
merged 17 commits into
flashinfer-ai:main
from
IwakuraRein:trtllm-gen-moe-autotuner
Aug 19, 2025
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
32433d1
add trtllm-gen fp4 moe autotuner
IwakuraRein 553b95e
minor
IwakuraRein 30059a4
minor
IwakuraRein e805e6f
upd
IwakuraRein 37316e9
minor
IwakuraRein bfd42ab
add bench
IwakuraRein 48b7786
address pre-commit
IwakuraRein 1f68223
use kwargs to make signature consistent
IwakuraRein 18e0f98
update gemm.py
IwakuraRein 1abfddc
update DtypeTrtllmGen
IwakuraRein 561777b
Merge remote-tracking branch 'myfork/main' into trtllm-gen-moe-autotuner
IwakuraRein 2c64abd
address coment
IwakuraRein d3fe6ab
revert accidental changes to ConstraintSpec
IwakuraRein 81f1b88
disable token tile dim in autotuner
IwakuraRein 957c95d
upd
IwakuraRein 72ba343
Merge remote-tracking branch 'myfork/main' into trtllm-gen-moe-autotuner
IwakuraRein 7e8e234
upd
IwakuraRein 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -881,11 +881,10 @@ std::vector<at::Tensor> trtllm_fp4_block_scale_moe_launcher( | |
TORCH_CHECK(hidden_states_scale.value().scalar_type() == at::ScalarType::Float8_e4m3fn, | ||
"hidden_states_scale must be fp8."); | ||
|
||
TORCH_CHECK(hidden_states_scale.value().dim() == 1, "hidden_states_scale must be 1D."); | ||
TORCH_CHECK(hidden_states_scale.value().sizes()[0] == | ||
tensorrt_llm::computeFP4LinearLayoutSFSize(args.num_tokens, | ||
args.hidden_size / sf_vec_size), | ||
"hidden_states_scale has incorrect size"); | ||
TORCH_CHECK( | ||
hidden_states_scale.value().numel() == tensorrt_llm::computeFP4LinearLayoutSFSize( | ||
args.num_tokens, args.hidden_size / sf_vec_size), | ||
"hidden_states_scale has incorrect size"); | ||
} | ||
|
||
TORCH_CHECK(gemm1_weights.scalar_type() == torch_ext::FLOAT4_E2M1X2, | ||
|
@@ -1059,7 +1058,8 @@ std::vector<at::Tensor> trtllm_fp4_block_scale_moe( | |
std::optional<int64_t> n_group, std::optional<int64_t> topk_group, int64_t intermediate_size, | ||
int64_t local_expert_offset, int64_t local_num_experts, | ||
std::optional<double> routed_scaling_factor, int64_t tile_tokens_dim, | ||
int64_t routing_method_type, bool do_finalize, bool enable_pdl, at::Tensor& output) { | ||
int64_t routing_method_type, bool do_finalize, bool enable_pdl, at::Tensor& output, | ||
int64_t config_index) { | ||
using RunnerType = tensorrt_llm::kernels::trtllmgen_moe::MoE::Runner; | ||
|
||
int const num_tokens = hidden_states.sizes()[0]; | ||
|
@@ -1112,8 +1112,10 @@ std::vector<at::Tensor> trtllm_fp4_block_scale_moe( | |
mDtypeAct, mDtypeWeights, mUseDeepSeekFp8, (int32_t)tile_tokens_dim, | ||
tensorrt_llm::kernels::ActType::SwiGlu, /*useShuffledMatrixA*/ true); | ||
|
||
auto const moeConfigIndex = mRunner->getDefaultValidConfigIndex( | ||
top_k, hidden_size, intermediate_size, local_num_experts, num_tokens); | ||
if (config_index == -1) { | ||
config_index = mRunner->getDefaultValidConfigIndex(top_k, hidden_size, intermediate_size, | ||
local_num_experts, num_tokens); | ||
} | ||
|
||
return trtllm_fp4_block_scale_moe_launcher( | ||
routing_logits, topk_ids, expert_weights, routing_bias, hidden_states, hidden_states_scale, | ||
|
@@ -1122,7 +1124,84 @@ std::vector<at::Tensor> trtllm_fp4_block_scale_moe( | |
output1_scales_gate_scalar, output2_scales_scalar, num_experts, top_k, n_group, topk_group, | ||
intermediate_size, local_expert_offset, local_num_experts, routed_scaling_factor, | ||
tile_tokens_dim, routing_method_type, do_finalize, *mRunner, mDtypeAct, mDtypeWeights, | ||
moeConfigIndex, enable_pdl, output); | ||
config_index, enable_pdl, output); | ||
} | ||
|
||
inline btg::Dtype get_dtype(int64_t const dtype) { | ||
switch (dtype) { | ||
case 0: | ||
return btg::Dtype::Bfloat16; | ||
case 1: | ||
return btg::Dtype::Bool; | ||
case 2: | ||
return btg::Dtype::E2m1; | ||
case 3: | ||
return btg::Dtype::E2m3; | ||
case 4: | ||
return btg::Dtype::E3m2; | ||
case 5: | ||
return btg::Dtype::E4m3; | ||
case 6: | ||
return btg::Dtype::E5m2; | ||
case 7: | ||
return btg::Dtype::Fp16; | ||
case 8: | ||
return btg::Dtype::Fp32; | ||
case 9: | ||
return btg::Dtype::Int8; | ||
case 10: | ||
return btg::Dtype::Int32; | ||
case 11: | ||
return btg::Dtype::Int64; | ||
case 12: | ||
return btg::Dtype::MxE2m1; | ||
case 13: | ||
return btg::Dtype::MxE4m3; | ||
case 14: | ||
return btg::Dtype::UE8m0; | ||
case 15: | ||
return btg::Dtype::UInt8; | ||
case 16: | ||
return btg::Dtype::UInt16; | ||
case 17: | ||
return btg::Dtype::UInt32; | ||
case 18: | ||
return btg::Dtype::UInt64; | ||
case 19: | ||
return btg::Dtype::UInt128; | ||
case 20: | ||
return btg::Dtype::Void; | ||
default: | ||
TORCH_CHECK(false, "Invalid trtllm-gen dtype"); | ||
} | ||
return btg::Dtype::E2m1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
int64_t trtllm_get_default_moe_configs(int64_t const tile_tokens_dim, int64_t const dtype_act_, | ||
int64_t const dtype_weights_, bool const useDeepSeekFp8, | ||
int64_t const top_k, int64_t const hidden_size, | ||
int64_t const intermediate_size, | ||
int64_t const num_local_experts, int64_t const num_tokens) { | ||
auto dtype_act = get_dtype(dtype_act_); | ||
auto dtype_weights = get_dtype(dtype_weights_); | ||
tensorrt_llm::kernels::trtllmgen_moe::MoE::Runner moe_runner( | ||
dtype_act, dtype_weights, useDeepSeekFp8, (int32_t)tile_tokens_dim, | ||
tensorrt_llm::kernels::ActType::SwiGlu, /*useShuffledMatrixA*/ true); | ||
return moe_runner.getDefaultValidConfigIndex(top_k, hidden_size, intermediate_size, | ||
num_local_experts, num_tokens); | ||
} | ||
|
||
std::vector<int64_t> trtllm_get_valid_moe_configs( | ||
int64_t const tile_tokens_dim, int64_t const dtype_act_, int64_t const dtype_weights_, | ||
bool const useDeepSeekFp8, int64_t const top_k, int64_t const hidden_size, | ||
int64_t const intermediate_size, int64_t const num_local_experts, int64_t const num_tokens) { | ||
auto dtype_act = get_dtype(dtype_act_); | ||
auto dtype_weights = get_dtype(dtype_weights_); | ||
tensorrt_llm::kernels::trtllmgen_moe::MoE::Runner moe_runner( | ||
dtype_act, dtype_weights, useDeepSeekFp8, (int32_t)tile_tokens_dim, | ||
tensorrt_llm::kernels::ActType::SwiGlu, /*useShuffledMatrixA*/ true); | ||
return moe_runner.getValidConfigIndices(top_k, hidden_size, intermediate_size, num_local_experts, | ||
num_tokens); | ||
} | ||
|
||
namespace trtllm_cubin_loader { | ||
|
@@ -1133,6 +1212,8 @@ TORCH_LIBRARY_FRAGMENT(TORCH_EXTENSION_NAME, m) { | |
m.def("trtllm_fp8_per_tensor_scale_moe", trtllm_fp8_per_tensor_scale_moe); | ||
m.def("trtllm_fp8_block_scale_moe", trtllm_fp8_block_scale_moe); | ||
m.def("trtllm_fp4_block_scale_moe", trtllm_fp4_block_scale_moe); | ||
m.def("trtllm_get_default_moe_configs", trtllm_get_default_moe_configs); | ||
m.def("trtllm_get_valid_moe_configs", trtllm_get_valid_moe_configs); | ||
} | ||
|
||
} // namespace flashinfer |
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
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.
this currenly finds the type by Dtype's Uid bits.
would it be more maintainable if we simply pass strings (so in code review, the case will more obviously match the type name) and document at the binding interface that the available options are in DtypeDecl.h Dtype
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.
the current approach has the advantage of being checked early using class DtypeTrtllmGen(IntEnum). so no objections, just raising options
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.
@aleozlx Thanks for the suggestion! What about defining a new enum in
trtllm_fused_moe_kernel_launcher.cu
and use macros to map it tobtg::Dtype
. Then expose to python.Uh oh!
There was an error while loading. Please reload this page.
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.
sure . that improves readability. or we can use
class Dtype(IntEnum)
i proposed in the other thread and remove another conversion. and hopefully__new__
serves to hide impl details in a way that user won't rely on the value (compared to a separate function). although the value changes can cause breakage thru the cpp interface, so we just have to align on something early.