|
| 1 | +.. _tutorial/fp8_kv_quantization_en: |
| 2 | + |
| 3 | +FP8 KV Quantization and Calibration Guide |
| 4 | +========================================= |
| 5 | + |
| 6 | +This chapter describes FP8 KV inference in LightLLM, including: |
| 7 | + |
| 8 | +- Running inference with calibration data (``fp8kv``) |
| 9 | +- Quantization granularity differences between FA3 and FlashInfer |
| 10 | +- Common errors and troubleshooting |
| 11 | + |
| 12 | +Overview |
| 13 | +-------- |
| 14 | + |
| 15 | +LightLLM FP8 KV inference requires a prepared calibration file (``kv_cache_calib.json``), |
| 16 | +which is loaded by ``--kv_quant_calibration_config_path``. |
| 17 | +You can use calibration files provided in ``test/advanced_config/``, |
| 18 | +export one with `LightCompress <https://github.com/ModelTC/LightCompress>`_, or use your own compatible file. |
| 19 | + |
| 20 | +Backend and Quantization Granularity |
| 21 | +------------------------------------ |
| 22 | + |
| 23 | +Current behavior: |
| 24 | + |
| 25 | +- ``fa3``: ``per_head`` scales (independent scale per head) |
| 26 | +- ``flashinfer``: ``per_tensor`` scales (one scalar for K and one scalar for V) |
| 27 | + |
| 28 | +Calibration files are backend-dependent: |
| 29 | + |
| 30 | +- ``per_head`` files for ``fa3`` should be used with ``fa3`` inference. |
| 31 | +- ``per_tensor`` files for ``flashinfer`` should be used with ``flashinfer`` inference. |
| 32 | + |
| 33 | +Avoid mixing calibration files across different backends. |
| 34 | + |
| 35 | +Start FP8 Inference with Calibration |
| 36 | +------------------------------------ |
| 37 | + |
| 38 | +Inference mode example (FA3): |
| 39 | + |
| 40 | +.. code-block:: console |
| 41 | +
|
| 42 | + $ python -m lightllm.server.api_server \ |
| 43 | + --model_dir /path/to/model \ |
| 44 | + --llm_kv_type fp8kv \ |
| 45 | + --llm_prefill_att_backend fa3 \ |
| 46 | + --llm_decode_att_backend fa3 \ |
| 47 | + --kv_quant_calibration_config_path /path/to/kv_cache_calib.json |
| 48 | +
|
| 49 | +Inference mode example (FlashInfer): |
| 50 | + |
| 51 | +.. code-block:: console |
| 52 | +
|
| 53 | + $ python -m lightllm.server.api_server \ |
| 54 | + --model_dir /path/to/model \ |
| 55 | + --llm_kv_type fp8kv \ |
| 56 | + --llm_prefill_att_backend flashinfer \ |
| 57 | + --llm_decode_att_backend flashinfer \ |
| 58 | + --kv_quant_calibration_config_path /path/to/kv_cache_calib.json |
| 59 | +
|
| 60 | +Notes: |
| 61 | + |
| 62 | +- ``fp8kv`` requires ``--kv_quant_calibration_config_path``. |
| 63 | +- Keep the inference backend consistent with the backend expected by the calibration file. |
| 64 | + |
| 65 | +Calibration File Schema |
| 66 | +----------------------- |
| 67 | + |
| 68 | +Key fields in ``kv_cache_calib.json``: |
| 69 | + |
| 70 | +- ``quant_type``: ``per_head`` or ``per_tensor`` |
| 71 | +- ``num_layers``: number of layers |
| 72 | +- ``num_head``: total number of heads |
| 73 | +- ``scales_shape``: shape of the scale tensor |
| 74 | +- ``scales``: actual scale values |
| 75 | +- ``qmin`` / ``qmax``: FP8 numeric range parameters |
| 76 | + |
| 77 | +At load time, LightLLM validates architecture, layer count, head count, and quantization type. |
| 78 | + |
| 79 | +Multi-GPU Note |
| 80 | +-------------- |
| 81 | + |
| 82 | +In multi-GPU (TP) setups, LightLLM slices the global scales to local rank heads automatically. |
| 83 | +You only need to provide one full ``kv_cache_calib.json`` file. |
| 84 | + |
| 85 | +Common Issues |
| 86 | +------------- |
| 87 | + |
| 88 | +1. Error says ``--kv_quant_calibration_config_path`` is required |
| 89 | + |
| 90 | + You are using ``--llm_kv_type fp8kv`` without a calibration file path. |
| 91 | + |
| 92 | +2. ``quant_type not match`` error |
| 93 | + |
| 94 | + Usually caused by backend/file mismatch (for example, using a ``per_head`` file with ``flashinfer``). |
| 95 | + |
| 96 | +3. Abnormal quality after backend switch |
| 97 | + |
| 98 | + Use a calibration file that matches the target backend instead of reusing an incompatible file. |
0 commit comments