|
1 | 1 | import argparse |
| 2 | +import time |
2 | 3 |
|
| 4 | +import torch |
3 | 5 | from transformers import AutoModelForCausalLM, AutoTokenizer |
4 | 6 |
|
5 | 7 | from fused_rms_norm import RMSNorm |
|
38 | 40 | default="cpu", |
39 | 41 | help='Device to use for inference (e.g., "cuda", "cpu").', |
40 | 42 | ) |
| 43 | + parser.add_argument( |
| 44 | + "--num-warmup-iterations", |
| 45 | + type=int, |
| 46 | + default=0, |
| 47 | + help="For profiling. The number of warmup iterations to run before measuring performance.", |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "--num-profiling-iterations", |
| 51 | + type=int, |
| 52 | + default=1, |
| 53 | + help="For profiling. The number of iterations to run for performance measurement.", |
| 54 | + ) |
41 | 55 |
|
42 | 56 | args = parser.parse_args() |
43 | 57 |
|
44 | 58 | model_name_or_path = args.model |
45 | 59 | prompts = args.prompts |
46 | 60 | max_new_tokens = args.max_new_tokens |
47 | 61 | device = args.device |
| 62 | + num_warmup_iterations = args.num_warmup_iterations |
| 63 | + num_profiling_iterations = args.num_profiling_iterations |
| 64 | + |
| 65 | + assert num_profiling_iterations >= 1 |
| 66 | + assert num_warmup_iterations >= 0 |
48 | 67 |
|
49 | 68 | tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) |
50 | 69 | model = AutoModelForCausalLM.from_pretrained(model_name_or_path).to(device) |
|
58 | 77 | replace_module(model, SiLU) |
59 | 78 |
|
60 | 79 | inputs = tokenizer(prompts, padding=True, return_tensors="pt").to(device) |
61 | | - outputs = model.generate(**inputs, max_new_tokens=max_new_tokens) |
| 80 | + |
| 81 | + for _ in range(num_warmup_iterations): |
| 82 | + model.generate(**inputs, max_new_tokens=max_new_tokens) |
| 83 | + |
| 84 | + if device == "cuda": |
| 85 | + torch.cuda.synchronize() |
| 86 | + |
| 87 | + start_time = time.time() |
| 88 | + |
| 89 | + for _ in range(num_profiling_iterations): |
| 90 | + outputs = model.generate(**inputs, max_new_tokens=max_new_tokens) |
| 91 | + |
| 92 | + if device == "cuda": |
| 93 | + torch.cuda.synchronize() |
| 94 | + |
| 95 | + end_time = time.time() |
| 96 | + avg_time_ms = (end_time - start_time) * 1000 / num_profiling_iterations |
| 97 | + |
62 | 98 | strings = tokenizer.batch_decode(outputs, skip_special_tokens=True) |
63 | 99 |
|
64 | 100 | print(strings) |
| 101 | + print(f"\nAverage inference time: {avg_time_ms:.4f} ms.") |
0 commit comments