foliation-chip-chat / tools /chip_aig_cloud_sim.py
chipnlp's picture
Public-safe chip chat (minimal tools) ts=1782814562
3cf9742 verified
Raw
History Blame Contribute Delete
2.97 kB
#!/usr/bin/env python3
"""HF / 本地同构 — NLP 64× AIG 网表元数据 + LUT 特征地址(纯 Python,无 Rust 模拟器)。"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
from tools.chip_distilled_router import routing_feature_address
def _manifest_path(root: Path) -> Path:
for rel in (
"artifacts/nlp_array_64x.manifest.json",
"artifacts/nlp_array_64x.master.aig.manifest.json",
):
p = root / rel
if p.is_file():
return p
return root / "artifacts/nlp_array_64x.manifest.json"
def aig_cloud_stats(root: Path | None = None) -> dict[str, Any]:
"""读取 AIG manifest + 确认 LUT 路由同构(云端 cpu-basic 可跑)。"""
base = root or Path(__file__).resolve().parents[1]
manifest_p = _manifest_path(base)
out: dict[str, Any] = {
"mode": "combinational_lut_aig_meta",
"manifest_present": manifest_p.is_file(),
"manifest_path": str(manifest_p.name),
}
if manifest_p.is_file():
try:
meta = json.loads(manifest_p.read_text(encoding="utf-8"))
out["manifest"] = {
k: meta[k]
for k in (
"version",
"tiles",
"total_logical_cores",
"master_and_gates",
"tensor_bus_width",
"interconnect",
)
if k in meta
}
except (json.JSONDecodeError, OSError) as exc:
out["manifest_error"] = str(exc)
aig = base / "artifacts/nlp_array_64x.aig"
master = base / "artifacts/nlp_array_64x.master.aig"
out["aig_present"] = aig.is_file()
out["master_aig_present"] = master.is_file()
if aig.is_file():
out["aig_bytes"] = aig.stat().st_size
if master.is_file():
out["master_aig_bytes"] = master.stat().st_size
lut = base / "artifacts/offline_distill/query_intent_lut.json"
out["lut_present"] = lut.is_file()
return out
def route_with_aig_context(text: str, root: Path | None = None) -> dict[str, Any]:
"""LUT 路由 + AIG 网表上下文(HF /v1/route/full 与本地 MCP 共用)。"""
from tools.chip_lut_engine import route_tensor
base = root or Path(__file__).resolve().parents[1]
addr = routing_feature_address(text)
lut = route_tensor(text)
stats = aig_cloud_stats(base)
payload = lut.to_dict() if hasattr(lut, "to_dict") else {
"opcode": lut.opcode,
"opcode_index": lut.opcode_index,
"intent_id": lut.intent_id,
"intent_name": lut.intent_name,
"feature_address": lut.feature_address,
"diagnostic": lut.diagnostic,
}
return {
**payload,
"feature_address_hex": f"0x{addr:04x}",
"aig": stats,
"compute_note": "Op-Code from 16-bit LUT collapse; AIG bundle is combinational reference (no GPU).",
}