Streaming Signal Contract for the MoE Grouped GEMMs
Experimental. This page is for whoever implements the kernel that feeds a grouped GEMM (for example an expert-parallel dispatch) or consumes its output (for example an expert-parallel combine), so that the two run concurrently instead of one after the other. The kernels stay separate. They share only a few int32 entries in device memory: the readiness signals.
Kernels: MoEGroupedGemmBf16Kernel (unfused/, both directions) and MoEGroupedGemmGluBiasBf16Kernel (glu/,
input direction only), BF16 on SM100-class GPUs. Both directions need use_dynamic_sched=True. Every option is off by
default; with the defaults the generated code is the same as without this feature.
1. Input direction: producer → GEMM
Workspace layout
The kernel’s workspace (get_workspace_bytes()) holds the signal entries. Hand signal[] to the producer as a view
of that workspace (zero-copy). Offsets come from moe_kernel_helpers.streaming_signal_workspace_offsets(kernel).
desc = kernel.get_desc_workspace_bytes(): 0 for dense weights, TMA descriptors for discrete weights.S = streaming_scheduler_state_offset(capacity) = round_up(4 + 4 * capacity, 128), so the producer-written signals and the GEMM’s own state never share a 128-byte line.- State size:
4 * (2 * E + 1 + 3 * capacity)bytes, where E is the local expert count: two int32 entries per expert, one claim count and a 3-entry batch record per M tile.
signal[t]
tranges over[0, padded_offsets[E - 1] / M), at moststreaming_signal_capacityM tiles. Later entries stay -1 and the GEMM never waits on them.- The position is the M tile id. The producer publishes no arrival order; the GEMM picks its own order, running ready M tiles in expert-consecutive batches of up to 16 M tiles.
- Readiness is
signal[t] >= 0. Reset with0xFFbytes (-1), never with zeros: 0 reads as “M tile 0 ready” and everyt >= 0as ready.
M tile size and capacity
Memory ordering (producer side)
- Write all rows of M tile
t, padding included. - If they were written with TMA /
cp.async.bulk, wait for completion withcp.async.bulk.wait_group 0(not the.readvariant, which only frees the source). - Make every writer’s stores happen-before the publishing thread (
__syncthreads(); across blocks, a per-M-tileacq_relcounter whose last arrival publishes). - Store
ttosignal[t]with a gpu-scope release (cuda::atomic_ref<int, cuda::thread_scope_device>::store(t, memory_order_release)).
The GEMM does a gpu-scope acquire load of signal[t] and then a fence.proxy.async.global before its TMA loads, so the
producer does not need a proxy fence of its own.
Reset and termination
- Who resets: the caller, before every launch, ordered before both the producer and the GEMM. Use
moe_kernel_helpers.reset_streaming_signal_workspace(workspace, kernel): counter and state 0,signal[]-1. Withall_complete=Trueit setssignal[t] = t, which runs the GEMM with no producer (tests, warm-up). Withuse_pdlthe reset must not be the GEMM’s immediate predecessor on its stream, since the GEMM waits for that launch only at exit (section 4). - No GEMM-side reset: the GEMM never resets the signal workspace, and on this path its helper kernel does not reset the claim counter either.
- Unbounded wait: waits have no timeout; a bounded wait could return with the M tile not ready. A cluster stops once
its claim index passes the last tile (every M tile times
ceil(N / cluster tile N)), before it waits on anything. - Two GEMM launches on one counter: a base kernel and a second kernel launched as its PDL dependent can share one workspace and claim from the same counter, and each tile is still computed once. Reset that workspace once, before the base.
Liveness
- The producer must be able to run while the GEMM waits. Give the GEMM fewer clusters than the device holds
(
max_active_clusters), or launch it as the producer’s programmatic dependent (section 4). Either way, the producer keeps the SMs it needs. - The producer must not wait on the GEMM.
2. Device layout (device_layout=True)
Here the producer also writes the per-expert offsets while the GEMM is already running, so the host never reads the routing.
- The GEMM reads
layout[0], one entry beforepadded_offsets. A standalonepadded_offsetstensor therefore reads out of bounds. - With
device_layout, no CTA exits early on a host-side total of 0, and every warp role runs in every CTA. A total of 0 ends the first claim in every CTA of the cluster.
3. Output direction: GEMM → consumer (N_CHUNK)
StreamingOutputNotifyConfig(mode=StreamingOutputNotifyMode.N_CHUNK, n_chunk_size=C, n_chunk_capacity, caller_reset=False, clusters_beside_consumer)
signal[u]
- Location: the last
4 × n_chunk_capacitybytes of the GEMM workspace. Get it fromstreaming_output_notify_offsets(kernel)["signal"]and hand it to the consumer zero-copy. signal[]is int32, one entry per N chunkuin[0, ceil(N / C)). Chunkuis D columns[u × C, min((u + 1) × C, N))of every padded row.
- A tile counts only after all its D stores have reached global memory:
cp.async.bulk.wait_group 0, then oneacq_relgpu-scope add per finished run of tiles. - The add that completes the chunk is followed by a gpu-scope release store of
signal[u] = u. - The consumer waits for exactly
u, with a gpu-scope acquire load, then reads the chunk with coherent loads. An in-progress value is always negative, so it is never mistaken for a publication. - The GEMM claims tiles N chunk → every M tile → the chunk’s N tiles, so chunks complete roughly in order. The consumer
still waits on each
signal[u].
Sizes
Reset
- Default: the GEMM call enqueues a one-block kernel ahead of itself that sets
signal[] = -1. Withuse_pdl=Truethat reset is the kernel the GEMM depends on, so every GEMM thread runsgriddepcontrol.waitat entry, before any count lands onsignal[]and before it lets a dependent consumer launch. The consumer therefore never sees a previous launch’ssignal[u] == u. caller_reset=True: the caller runsstreaming_output_notify.reset(workspace_ptr, kernel.get_workspace_bytes(), stream)before each GEMM, on the GEMM’s stream. The caller then owns the ordering: the reset’s stores must be visible before the GEMM starts counting, which holds when the reset completes before the GEMM launches. The constructor rejectsuse_pdl=Truehere (see §4). The consumer is still the GEMM’s programmatic dependent (see below).
Extra clusters and liveness
- The GEMM launches on every cluster of the device.
clusters_beside_consumer(≥ 1) is how many clusters fit beside the consumer’s SMs; clusters at or past that rank are extras. - An extra stops claiming once its last claim is at or past
max(chunk 0 tiles − grid clusters, 0). That frees its SMs at about the end of chunk 0. Every tile is still claimed exactly once. - The consumer must be the GEMM’s programmatic dependent on the same stream. Build the GEMM with
launch_pdl_dependents_at_entry=Trueand launch the consumer with PDL. Otherwise the consumer can take SMs the persistent GEMM still needs, and both hang. - A GEMM with no rows (
padded_offsets[E-1] == 0) exits before publishing anything. The consumer must not wait onsignal[]when it received no rows.
4. Programmatic dependent launch (PDL) options
5. Sketches
Producer of signal[] (CUDA C++; one block per M tile):
Consumer of an N_CHUNK GEMM (launched as the GEMM’s PDL dependent):
Single-GPU self-test
Pattern from test/python/gemm/cutedsl/test_grouped_gemm_streaming_signal.py:
-
Same bits: compare against the stock dynamic scheduler, bit for bit. Call
reset_streaming_signal_workspace(..., all_complete=True)so every M tile is ready, and compareCandDwith a launch built withuse_dynamic_sched=Trueonly. -
Early reads are caught: fill A with NaN and every
signal[t]with -1. Enqueue the producer (a sleep, then per M tile: copy the rows, then setsignal[t] = t) on a side stream, and launch the GEMM last with few clusters. Any early read puts NaN in D. -
Module-loading deadlock: with lazy module loading, the first launch of a kernel waits for running kernels to finish. A producer launched after a GEMM that is already spinning therefore deadlocks. To avoid it:
- run the GEMM once first, which loads its module;
- enqueue every producer launch before the GEMM’s launch.
CUDA_MODULE_LOADING=EAGERalso avoids it. -
N_CHUNK: after the GEMM,signal[u] == ufor everyu < ceil(N / C), and D matches the stock run bit for bit.
6. Constraints
- Cluster shapes: the input direction (
streaming_signal_capacity) supports only (2, 1) with the 2-CTA MMA; a (1, 1) cluster is not supported (the scheduler broadcasts each claim into a peer CTA’s shared memory and asserts at least two CTAs).N_CHUNKsupports (2, 1) with the 2-CTA MMA and (1, 1) with the 1-CTA MMA. Other shapes raiseValueError, because a CTA could exit while a peer still arrives on its shared memory. streaming_signal_capacityandN_CHUNKcannot be combined in one kernel; both needuse_dynamic_sched=True.- N must be known at build time (dense B).
device_layoutrequiresstreaming_signal_capacity.- Covered today: the BF16 plain and GLU grouped GEMM forward kernels. Other recipes are not wired up yet.
7. BF16 wgrad, input direction
The SM100-class BF16 wgrad kernel MoEGroupedGemmWgradBF16Kernel (wgrad/) reads a producer’s signal from a caller’s
vector, not from its own workspace: streaming_signal_rows=R at construction and streaming_signal at the call
(see grouped_gemm_wgrad.md).
Acknowledgements
The streaming signal contract and its schedulers were designed and developed by Wentao Guo, Phuong Nguyen, Zhongbo Zhu, Subhadeep Bhattacharya, Fei Wu, and Jimmy Zhang.