093ab883be
Add loaded-standing physics, PPO training, fixed-candidate acceptance, isolated inference, and fail-closed publication. Preserve the original 120-second drift failure and bind the operator-approved exception to the exact candidate and evidence. Include pending Reach training/release infrastructure and archived packages. Leave pre-existing short-worker visualization changes and large generated training artifacts out of this commit.
202 lines
10 KiB
Python
202 lines
10 KiB
Python
#!/usr/bin/env python3
|
||
"""Select validation checkpoint, export candidate, or finalize accepted immutable release."""
|
||
|
||
import argparse
|
||
import json
|
||
import shutil
|
||
import tempfile
|
||
from pathlib import Path
|
||
|
||
from runtime_common import critical_log_lines, write_json
|
||
|
||
|
||
def manifest(directory, cfg, status):
|
||
from SkillFactory.legs.offline import sha256
|
||
from stage2_acceptance import package_version
|
||
|
||
files = {
|
||
str(path.relative_to(directory)): sha256(path)
|
||
for path in sorted(directory.rglob("*"))
|
||
if path.is_file() and path.name != "manifest.json" and "__pycache__" not in path.parts
|
||
}
|
||
write_json(
|
||
directory / "manifest.json",
|
||
{
|
||
"format": "skillfactory.skillbundle.v1",
|
||
"interface": "go2-fl-reach-leg-token-v1",
|
||
"version": package_version(directory, cfg),
|
||
"status": status,
|
||
"frozen": True,
|
||
"hidden": cfg["training"]["hidden"],
|
||
"dependencies": {"python": "3.12.x", "torch": "2.10.0 (CPU validated; CUDA evaluated separately)"},
|
||
"scope": cfg["scope"],
|
||
"files": files,
|
||
},
|
||
)
|
||
|
||
|
||
USAGE = """# Go2 FL Reach SkillBundle @VERSION@
|
||
|
||
仅固定悬空 Go2 FL,100Hz控制/500Hz物理、名义复位、2秒定点目标。
|
||
目标为 anchor frame 足端 link xyz/L,由 q_nominal+0.25*a,a∈[-.75,.75]^3 的FK生成;不是关节目标输入。
|
||
不支持接触承重、Go1、其他形态、移动根、噪声、步态、整机或实机。无 latent 技能库声明。
|
||
|
||
依赖:Python3.12,torch==2.10.0(其传递依赖由 pip 安装);CPU无Isaac依赖。
|
||
`python -m pip install torch==2.10.0 --index-url https://download.pytorch.org/whl/cpu`
|
||
只把本包目录加入 sys.path,然后:
|
||
|
||
```python
|
||
from frozen_policy import FrozenPolicy
|
||
import json, torch
|
||
from pathlib import Path
|
||
root = Path('/path/to/go2_fl_reach-@VERSION@')
|
||
binding = json.loads((root/'binding.json').read_text())
|
||
p = FrozenPolicy(root, part_id='go2_fl', manifest_sha256=binding['manifest_sha256'])
|
||
tokens = torch.load(root/'example_tokens.pt', weights_only=True) # 仅接口示例;实际须提供实时状态/目标
|
||
command = p.act(tokens) # [B,8] float32,后5槽0;前3槽声明顺序、[-1,1]
|
||
q_target = p.position_targets(tokens) # [B,3] rad,已按soft_limits裁剪
|
||
```
|
||
|
||
实际调用方必须使用**自身资产的**manifest hash(示例从包读取仅供离线演示),按binding.joint_order映射。
|
||
输入完整旧leg-token-v1字典,字段/特征顺序见binding.feature_schema;mask bool,其余float32,同device。
|
||
归一化为固定物理尺度再经训练集mean/std;全部必要状态保存在actor.pt,推理无统计/参数更新。
|
||
joint_observation为(q-q_nominal)/scale、dq/URDF限速、last_applied_action;goal与foot位置均除length_scale_m。
|
||
静态tokens/context须匹配绑定;有效槽NaN/Inf、错shape/dtype/版本/硬件hash被拒绝,padding垃圾忽略。
|
||
执行器为显式IdealPD,Kp/Kd/effort/velocity/soft limits见binding;不能把输出直接当电机力矩。
|
||
调用方维持控制时序、有效传感、动作历史与驱动限矩限速。无实机watchdog/故障安全认证。
|
||
这是独立SkillBundle v1,不是声称通用SoftwareSpec v2 adapter已经实现。
|
||
|
||
完整性:manifest.json列出所有有效负载SHA256;外部SHA256SUMS固定manifest的信任锚。
|
||
Hash防意外损坏,不是签名;只执行可信来源的Python代码。拒绝覆盖已有发布目录。
|
||
任何权重、归一化、接口或元数据变化应发布新版本。查验manifest.status,candidate不是验收发布。
|
||
"""
|
||
|
||
|
||
def export(args, cfg):
|
||
import torch
|
||
from SkillFactory.legs.offline import canonical_hash, sha256
|
||
|
||
train_result = json.loads((args.run / "train/result.json").read_text())
|
||
if not train_result["training_complete"] or train_result["updates"] != cfg["training"]["updates"]:
|
||
raise ValueError("Formal training not completed")
|
||
candidates = []
|
||
for step in cfg["selection"]["candidate_updates"]:
|
||
path = args.run / f"validation_{step:06d}/result.json"
|
||
row = json.loads(path.read_text())
|
||
weights = args.run / f"train/step_{step:06d}.pt"
|
||
if row["weight_sha256"] != sha256(weights) or row["config_sha256"] != canonical_hash(cfg):
|
||
raise ValueError("Validation provenance mismatch")
|
||
if row["passed"] and all(row["gates"].values()):
|
||
log_path = args.run / f"validation_{step:06d}.log"
|
||
if not log_path.is_file() or critical_log_lines(log_path):
|
||
raise ValueError("Missing/critical validation log")
|
||
candidates.append((row["policy"]["mean_error_m"], step, weights))
|
||
if not candidates:
|
||
raise ValueError("No candidate passed pre-registered validation gates; no release")
|
||
_, step, weights = min(candidates)
|
||
args.output.mkdir(parents=True, exist_ok=False)
|
||
if getattr(args, "release_version", None):
|
||
write_json(
|
||
args.output / "release.json",
|
||
{
|
||
"format": "skillfactory.release.v1",
|
||
"version": args.release_version,
|
||
"training_config_sha256": canonical_hash(cfg),
|
||
},
|
||
)
|
||
from stage2_acceptance import package_version
|
||
|
||
version = package_version(args.output, cfg)
|
||
checkpoint = torch.load(weights, weights_only=True, map_location="cpu")
|
||
torch.save(checkpoint["learner"]["models"]["actor"], args.output / "actor.pt")
|
||
for name in ("binding.json", "example_tokens.pt"):
|
||
shutil.copy2(args.run / "data" / name, args.output / name)
|
||
shutil.copy2(
|
||
Path(__file__).resolve().parents[1] / "source/SkillFactory/SkillFactory/legs/frozen_policy.py",
|
||
args.output / "frozen_policy.py",
|
||
)
|
||
shutil.copy2(Path(cfg["bundle"]) / "LICENSE", args.output / "ASSET_LICENSE")
|
||
shutil.copy2(args.config, args.output / "config.json")
|
||
(args.output / "README.md").write_text(USAGE.replace("@VERSION@", version))
|
||
(args.output / "requirements.txt").write_text("torch==2.10.0\n")
|
||
selection = {
|
||
"selected_update": step,
|
||
"checkpoint_sha256": sha256(weights),
|
||
"actor_sha256": sha256(args.output / "actor.pt"),
|
||
"rule": cfg["selection"]["rule"],
|
||
"selection_seeds": [cfg["selection"]["seed"] + i for i in range(cfg["selection"]["batches"])],
|
||
"config_sha256": canonical_hash(cfg),
|
||
"training_identity": checkpoint["identity"],
|
||
}
|
||
write_json(args.output / "selection.json", selection)
|
||
manifest(args.output, cfg, "candidate")
|
||
print(json.dumps(selection, indent=2))
|
||
|
||
|
||
def finalize(args, cfg):
|
||
from stage2_acceptance import evaluate_acceptance, evaluate_release, package_version, require_acceptance
|
||
from verify_frozen_leg import verify_package
|
||
|
||
if args.output.exists():
|
||
raise ValueError("Immutable version already exists; publish a new version")
|
||
if getattr(args, "release_version", None) not in (None, package_version(args.candidate, cfg)):
|
||
raise ValueError("Finalization cannot change the tested candidate version")
|
||
# Mandatory engineering/resume/data/training/selection/physics/load gates are shared
|
||
# with the delivery checker. Do not create any public package before they pass.
|
||
require_acceptance(evaluate_acceptance(args.run, args.candidate, cfg))
|
||
if not getattr(args, "python", None):
|
||
raise ValueError("Finalization requires --python pointing to an isolated inference interpreter")
|
||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||
lock = args.output.parent / f".{args.output.name}.publish.lock"
|
||
with lock.open("x"):
|
||
try:
|
||
with tempfile.TemporaryDirectory(prefix=f".{args.output.name}-", dir=args.output.parent) as tmp:
|
||
staged = Path(tmp) / "package"
|
||
shutil.copytree(args.candidate, staged, ignore=shutil.ignore_patterns("__pycache__"))
|
||
evidence = staged / "evidence"
|
||
evidence.mkdir(exist_ok=True)
|
||
for mode in ("test", "boundary", "stability"):
|
||
shutil.copy2(args.run / mode / "result.json", evidence / f"{mode}.json")
|
||
for name in ("standalone.json", "resume_exact.json", "engineering_checks.json"):
|
||
shutil.copy2(args.run / name, evidence / name)
|
||
shutil.copy2(args.candidate / "manifest.json", evidence / "tested_candidate_manifest.json")
|
||
for step in cfg["selection"]["candidate_updates"]:
|
||
shutil.copy2(
|
||
args.run / f"validation_{step:06d}/result.json", evidence / f"validation_{step:06d}.json"
|
||
)
|
||
for name in ("train/result.json", "data/dataset.json"):
|
||
shutil.copy2(args.run / name, evidence / name.replace("/", "_"))
|
||
for name in ("provenance.json", "inference_dependencies.txt"):
|
||
if (args.run / name).is_file():
|
||
shutil.copy2(args.run / name, evidence / name)
|
||
# Final metadata changes the manifest hash: independently load these exact
|
||
# staged bytes too, before the accepted version becomes publicly visible.
|
||
manifest(staged, cfg, "accepted")
|
||
standalone = verify_package(staged, args.python, args.run / "release_standalone.json")
|
||
require_acceptance(evaluate_release(args.run, staged, cfg, standalone))
|
||
if args.output.exists():
|
||
raise ValueError("Immutable version already exists; publish a new version")
|
||
staged.rename(args.output)
|
||
finally:
|
||
lock.unlink(missing_ok=True)
|
||
print(f"Accepted immutable package: {args.output}")
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser(description=__doc__)
|
||
parser.add_argument("--config", type=Path, required=True)
|
||
parser.add_argument("--run", type=Path, required=True)
|
||
parser.add_argument("--output", type=Path, required=True)
|
||
parser.add_argument("--candidate", type=Path, help="If supplied, finalize this candidate after independent gates")
|
||
parser.add_argument("--python", type=Path, help="Required for finalization: isolated CPU inference interpreter")
|
||
parser.add_argument(
|
||
"--release-version", help="Candidate release version; does not modify the locked training config"
|
||
)
|
||
args = parser.parse_args()
|
||
cfg = json.loads(args.config.read_text())
|
||
finalize(args, cfg) if args.candidate else export(args, cfg)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|