Skip to content

Commit a5a53a2

Browse files
authored
Add local optoin for output json file (#1426)
* Add local optoin for output json file Summary: We are planning to run the benchmarks in local machine for now since there are some issues when trying to run in OSS CI, so we have to fill in some fields that is populated by the OSS CI before Test Plan: ``` python torchao/_models/llama/generate.py --checkpoint_path "${CHECKPOINT_PATH}/${MODEL_REPO}/model.pth" --compile --compile_prefill --output_json_path benchmark-results.json --output_json_local python torchao/_models/llama/generate.py --checkpoint_path "${CHECKPOINT_PATH}/${MODEL_REPO}/model.pth" --compile --compile_prefill --quantization autoquant --output_json_path benchmark-results.json --output_js\on_local python torchao/_models/sam/eval_combo.py --coco_root_dir datasets/coco2017 --coco_slice_name val2017 --sam_checkpoint_base_path checkpoints --sam_model_type vit_h --point_sampling_cache_dir tmp/sam_coco_mask_ce\nter_cache --mask_debug_out_dir tmp/sam_eval_masks_out --batch_size 32 --num_workers 8 --use_compile max-autotune --use_half bfloat16 --device cuda --output_json_path benchmark-results.json --output_json_local python torchao/_models/sam/eval_combo.py --coco_root_dir datasets/coco2017 --coco_slice_name val2017 --sam_checkpoint_base_path checkpoints --sam_model_type vit_h --point_sampling_cache_dir tmp/sam_coco_mask_ce\nter_cache --mask_debug_out_dir tmp/sam_eval_masks_out --batch_size 32 --num_workers 8 --use_compile max-autotune --use_half bfloat16 --device cuda --compression autoquant --output_json_path benchmark-results.j\son --output_json_local cd examples/sam2_amg_server python server.py ${CHECKPOINT_PATH}/sam2 large --port 4000 --host localhost --fast --benchmark --dry --output_json_path ../../benchmark-results.json --output_json_local python server.py ${CHECKPOINT_PATH}/sam2 large --port 4000 --host localhost --fast --use_autoquant --benchmark --dry --output_json_path ../../benchmark-results.json --output_json_local cd ../.. ``` Reviewers: Subscribers: Tasks: Tags: * fix
1 parent 02d413c commit a5a53a2

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

examples/sam2_amg_server/server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
import contextlib
3131
from torchao._models.utils import (
3232
get_arch_name,
33-
write_json_result,
33+
write_json_result_ossci,
34+
write_json_result_local,
3435
)
3536

3637
from torch._inductor import config as inductorconfig
@@ -564,7 +565,8 @@ def main(checkpoint_path,
564565
batch_size=1,
565566
load_fast="",
566567
save_fast="",
567-
output_json_path=None):
568+
output_json_path=None,
569+
output_json_local=False):
568570
if verbose:
569571
logging.basicConfig(level=logging.INFO,
570572
format='%(asctime)s - %(levelname)s - %(message)s',
@@ -661,6 +663,7 @@ def main(checkpoint_path,
661663
memory_result = [name, dtype, device, arch, "memory(MiB)", max_memory_allocated_bytes, None]
662664
memory_percent_result = [name, dtype, device, arch, "memory(%)", max_memory_allocated_percentage, None]
663665
performance_result = [name, dtype, device, arch, "time_s(avg)", avg_time_per_run, None]
666+
write_json_result = write_json_result_local if output_json_local else write_json_result_ossci
664667
write_json_result(output_json_path, headers, memory_result)
665668
write_json_result(output_json_path, headers, memory_percent_result)
666669
write_json_result(output_json_path, headers, performance_result)

torchao/_models/llama/generate.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
from torchao.utils import get_model_size_in_bytes, TORCH_VERSION_AT_LEAST_2_5
2121
from torchao._models.utils import (
2222
get_arch_name,
23-
write_json_result,
23+
write_json_result_ossci,
24+
write_json_result_local,
2425
)
2526

2627
torch.sparse.SparseSemiStructuredTensor._FORCE_CUTLASS = False
@@ -134,7 +135,7 @@ def decode_n_tokens(
134135
next_token, next_prob = next_token.clone(), next_prob.clone()
135136
input_pos += 1
136137
# in some instances not having this causes weird issues with the stored tokens when you run the next decode_one_token step
137-
new_tokens.append(next_token.clone())
138+
new_tokens.append(next_token.clone())
138139
callback(new_tokens[-1])
139140
new_probs.append(next_prob)
140141
cur_token = next_token
@@ -278,6 +279,7 @@ def main(
278279
precision=torch.bfloat16,
279280
write_result: Optional[Path] = None,
280281
output_json_path: Optional[Path] = None,
282+
output_json_local: bool = False,
281283
) -> None:
282284
"""Generates text samples based on a pre-trained Transformer model and tokenizer."""
283285

@@ -941,6 +943,7 @@ def callback(x):
941943
dtype = quantization or str(precision)
942944
memory_result = [name, dtype, device, arch, "mem/s", bandwidth, None]
943945
performance_result = [name, dtype, device, arch, "tok/s", tokpersec, None]
946+
write_json_result = write_json_result_local if output_json_local else write_json_result_ossci
944947
write_json_result(output_json_path, headers, memory_result)
945948
write_json_result(output_json_path, headers, performance_result)
946949

@@ -1042,6 +1045,11 @@ def callback(x):
10421045
default=None,
10431046
help="Path where to write the json result for dashboard",
10441047
)
1048+
parser.add_argument(
1049+
"--output_json_local",
1050+
action="store_true",
1051+
help="Whether to output json result for local machine or for CI machine, local option will fill in some dummy fields",
1052+
)
10451053

10461054
args = parser.parse_args()
10471055
print(args)
@@ -1069,4 +1077,5 @@ def callback(x):
10691077
args.precision,
10701078
args.write_result,
10711079
args.output_json_path,
1080+
args.output_json_local,
10721081
)

torchao/_models/sam/eval_combo.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from torchao.utils import TORCH_VERSION_AT_LEAST_2_5
2424
from torchao._models.utils import (
2525
get_arch_name,
26-
write_json_result,
26+
write_json_result_ossci,
27+
write_json_result_local,
2728
)
2829

2930
torch._dynamo.config.cache_size_limit = 50000
@@ -253,6 +254,7 @@ def run(
253254
memory_path=None,
254255
device="cuda",
255256
output_json_path=None,
257+
output_json_local=False,
256258
):
257259
from torch._inductor import config as inductorconfig
258260
inductorconfig.triton.unique_kernel_names = True
@@ -468,6 +470,7 @@ def mlp_only(mod, name):
468470
dtype = compress or str(use_half) or "torch.float32"
469471
memory_result = [name, dtype, device, arch, "memory(MiB)", max_memory_allocated_bytes, None]
470472
performance_result = [name, dtype, device, arch, "img_s(avg)", img_s, None]
473+
write_json_result = write_json_result_local if output_json_local else write_json_result_ossci
471474
write_json_result(output_json_path, headers, memory_result)
472475
write_json_result(output_json_path, headers, performance_result)
473476

torchao/_models/utils.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import torch
33
import platform
44
import os
5+
import datetime
6+
import hashlib
7+
import time
58

69
def get_arch_name() -> str:
710
if torch.cuda.is_available():
@@ -11,14 +14,65 @@ def get_arch_name() -> str:
1114
return platform.machine()
1215

1316

14-
def write_json_result(output_json_path, headers, row):
17+
def write_json_result_ossci(output_json_path, headers, row):
1518
"""
1619
Write the result into JSON format, so that it can be uploaded to the benchmark database
1720
to be displayed on OSS dashboard. The JSON format is defined at
1821
https://github.com/pytorch/pytorch/wiki/How-to-integrate-with-PyTorch-OSS-benchmark-database
22+
23+
OSS CI version, that will leave many fields to be filled in by CI
24+
"""
25+
mapping_headers = {headers[i]: v for i, v in enumerate(row)}
26+
record = {
27+
"benchmark": {
28+
"name": "TorchAO benchmark",
29+
"mode": "inference",
30+
"dtype": mapping_headers["dtype"],
31+
"extra_info": {
32+
"device": mapping_headers["device"],
33+
"arch": mapping_headers["arch"],
34+
},
35+
},
36+
"model": {
37+
"name": mapping_headers["name"],
38+
"type": "model",
39+
"origins": ["pytorch"],
40+
},
41+
"metric": {
42+
"name": mapping_headers["metric"],
43+
"benchmark_values": [mapping_headers["actual"]],
44+
"target_value": mapping_headers["target"],
45+
},
46+
}
47+
48+
with open(f"{os.path.splitext(output_json_path)[0]}.json", "a") as f:
49+
print(json.dumps(record), file=f)
50+
51+
52+
def write_json_result_local(output_json_path, headers, row):
53+
"""
54+
Write the result into JSON format, so that it can be uploaded to the benchmark database
55+
to be displayed on OSS dashboard. The JSON format is defined at
56+
https://github.com/pytorch/pytorch/wiki/How-to-integrate-with-PyTorch-OSS-benchmark-database
57+
58+
Local version (filling in dummy values for fields that should be populated by CI)
1959
"""
2060
mapping_headers = {headers[i]: v for i, v in enumerate(row)}
61+
today = datetime.date.today()
62+
sha_hash = hashlib.sha256(str(today).encode("utf-8")).hexdigest()
63+
first_second = datetime.datetime.combine(today, datetime.time.min)
64+
workflow_id = int(first_second.timestamp())
65+
job_id = workflow_id + 1
2166
record = {
67+
"timestamp": int(time.time()),
68+
"schema_version": "v3",
69+
"name": "devvm local benchmark",
70+
"repo": "pytorch/ao",
71+
"head_branch": "main",
72+
"head_sha": sha_hash,
73+
"workflow_id": workflow_id,
74+
"run_attempt": 1,
75+
"job_id": job_id,
2276
"benchmark": {
2377
"name": "TorchAO benchmark",
2478
"mode": "inference",

0 commit comments

Comments
 (0)