240 lines
7.8 KiB
Python
240 lines
7.8 KiB
Python
"""契约系统单测 (design_v2 §4.5 D15 校验规则 / 派生规格)。
|
|
|
|
运行: python -m pytest tests/test_contract.py -q (或 python tests/test_contract.py)
|
|
图像输入固定 1:1 (224x224, 256 token/路); 非 1:1 比例已移除。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
from pi_pipeline import contract as C # noqa: E402
|
|
|
|
|
|
def base_contract(**overrides) -> dict:
|
|
data = {
|
|
"cameras": ["cam1", "cam2", "cam3", "cam4"],
|
|
"state": {"dim": 14, "inject": True},
|
|
"language": {"enabled": True, "max_token_len": 200},
|
|
"action": {"dim": 14, "horizon": 10},
|
|
"norm_mode": "min_max",
|
|
"key_map": {
|
|
"observation.images.cam_high": ["cam1"],
|
|
"observation.images.cam_third_view": ["cam2"],
|
|
"observation.images.cam_left_wrist": ["cam3"],
|
|
"observation.images.cam_right_wrist": ["cam4"],
|
|
"observation.state": "state",
|
|
"action": "actions",
|
|
"task": "prompt",
|
|
},
|
|
}
|
|
data.update(overrides)
|
|
return data
|
|
|
|
|
|
def expect_exit(data_or_text, needle: str) -> None:
|
|
"""契约违例必须以非 0 退出且报错含 needle (在子进程中断言 SystemExit)。"""
|
|
if isinstance(data_or_text, dict):
|
|
payload = json.dumps(data_or_text)
|
|
else:
|
|
payload = data_or_text
|
|
code = (
|
|
"import sys;sys.path.insert(0, %r);"
|
|
"from pi_pipeline import contract;"
|
|
"contract.Contract.from_json(sys.stdin.read())"
|
|
% str(Path(__file__).resolve().parents[1])
|
|
)
|
|
result = subprocess.run(
|
|
[sys.executable, "-c", code],
|
|
input=payload,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
assert result.returncode != 0, f"expected non-zero exit, got 0; stdout={result.stdout}"
|
|
combined = result.stdout + result.stderr
|
|
assert needle in combined, f"error message missing {needle!r}: {combined}"
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
# 派生规格 (1:1 固定)
|
|
# ----------------------------------------------------------------------
|
|
|
|
def test_derived_specs():
|
|
c1 = C.Contract.from_dict(base_contract())
|
|
assert c1.per_cam_resolution == (224, 224)
|
|
assert c1.tokens_per_cam == 256
|
|
assert c1.prefix_tokens == 4 * 256 + 200
|
|
|
|
# 旧契约带 aspect_ratio: "1:1" 兼容, 其余取值非 0 退出
|
|
c_legacy = C.Contract.from_dict(base_contract(aspect_ratio="1:1"))
|
|
assert c_legacy.prefix_tokens == 4 * 256 + 200
|
|
|
|
|
|
def test_robotwin_default():
|
|
c = C.Contract.robotwin_default()
|
|
assert c.cameras == ("cam1", "cam2", "cam3", "cam4")
|
|
assert c.vision_map["observation.images.cam_high"] == ("cam1",)
|
|
assert c.state_source == "state"
|
|
assert c.action_source == "actions"
|
|
assert c.prompt_source == "prompt"
|
|
assert c.discrete_state_input is True
|
|
assert c.use_quantile_norm is False
|
|
kw = c.pi0_config_kwargs()
|
|
assert kw["image_keys"] == ("cam1", "cam2", "cam3", "cam4")
|
|
assert kw["pi05"] is True
|
|
|
|
|
|
def test_model_structure_roundtrip(tmp_path=None):
|
|
c = C.Contract.from_dict(base_contract())
|
|
structure = c.to_model_structure()
|
|
assert structure["derived"]["tokens_per_cam"] == 256
|
|
assert structure["derived"]["prefix_tokens"] == 4 * 256 + 200
|
|
restored = C.Contract.from_dict(structure["contract"])
|
|
assert restored.to_dict() == c.to_dict()
|
|
|
|
|
|
def test_one_to_many_mapping():
|
|
"""一对多: 一个外部键映射到多个内部槽 (同图复用, 默认独立增广 R-11)。"""
|
|
data = base_contract()
|
|
data["cameras"] = ["cam1", "cam2", "cam3", "cam4", "cam5"]
|
|
data["key_map"]["observation.images.cam_high"] = ["cam1", "cam5"]
|
|
c = C.Contract.from_dict(data)
|
|
assert c.vision_map["observation.images.cam_high"] == ("cam1", "cam5")
|
|
assert c.num_cameras == 5
|
|
assert c.prefix_tokens == 5 * 256 + 200
|
|
|
|
|
|
def test_camera_count_variants():
|
|
for n in (1, 2, 4):
|
|
data = base_contract()
|
|
data["cameras"] = [f"cam{i}" for i in range(1, n + 1)]
|
|
keep = {k: v for k, v in data["key_map"].items() if not isinstance(v, list)}
|
|
slots = [f"cam{i}" for i in range(1, n + 1)]
|
|
keep["observation.images.cam_high"] = slots
|
|
data["key_map"] = keep
|
|
c = C.Contract.from_dict(data)
|
|
assert c.num_cameras == n
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
# 校验规则 (D15, 违例非 0 退出)
|
|
# ----------------------------------------------------------------------
|
|
|
|
def test_reject_bad_slot_name():
|
|
expect_exit(base_contract(cameras=["base_0_rgb"]), "cam<N>")
|
|
|
|
|
|
def test_reject_duplicate_slots():
|
|
expect_exit(base_contract(cameras=["cam1", "cam1"]), "duplicate")
|
|
|
|
|
|
def test_reject_bad_aspect():
|
|
expect_exit(base_contract(aspect_ratio="21:9"), "aspect_ratio")
|
|
|
|
|
|
def test_reject_removed_aspect_43():
|
|
"""非 1:1 比例 (4:3/16:9) 已移除: 旧契约使用时非 0 退出。"""
|
|
expect_exit(base_contract(aspect_ratio="4:3"), "1:1")
|
|
expect_exit(base_contract(aspect_ratio="16:9"), "1:1")
|
|
|
|
|
|
def test_reject_missing_slot_mapping():
|
|
data = base_contract()
|
|
del data["key_map"]["observation.images.cam_left_wrist"]
|
|
expect_exit(data, "cam3")
|
|
|
|
|
|
def test_reject_multi_source_same_slot():
|
|
data = base_contract()
|
|
data["key_map"]["observation.images.cam_head"] = ["cam1"]
|
|
expect_exit(data, "multiple sources")
|
|
|
|
|
|
def test_reject_bare_string_image_target():
|
|
data = base_contract()
|
|
data["key_map"]["observation.images.cam_high"] = "cam1"
|
|
expect_exit(data, "LIST")
|
|
|
|
|
|
def test_reject_unknown_slot_target():
|
|
data = base_contract()
|
|
data["key_map"]["observation.images.cam_high"] = ["cam9"]
|
|
expect_exit(data, "unknown slot")
|
|
|
|
|
|
def test_reject_multiple_state_mappings():
|
|
data = base_contract()
|
|
data["key_map"]["state"] = "state"
|
|
expect_exit(data, "state")
|
|
|
|
|
|
def test_reject_missing_action_mapping():
|
|
data = base_contract()
|
|
del data["key_map"]["action"]
|
|
expect_exit(data, "action")
|
|
|
|
|
|
def test_reject_invalid_json():
|
|
expect_exit("{not json", "not valid JSON")
|
|
|
|
|
|
def test_reject_missing_prompt():
|
|
data = base_contract()
|
|
del data["key_map"]["task"]
|
|
expect_exit(data, "prompt")
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
# 归一化模式联动 (D4/D19)
|
|
# ----------------------------------------------------------------------
|
|
|
|
def test_norm_mode_vision_only():
|
|
c = C.Contract.from_dict(base_contract(norm_mode="vision_only"))
|
|
assert c.discrete_state_input is False
|
|
|
|
|
|
def test_norm_mode_quantile():
|
|
c = C.Contract.from_dict(base_contract(norm_mode="quantile"))
|
|
assert c.use_quantile_norm is True
|
|
|
|
|
|
def test_norm_mode_rejected():
|
|
expect_exit(base_contract(norm_mode="l2"), "norm_mode")
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
# serve 逆映射
|
|
# ----------------------------------------------------------------------
|
|
|
|
def test_inverse_vision_map():
|
|
c = C.Contract.robotwin_default()
|
|
inverse = c.inverse_vision_map()
|
|
assert inverse == {
|
|
"cam1": "observation.images.cam_high",
|
|
"cam2": "observation.images.cam_third_view",
|
|
"cam3": "observation.images.cam_left_wrist",
|
|
"cam4": "observation.images.cam_right_wrist",
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
failures = 0
|
|
for name, fn in sorted(globals().items()):
|
|
if name.startswith("test_") and callable(fn):
|
|
try:
|
|
fn()
|
|
print(f"PASS {name}")
|
|
except AssertionError as exc:
|
|
failures += 1
|
|
print(f"FAIL {name}: {exc}")
|
|
except Exception as exc: # noqa: BLE001
|
|
failures += 1
|
|
print(f"ERROR {name}: {type(exc).__name__}: {exc}")
|
|
print(f"\n{'ALL PASS' if failures == 0 else f'{failures} FAILURES'}")
|
|
sys.exit(1 if failures else 0)
|