Repository navigation
[None][perf] Optimize MiniMax-M3 NVFP4 context (prefill) path on Rubin (SM107) #20629
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
base: main
Are you sure you want to change the base?
Changes from all commits
59da05a
8b00e90
6431c53
f26bc90
9a37af5
f76ecba
64f54c8
5ce5fad
5d8d859
61f902f
5b0b789
4ad2fd5
841aa0a
b25cce4
1886b0f
badbf36
7d6fca5
89db928
f048e3c
d9c95bb
12f6dcc
4b01b99
ac62489
b5365fb
5085662
ce3a859
dc2f5c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- a/flash_attn/cute/flash_fwd_sm100.py | ||
| +++ b/flash_attn/cute/flash_fwd_sm100.py | ||
| @@ -2021,7 +2021,7 @@ | ||
|
|
||
| rescale_threshold = ( | ||
| 8.0 if const_expr(self.q_dtype.width == 16) else | ||
| - 4.0 if const_expr(self.q_dtype.width == 8) else | ||
| + 0.0 if const_expr(self.q_dtype.width == 8) else | ||
| 0.0 | ||
| ) | ||
| softmax = SoftmaxSm100.create( | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,6 +347,107 @@ __global__ void rms_norm_kernel(AllReduceParams params) | |
| #endif | ||
| } | ||
|
|
||
| //! rms_norm_kernel for rows that one CTA covers in a single pass (blockDim.x * kPackedSize >= hidden_size, no smem), | ||
| //! made persistent: each CTA walks rows blockIdx.x, blockIdx.x + gridDim.x, ... with the same thread -> element mapping | ||
| //! and block_reduce_sum tree as rms_norm_kernel, so the output is bitwise the same. The next row's inputs are loaded | ||
| //! before this row's reduction and the weight stays in registers, which keeps more bytes in flight per thread. | ||
| template <typename T, bool Bias, bool Residual, bool Affine> | ||
| __global__ void rms_norm_rows_kernel(AllReduceParams params) | ||
| { | ||
| static constexpr int kPackedSize = details::kBytesPerAccess / sizeof(T); | ||
| using PackedStruct = typename PackedOn16Bytes<T>::Type; | ||
|
|
||
| T const* bias_buffer = reinterpret_cast<T const*>(params.fusion_params.bias_buffer); | ||
| T const* residual_buffer = reinterpret_cast<T const*>(params.fusion_params.residual_buffer); | ||
| T const* weight_buffer = reinterpret_cast<T const*>(params.fusion_params.weight_buffer); | ||
| T* local_final_output_buffer = reinterpret_cast<T*>(params.local_output_buffer_ptr); | ||
| T* intermediate_buffer = reinterpret_cast<T*>(params.fusion_params.intermediate_buffer); | ||
|
|
||
| int const hidden = params.fusion_params.hidden_size; | ||
| int const rows = params.elts_total / hidden; | ||
| int const offset = threadIdx.x * kPackedSize; | ||
| bool const active = offset < hidden; | ||
|
|
||
| #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900) && (__CUDA_ARCH__ < 1200)) | ||
| cudaGridDependencySynchronize(); | ||
| #endif | ||
|
|
||
| PackedStruct weight_vec, bias_vec; | ||
| if (active) | ||
| { | ||
| if constexpr (Affine) | ||
| { | ||
| weight_vec.packed = *reinterpret_cast<int4 const*>(weight_buffer + offset); | ||
| } | ||
| if constexpr (Bias) | ||
| { | ||
| bias_vec.packed = *reinterpret_cast<int4 const*>(bias_buffer + offset); | ||
| } | ||
| } | ||
| PackedStruct inter_vec, residual_vec; | ||
| int row = blockIdx.x; | ||
| if (active && row < rows) | ||
| { | ||
| inter_vec.packed | ||
| = *reinterpret_cast<int4 const*>(intermediate_buffer + static_cast<int64_t>(row) * hidden + offset); | ||
| if constexpr (Residual) | ||
| { | ||
| residual_vec.packed | ||
| = *reinterpret_cast<int4 const*>(residual_buffer + static_cast<int64_t>(row) * hidden + offset); | ||
| } | ||
| } | ||
| for (; row < rows; row += gridDim.x) | ||
| { | ||
| int64_t const rowOffset = static_cast<int64_t>(row) * hidden + offset; | ||
| int const next = row + gridDim.x; | ||
| PackedStruct next_inter_vec, next_residual_vec; | ||
| if (active && next < rows) | ||
| { | ||
| next_inter_vec.packed | ||
| = *reinterpret_cast<int4 const*>(intermediate_buffer + static_cast<int64_t>(next) * hidden + offset); | ||
| if constexpr (Residual) | ||
| { | ||
| next_residual_vec.packed | ||
| = *reinterpret_cast<int4 const*>(residual_buffer + static_cast<int64_t>(next) * hidden + offset); | ||
| } | ||
| } | ||
| float acc = 0.f; | ||
| if (active) | ||
| { | ||
| if constexpr (Bias) | ||
| { | ||
| inter_vec.packed = add128b(inter_vec, bias_vec); | ||
| } | ||
| if constexpr (Residual) | ||
| { | ||
| inter_vec.packed = add128b(inter_vec, residual_vec); | ||
| *reinterpret_cast<int4*>(intermediate_buffer + rowOffset) = inter_vec.packed; | ||
| } | ||
| acc = accumulate<T>(acc, inter_vec); | ||
| } | ||
| acc = block_reduce_sum(acc); | ||
| float denom = rsqrtf(acc / hidden + params.fusion_params.eps); | ||
| if (active) | ||
| { | ||
| PackedStruct out_vec; | ||
| out_vec.packed = rms_norm<T, Affine>(denom, inter_vec, weight_vec); | ||
| *reinterpret_cast<int4*>(local_final_output_buffer + rowOffset) = out_vec.packed; | ||
| } | ||
| // block_reduce_sum's shared memory is written again by the next row. | ||
| __syncthreads(); | ||
| inter_vec.packed = next_inter_vec.packed; | ||
| residual_vec.packed = next_residual_vec.packed; | ||
| } | ||
| #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900) && (__CUDA_ARCH__ < 1200)) | ||
| cudaTriggerProgrammaticLaunchCompletion(); | ||
| #endif | ||
| } | ||
|
|
||
| // Kill switch for the SM107 rms_norm_rows_kernel path of rms_norm_kernel_launcher, and the fewest rows it takes: | ||
| // below that the per-row kernel is as fast, so decode-sized steps keep it. | ||
| constexpr bool kRmsNormRowsSm107 = true; | ||
| constexpr int kRmsNormRowsSm107MinRows = 1024; | ||
|
|
||
| template <typename T, bool Bias = false, bool Residual = false, bool Affine = false> | ||
| __global__ void rms_pre_post_norm_kernel(AllReduceParams params) // for gemma2 pre residual + post residual norm | ||
| { | ||
|
|
@@ -495,6 +596,24 @@ void rms_norm_kernel_launcher(AllReduceParams& params, cudaStream_t stream, AllR | |
| } | ||
| else | ||
| { | ||
| // SM107: a single-pass RMSNorm over many rows runs persistent (rms_norm_rows_kernel, bitwise the same). | ||
| if (kRmsNormRowsSm107 && fusionOp == AllReduceFusionOp::RESIDUAL_RMS_NORM && cta_num >= kRmsNormRowsSm107MinRows | ||
|
Collaborator
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. On current main, |
||
| && tensorrt_llm::common::getSMVersion() == 107) | ||
| { | ||
| static int const multiProcessorCount = tensorrt_llm::common::getMultiProcessorCount(); | ||
| cudaLaunchConfig_t kernelConfig = {0}; | ||
| kernelConfig.gridDim = std::min(cta_num, multiProcessorCount); | ||
| kernelConfig.blockDim = cta_size; | ||
| kernelConfig.dynamicSmemBytes = 0; | ||
| kernelConfig.stream = stream; | ||
| cudaLaunchAttribute attribute[1]; | ||
| attribute[0].id = cudaLaunchAttributeProgrammaticStreamSerialization; | ||
| attribute[0].val.programmaticStreamSerializationAllowed = tensorrt_llm::common::getEnvEnablePDL(); | ||
| kernelConfig.attrs = attribute; | ||
| kernelConfig.numAttrs = 1; | ||
| TLLM_CUDA_CHECK(cudaLaunchKernelEx(&kernelConfig, rms_norm_rows_kernel<T, Bias, Residual, Affine>, params)); | ||
| return; | ||
| } | ||
| if (tensorrt_llm::common::getEnvEnablePDL()) | ||
| { | ||
| TLLM_LOG_DEBUG("Enable PDL in rms_norm_kernel"); | ||
|
|
||
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.
Can we replace this patch and the
setup_fa4.sh/PYTHONPATHoverlay with upstream FA4, using these dependency lower bounds while retaining CUTLASS DSL 4.8.0?FlashAttention #2731 already merged the FP8
rescale_threshold=0.0fix, and the published4.0.0b38wheel contains it. Its declared dependencies are compatible with CUTLASS DSL 4.8.0 and Quack 0.6.5: FA4 requires DSL >=4.6.2, and Quack 0.6.5 requires DSL >=4.7. The comment inrequirements.txtsaying Quack 0.5.0 is the only compatible release should be updated too.Please also replace
_DENSE_FA4_PATCH_MARKER/inspect.getsource()detection inmsa_prefill.py: upstream uses8.0 if const_expr(self.q_dtype.width == 16) else 0.0, so the current exact-string check rejects the fixed upstream implementation. We can rely on the validated dependency requirement and retain the existing availability/fallback checks. Before removing the patch, validate the existing SM107 FA4/MSA accuracy and performance tests and the Quack SwiGLU path with the upgraded packages; dependency compatibility alone does not establish runtime compatibility. CI/release images can record the exact tested versions separately.