File size: 6,324 Bytes
bdebe19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Host interface for the MoE expert megakernel (see kernel.cu for the algorithm).
#pragma once
// MOE_SPLIT0: large-T (MIX) path expert split point -- batch 0 (side stream, gated on the Hs rows of
// experts [0, MOE_SPLIT0)) / batch 1 (experts [MOE_SPLIT0, 256), after the gate|up kernel).  Multiple of 8.
#ifndef MOE_SPLIT0
#define MOE_SPLIT0 160
#endif
// T6144 band (gu_only NCH >= 240) split point
#ifndef MOE_SPLIT0_W
#define MOE_SPLIT0_W 192
#endif
#include <cuda_runtime.h>
#include <cuda_bf16.h>
#include <cstdint>

struct DnFill {
  int on;                          // 0: the fill kernel writes the args (old path)
  int bn0, split0;                 // class-0 token tile (class 1 = bn0 < cnt <= bn1), expert batch split
  int bn1;                         // MOE_DNOVF0 class-1 cap (overflow experts -> class 0)
  int szA, szB, szD;               // stride template sizes in bytes (multiple of 4, <= 32)
  unsigned char* shapes;           // [1024] x 12 B
  void** pA; void** pB; void** pD; // [1024]
  unsigned char* sA; unsigned char* sB; unsigned char* sD;
  int* grow0;                      // [1024]
  const __nv_bfloat16* dw;         // down weights
  const __nv_bfloat16* h;          // Hs base
  __nv_bfloat16* xs;               // Dbuf base (pD, unused by the fused epilogue)
  unsigned int tA[8], tB[8], tD[8];
};
struct MoeArgs {
  int T;                          // num_tokens
  int nchunk;                     // token chunk size per work item (128/192/256)
  int lag;                        // queue lag (in chunks) between gate|up and down items of a chunk
  int wpromo = 2;                 // weight tensor-map L2 promotion: 1 128B, 2 256B
  int xpromo = 0;                 // token gather4 map L2 promotion: 0 none, 1 128B
  int xwin = 1;                   // hidden_states persisting-L2 access window on/off
  int xpol = 0;                   // token gather4 L2 hint: 0 evict_last, 1 none (window governs), 2 evict_normal
  int opol = 0;                   // full path: out zero-store + down red.add L2 policy (0 evict_last, 1 normal, 2 first)
  int dil = 0;                    // full path: down-weight interleave (0: 32-row halves of 64-row groups, 1: 16 of 32)
  int zpol = 2;                   // gu_only: out zero-store L2 policy (same codes)
  int cpx = 0;                    // 1: cp.async token-loader warp instead of TMA gather4 for tokens
  int ring = 0;                   // smem ring split: 0 default WS/XS, 1 alternative (fewer token slots)
  int h64 = 2;                    // down items Hs boxes: 0 32-row units, 1 merged 64/96/128-row units, 2 exact half-row box
  int dq = 0;                     // 1: dynamic work queue (atomic claim + smem item ring), NCH=192 full path only
  int nsw = 0;                    // gu_only, NCH=256, cpx: non-swapped gate|up items (kernel.cu NSW)
  int r3off = 0;                  // ew4 NCH=128: 1 = 2 x 256-col TMEM double buffer instead of the 3-slot ring
  int pblk = 0;                   // 1: megakernel reads hidden/ids/ew/out from g_ptr_blk (MIX graph path)
  int ew4 = 0;                    // 1: 4 epilogue warps instead of 8 (small-T full-mega path, NCH=128)
  int grid_cap = 0;               // gu_only: max CTAs of the gate|up megakernel grid (0 = all co-resident)
  int gu_zero = 0;                // gu_only: 1 = the megakernel's epilogue warps zero out (no memset)
  int gu_only = 0;                // 1: queue holds only gate|up items (large-T path: Hs -> CUTLASS down GEMM)
  unsigned int epoch = 0;         // gu_only: call epoch (moe_next_epoch) for the batch-0 wait kernel
  int* cnt_out = nullptr;         // gu_only: [256] per-expert token counts (CTA 0, before the epoch publish)
  int* inv = nullptr;
  int2* meta = nullptr;            // full path: [T*8] {out row offset, bf16x2 weight} per sorted row             // gu_only: [T*8] expert-sorted row of each (token*8+k) assignment
  const __nv_bfloat16* hidden;    // [T, 7168]
  const long long* ids;           // [T, 8]
  const __nv_bfloat16* ew;        // [T, 8]
  const __nv_bfloat16* gate;      // [256, 2048, 7168]
  const __nv_bfloat16* up;        // [256, 2048, 7168]
  const __nv_bfloat16* down;      // [256, 7168, 2048]
  // workspace
  int4* item_info;   // [22*max_chunks] {expert, row0, n, chunk<<8 | is_gateup<<7 | mtile}
  int* done;         // [max_chunks] per-chunk gate|up completion counters (target 64)
  int* num_items;    // [1]
  int* sorted_slot;  // [T*8]  (token*8+k) of each expert-sorted row
  __nv_bfloat16* hs; // [T*8, 2048] SwiGLU intermediate (expert-sorted)
  __nv_bfloat16* out;// [T, 7168]
  DnFill dnf = {};   // MIX path: down-GEMM args written in-megakernel (MOE_MKFILL)
};

void moe_forward(const MoeArgs& a, cudaStream_t st);
// reset the persisting-L2 set-aside to 0 (called before the CUTLASS large-T path)
void moe_clear_persist(cudaStream_t st);
// large-T path: megakernel in gate|up-only mode (routing + fused gate|up GEMM + SwiGLU -> a.hs, a.inv);
// no persisting-L2 window, no out writes.  a.nchunk must be 256 (cp.async token loader variant).
void moe_gateup_forward(const MoeArgs& a, cudaStream_t st);
// large-T path: epoch counter for moe_batch_wait (one per call)
unsigned int moe_next_epoch();
// large-T path: on stream st, block until routing of call a.epoch is done and every gate|up chunk of
// experts [0,128) has written its Hs rows (the batch-0 down GEMM is launched after it on st)
void moe_batch_wait(const MoeArgs& a, cudaStream_t st);
// targets of the batch-0 wait (done[2c] target, zero-counter target) for the fused arg-fill kernel
void moe_batch_wait_targets(const MoeArgs& a, int* target, int* zero_target);

// large-T path: device address of the routing-epoch flag (polled by the down-GEMM arg-fill kernel)
unsigned int* moe_route_epoch_ptr();
// MOE_MKFILL: [0] routing epoch, [1] down-args epoch (moe_route_epoch_ptr() + 1); [2] MOE_LPTGATE batch-1 class-1 down started; all zeroed by the graph head memset (12 B)
// device address of the 4 x u64 per-call pointer block {hidden, ids, ew, out} (MOE_PTRBLK)
void* moe_ptr_blk_ptr();

// MIX-path CUDA graph hooks (kernel.cu launch_mega): kernelParams array of the last megakernel launch,
// dry-run flag (rebuild the argument pack without launching) and no-PDL flag (graph capture)
extern void** g_mega_args;
extern int g_mega_dry, g_mega_nopdl;