kv4-hopper 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kv4_hopper-0.1.0/LICENSE +55 -0
- kv4_hopper-0.1.0/MANIFEST.in +8 -0
- kv4_hopper-0.1.0/NOTICE +9 -0
- kv4_hopper-0.1.0/PKG-INFO +180 -0
- kv4_hopper-0.1.0/README_pypi.md +154 -0
- kv4_hopper-0.1.0/integrations/vllm_plugin/kv4_vllm/__init__.py +263 -0
- kv4_hopper-0.1.0/integrations/vllm_plugin/kv4_vllm/backend.py +436 -0
- kv4_hopper-0.1.0/integrations/vllm_plugin/kv4_vllm/calib.py +113 -0
- kv4_hopper-0.1.0/integrations/vllm_plugin/kv4_vllm/calibrate.py +104 -0
- kv4_hopper-0.1.0/integrations/vllm_plugin/kv4_vllm/check.py +123 -0
- kv4_hopper-0.1.0/integrations/vllm_plugin/kv4_vllm/compare.py +109 -0
- kv4_hopper-0.1.0/kv4_hopper/__init__.py +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/DeepSeek-R1-Distill-Llama-8B.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/DeepSeek-R1-Distill-Qwen-7B.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/Llama-3.1-8B.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/Qwen2.5-72B-Instruct.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/Qwen2.5-7B-Instruct.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/Qwen3-32B.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/__init__.py +1 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/manifest.json +12 -0
- kv4_hopper-0.1.0/kv4_hopper/calib/phi-4.pt +0 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/__init__.py +0 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/_build.py +50 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/common.cuh +127 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/decode_fp4.cu +852 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/decode_fp4.py +302 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/decode_fp4_k8.cu +1078 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/decode_fp4_k8.py +324 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/merge.cu +597 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/merge.py +53 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/store_fp4.cu +625 -0
- kv4_hopper-0.1.0/kv4_hopper/cuda/store_fp4.py +253 -0
- kv4_hopper-0.1.0/kv4_hopper/formats.py +296 -0
- kv4_hopper-0.1.0/kv4_hopper/reference.py +32 -0
- kv4_hopper-0.1.0/kv4_hopper.egg-info/PKG-INFO +180 -0
- kv4_hopper-0.1.0/kv4_hopper.egg-info/SOURCES.txt +41 -0
- kv4_hopper-0.1.0/kv4_hopper.egg-info/dependency_links.txt +1 -0
- kv4_hopper-0.1.0/kv4_hopper.egg-info/entry_points.txt +6 -0
- kv4_hopper-0.1.0/kv4_hopper.egg-info/requires.txt +5 -0
- kv4_hopper-0.1.0/kv4_hopper.egg-info/top_level.txt +2 -0
- kv4_hopper-0.1.0/pyproject.toml +55 -0
- kv4_hopper-0.1.0/setup.cfg +4 -0
- kv4_hopper-0.1.0/setup.py +45 -0
kv4_hopper-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
10
|
+
|
|
11
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
12
|
+
|
|
13
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
14
|
+
|
|
15
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
16
|
+
|
|
17
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
18
|
+
|
|
19
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
20
|
+
|
|
21
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
22
|
+
|
|
23
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
24
|
+
|
|
25
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
26
|
+
|
|
27
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
28
|
+
|
|
29
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
30
|
+
|
|
31
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
32
|
+
|
|
33
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
34
|
+
|
|
35
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
36
|
+
|
|
37
|
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
38
|
+
|
|
39
|
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
40
|
+
|
|
41
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
42
|
+
|
|
43
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
kv4_hopper-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
kv4-hopper
|
|
2
|
+
Copyright 2026 Jian Zhang
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
baselines/pr53060/ contains files copied unmodified from vLLM pull request #53060
|
|
7
|
+
(https://github.com/vllm-project/vllm/pull/53060, head commit ae702ab8da5ed577583098272425df1a71ebf2b4),
|
|
8
|
+
part of the vLLM project, licensed under the Apache License, Version 2.0. They are used only as a
|
|
9
|
+
timing baseline.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kv4-hopper
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast 4-bit (NVFP4) KV-cache decoding for vLLM on Hopper GPUs (H100 / H200)
|
|
5
|
+
Author: Jian Zhang
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Paper, https://arxiv.org/abs/2611.30027
|
|
8
|
+
Project-URL: Source, https://github.com/thomaszhang2661/kv4-hopper
|
|
9
|
+
Project-URL: Issues, https://github.com/thomaszhang2661/kv4-hopper/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Environment :: GPU :: NVIDIA CUDA
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Requires-Python: <3.15,>=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
License-File: NOTICE
|
|
21
|
+
Requires-Dist: vllm<0.34,>=0.33.0
|
|
22
|
+
Provides-Extra: calib
|
|
23
|
+
Requires-Dist: transformers; extra == "calib"
|
|
24
|
+
Requires-Dist: datasets; extra == "calib"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# kv4-hopper
|
|
28
|
+
|
|
29
|
+
**Fast 4-bit KV-cache decoding for vLLM on Hopper GPUs (H100 / H200).** Experimental (alpha).
|
|
30
|
+
|
|
31
|
+
Hopper has no FP4 tensor cores, so 4-bit KV caches on Hopper are decoded in software. The kernels currently used for
|
|
32
|
+
this are slow. On an H200, the FlashInfer kernel behind vLLM's NVFP4 KV cache reaches only 10-19% of memory bandwidth.
|
|
33
|
+
`kv4-hopper` is a vLLM plugin with a Hopper-native decode kernel. It works as follows:
|
|
34
|
+
|
|
35
|
+
- a TMA producer warp feeds two wgmma warpgroups;
|
|
36
|
+
- the cache is laid out at write time, so decoded values go from registers straight to the tensor cores;
|
|
37
|
+
- the matrix operands are swapped, so small query groups are not padded to 64 rows.
|
|
38
|
+
|
|
39
|
+
The method and all measurements are in the paper:
|
|
40
|
+
[arXiv:2611.30027](https://arxiv.org/abs/2611.30027).
|
|
41
|
+
|
|
42
|
+
## What was measured
|
|
43
|
+
|
|
44
|
+
All numbers below are on one H200 SXM with vLLM 0.33.0. Details, error bars and the exact jobs are in the paper and
|
|
45
|
+
the repository.
|
|
46
|
+
|
|
47
|
+
- **Kernel.** Measured at 11 batch / context shapes. At every one of them, decode attention is 1.10-1.48x faster than
|
|
48
|
+
the faster of TensorRT-LLM's XQA and FlashAttention-4's native FP8 kernels.
|
|
49
|
+
- **Inside vLLM.** The comparison is against vLLM's FlashInfer FP8 KV path. On Llama-3.1-8B, a full decode step is
|
|
50
|
+
1.12-1.64x faster at batch 8 and above, and on par at batch 1. The KV cache holds 1.63x as many tokens as FP8.
|
|
51
|
+
- **Task accuracy.** MATH-500 with repeated runs, against a BF16 KV cache, on five models: Qwen2.5-7B-Instruct,
|
|
52
|
+
DeepSeek-R1-Distill-Qwen-7B, DeepSeek-R1-Distill-Llama-8B, Phi-4 and Qwen3-32B (thinking on). The 95% intervals
|
|
53
|
+
exclude accuracy losses larger than about 3.5 points.
|
|
54
|
+
- **Perplexity only.** Llama-3.1-8B (the base model) and Qwen2.5-72B-Instruct (FP8-dynamic weights) were measured
|
|
55
|
+
in vLLM for perplexity (within 0.15% of BF16 KV), not for task accuracy.
|
|
56
|
+
- **Where it does not help.** Batch-1 latency, and prefill-heavy request streams (long inputs, short outputs), which
|
|
57
|
+
are level with FP8 or slightly worse.
|
|
58
|
+
|
|
59
|
+
Other models are untested. Run the check below before relying on the results.
|
|
60
|
+
|
|
61
|
+
<!-- serving-capacity numbers added after the paper's v2 Table 6 is final -->
|
|
62
|
+
|
|
63
|
+
## Requirements
|
|
64
|
+
|
|
65
|
+
- An NVIDIA Hopper GPU (sm_90a: H100, H200). Other GPUs are not supported, and the plugin does not register on them.
|
|
66
|
+
- vLLM `>=0.33.0,<0.34` and its torch 2.14 (CUDA 13); Python 3.11-3.14 (prebuilt wheels for Linux x86_64, glibc 2.34
|
|
67
|
+
or newer). Other versions are untested.
|
|
68
|
+
The plugin builds on vLLM's UltraQuant backend, so other vLLM versions may break.
|
|
69
|
+
- An NVIDIA driver for CUDA 13 (580 or newer, or an older data-center driver with the CUDA 13 forward-compatibility
|
|
70
|
+
package), as vLLM 0.33 itself needs.
|
|
71
|
+
- Tested on one GPU. Tensor parallelism > 1 is not tested.
|
|
72
|
+
- Model limits:
|
|
73
|
+
- head dimension 128;
|
|
74
|
+
- 1 to 8 query heads per KV head (measured: 4, 7 and 8; 7 is padded to 8);
|
|
75
|
+
- no sliding-window attention;
|
|
76
|
+
- no attention sinks in the model.
|
|
77
|
+
|
|
78
|
+
## Install
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install vllm==0.33.0
|
|
82
|
+
pip install kv4-hopper
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The plugin registers itself through vLLM's plugin entry point. Nothing needs to be imported.
|
|
86
|
+
|
|
87
|
+
The wheel contains prebuilt sm_90a kernels. You can also build from source instead; this needs nvcc 13.x (the CUDA version of vLLM 0.33's torch), and
|
|
88
|
+
the first use compiles the kernels in about 1-3 minutes per module:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pip install --no-binary kv4-hopper kv4-hopper
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Use
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
vllm serve meta-llama/Llama-3.1-8B --kv-cache-dtype ultraquant_4bit --block-size 64
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Why `ultraquant_4bit`.** vLLM has no option for this format, so the plugin takes over vLLM's existing 4-bit option,
|
|
101
|
+
`ultraquant_4bit`. It reuses that option's cache handling in vLLM but stores NVFP4 in its own page layout (144 bytes
|
|
102
|
+
per token and KV head for K and V together at head dimension 128). The log shows a line from `kv4_vllm` when the plugin is active. `KV4_VLLM=0` turns the
|
|
103
|
+
plugin off and gives vLLM's own `ultraquant_4bit` path.
|
|
104
|
+
|
|
105
|
+
**The default is the configuration measured in the paper:**
|
|
106
|
+
|
|
107
|
+
- NVFP4 values with a per-layer FP32 scale;
|
|
108
|
+
- Hadamard rotation and static mean removal for K;
|
|
109
|
+
- the first 4 tokens of each sequence kept in BF16;
|
|
110
|
+
- layer 0 kept in BF16.
|
|
111
|
+
|
|
112
|
+
**Calibration.** The recipe needs a small per-layer calibration file for each model. Files are bundled for the seven
|
|
113
|
+
models listed under "What was measured". The plugin picks the bundled file from the served model's name. Files for
|
|
114
|
+
all ten models of the paper are also on Hugging Face:
|
|
115
|
+
[ThomasZhangNEU/kv4-hopper-calib](https://huggingface.co/datasets/ThomasZhangNEU/kv4-hopper-calib) (pass one with
|
|
116
|
+
`KV4_CALIB=<path>`). For any other model, including the Instruct version of a bundled base model, create one first:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pip install "kv4-hopper[calib]"
|
|
120
|
+
kv4-calibrate --model <hf model id> --out my_model.pt # one GPU, a few minutes for an 8B model
|
|
121
|
+
KV4_CALIB=my_model.pt vllm serve <hf model id> --kv-cache-dtype ultraquant_4bit --block-size 64
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
If no calibration file is found for the model, or the file does not match the model (layer count, KV-head count,
|
|
125
|
+
head dimension or model name), the plugin stops with an error. It never falls back to an untested setting.
|
|
126
|
+
|
|
127
|
+
## Check that it works on your setup
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
python -m kv4_vllm.check --model meta-llama/Llama-3.1-8B
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
This runs needle prompts through prefix-cache hits, chunked prefill and CUDA-graph decode, and compares the results
|
|
134
|
+
with a BF16 KV cache. A failure prints the case that failed.
|
|
135
|
+
|
|
136
|
+
## Compare against FP8 on your GPU
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
kv4-compare --model meta-llama/Llama-3.1-8B
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
This times one decode step with `vllm bench forward` (exact batches, CUDA graphs, dummy weights, no prefill mixed in)
|
|
143
|
+
for vLLM's FP8 KV cache on FlashInfer and for the plugin, at batch x context 1 x 16K, 8 x 16K, 32 x 16K and
|
|
144
|
+
16 x 64K (`--cases` takes others), and prints the median step times and the speed-ups. It also prints which decode
|
|
145
|
+
kernel FlashInfer used for FP8: vLLM uses TensorRT-LLM's XQA only if its network check to NVIDIA's artifact server
|
|
146
|
+
succeeds and otherwise falls back silently to a slower kernel; behind a proxy, set `VLLM_HAS_FLASHINFER_CUBIN=1`
|
|
147
|
+
for the XQA path. A few minutes on one H100 or H200; on any other GPU it says so and exits.
|
|
148
|
+
|
|
149
|
+
What to expect: the gain is at batch 8 and above and grows with context (1.12-1.64x per step on Llama-3.1-8B in the
|
|
150
|
+
paper); at batch 1 the plugin is on par with FP8 (1.01-1.05x per step), because there the step is mostly the weight
|
|
151
|
+
read, so a 1 x 16K row near 1.0x is the expected result, not a failure.
|
|
152
|
+
|
|
153
|
+
## Status and support
|
|
154
|
+
|
|
155
|
+
- This is research code released as an alpha. Expect rough edges.
|
|
156
|
+
- Maintained on a best-effort basis. Please report bugs as GitHub issues, with the output of `kv4_vllm.check`, the GPU,
|
|
157
|
+
the driver and the vLLM version.
|
|
158
|
+
- Work is under way to contribute the decode kernel upstream, to FlashInfer and vLLM. If that work lands, this
|
|
159
|
+
package will point to the upstream path.
|
|
160
|
+
|
|
161
|
+
## Citation
|
|
162
|
+
|
|
163
|
+
<!-- title = the title arXiv shows (v2 since 11-30: "... Without FP4 Hardware"; v1 was "...: Why Existing Kernels Are Slow and How to Fix Them").
|
|
164
|
+
Re-check on the day of each upload. -->
|
|
165
|
+
|
|
166
|
+
```bibtex
|
|
167
|
+
@misc{zhang2026kv4hopper,
|
|
168
|
+
title = {Fast 4-bit {KV}-Cache Decoding on Hopper {GPUs} Without {FP4} Hardware},
|
|
169
|
+
author = {Zhang, Jian},
|
|
170
|
+
year = {2026},
|
|
171
|
+
eprint = {2611.30027},
|
|
172
|
+
archivePrefix = {arXiv},
|
|
173
|
+
primaryClass = {cs.LG},
|
|
174
|
+
url = {https://arxiv.org/abs/2611.30027}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
Apache-2.0.
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# kv4-hopper
|
|
2
|
+
|
|
3
|
+
**Fast 4-bit KV-cache decoding for vLLM on Hopper GPUs (H100 / H200).** Experimental (alpha).
|
|
4
|
+
|
|
5
|
+
Hopper has no FP4 tensor cores, so 4-bit KV caches on Hopper are decoded in software. The kernels currently used for
|
|
6
|
+
this are slow. On an H200, the FlashInfer kernel behind vLLM's NVFP4 KV cache reaches only 10-19% of memory bandwidth.
|
|
7
|
+
`kv4-hopper` is a vLLM plugin with a Hopper-native decode kernel. It works as follows:
|
|
8
|
+
|
|
9
|
+
- a TMA producer warp feeds two wgmma warpgroups;
|
|
10
|
+
- the cache is laid out at write time, so decoded values go from registers straight to the tensor cores;
|
|
11
|
+
- the matrix operands are swapped, so small query groups are not padded to 64 rows.
|
|
12
|
+
|
|
13
|
+
The method and all measurements are in the paper:
|
|
14
|
+
[arXiv:2611.30027](https://arxiv.org/abs/2611.30027).
|
|
15
|
+
|
|
16
|
+
## What was measured
|
|
17
|
+
|
|
18
|
+
All numbers below are on one H200 SXM with vLLM 0.33.0. Details, error bars and the exact jobs are in the paper and
|
|
19
|
+
the repository.
|
|
20
|
+
|
|
21
|
+
- **Kernel.** Measured at 11 batch / context shapes. At every one of them, decode attention is 1.10-1.48x faster than
|
|
22
|
+
the faster of TensorRT-LLM's XQA and FlashAttention-4's native FP8 kernels.
|
|
23
|
+
- **Inside vLLM.** The comparison is against vLLM's FlashInfer FP8 KV path. On Llama-3.1-8B, a full decode step is
|
|
24
|
+
1.12-1.64x faster at batch 8 and above, and on par at batch 1. The KV cache holds 1.63x as many tokens as FP8.
|
|
25
|
+
- **Task accuracy.** MATH-500 with repeated runs, against a BF16 KV cache, on five models: Qwen2.5-7B-Instruct,
|
|
26
|
+
DeepSeek-R1-Distill-Qwen-7B, DeepSeek-R1-Distill-Llama-8B, Phi-4 and Qwen3-32B (thinking on). The 95% intervals
|
|
27
|
+
exclude accuracy losses larger than about 3.5 points.
|
|
28
|
+
- **Perplexity only.** Llama-3.1-8B (the base model) and Qwen2.5-72B-Instruct (FP8-dynamic weights) were measured
|
|
29
|
+
in vLLM for perplexity (within 0.15% of BF16 KV), not for task accuracy.
|
|
30
|
+
- **Where it does not help.** Batch-1 latency, and prefill-heavy request streams (long inputs, short outputs), which
|
|
31
|
+
are level with FP8 or slightly worse.
|
|
32
|
+
|
|
33
|
+
Other models are untested. Run the check below before relying on the results.
|
|
34
|
+
|
|
35
|
+
<!-- serving-capacity numbers added after the paper's v2 Table 6 is final -->
|
|
36
|
+
|
|
37
|
+
## Requirements
|
|
38
|
+
|
|
39
|
+
- An NVIDIA Hopper GPU (sm_90a: H100, H200). Other GPUs are not supported, and the plugin does not register on them.
|
|
40
|
+
- vLLM `>=0.33.0,<0.34` and its torch 2.14 (CUDA 13); Python 3.11-3.14 (prebuilt wheels for Linux x86_64, glibc 2.34
|
|
41
|
+
or newer). Other versions are untested.
|
|
42
|
+
The plugin builds on vLLM's UltraQuant backend, so other vLLM versions may break.
|
|
43
|
+
- An NVIDIA driver for CUDA 13 (580 or newer, or an older data-center driver with the CUDA 13 forward-compatibility
|
|
44
|
+
package), as vLLM 0.33 itself needs.
|
|
45
|
+
- Tested on one GPU. Tensor parallelism > 1 is not tested.
|
|
46
|
+
- Model limits:
|
|
47
|
+
- head dimension 128;
|
|
48
|
+
- 1 to 8 query heads per KV head (measured: 4, 7 and 8; 7 is padded to 8);
|
|
49
|
+
- no sliding-window attention;
|
|
50
|
+
- no attention sinks in the model.
|
|
51
|
+
|
|
52
|
+
## Install
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install vllm==0.33.0
|
|
56
|
+
pip install kv4-hopper
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The plugin registers itself through vLLM's plugin entry point. Nothing needs to be imported.
|
|
60
|
+
|
|
61
|
+
The wheel contains prebuilt sm_90a kernels. You can also build from source instead; this needs nvcc 13.x (the CUDA version of vLLM 0.33's torch), and
|
|
62
|
+
the first use compiles the kernels in about 1-3 minutes per module:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install --no-binary kv4-hopper kv4-hopper
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Use
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
vllm serve meta-llama/Llama-3.1-8B --kv-cache-dtype ultraquant_4bit --block-size 64
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**Why `ultraquant_4bit`.** vLLM has no option for this format, so the plugin takes over vLLM's existing 4-bit option,
|
|
75
|
+
`ultraquant_4bit`. It reuses that option's cache handling in vLLM but stores NVFP4 in its own page layout (144 bytes
|
|
76
|
+
per token and KV head for K and V together at head dimension 128). The log shows a line from `kv4_vllm` when the plugin is active. `KV4_VLLM=0` turns the
|
|
77
|
+
plugin off and gives vLLM's own `ultraquant_4bit` path.
|
|
78
|
+
|
|
79
|
+
**The default is the configuration measured in the paper:**
|
|
80
|
+
|
|
81
|
+
- NVFP4 values with a per-layer FP32 scale;
|
|
82
|
+
- Hadamard rotation and static mean removal for K;
|
|
83
|
+
- the first 4 tokens of each sequence kept in BF16;
|
|
84
|
+
- layer 0 kept in BF16.
|
|
85
|
+
|
|
86
|
+
**Calibration.** The recipe needs a small per-layer calibration file for each model. Files are bundled for the seven
|
|
87
|
+
models listed under "What was measured". The plugin picks the bundled file from the served model's name. Files for
|
|
88
|
+
all ten models of the paper are also on Hugging Face:
|
|
89
|
+
[ThomasZhangNEU/kv4-hopper-calib](https://huggingface.co/datasets/ThomasZhangNEU/kv4-hopper-calib) (pass one with
|
|
90
|
+
`KV4_CALIB=<path>`). For any other model, including the Instruct version of a bundled base model, create one first:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install "kv4-hopper[calib]"
|
|
94
|
+
kv4-calibrate --model <hf model id> --out my_model.pt # one GPU, a few minutes for an 8B model
|
|
95
|
+
KV4_CALIB=my_model.pt vllm serve <hf model id> --kv-cache-dtype ultraquant_4bit --block-size 64
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
If no calibration file is found for the model, or the file does not match the model (layer count, KV-head count,
|
|
99
|
+
head dimension or model name), the plugin stops with an error. It never falls back to an untested setting.
|
|
100
|
+
|
|
101
|
+
## Check that it works on your setup
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
python -m kv4_vllm.check --model meta-llama/Llama-3.1-8B
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
This runs needle prompts through prefix-cache hits, chunked prefill and CUDA-graph decode, and compares the results
|
|
108
|
+
with a BF16 KV cache. A failure prints the case that failed.
|
|
109
|
+
|
|
110
|
+
## Compare against FP8 on your GPU
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
kv4-compare --model meta-llama/Llama-3.1-8B
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
This times one decode step with `vllm bench forward` (exact batches, CUDA graphs, dummy weights, no prefill mixed in)
|
|
117
|
+
for vLLM's FP8 KV cache on FlashInfer and for the plugin, at batch x context 1 x 16K, 8 x 16K, 32 x 16K and
|
|
118
|
+
16 x 64K (`--cases` takes others), and prints the median step times and the speed-ups. It also prints which decode
|
|
119
|
+
kernel FlashInfer used for FP8: vLLM uses TensorRT-LLM's XQA only if its network check to NVIDIA's artifact server
|
|
120
|
+
succeeds and otherwise falls back silently to a slower kernel; behind a proxy, set `VLLM_HAS_FLASHINFER_CUBIN=1`
|
|
121
|
+
for the XQA path. A few minutes on one H100 or H200; on any other GPU it says so and exits.
|
|
122
|
+
|
|
123
|
+
What to expect: the gain is at batch 8 and above and grows with context (1.12-1.64x per step on Llama-3.1-8B in the
|
|
124
|
+
paper); at batch 1 the plugin is on par with FP8 (1.01-1.05x per step), because there the step is mostly the weight
|
|
125
|
+
read, so a 1 x 16K row near 1.0x is the expected result, not a failure.
|
|
126
|
+
|
|
127
|
+
## Status and support
|
|
128
|
+
|
|
129
|
+
- This is research code released as an alpha. Expect rough edges.
|
|
130
|
+
- Maintained on a best-effort basis. Please report bugs as GitHub issues, with the output of `kv4_vllm.check`, the GPU,
|
|
131
|
+
the driver and the vLLM version.
|
|
132
|
+
- Work is under way to contribute the decode kernel upstream, to FlashInfer and vLLM. If that work lands, this
|
|
133
|
+
package will point to the upstream path.
|
|
134
|
+
|
|
135
|
+
## Citation
|
|
136
|
+
|
|
137
|
+
<!-- title = the title arXiv shows (v2 since 11-30: "... Without FP4 Hardware"; v1 was "...: Why Existing Kernels Are Slow and How to Fix Them").
|
|
138
|
+
Re-check on the day of each upload. -->
|
|
139
|
+
|
|
140
|
+
```bibtex
|
|
141
|
+
@misc{zhang2026kv4hopper,
|
|
142
|
+
title = {Fast 4-bit {KV}-Cache Decoding on Hopper {GPUs} Without {FP4} Hardware},
|
|
143
|
+
author = {Zhang, Jian},
|
|
144
|
+
year = {2026},
|
|
145
|
+
eprint = {2611.30027},
|
|
146
|
+
archivePrefix = {arXiv},
|
|
147
|
+
primaryClass = {cs.LG},
|
|
148
|
+
url = {https://arxiv.org/abs/2611.30027}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
Apache-2.0.
|