feat(spatial): M0 契约、生成器、检查器与关系主干
按 mm/SPATIAL_FOUNDATION.md 落一个 CPU 可跑的 M0:数据契约把「可见观测」和 「完整状态」分开,标签只由检查器从完整状态算出,可见世界与完整世界答案不一致 时降级为 unknown。 - schema: Observation / FullState / Query / Label,整数几何,digest 可复现 - generator: 3 域 10 族(导通 / 支撑 / 约束),含隐藏支路与冗余约束 - checker: 三种判定 + 证据校验 + 三值标签(同观测不同真值 → 不足) - model: 双向注意力,边类型作非对称 bias,三头(答案 / 变化 / 证据) - train: 原子 checkpoint(含优化器、步数、RNG),可续训 - cli: smoke 覆盖切分不相交、同种子复现、标签可复算、单位与相机不变、 一次更新 + 断点恢复 审查中修掉的缺陷(前三项致命): 1. query.edit 根本没有进入输入,模型看不到「删了哪条边」,单族训练只能达到 多数类基线(0.719 = 0.719)。补上 edit token 与边级 removed 标记后到 1.0。 2. 证据头只用两端实体相加打分,看不到边特征,无法排除被删的边 → 有效率 0.464。 改为把边类型与被删标记并入证据打分后到 1.0。 3. 证据监督把检查器选的那一条路径当唯一正解,同构的合法路径被当成负例。 改为「任意合法路径上的边都算正」。 4. 生成器把隐藏支路的孤立 via 放进了观测,构成泄漏。 5. 注意力 bias 的 batch 索引张量维度错误;edit token 广播成 (B,B,d)。 6. resume 时 RNG 状态被带到 GPU、优化器状态留在 CPU。 实测(4 层 / 192 宽 / 1.8M,cond.p3 单族,400 步 / 3.9s): answer 1.000,change 1.000,evidence_valid 1.000,多数类基线 0.719。
This commit is contained in:
1 parent
2517132a53
commit
f31288bf06
10 files changed
+1375
No files matched your search
@@ -0,0 +1,212 @@
|
||||
"""Procedural scenes.
|
||||
|
||||
Three domains, each built so that a *structure family* -- not a single sample --
|
||||
is the unit of split. Families differ in topology depth and in what the
|
||||
observation withholds, never in rendering noise, so a test family is genuinely
|
||||
unseen structure.
|
||||
|
||||
Builders only construct geometry and visible subsets. They never write a label:
|
||||
``checker.label`` does that, using the full state plus the world obtained by
|
||||
restricting the full state to what the observation shows.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from dataclasses import replace
|
||||
|
||||
from . import checker
|
||||
from .schema import (DOF_AXES, Edge, Entity, FullState, Observation, Query,
|
||||
Scenario)
|
||||
|
||||
# family -> builder parameters. Splits are assigned in configs/*.json and must
|
||||
# be disjoint over families.
|
||||
FAMILIES = {
|
||||
"cond.p1": {"domain": "connection", "n_paths": 1, "hidden": 0},
|
||||
"cond.p3": {"domain": "connection", "n_paths": 3, "hidden": 0},
|
||||
"cond.p1h": {"domain": "connection", "n_paths": 2, "hidden": 1},
|
||||
"cond.p3h": {"domain": "connection", "n_paths": 4, "hidden": 1},
|
||||
"area.sup2": {"domain": "stability", "n_blocks": 2, "hidden": 0},
|
||||
"area.sup3": {"domain": "stability", "n_blocks": 3, "hidden": 0},
|
||||
"area.sup2h": {"domain": "stability", "n_blocks": 2, "hidden": 1},
|
||||
"dof.c6": {"domain": "freedom", "n_cons": 6, "hidden": 0},
|
||||
"dof.c6r": {"domain": "freedom", "n_cons": 7, "hidden": 0},
|
||||
"dof.c6h": {"domain": "freedom", "n_cons": 6, "hidden": 1},
|
||||
}
|
||||
|
||||
DOMAIN_OF = {f: spec["domain"] for f, spec in FAMILIES.items()}
|
||||
|
||||
|
||||
def _observe(entities, edges, visible_edges, visible_ents, hidden_layers, cameras, units):
|
||||
vis_e = [e for e in edges if e.eid in visible_edges]
|
||||
keep = set(visible_ents) | {e.a for e in vis_e} | {e.b for e in vis_e}
|
||||
return Observation(
|
||||
entities=[e for e in entities if e.eid in keep],
|
||||
edges=vis_e,
|
||||
cameras=list(cameras),
|
||||
units=dict(units),
|
||||
hidden_layers=sorted(hidden_layers),
|
||||
)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# connection: parallel branches into one trunk, optional inner-layer branch
|
||||
#
|
||||
# A --branch i--> M --trunk--> B
|
||||
#
|
||||
# removing one branch is harmless while another exists; removing the trunk is
|
||||
# never harmless. With one visible branch and one hidden branch the observation
|
||||
# cannot tell those apart.
|
||||
|
||||
|
||||
def _build_connection(rng, n_paths: int, hidden: int):
|
||||
ents = [Entity("A", "pad", attrs={"x": 0, "y": 0, "z": 0, "layer": 0}),
|
||||
Entity("B", "pad", attrs={"x": 100, "y": 0, "z": 0, "layer": 0}),
|
||||
Entity("M", "via", attrs={"x": 50, "y": 0, "z": 0, "layer": 0})]
|
||||
edges, visible, layers = [], set(), set()
|
||||
branch_edges = []
|
||||
for i in range(n_paths):
|
||||
is_hidden = i >= (n_paths - hidden)
|
||||
layer = (1 + i % 2) if is_hidden else 0
|
||||
vid = f"V{i}"
|
||||
ents.append(Entity(vid, "via", attrs={"x": 50, "y": 10 * (i + 1), "z": layer, "layer": layer}))
|
||||
e1 = Edge(f"c{i}a", "conduct", "A", vid, layer=layer)
|
||||
e2 = Edge(f"c{i}b", "conduct", vid, "M", layer=layer)
|
||||
edges += [e1, e2]
|
||||
if layer == 0:
|
||||
visible.update({e1.eid, e2.eid})
|
||||
branch_edges += [e1.eid, e2.eid]
|
||||
else:
|
||||
layers.add(layer)
|
||||
trunk = Edge("trunk", "conduct", "M", "B", layer=0)
|
||||
edges.append(trunk)
|
||||
visible.add(trunk.eid)
|
||||
|
||||
query = Query(kind="connection", a="A", b="B")
|
||||
r = rng.random()
|
||||
if r < 0.4 and branch_edges:
|
||||
query.edit = {"op": "remove_edge", "edge": branch_edges[0]}
|
||||
elif r < 0.7:
|
||||
query.edit = {"op": "remove_edge", "edge": "trunk"}
|
||||
|
||||
# a hidden branch must not appear as an isolated node either: the mere
|
||||
# presence of a via on layer 1 is already a leak of the hidden detour
|
||||
vis_ents = {"A", "B", "M"} | {f"V{i}" for i in range(n_paths - hidden)}
|
||||
return ents, edges, query, visible, vis_ents, layers, ["top"], {"length": "mm", "per_unit": 1}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# stability: stacked blocks, optional hidden pedestal
|
||||
|
||||
|
||||
def _build_stability(rng, n_blocks: int, hidden: int):
|
||||
ents = [Entity("G", "ground", attrs={"plane": 0}),
|
||||
Entity("P", "block", attrs={"box": [40, 40, 0, 60, 60, 20]}),
|
||||
Entity("T", "block", attrs={"box": [42, 42, 20, 58, 58, 36]})]
|
||||
edges = [Edge("kPG", "contact", "P", "G"), Edge("kPT", "contact", "T", "P")]
|
||||
if n_blocks >= 3:
|
||||
ents.append(Entity("U", "block", attrs={"box": [44, 44, 36, 56, 56, 48]}))
|
||||
edges.append(Edge("kTU", "contact", "U", "T"))
|
||||
vis_edges = {e.eid for e in edges}
|
||||
vis_ents = {e.eid for e in ents}
|
||||
layers = set()
|
||||
|
||||
if hidden:
|
||||
# the pedestal sits behind the front face: its contacts are unseen, and
|
||||
# the pedestal itself is not in view
|
||||
for e in list(edges):
|
||||
if "P" in (e.a, e.b):
|
||||
vis_edges.discard(e.eid)
|
||||
vis_ents.discard("P")
|
||||
|
||||
target = "U" if n_blocks >= 3 else "T"
|
||||
query = Query(kind="stability", a=target)
|
||||
# an edit can only name something the observation shows; with the pedestal
|
||||
# hidden the only legal edit is "nothing changes"
|
||||
if not hidden and rng.random() < 0.5:
|
||||
query.edit = {"op": "remove_entity", "entity": "P"}
|
||||
return ents, edges, query, vis_edges, vis_ents, layers, ["front", "iso"], {"length": "mm", "per_unit": 1}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# freedom: constraint sets, optionally redundant on one axis
|
||||
|
||||
|
||||
def _build_freedom(rng, n_cons: int, hidden: int):
|
||||
axes = list(DOF_AXES) + ["tz"] # a 7th constraint repeats tz: redundant
|
||||
ents = [Entity("P", "part", attrs={"x": 0, "y": 0, "z": 0})]
|
||||
edges, visible = [], set()
|
||||
for i in range(n_cons):
|
||||
fid = f"F{i}"
|
||||
ents.append(Entity(fid, "ground", attrs={"x": 0, "y": 0, "z": 0}))
|
||||
e = Edge(f"k{i}", "constraint", "P", fid, attrs={"axis": axes[i]})
|
||||
edges.append(e)
|
||||
visible.add(e.eid)
|
||||
|
||||
vis_ents = {e.eid for e in ents}
|
||||
layers = set()
|
||||
if hidden:
|
||||
visible.discard("k5") # one mate lives in the part definition
|
||||
vis_ents.discard("F5")
|
||||
|
||||
query = Query(kind="freedom", a="P")
|
||||
if n_cons == 7:
|
||||
# removing the duplicate must not change the verdict; that is the point
|
||||
query.edit = {"op": "remove_edge", "edge": "k6"}
|
||||
elif rng.random() < 0.6:
|
||||
query.edit = {"op": "remove_edge", "edge": "k0"}
|
||||
return ents, edges, query, visible, vis_ents, layers, ["iso"], {"length": "mm", "per_unit": 1}
|
||||
|
||||
|
||||
_BUILDERS = {
|
||||
"connection": lambda rng, spec: _build_connection(rng, spec["n_paths"], spec["hidden"]),
|
||||
"stability": lambda rng, spec: _build_stability(rng, spec["n_blocks"], spec["hidden"]),
|
||||
"freedom": lambda rng, spec: _build_freedom(rng, spec["n_cons"], spec["hidden"]),
|
||||
}
|
||||
|
||||
|
||||
def make(family: str, seed: int, index: int = 0) -> Scenario:
|
||||
spec = FAMILIES[family]
|
||||
rng = random.Random(f"{family}:{seed}:{index}")
|
||||
ents, edges, query, vis_edges, vis_ents, layers, cameras, units = \
|
||||
_BUILDERS[spec["domain"]](rng, spec)
|
||||
|
||||
full = FullState(entities=ents, edges=edges)
|
||||
obs = _observe(ents, edges, vis_edges, vis_ents, layers, cameras, units)
|
||||
# the world the observation alone describes: anything not visible is gone
|
||||
restricted = FullState(entities=list(obs.entities), edges=list(obs.edges))
|
||||
|
||||
sc = Scenario(key=f"{family}-{seed}-{index:04d}", domain=spec["domain"],
|
||||
family=family, seed=seed, obs=obs, query=query, full=full,
|
||||
tags={"n_hidden": spec["hidden"], "edit": query.edit is not None})
|
||||
sc.label = checker.label(obs, full, query, alternatives=[restricted])
|
||||
return sc
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# invariances: camera and units must not change the answer
|
||||
|
||||
|
||||
def with_camera(sc: Scenario, camera: str) -> Scenario:
|
||||
out = replace(sc, obs=replace(sc.obs, cameras=[camera]))
|
||||
out.tags = dict(sc.tags, camera=camera)
|
||||
return out
|
||||
|
||||
|
||||
def rescale(sc: Scenario, factor: int, unit: str) -> Scenario:
|
||||
"""Multiply integer geometry by ``factor`` and relabel the unit. The answer
|
||||
must be invariant; the fixture checks that it actually is."""
|
||||
def scale_ent(e: Entity) -> Entity:
|
||||
attrs = dict(e.attrs)
|
||||
for k in ("x", "y", "z"):
|
||||
if k in attrs:
|
||||
attrs[k] = attrs[k] * factor
|
||||
if "box" in attrs:
|
||||
attrs["box"] = [v * factor for v in attrs["box"]]
|
||||
return replace(e, attrs=attrs, unit=unit)
|
||||
|
||||
full = FullState(entities=[scale_ent(e) for e in sc.full.entities], edges=list(sc.full.edges))
|
||||
obs = replace(sc.obs, entities=[scale_ent(e) for e in sc.obs.entities],
|
||||
edges=list(sc.obs.edges),
|
||||
units={"length": unit, "per_unit": factor})
|
||||
out = replace(sc, obs=obs, full=full, tags=dict(sc.tags, unit=unit, scale=factor))
|
||||
return out
|
||||
Reference in new issue
Block a user