Skip to content

Commit bb0de70

Browse files
committed
Pass benchmark config via test spec
1 parent 26da97d commit bb0de70

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

.github/scripts/extract_benchmark_results.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
BENCHMARK_RESULTS_FILENAME = "benchmark_results.json"
2525
ARTIFACTS_FILENAME_REGEX = re.compile(r"(android|ios)-artifacts-(?P<job_id>\d+).json")
26+
BENCHMARK_CONFIG_REGEX = re.compile(
27+
r"# The benchmark config is (?P<benchmark_config>.+)"
28+
)
2629

2730
# iOS-related regexes and variables
2831
IOS_TEST_SPEC_REGEX = re.compile(
@@ -308,7 +311,7 @@ def extract_job_id(artifacts_filename: str) -> int:
308311
return int(m.group("job_id"))
309312

310313

311-
def read_benchmark_configs(benchmark_configs: str) -> Dict[str, Dict[str, str]]:
314+
def read_all_benchmark_configs() -> Dict[str, Dict[str, str]]:
312315
"""
313316
Read all the benchmark configs that we can find
314317
"""
@@ -325,16 +328,44 @@ def read_benchmark_configs(benchmark_configs: str) -> Dict[str, Dict[str, str]]:
325328
return benchmark_configs
326329

327330

328-
def get_benchmark_configs(benchmark_configs: Dict[str, Dict[str, str]]) -> str:
331+
def read_benchmark_config(
332+
artifact_s3_url: str, benchmark_configs_dir: str
333+
) -> Dict[str, str]:
329334
"""
330335
Get the correct benchmark config for this benchmark run
331336
"""
337+
try:
338+
with request.urlopen(artifact_s3_url) as data:
339+
for line in data.read().decode("utf8").splitlines():
340+
m = IOS_TEST_SPEC_REGEX.match(line)
341+
if not m:
342+
continue
343+
344+
benchmark_config = m.group("benchmark_config")
345+
filename = os.path.join(
346+
benchmark_configs_dir, f"{benchmark_config}.json"
347+
)
348+
349+
if not os.path.exists(filename):
350+
warning(f"There is no benchmark config {filename}")
351+
continue
352+
353+
with open(filename) as f:
354+
try:
355+
return json.load(f)
356+
except json.JSONDecodeError as e:
357+
warning(f"Fail to load benchmark config {filename}: {e}")
358+
359+
except error.HTTPError:
360+
warning(f"Fail to read the test spec output at {artifact_s3_url}")
361+
362+
return {}
332363

333364

334365
def transform(
335366
app_type: str,
336367
benchmark_results: List,
337-
benchmark_configs: Dict[str, Dict[str, str]],
368+
benchmark_config: Dict[str, str],
338369
repo: str,
339370
head_branch: str,
340371
workflow_name: str,
@@ -384,25 +415,25 @@ def transform(
384415
for r in benchmark_results
385416
]
386417
elif schema_version == "v3":
418+
v3_benchmark_results = []
387419
# From https://github.com/pytorch/pytorch/wiki/How-to-integrate-with-PyTorch-OSS-benchmark-database
388420
return [
389421
{
390422
"benchmark": {
391423
"name": "ExecuTorch",
392424
"mode": "inference",
393-
"dtype": quantization,
394425
"extra_info": {
395426
"app_type": app_type,
396-
"benchmark_configs":
427+
# Just keep a copy of the benchmark config here
428+
"benchmark_config": json.dumps(benchmark_config),
397429
},
398430
},
399431
"model": {
400-
"name": r["benchmarkModel"]["name"],
432+
"name": benchmark_config.get("model", r["benchmarkModel"]["name"]),
401433
"type": "OSS model",
402-
"backend": r["benchmarkModel"].get("backend", ""),
403-
"extra_info": {
404-
"quantization": quantization,
405-
},
434+
"backend": benchmark_config.get(
435+
"config", r["benchmarkModel"].get("backend", "")
436+
),
406437
},
407438
"metric": {
408439
"name": r["metric"],
@@ -433,7 +464,7 @@ def main() -> None:
433464
"v2": [],
434465
"v3": [],
435466
}
436-
benchmark_configs = read_benchmark_configs(args.benchmark_configs)
467+
benchmark_config = {}
437468

438469
with open(args.artifacts) as f:
439470
for artifact in json.load(f):
@@ -449,6 +480,11 @@ def main() -> None:
449480
artifact_type = artifact["type"]
450481
artifact_s3_url = artifact["s3_url"]
451482

483+
if artifact_type == "TESTSPEC_OUTPUT":
484+
benchmark_config = read_benchmark_config(
485+
artifact_s3_url, args.benchmark_configs
486+
)
487+
452488
if app_type == "ANDROID_APP":
453489
benchmark_results = extract_android_benchmark_results(
454490
job_name, artifact_type, artifact_s3_url
@@ -464,7 +500,7 @@ def main() -> None:
464500
results = transform(
465501
app_type,
466502
benchmark_results,
467-
benchmark_configs,
503+
benchmark_config,
468504
args.repo,
469505
args.head_branch,
470506
args.workflow_name,

.github/workflows/android-perf.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
- name: Prepare the spec
101101
shell: bash
102102
env:
103-
BENCHMARK_CONFIGS: ${{ toJSON(matrix) }}
103+
BENCHMARK_CONFIG: ${{ toJSON(matrix) }}
104104
working-directory: extension/benchmark/android/benchmark
105105
run: |
106106
set -eux
@@ -110,13 +110,18 @@ jobs:
110110
# We could write a script to properly use jinja here, but there is only one variable,
111111
# so let's just sed it
112112
sed -i -e 's,{{ model_path }},'"${MODEL_PATH}"',g' android-llm-device-farm-test-spec.yml.j2
113-
cp android-llm-device-farm-test-spec.yml.j2 android-llm-device-farm-test-spec.yml
114113
114+
BENCHMARK_CONFIG_ID="${{ matrix.model }}_${{ matrix.config }}"
115+
# The config for this benchmark runs, we save it in the test spec so that it can be fetched
116+
# later by the upload script
117+
sed -i -e 's,{{ benchmark_config_id }},'"${BENCHMARK_CONFIG_ID}"',g' android-llm-device-farm-test-spec.yml.j2
118+
119+
cp android-llm-device-farm-test-spec.yml.j2 android-llm-device-farm-test-spec.yml
115120
# Just print the test spec for debugging
116121
cat android-llm-device-farm-test-spec.yml
117122
118-
# Also dump the benchmark configs so that we can use it later in the dashboard
119-
echo "${BENCHMARK_CONFIGS}" > ${{ matrix.model }}_${{ matrix.config }}.json
123+
# Save the benchmark configs so that we can use it later in the dashboard
124+
echo "${BENCHMARK_CONFIG}" > "${BENCHMARK_CONFIG_ID}.json"
120125
121126
- name: Upload the spec
122127
uses: seemethere/upload-artifact-s3@v5
@@ -414,14 +419,14 @@ jobs:
414419
415420
- name: Download the list of benchmark configs from S3
416421
env:
417-
BENCHMARK_CONFIGS_S3_DIR: s3://gha-artifacts/${{ github.repository }}/${{ github.run_id }}/artifacts/benchmark-configs/
422+
BENCHMARK_CONFIGS_DIR: s3://gha-artifacts/${{ github.repository }}/${{ github.run_id }}/artifacts/benchmark-configs/
418423
shell: bash
419424
run: |
420425
set -eux
421426
422427
mkdir -p benchmark-configs
423428
pushd benchmark-configs
424-
${CONDA_RUN} aws s3 sync "${BENCHMARK_CONFIGS_S3_DIR}" .
429+
${CONDA_RUN} aws s3 sync "${BENCHMARK_CONFIGS_DIR}" .
425430
popd
426431
427432
ls -lah benchmark-configs

extension/benchmark/android/benchmark/android-llm-device-farm-test-spec.yml.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ phases:
88

99
pre_test:
1010
commands:
11+
# The benchmark config is {{ benchmark_config_id }}
12+
1113
# Download the model from S3
1214
- curl -s --fail '{{ model_path }}' -o model.zip
1315
- unzip model.zip && ls -la

0 commit comments

Comments
 (0)