# Multi-layer K norm, RoPE and KV cache write (`layered_kv_norm_rope_cache_write`) | ServingStudio Kernel Library

Normalize, rotate and scatter every layer's context K/V into its paged cache in one launch.

The DFlash2 drafter turns the target's hidden states into all of its layers' K/V with one GEMM; this one Triton launch (the vLLM fork's Beam store_context_kv) finishes them: per-layer K RMSNorm, NeoX RoPE, and the fp8 write of K and V into each layer's cache.

Category: Attention; group: MHA / GQA. Interactive page: https://servingstudio.cs.washington.edu/kernels.html?kind=layered_kv_norm_rope_cache_write. All kernel kinds: https://servingstudio.cs.washington.edu/kernels.md.

## Formula

- `GB/s = num_tokens · num_layers · 2 · Hkv · head_dim · (in bytes + cache bytes) / time`

## 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. Before timing, every layer's cache bytes are compared with the incumbent sequence (transpose, vLLM rms_norm, rotary_embedding, reshape_and_cache_flash per layer): at least 99.9% must match.

## Caveats

- Scales are 1.0 (the draft is unquantized).

## Arguments

| Argument | Type | Unit | Meaning |
| --- | --- | --- | --- |
| `num_tokens` | number | tokens | Context rows whose K/V are written. |
| `num_layers` | number | layers | Layers whose caches receive the rows. |
| `num_kv_heads` | number | heads | Key and value heads per layer. |
| `head_dim` | number | elements | Elements per head. |
| `page_size` | number | tokens | Tokens per KV-cache page. |
| `input_dtype` | dtype |  | Element type of the K/V projection. |
| `kv_dtype` | dtype |  | Element type of the KV cache. |

## Metrics

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

## Backends

- `vllm_triton`: vLLM fork vllm.models.beam.nvidia.dflash2_context_ops.store_context_kv. dtypes `bf16`, KV dtypes `fp8_e4m3`, any CUDA GPU.

## Measured rows

18 rows in all.

| GPU | Backend | Precision | Rows |
| --- | --- | --- | --- |
| NVIDIA B200 | `vllm_triton` | `bf16` | 18 |

## Used by these deployments

- [GLM-5.3-Flash NVFP4: ServingStudio design: DP attention, KDA TP groups, fused EP MoE, DFlash speculative decoding](https://servingstudio.cs.washington.edu/models/GLM-5.3-Flash-NVFP4/glm53_flash_nvfp4_dp_attn_kda_tp_fused_ep_moe_dflash2.md) (`GLM-5.3-Flash-NVFP4/glm53_flash_nvfp4_dp_attn_kda_tp_fused_ep_moe_dflash2`)

## API

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

---

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