|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Produce a reproducible mock GEMV integration demonstrating the Fabric interposer.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import csv |
| 7 | +import os |
| 8 | +import platform |
| 9 | +import subprocess |
| 10 | +import time |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +ROOT = Path(__file__).resolve().parent.parent |
| 14 | +BIN_DIR = ROOT / "bin" |
| 15 | +TOOLS_DIR = ROOT / "tools" |
| 16 | +LOG_DIR = ROOT / "logs" |
| 17 | +PLOT_DIR = ROOT / "plots" |
| 18 | +CSV_PATH = LOG_DIR / "reference_integration.csv" |
| 19 | +PLOT_PATH = PLOT_DIR / "reference_integration_summary.png" |
| 20 | + |
| 21 | + |
| 22 | +def shlib_ext() -> str: |
| 23 | + return ".dylib" if platform.system() == "Darwin" else ".so" |
| 24 | + |
| 25 | + |
| 26 | +def build_targets() -> None: |
| 27 | + targets = [ |
| 28 | + "bin/mock_llama", |
| 29 | + f"bin/libtfmbs_device{shlib_ext()}", |
| 30 | + f"bin/libtfmbs_intercept{shlib_ext()}", |
| 31 | + ] |
| 32 | + subprocess.run(["make", *targets], check=True) |
| 33 | + |
| 34 | + |
| 35 | +def run_process(command: list[str], env: dict[str, str] | None, log_path: Path) -> float: |
| 36 | + start = time.perf_counter() |
| 37 | + result = subprocess.run(command, env=env, capture_output=True, text=True) |
| 38 | + duration_ms = (time.perf_counter() - start) * 1000 |
| 39 | + LOG_DIR.mkdir(parents=True, exist_ok=True) |
| 40 | + log_path.write_text(result.stdout + result.stderr) |
| 41 | + result.check_returncode() |
| 42 | + return duration_ms |
| 43 | + |
| 44 | + |
| 45 | +def try_plot(durations: list[tuple[str, float]]) -> None: |
| 46 | + try: |
| 47 | + import matplotlib.pyplot as plt |
| 48 | + except ImportError: # pragma: no cover |
| 49 | + print("📉 matplotlib not installed; skipping chart.") |
| 50 | + return |
| 51 | + |
| 52 | + PLOT_DIR.mkdir(parents=True, exist_ok=True) |
| 53 | + labels, values = zip(*durations) |
| 54 | + fig, ax = plt.subplots(figsize=(6, 4)) |
| 55 | + ax.bar(labels, values, color=["#2f7bbf", "#1f9d4d"]) |
| 56 | + ax.set_ylabel("Milliseconds") |
| 57 | + ax.set_title("Mock Llama: CPU vs. Fabric-Accelerated") |
| 58 | + for idx, value in enumerate(values): |
| 59 | + ax.text(idx, value + max(values) * 0.01, f"{value:.1f} ms", ha="center") |
| 60 | + fig.tight_layout() |
| 61 | + fig.savefig(PLOT_PATH) |
| 62 | + print(f"📊 Saved chart to {PLOT_PATH.relative_to(ROOT)}") |
| 63 | + |
| 64 | + |
| 65 | +def main() -> int: |
| 66 | + build_targets() |
| 67 | + |
| 68 | + baseline_duration = run_process( |
| 69 | + [str(BIN_DIR / "mock_llama")], |
| 70 | + env=None, |
| 71 | + log_path=LOG_DIR / "reference_integration_cpu.log", |
| 72 | + ) |
| 73 | + |
| 74 | + fabric_env = os.environ.copy() |
| 75 | + fabric_env["FABRIC_SHORT_CIRCUIT"] = "1" |
| 76 | + fabric_env["TFMBS_DEBUG"] = "1" |
| 77 | + |
| 78 | + fabric_duration = run_process( |
| 79 | + [str(TOOLS_DIR / "tfmbs-run"), str(BIN_DIR / "mock_llama")], |
| 80 | + env=fabric_env, |
| 81 | + log_path=LOG_DIR / "reference_integration_fabric.log", |
| 82 | + ) |
| 83 | + |
| 84 | + durations = [ |
| 85 | + ("CPU Baseline", baseline_duration), |
| 86 | + ("Fabric Interposer", fabric_duration), |
| 87 | + ] |
| 88 | + |
| 89 | + CSV_PATH.parent.mkdir(parents=True, exist_ok=True) |
| 90 | + with CSV_PATH.open("w", newline="") as csvfile: |
| 91 | + writer = csv.DictWriter(csvfile, fieldnames=["mode", "duration_ms", "notes"]) |
| 92 | + writer.writeheader() |
| 93 | + writer.writerow( |
| 94 | + { |
| 95 | + "mode": "cpu_baseline", |
| 96 | + "duration_ms": f"{baseline_duration:.2f}", |
| 97 | + "notes": "Pure CPU mock GEMV run", |
| 98 | + } |
| 99 | + ) |
| 100 | + writer.writerow( |
| 101 | + { |
| 102 | + "mode": "fabric_interposer", |
| 103 | + "duration_ms": f"{fabric_duration:.2f}", |
| 104 | + "notes": "LD_PRELOAD/libtfmbs_intercept run", |
| 105 | + } |
| 106 | + ) |
| 107 | + |
| 108 | + try_plot(durations) |
| 109 | + |
| 110 | + speedup = baseline_duration / fabric_duration if fabric_duration > 0 else float("inf") |
| 111 | + |
| 112 | + print("✅ Reference integration complete.") |
| 113 | + print(f"- Baseline: {baseline_duration:.2f} ms") |
| 114 | + print(f"- Fabric: {fabric_duration:.2f} ms ({speedup:.2f}x faster)") |
| 115 | + print(f"- Logs: {LOG_DIR.relative_to(ROOT)}") |
| 116 | + print(f"- CSV: {CSV_PATH.relative_to(ROOT)}") |
| 117 | + if PLOT_PATH.exists(): |
| 118 | + print(f"- Chart: {PLOT_PATH.relative_to(ROOT)}") |
| 119 | + else: |
| 120 | + print("- Chart: (matplotlib missing; install it to generate the bar chart)") |
| 121 | + |
| 122 | + return 0 |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + raise SystemExit(main()) |
0 commit comments