Skip to content

Commit 1ca3421

Browse files
committed
Fix & linting
Signed-off-by: Zhanda <zhandazhu@gmail.com>
1 parent c523195 commit 1ca3421

3 files changed

Lines changed: 44 additions & 34 deletions

File tree

vllm/envs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ def get_vllm_port() -> int | None:
14561456
# average at BF16; 100 entries ≈ 0.9 GB.
14571457
"VLLM_POS_EMBED_CACHE_SIZE": lambda: int(
14581458
os.getenv("VLLM_POS_EMBED_CACHE_SIZE", "100")
1459+
),
14591460
# Controls whether to use FP8 attention for multimodal encoder (e.g., ViT)
14601461
"VLLM_MM_ENCODER_FP8_ATTN": lambda: bool(
14611462
int(os.getenv("VLLM_MM_ENCODER_FP8_ATTN", "0"))

vllm/model_executor/layers/attention/mm_encoder_attention.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _load_fp8_scales_file(path: str | None) -> dict[str, dict[str, float]]:
5353
if q is not None and k is not None and v is not None:
5454
scales[layer_name] = {"q": float(q), "k": float(k), "v": float(v)}
5555

56-
logger.info(f"Loaded FP8 attention scales from {path} ({len(scales)} layers)")
56+
logger.info("Loaded FP8 attention scales from %s (%d layers)", path, len(scales))
5757
return scales
5858

5959

@@ -180,12 +180,13 @@ def _init_fp8_attention(self, layer_name: str) -> None:
180180
self.skip_scale_q = self.fp8_scales["q"] == 1.0
181181
self.skip_scale_k = self.fp8_scales["k"] == 1.0
182182
self.skip_scale_v = self.fp8_scales["v"] == 1.0
183-
183+
184184
logger.debug(
185-
f"FP8 attention enabled for {layer_name}: "
186-
f"q={self.fp8_scales['q']:.4f}, "
187-
f"k={self.fp8_scales['k']:.4f}, "
188-
f"v={self.fp8_scales['v']:.4f}"
185+
"FP8 attention enabled for %s: q=%.4f, k=%.4f, v=%.4f",
186+
layer_name,
187+
self.fp8_scales["q"],
188+
self.fp8_scales["k"],
189+
self.fp8_scales["v"],
189190
)
190191

191192
@classmethod
@@ -292,7 +293,7 @@ def _quantize_to_fp8(
292293
skip_scale: bool = False,
293294
) -> torch.Tensor:
294295
"""Quantize a 3D (S, H, D) tensor to FP8.
295-
296+
296297
Uses QuantFP8 CustomOp when head_dim is aligned to 16; otherwise
297298
falls back to a stride-aware Triton kernel that pads head_dim to
298299
a multiple of 16 — no extra copy even for non-contiguous inputs.
@@ -307,9 +308,7 @@ def _quantize_to_fp8(
307308

308309
# QuantFP8 expects 2D input: (total_tokens, num_heads * head_dim)
309310
tensor_2d = tensor.reshape(orig_shape[0], -1)
310-
fp8_tensor, _ = self.fp8_quant.forward_cuda(
311-
tensor_2d, scale=scale
312-
)
311+
fp8_tensor, _ = self.fp8_quant.forward_cuda(tensor_2d, scale=scale)
313312
return fp8_tensor.reshape(orig_shape)
314313

315314
# Fall back to Triton kernel for padding head_dim to a multiple of 16
@@ -327,9 +326,15 @@ def _forward_flashinfer(
327326
) -> torch.Tensor:
328327
if self.fp8_enabled:
329328
assert self.fp8_quant is not None and self.fp8_scales is not None
330-
query = self._quantize_to_fp8(query, self._fp8_q_scale, skip_scale=self.skip_scale_q)
331-
key = self._quantize_to_fp8(key, self._fp8_k_scale, skip_scale=self.skip_scale_k)
332-
value = self._quantize_to_fp8(value, self._fp8_v_scale, skip_scale=self.skip_scale_v)
329+
query = self._quantize_to_fp8(
330+
query, self._fp8_q_scale, skip_scale=self.skip_scale_q
331+
)
332+
key = self._quantize_to_fp8(
333+
key, self._fp8_k_scale, skip_scale=self.skip_scale_k
334+
)
335+
value = self._quantize_to_fp8(
336+
value, self._fp8_v_scale, skip_scale=self.skip_scale_v
337+
)
333338

334339
output = vit_flashinfer_wrapper(
335340
q=query,
@@ -348,7 +353,7 @@ def _forward_flashinfer(
348353

349354
# Un-pad head dimension if it was padded during FP8 quantization
350355
if self.fp8_enabled and output.shape[-1] != self.head_size:
351-
output = output[..., :self.head_size]
356+
output = output[..., : self.head_size]
352357
output = output.contiguous()
353358

354359
return output

vllm/model_executor/layers/quantization/input_quant_fp8.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
33

44
import os
5+
56
import torch
67
import 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

Comments
 (0)