|
| 1 | +import os |
| 2 | +import csv |
| 3 | +import sys |
| 4 | +import uuid |
| 5 | +import time |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | + |
| 9 | +TOKENS = 256 |
| 10 | + |
| 11 | +online_threads = None |
| 12 | + |
| 13 | + |
| 14 | +def parse_args(): |
| 15 | + parser = argparse.ArgumentParser(description="Run offline benchmark.") |
| 16 | + parser.add_argument("-m", "--model", |
| 17 | + type=str, required=True, |
| 18 | + help="name of the model") |
| 19 | + parser.add_argument("-b", "--batch_size", |
| 20 | + type=int, required=True, |
| 21 | + help="batch size to feed the model with") |
| 22 | + parser.add_argument("-p", "--prompt_size", |
| 23 | + type=int, required=True, |
| 24 | + help="prompt size to feed the model with") |
| 25 | + parser.add_argument("-r", "--threads_range", |
| 26 | + type=str, required=True, |
| 27 | + help="range of threads to use, e.g. '0-63,128-191', threads will be divided between processes " |
| 28 | + "- hint: 'lscpu | grep NUMA'") |
| 29 | + parser.add_argument("--kv_cache", |
| 30 | + type=int, default=65536, |
| 31 | + help="kv cache size") |
| 32 | + parser.add_argument("-n", "--num_processes", |
| 33 | + type=int, default=1, |
| 34 | + help="number of processes to spawn") |
| 35 | + parser.add_argument("-t", "--num_threads", |
| 36 | + type=int, default=1, |
| 37 | + help="number of threads to use per process") |
| 38 | + return parser.parse_args() |
| 39 | + |
| 40 | + |
| 41 | +def parse_threads_range(threads_range: str) -> list[int]: |
| 42 | + threads_range = [s.split("-") for s in threads_range.split(",")] |
| 43 | + if not all([len(s) == 2 for s in threads_range]): |
| 44 | + print("Format of --threads_range argument must be '{idx}-{idx},{idx}-{idx},...', " |
| 45 | + "e.g. '88-88' to use just thread idx 88") |
| 46 | + sys.exit(1) |
| 47 | + designated_threads = [] |
| 48 | + for s in threads_range: |
| 49 | + s_0, s_1 = int(s[0]), int(s[1]) |
| 50 | + if s_1 < s_0: |
| 51 | + print(f"Range {s_0}-{s_1} is not valid, second value has to be equal to or greater than the first value") |
| 52 | + sys.exit(1) |
| 53 | + designated_threads += [i for i in range(s_0, s_1 + 1)] |
| 54 | + return designated_threads |
| 55 | + |
| 56 | + |
| 57 | +def gen_threads_config(num_threads, process_id): |
| 58 | + threads_to_use = [str(t) for t in online_threads[num_threads * process_id:num_threads * (process_id + 1)]] |
| 59 | + assert len(threads_to_use) == num_threads |
| 60 | + return ",".join(threads_to_use) |
| 61 | + |
| 62 | + |
| 63 | +def summarize_results(logs_dir, args, start, finish): |
| 64 | + ttfts = [] |
| 65 | + tg_lats = [] |
| 66 | + for n in range(args.num_processes): |
| 67 | + results = open(f"{logs_dir}/log_{n}", "r").readlines()[-9].split("|") |
| 68 | + prompt_size = int(results[1]) |
| 69 | + assert prompt_size == args.prompt_size |
| 70 | + tokens_generated = int(results[2]) |
| 71 | + assert tokens_generated == TOKENS |
| 72 | + batch_size = int(results[3]) |
| 73 | + assert batch_size == args.batch_size |
| 74 | + ttfts.append(float(results[5])) |
| 75 | + tg_lats.append(float(results[7])) |
| 76 | + |
| 77 | + pp_throughput = sum([args.batch_size * args.prompt_size / ttft for ttft in ttfts]) |
| 78 | + avg_pp_latency = sum(ttfts) / len(ttfts) |
| 79 | + tg_throughput = sum([args.batch_size * TOKENS / lat for lat in tg_lats]) |
| 80 | + tg_per_token_lats = [lat / TOKENS for lat in tg_lats] |
| 81 | + avg_tg_latency = sum(tg_per_token_lats) / len(tg_per_token_lats) |
| 82 | + avg_total_speed = args.num_processes * args.batch_size * (args.prompt_size + TOKENS) / max([ttft + tg_lat for ttft, tg_lat in zip(ttfts, tg_lats)]) |
| 83 | + |
| 84 | + results_filename = f"{args.model.split('/')[-1]}@PP{str(args.prompt_size)}@TG{str(TOKENS)}.csv" |
| 85 | + if os.path.exists(results_filename): |
| 86 | + first_write = False |
| 87 | + else: |
| 88 | + first_write = True |
| 89 | + with open(results_filename, "a") as f: |
| 90 | + writer = csv.writer(f) |
| 91 | + if first_write: |
| 92 | + writer.writerow( |
| 93 | + ["n_proc", "n_threads", "batch_size", "prompt_size", "output_tokens", "pp_throughput_tps", |
| 94 | + "pp_avg_latency_sec", "tg_throughput_tps", "tg_avg_latency_sec", "pp+tg_throughput_tps", "concurrency", "start", "finish"]) |
| 95 | + writer.writerow( |
| 96 | + [args.num_processes, args.num_threads, args.batch_size, args.prompt_size, TOKENS, pp_throughput, |
| 97 | + avg_pp_latency, tg_throughput, avg_tg_latency, avg_total_speed, args.batch_size * args.num_processes, start, finish]) |
| 98 | + print(f"Result saved in {results_filename}") |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + global online_threads |
| 103 | + |
| 104 | + args = parse_args() |
| 105 | + |
| 106 | + designated_threads = parse_threads_range(args.threads_range) |
| 107 | + numa_config = subprocess.run(["numactl", "--show"], capture_output=True, text=True, check=True) |
| 108 | + online_threads = [int(t) for t in numa_config.stdout.split("physcpubind: ")[1].split(" \ncpubind:")[0].split() |
| 109 | + if int(t) in designated_threads] |
| 110 | + if len(online_threads) < args.num_processes * args.num_threads: |
| 111 | + print(f"Requested config requires {args.num_processes * args.num_threads} threads, while only {len(online_threads)} threads are both online and designated") |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + logs_dir = os.path.join("/tmp", str(uuid.uuid4())) |
| 115 | + os.mkdir(logs_dir) |
| 116 | + current_subprocesses = list() |
| 117 | + for n in range(args.num_processes): |
| 118 | + logfile = f"{logs_dir}/log_{n}" |
| 119 | + cmd = ["numactl", f"--physcpubind={gen_threads_config(args.num_threads, n)}", |
| 120 | + "/llm/batched-bench", args.model, str(args.kv_cache), "2048", "512", "0", "0", "0", str(args.prompt_size), str(TOKENS), |
| 121 | + str(args.batch_size), str(args.num_threads)] |
| 122 | + current_subprocesses.append( |
| 123 | + subprocess.Popen(cmd, stdout=open(logfile, 'wb'), stderr=open(logfile, 'wb'))) |
| 124 | + start = time.time() |
| 125 | + if any(p.wait() != 0 for p in current_subprocesses): |
| 126 | + print("FAIL: At least one process returned exit code other than 0 or died!") |
| 127 | + sys.exit(1) |
| 128 | + finish = time.time() |
| 129 | + summarize_results(logs_dir, args, start, finish) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + main() |
0 commit comments