[codex] Optimize hidden-size 512 RMSNorm dispatch (#24710)

Co-authored-by: Codex <codex@example.com>
This commit is contained in:
Xiaoyu Zhang
2026-05-19 09:26:10 +08:00
committed by GitHub
co-authored by Codex
parent 170fe57cf0
commit 2424303dfb
3 changed files with 33 additions and 21 deletions
@@ -85,6 +85,10 @@ __global__ __launch_bounds__(kDim / 16) void rmsnorm_cta_double(const RMSNormPar
} }
sum_of_squares = warp::reduce_sum(sum_of_squares); sum_of_squares = warp::reduce_sum(sum_of_squares);
float norm_factor;
if constexpr (kNumWarps == 1) {
norm_factor = math::rsqrt(sum_of_squares / kDim + eps);
} else {
const auto warp_id = threadIdx.x / kWarpThreads; const auto warp_id = threadIdx.x / kWarpThreads;
smem[warp_id] = sum_of_squares; smem[warp_id] = sum_of_squares;
__syncthreads(); __syncthreads();
@@ -95,7 +99,8 @@ __global__ __launch_bounds__(kDim / 16) void rmsnorm_cta_double(const RMSNormPar
smem[tx] = math::rsqrt(sum_of_squares / kDim + eps); smem[tx] = math::rsqrt(sum_of_squares / kDim + eps);
} }
__syncthreads(); __syncthreads();
const float norm_factor = smem[warp_id]; norm_factor = smem[warp_id];
}
Storage output_first, output_second; Storage output_first, output_second;
#pragma unroll #pragma unroll
@@ -147,6 +152,10 @@ __global__ __launch_bounds__(kDim / 16) void rmsnorm_cta_wide(const RMSNormParam
} }
sum_of_squares = warp::reduce_sum(sum_of_squares); sum_of_squares = warp::reduce_sum(sum_of_squares);
float norm_factor;
if constexpr (kNumWarps == 1) {
norm_factor = math::rsqrt(sum_of_squares / kDim + eps);
} else {
const auto warp_id = threadIdx.x / kWarpThreads; const auto warp_id = threadIdx.x / kWarpThreads;
smem[warp_id] = sum_of_squares; smem[warp_id] = sum_of_squares;
__syncthreads(); __syncthreads();
@@ -157,7 +166,8 @@ __global__ __launch_bounds__(kDim / 16) void rmsnorm_cta_wide(const RMSNormParam
smem[tx] = math::rsqrt(sum_of_squares / kDim + eps); smem[tx] = math::rsqrt(sum_of_squares / kDim + eps);
} }
__syncthreads(); __syncthreads();
const float norm_factor = smem[warp_id]; norm_factor = smem[warp_id];
}
Storage output_vec; Storage output_vec;
#pragma unroll #pragma unroll
+2
View File
@@ -46,6 +46,8 @@ def _is_supported_rmsnorm_hidden_size(d: int) -> bool:
def _rmsnorm_kernel_class(hidden_size: int) -> str: def _rmsnorm_kernel_class(hidden_size: int) -> str:
if hidden_size in _RMSNORM_WARP_SIZES: if hidden_size in _RMSNORM_WARP_SIZES:
return "RMSNormWarpKernel" return "RMSNormWarpKernel"
if hidden_size == 512:
return "RMSNormHalfKernel"
if hidden_size >= _RMSNORM_HALF_BLOCK_MIN_SIZE: if hidden_size >= _RMSNORM_HALF_BLOCK_MIN_SIZE:
if hidden_size % 512 == 0: if hidden_size % 512 == 0:
return "RMSNormHalfKernel" return "RMSNormHalfKernel"
@@ -88,7 +88,7 @@ def test_rmsnorm_hidden_size_support(hidden_size: int) -> None:
(64, "RMSNormWarpKernel"), (64, "RMSNormWarpKernel"),
(128, "RMSNormWarpKernel"), (128, "RMSNormWarpKernel"),
(256, "RMSNormWarpKernel"), (256, "RMSNormWarpKernel"),
(512, "RMSNormKernel"), (512, "RMSNormHalfKernel"),
(1536, "RMSNormKernel"), (1536, "RMSNormKernel"),
(2048, "RMSNormHalfKernel"), (2048, "RMSNormHalfKernel"),
(2304, "RMSNormKernel"), # NOTE: not 512 aligned (2304, "RMSNormKernel"), # NOTE: not 512 aligned