This document is relevant for: Trn2, Trn3
Permute A2AV Kernel API Reference#
Permute tokens by destination EP rank and dispatch via all-to-all-v.
Gathers tokens per destination EP rank into a packed send buffer at cumsum(send_counts) row offsets, builds the (4, EP) uint32 metadata tensor, then exchanges via ncc.all_to_all_v. The receive-side layout is determined by the runtime (packed cumsum of recv_counts).
Background#
The permute_a2av kernel is the MoE training dispatch step: it permutes tokens by their destination expert-parallel (EP) rank and exchanges them across ranks via ncc.all_to_all_v.
API Reference#
Source code for this kernel API can be found at: permute_a2av.py
permute_a2av#
- nkilib.experimental.collectives.a2av_train.permute_a2av(hidden_states: nl.NkiTensor, packed_source_indices: nl.NkiTensor, send_counts: nl.NkiTensor, recv_capacity: int, replica_group: ReplicaGroup, graph_owned_buffers: bool = False, zero_tail: bool = True, given_regroup_indices: nl.NkiTensor = None) tuple[nl.NkiTensor, nl.NkiTensor, nl.NkiTensor, nl.NkiTensor, nl.NkiTensor]#
Permute tokens by destination EP rank and dispatch via all-to-all-v.
- Parameters:
hidden_states (
nl.NkiTensor) – [N, H]@HBM routed input rows.packed_source_indices (
nl.NkiTensor) – [N]@HBM, int32 permutation of source rows in destination-major order. Every entry must be in [0, N), with peer segments defined by send_counts.send_counts (
nl.NkiTensor) – [1, EP]@HBM, int32/uint32 row counts.recv_capacity (
int) – Static number of rows in the packed receive output. For top-k routing with unique expert IDs andLlocal experts per destination rank, a dropless bound isglobal_token_count * min(top_k, L). This reduces toglobal_token_countwhenL == 1. Duplicate expert IDs within one token invalidate this bound.replica_group (
ReplicaGroup) – EP replica group.graph_owned_buffers (
bool) – True receives directly into an aligned graph-owned output for NLE == 1. False retains eager staging; unaligned capacities and NLE > 1 retain their copy or gather.zero_tail (
bool) – Zero the rows ofrecv_datapast the receive count. Callers that never read those rows can pass False to skip zeroing the capacity; the rows past the count in the last partial 128-row tile, and in a few more tiles up to the copy loop’s bound (none for an aligned graph-owned output, which skips the copy), are still written with zeros, and the rest are undefined.given_regroup_indices (
nl.NkiTensor) – int32[recv_capacity]permutation returned asregroup_indicesby an earlier call with the same send counts on every rank, such as the forward dispatch of the same routing. WithNLE > 1it replaces building the permutation; the returnedregroup_indicesandbwmm_countsare then int32[1]placeholders.
- Returns:
[C, H]@HBM. Source-major packed rows followed by deterministic zero padding.
- Returns:
[4, EP]@HBM, uint32. Send counts/displacements and receive counts/displacements, all measured in elements.
- Return type:
tuple[nl.NkiTensor, nl.NkiTensor]
Dimensions:
T: number of local tokens (SP sharded).
H: hidden dimension.
EP: number of expert-parallel ranks (== replica_group size).
This document is relevant for: Trn2, Trn3