Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions csrc/trtllm_batched_gemm_runner.cu
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,20 @@ TrtllmGenBatchedGemmRunner::TrtllmGenBatchedGemmRunner(
if (options.mFusedBiasShuffleMode != mOptions.fusedBiasShuffleMode) continue;
if (options.mBiasDtype != mOptions.biasDtype) continue;
}
bool const usesPerTokenScaling =
options.mTransposeMmaOutput ? options.mUsePerTokenSfB : options.mUsePerTokenSfA;
if (mOptions.usePerTokenScaling) {
if (options.mTransposeMmaOutput && !options.mUsePerTokenSfB) continue;
if (!options.mTransposeMmaOutput && !options.mUsePerTokenSfA) continue;
if (!usesPerTokenScaling) continue;
if (options.mPerTokenSfDtype != mOptions.perTokenSfDtype) continue;
}
// The MoE pipeline allocates and consumes output scaling factors in the
// block format's default dtype. Reject cubins that override that
// contract, such as bmm_E2m1xFp32_* kernels that emit linear FP32
// scaling factors into a buffer sized for E4M3 factors.
if (tg::dtypeIsBlockFmt(options.mDtypeC)) {
if (options.mDtypeSfC != tg::dtypeGetBlockSfType(options.mDtypeC)) continue;
} else if (options.mDtypeSfC != Dtype::Void) {
continue;
}
if (mOptions.usePerChannelScaling) {
if (options.mTransposeMmaOutput && !options.mUsePerTokenSfA) continue;
Expand Down Expand Up @@ -164,6 +175,7 @@ TrtllmGenBatchedGemmRunner::TrtllmGenBatchedGemmRunner(
<< ", mFusedBiasShuffleMode: " << (int64_t)mOptions.fusedBiasShuffleMode
<< ", mBiasDtype: " << tg::dtypeToString(mOptions.biasDtype)
<< ", mUsePerTokenScaling: " << mOptions.usePerTokenScaling
<< ", mPerTokenSfDtype: " << tg::dtypeToString(mOptions.perTokenSfDtype)
<< ", mUsePerChannelScaling: " << mOptions.usePerChannelScaling;
FLASHINFER_CHECK(!mPassingConfigIndices.empty(), error_msg.str());
}
Expand Down
35 changes: 20 additions & 15 deletions csrc/trtllm_fused_moe_kernel_launcher.cu
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,7 @@ class FusedMoeLauncher {

void prepare_moe_common(int64_t& moe_tactic) {
using RunnerType = tensorrt_llm::kernels::trtllmgen_moe::MoE::Runner;
// FIXME(siyuan): check llama4 routing after the fp4 FC1 kernels with bf16 scale factors were
// generated
bool usePerTokenScalingGemm1 =
per_token_scales.has_value() /* ||
static_cast<RoutingMethodType>(this->routing_method_type) == RoutingMethodType::Llama4*/
;
bool usePerTokenScalingGemm1 = per_token_scales.has_value() || args->mUseRoutingScalesOnInput;
// FIXME(siyuan): currently only nvfp4 x nvfp4 uses per-token scaling in both FC1 and FC2
bool usePerTokenScalingGemm2 = per_token_scales.has_value() && mDtypeAct == btg::Dtype::E2m1;
// For FP8 block-scale (E4m3 activations, E4m3 weights) with DeepSeek FP8 and no
Expand Down Expand Up @@ -947,6 +942,7 @@ class Fp8PerTensorLauncher : public FusedMoeLauncher {
int64_t weight_layout, bool use_routing_scales_on_input_param,
ActivationType activation_type, bool norm_topk_prob = true) {
this->use_routing_scales_on_input = use_routing_scales_on_input_param;
args->mUseRoutingScalesOnInput = use_routing_scales_on_input_param;

auto dtype = hidden_states.dtype();
if (dtype == dl_float16) {
Expand Down Expand Up @@ -1175,7 +1171,8 @@ class Fp8PerTensorLauncher : public FusedMoeLauncher {
int64_t intermediate_size, int64_t num_local_experts,
int64_t num_tokens, int64_t act_type,
bool use_shuffled_weight, int64_t weight_layout,
btg::Dtype dtype_act, btg::Dtype dtype_weights) {
btg::Dtype dtype_act, btg::Dtype dtype_weights,
bool use_routing_scales_on_input) {
Array<Array<int64_t>> valid_configs;

std::vector<int32_t> supported_tile_nums(mSupportedTileNums.begin(), mSupportedTileNums.end());
Expand All @@ -1190,8 +1187,8 @@ class Fp8PerTensorLauncher : public FusedMoeLauncher {
static_cast<batchedGemm::gemm::MatrixLayout>(weight_layout),
// FP8 per-tensor doesn't use Mn-bias (LoRA) cubins.
/*gemm1BiasType*/ batchedGemm::gemm::BiasType::None,
true, // usePerTokenScalingGemm1. always true for per-tensor fp8 due to llama4 routing
false, false, false);
/*usePerTokenScalingGemm1*/ use_routing_scales_on_input,
/*usePerTokenScalingGemm2*/ false, false, false);

auto cfgs = moe_runner->getValidConfigIndices(top_k, hidden_size, intermediate_size,
num_local_experts, num_tokens);
Expand Down Expand Up @@ -1866,10 +1863,15 @@ class FP4BlockScaleLauncher : public FusedMoeLauncher {
public:
static constexpr std::array<int32_t, 4> mBaseSupportedTileNums = {8, 16, 32, 64};

static std::vector<int32_t> getSupportedTileNums(btg::Dtype dtype_act) {
static std::vector<int32_t> getSupportedTileNums(btg::Dtype dtype_act, btg::Dtype dtype_weights) {
std::vector<int32_t> tiles(mBaseSupportedTileNums.begin(), mBaseSupportedTileNums.end());
if (dtype_act != btg::Dtype::Bfloat16) {
tiles.push_back(128);
// Keep tactic enumeration aligned with the public BMM artifact.
if ((dtype_weights == btg::Dtype::E2m1 && dtype_act == btg::Dtype::E2m1) ||
(dtype_weights == btg::Dtype::MxE2m1 && dtype_act == btg::Dtype::MxE4m3)) {
tiles.push_back(192);
}
tiles.push_back(256);
}
return tiles;
Expand Down Expand Up @@ -2207,7 +2209,7 @@ class FP4BlockScaleLauncher : public FusedMoeLauncher {
bool use_per_token_scaling) {
Array<Array<int64_t>> valid_configs;

std::vector<int32_t> tile_sizes = getSupportedTileNums(dtype_act);
std::vector<int32_t> tile_sizes = getSupportedTileNums(dtype_act, dtype_weights);
std::set<int32_t> selected_tile_nums =
computeSelectedTileN(tile_sizes, num_tokens, top_k, num_local_experts);

Expand All @@ -2220,9 +2222,11 @@ class FP4BlockScaleLauncher : public FusedMoeLauncher {
/*weight_layout*/ batchedGemm::gemm::MatrixLayout::MajorK,
// FP4 MoE getValidConfigs doesn't exercise the Mn-bias (LoRA) cubins.
/*gemm1BiasType*/ batchedGemm::gemm::BiasType::None,
// NOTE(siyuan): currently FP4 MoE always apply per-token scaling to both FC1 and FC2.
/*usePerTokenScalingGemm1*/ use_per_token_scaling,
/*usePerTokenScalingGemm2*/ use_per_token_scaling, false, false);
// Match prepare_moe_common(): only NVFP4 uses the explicit
// per-token scale operand for FC2.
/*usePerTokenScalingGemm2*/
use_per_token_scaling && dtype_act == btg::Dtype::E2m1, false, false);

auto cfgs = moe_runner->getValidConfigIndices(top_k, hidden_size, intermediate_size,
num_local_experts, num_tokens);
Expand Down Expand Up @@ -2761,7 +2765,8 @@ Array<Tensor> trtllm_fp4_block_scale_moe(
}

// Determine supported tile sizes
std::vector<int32_t> mSupportedTileN = FP4BlockScaleLauncher::getSupportedTileNums(mDtypeAct);
std::vector<int32_t> mSupportedTileN =
FP4BlockScaleLauncher::getSupportedTileNums(mDtypeAct, mDtypeWeights);
// Build launchers for ALL supported tiles so autotuner-cached tactics always find their tile_N.

// Create a map of launchers for each tile size
Expand Down Expand Up @@ -2973,7 +2978,7 @@ Array<Array<int64_t>> trtllm_get_valid_moe_configs(
}
return Fp8PerTensorLauncher::getValidConfigs(
top_k, hidden_size, intermediate_size, num_local_experts, num_tokens, act_type,
use_shuffled_weight, weight_layout, dtype_act, dtype_weights);
use_shuffled_weight, weight_layout, dtype_act, dtype_weights, use_per_token_scaling);
} else if (dtype_weights == btg::Dtype::E2m1 || dtype_weights == btg::Dtype::MxE2m1) {
if (has_gemm1_lora_delta) {
TVM_FFI_LOG_AND_THROW(NotImplementedError)
Expand Down
11 changes: 11 additions & 0 deletions csrc/trtllm_fused_moe_runner.cu
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ static inline ActType activationTypeToGatedActType(ActivationType actType) {
return ActType::SwiGlu;
case ActivationType::Geglu:
return ActType::GeGlu;
case ActivationType::Situ:
return ActType::SiTuGlu;
default:
FLASHINFER_CHECK(false, "Unsupported gated activation type ",
serializeActivationType(actType), " of enum ",
Expand Down Expand Up @@ -425,6 +427,9 @@ tensorrt_llm::kernels::TrtllmGenBatchedGemmRunnerOptions getOptions(
.fusedBiasShuffleMode = fusedBiasShuffleMode,
.biasDtype = biasDtype,
.usePerTokenScaling = usePerTokenScaling,
.perTokenSfDtype = usePerTokenScaling ? (dtypeAct == btg::Dtype::E4m3 ? btg::Dtype::Bfloat16
: btg::Dtype::Fp32)
: btg::Dtype::Void,
.usePerChannelScaling = usePerChannelScaling,
};
return options;
Expand All @@ -449,6 +454,9 @@ tensorrt_llm::kernels::TrtllmGenBatchedGemmRunnerOptions getOptions(
.fusedBiasShuffleMode = fusedBiasShuffleMode,
.biasDtype = biasDtype,
.usePerTokenScaling = usePerTokenScaling,
.perTokenSfDtype = usePerTokenScaling ? (dtypeAct == btg::Dtype::E4m3 ? btg::Dtype::Bfloat16
: btg::Dtype::Fp32)
: btg::Dtype::Void,
.usePerChannelScaling = usePerChannelScaling};
return options;
}
Expand Down Expand Up @@ -560,6 +568,9 @@ tensorrt_llm::kernels::TrtllmGenBatchedGemmRunnerOptions getOptions(
.useShuffledMatrix = useShuffledMatrix,
.weightLayout = weightLayout,
.usePerTokenScaling = usePerTokenScaling,
.perTokenSfDtype = usePerTokenScaling ? (dtypeAct == btg::Dtype::E4m3 ? btg::Dtype::Bfloat16
: btg::Dtype::Fp32)
: btg::Dtype::Void,
.usePerChannelScaling = usePerChannelScaling};
return options;
}
Expand Down
4 changes: 2 additions & 2 deletions flashinfer/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ArtifactPath:

TRTLLM_GEN_FMHA: str = "158f6fa11ef139a098cfddcdddce73ca99d164ad/fmha/trtllm-gen/"
TRTLLM_GEN_BMM: str = (
"b368d003e8fdfe4b271bff7c788ac52ef789a81b/batched_gemm-da58956-b4ac80e/"
"5988e15c0e6d006c6a64c0f6c6748b4d3150c1af/batched_gemm-3d40263-3e19f0a/"
)
TRTLLM_GEN_GEMM: str = (
"10f64528a1172dae8e29601a3b99ab9dc78d37be/gemm-91e0ba0-2710384/"
Expand All @@ -160,7 +160,7 @@ class CheckSumHash:
"c2d9399b2537be785882354a4f9902ed6c03136c0ea341e201eac40c3923e1dc"
)
TRTLLM_GEN_BMM: str = (
"d0178cd486be54e622386e88daba9c2aca654be7e6f3dcd1af7ecca3354492d2"
"b19ed6c8b1d3fc13ced823bd65ee764d35a19080aea97e742c82ee73ce4c19b0"
)
DEEPGEMM: str = "1a2a166839042dbd2a57f48051c82cd1ad032815927c753db269a4ed10d0ffbf"
TRTLLM_GEN_GEMM: str = (
Expand Down
17 changes: 17 additions & 0 deletions flashinfer/fused_moe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,7 @@ def trtllm_fp8_per_tensor_scale_moe_op(
weight_layout=WeightLayout.MajorK,
use_shuffled_weight=True,
activation_type=activation_type,
use_per_token_scaling=use_routing_scales_on_input,
num_experts=num_experts,
)

Expand Down Expand Up @@ -4428,10 +4429,17 @@ def trtllm_fp4_block_scale_moe(
``[num_experts, 2 * intermediate_size]`` FC1 bias, ``float32``.
gemm1_alpha : Optional[torch.Tensor]
``[num_experts]`` swiglu alpha, ``float32``.
For SiTU this is ``[local_num_experts]``, finite and positive;
``None`` materializes per-expert ``alpha=1``.

gemm1_beta : Optional[torch.Tensor]
``[num_experts]`` swiglu beta, ``float32``.
For SiTU this is ``[local_num_experts]``, finite and positive;
``None`` materializes per-expert ``beta=1``.
gemm1_clamp_limit : Optional[torch.Tensor]
``[num_experts]`` swiglu clamp limit, ``float32``.
For SiTU a provided limit is per-local-expert, finite, and positive;
it clamps ``x0`` to ``[-limit, limit]`` and ``x1`` from above.
gemm2_weights : torch.Tensor
``[num_experts, hidden_size, intermediate_size]`` packed FP4 FC2
weights, dtype ``uint8``.
Expand Down Expand Up @@ -4490,6 +4498,7 @@ def trtllm_fp4_block_scale_moe(
activation_type : int
Activation type (default ``3`` β€” Swiglu). ``3`` Swiglu; ``4`` Geglu;
``6`` Relu2; ``7`` Identity.
``10`` SiTU uses ``beta*tanh(x0/beta) * alpha*tanh(x1/alpha)*sigmoid(x1)``.
per_token_scale : Optional[torch.Tensor]
``[seq_len]`` per-token scaling factors, ``float32``.
output : Optional[torch.Tensor]
Expand Down Expand Up @@ -4628,10 +4637,17 @@ def trtllm_fp4_block_scale_routed_moe(
``[num_experts, 2 * intermediate_size]`` FC1 bias, float32.
gemm1_alpha : Optional[torch.Tensor]
``[num_experts]`` swiglu alpha, float32.
For SiTU this is ``[local_num_experts]``, finite and positive;
``None`` materializes per-expert ``alpha=1``.

gemm1_beta : Optional[torch.Tensor]
``[num_experts]`` swiglu beta, float32.
For SiTU this is ``[local_num_experts]``, finite and positive;
``None`` materializes per-expert ``beta=1``.
gemm1_clamp_limit : Optional[torch.Tensor]
``[num_experts]`` swiglu clamp limit, float32.
For SiTU a provided limit is per-local-expert, finite, and positive;
it clamps ``x0`` to ``[-limit, limit]`` and ``x1`` from above.
gemm2_weights : torch.Tensor
``[num_experts, hidden_size, intermediate_size]`` packed FP4 FC2
weights, ``uint8``.
Expand Down Expand Up @@ -4689,6 +4705,7 @@ def trtllm_fp4_block_scale_routed_moe(
Whether to enable Programmatic Dependent Launch.
activation_type : int
Activation type (default ``3`` β€” Swiglu).
``10`` SiTU uses ``beta*tanh(x0/beta) * alpha*tanh(x1/alpha)*sigmoid(x1)``.
per_token_scale : Optional[torch.Tensor]
``[seq_len]`` per-token scaling factors, float32.
output : Optional[torch.Tensor]
Expand Down
6 changes: 4 additions & 2 deletions flashinfer/tllm_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class ActivationType(IntEnum):
SwigluStep = 7
GegluTanh = 8
Identity = 9
InvalidType = 10
Situ = 10
InvalidType = 11

# Eval-safe repr β€” see ``RoutingMethodType.__repr__``.
def __repr__(self) -> str:
Expand All @@ -92,6 +93,7 @@ def is_gated(self) -> bool:
ActivationType.SwigluBias,
ActivationType.SwigluStep,
ActivationType.GegluTanh,
ActivationType.Situ,
)


Expand Down Expand Up @@ -128,7 +130,7 @@ def is_gated_activation(activation_type: Union[int, ActivationType]) -> bool:
-------
bool
``True`` if ``activation_type`` belongs to the gated activation family
(``Swiglu``, ``Geglu``, ``SwigluBias``, ``SwigluStep``, ``GegluTanh``);
(``Swiglu``, ``Geglu``, ``SwigluBias``, ``SwigluStep``, ``GegluTanh``, ``Situ``);
``False`` otherwise.

Examples
Expand Down
Loading
Loading