|
| 1 | +import json |
| 2 | +import torch |
| 3 | +import platform |
| 4 | +import os |
| 5 | +import time |
| 6 | +import datetime |
| 7 | +import hashlib |
| 8 | + |
| 9 | +def get_arch_name() -> str: |
| 10 | + if torch.cuda.is_available(): |
| 11 | + return torch.cuda.get_device_name() |
| 12 | + else: |
| 13 | + # This returns x86_64 or arm64 (for aarch64) |
| 14 | + return platform.machine() |
| 15 | + |
| 16 | + |
| 17 | +def write_json_result(output_json_path, headers, row, compile): |
| 18 | + """ |
| 19 | + Write the result into JSON format, so that it can be uploaded to the benchmark database |
| 20 | + to be displayed on OSS dashboard. The JSON format is defined at |
| 21 | + https://github.com/pytorch/pytorch/wiki/How-to-integrate-with-PyTorch-OSS-benchmark-database |
| 22 | + """ |
| 23 | + mapping_headers = {headers[i]: v for i, v in enumerate(row)} |
| 24 | + today = datetime.date.today() |
| 25 | + sha_hash = hashlib.sha256(str(today).encode("utf-8")).hexdigest() |
| 26 | + first_second = datetime.datetime.combine(today, datetime.time.min) |
| 27 | + workflow_id = int(first_second.timestamp()) |
| 28 | + job_id = workflow_id + 1 |
| 29 | + record = { |
| 30 | + "timestamp": int(time.time()), |
| 31 | + "schema_version": "v3", |
| 32 | + "name": "devvm local benchmark", |
| 33 | + "repo": "pytorch/ao", |
| 34 | + "head_branch": "main", |
| 35 | + "head_sha": sha_hash, |
| 36 | + "workflow_id": workflow_id, |
| 37 | + "run_attempt": 1, |
| 38 | + "job_id": job_id, |
| 39 | + "benchmark": { |
| 40 | + "name": "TorchAO benchmark", |
| 41 | + "mode": "inference", |
| 42 | + "dtype": mapping_headers["dtype"], |
| 43 | + "extra_info": { |
| 44 | + "device": mapping_headers["device"], |
| 45 | + "arch": mapping_headers["arch"], |
| 46 | + "min_sqnr": None, |
| 47 | + "compile": compile, |
| 48 | + }, |
| 49 | + }, |
| 50 | + "model": { |
| 51 | + "name": mapping_headers["name"], |
| 52 | + "type": "model", |
| 53 | + # TODO: make this configurable |
| 54 | + "origins": ["torchbench"], |
| 55 | + }, |
| 56 | + "metric": { |
| 57 | + "name": mapping_headers["metric"], |
| 58 | + "benchmark_values": [mapping_headers["actual"]], |
| 59 | + "target_value": mapping_headers["target"], |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + with open(f"{os.path.splitext(output_json_path)[0]}.json", "a") as f: |
| 64 | + print(json.dumps(record), file=f) |
| 65 | + |
| 66 | +def benchmark_and_write_json_result(model, args, kwargs, quantization, device, compile=True): |
| 67 | + print(quantization + " run") |
| 68 | + from torchao.utils import benchmark_model, profiler_runner |
| 69 | + if compile: |
| 70 | + model = torch.compile(model, mode="max-autotune") |
| 71 | + benchmark_model(model, 20, args, kwargs) |
| 72 | + elapsed_time = benchmark_model(model, 100, args, kwargs) |
| 73 | + print("elapsed_time: ", elapsed_time, " milliseconds") |
| 74 | + |
| 75 | + name = model._orig_mod.__class__.__name__ |
| 76 | + headers = ["name", "dtype", "compile", "device", "arch", "metric", "actual", "target"] |
| 77 | + arch = get_arch_name() |
| 78 | + dtype = quantization |
| 79 | + performance_result = [name, dtype, compile, device, arch, "time_ms(avg)", elapsed_time, None] |
| 80 | + write_json_result(_OUTPUT_JSON_PATH, headers, performance_result) |
0 commit comments