feat: add per-tensor FP8 e4m3 quant/dequant ops for KV cache quantiza… - #1521
Open
BoBoDai wants to merge 2 commits into
Open
feat: add per-tensor FP8 e4m3 quant/dequant ops for KV cache quantiza…#1521BoBoDai wants to merge 2 commits into
BoBoDai wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: add per-tensor FP8 e4m3 quant/dequant ops for KV cache quantization
摘要
本 PR 新增两个算子:
per_tensor_quant_fp8(量化)与per_tensor_dequant_fp8(反量化),实现 FP8 e4m3 格式的对称 per-tensor 量化。二者为 InfiniLM 后续 FP8 KV cache 量化(--kv-cache-dtype fp8,尚未实现)提供前置算子基础:存储 K/V 时调用 quant 算子将状态转换为 FP8 e4m3,attention 读取时调用 dequant 算子还原,均为该路径每次 decode 所需的核心内核。本 PR 仅包含算子实现与配套测试,KV cache 侧的接入与基准测试属于后续工作。动机:FP8 e4m3 是当前主流推理框架(vLLM、SGLang 等)KV cache 量化的常用格式,
在同等 1 字节占用下精度优于 INT8,契合低精度格式方向。
e4m3 格式
[-448, 448];最小正规数2^-6,最小次正规数2^-9。0x7E/0xFE),不依赖 CUDA fp8 转换头。2^-9)。API
算子接口完全参照现有
per_tensor_quant_int8与per_tensor_dequant_int8的实现约定:|
per_tensor_quant_fp8|infiniopPerTensorQuantFp8(desc, ws, x_packed, x_scale, x, is_static, stream)||
per_tensor_dequant_fp8|infiniopPerTensorDequantFp8(desc, ws, x, x_packed, x_scale, stream)|x(fp16/bf16/fp32) ->x_packed(fp8/uint8) +x_scale(fp32,[1]);dequant 为x_packed(fp8/uint8) +x_scale(fp32,[1]) ->x(fp16/bf16/fp32)。max|x| / 448)与静态(调用方给定 scale)两种模式。infiniopGet*WorkspaceSize/infiniopDestroy*Descriptor入口,并在include/infiniop.h、include/infinicore/ops.hpp注册,含 infinicore C++ 封装(PerTensorQuantFp8/PerTensorDequantFp8)。per_tensor_quant_int8一致。测试
test/infiniop/下新增三个测试文件(对照 int8 测试结构):per_tensor_quant_fp8.py:动态/静态模式、strided 布局、F16/BF16/F32; 另含确定性边界用例(零、次正规数、进位舍入、精确 448、超范围饱和),以及全零输入保底用例(动态模式下 scale=0 不除零、输出全 0、scale 归一化为 1.0)。per_tensor_dequant_fp8.py:形状/stride 覆盖;另含字节级边界用例(0x00/0x01/0x07/0x08/0x7E/0x7F/0x80/0xFF等,覆盖零、次正规、最小正规、最大值与保留位模式)。w8a8fp8.py:quant + dequant + matmul 端到端链路,两种模式——WMode.FP16(仅激活量化、权重保持 fp16,对应 KV cache 场景)与WMode.FP8(完整对称 W8A8-FP8)。验证
运行

python scripts/python_test.py --nvidia,所有算子测试全部通过。运行

python scripts/python_test.py --cpu,所有算子测试全部通过。后续工作(不在本 PR 范围内)
KVQuantAlgo::FP8_E4M3、--kv-cache-dtype fp8):存储 K/V 时以per_tensor_quant_fp8量化、读取时以per_tensor_dequant_fp8反量化,配合静态 per-cache scale。