|
| 1 | +""" |
| 2 | +Inference benchmarking tool. |
| 3 | +
|
| 4 | +Requirements: |
| 5 | + transformers |
| 6 | + accelerate |
| 7 | + bitsandbytes |
| 8 | + optimum-benchmark |
| 9 | +
|
| 10 | +Usage: python inference_benchmark.py model_id |
| 11 | +
|
| 12 | +options: |
| 13 | + -h, --help show this help message and exit |
| 14 | + --configs {bf16,fp16,nf4,nf4-dq,int8,int8-decomp} [{bf16,fp16,nf4,nf4-dq,int8,int8-decomp} ...] |
| 15 | + --bf16 |
| 16 | + --fp16 |
| 17 | + --nf4 |
| 18 | + --nf4-dq |
| 19 | + --int8 |
| 20 | + --int8-decomp |
| 21 | + --batches BATCHES [BATCHES ...] |
| 22 | + --input-length INPUT_LENGTH |
| 23 | + --out-dir OUT_DIR |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +from optimum_benchmark import Benchmark, BenchmarkConfig, InferenceConfig, ProcessConfig, PyTorchConfig |
| 30 | +from optimum_benchmark.logging_utils import setup_logging |
| 31 | +import torch |
| 32 | + |
| 33 | +BFLOAT16_SUPPORT = torch.cuda.get_device_capability()[0] >= 8 |
| 34 | + |
| 35 | +WEIGHTS_CONFIGS = { |
| 36 | + "fp16": {"torch_dtype": "float16", "quantization_scheme": None, "quantization_config": {}}, |
| 37 | + "bf16": {"torch_dtype": "bfloat16", "quantization_scheme": None, "quantization_config": {}}, |
| 38 | + "nf4": { |
| 39 | + "torch_dtype": "bfloat16" if BFLOAT16_SUPPORT else "float16", |
| 40 | + "quantization_scheme": "bnb", |
| 41 | + "quantization_config": { |
| 42 | + "load_in_4bit": True, |
| 43 | + "bnb_4bit_quant_type": "nf4", |
| 44 | + "bnb_4bit_use_double_quant": False, |
| 45 | + "bnb_4bit_compute_dtype": torch.bfloat16 if BFLOAT16_SUPPORT else "float16", |
| 46 | + }, |
| 47 | + }, |
| 48 | + "nf4-dq": { |
| 49 | + "torch_dtype": "bfloat16" if BFLOAT16_SUPPORT else "float16", |
| 50 | + "quantization_scheme": "bnb", |
| 51 | + "quantization_config": { |
| 52 | + "load_in_4bit": True, |
| 53 | + "bnb_4bit_quant_type": "nf4", |
| 54 | + "bnb_4bit_use_double_quant": True, |
| 55 | + "bnb_4bit_compute_dtype": torch.bfloat16 if BFLOAT16_SUPPORT else "float16", |
| 56 | + }, |
| 57 | + }, |
| 58 | + "int8-decomp": { |
| 59 | + "torch_dtype": "float16", |
| 60 | + "quantization_scheme": "bnb", |
| 61 | + "quantization_config": { |
| 62 | + "load_in_8bit": True, |
| 63 | + "llm_int8_threshold": 6.0, |
| 64 | + }, |
| 65 | + }, |
| 66 | + "int8": { |
| 67 | + "torch_dtype": "float16", |
| 68 | + "quantization_scheme": "bnb", |
| 69 | + "quantization_config": { |
| 70 | + "load_in_8bit": True, |
| 71 | + "llm_int8_threshold": 0.0, |
| 72 | + }, |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + setup_logging(level="INFO") |
| 78 | + |
| 79 | + parser = argparse.ArgumentParser(description="bitsandbytes inference benchmark tool") |
| 80 | + |
| 81 | + parser.add_argument("model_id", type=str, help="The model checkpoint to use.") |
| 82 | + |
| 83 | + parser.add_argument( |
| 84 | + "--configs", |
| 85 | + nargs="+", |
| 86 | + choices=["bf16", "fp16", "nf4", "nf4-dq", "int8", "int8-decomp"], |
| 87 | + default=["nf4", "int8", "int8-decomp"], |
| 88 | + ) |
| 89 | + parser.add_argument("--bf16", dest="configs", action="append_const", const="bf16") |
| 90 | + parser.add_argument("--fp16", dest="configs", action="append_const", const="fp16") |
| 91 | + parser.add_argument("--nf4", dest="configs", action="append_const", const="nf4") |
| 92 | + parser.add_argument("--nf4-dq", dest="configs", action="append_const", const="nf4-dq") |
| 93 | + parser.add_argument("--int8", dest="configs", action="append_const", const="int8") |
| 94 | + parser.add_argument("--int8-decomp", dest="configs", action="append_const", const="int8-decomp") |
| 95 | + |
| 96 | + parser.add_argument("--batches", nargs="+", type=int, default=[1, 8, 16, 32]) |
| 97 | + parser.add_argument("--input-length", type=int, default=64) |
| 98 | + |
| 99 | + parser.add_argument("--out-dir", type=str, default="reports") |
| 100 | + |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + out_dir = Path(args.out_dir) |
| 104 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 105 | + |
| 106 | + for batch_size in args.batches: |
| 107 | + print(f"Benchmarking batch size: {batch_size}") |
| 108 | + for config in args.configs: |
| 109 | + launcher_config = ProcessConfig(device_isolation=True, start_method="spawn") |
| 110 | + scenario_config = InferenceConfig( |
| 111 | + latency=True, |
| 112 | + memory=True, |
| 113 | + input_shapes={"batch_size": batch_size, "sequence_length": args.input_length}, |
| 114 | + ) |
| 115 | + backend_config = PyTorchConfig( |
| 116 | + device="cuda", |
| 117 | + device_ids="0", |
| 118 | + device_map="auto", |
| 119 | + no_weights=False, |
| 120 | + model=args.model_id, |
| 121 | + **WEIGHTS_CONFIGS[config], |
| 122 | + ) |
| 123 | + benchmark_config = BenchmarkConfig( |
| 124 | + name=f"benchmark-{config}-bsz{batch_size}", |
| 125 | + scenario=scenario_config, |
| 126 | + launcher=launcher_config, |
| 127 | + backend=backend_config, |
| 128 | + ) |
| 129 | + |
| 130 | + out_path = out_dir / f"benchmark_{config}_bsz{batch_size}.json" |
| 131 | + |
| 132 | + benchmark_report = Benchmark.launch(benchmark_config) |
| 133 | + benchmark_report.log() |
| 134 | + benchmark_report.save_json(out_path) |
0 commit comments