# MoE all-gather that quantizes on the way out (`moe_ep_quantize_and_all_gather`) | ServingStudio Kernel Library

Quantize each GPU's bf16 activations to NVFP4 and gather them, with the top-k routing, to every data-parallel GPU in one launch.

The same exchange as moe_ep_quantized_all_gather, but it starts from the bf16 hidden states: the kernel computes the NVFP4 values and e4m3 block scales (one per 16 elements, linear layout) and writes them, with the fp32 top-k weights and int32 expert IDs, straight into every GPU's landing rows. It replaces a separate NVFP4 quantization kernel plus the gather. GPU r contributes per_rank_tokens\[r\] tokens; every GPU ends with all of them in rank order.

Category: Communication. Interactive page: https://servingstudio.cs.washington.edu/kernels.html?kind=moe_ep_quantize_and_all_gather. All kernel kinds: https://servingstudio.cs.washington.edu/kernels.md.

## Formula

- `token bytes = hidden_size/2 + hidden_size/16 + 8·top_k`
- `total bytes = sum(per_rank_tokens) · token bytes`
- `algbw = (total bytes / num_gpus) / time`
- `busbw = (total bytes − min(per_rank_tokens) · token bytes) / time`

## How it is measured

N calls captured in one CUDA graph and replayed 20 times after 20 warm-up calls; CUDA-event time per call; the slowest rank is kept. Every shape is first checked bit for bit against vLLM's own NVFP4 quantization (moe_kernel_quantize_input) of the same inputs.

## Caveats

- The scales are produced in the linear layout; the swizzle the grouped GEMM needs happens after the gather and is not part of this measurement.

## Arguments

| Argument | Type | Unit | Meaning |
| --- | --- | --- | --- |
| `num_gpus` | number | GPUs | GPUs in the data-parallel group. |
| `per_rank_tokens` | list | tokens | Token counts for each GPU, in rank order. |
| `hidden_size` | number | elements | Elements in each token's hidden state. |
| `top_k` | number | experts | Experts selected per token. |
| `activation_dtype` | dtype |  | Element type of the gathered activations; nvfp4_e2m1 is measured. |
| `fabric` | label |  | Interconnect label; this backend requires nvlink. |

## Metrics

- Time (`time_ms`, ms)
- Algorithm bandwidth (`algbw_gbps`, GB/s)
- Bus bandwidth (`busbw_gbps`, GB/s)
- Energy (`energy_j`, J)

## Backends

- `mnnvl_lamport`: One kernel launch: each thread quantizes 16-byte vectors of its rows (TRT-LLM's NVFP4 math) and stores them directly into every peer's landing rows in fabric (MNNVL) memory; Lamport-style arrival, no separate barrier. dtypes `nvfp4_e2m1`, any CUDA GPU. Source: https://github.com/flashinfer-ai/flashinfer/blob/main/include/flashinfer/comm/moe_alltoall_lamport.cuh

## Measured rows

None measured yet.

## API

- This kind: https://servingstudio.cs.washington.edu/api/public/v1/kernels/moe_ep_quantize_and_all_gather
- Measured rows: https://servingstudio.cs.washington.edu/api/public/v1/kernels/moe_ep_quantize_and_all_gather/rows (filter by any column, such as `?gpu=NVIDIA%20H200`; `format=csv` for CSV)
- Configs the public deployments ask of it: https://servingstudio.cs.washington.edu/api/public/v1/kernels/moe_ep_quantize_and_all_gather/configs

---

This page as Markdown: https://servingstudio.cs.washington.edu/kernels/moe_ep_quantize_and_all_gather.md. A guide to the whole site for AI agents: [llms.txt](https://servingstudio.cs.washington.edu/llms.txt).
