neuplan-neuromorphic-compil.../neuplan/backends/loihi.py

30 lines
1.1 KiB
Python

"""Toy Loihi-like backend for NeuPlan MVP.
This is a lightweight simulator that pretends to run a neuromorphic graph on
Loihi-like hardware. It converts a NeuPlan NIR (toy dict) into a latency/energy
estimate and returns a simple executable plan skeleton.
"""
from __future__ import annotations
from typing import Dict, Any, List
class LoihiBackend:
def __init__(self, quantization_bits: int = 8, time_scale: float = 1.0) -> None:
self.quantization_bits = quantization_bits
self.time_scale = time_scale
def run(self, nir: Dict[str, Any], time_budget_s: float) -> Dict[str, Any]:
nodes: List[Dict[str, Any]] = nir.get("nodes", [])
# naive latency model: 5ms per node scaled by time_scale
latency = max(0.001, len(nodes) * 0.005 / max(1e-6, self.time_scale))
status = "ok" if latency <= time_budget_s else "timeout"
energy = max(0.01, len(nodes) * 0.02)
plan = {"steps": [{"node": n} for n in nodes]}
return {
"status": status,
"latency_s": latency,
"energy_j": energy,
"plan": plan,
}