our_knp/cats-dogs/benchmark_backends.py
int 8f43927575 Initial import: KNP experiments (examples, viz, cats-dogs, reports)
examples/ is based on KasperskyLab/knp at 55470ed6 with local changes
to the mnist-learn altai/blifat models, cuda-backend CMake and model_loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 18:17:15 +03:00

49 lines
2.2 KiB
Python

"""Compare CPU backends on the same small AltAI/STDP validation task."""
import argparse
import json
import os
from pathlib import Path
import signal
import subprocess
import sys
import time
ap = argparse.ArgumentParser()
ap.add_argument('--prefix', default='backend_bench')
ap.add_argument('--skip-stock', action='store_true')
a = ap.parse_args()
root = Path('/home/int/run/cats-dogs')
rows = []
cases = [('single', 'single', 1), ('stock24', 'multi', 24), ('tuned2', 'tuned', 2), ('tuned4', 'tuned', 4)]
for label, backend, threads in cases:
if a.skip_stock and backend == 'multi':
continue
name = f'{a.prefix}_{label}'
command = [sys.executable, '/mnt/c/KNP/cats-dogs/run.py', name,
'--data-dir', str(root / 'pilot-features'), '--dataset', 'sparse_hog_validation',
'--train', '800', '--count', '200', '--threshold', '4000', '--quant', 'weights',
'--backend', backend, '--threads', str(threads)]
started = time.monotonic()
try:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, start_new_session=True)
try:
stdout, stderr = process.communicate(timeout=45)
except subprocess.TimeoutExpired:
os.killpg(process.pid, signal.SIGTERM)
process.communicate()
raise
if process.returncode:
raise RuntimeError(stderr[-2000:])
row = {'backend': label, **json.loads((root / name / 'metrics.json').read_text())}
ref = root / f'{a.prefix}_single'
row['same_weights_and_result'] = all((ref / f).read_bytes() == (root / name / f).read_bytes()
for f in ('float_weights.csv', 'quant_weights.csv', 'result.csv'))
except subprocess.TimeoutExpired:
row = {'backend': label, 'timeout_seconds': 45}
except Exception as error:
row = {'backend': label, 'error': str(error)}
row['wall_seconds'] = round(time.monotonic() - started, 3)
rows.append(row)
print(json.dumps(row), flush=True)
(root / f'acceleration/{a.prefix}-benchmark.json').write_text(json.dumps(rows, indent=2) + '\n')