Skip to content

Commit

Permalink
[not4land] Local torchao benchmark
Browse files Browse the repository at this point in the history
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
  • Loading branch information
jerryzh168 committed Jan 5, 2025
1 parent 675fb8f commit 81d0bff
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 7 deletions.
14 changes: 14 additions & 0 deletions cron_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
# conda activate /home/jerryzh/anaconda3/envs/benchmark
# getting the latest torchao nightly
pip uninstall -y torchao
pip install -I --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu124
cd ~/local/cron_jobs/benchmark
rm benchmark_results.json
export HUGGING_FACE_HUB_TOKEN=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

This comment has been minimized.

Copy link
@xuzhao9

xuzhao9 Jan 5, 2025

Contributor

We should not expose any secret in the public commit

~/anaconda3/envs/benchmark/bin/python run_benchmark.py torchao --performance --inference --bfloat16 --inductor-compile-mode max-autotune --torchbench --ci
alias with-proxy="http_proxy=http://regional-fwdproxy6-shv-01.rcco0.facebook.com:8080 https_proxy=http://regional-fwdproxy6-shv-01.rcco0.facebook.com:8080 ftp_proxy=http://regional-fwdproxy6-shv-01.rcco0.facebook.com:8080"
echo "Uploading to s3"
with-proxy ~/anaconda3/envs/benchmark/bin/python upload_to_s3.py --json-path benchmark_results.json
echo "After running upload_to_s3"
aws s3 ls ossci-benchmarks/v3/pytorch/ao/devvm2167/
13 changes: 13 additions & 0 deletions manual_cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target_hour=03
target_min=00
while true
do
current_hour=$(date +%H)
current_min=$(date +%M)
if [ $current_hour -eq $target_hour ] && [ $current_min -eq $target_min ] ; then
echo "Cron job started at $(date)"
sh ~/local/cron_jobs/benchmark/cron_script.sh > ~/local/cron_jobs/benchmark/local_cron_log 2>~/local/cron_jobs/benchmark/local_cron_err
echo "Cron job executed at $(date)"
fi
sleep 60
done
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pytest
pytest-benchmark
requests
tabulate
git+https://github.com/huggingface/pytorch-image-models.git@730b907
# git+https://github.com/huggingface/pytorch-image-models.git@730b907
# this version of transformers is required by linger-kernel
# https://github.com/linkedin/Liger-Kernel/blob/main/pyproject.toml#L23
transformers==4.44.2
Expand Down
52 changes: 52 additions & 0 deletions upload_to_s3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import io
import json
from functools import lru_cache
import boto3
from typing import Any
import gzip

@lru_cache
def get_s3_resource() -> Any:
return boto3.resource("s3")

def upload_to_s3(
bucket_name: str,
key: str,
json_path: str,
) -> None:
print(f"Writing {json_path} documents to S3")
data = []
with open(f"{os.path.splitext(json_path)[0]}.json", "r") as f:
for l in f.readlines():
data.append(json.loads(l))

body = io.StringIO()
for benchmark_entry in data:
json.dump(benchmark_entry, body)
body.write("\n")

try:
get_s3_resource().Object(
f"{bucket_name}",
f"{key}",
).put(
Body=body.getvalue(),
ContentType="application/json",
)
except e:
print("fail to upload to s3:", e)
return
print("Done!")

if __name__ == "__main__":
import argparse
import datetime
parser = argparse.ArgumentParser(description="Upload benchmark result json file to clickhouse")
parser.add_argument("--json-path", type=str, help="json file path to upload to click house", required=True)
args = parser.parse_args()
today = datetime.date.today()
today = datetime.datetime.combine(today, datetime.time.min)
today_timestamp = str(int(today.timestamp()))
print("Today timestamp:", today_timestamp)
upload_to_s3("ossci-benchmarks", "v3/pytorch/ao/devvm2167/torchbenchmark-torchbench-" + today_timestamp + ".json", args.json_path)
38 changes: 38 additions & 0 deletions userbenchmark/dynamo/dynamobench/torchao_backend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any, Callable

import torch
from .utils import get_arch_name, write_json_result

_OUTPUT_JSON_PATH = "benchmark_results"

def setup_baseline():
from torchao.quantization.utils import recommended_inductor_config_setter
Expand All @@ -11,6 +13,21 @@ def setup_baseline():
torch._dynamo.config.cache_size_limit = 10000


def benchmark_and_write_json_result(model, args, kwargs, quantization, device):
print(quantization + " run")
from torchao.utils import benchmark_model, profiler_runner
model = torch.compile(model, mode="max-autotune")
benchmark_model(model, 20, args, kwargs)
elapsed_time = benchmark_model(model, 100, args, kwargs)
print("elapsed_time: ", elapsed_time, " milliseconds")

name = model._orig_mod.__class__.__name__
headers = ["name", "dtype", "device", "arch", "metric", "actual", "target"]
arch = get_arch_name()
dtype = quantization
performance_result = [name, dtype, device, arch, "time_ms(avg)", elapsed_time, None]
write_json_result(_OUTPUT_JSON_PATH, headers, performance_result)

def torchao_optimize_ctx(quantization: str):
from torchao.quantization.quant_api import (
autoquant,
Expand All @@ -20,10 +37,21 @@ def torchao_optimize_ctx(quantization: str):
quantize_,
)
from torchao.utils import unwrap_tensor_subclass
import torchao

def inner(model_iter_fn: Callable):
def _torchao_apply(module: torch.nn.Module, example_inputs: Any):
if getattr(module, "_quantized", None) is None:
if quantization == "noquant":
if isinstance(example_inputs, dict):
args = ()
kwargs = example_inputs
else:
args = example_inputs
kwargs = {}

benchmark_and_write_json_result(module, args, kwargs, "noquant", "cuda")

if quantization == "int8dynamic":
quantize_(
module,
Expand All @@ -47,6 +75,16 @@ def _torchao_apply(module: torch.nn.Module, example_inputs: Any):
"NotAutoquantizable"
f"Found no autoquantizable layers in model {type(module)}, stopping autoquantized run"
)

if isinstance(example_inputs, dict):
args = ()
kwargs = example_inputs
else:
args = example_inputs
kwargs = {}

torchao.quantization.utils.recommended_inductor_config_setter()
benchmark_and_write_json_result(module, args, kwargs, "autoquant", "cuda")
else:
unwrap_tensor_subclass(module)
setattr(module, "_quantized", True) # noqa: B010
Expand Down
62 changes: 62 additions & 0 deletions userbenchmark/dynamo/dynamobench/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import torch
import platform
import os
import time
import datetime
import hashlib

def get_arch_name() -> str:
if torch.cuda.is_available():
return torch.cuda.get_device_name()
else:
# This returns x86_64 or arm64 (for aarch64)
return platform.machine()


def write_json_result(output_json_path, headers, row):
"""
Write the result into JSON format, so that it can be uploaded to the benchmark database
to be displayed on OSS dashboard. The JSON format is defined at
https://github.com/pytorch/pytorch/wiki/How-to-integrate-with-PyTorch-OSS-benchmark-database
"""
mapping_headers = {headers[i]: v for i, v in enumerate(row)}
today = datetime.date.today()
sha_hash = hashlib.sha256(str(today).encode("utf-8")).hexdigest()
first_second = datetime.datetime.combine(today, datetime.time.min)
workflow_id = int(first_second.timestamp())
job_id = workflow_id + 1
record = {
"timestamp": int(time.time()),
"schema_version": "v3",
"name": "devvm local benchmark",
"repo": "pytorch/ao",
"head_branch": "main",
"head_sha": sha_hash,
"workflow_id": workflow_id,
"run_attempt": 1,
"job_id": job_id,
"benchmark": {
"name": "TorchAO benchmark",
"mode": "inference",
"dtype": mapping_headers["dtype"],
"extra_info": {
"device": mapping_headers["device"],
"arch": mapping_headers["arch"],
},
},
"model": {
"name": mapping_headers["name"],
"type": "model",
# TODO: make this configurable
"origins": ["torchbench"],
},
"metric": {
"name": mapping_headers["metric"],
"benchmark_values": [mapping_headers["actual"]],
"target_value": mapping_headers["target"],
},
}

with open(f"{os.path.splitext(output_json_path)[0]}.json", "a") as f:
print(json.dumps(record), file=f)
6 changes: 2 additions & 4 deletions userbenchmark/group_bench/configs/torch_ao.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ metrics:
test_group:
test_batch_size_default:
subgroup:
- extra_args:
- extra_args: --quantization int8dynamic
- extra_args: --quantization int8weightonly
- extra_args: --quantization int4weightonly
- extra_args: --quantization noquant
- extra_args: --quantization autoquant
4 changes: 2 additions & 2 deletions userbenchmark/torchao/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def _get_ci_args(


def _get_full_ci_args(modelset: str) -> List[List[str]]:
backends = ["autoquant", "int8dynamic", "int8weightonly", "noquant"]
backends = ["autoquant", "noquant"]
modelset = [modelset]
dtype = ["bfloat16"]
mode = ["inference"]
device = ["cuda"]
experiment = ["performance", "accuracy"]
experiment = ["performance"]
cfgs = itertools.product(*[backends, modelset, dtype, mode, device, experiment])
return [_get_ci_args(*cfg) for cfg in cfgs]

Expand Down

0 comments on commit 81d0bff

Please sign in to comment.