-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathmoeAlltoAllKernels.cu
More file actions
1436 lines (1313 loc) · 66.3 KB
/
Copy pathmoeAlltoAllKernels.cu
File metadata and controls
1436 lines (1313 loc) · 66.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2025-2026, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/config.h"
#include "tensorrt_llm/common/cudaUtils.h"
#include "tensorrt_llm/common/envUtils.h"
#include "tensorrt_llm/common/vec_dtypes.cuh"
#include "tensorrt_llm/kernels/communicationKernels/moeAlltoAllKernels.h"
#include "tensorrt_llm/kernels/quantization.cuh"
#include <cooperative_groups.h>
#include <cstdint>
#include <type_traits>
TRTLLM_NAMESPACE_BEGIN
namespace kernels::moe_comm
{
using tensorrt_llm::common::launchWithPdlWhenEnabled;
#define ENABLE_DEBUG_PRINT 0
#define DISABLE_SYNC_FOR_PROFILING 0
#ifndef DISABLE_TIMEOUT
#define DISABLE_TIMEOUT 0
#endif
// Macros for concise launch-time specialization
#define SWITCH_BOOL(flag, NAME, ...) \
if (flag) \
{ \
constexpr bool NAME = true; \
__VA_ARGS__ \
} \
else \
{ \
constexpr bool NAME = false; \
__VA_ARGS__ \
}
#define SWITCH_TOP_K(top_k, TOP_K, ...) \
switch (top_k) \
{ \
case 22: \
{ \
constexpr int TOP_K = 22; \
__VA_ARGS__; \
break; \
} \
case 18: \
{ \
constexpr int TOP_K = 18; \
__VA_ARGS__; \
break; \
} \
case 16: \
{ \
constexpr int TOP_K = 16; \
__VA_ARGS__; \
break; \
} \
case 14: \
{ \
constexpr int TOP_K = 14; \
__VA_ARGS__; \
break; \
} \
case 12: \
{ \
constexpr int TOP_K = 12; \
__VA_ARGS__; \
break; \
} \
case 10: \
{ \
constexpr int TOP_K = 10; \
__VA_ARGS__; \
break; \
} \
case 8: \
{ \
constexpr int TOP_K = 8; \
__VA_ARGS__; \
break; \
} \
case 6: \
{ \
constexpr int TOP_K = 6; \
__VA_ARGS__; \
break; \
} \
case 4: \
{ \
constexpr int TOP_K = 4; \
__VA_ARGS__; \
break; \
} \
case 2: \
{ \
constexpr int TOP_K = 2; \
__VA_ARGS__; \
break; \
} \
case 1: \
{ \
constexpr int TOP_K = 1; \
__VA_ARGS__; \
break; \
} \
default: \
{ \
TLLM_CHECK_WITH_INFO(false, "Unsupported top_k"); \
} \
}
#define SWITCH_DTYPE(dtype, TYPE, ...) \
switch (dtype) \
{ \
case nvinfer1::DataType::kHALF: \
{ \
using TYPE = half; \
__VA_ARGS__; \
break; \
} \
case nvinfer1::DataType::kBF16: \
{ \
using TYPE = __nv_bfloat16; \
__VA_ARGS__; \
break; \
} \
case nvinfer1::DataType::kFLOAT: \
{ \
using TYPE = float; \
__VA_ARGS__; \
break; \
} \
case nvinfer1::DataType::kFP8: \
{ \
using TYPE = __nv_fp8_e4m3; \
__VA_ARGS__; \
break; \
} \
default: \
{ \
TLLM_CHECK_WITH_INFO(false, "Unsupported dtype for moe_a2a_combine"); \
} \
}
#if DISABLE_TIMEOUT
#define check_timeout(s) false
#else
// 300 * 2000 MHz - should be high enough on any GPU but will prevent a hang
#define check_timeout(s) ((clock64() - (s)) > (300ll * 2000ll * 1000ll * 1000ll))
#endif
// ============================================================================
// Helper Functions for Expert-to-Rank Mapping
// ============================================================================
// Compute which rank owns a given expert using contiguous ceil/floor partitioning.
// Supports non-divisible distribution when num_experts % ep_size != 0:
// base = num_experts / ep_size
// remainder = num_experts % ep_size
// - Ranks [0, remainder) each own (base + 1) experts.
// - Ranks [remainder, ep_size) each own base experts.
//
// Example A (uniform): 32 experts, 4 ranks -> base=8, remainder=0
// - Rank 0: experts 0-7
// - Rank 1: experts 8-15
// - Rank 2: experts 16-23
// - Rank 3: experts 24-31
//
// Example B (non-divisible): 384 experts, 5 ranks -> base=76, remainder=4
// - Rank 0: experts 0-76 (77 experts)
// - Rank 1: experts 77-153 (77 experts)
// - Rank 2: experts 154-230 (77 experts)
// - Rank 3: experts 231-307 (77 experts)
// - Rank 4: experts 308-383 (76 experts)
//
// `base` and `remainder` are precomputed by the caller once outside the per-token TOP_K loop
// so the hot path performs at most one integer divide.
__device__ __forceinline__ int compute_target_rank_id(int expert_id, int base, int remainder)
{
// Fast path for the uniform (num_experts % ep_size == 0) case: identical to the
// pre-ceil/floor implementation, so existing divisible deployments incur no overhead.
if (remainder == 0)
{
return expert_id / base;
}
int const split = remainder * (base + 1); // boundary expert id
if (expert_id < split)
{
// Falls inside the (base + 1)-sized prefix block.
return expert_id / (base + 1);
}
// Falls inside the base-sized suffix block.
return remainder + (expert_id - split) / base;
}
// Test bit `rank` in a kRankMaskWords-wide little-endian uint64 bitmask.
// Word 0 covers ranks 0..63, word 1 covers ranks 64..127, etc.
// `rank >> 6` and `rank & 63` divide / modulo by 64.
__device__ __forceinline__ bool is_rank_active(uint64_t const* mask, int rank)
{
return (mask[rank >> 6] >> (rank & 63)) & 1ULL;
}
// ============================================================================
// Helper Functions for Vectorized Memory Operations
// ============================================================================
struct BlockPolicy
{
__device__ static int stride()
{
return blockDim.x;
}
__device__ static int offset()
{
return threadIdx.x;
}
__device__ static int token_idx()
{
return blockIdx.x;
}
__device__ static void sync()
{
__syncthreads();
}
};
template <int VEC_SIZE, typename ThreadingPolicy>
__device__ void vectorized_copy_impl(void* dst, void const* src, int size)
{
using flashinfer::vec_t;
uint8_t* dst_ptr = static_cast<uint8_t*>(dst);
uint8_t const* src_ptr = static_cast<uint8_t const*>(src);
int const stride = ThreadingPolicy::stride() * VEC_SIZE;
for (int offset = ThreadingPolicy::offset() * VEC_SIZE; offset < size; offset += stride)
{
vec_t<uint8_t, VEC_SIZE> v;
v.load(src_ptr + offset);
v.store(dst_ptr + offset);
}
}
template <typename ThreadingPolicy>
__device__ void vectorized_copy(void* dst, void const* src, int size)
{
if (size % 16 == 0)
{
vectorized_copy_impl<16, ThreadingPolicy>(dst, src, size);
}
else if (size % 8 == 0)
{
vectorized_copy_impl<8, ThreadingPolicy>(dst, src, size);
}
else if (size % 4 == 0)
{
vectorized_copy_impl<4, ThreadingPolicy>(dst, src, size);
}
else if (size % 2 == 0)
{
vectorized_copy_impl<2, ThreadingPolicy>(dst, src, size);
}
else
{
vectorized_copy_impl<1, ThreadingPolicy>(dst, src, size);
}
}
// Vectorized dispatch: load one vec from source and write to up to TOP_K destinations
template <int VEC_SIZE, int TOP_K, typename ThreadingPolicy>
__device__ void vectorized_dispatch_impl(uint8_t const* src_ptr, int bytes_per_token, int rank_id,
int max_tokens_per_rank, int payload_idx, DispatchKernelPointers const& ptrs, int const* topk_target_ranks,
int const* topk_send_indices)
{
using flashinfer::vec_t;
// Precompute destination base pointers per k
uint8_t* dst_base_k[TOP_K];
#pragma unroll
for (int k = 0; k < TOP_K; ++k)
{
int dst_idx_k = topk_send_indices[k];
int target_rank_k = topk_target_ranks[k];
if (dst_idx_k < 0)
{
dst_base_k[k] = nullptr;
continue;
}
uint8_t* dst_data = static_cast<uint8_t*>(ptrs.recv_buffers[target_rank_k][payload_idx]);
size_t base_source_rank
= static_cast<size_t>(rank_id) * static_cast<size_t>(max_tokens_per_rank) + static_cast<size_t>(dst_idx_k);
size_t base_token = base_source_rank * static_cast<size_t>(bytes_per_token);
dst_base_k[k] = dst_data + base_token;
}
// TODO: process all payloads. index could be reused.
int const stride = ThreadingPolicy::stride() * VEC_SIZE;
for (int offset = ThreadingPolicy::offset() * VEC_SIZE; offset < bytes_per_token; offset += stride)
{
vec_t<uint8_t, VEC_SIZE> v;
v.load(src_ptr + offset);
#pragma unroll
for (int k = 0; k < TOP_K; ++k)
{
uint8_t* dst_base = dst_base_k[k];
if (dst_base == nullptr)
{
continue;
}
v.store(dst_base + offset);
}
}
}
template <int TOP_K, typename ThreadingPolicy>
__device__ void vectorized_dispatch(uint8_t const* src_ptr, int bytes_per_token, int rank_id, int max_tokens_per_rank,
int payload_idx, DispatchKernelPointers const& ptrs, int const* topk_target_ranks, int const* topk_send_indices)
{
if (bytes_per_token % 16 == 0)
{
vectorized_dispatch_impl<16, TOP_K, ThreadingPolicy>(src_ptr, bytes_per_token, rank_id, max_tokens_per_rank,
payload_idx, ptrs, topk_target_ranks, topk_send_indices);
}
else if (bytes_per_token % 8 == 0)
{
vectorized_dispatch_impl<8, TOP_K, ThreadingPolicy>(src_ptr, bytes_per_token, rank_id, max_tokens_per_rank,
payload_idx, ptrs, topk_target_ranks, topk_send_indices);
}
else if (bytes_per_token % 4 == 0)
{
vectorized_dispatch_impl<4, TOP_K, ThreadingPolicy>(src_ptr, bytes_per_token, rank_id, max_tokens_per_rank,
payload_idx, ptrs, topk_target_ranks, topk_send_indices);
}
else if (bytes_per_token % 2 == 0)
{
vectorized_dispatch_impl<2, TOP_K, ThreadingPolicy>(src_ptr, bytes_per_token, rank_id, max_tokens_per_rank,
payload_idx, ptrs, topk_target_ranks, topk_send_indices);
}
else
{
vectorized_dispatch_impl<1, TOP_K, ThreadingPolicy>(src_ptr, bytes_per_token, rank_id, max_tokens_per_rank,
payload_idx, ptrs, topk_target_ranks, topk_send_indices);
}
}
__global__ void moeA2APrepareDispatchKernel(
int* send_counters, int* local_token_counter, int ep_size, uint32_t* flag_val_ptr)
{
#if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900))
cudaGridDependencySynchronize();
cudaTriggerProgrammaticLaunchCompletion();
#endif
int idx = blockIdx.x * blockDim.x + threadIdx.x;
// Zero send_counters
if (idx < ep_size)
{
send_counters[idx] = 0;
}
// Zero local_token_counter and increment flag_val
if (idx == 0)
{
*local_token_counter = 0;
// Increment flag_val for this dispatch round
*flag_val_ptr = *flag_val_ptr + 1;
}
}
// ============================================================================
// Dispatch Kernels
// ============================================================================
template <typename ThreadingPolicy, int TOP_K, bool ENABLE_EPLB>
__global__ void moeA2ADispatchKernel(int32_t const* token_selected_experts, // [local_num_tokens, TOP_K]
const DispatchKernelPointers ptrs, // Struct containing all kernel pointers
int num_payloads, // Number of payloads
int max_tokens_per_rank, // Maximum tokens per rank
int local_num_tokens, int rank_id, int ep_size, int num_experts, int eplb_stats_num_experts)
{
int thread_idx = ThreadingPolicy::offset();
int local_token_idx = ThreadingPolicy::token_idx();
if (local_num_tokens == 0)
{
// Special case: If local_num_tokens == 0,
// we need to keep the threads where local_token_idx == 0 alive to participate in the synchronization.
// Other threads should return.
if (local_token_idx > 0)
return;
#if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900))
cudaGridDependencySynchronize();
#endif
}
else
{
// Threads that do not have a token to process should return.
if (local_token_idx >= local_num_tokens)
return;
// One block per token: a single shared-memory tile is reused by the entire CTA.
extern __shared__ int smem[];
int* smem_topk_target_ranks = smem;
int* smem_topk_send_indices = smem + TOP_K;
uint64_t already_copied[kRankMaskWords] = {};
// Precompute the ceil/floor partition parameters once per thread, outside the
// per-token TOP_K loop. The fast path (remainder == 0) then collapses to a single
// integer divide per call, matching the pre-PR uniform-partition cost exactly.
int const ep_base = num_experts / ep_size;
int const ep_remainder = num_experts - ep_base * ep_size; // == num_experts % ep_size
#if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900))
cudaGridDependencySynchronize();
#endif
for (int k = 0; k < TOP_K; k++)
{
int expert_id = token_selected_experts[local_token_idx * TOP_K + k];
// Use contiguous ceil/floor partitioning to determine target rank.
// Supports the non-divisible case where num_experts % ep_size != 0.
int target_rank = compute_target_rank_id(expert_id, ep_base, ep_remainder);
// Skip duplicates AND dead ranks: both produce the same -1 sentinel that combine
// checks via topk_send_indices[k] < 0. A token whose only target is dead is dropped
// from this collective; higher-layer logic (EPLB redistribution) is responsible
// for re-routing such tokens on subsequent iterations.
int const mask_word = target_rank >> 6;
uint64_t const mask_bit = 1ULL << (target_rank & 63);
bool const target_already_copied = already_copied[mask_word] & mask_bit;
bool const target_dead = !is_rank_active(ptrs.active_rank_mask, target_rank);
if (target_already_copied || target_dead)
{
if (thread_idx == 0)
{
ptrs.topk_target_ranks[local_token_idx * TOP_K + k] = -1;
ptrs.topk_send_indices[local_token_idx * TOP_K + k] = -1;
// Mirror to shared memory immediately
smem_topk_target_ranks[k] = -1;
smem_topk_send_indices[k] = -1;
}
continue;
}
// Only one thread per warp should increment the counter
int dst_token_idx;
if (thread_idx == 0)
{
dst_token_idx = atomicAdd(&ptrs.send_counters[target_rank], 1);
ptrs.topk_target_ranks[local_token_idx * TOP_K + k] = target_rank;
ptrs.topk_send_indices[local_token_idx * TOP_K + k] = dst_token_idx;
// Mirror to shared memory immediately
smem_topk_target_ranks[k] = target_rank;
smem_topk_send_indices[k] = dst_token_idx;
}
already_copied[mask_word] |= mask_bit;
}
// Sync before dispatching data
ThreadingPolicy::sync();
// Read staged routing once into registers per thread
int topk_target_ranks[TOP_K];
int topk_send_indices[TOP_K];
#pragma unroll
for (int k = 0; k < TOP_K; ++k)
{
topk_target_ranks[k] = smem_topk_target_ranks[k];
topk_send_indices[k] = smem_topk_send_indices[k];
}
// Perform a single source load and TOP_K fanout per payload
for (int payload_idx = 0; payload_idx < num_payloads; payload_idx++)
{
uint8_t const* src_data = static_cast<uint8_t const*>(ptrs.src_data_ptrs[payload_idx]);
int bytes_per_token = ptrs.payload_bytes_per_token[payload_idx];
uint8_t const* src_ptr = src_data + local_token_idx * bytes_per_token;
vectorized_dispatch<TOP_K, ThreadingPolicy>(src_ptr, bytes_per_token, rank_id, max_tokens_per_rank,
payload_idx, ptrs, topk_target_ranks, topk_send_indices);
}
ThreadingPolicy::sync();
}
#if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900))
cudaTriggerProgrammaticLaunchCompletion();
#endif
bool is_first_warp = threadIdx.x / warpSize == 0;
if (is_first_warp)
{
int lane_id = threadIdx.x % warpSize;
bool is_last_token = false;
if (lane_id == 0)
{
if (local_num_tokens != 0)
{
int cnt = atomicAdd(ptrs.local_token_counter, 1);
is_last_token = cnt + 1 == local_num_tokens;
}
else
{
is_last_token = true;
}
}
is_last_token = __shfl_sync(0xffffffff, is_last_token, 0);
if (is_last_token)
{
// Store send_counters to recv_counters.
// Skip masked target ranks: their symmetric memory may be inaccessible.
#pragma unroll 1 // No unroll as one iter is typically enough
for (int target_rank = lane_id; target_rank < ep_size; target_rank += warpSize)
{
if (!is_rank_active(ptrs.active_rank_mask, target_rank))
continue;
int send_count = ptrs.send_counters[target_rank];
ptrs.recv_counters[target_rank][rank_id] = send_count;
}
if constexpr (ENABLE_EPLB)
{
// Write local stats into peer buffers before the release fence below.
// Skip masked target ranks for the same reason as above.
#pragma unroll 1
for (int target_rank = 0; target_rank < ep_size; ++target_rank)
{
if (!is_rank_active(ptrs.active_rank_mask, target_rank))
continue;
int* target_stats = ptrs.eplb_gathered_stats[target_rank];
for (int expert_id = lane_id; expert_id < eplb_stats_num_experts; expert_id += warpSize)
{
int stat_val = ptrs.eplb_local_stats[expert_id];
target_stats[rank_id * eplb_stats_num_experts + expert_id] = stat_val;
}
}
}
#if !DISABLE_SYNC_FOR_PROFILING
uint32_t expected_value = *ptrs.flag_val;
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 900
// .acquire and .release qualifiers for fence instruction require sm_90 or higher.
asm volatile("fence.release.sys;");
#else
asm volatile("fence.acq_rel.sys;");
#endif
// Signal completion to all active peers; skip dead ranks (their symmetric memory
// is unreachable).
#pragma unroll 1 // No unroll as one iter is typically enough
for (int target_rank = lane_id; target_rank < ep_size; target_rank += warpSize)
{
if (!is_rank_active(ptrs.active_rank_mask, target_rank))
continue;
uint32_t* flag_addr = &ptrs.completion_flags[target_rank][rank_id];
asm volatile("st.relaxed.sys.u32 [%0], %1;" ::"l"(flag_addr), "r"(expected_value));
#if ENABLE_DEBUG_PRINT
printf("dispatch: +++Rank %d setting completion flag to %d for rank %d\n", rank_id, expected_value,
target_rank);
#endif
}
// Wait for all active peers to signal; skip dead ranks (otherwise we would
// spin forever — this is the bug the rank-mask is here to prevent).
#pragma unroll 1 // No unroll
for (int peer_rank = lane_id; peer_rank < ep_size; peer_rank += warpSize)
{
if (!is_rank_active(ptrs.active_rank_mask, peer_rank))
continue;
bool flag_set = false;
auto s = clock64();
do
{
uint32_t* flag_ptr = &ptrs.completion_flags[rank_id][peer_rank];
uint32_t flag_value;
// Acquire load to ensure visibility of peer's release-store
asm volatile("ld.relaxed.sys.u32 %0, [%1];" : "=r"(flag_value) : "l"(flag_ptr));
#if ENABLE_DEBUG_PRINT
printf(
"combine: ---Rank %d received completion flag from rank %d, flag_value: %d, expected_value: "
"%d, address: %p\n",
rank_id, peer_rank, flag_value, expected_value, flag_ptr);
#endif
flag_set = flag_value == expected_value;
} while (!flag_set && !check_timeout(s));
if (__builtin_expect(!flag_set, 0))
{
printf("dispatch: ---Rank %d timed out waiting for completion flag from rank %d\n", rank_id,
peer_rank);
asm volatile("trap;");
return;
}
}
#endif
}
}
}
void moe_a2a_prepare_dispatch_launch(MoeA2ADispatchParams const& params)
{
launchWithPdlWhenEnabled("moeA2APrepareDispatchKernel", moeA2APrepareDispatchKernel, 1, params.ep_size, 0,
params.stream, params.send_counters, params.local_token_counter, params.ep_size, params.flag_val);
}
// ============================================================================
// Launch Functions
// ============================================================================
void moe_a2a_dispatch_launch(MoeA2ADispatchParams const& params)
{
// Validate parameters
TLLM_CHECK(params.top_k > 0 && params.top_k <= kMaxTopK);
TLLM_CHECK(params.ep_size > 0 && params.ep_size <= kMaxRanks);
TLLM_CHECK(params.ep_rank >= 0 && params.ep_rank < params.ep_size);
TLLM_CHECK(params.local_num_tokens >= 0);
TLLM_CHECK(params.num_payloads > 0 && params.num_payloads <= kMaxPayloads);
// The local rank must always be marked active in its own view of the mask;
// otherwise the kernel itself would be running on a "dead" rank.
TLLM_CHECK_WITH_INFO((params.active_rank_mask[params.ep_rank >> 6] >> (params.ep_rank & 63)) & 1ULL,
"active_rank_mask must mark the local ep_rank (%d) as active", params.ep_rank);
// Prepare kernel pointers struct
DispatchKernelPointers kernel_ptrs = {};
// Fill source data pointers and payload sizes
for (int i = 0; i < params.num_payloads; i++)
{
kernel_ptrs.src_data_ptrs[i] = params.payloads[i].src_data;
kernel_ptrs.payload_bytes_per_token[i]
= params.payloads[i].element_size * params.payloads[i].elements_per_token;
}
// Fill receive buffer pointers
for (int target_rank = 0; target_rank < params.ep_size; target_rank++)
{
kernel_ptrs.recv_counters[target_rank] = params.recv_counters[target_rank];
kernel_ptrs.eplb_gathered_stats[target_rank] = params.eplb_gathered_stats[target_rank];
for (int payload = 0; payload < params.num_payloads; payload++)
{
kernel_ptrs.recv_buffers[target_rank][payload] = params.recv_buffers[target_rank][payload];
}
}
// Copy completion flag pointers
for (int i = 0; i < params.ep_size; i++)
{
kernel_ptrs.completion_flags[i] = params.completion_flags[i];
}
kernel_ptrs.flag_val = params.flag_val;
// Copy communication tracking pointers
kernel_ptrs.send_counters = params.send_counters;
kernel_ptrs.local_token_counter = params.local_token_counter;
kernel_ptrs.topk_target_ranks = params.topk_target_ranks;
kernel_ptrs.topk_send_indices = params.topk_send_indices;
kernel_ptrs.eplb_local_stats = params.eplb_local_stats;
// Copy active-rank bitmask into the kernel pointers struct
for (int w = 0; w < kRankMaskWords; ++w)
{
kernel_ptrs.active_rank_mask[w] = params.active_rank_mask[w];
}
int const kBlockSize = tensorrt_llm::common::getEnvMoeA2ADispatchBlockSize();
// One block per token: grid_size == local_num_tokens. If 0, launch a single block to
// keep the synchronization path alive.
int grid_size = params.local_num_tokens;
if (grid_size == 0)
{
grid_size = 1;
}
int shared_bytes = 2 * params.top_k * (int) sizeof(int);
SWITCH_BOOL(params.enable_eplb, EPLB_STATS, SWITCH_TOP_K(params.top_k, TOP_K, {
auto kernel_fn = moeA2ADispatchKernel<BlockPolicy, TOP_K, EPLB_STATS>;
launchWithPdlWhenEnabled("moeA2ADispatchKernel", kernel_fn, grid_size, kBlockSize, shared_bytes, params.stream,
params.token_selected_experts, kernel_ptrs, params.num_payloads, params.max_tokens_per_rank,
params.local_num_tokens, params.ep_rank, params.ep_size, params.num_experts, params.eplb_stats_num_experts);
}))
}
// ============================================================================
// Combine kernels
// ============================================================================
// Accumulate across all valid ranks into float32 registers, then store as T.
// InT: input element type in recv buffer (defaults to T for same-type accumulation).
// T: output element type written to dst.
//
// Unified path: load VEC_SIZE bytes, reinterpret as InT[elems_per_vec], accumulate as float32,
// store as T. Works for same-type (InT==T: half/bf16/float) and cross-type
// (e.g. InT=fp8_e4m3, T=bf16). sizeof(InT) must divide VEC_SIZE.
template <int VEC_SIZE, int TOP_K, typename ThreadingPolicy, typename T, typename InT = T>
__device__ void vectorized_combine_impl(T* dst_typed_base, int size_per_token, int stride_per_token, int rank_id,
int max_tokens_per_rank, CombineKernelPointers const& ptrs)
{
using flashinfer::vec_t;
// elems_per_vec: number of InT elements per VEC_SIZE-byte load (constexpr).
constexpr int elems_per_vec = VEC_SIZE / static_cast<int>(sizeof(InT));
int const stride = ThreadingPolicy::stride() * VEC_SIZE;
int const local_token_idx = ThreadingPolicy::token_idx();
// offset is a byte offset into the recv buffer, stepping by VEC_SIZE bytes.
for (int offset = ThreadingPolicy::offset() * VEC_SIZE; offset < size_per_token; offset += stride)
{
// Per-k vec_t<float, elems_per_vec> accumulators, zero-initialised via fill().
// Using vec_t enables cast_store() for the output, emitting a vectorized int4 write.
vec_t<float, elems_per_vec> acc[TOP_K];
// Pass 1: issue all TOP_K loads back-to-back without any type conversion.
// Raw InT bytes are loaded directly into acc[k]'s register storage, reinterpreted as
// vec_t<InT, elems_per_vec> (VEC_SIZE bytes, fitting in the low end of acc[k]'s
// sizeof(float)*elems_per_vec allocation). Separating load from cast lets the compiler
// schedule all VEC_SIZE-byte global loads consecutively, hiding memory latency across k.
#pragma unroll
for (int k = 0; k < TOP_K; ++k)
{
int target_rank = ptrs.topk_target_ranks[local_token_idx * TOP_K + k];
int dst_idx = ptrs.topk_send_indices[local_token_idx * TOP_K + k];
if (dst_idx < 0 || !is_rank_active(ptrs.active_rank_mask, target_rank))
{
acc[k].fill(0.0f);
continue;
}
uint8_t const* recv_buffer = static_cast<uint8_t const*>(ptrs.recv_buffers[target_rank][0]);
size_t base_source_rank = static_cast<size_t>(rank_id) * static_cast<size_t>(max_tokens_per_rank)
+ static_cast<size_t>(dst_idx);
// stride_per_token: byte distance between tokens in the recv buffer.
// Equals size_per_token for normal cases; may differ for FP8 in-place
// (BF16-stride workspace but FP8-sized payload).
size_t base_token = base_source_rank * static_cast<size_t>(stride_per_token);
reinterpret_cast<vec_t<InT, elems_per_vec>&>(acc[k]).load(
reinterpret_cast<InT const*>(recv_buffer + base_token + offset));
}
// Pass 2: in-place cast InT → float, iterating j in descending order.
// float[j] occupies bytes [j*4, j*4+3]; InT[j] occupies [j*sizeof(InT), ...).
// For sizeof(InT) < sizeof(float), high-j float writes land above all remaining
// InT bytes, so descending order is always write-after-read safe.
#pragma unroll
for (int k = 0; k < TOP_K; ++k)
{
int target_rank = ptrs.topk_target_ranks[local_token_idx * TOP_K + k];
int dst_idx = ptrs.topk_send_indices[local_token_idx * TOP_K + k];
if (dst_idx < 0 || !is_rank_active(ptrs.active_rank_mask, target_rank))
{
continue; // acc[k] already holds 0.0f from fill() above
}
#pragma unroll
for (int j = elems_per_vec - 1; j >= 0; --j)
acc[k][j] = static_cast<float>(reinterpret_cast<InT const*>(&acc[k])[j]);
}
// Reduce acc[TOP_K] into acc[0] via unrolled tree-reduction.
// acc[k][j] uses vec_t::operator[] which returns float& — no indirection overhead.
if constexpr (TOP_K == 22)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
acc[2][j] += acc[3][j];
acc[4][j] += acc[5][j];
acc[6][j] += acc[7][j];
acc[8][j] += acc[9][j];
acc[10][j] += acc[11][j];
acc[12][j] += acc[13][j];
acc[14][j] += acc[15][j];
acc[16][j] += acc[17][j];
acc[18][j] += acc[19][j];
acc[20][j] += acc[21][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[2][j];
acc[4][j] += acc[6][j];
acc[8][j] += acc[10][j];
acc[12][j] += acc[14][j];
acc[16][j] += acc[18][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[4][j];
acc[8][j] += acc[12][j];
acc[16][j] += acc[20][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[8][j];
acc[0][j] += acc[16][j];
}
}
else if constexpr (TOP_K == 16)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
acc[2][j] += acc[3][j];
acc[4][j] += acc[5][j];
acc[6][j] += acc[7][j];
acc[8][j] += acc[9][j];
acc[10][j] += acc[11][j];
acc[12][j] += acc[13][j];
acc[14][j] += acc[15][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[2][j];
acc[4][j] += acc[6][j];
acc[8][j] += acc[10][j];
acc[12][j] += acc[14][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[4][j];
acc[8][j] += acc[12][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[8][j];
}
}
else if constexpr (TOP_K == 10)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
acc[2][j] += acc[3][j];
acc[4][j] += acc[5][j];
acc[6][j] += acc[7][j];
acc[8][j] += acc[9][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[2][j];
acc[4][j] += acc[6][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[4][j];
acc[0][j] += acc[8][j];
}
}
else if constexpr (TOP_K == 8)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
acc[2][j] += acc[3][j];
acc[4][j] += acc[5][j];
acc[6][j] += acc[7][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[2][j];
acc[4][j] += acc[6][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[4][j];
}
}
else if constexpr (TOP_K == 6)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
acc[2][j] += acc[3][j];
acc[4][j] += acc[5][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[2][j];
acc[0][j] += acc[4][j];
}
}
else if constexpr (TOP_K == 4)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
acc[2][j] += acc[3][j];
}
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[2][j];
}
}
else if constexpr (TOP_K == 2)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[1][j];
}
}
else if constexpr (TOP_K == 1)
{
// nothing to do
}
else
{
// Generic fallback: accumulate all into acc[0]
#pragma unroll
for (int k = 1; k < TOP_K; ++k)
{
#pragma unroll
for (int j = 0; j < elems_per_vec; ++j)
{
acc[0][j] += acc[k][j];
}
}
}
// cast_store: converts float→T element-by-element then writes via vectorized int4 store.
acc[0].cast_store(dst_typed_base + offset / static_cast<int>(sizeof(InT)));
}
}
// Wrapper that selects vector width based on size_per_token alignment.
// stride_per_token: byte distance between tokens in the recv buffer (may differ from
// size_per_token when FP8 in-place uses BF16-stride workspace with FP8-sized payload).
// InT: input element type in recv buffer (defaults to T for same-type accumulation)
template <int TOP_K, typename ThreadingPolicy, typename T, typename InT = T>
__device__ void vectorized_combine(T* dst_typed_base, int size_per_token, int stride_per_token, int rank_id,
int max_tokens_per_rank, CombineKernelPointers const& ptrs)
{
// Each branch is guarded by if constexpr (sizeof(InT) <= VEC_SIZE) so that the compiler
// never instantiates vectorized_combine_impl with elems_per_vec=0.
// Branches where VEC_SIZE < sizeof(InT) are unreachable at runtime because size_per_token
// is always a multiple of sizeof(InT), so a larger alignment branch is taken first.
if (size_per_token % 16 == 0)
{
if constexpr (static_cast<int>(sizeof(InT)) <= 16)
vectorized_combine_impl<16, TOP_K, ThreadingPolicy, T, InT>(
dst_typed_base, size_per_token, stride_per_token, rank_id, max_tokens_per_rank, ptrs);
}
else if (size_per_token % 8 == 0)
{
if constexpr (static_cast<int>(sizeof(InT)) <= 8)
vectorized_combine_impl<8, TOP_K, ThreadingPolicy, T, InT>(
dst_typed_base, size_per_token, stride_per_token, rank_id, max_tokens_per_rank, ptrs);
}
else if (size_per_token % 4 == 0)
{
if constexpr (static_cast<int>(sizeof(InT)) <= 4)
vectorized_combine_impl<4, TOP_K, ThreadingPolicy, T, InT>(
dst_typed_base, size_per_token, stride_per_token, rank_id, max_tokens_per_rank, ptrs);
}
else if (size_per_token % 2 == 0)
{
if constexpr (static_cast<int>(sizeof(InT)) <= 2)
vectorized_combine_impl<2, TOP_K, ThreadingPolicy, T, InT>(
dst_typed_base, size_per_token, stride_per_token, rank_id, max_tokens_per_rank, ptrs);
}
else
{
if constexpr (static_cast<int>(sizeof(InT)) <= 1)