[Perf] Padded nvfp4 quant kernel to remove additional copy, 2.4%~5.7% e2e performance improvement (#42774)
Signed-off-by: yewentao256 <zhyanwentao@126.com>
This commit is contained in:
+24
-5
@@ -69,15 +69,20 @@ def create_fp4_output_tensors(
|
||||
n: int,
|
||||
device: torch.device,
|
||||
is_sf_swizzled_layout: bool,
|
||||
padded_n: int | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
Allocate both output tensors for scaled_fp4_quant:
|
||||
(quantized_output, output_scale).
|
||||
|
||||
Must match the C++ scaled_fp4_quant_func allocation exactly.
|
||||
Must match the C++ scaled_fp4_quant_func allocation exactly when
|
||||
``padded_n`` is ``None``. When ``padded_n`` is provided, allocate a larger
|
||||
packed-FP4 output/scale buffer so the quantization kernel can write
|
||||
CUTLASS-compatible K padding directly
|
||||
"""
|
||||
output = torch.empty((m, n // 2), device=device, dtype=torch.uint8)
|
||||
output_scale = create_fp4_scale_tensor(m, n, device, is_sf_swizzled_layout)
|
||||
physical_n = padded_n if padded_n is not None else n
|
||||
output = torch.empty((m, physical_n // 2), device=device, dtype=torch.uint8)
|
||||
output_scale = create_fp4_scale_tensor(m, physical_n, device, is_sf_swizzled_layout)
|
||||
return output, output_scale
|
||||
|
||||
|
||||
@@ -1597,6 +1602,7 @@ def scaled_fp4_quant(
|
||||
input_global_scale: torch.Tensor,
|
||||
is_sf_swizzled_layout: bool = True,
|
||||
backend: str = "none",
|
||||
padded_n: int | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
Quantize input tensor to FP4 and return quantized tensor and scale.
|
||||
@@ -1611,6 +1617,8 @@ def scaled_fp4_quant(
|
||||
input: The input tensor to be quantized to FP4
|
||||
input_global_scale: A scalar scaling factor for the entire tensor.
|
||||
use_8x4_sf_layout: Whether to use the 8x4 or 128x4 layout for the scaling
|
||||
padded_n: Optional padded K dimension. When provided, the quantized
|
||||
output and scale tensors are allocated for ``padded_n``
|
||||
|
||||
Returns:
|
||||
tuple[torch.Tensor, torch.Tensor]: The output tensor in FP4 but every
|
||||
@@ -1628,9 +1636,16 @@ def scaled_fp4_quant(
|
||||
assert input.dtype in (torch.float16, torch.bfloat16), (
|
||||
f"input.dtype needs to be fp16 or bf16 but got {input.dtype}."
|
||||
)
|
||||
if padded_n is not None:
|
||||
assert padded_n >= n, f"padded_n must be >= n, got padded_n={padded_n}, n={n}."
|
||||
assert padded_n % block_size == 0, (
|
||||
f"padded_n has to be a multiple of {block_size}, but got {padded_n}."
|
||||
)
|
||||
|
||||
use_8x4_sf_layout = True if "trtllm" in backend and m <= 32 else False # noqa: SIM210
|
||||
|
||||
if use_8x4_sf_layout and padded_n is not None and padded_n != n:
|
||||
# TODO: support this case
|
||||
raise ValueError("padded_n is not supported with TRTLLM 8x4 scale layout.")
|
||||
if use_8x4_sf_layout:
|
||||
output, output_scale = flashinfer_quant_nvfp4_8x4_sf_layout(
|
||||
input, input_global_scale
|
||||
@@ -1638,7 +1653,11 @@ def scaled_fp4_quant(
|
||||
else:
|
||||
# Pre-allocate and call .out variant (same behavior as old in-place API)
|
||||
output, output_scale = create_fp4_output_tensors(
|
||||
m, n, input.device, is_sf_swizzled_layout
|
||||
m,
|
||||
n,
|
||||
input.device,
|
||||
is_sf_swizzled_layout,
|
||||
padded_n=padded_n,
|
||||
)
|
||||
torch.ops._C.scaled_fp4_quant.out(
|
||||
input,
|
||||
|
||||
Reference in New Issue
Block a user