|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import argparse |
| 4 | +import copy |
| 5 | +import itertools |
| 6 | + |
| 7 | +import torch |
| 8 | +from weight_shapes import WEIGHT_SHAPES |
| 9 | + |
| 10 | +from vllm._custom_ops import cutlass_scaled_mm as vllm_scaled_mm |
| 11 | +from vllm._custom_ops import scaled_int8_quant as vllm_scaled_int8_quant |
| 12 | +from vllm.triton_utils import triton |
| 13 | + |
| 14 | + |
| 15 | +@triton.testing.perf_report( |
| 16 | + triton.testing.Benchmark( |
| 17 | + x_names=["batch_size"], |
| 18 | + x_vals=[1, 16, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384], |
| 19 | + x_log=False, |
| 20 | + line_arg="provider", |
| 21 | + line_vals=[ |
| 22 | + "torch-bf16", |
| 23 | + # "int8-tensor-w-token-a", |
| 24 | + "int8-tensor-w-tensor-a", |
| 25 | + "int8-channel-w-token-a", |
| 26 | + # "int8-channel-w-tensor-a", |
| 27 | + # "int8-tensor-w-token-a-noquant", |
| 28 | + "int8-tensor-w-tensor-a-noquant", |
| 29 | + "int8-channel-w-token-a-noquant", |
| 30 | + # "int8-channel-w-tensor-a-noquant", |
| 31 | + ], |
| 32 | + line_names=[ |
| 33 | + "torch-bf16", |
| 34 | + # "int8-tensor-w-token-a", |
| 35 | + "int8-tensor-w-tensor-a", |
| 36 | + "int8-channel-w-token-a", |
| 37 | + # "int8-channel-w-tensor-a", |
| 38 | + # "int8-tensor-w-token-a-noquant", |
| 39 | + "int8-tensor-w-tensor-a-noquant", |
| 40 | + "int8-channel-w-token-a-noquant", |
| 41 | + # "int8-channel-w-tensor-a-noquant", |
| 42 | + ], |
| 43 | + ylabel="TFLOP/s (larger is better)", |
| 44 | + plot_name="BF16 vs INT8 GEMMs", |
| 45 | + args={}, |
| 46 | + ) |
| 47 | +) |
| 48 | +def benchmark(batch_size, provider, N, K): |
| 49 | + M = batch_size |
| 50 | + device = "cuda" |
| 51 | + dtype = torch.bfloat16 |
| 52 | + a = torch.randn((M, K), device=device, dtype=dtype) |
| 53 | + b = torch.randn((N, K), device=device, dtype=dtype) |
| 54 | + |
| 55 | + quantiles = [0.5, 0.2, 0.8] |
| 56 | + |
| 57 | + if "torch-bf16" in provider: |
| 58 | + ms, min_ms, max_ms = triton.testing.do_bench_cudagraph( |
| 59 | + lambda: torch.nn.functional.linear(a, b), quantiles=quantiles |
| 60 | + ) |
| 61 | + |
| 62 | + elif "int8" in provider: |
| 63 | + # Weights are always quantized ahead of time |
| 64 | + if "noquant" in provider: |
| 65 | + # For "no quant", we don't measure the time for activations |
| 66 | + if "tensor-w-token-a" in provider: |
| 67 | + # Dynamic per-token quant for A, static per-tensor quant for B |
| 68 | + scale_b = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 69 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b, scale_b) |
| 70 | + assert scale_b_int8.numel() == 1 |
| 71 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a) |
| 72 | + |
| 73 | + elif "tensor-w-tensor-a" in provider: |
| 74 | + # Static per-tensor quantization with fixed scales for both A and B |
| 75 | + scale_a = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 76 | + scale_b = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 77 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b, scale_b) |
| 78 | + assert scale_b_int8.numel() == 1 |
| 79 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a, scale_a) |
| 80 | + |
| 81 | + elif "channel-w-token-a" in provider: |
| 82 | + # Dynamic per-channel quantization for weights, per-token quant for A |
| 83 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b) |
| 84 | + assert scale_b_int8.numel() == N |
| 85 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a) |
| 86 | + |
| 87 | + elif "channel-w-tensor-a" in provider: |
| 88 | + # Dynamic per-channel quantization for weights, per-tensor quant for A |
| 89 | + scale_a = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 90 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b) |
| 91 | + assert scale_b_int8.numel() == N |
| 92 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a, scale_a) |
| 93 | + |
| 94 | + def run_quant(): |
| 95 | + return vllm_scaled_mm(a_int8, b_int8, scale_a_int8, scale_b_int8, dtype) |
| 96 | + |
| 97 | + else: |
| 98 | + # Quantize the activations during the GEMM call |
| 99 | + if "tensor-w-token-a" in provider: |
| 100 | + # Dynamic per-token quant for A, static per-tensor quant for B |
| 101 | + scale_b = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 102 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b, scale_b) |
| 103 | + assert scale_b_int8.numel() == 1 |
| 104 | + |
| 105 | + def run_quant(): |
| 106 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a) |
| 107 | + return vllm_scaled_mm( |
| 108 | + a_int8, b_int8, scale_a_int8, scale_b_int8, dtype |
| 109 | + ) |
| 110 | + |
| 111 | + elif "tensor-w-tensor-a" in provider: |
| 112 | + # Static per-tensor quantization with fixed scales for both A and B |
| 113 | + scale_a = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 114 | + scale_b = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 115 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b, scale_b) |
| 116 | + assert scale_b_int8.numel() == 1 |
| 117 | + |
| 118 | + def run_quant(): |
| 119 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a, scale_a) |
| 120 | + return vllm_scaled_mm( |
| 121 | + a_int8, b_int8, scale_a_int8, scale_b_int8, dtype |
| 122 | + ) |
| 123 | + |
| 124 | + elif "channel-w-token-a" in provider: |
| 125 | + # Dynamic per-channel quant for weights, per-token quant for A |
| 126 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b) |
| 127 | + assert scale_b_int8.numel() == N |
| 128 | + |
| 129 | + def run_quant(): |
| 130 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a) |
| 131 | + return vllm_scaled_mm( |
| 132 | + a_int8, b_int8, scale_a_int8, scale_b_int8, dtype |
| 133 | + ) |
| 134 | + |
| 135 | + elif "channel-w-tensor-a" in provider: |
| 136 | + # Dynamic per-channel quant for weights, static per-tensor quant for A |
| 137 | + scale_a = torch.tensor([1.0], device=device, dtype=torch.float32) |
| 138 | + b_int8, scale_b_int8, _ = vllm_scaled_int8_quant(b) |
| 139 | + assert scale_b_int8.numel() == N |
| 140 | + |
| 141 | + def run_quant(): |
| 142 | + a_int8, scale_a_int8, _ = vllm_scaled_int8_quant(a, scale_a) |
| 143 | + return vllm_scaled_mm( |
| 144 | + a_int8, b_int8, scale_a_int8, scale_b_int8, dtype |
| 145 | + ) |
| 146 | + |
| 147 | + b_int8 = b_int8.t() |
| 148 | + |
| 149 | + ms, min_ms, max_ms = triton.testing.do_bench_cudagraph( |
| 150 | + lambda: run_quant(), quantiles=quantiles |
| 151 | + ) |
| 152 | + |
| 153 | + # Calculate TFLOP/s, two flops per multiply-add |
| 154 | + tflops = lambda ms: (2 * M * N * K) * 1e-12 / (ms * 1e-3) |
| 155 | + return tflops(ms), tflops(max_ms), tflops(min_ms) |
| 156 | + |
| 157 | + |
| 158 | +def prepare_shapes(args): |
| 159 | + KN_model_names = [] |
| 160 | + models_tps = list(itertools.product(args.models, args.tp_sizes)) |
| 161 | + for model, tp_size in models_tps: |
| 162 | + assert model in WEIGHT_SHAPES |
| 163 | + for KN, tp_split_dim in copy.deepcopy(WEIGHT_SHAPES[model]): |
| 164 | + KN[tp_split_dim] = KN[tp_split_dim] // tp_size |
| 165 | + KN.append(model) |
| 166 | + KN_model_names.append(KN) |
| 167 | + return KN_model_names |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + parser = argparse.ArgumentParser() |
| 172 | + parser.add_argument( |
| 173 | + "--models", |
| 174 | + nargs="+", |
| 175 | + type=str, |
| 176 | + default=["meta-llama/Llama-3.1-8B-Instruct"], |
| 177 | + choices=[*WEIGHT_SHAPES.keys()], |
| 178 | + help="List of models to benchmark", |
| 179 | + ) |
| 180 | + parser.add_argument( |
| 181 | + "--tp-sizes", |
| 182 | + nargs="+", |
| 183 | + type=int, |
| 184 | + default=[1], |
| 185 | + help="List of tensor parallel sizes", |
| 186 | + ) |
| 187 | + args = parser.parse_args() |
| 188 | + |
| 189 | + KN_model_names = prepare_shapes(args) |
| 190 | + for K, N, model_name in KN_model_names: |
| 191 | + print(f"{model_name}, N={N} K={K}, BF16 vs INT8 GEMMs TFLOP/s:") |
| 192 | + benchmark.run( |
| 193 | + print_data=True, |
| 194 | + show_plots=True, |
| 195 | + save_path=f"bench_int8_res_n{N}_k{K}", |
| 196 | + N=N, |
| 197 | + K=K, |
| 198 | + ) |
| 199 | + |
| 200 | + print("Benchmark finished!") |
0 commit comments