|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import itertools |
| 4 | +from typing import Callable |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from vllm import _custom_ops as ops |
| 9 | +from vllm.config import CompilationConfig, VllmConfig, set_current_vllm_config |
| 10 | +from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 |
| 11 | +from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape |
| 12 | +from vllm.triton_utils import triton |
| 13 | + |
| 14 | + |
| 15 | +# TODO(luka): use standalone_compile utility |
| 16 | +def with_dyn_arg(fn: Callable, arg_index: int, dim_index: int): |
| 17 | + def inner(*args): |
| 18 | + torch._dynamo.mark_dynamic(args[arg_index], dim_index) |
| 19 | + return fn(*args) |
| 20 | + |
| 21 | + return inner |
| 22 | + |
| 23 | + |
| 24 | +torch._dynamo.config.recompile_limit = 8888 |
| 25 | +compilation_config = CompilationConfig(custom_ops=["none"]) |
| 26 | +with set_current_vllm_config(VllmConfig(compilation_config=compilation_config)): |
| 27 | + torch_per_token_quant_fp8 = torch.compile( |
| 28 | + QuantFP8(False, GroupShape.PER_TOKEN), |
| 29 | + fullgraph=True, |
| 30 | + dynamic=False, # recompile for different shapes |
| 31 | + ) |
| 32 | + |
| 33 | + # First dim is explicitly dynamic to simulate vLLM usage |
| 34 | + torch_per_token_quant_fp8 = with_dyn_arg(torch_per_token_quant_fp8, 0, 0) |
| 35 | + |
| 36 | + |
| 37 | +def cuda_per_token_quant_fp8( |
| 38 | + input: torch.Tensor, |
| 39 | +) -> tuple[torch.Tensor, torch.Tensor]: |
| 40 | + return ops.scaled_fp8_quant(input) |
| 41 | + |
| 42 | + |
| 43 | +def calculate_diff(batch_size: int, seq_len: int): |
| 44 | + """Calculate difference between Triton and CUDA implementations.""" |
| 45 | + device = torch.device("cuda") |
| 46 | + x = torch.rand((batch_size * seq_len, 4096), dtype=torch.float16, device=device) |
| 47 | + |
| 48 | + torch_out, torch_scale = torch_per_token_quant_fp8(x) |
| 49 | + cuda_out, cuda_scale = cuda_per_token_quant_fp8(x) |
| 50 | + |
| 51 | + if torch.allclose( |
| 52 | + cuda_out.to(torch.float32), torch_out.to(torch.float32), rtol=1e-3, atol=1e-5 |
| 53 | + ) and torch.allclose(cuda_scale, torch_scale, rtol=1e-3, atol=1e-5): |
| 54 | + print("✅ All implementations match") |
| 55 | + else: |
| 56 | + print("❌ Implementations differ") |
| 57 | + |
| 58 | + |
| 59 | +batch_size_range = [1, 16, 32, 64, 128] |
| 60 | +seq_len_range = [1, 16, 64, 128, 256, 512, 1024, 2048, 4096] |
| 61 | + |
| 62 | +configs = list(itertools.product(batch_size_range, seq_len_range)) |
| 63 | + |
| 64 | + |
| 65 | +@triton.testing.perf_report( |
| 66 | + triton.testing.Benchmark( |
| 67 | + x_names=["batch_size", "seq_len"], |
| 68 | + x_vals=configs, |
| 69 | + line_arg="provider", |
| 70 | + line_vals=["torch", "cuda"], |
| 71 | + line_names=["Torch", "CUDA"], |
| 72 | + styles=[("blue", "-"), ("green", "-")], |
| 73 | + ylabel="us", |
| 74 | + plot_name="per-token-dynamic-quant-fp8-performance", |
| 75 | + args={}, |
| 76 | + ) |
| 77 | +) |
| 78 | +def benchmark_quantization(batch_size, seq_len, provider): |
| 79 | + dtype = torch.float16 |
| 80 | + device = torch.device("cuda") |
| 81 | + |
| 82 | + x = torch.randn(batch_size * seq_len, 4096, device=device, dtype=dtype) |
| 83 | + |
| 84 | + quantiles = [0.5, 0.2, 0.8] |
| 85 | + |
| 86 | + if provider == "torch": |
| 87 | + fn = lambda: torch_per_token_quant_fp8(x.clone()) |
| 88 | + elif provider == "cuda": |
| 89 | + fn = lambda: cuda_per_token_quant_fp8(x.clone()) |
| 90 | + |
| 91 | + ms, min_ms, max_ms = triton.testing.do_bench_cudagraph(fn, quantiles=quantiles) |
| 92 | + |
| 93 | + return 1000 * ms, 1000 * max_ms, 1000 * min_ms |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + calculate_diff(batch_size=4, seq_len=4096) |
| 98 | + benchmark_quantization.run(print_data=True) |
0 commit comments