[DSv4] Use cvt PTX for FP32->FP4 conversion (#41015)
Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg>
This commit is contained in:
@@ -21,7 +21,7 @@ and N_QUANT_BLOCKS ue8m0 bytes.
|
||||
|
||||
from vllm.triton_utils import tl, triton
|
||||
|
||||
from .fused_indexer_q import _e2m1_nibble
|
||||
from .fused_indexer_q import _fp32x2_to_fp4x2
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -566,18 +566,18 @@ def _fused_kv_compress_norm_rope_insert_indexer_mxfp4_attn(
|
||||
tl.max(tl.abs(even_2d), axis=1),
|
||||
tl.max(tl.abs(odd_2d), axis=1),
|
||||
)
|
||||
amax = tl.maximum(amax, 1e-4)
|
||||
amax = tl.maximum(amax, 6.0 * (2**-126))
|
||||
|
||||
# ue8m0 block scale: 2^ceil(log2(amax / 6.0)), stored as (exp + 127) byte.
|
||||
log2_ratio = tl.ceil(tl.log2(amax / 6.0))
|
||||
log2_ratio = tl.ceil(tl.log2(amax * (1.0 / 6.0)))
|
||||
log2_ratio = tl.minimum(tl.maximum(log2_ratio, -127.0), 127.0)
|
||||
inv_scale = tl.exp2(-log2_ratio)
|
||||
ue8m0 = (log2_ratio + 127.0).to(tl.uint8) # [N_QUANT_BLOCKS]
|
||||
|
||||
inv_scale_col = tl.reshape(inv_scale, (N_QUANT_BLOCKS, 1))
|
||||
lo_nib = _e2m1_nibble(even_2d * inv_scale_col) # (N_BLOCKS, HALF_BLOCK) uint8
|
||||
hi_nib = _e2m1_nibble(odd_2d * inv_scale_col)
|
||||
packed = lo_nib | (hi_nib << 4)
|
||||
packed = _fp32x2_to_fp4x2(
|
||||
even_2d * inv_scale_col, odd_2d * inv_scale_col
|
||||
) # (N_BLOCKS, HALF_BLOCK) uint8
|
||||
packed_flat = tl.reshape(packed, (TOKEN_STRIDE,))
|
||||
|
||||
tl.store(val_ptr + tl.arange(0, TOKEN_STRIDE), packed_flat)
|
||||
|
||||
@@ -24,36 +24,22 @@ def _get_cos_sin(
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _e2m1_nibble(x):
|
||||
"""Quantize fp32 x (already scale-divided) to E2M1 4-bit nibble in uint8.
|
||||
Matches torch.bucketize with boundaries
|
||||
[0.25, 0.75, 1.25, 1.75, 2.5, 3.5, 5.0] and right=False (each boundary
|
||||
belongs to the lower bucket), plus sign bit."""
|
||||
abs_x = tl.minimum(tl.abs(x), 6.0)
|
||||
code = tl.where(
|
||||
abs_x <= 0.25,
|
||||
0.0,
|
||||
tl.where(
|
||||
abs_x <= 0.75,
|
||||
1.0,
|
||||
tl.where(
|
||||
abs_x <= 1.25,
|
||||
2.0,
|
||||
tl.where(
|
||||
abs_x <= 1.75,
|
||||
3.0,
|
||||
tl.where(
|
||||
abs_x <= 2.5,
|
||||
4.0,
|
||||
tl.where(abs_x <= 3.5, 5.0, tl.where(abs_x <= 5.0, 6.0, 7.0)),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
code_u8 = code.to(tl.uint8)
|
||||
sign = ((x < 0) & (code_u8 != 0)).to(tl.uint8)
|
||||
return code_u8 | (sign << 3)
|
||||
def _fp32x2_to_fp4x2(x_lo, x_hi):
|
||||
# NOTE: $1 is high nibble, $2 is low nibble
|
||||
return tl.inline_asm_elementwise(
|
||||
"""
|
||||
{
|
||||
.reg .b8 tmp;
|
||||
cvt.rn.satfinite.e2m1x2.f32 tmp, $1, $2;
|
||||
cvt.u32.u8 $0, tmp;
|
||||
}
|
||||
""",
|
||||
constraints="=r,f,f",
|
||||
args=[x_hi, x_lo],
|
||||
dtype=tl.uint32,
|
||||
is_pure=True,
|
||||
pack=1,
|
||||
).to(tl.uint8)
|
||||
|
||||
|
||||
@triton.jit
|
||||
@@ -65,17 +51,16 @@ def _quantize_mxfp4_pair(x_lo, x_hi):
|
||||
- ue8m0 : scalar uint8 (block scale = 2^(ue8m0 - 127))
|
||||
"""
|
||||
amax = tl.maximum(tl.max(tl.abs(x_lo)), tl.max(tl.abs(x_hi)))
|
||||
amax = tl.maximum(amax, 1e-4)
|
||||
# 6 * 2^-126 is from https://huggingface.co/deepseek-ai/DeepSeek-V4-Pro/blob/main/inference/kernel.py#L163
|
||||
amax = tl.maximum(amax, 6.0 * (2**-126))
|
||||
# ue8m0 block scale: 2^ceil(log2(amax/6.0)).
|
||||
log2_ratio = tl.math.ceil(tl.math.log2(amax / 6.0))
|
||||
log2_ratio = tl.math.ceil(tl.math.log2(amax * (1.0 / 6.0)))
|
||||
log2_ratio = tl.minimum(tl.maximum(log2_ratio, -127.0), 127.0)
|
||||
scale = tl.math.exp2(log2_ratio)
|
||||
ue8m0 = (log2_ratio + 127.0).to(tl.uint8)
|
||||
|
||||
inv_scale = 1.0 / scale
|
||||
lo_nib = _e2m1_nibble(x_lo * inv_scale)
|
||||
hi_nib = _e2m1_nibble(x_hi * inv_scale)
|
||||
packed = lo_nib | (hi_nib << 4)
|
||||
packed = _fp32x2_to_fp4x2(x_lo * inv_scale, x_hi * inv_scale)
|
||||
return packed, ue8m0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user