55 ************************************************************************/
66
77#include < assert.h>
8- #include < climits>
98#include < cuda_runtime.h>
109#include < transformer_engine/fused_router.h>
1110
11+ #include < climits>
12+
1213#include " ../common.h"
1314#include " ../util/logging.h"
1415#include " async_loader.h"
@@ -24,11 +25,12 @@ namespace fused_router {
2425// =============================================================================
2526
2627template <typename DataType, typename BiasType, TopkFuncType TopkFunc = TopkFuncType::Naive>
27- __global__ void fused_topk_forward_simple_kernel (
28- const DataType *logits, int num_tokens, int num_experts, int topk, bool use_pre_softmax,
29- int num_groups, int group_topk, float scaling_factor, int score_function,
30- const BiasType *expert_bias, DataType *probs, bool *routing_map,
31- CompType *intermediate_output) {
28+ __global__ void fused_topk_forward_simple_kernel (const DataType *logits, int num_tokens,
29+ int num_experts, int topk, bool use_pre_softmax,
30+ int num_groups, int group_topk,
31+ float scaling_factor, int score_function,
32+ const BiasType *expert_bias, DataType *probs,
33+ bool *routing_map, CompType *intermediate_output) {
3234 int num_token_per_block = blockDim .x / kThreadsPerWarp ;
3335 int warp_id = threadIdx .x / kThreadsPerWarp ;
3436 int lane_id = threadIdx .x % kThreadsPerWarp ;
@@ -112,9 +114,8 @@ __global__ void fused_topk_forward_simple_kernel(
112114 if (group_topk > 0 ) {
113115 int group_size = num_experts / num_groups;
114116 for (int i = 0 ; i < num_groups; i++) {
115- topk_and_mask<TopkFunc>(
116- scores + i * group_size, group_size, topk / group_topk,
117- topk_indices, topk_scores, lane_id);
117+ topk_and_mask<TopkFunc>(scores + i * group_size, group_size, topk / group_topk,
118+ topk_indices, topk_scores, lane_id);
118119 __syncwarp ();
119120 if (lane_id == 0 ) {
120121 CompType tmp = 0.0 ;
@@ -125,8 +126,8 @@ __global__ void fused_topk_forward_simple_kernel(
125126 }
126127 __syncwarp ();
127128 }
128- topk_and_mask<TopkFunc>(
129- group_scores, num_groups, group_topk, topk_indices, topk_scores, lane_id);
129+ topk_and_mask<TopkFunc>(group_scores, num_groups, group_topk, topk_indices, topk_scores,
130+ lane_id);
130131 __syncwarp ();
131132 for (int i = 0 ; i < group_topk; i++) {
132133 int st = topk_indices[i] * group_size;
@@ -430,7 +431,8 @@ __global__ void fused_topk_with_score_function_forward_kernel(
430431 // Sigmoid/Sqrtsoftplus post-processing when topk > 1
431432 if constexpr (ScoreFunc == 0 || ScoreFunc == 2 ) {
432433 if (topk > 1 ) {
433- CompType sum_scores = warp_reduce_on_shmem<CompType, ReduceFuncType::SUM >(topk_scores, topk, lane_id);
434+ CompType sum_scores =
435+ warp_reduce_on_shmem<CompType, ReduceFuncType::SUM >(topk_scores, topk, lane_id);
434436 for (int i = lane_id; i < topk; i += kThreadsPerWarp ) {
435437 topk_scores[i] = topk_scores[i] / (sum_scores + epsilon);
436438 }
@@ -462,8 +464,7 @@ void fused_topk_with_score_function_forward_kernel_launcher(
462464 " num_tokens * num_experts exceeds INT_MAX (kernel uses int offsets), got " ,
463465 static_cast <int64_t >(num_tokens) * num_experts);
464466 if (group_topk > 0 ) {
465- NVTE_CHECK (topk % group_topk == 0 ,
466- " topk must be divisible by group_topk, got topk=" , topk,
467+ NVTE_CHECK (topk % group_topk == 0 , " topk must be divisible by group_topk, got topk=" , topk,
467468 " group_topk=" , group_topk);
468469 }
469470 size_t num_token_per_block = kThreadsPerBlock / kThreadsPerWarp ;
@@ -504,8 +505,8 @@ void fused_topk_with_score_function_forward_kernel_launcher(
504505 check_shared_memory_capacity_num_experts (other_shmem, num_experts);
505506
506507 auto launch_simple = [&](auto kernel) {
507- NVTE_CHECK_CUDA (cudaFuncSetAttribute (kernel, cudaFuncAttributeMaxDynamicSharedMemorySize,
508- other_shmem));
508+ NVTE_CHECK_CUDA (
509+ cudaFuncSetAttribute (kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, other_shmem));
509510 kernel<<<total_blocks, kThreadsPerBlock , other_shmem, stream>>> (
510511 logits, num_tokens, num_experts, topk, use_pre_softmax, num_groups, group_topk,
511512 scaling_factor, score_function, expert_bias, probs, routing_map, intermediate_output);
0 commit comments