vllm.model_executor.layers.quantization.utils.fp8_utils
¶
Functions:
-
create_fp8_input_scale–Create input scale parameter for static activation quantization.
-
create_fp8_scale_parameter–Create scale parameter based on quantization strategy.
-
create_fp8_weight_parameter–Create FP8 weight parameter.
-
get_fp8_block_weight_scale–Return the block-FP8 weight scale for supported quant methods.
-
get_w8a8_block_fp8_configs–Return optimized configurations for the w8a8 block fp8 kernel.
-
input_to_float8–This function quantizes input values to float8 values "
-
per_token_group_quant_fp8–Function to perform per-token-group quantization on an input tensor
x. -
per_token_group_quant_fp8_packed_for_deepgemm–FP8 per-token-group quantization for DeepGEMM.
-
process_fp8_input_tensor_strategy_moe–Process moe input scales for tensor-wise quantization strategy.
-
process_fp8_weight_block_strategy–Normalize FNUZ if needed and return
(N, K)weight (no transpose). -
process_fp8_weight_channel_strategy–Normalize FNUZ if needed and return
(K, N)weight. -
process_fp8_weight_tensor_strategy–Requantize fused shards to one scale and return
(K, N)weight. -
process_fp8_weight_tensor_strategy_moe–Process moe weights for tensor-wise quantization strategy.
-
requant_weight_ue8m0_inplace–Re-quantise weight so that its per-block scaling factors are in the
-
silu_mul_per_token_group_quant_fp8_colmajor–Gated activation + block-fp8 quant.
alpha/betaselect the gate -
silu_mul_quant_fp8_packed_triton–Fuse activation and FP8 quantization, optionally skipping expert padding.
-
validate_fp8_block_shape–Validate block quantization shapes for tensor parallelism.
-
validate_fp8_block_shape_moe–Validate fused MoE block quantization shapes for tensor parallelism.
-
w8a8_triton_block_scaled_mm–This function performs matrix multiplication with block-wise
_maybe_pad_fp8_weight(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
_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, BLOCK)
¶
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(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, BLOCK)
¶
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(y_ptr, y_q_ptr, y_s_ptr, expert_ends_ptr, M, N, y_s_col_stride, eps, clamp_limit, alpha, beta, fp8_min, fp8_max, use_ue8m0, HAS_CLAMP, GROUP_SIZE, BLOCK_M, BLOCK_N, EXPERT_ALIGNMENT)
¶
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
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 | |
_upcast_e8m0_to_fp32(scale)
¶
Upcast E8M0 (exponent-only) scale to float32.
E8M0 stores only the 8-bit biased exponent (bias=127). To convert to float32 we place those 8 bits into the exponent field of an IEEE-754 float32 (bits 23-30) with sign=0 and mantissa=0.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
_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, BLOCK_SIZE_N, BLOCK_SIZE_K, GROUP_SIZE_M)
¶
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
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 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 | |
create_fp8_input_scale(output_partition_sizes, weight_loader)
¶
Create input scale parameter for static activation quantization.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
create_fp8_scale_parameter(parameter_type, output_partition_sizes, input_size_per_partition, block_size, weight_loader, scale_dtype=None)
¶
Create scale parameter based on quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
create_fp8_weight_parameter(output_size_per_partition, input_size_per_partition, weight_loader)
¶
Create FP8 weight parameter.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
get_fp8_block_weight_scale(layer)
¶
Return the block-FP8 weight scale for supported quant methods.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
get_w8a8_block_fp8_configs(N, K, block_n, block_k)
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(x, dtype=None)
¶
This function quantizes input values to float8 values " "with tensor-wise quantization.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
per_token_group_quant_fp8(x, group_size, eps=1e-10, dtype=None, column_major_scales=False, tma_aligned_scales=False, out_q=None, use_ue8m0=None)
¶
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.
Parameters:
-
(x¶Tensor) –The input tensor with ndim >= 2.
-
(group_size¶int) –The group size used for quantization.
-
(eps¶float, default:1e-10) –The minimum to avoid dividing zero.
-
(dtype¶dtype | None, default:None) –The dtype of output tensor. Note that only
torch.float8_e4m3fnis supported for now. -
(column_major_scales¶bool, default:False) –Outputs scales in column major.
-
(tma_aligned_scales¶bool, default:False) –Outputs scales in TMA-aligned layout.
-
(out_q¶Tensor | None, default:None) –Optional output tensor. If not provided, function will create.
-
(use_ue8m0¶bool | None, default:None) –If True, round each scale down to a power of two (UE8M0 format). None defers to the platform default.
Returns:
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | |
per_token_group_quant_fp8_packed_for_deepgemm(x, group_size, eps=1e-10, use_ue8m0=None, out_q=None)
¶
FP8 per-token-group quantization for DeepGEMM.
Returns:
-
tuple[Tensor, Tensor]–(x_q, x_s_packed) x_q: FP8 activations, same shape as
x. x_s_packed: Int32 tensor with logical shape [mn, ceil(num_groups_per_row / 4)], laid out with TMA-aligned stride along the packed-K dimension
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_input_tensor_strategy_moe(w13_input_scale, w2_input_scale, enable_eplb)
¶
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(weight, weight_scale)
¶
Normalize FNUZ if needed and return (N, K) weight (no transpose).
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_channel_strategy(weight, weight_scale, input_scale=None)
¶
Normalize FNUZ if needed and return (K, N) weight.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_tensor_strategy(weight, weight_scale, logical_widths, input_scale=None)
¶
Requantize fused shards to one scale and return (K, N) weight.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
process_fp8_weight_tensor_strategy_moe(weight, weight_scales, shard_size, num_experts, is_act_and_mul=True)
¶
Process moe weights for tensor-wise quantization strategy.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
requant_weight_ue8m0_inplace(weight, weight_scale, block_size=(128, 128))
¶
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:
-
(weight¶Tensor) –Block-quantised weight tensor stored in
torch.float8_e4m3fn. Expected shape(..., M, K). -
(weight_scale¶Tensor) –Corresponding per-block scale tensor (
torch.float32) with shape(..., M // block_size[0], K // block_size[1]). -
(block_size¶Sequence[int], default:(128, 128)) –2-element iterable
[block_m, block_k]describing the block quantisation granularity.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
silu_mul_per_token_group_quant_fp8_colmajor(input, output=None, use_ue8m0=None, eps=1e-10, clamp_limit=None, group_size=128, alpha=1.0, beta=0.0, *, expert_ends=None, expert_alignment=0)
¶
Gated activation + block-fp8 quant. alpha/beta select the gate
(silu: alpha=1, beta=0; swigluoai: alpha, beta from config).
Optional expert endpoints follow DeepGEMM's prefix-sum layout. Padding values and scales are left unwritten and must not be consumed as live rows.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
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 | |
silu_mul_quant_fp8_packed_triton(input, group_size=128, output_q=None, clamp_limit=None, alpha=1.0, beta=0.0, *, expert_ends=None, expert_alignment=0)
¶
Fuse activation and FP8 quantization, optionally skipping expert padding.
expert_ends uses DeepGEMM's prefix-sum layout: each entry is the
exclusive end of an expert's live rows, including earlier alignment gaps.
An expert starts at the preceding end rounded up to expert_alignment.
Padding outputs are left unwritten and must not be consumed as live rows.
Without expert_ends, the original dense launch configuration is used
and the metadata loads are compiled out.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
validate_fp8_block_shape(layer, input_size, output_size, input_size_per_partition, output_partition_sizes, block_size)
¶
Validate block quantization shapes for tensor parallelism.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
validate_fp8_block_shape_moe(intermediate_size_per_partition, block_size)
¶
Validate fused MoE block quantization shapes for tensor parallelism.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
w8a8_triton_block_scaled_mm(A, B, As, Bs, block_size, output_dtype=torch.float16)
¶
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.
Parameters:
-
(A¶Tensor) –The input tensor, e.g., activation.
-
(B¶Tensor) –The input tensor, e.g., weight.
-
(As¶Tensor) –The per-token-group quantization scale for
A. -
(Bs¶Tensor) –The per-block quantization scale for
B. -
(block_size¶list[int]) –The block size for per-block quantization. It should be 2-dim, e.g., [128, 128].
-
(output_dtype¶dtype, default:float16) –The dtype of the returned tensor.
Returns:
-
Tensor–torch.Tensor: The result of matmul.
Source code in vllm/model_executor/layers/quantization/utils/fp8_utils.py
961 962 963 964 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 1037 1038 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 | |