Microscaling (MX) Formats: MXFP4,NVFP4

  • 本文主要介绍目前已被 Blackwell GPU 硬件支持的两种 FP4 格式:MXFP4 和 NVFP4;目前 vLLM 也已经实现了对 NVFP4 WA 量化的支持
    在这里插入图片描述在这里插入图片描述

MXFP4

  • Microscaling (MX). MX-compliant format 如下所示, k k k 个数据 ( P 1 , . . . , P k P_1,...,P_k P1,...,Pk) 共享一个 scale X X X,这 k k k 个数据称为一个 block,每个 block 需要占用 ( w + k d ) (w+kd) (w+kd) bits;这 k k k 个数据的真实数值由 v i = X P i v_i=XP_i vi=XPi 得到,实际数值范围与 FP32 一致
    在这里插入图片描述
  • Concrete MX-compliant Formats. 重点看 MXFP4,其 block size 为 32,也就是说每 32 个数据共享相同 scale, P i P_i Pi 数据类型为 FP4 (E2M1),scale 数据类型为 FP8 (E8M0) (无符号位)
    在这里插入图片描述FP4 (E2M1) 的标准定义如下,正规数 (指数部分 E > 0 E>0 E>0) 计算公式为 v = ( − 1 ) S × 2 E − b i a s × ( 1 + 2 − m × M ) v=(-1)^S\times 2^{E-bias}\times(1+2^{-m}\times M) v=(1)S×2Ebias×(1+2m×M),非正规数 (指数部分 E = 0 E=0 E=0) 计算公式为 v = ( − 1 ) S × 2 1 − b i a s × ( 0 + 2 − m × M ) v=(-1)^S\times 2^{1-bias}\times(0+2^{-m}\times M) v=(1)S×21bias×(0+2m×M),因此 FP4 (E2M1) 数值依次为 0 , 0.5 , 1 , 1 , 2 , 3 , 4 , 6 0, 0.5, 1, 1, 2, 3, 4, 6 0,0.5,1,1,2,3,4,6,数值范围为 [ − 6 , 6 ] [-6,6] [6,6]
    在这里插入图片描述FP8 (E8M0) 的标准定义如下,数值范围为 [ − 2 127 , 2 127 ] [-2^{127},2^{127}] [2127,2127]
    在这里插入图片描述
  • MXFP4 Quantization Procedure. (1) Block Scaling. block scale 可以直接由 s d e c , e 8 m 0 = e8m0 ( amax ( P ) 6 ) s_{dec,e8m0}=\text{e8m0}\left(\frac{\text{amax}(\mathbf P)}{6}\right) sdec,e8m0=e8m0(6amax(P)) s e n c = 1 fp16 ( s d e c ) s_{enc}=\frac{1}{\text{fp16}(s_{dec})} senc=fp16(sdec)1 得到,但由于 E8M0 精度较差,如果 s d e c s_{dec} sdec 向下取整,那么就可能会有大量 outliers overflow,从而影响训练稳定性,因此通常需要 s d e c s_{dec} sdec 向上取整,但这样也会导致一部分的 FP4 range 没有利用到,变相地减少了 FP4 的动态表征范围;(2) Conversion. 假设输入都已量化到 FP4,单个 block 内的计算过程如下:
    s d e c , e 8 m 0 x ⋅ s d e c , e 8 m 0 y ⋅ ∑ k ∈ b ( x ^ k ⋅ y ^ k ) s^x_{dec,e8m0}\cdot s^y_{dec,e8m0}\cdot \sum_{k\in b}(\hat x_k\cdot \hat y_k) sdec,e8m0xsdec,e8m0ykb(x^ky^k)其中, x ^ k = q ( x k ⋅ s e n c x ) \hat x_k=q(x_k\cdot s_{enc}^x) x^k=q(xksencx) y ^ k = q ( y k ⋅ s e n c y ) \hat y_k=q(y_k\cdot s_{enc}^y) y^k=q(yksency) q ( ⋅ ) q(\cdot) q() 为 FP4 量化函数,目前 Blackwell GPUs 支持多种 FP4 取整方式,包括 round-to-nearest-even (Tie-breaking rule: If the value is exactly midway between two representable numbers (a “tie”), it rounds to the one with an even mantissa) 和 stochastic rounding

NVFP4

  • NVFP4 相比 MXFP4, P i P_i Pi 同样采用了 FP4 (E2M1) 的数据类型,但不同的是采用了更小的 block size (16),相同 block 内共享一个 FP8 (E4M3) scaling,通过采用非指数的高精度缩放因子,NVFP4 能更准确地匹配块内值分布,减少量化误差,但代价是会极大地缩小数据表示范围;为了扩充数据表示范围,NVFP4 采用了双层缩放策略,张量内的所有数据还同时共享一个全局的 FP32 scaling,使得 NVFP4 可以覆盖接近 FP32 的动态范围
    在这里插入图片描述在这里插入图片描述
  • NVFP4 Quantization Procedure. block scale 用于将数据映射到 FP4 (E2M1) 能表示的范围内,可以由 s d e c , b = amax ( B ) 6 s_{dec,b}=\frac{\text{amax}(\mathbf B)}{6} sdec,b=6amax(B) 得到,其中 B \mathbf B B 为一个大小为 16 的 block. NVFP4 本质上是对所有 local scale s d e c , b s_{dec,b} sdec,b 一起做了 per-tensor sym FP8 (E4M3) 量化,对应的 scale 即为 FP32 的 global scale. 最大的 local scale 为 amax ( X ) 6 \frac{\text{amax}(\mathbf X)}{6} 6amax(X),其中 X \mathbf X X 为整个 tensor,因此 global scale s d e c = amax ( X ) 6 ⋅ 448 s_{dec}=\frac{\text{amax}(\mathbf X)}{6\cdot 448} sdec=6448amax(X) s e n c = 6 ⋅ 448 amax ( X ) s_{enc}=\frac{6\cdot 448}{\text{amax}(\mathbf X)} senc=amax(X)6448 (也可以看作是 global scale 将数据映射到了 FP4 (E2M1) × \times × FP8 (E4M3) 能表示的范围内),这样 FP8 local scale s d e c , b , e 4 m 3 = e4m3 ( s d e c , b ⋅ s e n c ) s_{dec,b,e4m3}=\text{e4m3}(s_{dec,b}\cdot s_{enc}) sdec,b,e4m3=e4m3(sdec,bsenc) s e n c , b = 1 / ( fp32 ( s d e c , b , e 4 m 3 ) ⋅ s d e c ) s_{enc,b}=1/(\text{fp32}(s_{dec,b,e4m3})\cdot s_{dec}) senc,b=1/(fp32(sdec,b,e4m3)sdec);需要注意的是,local scale 支持 dynamic/static 计算,而 global scale 只支持静态计算,对于激活值量化,global scale 作用于所有激活值,因此需要提前在校准集上离线校准
    在这里插入图片描述
  • NVFP4 Computation. 假设输入都已量化到 FP4,单个 block 内的计算过程如下:
    s d e c x ⋅ s d e c y ⋅ ( s d e c , b , e 4 m 3 x ⋅ s d e c , b , e 4 m 3 y ⋅ ∑ k ∈ b ( x ^ k ⋅ y ^ k ) ) s^x_{dec}\cdot s^y_{dec}\cdot\left(s^x_{dec,b,e4m3}\cdot s^y_{dec,b,e4m3}\cdot \sum_{k\in b}(\hat x_k\cdot \hat y_k)\right) sdecxsdecy(sdec,b,e4m3xsdec,b,e4m3ykb(x^ky^k))其中, x ^ k = q ( x k ⋅ s e n c , b x ) \hat x_k=q(x_k\cdot s_{enc,b}^x) x^k=q(xksenc,bx) y ^ k = q ( y k ⋅ s e n c , b y ) \hat y_k=q(y_k\cdot s_{enc,b}^y) y^k=q(yksenc,by) q ( ⋅ ) q(\cdot) q() 为 FP4 量化函数,目前 Blackwell GPUs 支持多种 FP4 取整方式,包括 round-to-nearest-even (Tie-breaking rule: If the value is exactly midway between two representable numbers (a “tie”), it rounds to the one with an even mantissa) 和 stochastic rounding

  • 从下图中可以看到,NVFP4 量化在大规模 LLM (>32B) 上可以达到接近无损的效果,DeepSeek 量化模型开源在了 huggingface;需要注意的是,对于 DeepSeek-R1 部分层仍然使用高精度,例如 QKV GEMM 保持为 FP16,Attention 和 KV Cache 均使用 FP8 量化;此外,对于 Qwen3-235B,QKV GEMM 也可以量化到 NVFP4,仅 Attention 和 KV Cache 需要使用 FP8 量化
    在这里插入图片描述

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值