# mHC post/pre, RMSNorm and FP8 cast (`mega_mhc`) | ServingStudio Kernel Library

Mix a sub-layer's output into the mHC residual streams, compute the next sub-layer's mixes and RMS-normalized input, and cast it to FP8, in one kernel.

DeepInfer runs every sub-layer entry that follows another sub-layer as one persistent DeepGEMM mega_mhc launch: the previous sub-layer's post mix into the residual streams, the next pre's TF32 GEMM and RMS statistic split over K, the sigmoid gates and Sinkhorn comb, the collapse with the pre mix carried from the previous sub-layer (shifted mHC), the RMSNorm, and the FP8 E4M3 cast with UE8M0 scales per 32 channels. It writes the BF16 and the FP8 layer input.

Category: Normalization; group: Hyper-connections. Interactive page: https://servingstudio.cs.washington.edu/kernels.html?kind=mega_mhc. All kernel kinds: https://servingstudio.cs.washington.edu/kernels.md.

## Formula

- `streamⱼ ← Σᵢ combᵢⱼ · streamᵢ + postⱼ · x`
- `mixes = RMS-scaled (flatten(streams) @ fnᵀ); y = RMSNorm(Σₕ prev_preₕ · streamₕ)`
- `y_fp8, scales = cast_e4m3(y, 32 channels per UE8M0 scale)`

## How it is measured

GPU kernel time from CUPTI activity records, averaged over repeated launches in one capture. The L2 cache is flushed before each launch, so the numbers are cold-cache times. One launch per call. The outputs are checked against DeepInfer's unfused shifted path (TileKernels post, apply-mix, split-mixes, Sinkhorn; DeepGEMM prenorm GEMM) on the same operands before timing.

## Caveats

- DeepGEMM picks the K-split count from num_tokens and B200's 148 SMs; the time steps where it changes and where the 16-split work spills into another wave.
- For sf_layout='mega_moe' the shared-scale tile height is DeepInfer's get_block_m_for_mega_moe for DP4/EP4, 416 slots, top-6, fp8 x fp4.
- TFLOPS counts the prenorm GEMM; GB/s one pass over the call's tensors.

## Arguments

| Argument | Type | Unit | Meaning |
| --- | --- | --- | --- |
| `num_tokens` | number | tokens | Rows entering the sub-layer on this GPU. |
| `hidden_size` | number | elements | Width of each residual stream. |
| `hc_mult` | number | streams | Residual streams per token (mHC expansion). |
| `hidden_dtype` | dtype |  | Element type of the streams and the layer output (bf16). |
| `sf_layout` | label |  | Consumer of the FP8 output: 'gemm' (attention entry, column-major scales for the MXFP8 GEMM) or 'mega_moe' (FFN entry, mega MoE buffer scales plus the shared expert's tiled copy). |

## Metrics

- Time (`time_ms`, ms)
- Throughput (`tflops`, TFLOPS)
- Memory bandwidth (`memory_bandwidth_gbps`, GB/s)
- Energy (`energy_j`, J)

## Backends

- `deepgemm_sm100`: DeepGEMM mega_mhc as DeepInfer's _mega_mhc calls it: shifted, BF16 and FP8 outputs, scales in the attention GEMM or mega MoE layout. dtypes `bf16`, SM targets `sm_100f`, GPUs B200-SXM-180GB, B300-SXM-288GB, GB200-Superchip. Source: https://github.com/deepseek-ai/DeepGEMM

## Measured rows

188 rows in all.

| GPU | Backend | Precision | Rows |
| --- | --- | --- | --- |
| NVIDIA B200 | `deepgemm_sm100` | `bf16` | 188 |

## Used by these deployments

- [DeepSeek V4.1 Flash 1017: vLLM or DeepInfer, DP attention, EP MoE](https://servingstudio.cs.washington.edu/models/DeepSeek-V4.1-Flash-1017/deepseek_v41_vllm_dp_attn_ep_moe.md) (`DeepSeek-V4.1-Flash-1017/deepseek_v41_vllm_dp_attn_ep_moe`)
- [DeepSeek V4.1 Flash 1017: vLLM, DP attention, EP MoE, DSpark speculative decoding](https://servingstudio.cs.washington.edu/models/DeepSeek-V4.1-Flash-1017/deepseek_v41_vllm_dp_attn_ep_moe_dspark.md) (`DeepSeek-V4.1-Flash-1017/deepseek_v41_vllm_dp_attn_ep_moe_dspark`)

## API

- This kind: https://servingstudio.cs.washington.edu/api/public/v1/kernels/mega_mhc
- Measured rows: https://servingstudio.cs.washington.edu/api/public/v1/kernels/mega_mhc/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/mega_mhc/configs

---

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