perf(simulation): cache SAM3 text and retain exact perception evidence
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from k1link.simulation.ai_polygon.concept_surfaces import PROMPTS, FixedConceptTextEncoder
|
||||
from k1link.simulation.ai_polygon.perception_evidence import archive_perception
|
||||
|
||||
|
||||
def test_fixed_text_cache_is_bounded_to_model_prompts_and_keeps_prompt_identity():
|
||||
calls = []
|
||||
|
||||
def encode(prompts, *, device):
|
||||
calls.append((tuple(prompts), device))
|
||||
return {"language_features": np.array([PROMPTS.index(prompts[0])])}
|
||||
|
||||
cached = FixedConceptTextEncoder(encode, "cuda")
|
||||
for prompt in (*PROMPTS, *reversed(PROMPTS), *PROMPTS):
|
||||
result = cached([prompt], device="cuda")
|
||||
assert result["language_features"].tolist() == [PROMPTS.index(prompt)]
|
||||
result.clear() # Processor-owned mapping must not erase cached keys.
|
||||
assert calls == [((prompt,), "cuda") for prompt in PROMPTS]
|
||||
for prompts, device in [(["asphalt"], "cuda"), (["sidewalk", "wall"], "cuda"),
|
||||
(["sidewalk"], "cpu")]:
|
||||
with pytest.raises(ValueError, match="Unexpected fixed"):
|
||||
cached(prompts, device=device)
|
||||
assert len(calls) == 4
|
||||
# A replacement model owns new features, never a process-global text cache.
|
||||
other = FixedConceptTextEncoder(lambda *a, **k: {"language_features": [99]}, "cuda")
|
||||
assert other(["sidewalk"], device="cuda")["language_features"] == [99]
|
||||
|
||||
|
||||
def view(frame, seed, yaw):
|
||||
rng = np.random.default_rng(seed)
|
||||
labels = rng.integers(0, 6, (512, 512), dtype=np.uint8)
|
||||
candidate = np.isin(labels, [1, 2, 3])
|
||||
return {
|
||||
"frame_id": frame,
|
||||
"rgb": rng.integers(0, 256, (600, 800, 3), dtype=np.uint8),
|
||||
"observation": {
|
||||
"captured_at": 123.5, "simulation_time_ns": 78000000000,
|
||||
"pose": [1, 2, .37, 0, 0, 0, 1],
|
||||
"calibration": {"view_yaw_degrees": yaw},
|
||||
},
|
||||
"values": {
|
||||
"segmentation.labels": labels,
|
||||
"segmentation.known": labels != 0,
|
||||
"segmentation.surface": candidate,
|
||||
"navigation.surface": candidate & (labels == 1),
|
||||
"source.crossing-mask": np.zeros_like(candidate),
|
||||
"source.crossing-admission": {"state": "sidewalk", "admitted": False},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_exact_evidence_round_trips_input_and_distinguishes_candidate_from_permission(tmp_path):
|
||||
original = view(2359, 2359, 0)
|
||||
extra = view(2359, 2360, 90)
|
||||
original["supplement"] = extra
|
||||
records = archive_perception(tmp_path, original)
|
||||
assert [r["role"] for r in records] == ["task", "supplement"]
|
||||
for result, receipt in zip((original, extra), records, strict=True):
|
||||
path = tmp_path / receipt["file"]
|
||||
assert hashlib.sha256(path.read_bytes()).hexdigest() == receipt["sha256"]
|
||||
with np.load(path, allow_pickle=False) as saved:
|
||||
assert saved["rgb"].dtype == np.uint8
|
||||
assert saved["rgb"].tobytes() == result["rgb"].tobytes()
|
||||
assert hashlib.sha256(saved["rgb"].tobytes()).hexdigest() == receipt["rgb_sha256"]
|
||||
for key, port in (("labels", "segmentation.labels"), ("known", "segmentation.known"),
|
||||
("candidate_surface", "segmentation.surface"),
|
||||
("permitted_surface", "navigation.surface")):
|
||||
np.testing.assert_array_equal(saved[key], result["values"][port])
|
||||
assert (np.count_nonzero(saved["candidate_surface"])
|
||||
> np.count_nonzero(saved["permitted_surface"]))
|
||||
metadata = json.loads(str(saved["metadata_json"]))
|
||||
assert metadata["frame_id"] == 2359
|
||||
assert metadata["captured_at"] == result["observation"]["captured_at"]
|
||||
assert metadata["simulation_time_ns"] == 78000000000
|
||||
assert metadata["calibration"] == result["observation"]["calibration"]
|
||||
assert metadata["rgb_sha256"] == receipt["rgb_sha256"]
|
||||
|
||||
|
||||
def test_perception_evidence_rejects_preview_resolution(tmp_path):
|
||||
result = view(1, 2, 0)
|
||||
result["rgb"] = result["rgb"][::2, ::2]
|
||||
with pytest.raises(ValueError, match="exact 800x600"):
|
||||
archive_perception(tmp_path, result)
|
||||
assert not list(tmp_path.iterdir())
|
||||
Reference in New Issue
Block a user