perf(simulation): cache SAM3 text and retain exact perception evidence

This commit is contained in:
DCCONSTRUCTIONS
2026-10-08 18:29:32 +03:00
parent 021b9f427f
commit 9a2ea5fc2e
6 changed files with 174 additions and 2 deletions
@@ -34,6 +34,8 @@ def registry(adapter_root: Path) -> ModuleRegistry:
"run_inputs.py",
"camera_optics.py",
"sensor_mounts.py",
"perception_evidence.py",
"evidence_arrays.py",
)},
**{name: hashlib.sha256((adapter_root / name).read_bytes()).hexdigest()
for name in ("continuous_composition.py", "continuous_pipeline.py", "process_pipeline.py", "urban_crossing.py")},
@@ -31,3 +31,26 @@ def concept_rasters(masks):
labels = np.asarray(Image.fromarray(labels).crop((100, 0, 700, 600)).resize(
(512, 512), Image.Resampling.NEAREST))
return labels, np.isin(labels, (1, 2, 3)), labels != 0
class FixedConceptTextEncoder:
"""Cache the four image-independent prompts for one immutable model instance.
The pinned processor still owns prompt setup and grounding. A fresh mapping
prevents it from changing the cache's keys; its feature tensors are read-only
inputs to the pinned grounding model. Model recreation destroys this cache.
"""
def __init__(self, encode, device):
self.encode = encode
self.device = device
self.features = {}
def __call__(self, prompts, *, device):
if (not isinstance(prompts, list) or len(prompts) != 1
or prompts[0] not in PROMPTS or device != self.device):
raise ValueError("Unexpected fixed concept text request")
prompt = prompts[0]
if prompt not in self.features:
self.features[prompt] = dict(self.encode(prompts, device=device))
return dict(self.features[prompt])
@@ -0,0 +1,49 @@
"""Exact provider input and distinct surface outputs for causal offline replay."""
import hashlib
import json
from pathlib import Path
import numpy as np
from .evidence_arrays import save_arrays
def archive_perception(directory, result):
"""Called only by the existing bounded writer, after perception publication."""
records = []
for role, view in (("task", result), ("supplement", result.get("supplement"))):
if view is None:
continue
rgb = np.asarray(view["rgb"])
if rgb.dtype != np.uint8 or rgb.shape != (600, 800, 3):
raise ValueError("Expected exact 800x600 RGB provider input")
values, observation = view["values"], view["observation"]
arrays = {
"rgb": rgb,
"labels": values["segmentation.labels"],
"known": values["segmentation.known"],
"candidate_surface": values["segmentation.surface"],
"permitted_surface": values["navigation.surface"],
"crossing_mask": values["source.crossing-mask"],
}
if any(np.asarray(a).shape != (512, 512) for k, a in arrays.items() if k != "rgb"):
raise ValueError("Surface evidence does not match the provider raster")
metadata = dict(
schema_version="missioncore.perception-evidence/v1",
frame_id=view["frame_id"], role=role,
captured_at=observation["captured_at"],
simulation_time_ns=observation["simulation_time_ns"],
pose=observation["pose"], calibration=observation["calibration"],
crossing=values["source.crossing-admission"],
rgb_sha256=hashlib.sha256(rgb.tobytes()).hexdigest(),
rgb_layout="RGB uint8 HWC; exact provider input, before lossy preview encoding",
)
path = Path(directory) / f"{view['frame_id']:08d}.{role}.perception.npz"
save_arrays(path, **arrays,
metadata_json=json.dumps(metadata, sort_keys=True, allow_nan=False))
with path.open("rb") as stream:
digest = hashlib.file_digest(stream, "sha256").hexdigest()
records.append(dict(role=role, file=path.name, sha256=digest,
rgb_sha256=metadata["rgb_sha256"]))
return records