Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
107 lines
3.8 KiB
Python
107 lines
3.8 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
SRC = Path(__file__).resolve().parents[1] / "simulation/semantic-learning/detail_refinement.py"
|
|
if not SRC.exists():
|
|
SRC = Path(__file__).with_name("detail_refinement.py")
|
|
spec = importlib.util.spec_from_file_location("detail_refinement", SRC)
|
|
probe = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(probe)
|
|
|
|
|
|
def test_group_exclusion_rejects_parent_and_record_leakage():
|
|
groups = ["a", "b", "c", "d"]
|
|
rr = [dict(sample_id=g, split="train", group=g) for g in groups[1:]]
|
|
probe.validate_groups(rr, "a", groups, groups[1:])
|
|
for parent, rows in [
|
|
(groups, rr),
|
|
(groups[1:], rr + [rr[0]]),
|
|
(groups[1:], rr[:-1]),
|
|
(groups[1:], [dict(rr[0], split="regression"), *rr[1:]]),
|
|
(groups[1:], [dict(rr[0], group="a"), *rr[1:]]),
|
|
]:
|
|
with pytest.raises(ValueError):
|
|
probe.validate_groups(rows, "a", groups, parent)
|
|
|
|
|
|
def torch_fixture():
|
|
torch = pytest.importorskip("torch")
|
|
torch.set_num_threads(2)
|
|
torch.manual_seed(19)
|
|
return (
|
|
torch,
|
|
torch.randn(1, 1, 17, 23),
|
|
torch.randint(0, 256, (1, 3, 17, 23), dtype=torch.uint8),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("arm", ["coarse_only", "native_rgb"])
|
|
def test_zero_initialization_is_exact_parent_under_c4_and_tiles(arm):
|
|
t, coarse, rgb = torch_fixture()
|
|
net = probe.network(12)
|
|
x = probe.inputs(coarse, rgb, arm)
|
|
for tile in [None, 1, 7, 32]:
|
|
with t.no_grad():
|
|
got = probe.prediction(net, coarse, x, tile)
|
|
assert t.equal(got, coarse)
|
|
assert t.equal(got >= 0, coarse >= 0)
|
|
|
|
|
|
def test_shared_initialization_and_rgb_ablation():
|
|
t, coarse, rgb = torch_fixture()
|
|
a, b = probe.network(42), probe.network(42)
|
|
assert all(t.equal(v, b.state_dict()[k]) for k, v in a.state_dict().items())
|
|
x = probe.inputs(coarse, rgb, "coarse_only")
|
|
assert t.equal(x[:, :1], coarse) and not x[:, 1:].any()
|
|
y = probe.inputs(coarse, rgb, "native_rgb")
|
|
assert t.equal(y[:, :1], coarse) and t.equal(y[:, 1:], rgb.float() / 127.5 - 1)
|
|
assert sum(p.numel() for p in a.parameters()) == 2929
|
|
|
|
|
|
@pytest.mark.parametrize("bad", ["shape", "dtype", "nonfinite", "gradient", "arm"])
|
|
def test_input_contract_rejects_invalid_pairs(bad):
|
|
t, coarse, rgb = torch_fixture()
|
|
arm = "native_rgb"
|
|
if bad == "shape":
|
|
rgb = rgb[..., :-1]
|
|
elif bad == "dtype":
|
|
rgb = rgb.float()
|
|
elif bad == "nonfinite":
|
|
coarse[0, 0, 0, 0] = float("nan")
|
|
elif bad == "gradient":
|
|
coarse.requires_grad_(True)
|
|
else:
|
|
arm = "street_patch"
|
|
with pytest.raises(ValueError):
|
|
probe.inputs(coarse, rgb, arm)
|
|
|
|
|
|
def test_nonzero_correction_tiling_c4_equivariance_and_gradients():
|
|
t, coarse, rgb = torch_fixture()
|
|
net = probe.network(42)
|
|
x = probe.inputs(coarse, rgb, "native_rgb")
|
|
opt = t.optim.AdamW(net.parameters(), lr=0.001)
|
|
yy = (rgb[:, :1].float() / 255 > 0.5).float()
|
|
for _ in range(3):
|
|
opt.zero_grad()
|
|
t.nn.functional.binary_cross_entropy_with_logits(coarse + net(x), yy).backward()
|
|
opt.step()
|
|
assert net[0].weight.grad[:, 1:].abs().sum() > 0
|
|
assert coarse.grad is None
|
|
assert net[-1].weight.abs().sum() > 0
|
|
with t.no_grad():
|
|
expected = probe.prediction(net, coarse, x)
|
|
for tile in [1, 7, 32]:
|
|
assert t.allclose(
|
|
probe.prediction(net, coarse, x, tile), expected, atol=2e-6, rtol=1e-6
|
|
)
|
|
for k in range(4):
|
|
xx = probe.inputs(t.rot90(coarse, k, [-2, -1]), t.rot90(rgb, k, [-2, -1]), "native_rgb")
|
|
got = t.rot90(probe.prediction(net, t.rot90(coarse, k, [-2, -1]), xx), -k, [-2, -1])
|
|
assert t.allclose(got, expected, atol=2e-6, rtol=1e-6)
|
|
assert not t.equal(expected, coarse)
|
|
with pytest.raises(ValueError):
|
|
probe.residual(net, x, 0)
|