# Q/K norm, RoPE, fp8 query and KV cache write (`qk_norm_rope_fp8_kv_write`) | ServingStudio Kernel Library

Normalize and rotate queries and keys, quantize them to fp8 and write K/V into the paged cache.

Between a GQA layer's qkv projection and an fp8 attention call, one Triton launch normalizes each query and key head, applies NeoX RoPE, writes the queries as fp8 and scatters the rotated keys and the values as fp8 into the paged cache. The DFlash2 draft uses it in place of five launches.

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

## Formula

- `q' = fp8(RoPE(RMSNorm(q))), k' = fp8(RoPE(RMSNorm(k))), v' = fp8(v)`
- `GB/s = num_tokens · head_dim · ((Hq + 2 Hkv) · 2 + Hq + 2 Hkv) / 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, the query and cache fp8 codes are compared with the incumbent sequence (native norm/RoPE/quant plus vLLM reshape_and_cache_flash): at least 90% of codes must match.

## Caveats

- Not bit-exact with the incumbent: about 95% of fp8 codes match, the rest differ by bf16 rounding of small values.
- Scales are 1.0 (the draft is unquantized).

## Arguments

| Argument | Type | Unit | Meaning |
| --- | --- | --- | --- |
| `num_tokens` | number | tokens | Rows of the packed qkv projection output. |
| `num_qo_heads` | number | heads | Query heads per row. |
| `num_kv_heads` | number | heads | Key and value heads per row. |
| `head_dim` | number | elements | Elements per head. |
| `page_size` | number | tokens | Tokens per KV-cache page. |
| `input_dtype` | dtype |  | Element type of the qkv input. |
| `kv_dtype` | dtype |  | Element type of the query output and the KV cache. |

## Metrics

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

## Backends

- `triton`: In-repo qk_norm_rope_fp8 Triton kernel (runners/dflash2/draft_chain_vendor.py). dtypes `bf16`, KV dtypes `fp8_e4m3`, any CUDA GPU.

## Measured rows

18 rows in all.

| GPU | Backend | Precision | Rows |
| --- | --- | --- | --- |
| NVIDIA B200 | `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/qk_norm_rope_fp8_kv_write
- Measured rows: https://servingstudio.cs.washington.edu/api/public/v1/kernels/qk_norm_rope_fp8_kv_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/qk_norm_rope_fp8_kv_write/configs

---

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