22# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33
44import os
5+
56import torch
67import torch .nn .functional as F
78
@@ -33,7 +34,7 @@ def _quantize_pad_fp8_kernel(
3334 stride_yh , # output stride along head dim
3435 stride_yd , # output stride along head_dim dim (usually 1)
3536 num_heads ,
36- n_rows , # total rows = S * H
37+ n_rows , # total rows = S * H
3738 n_cols ,
3839 n_cols_padded ,
3940 fp8_min ,
@@ -56,10 +57,12 @@ def _quantize_pad_fp8_kernel(
5657 s = offs_m // num_heads
5758 h = offs_m % num_heads
5859
59- x_ptrs = (x_ptr
60- + s [:, None ] * stride_xs
61- + h [:, None ] * stride_xh
62- + offs_n [None , :] * stride_xd )
60+ x_ptrs = (
61+ x_ptr
62+ + s [:, None ] * stride_xs
63+ + h [:, None ] * stride_xh
64+ + offs_n [None , :] * stride_xd
65+ )
6366 x = tl .load (x_ptrs , mask = mask_in , other = 0.0 ).to (tl .float32 )
6467 if SKIP_SCALE :
6568 x_q = x
@@ -69,10 +72,12 @@ def _quantize_pad_fp8_kernel(
6972 x_q = tl .where (mask_in , x_q , 0.0 )
7073 x_q = tl .clamp (x_q , fp8_min , fp8_max ).to (y_ptr .dtype .element_ty )
7174
72- y_ptrs = (y_ptr
73- + s [:, None ] * stride_ys
74- + h [:, None ] * stride_yh
75- + offs_n [None , :] * stride_yd )
75+ y_ptrs = (
76+ y_ptr
77+ + s [:, None ] * stride_ys
78+ + h [:, None ] * stride_yh
79+ + offs_n [None , :] * stride_yd
80+ )
7681 tl .store (y_ptrs , x_q , mask = mask_out )
7782
7883
@@ -107,24 +112,20 @@ def quantize_fp8_pad_head_dim_triton(
107112 num_warps : int | None = None ,
108113 block_m : int | None = None ,
109114) -> torch .Tensor :
110- """Quantize a 4D (B, S, H, D) or 3D (S, H, D) tensor to FP8 while padding D to a multiple of 16.
115+ """Quantize a 3D/ 4D tensor to FP8, padding head_dim to a multiple of 16.
111116
112117 Reads directly from the input using its 3D strides, so non-contiguous
113118 views (e.g. Q/K/V slices from an interleaved QKV buffer) are handled
114119 without an extra copy. Output is always a fresh contiguous tensor
115120 with shape (S, H, padded_D).
116121 """
117122 if not HAS_TRITON :
118- raise RuntimeError (
119- "Triton is required to quantize with head_dim padding."
120- )
123+ raise RuntimeError ("Triton is required to quantize with head_dim padding." )
121124
122125 original_shape = tensor .shape
123126 if tensor .dim () == 4 :
124127 tensor = tensor .view (- 1 , tensor .shape [- 2 ], tensor .shape [- 1 ])
125- assert tensor .dim () == 3 , (
126- f"Expected 3D input (S, H, D), got { tensor .dim ()} D"
127- )
128+ assert tensor .dim () == 3 , f"Expected 3D input (S, H, D), got { tensor .dim ()} D"
128129 S , H , D = tensor .shape
129130 padded_head_dim = (D + 15 ) // 16 * 16
130131 out_dtype = current_platform .fp8_dtype ()
@@ -141,15 +142,18 @@ def quantize_fp8_pad_head_dim_triton(
141142 if block_n is None or num_warps is None or block_m is None :
142143 block_n , num_warps , block_m = _get_fp8_pad_quant_config (padded_head_dim )
143144
144- grid = (triton .cdiv (n_rows , block_m ),
145- triton .cdiv (padded_head_dim , block_n ))
145+ grid = (triton .cdiv (n_rows , block_m ), triton .cdiv (padded_head_dim , block_n ))
146146
147147 _quantize_pad_fp8_kernel [grid ](
148148 tensor ,
149149 output ,
150150 scale_1d ,
151- tensor .stride (0 ), tensor .stride (1 ), tensor .stride (2 ),
152- output .stride (0 ), output .stride (1 ), output .stride (2 ),
151+ tensor .stride (0 ),
152+ tensor .stride (1 ),
153+ tensor .stride (2 ),
154+ output .stride (0 ),
155+ output .stride (1 ),
156+ output .stride (2 ),
153157 H ,
154158 n_rows ,
155159 D ,
0 commit comments