Skip to content
Open
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
49 changes: 6 additions & 43 deletions cpp/tensorrt_llm/kernels/trtllmGenKernels/fmha/fmhaKernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ class TllmGenFmhaKernel
parseOptionsFromRunnerParams(params, options);
options.mCudaArch = intToCudaArch(mSM);

std::tie(options, optionsFromArgs, ctaDim)
= selectKernelWithCgaSmemReductionLimit(options, optionsFromArgs, params.mMultiProcessorCount);
FmhaAutoTuner autoTuner(options, optionsFromArgs, params.mMultiProcessorCount);
std::tie(options, optionsFromArgs, ctaDim) = autoTuner.selectKernel();

// Check if the options are valid or not.
checkFmhaOptions(options, optionsFromArgs);
Expand Down Expand Up @@ -409,8 +409,8 @@ class TllmGenFmhaKernel
parseOptionsFromRunnerParams(params, options);
options.mCudaArch = intToCudaArch(mSM);

std::tie(options, optionsFromArgs, ctaDim)
= selectKernelWithCgaSmemReductionLimit(options, optionsFromArgs, params.mMultiProcessorCount);
FmhaAutoTuner autoTuner(options, optionsFromArgs, params.mMultiProcessorCount);
std::tie(options, optionsFromArgs, ctaDim) = autoTuner.selectKernel();

checkFmhaOptions(options, optionsFromArgs);
updateFmhaOptions(options, optionsFromArgs);
Expand Down Expand Up @@ -527,8 +527,8 @@ class TllmGenFmhaKernel
parseOptionsFromRunnerParams(params, options);
options.mCudaArch = intToCudaArch(mSM);

std::tie(options, optionsFromArgs, ctaDim)
= selectKernelWithCgaSmemReductionLimit(options, optionsFromArgs, params.mMultiProcessorCount);
FmhaAutoTuner autoTuner(options, optionsFromArgs, params.mMultiProcessorCount);
std::tie(options, optionsFromArgs, ctaDim) = autoTuner.selectKernel();

// Overwrite AutoTuner decision: SageAttention with SfsPV is known to cause regression to persistent scheduler.
// Remove this overwritten once we refresh the cubin kernels that containing the related fix.
Expand Down Expand Up @@ -663,43 +663,6 @@ class TllmGenFmhaKernel
}

private:
std::tuple<FmhaOptions, FmhaOptionsFromArgs, int32_t> selectKernelWithCgaSmemReductionLimit(
FmhaOptions options, FmhaOptionsFromArgs optionsFromArgs, int32_t multiProcessorCount) const
{
if (isGmemReduction(options.mMultiCtasKvMode) && options.mTileScheduler == TileScheduler::Static)
{
constexpr int kMaxCgaClusterDimX = 16;
constexpr int kMinPreSelectCgaClusterDimX = 2;
// selectKernel may promote this candidate to CgaSmemReduction and call computeNumCtas
// before returning the selected options. Keep the candidate legal for both 1-CTA and
// 2-CTA FMHA kernels; the exact selected clusterDimX is applied below.
options.mMaxNumCtasPerSeqKv = std::min(options.mMaxNumCtasPerSeqKv,
kMaxCgaClusterDimX / std::max(options.mClusterDimX, kMinPreSelectCgaClusterDimX));
}

int32_t ctaDim = 512;
FmhaAutoTuner autoTuner(options, optionsFromArgs, multiProcessorCount);
std::tie(options, optionsFromArgs, ctaDim) = autoTuner.selectKernel();
limitCgaSmemReductionCtasKv(options);
return {options, optionsFromArgs, ctaDim};
}

void limitCgaSmemReductionCtasKv(FmhaOptions& options) const
{
if (!isCgaSmemReduction(options.mMultiCtasKvMode))
{
return;
}
constexpr int kMaxCgaClusterDimX = 16;
if (options.mClusterDimX * options.mMaxNumCtasPerSeqKv > kMaxCgaClusterDimX)
{
TLLM_LOG_WARNING(
"CGA reduction is not supported when numCtasPerSeqKv * clusterDimX > 16. Set mMaxNumCtasPerSeqKv to "
"16 / clusterDimX");
options.mMaxNumCtasPerSeqKv = kMaxCgaClusterDimX / options.mClusterDimX;
}
}

inline uint64_t hashID(int qkvLayout, int maskType, int kernelType, int scheduler, int multiCtasKvMode,
int headDimPerCtaV, int headDimQk, int headDimV, int tileSizeQ, int tileSizeKv, int numTokensPerPage,
bool reuseSmemKForV, bool uses2CtaMma, int sparseAttention, bool skipsSoftmax, bool groupsHeadsQ,
Expand Down
Loading