Files
Raptor/validation/raptor_validation/raptor.py
T
NiccoloN 4a1fe710b2
Validate Operations / validate-operations (push) Has been cancelled
force selected pipeline stages in throughput mode
increase throughput scheduler budget
add experimental throughput auto mode
2026-11-02 12:08:33 +01:00

121 lines
3.8 KiB
Python

import argparse
import json
import re
import shlex
import shutil
import subprocess
from pathlib import Path
from colorama import Fore, Style
from .subprocess_utils import run_command_with_reporter
PIM_PASS_LABELS = (
("ONNXToSpatialPass", "ONNX to Spatial"),
("MergeComputeNodesPass", "Merge compute nodes"),
("SpatialToPimPass", "Spatial to Pim"),
("PimBufferizationPass", "Bufferize Pim"),
("HostConstantFoldingPass", "Fold host constants"),
("PimLocalMemoryPlanningPass", "Plan local memory"),
("VerificationPass", "Verify Pim"),
("EmitPimCodePass", "Emit Pim code"),
)
PIM_PASS_LABEL_BY_SUFFIX = dict(PIM_PASS_LABELS)
TIMING_LINE_RE = re.compile(r"^\s*([0-9]+\.[0-9]+)\s+\(\s*[0-9.]+%\)\s+(.+?)\s*$")
def parse_pipeline_depth(value):
if value == "auto":
return value
try:
depth = int(value)
except ValueError:
raise argparse.ArgumentTypeError("pipeline must be a positive integer or auto") from None
if depth < 1:
raise argparse.ArgumentTypeError("pipeline must be a positive integer or auto")
return depth
def pipeline_sort_key(value):
return (str(value) == "auto", 0 if str(value) == "auto" else int(value))
def read_selected_pipeline_depth(pim_dir):
if pim_dir is None:
return None
report = Path(pim_dir).parent / "reports/pipeline_selection.json"
return json.loads(report.read_text()).get("selected_depth") if report.exists() else None
def _parse_pim_pass_timings(output_text):
pass_timings = {}
for line in output_text.splitlines():
match = TIMING_LINE_RE.match(line)
if not match:
continue
duration = float(match.group(1))
pass_name = match.group(2)
for suffix, label in PIM_PASS_LABEL_BY_SUFFIX.items():
if pass_name.endswith(suffix):
pass_timings[label] = pass_timings.get(label, 0.0) + duration
break
return pass_timings
def _format_command(cmd):
return shlex.join(str(arg) for arg in cmd)
def compile_with_raptor(network_path, raptor_onnx_path: Path, output_base: Path,
crossbar_size, crossbar_count, core_count,
raptor_extra_args, cwd, verbose, reporter, timeout_sec):
pim_dir = Path(output_base).parent / "pim"
if pim_dir.exists():
shutil.rmtree(pim_dir)
# Define the arguments, with the possibility to set crossbar size and count
args = [
network_path,
"-o",
output_base,
"--maccel=PIM",
"--EmitPimCodegen",
f"--crossbar-size={crossbar_size}",
f"--crossbar-count={crossbar_count}",
]
if core_count is not None:
args.append(f"--core-count={core_count}")
if raptor_extra_args:
args.extend(str(arg) for arg in raptor_extra_args)
if verbose:
args.append("--enable-timing")
cmd = [str(raptor_onnx_path)] + [str(arg) for arg in args]
if reporter is not None:
reporter.log(f" Raptor command: {_format_command(cmd)}")
else:
print(f"Raptor command: {_format_command(cmd)}")
try:
output_text = run_command_with_reporter(
cmd,
cwd=cwd,
reporter=reporter,
capture_output=True,
timeout_sec=timeout_sec,
)
selected_depth = read_selected_pipeline_depth(pim_dir)
if selected_depth is not None:
message = f"Selected pipeline depth: {selected_depth}"
if reporter is not None:
reporter.log(message)
else:
print(message)
if reporter is None:
print(Fore.GREEN + "Raptor execution successful" + Style.RESET_ALL)
return _parse_pim_pass_timings(output_text)
except subprocess.CalledProcessError:
if reporter is None:
print(Fore.RED + "Raptor execution failed" + Style.RESET_ALL)
raise