[Kernel][Helion] Add Helion kernel benchmark script (#48512)
Signed-off-by: Sean Chen <seachen@redhat.com>
This commit is contained in:
@@ -109,7 +109,18 @@ def baseline(
|
||||
scale: torch.Tensor, # [num_tokens, 1]
|
||||
scale_ub: torch.Tensor | None = None, # scalar tensor
|
||||
) -> None:
|
||||
torch.ops._C.dynamic_per_token_scaled_fp8_quant(result, input, scale, scale_ub)
|
||||
fp8_min, fp8_max = get_fp8_min_max()
|
||||
min_scaling_factor = 1.0 / (fp8_max * 512.0)
|
||||
|
||||
x = input.to(torch.float32)
|
||||
s = torch.amax(torch.abs(x), dim=-1, keepdim=True)
|
||||
if scale_ub is not None:
|
||||
s = s.clamp(max=scale_ub)
|
||||
s = (s * (1.0 / fp8_max)).clamp(min=min_scaling_factor)
|
||||
y = (x / s).clamp(fp8_min, fp8_max)
|
||||
|
||||
scale.copy_(s)
|
||||
result.copy_(y.to(result.dtype))
|
||||
|
||||
|
||||
# Overwrite autotune_baseline_atol and autotune_baseline_rtol
|
||||
|
||||
@@ -164,18 +164,17 @@ def baseline(
|
||||
dummy_is_scale_transposed: bool = False,
|
||||
dummy_is_tma_aligned: bool = False,
|
||||
) -> None:
|
||||
torch.ops._C.per_token_group_fp8_quant(
|
||||
input,
|
||||
output_q,
|
||||
output_s,
|
||||
group_size,
|
||||
eps,
|
||||
fp8_min,
|
||||
fp8_max,
|
||||
scale_ue8m0,
|
||||
dummy_is_scale_transposed,
|
||||
dummy_is_tma_aligned,
|
||||
)
|
||||
num_tokens, hidden_size = input.shape
|
||||
groups_per_row = hidden_size // group_size
|
||||
|
||||
x = input.view(num_tokens, groups_per_row, group_size).to(torch.float32)
|
||||
s = torch.clamp(torch.amax(torch.abs(x), dim=-1), min=eps) / fp8_max
|
||||
if scale_ue8m0:
|
||||
s = torch.exp2(torch.ceil(torch.log2(s)))
|
||||
y = torch.clamp(x / s[:, :, None], fp8_min, fp8_max)
|
||||
|
||||
output_s.copy_(s)
|
||||
output_q.copy_(y.view(num_tokens, hidden_size).to(output_q.dtype))
|
||||
|
||||
|
||||
@register_kernel(
|
||||
|
||||
@@ -131,9 +131,37 @@ def baseline(
|
||||
scale_ub: torch.Tensor | None = None, # []
|
||||
residual: torch.Tensor | None = None, # [num_tokens, hidden_size]
|
||||
) -> None:
|
||||
torch.ops._C.rms_norm_dynamic_per_token_quant(
|
||||
result, input, weight, scale, epsilon, scale_ub, residual
|
||||
)
|
||||
_, hidden_size = input.shape
|
||||
quant_dtype = result.dtype
|
||||
qtype_min: int | float
|
||||
qtype_max: int | float
|
||||
|
||||
if quant_dtype == torch.int8:
|
||||
qtype_min, qtype_max = get_int8_min_max()
|
||||
min_scaling_factor = get_int8_min_scaling_factor()
|
||||
else:
|
||||
qtype_min, qtype_max = get_fp8_min_max()
|
||||
min_scaling_factor = 1.0 / (qtype_max * 512.0)
|
||||
|
||||
x = input.to(torch.float32)
|
||||
if residual is not None:
|
||||
x = x + residual
|
||||
residual.copy_(x.to(residual.dtype))
|
||||
|
||||
rms = torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + epsilon)
|
||||
x_norm = (x * rms).to(input.dtype) * weight
|
||||
|
||||
s = torch.amax(torch.abs(x_norm), dim=-1, keepdim=True).to(torch.float32)
|
||||
if scale_ub is not None:
|
||||
s = s.clamp(max=scale_ub)
|
||||
s = (s * (1.0 / qtype_max)).clamp(min=min_scaling_factor)
|
||||
|
||||
y = x_norm / s
|
||||
if quant_dtype == torch.int8:
|
||||
y = y.round()
|
||||
|
||||
scale.copy_(s)
|
||||
result.copy_(y.clamp(qtype_min, qtype_max).to(result.dtype))
|
||||
|
||||
|
||||
# Overwrite autotune_baseline_atol and autotune_baseline_rtol
|
||||
|
||||
@@ -169,16 +169,40 @@ def baseline(
|
||||
group_size: int,
|
||||
is_scale_transposed: bool,
|
||||
) -> None:
|
||||
torch.ops._C.rms_norm_per_block_quant(
|
||||
result,
|
||||
input,
|
||||
weight,
|
||||
scale,
|
||||
epsilon,
|
||||
scale_ub,
|
||||
residual,
|
||||
group_size,
|
||||
is_scale_transposed,
|
||||
num_tokens, hidden_size = input.shape
|
||||
groups_per_row = hidden_size // group_size
|
||||
quant_dtype = result.dtype
|
||||
qtype_min: int | float
|
||||
qtype_max: int | float
|
||||
|
||||
if quant_dtype == torch.int8:
|
||||
qtype_min, qtype_max = get_int8_min_max()
|
||||
min_scaling_factor = get_int8_min_scaling_factor()
|
||||
else:
|
||||
qtype_min, qtype_max = get_fp8_min_max()
|
||||
min_scaling_factor = 1.0 / (qtype_max * 512.0)
|
||||
|
||||
x = input.to(torch.float32)
|
||||
if residual is not None:
|
||||
x = x + residual
|
||||
residual.copy_(x.to(residual.dtype))
|
||||
|
||||
rms = torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + epsilon)
|
||||
x_norm = (x * rms).to(input.dtype) * weight
|
||||
x_grouped = x_norm.view(num_tokens, groups_per_row, group_size).to(torch.float32)
|
||||
|
||||
s = torch.amax(torch.abs(x_grouped), dim=-1).to(torch.float32)
|
||||
if scale_ub is not None:
|
||||
s = s.clamp(max=scale_ub)
|
||||
s = (s * (1.0 / qtype_max)).clamp(min=min_scaling_factor)
|
||||
|
||||
y = x_grouped / s[:, :, None]
|
||||
if quant_dtype == torch.int8:
|
||||
y = y.round()
|
||||
|
||||
scale.copy_(s)
|
||||
result.copy_(
|
||||
y.clamp(qtype_min, qtype_max).view(num_tokens, hidden_size).to(result.dtype)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ from vllm.kernels.helion.utils import (
|
||||
get_int8_min_scaling_factor,
|
||||
)
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.activation import SiluAndMul
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
get_fp8_min_max,
|
||||
)
|
||||
@@ -150,8 +151,34 @@ def baseline(
|
||||
scale_ub: torch.Tensor | None = None, # scalar tensor
|
||||
is_scale_transposed: bool = False,
|
||||
) -> None:
|
||||
torch.ops._C.silu_and_mul_per_block_quant(
|
||||
out, input, scales, group_size, scale_ub, is_scale_transposed
|
||||
num_tokens, intermediate_size = out.shape
|
||||
groups_per_row = intermediate_size // group_size
|
||||
quant_dtype = out.dtype
|
||||
qtype_min: int | float
|
||||
qtype_max: int | float
|
||||
|
||||
if quant_dtype == torch.int8:
|
||||
qtype_min, qtype_max = get_int8_min_max()
|
||||
min_scaling_factor = get_int8_min_scaling_factor()
|
||||
else:
|
||||
qtype_min, qtype_max = get_fp8_min_max()
|
||||
min_scaling_factor = 1.0 / (qtype_max * 512.0)
|
||||
|
||||
act = SiluAndMul.forward_native(input.to(torch.float32))
|
||||
x_grouped = act.view(num_tokens, groups_per_row, group_size)
|
||||
|
||||
s = torch.amax(torch.abs(x_grouped), dim=-1)
|
||||
if scale_ub is not None:
|
||||
s = s.clamp(max=scale_ub)
|
||||
s = (s * (1.0 / qtype_max)).clamp(min=min_scaling_factor)
|
||||
|
||||
y = x_grouped / s[:, :, None]
|
||||
if quant_dtype == torch.int8:
|
||||
y = y.round()
|
||||
|
||||
scales.copy_(s)
|
||||
out.copy_(
|
||||
y.clamp(qtype_min, qtype_max).view(num_tokens, intermediate_size).to(out.dtype)
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user