vllm.model_executor.layers.quantization.utils.fp8_utils ¶
W8A8BlockFp8LinearOp ¶
This class executes a Blocked FP8 linear layer using cutlass if supported and torch.scaled_mm otherwise.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
deepgemm_input_quant_op instance-attribute ¶
deepgemm_input_quant_op = (
QuantFP8(
False,
act_quant_group_shape,
column_major_scales=True,
use_ue8m0=use_deep_gemm_e8m0,
)
if is_deep_gemm_supported
else None
)
is_flashinfer_supported instance-attribute ¶
is_flashinfer_supported = (
is_flashinfer_fp8_blockscale_gemm_supported()
)
__init__ ¶
__init__(
weight_group_shape: GroupShape,
act_quant_group_shape: GroupShape,
cutlass_block_fp8_supported: bool = CUTLASS_BLOCK_FP8_SUPPORTED,
use_aiter_and_is_supported: bool = False,
)
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_dispatch_w8a8_blockscale_op ¶
_dispatch_w8a8_blockscale_op(
use_cutlass: bool, use_aiter_and_is_supported: bool
) -> tuple[
Callable[
[Tensor, Tensor, Tensor, Tensor | None], Tensor
],
QuantFP8 | None,
]
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_run_aiter ¶
_run_aiter(
input_2d: Tensor,
weight: Tensor,
weight_scale: Tensor,
input_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_run_cutlass ¶
_run_cutlass(
input_2d: Tensor,
weight: Tensor,
weight_scale: Tensor,
input_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_run_deepgemm ¶
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_run_flashinfer ¶
Run FlashInfer FP8 block-scale GEMM.
This backend uses TensorRT-LLM's FP8 block-scale GEMM kernels and supports FP8+FP8 (W8A8 full quantization) on SM90+ (Hopper).
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_run_triton ¶
_run_triton(
input_2d: Tensor,
weight: Tensor,
weight_scale: Tensor,
input_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
apply ¶
apply(
input: Tensor,
weight: Tensor,
weight_scale: Tensor,
input_scale: Tensor | None = None,
bias: Tensor | None = None,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_flashinfer_fp8_blockscale_gemm_fake ¶
_flashinfer_fp8_blockscale_gemm_fake(
input: Tensor,
weight: Tensor,
weight_scale: Tensor,
group_size: int,
use_deep_gemm_e8m0: bool,
) -> Tensor
Required fake/meta implementation for torch.compile graph tracing.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_flashinfer_fp8_blockscale_gemm_impl ¶
_flashinfer_fp8_blockscale_gemm_impl(
input: Tensor,
weight: Tensor,
weight_scale: Tensor,
group_size: int,
use_deep_gemm_e8m0: bool,
) -> Tensor
Conditional FlashInfer FP8 blockscale GEMM with batch-size-dependent selection.
This function switches between two optimized kernels based on the input batch size: - For small batches (M < 32): Uses FlashInfer's DeepGEMM swapAB optimization. - For larger batches (M >= 32): Uses the official DeepGEMM kernel.
The conditional logic must use torch.cond() instead of a simple if-else statement to maintain compatibility with torch.compile graph compilation.
This batch-size-dependent selection is essential for maintaining model accuracy. Benchmarks on GSM8K show a significant accuracy gap (88% vs 95%) for DeepSeek-V3.1 when using FlashInfer's DeepGEMM on M>=32. The M < 32 strategy fixes the accurracy drop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input | Tensor | Input tensor of shape (batch_size, input_dim) in FP8 format | required |
weight | Tensor | Weight tensor of shape (output_dim, input_dim) in FP8 format | required |
weight_scale | Tensor | Scale factors for weight quantization (per-group) | required |
group_size | int | Quantization group size for the weight tensor | required |
use_deep_gemm_e8m0 | bool | Whether to use the E8M0 format in DeepGEMM quantization | required |
Returns:
| Type | Description |
|---|---|
Tensor | Output tensor of shape (batch_size, output_dim) in bfloat16 format |
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
_fp8_gemm_nt_op ¶
_fp8_gemm_nt_op(
q_input: Tensor,
input_scale: Tensor,
weight: Tensor,
weight_scale: Tensor,
output: Tensor,
use_deep_gemm_e8m0: bool,
) -> None
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_fp8_gemm_nt_op_fake ¶
_fp8_gemm_nt_op_fake(
q_input: Tensor,
input_scale: Tensor,
weight: Tensor,
weight_scale: Tensor,
output: Tensor,
use_deep_gemm_e8m0: bool,
) -> None
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_maybe_pad_fp8_weight ¶
Pad the weight tensor. This is an optimization on ROCm platform, which can benefit from tensors located far enough from one another in memory
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_padded_cutlass ¶
_padded_cutlass(
qx: Tensor,
weight: Tensor,
x_scale: Tensor,
weight_scale: Tensor,
block_size: list[int],
output_dtype: dtype,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_padded_cutlass_fake ¶
_padded_cutlass_fake(
qx: Tensor,
weight: Tensor,
x_scale: Tensor,
weight_scale: Tensor,
block_size: list[int],
output_dtype: dtype,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_per_token_group_quant_fp8 ¶
_per_token_group_quant_fp8(
y_ptr,
y_q_ptr,
y_s_ptr,
group_size,
y_num_columns,
y_row_stride,
eps,
fp8_min,
fp8_max,
use_ue8m0: constexpr,
BLOCK: constexpr,
)
A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into float8 values.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_per_token_group_quant_fp8_colmajor ¶
_per_token_group_quant_fp8_colmajor(
y_ptr,
y_q_ptr,
y_s_ptr,
group_size,
y_num_columns,
y_row_stride,
y_s_col_stride,
eps,
fp8_min,
fp8_max,
use_ue8m0: constexpr,
BLOCK: constexpr,
)
A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into float8 values.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_silu_mul_per_token_group_quant_fp8_colmajor ¶
_silu_mul_per_token_group_quant_fp8_colmajor(
y_ptr,
y_q_ptr,
y_s_ptr,
M,
N,
y_s_col_stride: int64,
eps,
fp8_min,
fp8_max,
use_ue8m0: constexpr,
GROUP_SIZE: constexpr,
BLOCK_M: constexpr,
BLOCK_N: constexpr,
)
Each thread block (BLOCK_N) computes [BLOCK_M, GROUP_SIZE] act-mul outputs. Then the thread block quantizes the [BLOCK_M, GROUP_SIZE] block of values and fills the outputs tensors at the right positions.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_triton_per_token_group_quant_fp8_fake ¶
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_triton_per_token_group_quant_fp8_impl ¶
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_w8a8_triton_block_scaled_mm ¶
_w8a8_triton_block_scaled_mm(
A,
B,
C,
As,
Bs,
M,
N,
K,
group_n,
group_k,
stride_am,
stride_ak,
stride_bk,
stride_bn,
stride_cm,
stride_cn,
stride_As_m,
stride_As_k,
stride_Bs_k,
stride_Bs_n,
BLOCK_SIZE_M: constexpr,
BLOCK_SIZE_N: constexpr,
BLOCK_SIZE_K: constexpr,
GROUP_SIZE_M: constexpr,
)
Triton-accelerated function used to perform linear operations (dot product) on input tensors A and B with block-wise quantization, and store the result in output tensor C.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 | |
_w8a8_triton_block_scaled_mm_fake ¶
_w8a8_triton_block_scaled_mm_fake(
qx: Tensor,
weight: Tensor,
x_scale: Tensor,
weight_scale: Tensor,
block_size: list[int],
output_dtype: dtype,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_w8a8_triton_block_scaled_mm_func ¶
_w8a8_triton_block_scaled_mm_func(
qx: Tensor,
weight: Tensor,
x_scale: Tensor,
weight_scale: Tensor,
block_size: list[int],
output_dtype: dtype,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
create_fp8_input_scale ¶
create_fp8_input_scale(
output_partition_sizes: list[int],
weight_loader: Callable | None,
) -> Parameter
Create input scale parameter for static activation quantization.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
create_fp8_scale_parameter ¶
create_fp8_scale_parameter(
parameter_type: Parameter,
output_partition_sizes: list[int],
input_size_per_partition: int,
block_size: list[int] | None,
weight_loader: Callable | None,
) -> Parameter
Create scale parameter based on quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
create_fp8_weight_parameter ¶
create_fp8_weight_parameter(
output_size_per_partition: int,
input_size_per_partition: int,
weight_loader: Callable | None,
) -> Parameter
Create FP8 weight parameter.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
cutlass_scaled_mm ¶
cutlass_scaled_mm(
A: Tensor,
B: Tensor,
As: Tensor,
Bs: Tensor,
block_size: list[int],
output_dtype: dtype = float16,
) -> Tensor
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
deepgemm_post_process_fp8_weight_block ¶
deepgemm_post_process_fp8_weight_block(
wq: Tensor,
ws: Tensor,
quant_block_shape: tuple[int],
use_e8m0: bool,
) -> tuple[Tensor, Tensor]
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
get_w8a8_block_fp8_configs cached ¶
Return optimized configurations for the w8a8 block fp8 kernel. The return value will be a dictionary that maps an irregular grid of batch sizes to configurations of the w8a8 block fp8 kernel. To evaluate the kernel on a given batch size bs, the closest batch size in the grid should be picked and the associated configuration chosen to invoke the kernel.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
input_to_float8 ¶
This function quantizes input values to float8 values " "with tensor-wise quantization.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
is_fp8 ¶
maybe_post_process_fp8_weight_block ¶
maybe_post_process_fp8_weight_block(layer: Module)
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
per_token_group_quant_fp8 ¶
per_token_group_quant_fp8(
x: Tensor,
group_size: int,
eps: float = 1e-10,
dtype: dtype | None = None,
column_major_scales: bool = False,
out_q: Tensor | None = None,
use_ue8m0: bool | None = None,
) -> tuple[Tensor, Tensor]
Function to perform per-token-group quantization on an input tensor x. It converts the tensor values into signed float8 values and returns the quantized tensor along with the scaling factor used for quantization. Args: x: The input tensor with ndim >= 2. group_size: The group size used for quantization. eps: The minimum to avoid dividing zero. dtype: The dype of output tensor. Note that only torch.float8_e4m3fn is supported for now. column_major_scales: Outputs scales in column major. out_q: Optional output tensor. If not provided, function will create. Returns: tuple[torch.Tensor, torch.Tensor]: The quantized tensor and the scaling factor.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | |
per_token_group_quant_fp8_packed_for_deepgemm ¶
per_token_group_quant_fp8_packed_for_deepgemm(
x: Tensor,
group_size: int,
eps: float = 1e-10,
use_ue8m0: bool | None = None,
out_q: Tensor | None = None,
) -> tuple[Tensor, Tensor]
FP8 per-token-group quantization for DeepGEMM.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor] | (x_q, x_s_packed) x_q: FP8 activations, same shape as |
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 | |
prepare_fp8_moe_layer_for_deepgemm ¶
prepare_fp8_moe_layer_for_deepgemm(
w13: Tensor,
w2: Tensor,
w13_scale: Tensor,
w2_scale: Tensor,
block_shape: tuple[int],
)
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_input_tensor_strategy_moe ¶
process_fp8_input_tensor_strategy_moe(
w13_input_scale: Tensor, w2_input_scale: Tensor
) -> tuple[Tensor, Tensor]
Process moe input scales for tensor-wise quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_block_strategy ¶
Process weights for block-wise quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_channel_strategy ¶
process_fp8_weight_channel_strategy(
weight: Tensor,
weight_scale: Tensor,
input_scale: Tensor | None = None,
) -> tuple[Tensor, Tensor, Tensor | None]
Process weights for channel-wise quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_tensor_strategy ¶
process_fp8_weight_tensor_strategy(
weight: Tensor,
weight_scale: Tensor,
logical_widths: list[int],
input_scale: Tensor | None = None,
) -> tuple[Tensor, Tensor, Tensor | None]
Process weights for tensor-wise quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_tensor_strategy_moe ¶
process_fp8_weight_tensor_strategy_moe(
weight: Tensor,
weight_scales: Tensor,
shard_size: int,
num_experts: int,
is_act_and_mul: bool = True,
) -> tuple[Tensor, Tensor]
Process moe weights for tensor-wise quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
requant_weight_ue8m0_inplace ¶
requant_weight_ue8m0_inplace(
weight: Tensor,
weight_scale: Tensor,
block_size: Sequence[int] = (128, 128),
) -> None
Re-quantise weight so that its per-block scaling factors are in the UE8M0 (power-of-two) format expected by the new DeepGEMM kernels inplace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight | Tensor | Block-quantised weight tensor stored in | required |
weight_scale | Tensor | Corresponding per-block scale tensor ( | required |
block_size | Sequence[int] | 2-element iterable | (128, 128) |
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
silu_mul_per_token_group_quant_fp8_colmajor ¶
silu_mul_per_token_group_quant_fp8_colmajor(
input: Tensor,
output: Tensor | None = None,
use_ue8m0: bool | None = None,
eps: float = 1e-10,
)
silu+mul + block-fp8 quant with group size 128.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
validate_fp8_block_shape ¶
validate_fp8_block_shape(
layer: Module,
input_size: int,
output_size: int,
input_size_per_partition: int,
output_partition_sizes: list[int],
block_size: list[int],
) -> None
Validate block quantization shapes for tensor parallelism.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
w8a8_triton_block_scaled_mm ¶
w8a8_triton_block_scaled_mm(
A: Tensor,
B: Tensor,
As: Tensor,
Bs: Tensor,
block_size: list[int],
output_dtype: dtype = float16,
) -> Tensor
This function performs matrix multiplication with block-wise quantization. It takes two input tensors A and B with scales As and Bs. The output is returned in the specified output_dtype. Args: A: The input tensor, e.g., activation. B: The input tensor, e.g., weight. As: The per-token-group quantization scale for A. Bs: The per-block quantization scale for B. block_size: The block size for per-block quantization. It should be 2-dim, e.g., [128, 128]. output_dytpe: The dtype of the returned tensor. Returns: torch.Tensor: The result of matmul.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 | |