[Kernel][Performance] Enable smaller Scaling Factor tiling for NVFP4 small-batch decoding (#30885)

Signed-off-by: LopezCastroRoberto <roberto.lopez.castro@udc.es>
Signed-off-by: Roberto L. Castro <38211239+LopezCastroRoberto@users.noreply.github.com>
Signed-off-by: LopezCastroRoberto <rocastro@redhat.com>
This commit is contained in:
Roberto L. Castro
2026-01-13 15:22:53 -08:00
committed by GitHub
parent 2a60ac91d0
commit 8ef50d9a6b
9 changed files with 177 additions and 32 deletions
+54 -1
View File
@@ -406,12 +406,21 @@ if has_flashinfer():
B_scale: torch.Tensor,
g_scale: torch.Tensor,
dtype: torch.dtype,
use_8x4_sf_layout: bool,
backend: str,
) -> torch.Tensor:
from flashinfer import mm_fp4 as flashinfer_mm_fp4_
return flashinfer_mm_fp4_(
A, B, A_scale, B_scale, g_scale, dtype, block_size=16, backend=backend
A,
B,
A_scale,
B_scale,
g_scale,
dtype,
block_size=16,
use_8x4_sf_layout=use_8x4_sf_layout,
backend=backend,
)
@torch.library.register_fake(
@@ -424,6 +433,7 @@ if has_flashinfer():
B_scale: torch.Tensor,
g_scale: torch.Tensor,
dtype: torch.dtype,
use_8x4_sf_layout: bool,
backend: str,
) -> torch.Tensor:
return torch.empty(A.shape[0], B.shape[1], dtype=dtype, device=A.device)
@@ -460,6 +470,39 @@ if has_flashinfer():
A.shape[0], A.shape[1], B.shape[2], dtype=dtype, device=A.device
)
@torch.library.custom_op(
"vllm::flashinfer_nvfp4_quantize",
mutates_args=[],
device_types="cuda",
)
def flashinfer_nvfp4_quantize(
a: torch.Tensor, a_global_sf: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
from flashinfer import SfLayout
from flashinfer import nvfp4_quantize as nvfp4_quantize_
return nvfp4_quantize_(
a, a_global_sf, sfLayout=SfLayout.layout_8x4, do_shuffle=False
)
@torch.library.register_fake(
"vllm::flashinfer_nvfp4_quantize",
)
def flashinfer_nvfp4_quantize_fake(
a: torch.Tensor, a_global_sf: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
m, n = a.shape
round_up = lambda x, y: (x + y - 1) // y * y
rounded_m = round_up(m, 8)
scale_n = n // 16
rounded_n = round_up(scale_n, 4)
return torch.empty(m, n // 2, dtype=torch.uint8, device=a.device), torch.empty(
rounded_m, rounded_n, dtype=torch.uint8, device=a.device
)
def flashinfer_scaled_fp4_mm(
a: torch.Tensor,
@@ -479,6 +522,8 @@ def flashinfer_scaled_fp4_mm(
block_scale_a = block_scale_a.view(torch.uint8)
block_scale_b = block_scale_b.view(torch.uint8)
use_8x4_sf_layout = True if backend == "trtllm" and a.shape[0] <= 32 else False # noqa: SIM210
return flashinfer_mm_fp4(
a,
b.t(),
@@ -486,6 +531,7 @@ def flashinfer_scaled_fp4_mm(
block_scale_b.t(),
alpha,
out_dtype,
use_8x4_sf_layout=use_8x4_sf_layout,
backend=backend,
)
@@ -520,6 +566,12 @@ def flashinfer_scaled_fp8_mm(
return output
def flashinfer_quant_nvfp4_8x4_sf_layout(
a: torch.Tensor, a_global_sf: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
return flashinfer_nvfp4_quantize(a, a_global_sf)
flashinfer_fp8_blockscale_gemm = _lazy_import_wrapper(
"flashinfer.gemm", "fp8_blockscale_gemm_sm90"
)
@@ -596,6 +648,7 @@ __all__ = [
"use_trtllm_attention",
"flashinfer_scaled_fp4_mm",
"flashinfer_scaled_fp8_mm",
"flashinfer_quant_nvfp4_8x4_sf_layout",
"flashinfer_fp8_blockscale_gemm",
"should_use_flashinfer_for_blockscale_fp8_gemm",
"is_flashinfer_fp8_blockscale_gemm_supported",