Implement typed evidence latent training and benchmarks
CI / test (push) Failing after 6s

This commit is contained in:
2026-10-01 22:44:07 +08:00
parent 1d798cb73f
commit b3774e783c
22 changed files with 747 additions and 119 deletions
+63 -21
View File
@@ -1,20 +1,49 @@
# Composable Evidence States Research
This repository contains the first-stage MVP for learning fact-level evidence slots from
synthetic images and natural-language rules. The Composer is intentionally deferred until
the extractor passes its fact-recovery checks.
第一阶段 MVP:在全合成符号世界中生成图像事实与文本事实/规则,训练 Evidence
Extractor 将两种模态映射到可比较的 latent space。Composer 暂不在本阶段实现。
## Current scope
## 数据方案 A:全合成符号世界
- Generate deterministic scenes containing colored geometric objects and spatial relations.
- Render scenes to PNG while preserving machine-readable fact triples.
- Generate textual implication rules as normalized triples.
- Build IID and held-out-combination splits without leaking held-out rule pairs into training.
- Train visual and textual slot extractors with Hungarian set matching.
- Measure triple exact match, component accuracy, precision, recall, and F1.
- Run CPU smoke tests with a tiny frozen backbone or GPU experiments with frozen CLIP.
每张图包含两个带颜色和形状的对象以及一条空间关系。manifest 保存完整结构化标注,
同时生成两类文本状态:
## Quick start
- `FACT`:例如 `obj1 is red`,结构为
`[FACT, subject, predicate, object, -, -]`。
- `RULE`:例如 `Every red object belongs to class alpha`,结构为
`[RULE, variable, antecedent predicate, antecedent object,
consequent predicate, consequent object]`。
- `NULL`:表示未被事实或规则占用的 slot。
FACT 与 RULE 共享 subject、predicate、object 分类头,但类型头明确区分二者;RULE
还有独立的 consequent predicate/object 训练目标。视觉分支只产生 FACT,文本分支同时
产生 FACT 和 RULE。因此实现不会把“事实”和“规则”当成完全相同的对象。
`test_ood` 使用训练集中明确排除的颜色–形状组合,并同时改变规则映射,用来检查组合泛化。
## 统一 latent space 的两个实验方案
### 方案 1(当前默认):共享分类头 + 跨模态对比学习
视觉和文本 extractor 使用相同的标签空间与同构分类头。Hungarian set matching 处理无序
slots;对比损失只把语义完全相同的视觉 FACT 与文本 FACT 拉近。RULE 保持为文本独有的
有类型状态,不参与错误的视觉规则对齐。
配置:`configs/mvp.yaml`,其中 `prototype.enabled: false`。
### 方案 2(已实现、默认关闭):Canonical Fact Prototype
`CanonicalFactPrototype` 将类型、主语、前件谓词/宾语以及后件谓词/宾语的可学习 embedding
组合成规范原型,并用 cosine loss 把 matched latent state 拉向该原型。类型 embedding 与
RULE 后件字段确保 FACT 和 RULE 的原型结构不同。
使用完整的 `configs/prototype.yaml` 即可单独训练和比较:
```bash
uv run python train_extractor.py --config configs/prototype.yaml --backbone tiny
```
## 运行
```bash
uv sync --extra dev
@@ -25,7 +54,7 @@ uv run python -m eval.evaluate_slots --config configs/mvp.yaml \
uv run pytest
```
For the first real GPU experiment:
正式 GPU 实验可把 `--small` 去掉并改用冻结 CLIP:
```bash
uv sync --extra dev --extra hf
@@ -33,14 +62,27 @@ uv run python -m data.build_splits --config configs/mvp.yaml
uv run python train_extractor.py --config configs/mvp.yaml --backbone clip
```
The CLIP path uses `openai/clip-vit-base-patch32` by default. It is a deliberately small
backbone for validating the extractor. A Qwen-VL adapter and the Composer belong to the next
milestone after the evidence slots are shown to recover atomic facts reliably.
## 每次训练后的独立验收
## Success gate before Composer work
以下命令运行统一 benchmark,写出 JSON,并在任一指标未达标时返回非零退出码:
- Text-rule triple F1 >= 0.95.
- Visual-fact triple F1 >= 0.90 on the IID test split.
- No severe slot collapse: predicted valid slots track the true fact count.
- Held-out rule-combination performance is reported separately from IID performance.
```bash
uv run python -m eval.benchmark_extractor \
--config configs/mvp.yaml \
--checkpoint artifacts/clip/best.pt \
--backbone clip \
--output artifacts/clip/benchmark.json \
--enforce-gates
```
| 指标 | 门槛 | 定义 |
| --- | ---: | --- |
| Text fact/rule F1 | 0.98 | IID 文本中 typed FACT 与 typed RULE 的六字段 exact-match micro F1 |
| Visual fact F1 | 0.90 | IID 图像 FACT 的四个有效字段 exact-match micro F1 |
| Cross-modal Recall@1 | 0.80 | 文本 FACT 检索全部视觉 FACT,最近邻标签完全相同 |
| Hard-negative Recall@1 | 0.75 | 候选仅含正确项与恰好错一个基础字段的视觉 FACT |
| OOD fact F1 | 0.80 | held-out 颜色–形状组合上的视觉 FACT exact-match F1 |
| NULL slot accuracy | 0.95 | permutation matching 后,全部视觉/文本 slots 的类型准确率,含 NULL |
CI 的单 epoch smoke run 会执行 benchmark 来验证整条评测管线,但不会使用
`--enforce-gates`,因为 smoke 模型并不代表正式实验是否达标。正式训练完成后使用上述强制命令。