feat: checkpoint contact-cycle evaluation and RootXY PPO progress
Consolidate next-stage docs, archive superseded narratives, and preserve frozen document inputs. Retain invalid P2 pairs, partial RootXY training, failed attempts and nonselectable checkpoints as explicit limitations. Include supervised evaluation/training, desktop GPU admission, bounded evidence and atomic-publication monitor repairs with CPU regressions. Ignore local dotenv configuration. Validation: 1403 passed, 6 skipped for contact-cycle and related contracts/CLI tests. Full-suite legacy ABI and registration tests require Python 3.12/Torch 2.10/pxr; no runtime checks relaxed. No new physical run or accepted skill.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CPU-only, explicitly reviewed external P1 adoption; no grant, reservation or Kit.
|
||||
|
||||
Writes only an allowlisted P2 root's p1-adoption.json, exclusively and read-only.
|
||||
The parent must independently review its digest before any extended P2 grant.
|
||||
"""
|
||||
import argparse
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--parent-review', required=True)
|
||||
parser.add_argument('--trusted-source-budget-sha256', required=True)
|
||||
parser.add_argument('--artifact-root', type=Path, default=Path('/home/cen/sf-runs/p2-01'),
|
||||
choices=[Path('/home/cen/sf-runs/p2-01'), Path('/home/cen/sf-runs/p2-02')])
|
||||
args = parser.parse_args(argv)
|
||||
spec = importlib.util.spec_from_file_location('_adoption_runner', ROOT / 'scripts/evaluate_cycle_sim_residual.py')
|
||||
runner = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(runner)
|
||||
adoption = runner.cpu_module('evaluation_adoption')
|
||||
print(adoption.create_adoption(parent_review=args.parent_review,
|
||||
trusted_source_budget_sha256=args.trusted_source_budget_sha256,
|
||||
target_root=args.artifact_root))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Offline CPU trace readback. Audit consistency is NOT skill acceptance."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
from runtime_common import write_json
|
||||
from SkillFactory.legs.contact_cycle.metrics import summarize
|
||||
|
||||
|
||||
def audit(run):
|
||||
run = Path(run)
|
||||
report = json.loads((run/"result.json").read_text())
|
||||
cfg = json.loads((run/"config.json").read_text())
|
||||
locked = json.loads((run/"invocation.json").read_text())["thresholds"]["gates"]
|
||||
trace = torch.load(run/"physics_trace.pt", map_location="cpu", weights_only=True)
|
||||
rows = summarize(trace, report["physics_dt"], locked, cfg["scenarios"])
|
||||
samples = round(sum(cfg["phase_durations_s"])/report["physics_dt"])
|
||||
gates = {
|
||||
"not_skill_acceptance": report["accepted_skill"] is False and report["trained_skill"] is False,
|
||||
"same_rows_from_raw_trace": rows == report["rows"],
|
||||
"summary_status_consistent": (
|
||||
report["passed"] == all(report["gates"].values())
|
||||
and report["gates"]["all_scenario_metrics"] == all(r["success"] for r in rows)
|
||||
and report["status"] == ("completed" if report["passed"] else "failed")
|
||||
),
|
||||
"complete_duration": len(trace["time"]) == samples == report["physics_steps"],
|
||||
"monotonic_physics_clock": bool(torch.allclose(trace["time"][1:]-trace["time"][:-1],
|
||||
torch.full_like(trace["time"][1:], report["physics_dt"]),
|
||||
atol=1e-6, rtol=0)),
|
||||
}
|
||||
identity = json.loads((run/"identity.json").read_text())
|
||||
snapshot_files = list((run/"source_snapshot").rglob("*"))
|
||||
hashes = {hashlib.sha256(p.read_bytes()).hexdigest() for p in snapshot_files if p.is_file()}
|
||||
gates["bound_source_snapshots"] = all(digest in hashes for digest in identity.values())
|
||||
# Independent scalar loop for the slip integral (does not call summarize's implementation).
|
||||
slip = []
|
||||
for e in range(trace["phase"].shape[1]):
|
||||
total = 0.0
|
||||
for k in range(len(trace["time"])):
|
||||
phase, progress = int(trace["phase"][k, e]), float(trace["progress"][k, e])
|
||||
stance = (phase == 0 and float(trace["time"][k, e]) >= 2) or (phase == 5 and progress >= 0.8)
|
||||
if stance and float(trace["normal"][k, e, 2]) > 2:
|
||||
total += float(trace["slip_speed"][k, e]) * report["physics_dt"]
|
||||
slip.append(total)
|
||||
gates["independent_slip_integral"] = all(
|
||||
not r["complete"] or not r["finite"] or abs(slip[i]-r["stance_slip_m"]) <= 1e-5
|
||||
for i, r in enumerate(rows))
|
||||
failures = []
|
||||
for row in rows:
|
||||
if not row["success"]:
|
||||
e = row["env"]
|
||||
indices = torch.nonzero(trace["would_fail"][:, e]).flatten()
|
||||
failures.append({"env": e, "scenario": row["scenario"], "reasons": row["failure_reasons"],
|
||||
"first_task_failure_s": float(trace["time"][indices[0], e]) if len(indices) else None,
|
||||
"peak_height_error_s": float(trace["time"][trace["height_error"][:, e].argmax(), e])})
|
||||
return {"passed": all(gates.values()), "accepted_skill": False, "audit_only": True,
|
||||
"original_physical_passed": report["passed"], "gates": gates, "failure_cases": failures,
|
||||
"trace_sha256": hashlib.sha256((run/"physics_trace.pt").read_bytes()).hexdigest(),
|
||||
"report_sha256": hashlib.sha256((run/"result.json").read_bytes()).hexdigest()}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--run", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
if args.output.exists():
|
||||
parser.error("Refusing to overwrite audit evidence")
|
||||
result = audit(args.run)
|
||||
write_json(args.output, result)
|
||||
raise SystemExit(0 if result["passed"] else 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,158 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Offline, nominal simulation-function check. Never runs Kit, a policy or an optimizer."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_CONFIG = ROOT / 'configs/contact_cycle/simulation_functional_v1.json'
|
||||
|
||||
|
||||
def longest_run(mask):
|
||||
longest = current = 0
|
||||
for value in mask.tolist():
|
||||
current = current + 1 if value else 0
|
||||
longest = max(longest, current)
|
||||
return longest
|
||||
|
||||
|
||||
def evaluate(trace, cfg, controller_kind):
|
||||
"""One actual first episode, [T,1,...]. Early stops/resets cannot be repaired by relabeling."""
|
||||
if controller_kind not in ('analytic_reference', 'learned_policy'):
|
||||
raise ValueError('Declare the controller evidence type')
|
||||
if (cfg['format'] != 'skillfactory.contact-cycle-simulation-functional.v1'
|
||||
or len(cfg['phase_durations_s']) != 6
|
||||
or any(type(x) not in (int, float) or not math.isfinite(x) or x <= 0
|
||||
for x in [cfg['physics_dt_s'], *cfg['phase_durations_s'], *cfg['gates'].values()])
|
||||
or type(cfg['gates']['minimum_complete_cycles']) is not int):
|
||||
raise ValueError('Invalid functional contract')
|
||||
required = {'time', 'phase', 'scenario', 'weight', 'normal', 'foot', 'target', 'gap',
|
||||
'signed_height_error', 'tilt', 'external', 'guide', 'auxiliary', 'joint_excess',
|
||||
'limit_ratio', 'contact_acquired', 'actor_metadata_available', 'control_action_available'}
|
||||
out = {'controller_kind': controller_kind, 'sim_functional_passed': False,
|
||||
'accepted_skill': False, 'gates': {}, 'metrics': {}}
|
||||
if not required <= trace.keys():
|
||||
out['missing_fields'] = sorted(required - trace.keys())
|
||||
return out
|
||||
n = len(trace['time'])
|
||||
if not n or any(v.shape[:2] != (n, 1) for v in trace.values()):
|
||||
out['error'] = 'Require nonempty single-episode [T,1,...] arrays'
|
||||
return out
|
||||
gates, metrics = out['gates'], out['metrics']
|
||||
gates['finite'] = all(bool(torch.isfinite(v).all()) for v in trace.values())
|
||||
if not gates['finite']:
|
||||
return out
|
||||
d = {k: v[:, 0] for k, v in trace.items()}
|
||||
t, dt, g = d['time'], cfg['physics_dt_s'], cfg['gates']
|
||||
duration = sum(cfg['phase_durations_s'])
|
||||
expected = torch.arange(1, round(duration / dt) + 1, dtype=t.dtype, device=t.device) * dt
|
||||
gates['complete_clock_without_reset'] = n == len(expected) and bool(torch.allclose(t, expected, atol=1e-5, rtol=0))
|
||||
gates['nominal_only'] = bool((d['scenario'] == cfg['nominal_scenario_id']).all())
|
||||
gates['no_external_disturbance'] = bool((d['external'].abs() <= 1e-6).all())
|
||||
gates['actions_recorded'] = bool(d['control_action_available'].all())
|
||||
gates['controller_metadata'] = (bool(d['actor_metadata_available'].all()) if controller_kind == 'learned_policy'
|
||||
else not bool(d['actor_metadata_available'].any()))
|
||||
if not gates['complete_clock_without_reset']:
|
||||
metrics['actual_end_s'] = float(t[-1])
|
||||
return out
|
||||
ends = t.new_tensor(cfg['phase_durations_s']).cumsum(0)
|
||||
phase = torch.bucketize(t.contiguous(), ends[:-1], right=True)
|
||||
gates['six_command_phases'] = bool(torch.equal(d['phase'], phase)) and set(phase.tolist()) == set(range(6))
|
||||
if not gates['six_command_phases']:
|
||||
return out
|
||||
normal, weight = d['normal'][:, 2], d['weight']
|
||||
gates['positive_weight'] = bool((weight > 0).all())
|
||||
if not gates['positive_weight']:
|
||||
return out
|
||||
windows = {'support': (phase == 0) & (t >= ends[0] - g['support_final_window_s']),
|
||||
'unload': (phase == 1) & (t >= ends[1] - g['unload_final_window_s']),
|
||||
'reload': (phase == 5) & (t >= duration - g['reload_final_window_s'])}
|
||||
for name, mask in windows.items():
|
||||
contact = float((normal[mask] > g['contact_force_on_N']).float().mean())
|
||||
load = float((normal[mask] / weight[mask]).mean())
|
||||
metrics[name + '_contact_fraction'] = contact
|
||||
metrics[name + '_load_fraction'] = load
|
||||
if name != 'unload':
|
||||
gates[name + '_ground_support'] = (contact >= g['support_reload_contact_fraction_min']
|
||||
and load >= g['support_reload_load_fraction_min'])
|
||||
else:
|
||||
metrics['unload_remaining_support_fraction'] = load / max(metrics['support_load_fraction'], 1e-6)
|
||||
gates['unloaded'] = (metrics['unload_remaining_support_fraction']
|
||||
<= g['unload_remaining_support_fraction_max'])
|
||||
air = (d['gap'] >= g['swing_gap_m_min']) & (normal <= g['contact_force_on_N'])
|
||||
swing = phase == 3
|
||||
metrics['swing_air_fraction'] = float(air[swing].float().mean())
|
||||
metrics['swing_contiguous_air_s'] = longest_run(air[swing]) * dt
|
||||
gates['real_flight'] = (metrics['swing_air_fraction'] >= g['swing_air_fraction_min']
|
||||
and metrics['swing_contiguous_air_s'] >= g['swing_contiguous_air_s_min'])
|
||||
pre, post = (phase == 2).nonzero()[-1, 0], swing.nonzero()[-1, 0]
|
||||
requested = d['target'][post, :2] - d['target'][pre, :2]
|
||||
actual = d['foot'][post, :2] - d['foot'][pre, :2]
|
||||
gates['three_dimensional_command'] = bool((requested.abs() > .001).all())
|
||||
progress = actual * requested.sign() / requested.abs().clamp_min(1e-6)
|
||||
metrics['swing_xy_progress_fraction'] = progress.tolist()
|
||||
gates['xy_swing_motion'] = bool((progress >= g['swing_xy_progress_fraction_min']).all())
|
||||
landing_error = (d['foot'][windows['reload']] - d['target'][windows['reload']]).norm(dim=-1)
|
||||
metrics['landing_error_m'] = float(landing_error.mean())
|
||||
gates['landing_position'] = metrics['landing_error_m'] <= g['landing_position_error_m_max']
|
||||
gates['contact_acquired'] = bool(d['contact_acquired'].any())
|
||||
gates['no_gross_collapse'] = (float(d['signed_height_error'].abs().max()) <= g['root_height_error_m_max']
|
||||
and float(d['tilt'].max()) <= g['root_tilt_rad_max'])
|
||||
gates['unilateral_contact'] = float(normal.min()) >= -g['normal_force_negative_tolerance_N']
|
||||
gates['no_gross_penetration'] = float(d['gap'].min()) >= -g['ground_penetration_m_max']
|
||||
gates['actuator_joint_bounds'] = (float(d['limit_ratio'].max()) <= g['normalized_effort_velocity_max']
|
||||
and float(d['joint_excess'].max()) <= g['joint_limit_excess_rad_max'])
|
||||
auxiliary = d['auxiliary']
|
||||
# A point-applied vertical force can have COM-reduced x/y moments, but no free couple.
|
||||
# The declared payload COM offset has norm .06m; do not blanket-zero valid r x F.
|
||||
moment_bound = auxiliary[..., 2].clamp_min(0) * g['auxiliary_lever_arm_m_max'] + 1e-6
|
||||
gates['declared_fixture'] = (float(d['guide'][..., 2].abs().max()) <= g['guide_vertical_N_max']
|
||||
and float(auxiliary[..., :2].abs().max()) <= 1e-6
|
||||
and float(auxiliary[..., 2].min()) >= -1e-6
|
||||
and float(auxiliary[..., 2].sum(-1).max()) <= g['auxiliary_vertical_N_max']
|
||||
and float(auxiliary[..., 5].abs().max()) <= 1e-6
|
||||
and bool((auxiliary[..., 3:].norm(dim=-1) <= moment_bound).all())
|
||||
and float(auxiliary[phase == 0].abs().max()) <= 1e-6)
|
||||
out['sim_functional_passed'] = all(gates.values())
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--trace', type=Path, required=True)
|
||||
parser.add_argument('--config', type=Path, default=DEFAULT_CONFIG)
|
||||
parser.add_argument('--controller-kind', choices=['analytic_reference', 'learned_policy'], required=True)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
if args.output.exists():
|
||||
raise FileExistsError('Never overwrite an earlier audit')
|
||||
cfg = json.loads(args.config.read_text())
|
||||
raw = torch.load(args.trace, map_location='cpu', weights_only=True)
|
||||
if 'time' in raw:
|
||||
traces = {str(e): {k: v[:, e:e+1] for k, v in raw.items()}
|
||||
for e in range(raw['time'].shape[1])}
|
||||
else:
|
||||
traces = raw # saved validation subsets keyed by environment
|
||||
rows = {e: evaluate(d, cfg, args.controller_kind) for e, d in traces.items()}
|
||||
passed = sum(r['sim_functional_passed'] for r in rows.values())
|
||||
result = {'format': cfg['format'], 'controller_kind': args.controller_kind,
|
||||
'sim_functional_passed': passed >= cfg['gates']['minimum_complete_cycles'],
|
||||
'trace_path': str(args.trace), 'trace_sha256': hashlib.sha256(args.trace.read_bytes()).hexdigest(),
|
||||
'config_sha256': hashlib.sha256(args.config.read_bytes()).hexdigest(),
|
||||
'audit_source_sha256': hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), 'rows': rows,
|
||||
'functional_cycles_passed': passed,
|
||||
'accepted_skill': False, 'new_simulation_steps': 0, 'new_optimizer_steps': 0,
|
||||
'note': 'Retrospective development replay. Non-nominal rows are outside this scope; '
|
||||
'no learned claim from analytic control.'}
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(result, indent=2, allow_nan=False) + '\n')
|
||||
print(json.dumps({k: v for k, v in result.items() if k != 'rows'}, indent=2))
|
||||
raise SystemExit(0 if result['sim_functional_passed'] else 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CPU-only JSONL evidence re-audit; no policy, physics, registration or optimizer."""
|
||||
import argparse
|
||||
import importlib.util
|
||||
import json
|
||||
import lzma
|
||||
import sys
|
||||
import time
|
||||
import types
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def cpu_modules():
|
||||
# Private offline namespace, NOT SkillFactory and never used for frozen identity verification.
|
||||
directory = ROOT / 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim'
|
||||
name = '_contact_cycle_offline_cpu'
|
||||
package = types.ModuleType(name)
|
||||
package.__path__ = [str(directory)]
|
||||
sys.modules[name] = package
|
||||
modules = []
|
||||
for leaf in ('evaluation_protocol', 'evaluation_audit'):
|
||||
spec = importlib.util.spec_from_file_location(name + '.' + leaf, directory / (leaf + '.py'))
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = module
|
||||
spec.loader.exec_module(module)
|
||||
modules.append(module)
|
||||
return modules
|
||||
|
||||
|
||||
def trace_lines(path, *, compressed, max_bytes, max_record_bytes, deadline, max_decoded_bytes=None):
|
||||
"""Incremental legacy gzip / v2 XZ / raw JSONL, never whole-stream decompression.
|
||||
|
||||
XZ decoder memory is capped, including dictionaries advertised in headers.
|
||||
Input, output, individual records and per-member output are independently
|
||||
bounded; deadlines are checked even while draining buffered decoder output.
|
||||
"""
|
||||
_, limits = cpu_modules()
|
||||
chunk_bytes = 64 * 1024
|
||||
max_member_bytes = limits.MAX_CHUNK_RAW_BYTES
|
||||
max_decoded_bytes = max_bytes * 64 if max_decoded_bytes is None else max_decoded_bytes
|
||||
xz = compressed and path.name.endswith('.xz')
|
||||
read_bytes = decoded_bytes = member_bytes = member_records = 0
|
||||
line = bytearray()
|
||||
|
||||
def check():
|
||||
if time.monotonic() > deadline or path.stat().st_size > max_bytes or read_bytes > max_bytes:
|
||||
raise ValueError('budget_audit_resource_or_record_limit')
|
||||
|
||||
decoder = None
|
||||
with path.open('rb') as source:
|
||||
while True:
|
||||
check()
|
||||
data = source.read(min(chunk_bytes, max_bytes - read_bytes + 1))
|
||||
read_bytes += len(data)
|
||||
check() # Includes EOF and time spent reading compressed headers/tails.
|
||||
if not data:
|
||||
if decoder is not None:
|
||||
raise EOFError('truncated compressed member')
|
||||
if line:
|
||||
raise ValueError('budget_audit_resource_or_record_limit')
|
||||
return
|
||||
# XZ may retain input internally. Drain it with bounded output before
|
||||
# reading more source, including when the source is already at EOF.
|
||||
while data or (xz and decoder is not None and not decoder.needs_input):
|
||||
check()
|
||||
member_done = False
|
||||
if compressed:
|
||||
if decoder is None:
|
||||
decoder = (lzma.LZMADecompressor(format=lzma.FORMAT_XZ,
|
||||
memlimit=limits.XZ_DECODER_MEMORY_BYTES)
|
||||
if xz else zlib.decompressobj(wbits=31))
|
||||
member_bytes = member_records = 0
|
||||
decoded = decoder.decompress(data, chunk_bytes)
|
||||
member_done = decoder.eof
|
||||
if xz:
|
||||
remaining = decoder.unused_data if member_done else b''
|
||||
else:
|
||||
remaining = decoder.unused_data if member_done else decoder.unconsumed_tail
|
||||
if not decoded and len(remaining) == len(data):
|
||||
raise ValueError('gzip decoder made no progress')
|
||||
data = remaining
|
||||
else:
|
||||
decoded, data = data, b''
|
||||
check()
|
||||
decoded_bytes += len(decoded)
|
||||
member_bytes += len(decoded)
|
||||
if decoded_bytes > max_decoded_bytes or compressed and member_bytes > max_member_bytes:
|
||||
raise ValueError('budget_audit_decoded_limit')
|
||||
# At most one bounded record plus a 64KiB decoded block is held.
|
||||
for part in decoded.splitlines(keepends=True):
|
||||
line.extend(part)
|
||||
if len(line) > max_record_bytes:
|
||||
raise ValueError('budget_audit_resource_or_record_limit')
|
||||
if line.endswith(b'\n'):
|
||||
member_records += 1
|
||||
if xz and member_records > limits.MAX_CHUNK_RECORDS:
|
||||
raise ValueError('budget_audit_chunk_record_limit')
|
||||
check()
|
||||
yield bytes(line)
|
||||
line.clear()
|
||||
if member_done:
|
||||
if xz and line:
|
||||
raise ValueError('incomplete record at compressed member boundary')
|
||||
decoder = None
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--protocol', type=Path, required=True)
|
||||
parser.add_argument('--trial-index', type=int, required=True)
|
||||
parser.add_argument('--trace', type=Path, required=True)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
parser.add_argument('--max-trace-bytes', type=int, required=True)
|
||||
parser.add_argument('--wall-seconds', type=float, required=True)
|
||||
args = parser.parse_args()
|
||||
protocol_module, audit_module = cpu_modules()
|
||||
protocol = protocol_module.load_protocol(args.protocol)
|
||||
if not 0 < args.max_trace_bytes <= protocol.document['resources']['max_artifact_bytes']:
|
||||
parser.error('Require bounded positive trace budget within protocol cap')
|
||||
if not 0 < args.wall_seconds <= 3600:
|
||||
parser.error('Require bounded positive CPU audit wall budget')
|
||||
audit = audit_module.TrialAudit(protocol, args.trial_index)
|
||||
deadline = time.monotonic() + args.wall_seconds
|
||||
count = 0
|
||||
try:
|
||||
if args.trace.stat().st_size > args.max_trace_bytes:
|
||||
raise ValueError('budget_audit_compressed_bytes')
|
||||
for line in trace_lines(args.trace, compressed=args.trace.name.endswith(('.jsonl.gz', '.jsonl.xz')),
|
||||
max_bytes=args.max_trace_bytes, max_record_bytes=audit_module.MAX_RECORD_BYTES,
|
||||
deadline=deadline):
|
||||
count += 1
|
||||
if count > 2 * audit.trial.total_ticks + 16:
|
||||
raise ValueError('budget_audit_resource_or_record_limit')
|
||||
audit.consume(json.loads(line))
|
||||
if audit.terminal is None:
|
||||
audit.fail(('recording_missing_terminal',), kind='terminal')
|
||||
except (OSError, EOFError, zlib.error, lzma.LZMAError, ValueError, KeyError, TypeError) as exc:
|
||||
audit.fail((f'recording_offline:{exc}',), kind='offline')
|
||||
report = audit.report()
|
||||
report.update(terminal=audit.terminal, source_trace=str(args.trace),
|
||||
new_simulation_steps=0, new_optimizer_steps=0)
|
||||
with args.output.open('x') as output:
|
||||
json.dump(report, output, indent=2, allow_nan=False)
|
||||
output.write('\n')
|
||||
print(json.dumps({'functional_passed': report['functional_passed'], 'first_failure': report['first_failure']}))
|
||||
return 0 if report['functional_passed'] else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,6 +3,7 @@
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
@@ -74,6 +75,14 @@ def evaluate(env, args, policy, inputs):
|
||||
original_loads, original_update = s.apply_loads, env.scene.update
|
||||
frames = 0
|
||||
shots, actions, memory = [], [], []
|
||||
arrows = None
|
||||
if args.force_arrows:
|
||||
from standing_force_visualizer import DisturbanceForceArrows
|
||||
|
||||
arrows = DisturbanceForceArrows(args.force_arrow_scale)
|
||||
print(
|
||||
f"Orange arrows: actual world disturbance force, {args.force_arrow_scale:g} m/N; no torque/guide/reaction"
|
||||
)
|
||||
|
||||
def apply_loads():
|
||||
s.last_guide[:] = s.guidance()
|
||||
@@ -87,6 +96,8 @@ def evaluate(env, args, policy, inputs):
|
||||
body_ids=s.payload_ids,
|
||||
is_global=True,
|
||||
)
|
||||
if arrows is not None and frames % env.cfg.decimation == 0:
|
||||
arrows.update(s) # display only, after applying this control step's actual load
|
||||
|
||||
def observe(dt):
|
||||
nonlocal frames
|
||||
@@ -135,6 +146,8 @@ def evaluate(env, args, policy, inputs):
|
||||
print(f"{args.mode} {i + 1}/{steps}", flush=True)
|
||||
finally:
|
||||
s.apply_loads, env.scene.update = original_loads, original_update
|
||||
if arrows is not None:
|
||||
arrows.close()
|
||||
cpu = {k: v[:frames].cpu() for k, v in trace.items()}
|
||||
torch.save(cpu, args.output / "physics_trace.pt")
|
||||
if shots:
|
||||
@@ -176,6 +189,11 @@ def evaluate(env, args, policy, inputs):
|
||||
passed=all(result["gates"].values()),
|
||||
mode=args.mode,
|
||||
controller=args.controller,
|
||||
visualization={
|
||||
"force_arrows": args.force_arrows,
|
||||
"force_arrow_scale_m_per_N": args.force_arrow_scale,
|
||||
"source": "actual applied disturbance force in world frame; excludes torque, guide, reaction",
|
||||
},
|
||||
input_hash=meta["input_hash"],
|
||||
metadata=meta,
|
||||
accepted_skill=False,
|
||||
@@ -194,8 +212,16 @@ def main():
|
||||
parser.add_argument("--mode", choices=("holdout", "full", "stability", "exploratory"), required=True)
|
||||
parser.add_argument("--controller", choices=("policy", "hold"), default="policy")
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--force-arrows", action="store_true", help="Draw actual disturbance force in Kit (visual only)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--force-arrow-scale", type=float, default=0.03, help="Arrow length per newton, m/N (default: 0.03)"
|
||||
)
|
||||
add_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
if not math.isfinite(args.force_arrow_scale) or args.force_arrow_scale <= 0:
|
||||
parser.error("--force-arrow-scale must be finite and positive")
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
write_json(args.output / "result.json", {"passed": False, "status": "running", "accepted_skill": False})
|
||||
app = AppLauncher(args).app
|
||||
@@ -221,6 +247,8 @@ def main():
|
||||
"scripts/runtime_common.py",
|
||||
"scripts/supervise_sim.py",
|
||||
]
|
||||
if args.force_arrows:
|
||||
source_paths.append("scripts/standing_force_visualizer.py")
|
||||
training_identity = json.loads((args.package / "binding.json").read_text())["training_identity"]
|
||||
for path, digest in training_identity["sources"].items():
|
||||
if sha256(path) != digest:
|
||||
|
||||
@@ -0,0 +1,310 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Dedicated ONE preparation-only supervisor; never an execution grant or retry.
|
||||
|
||||
Native scans run on a daemon thread, never on the BOOTTIME deadline owner.
|
||||
The main thread alone spawns, discovers, signals and reaps the Kit process tree.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import ctypes
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SPEC = importlib.util.spec_from_file_location('preparation_runner', ROOT / 'scripts/evaluate_cycle_sim_residual.py')
|
||||
r = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(r)
|
||||
b = r.cpu_module('evaluation_budget')
|
||||
TOTAL_SECONDS = 600
|
||||
PREPARE_SECONDS = 480
|
||||
CANDIDATE = ROOT / 'logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt'
|
||||
|
||||
|
||||
class AsyncNativeMonitor:
|
||||
"""A blocked native scan cannot block the sole deadline/cleanup owner."""
|
||||
def __init__(self, policy):
|
||||
self.state = (None, None, None)
|
||||
self.stop = threading.Event()
|
||||
self.policy = policy
|
||||
self.thread = threading.Thread(target=self._scan, daemon=True, name='native-monitor')
|
||||
self.thread.start()
|
||||
|
||||
def _scan(self):
|
||||
monitor = None
|
||||
try:
|
||||
monitor = r.NativeMonitor(self.policy, b)
|
||||
self.state = (b.boottime(), monitor.report(), None)
|
||||
while not self.stop.wait(self.policy['check_interval_seconds']):
|
||||
monitor.check(force=True)
|
||||
self.state = (b.boottime(), monitor.report(), None)
|
||||
except BaseException as error:
|
||||
completed, report, _ = self.state
|
||||
report = getattr(error, 'report', monitor.report() if monitor is not None else report)
|
||||
self.state = (completed, report, repr(error))
|
||||
|
||||
def check(self):
|
||||
completed, report, error = self.state
|
||||
if error:
|
||||
raise RuntimeError('preparation_native_monitor: ' + error)
|
||||
if completed is not None and b.boottime() - completed > 2:
|
||||
raise RuntimeError('preparation_native_scan_stale_over_2_seconds')
|
||||
return report
|
||||
|
||||
|
||||
def write(root, name, value, *, final=False):
|
||||
payload = b.encode(value)
|
||||
reserve = 0 if final else b.HEADROOM_BYTES + b.STDOUT_ALLOWANCE
|
||||
if b.artifact_size(root, exclude_native=True) + len(payload) > b.CAP_BYTES - reserve:
|
||||
raise RuntimeError('preparation_aggregate_output_cap')
|
||||
b.durable_write(root / name, payload)
|
||||
|
||||
|
||||
def without_core_dumps(command):
|
||||
"""Child-only soft/hard RLIMIT_CORE=0; neither a crash fix nor global policy.
|
||||
|
||||
Use an exec shell rather than unsafe preexec_fn in this threaded supervisor.
|
||||
Piped OS crash handlers may ignore RLIMIT_CORE; old evidence is untouched.
|
||||
"""
|
||||
return ['/usr/bin/bash', '-c',
|
||||
'ulimit -S -c 0 && ulimit -H -c 0 || exit 125; exec "$@"',
|
||||
'preparation-no-core', *command]
|
||||
|
||||
|
||||
def postexit_identity(root, frozen_before, protocol=r.PROTOCOLS[0]):
|
||||
"""External owner: require durable child evidence, independently rehash files."""
|
||||
output = root / preparation_name(protocol)
|
||||
manifest = (output / 'binding.json').read_bytes()
|
||||
report = json.loads((output / 'preparation.json').read_bytes())
|
||||
if (report.get('preclose_identity_verified') is not True
|
||||
or report.get('manifest_sha256_for_independent_review') != r.hashlib.sha256(manifest).hexdigest()
|
||||
or (output / 'preparation-failure.json').exists()):
|
||||
raise ValueError('preparation_missing_or_failed_preclose_identity')
|
||||
evidence = json.loads((output / 'preclose-filesystem.json').read_bytes())
|
||||
independent = r.bound_filesystem_inputs(manifest)
|
||||
independent['files'].update(frozen_before)
|
||||
if evidence != independent:
|
||||
raise ValueError('preparation_postexit_bound_inputs_changed')
|
||||
return r.verify_filesystem_inputs(evidence)
|
||||
|
||||
|
||||
def collect_postexit_identity(root, frozen_before, child, errors, deadline, protocol=r.PROTOCOLS[0]):
|
||||
if child is None:
|
||||
return None
|
||||
result = []
|
||||
|
||||
def rehash():
|
||||
try:
|
||||
result.append((postexit_identity(root, frozen_before, protocol), None))
|
||||
except BaseException as error:
|
||||
result.append((None, repr(error)))
|
||||
|
||||
# File I/O must not take deadline ownership away from the sole supervisor.
|
||||
thread = threading.Thread(target=rehash, daemon=True, name='postexit-identity')
|
||||
thread.start()
|
||||
while thread.is_alive():
|
||||
remaining = deadline - b.boottime()
|
||||
if remaining <= 0:
|
||||
errors.append('preparation_postexit_identity_deadline')
|
||||
return None
|
||||
thread.join(min(.05, remaining))
|
||||
report, error = result[0]
|
||||
if error:
|
||||
errors.append(error)
|
||||
return report
|
||||
|
||||
|
||||
def preparation_name(protocol):
|
||||
return 'prep-' + r.protocol_stage(protocol).lower()
|
||||
|
||||
|
||||
def supervise(root, policy_path, trusted_policy_digest, protocol=r.PROTOCOLS[0]):
|
||||
name = preparation_name(protocol)
|
||||
if r.resource_profile(protocol) is not None:
|
||||
b.extended_historical_calls(root) # same finite root allowlist as reservation
|
||||
started = b.boottime()
|
||||
deadline = started + TOTAL_SECONDS
|
||||
if not hasattr(os, 'pidfd_open') or not hasattr(signal, 'pidfd_send_signal'):
|
||||
raise RuntimeError('preparation_pidfd_required')
|
||||
r.protect_artifact_path(root)
|
||||
if any((root / name).exists() for name in ('prep-p1', 'prep-p2', 'native/prep-p1', 'native/prep-p2', 'preparation-start.json', 'budget.json')):
|
||||
raise RuntimeError('preparation_requires_fresh_campaign_no_retry')
|
||||
policy = r.storage_policy_check(SimpleNamespace(
|
||||
artifact_root=root, protocol=protocol, storage_policy=policy_path,
|
||||
trusted_storage_policy_sha256=trusted_policy_digest), b)
|
||||
if policy['not_after_boottime'] < deadline:
|
||||
raise RuntimeError('preparation_policy_expiry_before_cleanup')
|
||||
owned = r.OwnedProcesses()
|
||||
if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0:
|
||||
raise RuntimeError('preparation_subreaper_setup_failed')
|
||||
before = b.digest(CANDIDATE)
|
||||
frozen_before = {str(p): b.digest(p) for p in CANDIDATE.parent.rglob('*') if p.is_file()}
|
||||
write(root, 'preparation-start.json', {
|
||||
'started_boottime': started, 'deadline_boottime': deadline,
|
||||
'prepare_cutoff_boottime': started + PREPARE_SECONDS,
|
||||
'supervisor_pid_starttime': owned.supervisor, 'dedicated_sole_spawner': True,
|
||||
'candidate_before_sha256': before, 'kit_only_attempt': True,
|
||||
'actual_run_reservations_consumed': 0, 'execution_authorized': False,
|
||||
'storage_policy_sha256': trusted_policy_digest, 'policy_role': 'preparation-monitor-only',
|
||||
'supervisor_sha256': b.digest(Path(__file__)), 'runner_sha256': b.digest(ROOT / r.SCRIPT),
|
||||
'independent_native_scan_deadline_owner': True,
|
||||
'stdout_limit_bytes': b.STDOUT_BYTES, 'cleanup_reserve_bytes': b.HEADROOM_BYTES,
|
||||
'stdout_allowance_bytes': b.STDOUT_ALLOWANCE,
|
||||
'child_rlimit_core': {'soft': 0, 'hard': 0, 'scope': 'launched child and descendants only'},
|
||||
})
|
||||
monitor = AsyncNativeMonitor(policy)
|
||||
child = capture = None
|
||||
command = None
|
||||
clean = False
|
||||
errors = []
|
||||
interrupted = threading.Event()
|
||||
handlers = {sig: signal.signal(sig, lambda *_: interrupted.set()) for sig in (signal.SIGTERM, signal.SIGINT)}
|
||||
native_report = None
|
||||
launch_started = launch_ended = None
|
||||
|
||||
def check():
|
||||
nonlocal native_report
|
||||
if interrupted.is_set():
|
||||
raise RuntimeError('preparation_interrupted')
|
||||
if b.boottime() >= started + PREPARE_SECONDS:
|
||||
raise RuntimeError('preparation_480s_deadline')
|
||||
native_report = monitor.check()
|
||||
if b.artifact_size(root, exclude_native=True) > b.CAP_BYTES - b.HEADROOM_BYTES - b.STDOUT_ALLOWANCE:
|
||||
raise RuntimeError('preparation_aggregate_output_cap')
|
||||
|
||||
def cleanup_pump():
|
||||
nonlocal native_report
|
||||
if capture is not None:
|
||||
try:
|
||||
capture.pump()
|
||||
except Exception as error:
|
||||
text = repr(error)
|
||||
if text not in errors:
|
||||
errors.append(text)
|
||||
try:
|
||||
native_report = monitor.check()
|
||||
except Exception as error:
|
||||
text = repr(error)
|
||||
if text not in errors:
|
||||
errors.append(text)
|
||||
|
||||
try:
|
||||
while native_report is None:
|
||||
check()
|
||||
time.sleep(.05)
|
||||
write(root, 'native-baseline.json', native_report)
|
||||
native = root / 'native'
|
||||
if not native.exists():
|
||||
b.durable_directory(native)
|
||||
support = native / 'preparation-support'
|
||||
b.durable_directory(support)
|
||||
redirects = r.native_redirects(support, preparation=True)
|
||||
for directory in redirects.values():
|
||||
b.durable_directory(Path(directory))
|
||||
environment = dict(os.environ, PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1',
|
||||
**redirects)
|
||||
for key in ('CONDA_PREFIX', 'VIRTUAL_ENV', 'PYTHONEXE', 'PYTHONHOME', 'args'):
|
||||
environment.pop(key, None)
|
||||
environment['PYTHONPATH'] = ':'.join([
|
||||
str(ROOT / 'source/SkillFactory'),
|
||||
*['/home/cen/IsaacLab/source/' + name for name in
|
||||
('isaaclab', 'isaaclab_physx', 'isaaclab_assets', 'isaaclab_tasks')]])
|
||||
arguments = [str(ROOT / r.SCRIPT), '--prepare-binding', '--discover-inventory',
|
||||
'--protocol', protocol, '--trial-index', '0', '--device', 'cuda:0',
|
||||
'--artifact-root', str(root), '--output', str(root / name)]
|
||||
command = without_core_dumps(r.launch_command(arguments, environment))
|
||||
write(root, 'preparation-command.json', {'argv': command, 'cwd': str(ROOT),
|
||||
'environment_overrides': {k: environment[k] for k in ('HOME', 'TMPDIR', 'XDG_CACHE_HOME',
|
||||
'XDG_CONFIG_HOME', 'XDG_DATA_HOME', 'PYTHONPATH', 'PYTHONDONTWRITEBYTECODE',
|
||||
'PYTEST_DISABLE_PLUGIN_AUTOLOAD')}, 'launch_identity': r.launch_identity()})
|
||||
check()
|
||||
launch_started = b.boottime()
|
||||
child = subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT, start_new_session=True)
|
||||
try:
|
||||
owned.attach(child)
|
||||
except BaseException:
|
||||
owned.unknown = True
|
||||
raise
|
||||
capture = r.StdoutCapture(child.stdout, root / 'preparation.stdout.log', b)
|
||||
while not capture.eof or child.poll() is None:
|
||||
capture.check()
|
||||
check()
|
||||
owned.discover()
|
||||
time.sleep(.02)
|
||||
launch_ended = b.boottime()
|
||||
if child.returncode != 0:
|
||||
raise RuntimeError('preparation_child_exit_' + str(child.returncode))
|
||||
except BaseException as error:
|
||||
errors.append(repr(error))
|
||||
finally:
|
||||
try:
|
||||
if child is not None:
|
||||
try:
|
||||
if capture is None and child.stdout is not None:
|
||||
child.stdout.close()
|
||||
finally:
|
||||
clean = r.cleanup_group(child, deadline, b, owned, cleanup_pump)
|
||||
else:
|
||||
clean = not owned.discover() and not owned.unknown
|
||||
if capture is not None:
|
||||
capture.close()
|
||||
if capture.discarded or capture.error:
|
||||
errors.append('preparation_stdout_incomplete')
|
||||
native_report = monitor.check()
|
||||
except BaseException as error:
|
||||
errors.append(repr(error))
|
||||
finally:
|
||||
monitor.stop.set() # never join a blocked filesystem scanner
|
||||
for sig, handler in handlers.items():
|
||||
signal.signal(sig, handler)
|
||||
postexit = collect_postexit_identity(root, frozen_before, child, errors, deadline, protocol)
|
||||
after = b.digest(CANDIDATE)
|
||||
if after != before:
|
||||
errors.append('candidate_changed')
|
||||
remaining = owned.discover()
|
||||
report = {
|
||||
'execution_authorized': False, 'kit_only_launch_started': child is not None,
|
||||
'actual_run_reservations_consumed': 0, 'command_record': 'preparation-command.json' if command else None,
|
||||
'started_boottime': started, 'finished_boottime': b.boottime(), 'deadline_boottime': deadline,
|
||||
'launch_started_boottime': launch_started, 'launch_ended_boottime': launch_ended,
|
||||
'returncode': child.returncode if child is not None else None,
|
||||
'postexit_identity': postexit,
|
||||
'identity_ownership': 'child actual cfg preclose; parent immutable files and process cleanup postexit',
|
||||
'cleanup_clean': clean, 'ownership_unknown': owned.unknown,
|
||||
'owned_remaining': remaining, 'owned_identities': sorted(owned.known),
|
||||
'stdout_bytes': capture.written if capture else 0, 'stdout_discarded': capture.discarded if capture else 0,
|
||||
'stdout_eof': capture.eof if capture else False, 'errors': errors,
|
||||
'monitor': monitor.state[1], 'monitor_last_completed_boottime': monitor.state[0],
|
||||
'monitor_thread_still_alive': monitor.thread.is_alive(),
|
||||
'candidate_before_sha256': before, 'candidate_after_sha256': after,
|
||||
'protocol': protocol, 'preparation_directory': name,
|
||||
'outcomes': {filename: (root / name / filename).is_file()
|
||||
for filename in ('inventory.proposed.json', 'binding.json', 'preparation.json')},
|
||||
'artifact_bytes_before_report': b.artifact_size(root, exclude_native=True),
|
||||
'scope': b.STORAGE_SCOPE, 'native_hard_cap': False,
|
||||
'inventory_and_manifest_trust': 'unreviewed proposals only; no execution identity approved',
|
||||
}
|
||||
write(root, 'preparation-supervisor.json', report, final=True)
|
||||
return 0 if child is not None and clean and not remaining and not errors else 1
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--protocol', choices=r.PROTOCOLS, default=r.PROTOCOLS[0])
|
||||
parser.add_argument('--artifact-root', type=Path, required=True)
|
||||
parser.add_argument('--storage-policy', type=Path, required=True)
|
||||
parser.add_argument('--trusted-storage-policy-sha256', required=True)
|
||||
args = parser.parse_args(argv)
|
||||
return supervise(args.artifact_root, args.storage_policy, args.trusted_storage_policy_sha256, args.protocol)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,197 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CPU-only saved-record component replay; no simulator, inference or training.
|
||||
|
||||
Private namespace packages bypass task registration, for offline measurement only,
|
||||
never for identity verification. Originals are read-only; outputs are exclusive.
|
||||
"""
|
||||
import argparse
|
||||
import cProfile
|
||||
import dataclasses
|
||||
import gzip
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import lzma
|
||||
import pstats
|
||||
import sys
|
||||
import time
|
||||
import types
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def modules():
|
||||
base = ROOT / 'source/SkillFactory/SkillFactory'
|
||||
for relative in ('', '.legs', '.legs.contact_cycle', '.legs.contact_cycle_sim'):
|
||||
name = 'SkillFactory' + relative
|
||||
package = types.ModuleType(name)
|
||||
package.__path__ = [str(base / relative.lstrip('.').replace('.', '/'))]
|
||||
sys.modules[name] = package
|
||||
from SkillFactory.legs.contact_cycle_sim import evaluation_audit, evaluation_budget, evaluation_protocol
|
||||
path = base / 'tasks/manager_based/contact_cycle_sim/evaluation_recording.py'
|
||||
spec = importlib.util.spec_from_file_location('_offline_recording', path)
|
||||
recording = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(recording)
|
||||
return evaluation_protocol, evaluation_audit, evaluation_budget, recording
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--row0', type=Path, required=True)
|
||||
parser.add_argument('--row1', type=Path, required=True)
|
||||
parser.add_argument('--budget-root', type=Path, required=True)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
parser.add_argument('--replay-directory', type=Path)
|
||||
args = parser.parse_args()
|
||||
protocol_module, audit_module, budget_module, recording = modules()
|
||||
protocol = protocol_module.load_protocol(ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json')
|
||||
result = {'scope': 'CPU saved-record replay only; not end-to-end or GPU throughput',
|
||||
'python': sys.version, 'rows': [], 'budget_root': str(args.budget_root)}
|
||||
deadline = time.monotonic() + 1800
|
||||
if args.replay_directory is not None:
|
||||
args.replay_directory.mkdir(exist_ok=False)
|
||||
for index, path in enumerate((args.row0, args.row1)):
|
||||
audit = audit_module.TrialAudit(protocol, index)
|
||||
seconds = defaultdict(float)
|
||||
counts = defaultdict(int)
|
||||
compressed = defaultdict(int)
|
||||
chunks = []
|
||||
chunk_size = 0
|
||||
digest = hashlib.sha256()
|
||||
profiler = cProfile.Profile()
|
||||
|
||||
def timed(name, fn, *values, **kwargs):
|
||||
start = time.perf_counter()
|
||||
value = fn(*values, **kwargs)
|
||||
seconds[name] += time.perf_counter() - start
|
||||
return value
|
||||
|
||||
def flush():
|
||||
nonlocal chunks, chunk_size
|
||||
if not chunks:
|
||||
return
|
||||
raw = b''.join(chunks)
|
||||
for name, codec in [('gzip_chunk_level1', lambda b: gzip.compress(b, compresslevel=1, mtime=0)),
|
||||
('xz_chunk_preset0', lambda b: lzma.compress(b, preset=0))]:
|
||||
data = timed(name, codec, raw)
|
||||
compressed[name] += len(data)
|
||||
assert (gzip.decompress(data) if name.startswith('gzip') else lzma.decompress(data)) == raw
|
||||
counts['chunks'] += 1
|
||||
chunks, chunk_size = [], 0
|
||||
|
||||
start = time.perf_counter()
|
||||
with gzip.open(path, 'rb') as source:
|
||||
while True:
|
||||
raw = timed('gzip_readline', source.readline, audit_module.MAX_RECORD_BYTES + 1)
|
||||
if not raw:
|
||||
break
|
||||
if len(raw) > audit_module.MAX_RECORD_BYTES or time.monotonic() >= deadline:
|
||||
raise RuntimeError('offline benchmark bound')
|
||||
record = timed('json_loads', json.loads, raw)
|
||||
counts[record['kind']] += 1
|
||||
safe = timed('json_safe', recording._json_safe, record)
|
||||
encoded = timed('json_dumps_encode', lambda: (json.dumps(safe, allow_nan=False,
|
||||
separators=(',', ':')) + '\n').encode())
|
||||
assert encoded == raw # Complete serialized fields and order, not just parsed equality.
|
||||
digest.update(encoded)
|
||||
compressed['gzip_record_level1'] += len(timed('gzip_record_level1',
|
||||
gzip.compress, encoded, compresslevel=1, mtime=0))
|
||||
timed('plain_copy', audit_module.plain, record)
|
||||
timed('recorder_copied', recording.copied, record)
|
||||
causes = timed('audit_consume', audit.consume, record)
|
||||
# Profile a separate prefix audit below; never alter this audit's ordering.
|
||||
if len(chunks) == 64 or chunk_size + len(raw) > 8 * 1024 * 1024:
|
||||
flush()
|
||||
chunks.append(raw)
|
||||
chunk_size += len(raw)
|
||||
if causes or record['kind'] in ('initial', 'seam', 'step_result', 'terminal', 'capture_fault'):
|
||||
flush()
|
||||
# Real read-only aggregate walk, same frequency as original recording callback.
|
||||
timed('artifact_size_scan', budget_module.artifact_size, args.budget_root, exclude_native=True)
|
||||
flush()
|
||||
elapsed = time.perf_counter() - start
|
||||
assert audit.terminal is not None
|
||||
report = audit.report()
|
||||
original = json.loads((path.parent / 'metrics.json').read_text())
|
||||
assert all(original[k] == value for k, value in report.items()), 'saved audit mismatch'
|
||||
profile_audit = audit_module.TrialAudit(protocol, index)
|
||||
with gzip.open(path, 'rb') as source:
|
||||
for _ in range(161):
|
||||
record = json.loads(source.readline())
|
||||
profiler.runcall(profile_audit.consume, record)
|
||||
stats = pstats.Stats(profiler)
|
||||
top = sorted(stats.stats.items(), key=lambda item: item[1][3], reverse=True)[:20]
|
||||
result['rows'].append({'path': str(path), 'original_bytes': path.stat().st_size,
|
||||
'records': dict(counts), 'component_seconds': dict(seconds), 'measured_loop_seconds': elapsed,
|
||||
'compressed_bytes': dict(compressed), 'serialized_sha256': digest.hexdigest(),
|
||||
'saved_metrics_audit_equal': True,
|
||||
'bytes_per_tick': {k: v / counts['tick'] for k, v in compressed.items()},
|
||||
'projected_90000_tick_bytes': {k: 90000 * v / counts['tick'] for k, v in compressed.items()},
|
||||
'audit_ticks_per_second': counts['tick'] / seconds['audit_consume'],
|
||||
'audit_profile_prefix_161_records': [{'function': str(key), 'primitive_calls': value[0],
|
||||
'calls': value[1], 'self_seconds': value[2], 'cumulative_seconds': value[3]} for key, value in top]})
|
||||
if args.replay_directory is not None:
|
||||
checks = defaultdict(int)
|
||||
scan_seconds = 0.
|
||||
|
||||
def budget(stage, pending, written):
|
||||
nonlocal scan_seconds
|
||||
started = time.perf_counter()
|
||||
size = (budget_module.artifact_size(args.budget_root, exclude_native=True)
|
||||
+ budget_module.artifact_size(args.replay_directory))
|
||||
scan_seconds += time.perf_counter() - started
|
||||
checks[stage] += 1
|
||||
if size + pending > 1024 ** 3 or time.monotonic() > deadline:
|
||||
raise RuntimeError('offline replay budget')
|
||||
|
||||
started = time.perf_counter()
|
||||
recorder = recording.EvaluationRecorder(protocol, index, args.replay_directory / f'row{index}',
|
||||
budget_check=budget, supplemental=lambda _: {})
|
||||
with gzip.open(path, 'rb') as source:
|
||||
for line in source:
|
||||
record = json.loads(line)
|
||||
if record['kind'] == 'terminal':
|
||||
stop_type = dataclasses.make_dataclass('SavedStop', [(k, object) for k in record['runtime_stop']])
|
||||
acknowledged = []
|
||||
live = recorder.finalize(stop=stop_type(**record['runtime_stop']),
|
||||
external_failure=record['external_failure'], acknowledge=acknowledged.append)
|
||||
assert len(acknowledged) == 1
|
||||
else:
|
||||
recorder._record(record)
|
||||
replay_seconds = time.perf_counter() - started
|
||||
assert not recorder.faults
|
||||
assert all(live[k] == value for k, value in report.items())
|
||||
# Production bounded reader, complete traversal including trailers/EOF.
|
||||
spec = importlib.util.spec_from_file_location('_offline_reader', ROOT / 'scripts/audit_cycle_sim_evaluation.py')
|
||||
reader = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(reader)
|
||||
trace = recorder.directory / audit_module.TRACE_FILENAME
|
||||
replay_audit = audit_module.TrialAudit(protocol, index)
|
||||
actual_digest = hashlib.sha256()
|
||||
decoded_records = 0
|
||||
for line in reader.trace_lines(trace, compressed=True, max_bytes=128 * 1024 * 1024,
|
||||
max_record_bytes=audit_module.MAX_RECORD_BYTES, deadline=deadline):
|
||||
actual_digest.update(line)
|
||||
decoded_records += 1
|
||||
replay_audit.consume(json.loads(line))
|
||||
assert actual_digest.hexdigest() == digest.hexdigest()
|
||||
assert decoded_records == sum(v for k, v in counts.items() if k != 'chunks')
|
||||
assert replay_audit.report() == report
|
||||
result['rows'][-1]['production_replay'] = {
|
||||
'trace': str(trace), 'trace_bytes': trace.stat().st_size,
|
||||
'bytes_per_tick': trace.stat().st_size / counts['tick'],
|
||||
'cpu_seconds_including_read_json_budget_fsync': replay_seconds,
|
||||
'cpu_ticks_per_second': counts['tick'] / replay_seconds,
|
||||
'budget_checks': dict(checks), 'budget_scan_seconds': scan_seconds,
|
||||
'decoded_bytes_sha256_equal': True, 'decoded_records': decoded_records,
|
||||
'audit_report_equal': True, 'saved_metrics_equal': True}
|
||||
with args.output.open('x') as output:
|
||||
json.dump(result, output, indent=2)
|
||||
output.write('\n')
|
||||
print(json.dumps(result, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,258 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Frozen u256 replay, never training. --check-only performs CPU preflight without Kit.
|
||||
|
||||
--replay is one nominal first episode (900 controls / 4500 physics ticks per env),
|
||||
not continuous cycling or acceptance. Physics execution requires separate approval.
|
||||
Use supervise_sim.py with a fresh sibling log/report and <=360s outer timeout.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import write_json
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def record_episodes(recorder, output, mode, functional, nominal, boundaries):
|
||||
"""Persist full physics-rate first episodes even when execution/auditing fails."""
|
||||
import torch
|
||||
from audit_contact_cycle_functional import evaluate
|
||||
from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES
|
||||
|
||||
torch.save(recorder.initial_state, output / "initial_state.pt")
|
||||
if not recorder.frames:
|
||||
write_json(output / "episodes.json", {"rows": [], "error": "No physics samples captured"})
|
||||
return []
|
||||
trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]}
|
||||
# SimCycleState.policy() exposes these signed simulation-truth errors in 46D.
|
||||
trace["root_position_error_m"] = trace["policy_snapshot"][..., 32:35].clone()
|
||||
trace["root_velocity_error_m_s"] = trace["policy_snapshot"][..., 35:38].clone()
|
||||
episodes = {str(e): {k: v[:stop, e:e+1] for k, v in trace.items()}
|
||||
for e, stop in enumerate(recorder.stops)}
|
||||
torch.save(episodes, output / "first_episodes.pt")
|
||||
write_json(output / "boundaries.json", boundaries)
|
||||
rows = []
|
||||
for e, episode in episodes.items():
|
||||
d = {k: v[:, 0] for k, v in episode.items()}
|
||||
flags = d["failure_flags"]
|
||||
hits = flags.any(-1).nonzero().flatten()
|
||||
causes = [name for j, name in enumerate(FAILURE_NAMES) if bool(flags[:, j].any())]
|
||||
# The immutable auditor has two metadata interfaces, not three controller claims.
|
||||
audit_kind = "learned_policy" if mode == "learned" else "analytic_reference"
|
||||
audit = evaluate(episode, functional, audit_kind)
|
||||
boundary = boundaries[int(e)]
|
||||
actual = d["processed_target"] - nominal.cpu() - .5 * d["reference_used_normalized"]
|
||||
invariants = {
|
||||
"finite": all(bool(torch.isfinite(v).all()) for v in episode.values()),
|
||||
"actor46": d["actor_input"].shape[-1] == 46,
|
||||
"request_bound": bool((d["requested_action"].abs() <= 1).all()),
|
||||
"reference_bound": bool((d["reference_used_normalized"].abs() <= 1).all()),
|
||||
"actual_residual_bound": bool((actual.abs() <= .05 + 1e-6).all()),
|
||||
"actual_residual_logged": bool(torch.allclose(actual, d["effective_residual_rad"], atol=1e-6, rtol=0)),
|
||||
}
|
||||
if mode == "learned":
|
||||
invariants["deterministic_action"] = bool(torch.allclose(
|
||||
d["requested_action"], d["actor_mean"].tanh(), atol=1e-6, rtol=0))
|
||||
elif mode == "reference":
|
||||
invariants["zero_residual"] = not bool(d["requested_action"].any() or actual.abs().gt(1e-6).any())
|
||||
success = bool(audit["sim_functional_passed"] and all(invariants.values())
|
||||
and boundary["reason"] == "timeout" and not recorder.early[int(e)]
|
||||
and not causes and not d["would_fail"].any())
|
||||
swing = d["phase"] == 3
|
||||
rows.append({"env": int(e), "success": success, "functional_audit": audit,
|
||||
"audit_controller_kind_is_metadata_only": True, "invariants": invariants,
|
||||
"first_episode_samples": len(d["time"]), "termination": boundary,
|
||||
"terminal_causes": causes,
|
||||
"first_failure_time_s": float(d["time"][int(hits[0])]) if len(hits) else None,
|
||||
"phase_samples": {str(p): int((d["phase"] == p).sum()) for p in range(6)},
|
||||
"height_rms_m": float(d["signed_height_error"].square().mean().sqrt())
|
||||
if invariants["finite"] else None,
|
||||
"swing_rms_m": float((d["foot"][swing] - d["target"][swing]).square().sum(-1).mean().sqrt())
|
||||
if invariants["finite"] and swing.any() else None,
|
||||
"landing_error_m": audit["metrics"].get("landing_error_m")})
|
||||
write_json(output / "episodes.json", {"rows": rows, "controller_mode": mode, "accepted_skill": False})
|
||||
return rows
|
||||
|
||||
|
||||
def run_episode(env, args, policy, functional, guard):
|
||||
import torch
|
||||
from SkillFactory.legs.contact_cycle_sim.model import features
|
||||
from SkillFactory.legs.contact_cycle_sim.replay import inference_action
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
||||
|
||||
s = state(env)
|
||||
if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0
|
||||
or env.cfg.sim.physics.enable_external_forces_every_iteration
|
||||
or env.cfg.terminations.failed is None or env.physics_dt != .002 or env.step_dt != .01
|
||||
or env.cfg.episode_length_s != 9):
|
||||
raise RuntimeError("Require frozen floating-root PGS/500Hz physics/100Hz control/9s failure-enabled task")
|
||||
x = features(env.reset()[0]["policy"])
|
||||
if bool(s.scenarios.any() or s.stand_only.any()):
|
||||
raise RuntimeError("Frozen replay must contain nominal complete cycles only")
|
||||
boundaries = [{"reason": "incomplete", "control_step": None} for _ in range(env.num_envs)]
|
||||
recorder = SimCycleRecorder(env)
|
||||
rewards = []
|
||||
rows = []
|
||||
try:
|
||||
with recorder, torch.no_grad():
|
||||
for step in range(900):
|
||||
guard()
|
||||
action, mean, std = inference_action(x, args.controller, policy, args.script_action)
|
||||
recorder.before_control(x if policy is not None else None, action, mean, std)
|
||||
obs, reward, terminated, truncated, _ = env.step(action)
|
||||
rewards.append(reward.detach().cpu().clone())
|
||||
recorder.end_control(terminated, truncated, step == 899)
|
||||
for e in (terminated | truncated).nonzero().flatten().cpu().tolist():
|
||||
if boundaries[e]["control_step"] is None:
|
||||
boundaries[e] = {"reason": "physical_failure" if bool(terminated[e]) else "timeout",
|
||||
"control_step": step + 1}
|
||||
if not torch.isfinite(reward).all():
|
||||
raise RuntimeError("Nonfinite replay reward")
|
||||
guard()
|
||||
# Stop the entire batch on any early first boundary; never splice a reset episode.
|
||||
if bool(terminated.any()) or (step != 899 and bool(truncated.any())):
|
||||
for boundary in boundaries:
|
||||
if boundary["control_step"] is None:
|
||||
boundary.update(reason="batch_stopped_after_peer_boundary", control_step=step + 1)
|
||||
break
|
||||
if bool((terminated | truncated).all()):
|
||||
break
|
||||
x = features(obs["policy"])
|
||||
except BaseException as error:
|
||||
for boundary in boundaries:
|
||||
if boundary["control_step"] is None:
|
||||
boundary.update(reason="exception", error=f"{type(error).__name__}: {error}")
|
||||
raise
|
||||
finally:
|
||||
if rewards:
|
||||
torch.save(torch.stack(rewards), args.output / "control_rewards.pt")
|
||||
rows = record_episodes(recorder, args.output, args.controller, functional, s.nominal, boundaries)
|
||||
return {"passed": len(rows) == env.num_envs and all(row["success"] for row in rows),
|
||||
"rows": rows, "control_steps": len(rewards), "physics_samples": len(recorder.frames)}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
execution = parser.add_mutually_exclusive_group(required=True)
|
||||
execution.add_argument("--check-only", action="store_true")
|
||||
execution.add_argument("--replay", action="store_true")
|
||||
parser.add_argument("--controller", choices=("learned", "reference", "script"), default="learned")
|
||||
parser.add_argument("--checkpoint", type=Path)
|
||||
parser.add_argument("--script-action", nargs=3, type=float, metavar=("HIP", "THIGH", "CALF"))
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--num-envs", type=int, choices=range(1, 9), default=1)
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
parser.add_argument("--headless", action="store_true")
|
||||
args = parser.parse_args(argv)
|
||||
from SkillFactory.legs.contact_cycle_sim.replay import (
|
||||
CANDIDATE,
|
||||
controller_identity,
|
||||
create_output,
|
||||
load_frozen_policy,
|
||||
verify_bundle,
|
||||
)
|
||||
|
||||
checkpoint = (args.checkpoint or ROOT / CANDIDATE).resolve()
|
||||
args.output = create_output(ROOT, args.output, checkpoint)
|
||||
result = {"passed": False, "accepted_skill": False, "optimizer_steps": 0,
|
||||
"controller_mode": args.controller, "initialization_entered": False, "physics_executed": False,
|
||||
"checkpoint_requested": str(checkpoint)}
|
||||
app = env = safety = None
|
||||
code = 1
|
||||
started = time.monotonic()
|
||||
stopped = False
|
||||
old_handlers = {}
|
||||
|
||||
def stop(*_):
|
||||
nonlocal stopped
|
||||
stopped = True
|
||||
|
||||
def guard():
|
||||
if stopped or time.monotonic() - started >= 300:
|
||||
raise InterruptedError("Replay interrupted or 300s wall bound exceeded; no retry")
|
||||
if safety is not None:
|
||||
safety.check()
|
||||
|
||||
try:
|
||||
result.update(controller_identity(args.controller, args.script_action))
|
||||
manifest, binding = verify_bundle(ROOT, checkpoint)
|
||||
write_json(args.output / "identity.json", {**binding, **result})
|
||||
policy = load_frozen_policy(checkpoint, manifest) if args.controller == "learned" else None
|
||||
result["preflight_passed"] = True
|
||||
if args.check_only:
|
||||
result.update(passed=True, scope="CPU identity/inference loading only; no physical success claim")
|
||||
code = 0
|
||||
else:
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
old_handlers[sig] = signal.signal(sig, stop)
|
||||
guard()
|
||||
# Existing configs/assets resolve relative to the repository, not the caller's cwd.
|
||||
os.chdir(ROOT)
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
app = AppLauncher(headless=args.headless, device=args.device).app
|
||||
guard()
|
||||
import torch
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv
|
||||
|
||||
cfg = manifest["training"]
|
||||
environment_cfg = SimContactCycleEnvCfg(residual_config=str(ROOT / cfg["task_config"]))
|
||||
environment_cfg.scene.num_envs = args.num_envs
|
||||
environment_cfg.sim.device = args.device
|
||||
environment_cfg.seed = cfg["validation_seed"]
|
||||
# Construction resets/warms up PhysX; a constructor failure cannot prove zero physics.
|
||||
result.update(initialization_entered=True, physics_executed=None, status="initializing")
|
||||
write_json(args.output / "result.json", result)
|
||||
env = SimContactCycleEnv(cfg=environment_cfg)
|
||||
result["physics_executed"] = True
|
||||
if policy is not None:
|
||||
policy = policy.to(env.device)
|
||||
result["runtime"] = {"torch": str(torch.__version__), "device": str(env.device),
|
||||
"seed": cfg["validation_seed"], "num_envs": env.num_envs,
|
||||
"physics_dt": env.physics_dt, "control_dt": env.step_dt,
|
||||
"max_control_steps": 900, "max_wall_s": 300,
|
||||
"note": "Nominal development replay; numerical differences may occur"}
|
||||
functional = json.loads((ROOT / cfg["functional_config"]).read_text())
|
||||
thresholds = json.loads((ROOT / cfg["safety_config"]).read_text())["gates"]
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
result.update(run_episode(env, args, policy, functional, guard))
|
||||
guard()
|
||||
code = 0 if result["passed"] else 2
|
||||
_, final_binding = verify_bundle(ROOT, checkpoint)
|
||||
if final_binding != binding:
|
||||
raise RuntimeError("Replay inputs changed during execution")
|
||||
except BaseException:
|
||||
result.update(passed=False, error=traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
code = 1
|
||||
finally:
|
||||
if safety is not None:
|
||||
result["physics_safety"] = safety.report()
|
||||
if env is not None:
|
||||
try:
|
||||
env.close()
|
||||
except BaseException:
|
||||
result.update(passed=False, shutdown_error=traceback.format_exc())
|
||||
code = 1
|
||||
result.update(status="completed" if code == 0 else "failed", wall_seconds=time.monotonic() - started)
|
||||
try:
|
||||
write_json(args.output / "result.json", result)
|
||||
finally:
|
||||
for sig, handler in old_handlers.items():
|
||||
signal.signal(sig, handler)
|
||||
if app is not None:
|
||||
app.close(exit_code=code)
|
||||
return code
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,871 @@
|
||||
"""Dedicated RootXY supervisor adapter. Importing this module never starts Kit.
|
||||
|
||||
Ownership/native monitoring/launch transport reuse reviewed evaluator helpers;
|
||||
no evaluation grant, protocol, ledger, checkpoint or candidate authority is reused.
|
||||
"""
|
||||
import argparse
|
||||
import ctypes
|
||||
from contextlib import contextmanager
|
||||
import fcntl
|
||||
import hashlib
|
||||
import importlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import signal
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import evaluate_cycle_sim_residual as owner
|
||||
|
||||
ROOT = owner.ROOT
|
||||
SCRIPT = 'scripts/train_cycle_sim_rootxy_v2.py'
|
||||
PROFILE = 'rootxy-v2-7200s-1GiB-73460calls'
|
||||
TASK_ID = 'SkillFactory-Go2FL-ContactCycleSimRootXY-v2'
|
||||
PREREG_FORMAT = 'skillfactory.contact-cycle-sim-rootxy-preregistration.v2'
|
||||
GRANT_FORMAT = 'skillfactory.rootxy-parent-grant.v2'
|
||||
POLICY_FORMAT = 'skillfactory.rootxy-native-policy.v2'
|
||||
PREPARATION_FORMAT = 'skillfactory.rootxy-config-preparation.v2'
|
||||
STORAGE_SCOPE = 'rootxy-new-owned.native-monitored-stop.v2'
|
||||
DECISION_FORMAT = 'skillfactory.rootxy-p2-decision.v2'
|
||||
CAP = 1 << 30
|
||||
HEADROOM = 16 << 20
|
||||
STDOUT = 1 << 20
|
||||
NATIVE_LIFETIME = 10800
|
||||
b = owner.cpu_module('evaluation_budget')
|
||||
|
||||
|
||||
def canonical(value):
|
||||
return b.encode(value)
|
||||
|
||||
|
||||
def sha(value):
|
||||
return hashlib.sha256(canonical(value)).hexdigest()
|
||||
|
||||
|
||||
def read(path, expected=None):
|
||||
path = Path(path)
|
||||
if not path.is_file() or path.is_symlink() or path.stat().st_size > 32 << 20:
|
||||
raise ValueError('Missing/unsafe/oversized RootXY input: ' + str(path))
|
||||
data = path.read_bytes()
|
||||
if expected is not None:
|
||||
owner.read_trusted(path, expected)
|
||||
return json.loads(data)
|
||||
|
||||
|
||||
def checked_root(path):
|
||||
root = Path(path)
|
||||
if (not root.is_absolute() or root != root.resolve()
|
||||
or root.is_relative_to(ROOT) and not root.is_relative_to(ROOT / 'logs/contact_cycle/rootxy_v2')
|
||||
or root.is_relative_to(ROOT / 'releases')
|
||||
or root.is_relative_to(ROOT / 'logs/contact_cycle/sim_residual_training_v1')):
|
||||
raise ValueError('Canonical new isolated RootXY root required')
|
||||
return root
|
||||
|
||||
|
||||
def verify_sources(manifest):
|
||||
if (manifest.get('format') != PREREG_FORMAT or manifest.get('task_id') != TASK_ID
|
||||
or manifest.get('launch_integration_complete') is not True
|
||||
or manifest.get('accepted_skill') is not False):
|
||||
raise ValueError('New RootXY preregistration required')
|
||||
for relative, expected in manifest['sources'].items():
|
||||
path = ROOT / relative
|
||||
if (Path(relative).is_absolute() or '..' in Path(relative).parts or path != path.resolve()
|
||||
or b.digest(path) != expected):
|
||||
raise ValueError('RootXY source drift: ' + relative)
|
||||
cfg = manifest['training']
|
||||
fixed = {'seeds': [92511], 'validation_seed': 93511, 'num_envs': 128,
|
||||
'max_total_wall_s': 7200, 'cleanup_reserve_s': 120, 'max_artifact_bytes': CAP,
|
||||
'max_validation_transitions': 806400, 'validation_updates': [64, 128, 192, 256]}
|
||||
if any(cfg.get(k) != v for k, v in fixed.items()):
|
||||
raise ValueError('RootXY resource contract changed')
|
||||
|
||||
|
||||
def launch_identity():
|
||||
app_path = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/app/app_launcher.py')
|
||||
return {'app_launcher': {'path': str(app_path), 'sha256': b.digest(app_path)}, 'format': 'skillfactory.rootxy-launch-identity.v2',
|
||||
'transport': owner.launch_identity(), 'profile': PROFILE, 'task_id': TASK_ID,
|
||||
'entrypoint': {'path': str(ROOT / SCRIPT), 'sha256': b.digest(ROOT / SCRIPT)},
|
||||
'adapter': {'path': str(Path(__file__).resolve()), 'sha256': b.digest(__file__)}}
|
||||
|
||||
|
||||
def redirects(root, mode):
|
||||
return owner.native_redirects(root / 'native' / mode, preparation=True)
|
||||
|
||||
|
||||
def policy_check(args, now):
|
||||
policy = read(args.storage_policy, args.trusted_storage_policy_sha256)
|
||||
root = args.output
|
||||
fields = {'format', 'profile', 'artifact_root', 'limit_bytes', 'native_paths', 'shared_native_paths',
|
||||
'task_owned_cache_paths', 'free_space_paths', 'free_space_mount_identities', 'mount_identity',
|
||||
'check_interval_seconds', 'max_native_growth_bytes', 'min_free_bytes', 'coverage_gaps',
|
||||
'native_hard_cap', 'boot_id', 'issued_boottime', 'not_after_boottime', 'lifetime_seconds'}
|
||||
duration = policy.get('lifetime_seconds')
|
||||
if (set(policy) != fields or policy['format'] != POLICY_FORMAT or policy['profile'] != PROFILE
|
||||
or policy['artifact_root'] != str(root) or policy['limit_bytes'] != CAP
|
||||
or policy['native_paths'] != owner.native_paths(root)
|
||||
or policy['shared_native_paths'] != owner.shared_native_paths()
|
||||
or policy['task_owned_cache_paths'] != sorted([*redirects(root, 'prepare').values(),
|
||||
*redirects(root, 'run').values()])
|
||||
or policy['free_space_paths'] != owner.free_space_paths(root)
|
||||
or policy['free_space_mount_identities'] != {
|
||||
p: owner.mount_identity(Path(p)) for p in owner.free_space_paths(root)}
|
||||
or policy['mount_identity'] != owner.mount_identity(root)
|
||||
or policy['coverage_gaps'] != list(owner.NATIVE_GAPS) or policy['native_hard_cap'] is not False
|
||||
or policy['boot_id'] != b.boot_id() or type(duration) is not int or not 7200 <= duration <= 10800
|
||||
or type(policy['issued_boottime']) not in (int, float)
|
||||
or not 0 <= policy['issued_boottime'] <= now
|
||||
or policy['not_after_boottime'] != policy['issued_boottime'] + duration
|
||||
or type(policy['check_interval_seconds']) not in (int, float)
|
||||
or not 0 < policy['check_interval_seconds'] <= 1
|
||||
or type(policy['max_native_growth_bytes']) is not int
|
||||
or not 0 < policy['max_native_growth_bytes'] <= CAP
|
||||
or type(policy['min_free_bytes']) is not int or policy['min_free_bytes'] < 2 * CAP):
|
||||
raise PermissionError('Exact RootXY native MONITOR policy required (not quota)')
|
||||
return policy
|
||||
|
||||
|
||||
def admission(args, now):
|
||||
args.output = checked_root(args.output)
|
||||
mode = 'prepare' if args.prepare else 'run'
|
||||
required = ('manifest', 'trusted_manifest_sha256', 'authorization', 'trusted_authorization_sha256',
|
||||
'storage_policy', 'trusted_storage_policy_sha256')
|
||||
if any(getattr(args, k, None) is None for k in required):
|
||||
raise PermissionError('Fresh exact RootXY manifest/native-policy/parent grant required')
|
||||
manifest = read(args.manifest, args.trusted_manifest_sha256)
|
||||
verify_sources(manifest)
|
||||
grant = read(args.authorization, args.trusted_authorization_sha256)
|
||||
fields = {'format', 'profile', 'task_id', 'mode', 'artifact_root', 'manifest_sha256', 'storage_policy_sha256',
|
||||
'launch_identity_sha256', 'boot_id', 'not_after_boottime', 'parent_review',
|
||||
'preparation_sha256', 'binding_sha256', 'p2_decision_sha256', 'gpu_identity'}
|
||||
if (set(grant) != fields or grant['format'] != GRANT_FORMAT or grant['profile'] != PROFILE
|
||||
or grant['task_id'] != TASK_ID or grant['mode'] != mode or grant['artifact_root'] != str(args.output)
|
||||
or grant['manifest_sha256'] != args.trusted_manifest_sha256
|
||||
or grant['storage_policy_sha256'] != args.trusted_storage_policy_sha256
|
||||
or grant['launch_identity_sha256'] != sha(launch_identity())
|
||||
or grant['boot_id'] != b.boot_id() or type(grant['not_after_boottime']) not in (int, float)
|
||||
or not now < grant['not_after_boottime'] <= now + 3600
|
||||
or type(grant['parent_review']) is not str or not grant['parent_review'].strip()):
|
||||
raise PermissionError('RootXY parent grant identity/expiry mismatch')
|
||||
if mode == 'prepare':
|
||||
if any(grant[k] is not None for k in ('preparation_sha256', 'binding_sha256', 'p2_decision_sha256')):
|
||||
raise PermissionError('PREP has pending P2/config identity only, not runtime approval')
|
||||
if args.output.exists():
|
||||
raise FileExistsError('PREP requires a NEW root; no retry')
|
||||
else:
|
||||
for field in ('preparation_sha256', 'binding_sha256', 'p2_decision_sha256'):
|
||||
value = grant[field]
|
||||
if (type(value) is not str or len(value) != 64
|
||||
or any(c not in '0123456789abcdef' for c in value)):
|
||||
raise PermissionError('RUN requires an exact independently approved ' + field)
|
||||
if any((args.output / name).exists() for name in (
|
||||
'run-claim.lock', 'launch_claim.json', 'budget.json', 'budget.lock', 'locked_candidate.json',
|
||||
'run-worker-result.json', 'run-supervisor.json')):
|
||||
raise FileExistsError('Fresh-only RootXY RUN; no prior trial or candidate artifacts')
|
||||
preparation = read(args.output / 'preparation.json', grant['preparation_sha256'])
|
||||
binding = read(args.output / 'training-binding.json', grant['binding_sha256'])
|
||||
decision = read(args.p2_decision, grant['p2_decision_sha256'])
|
||||
if (preparation.get('format') != PREPARATION_FORMAT or preparation.get('status') != 'prepared_config_only'
|
||||
or preparation.get('manifest_sha256') != args.trusted_manifest_sha256
|
||||
or preparation.get('binding_sha256') != grant['binding_sha256']
|
||||
or preparation.get('artifact_root') != str(args.output)
|
||||
or preparation.get('physics_calls') != 0 or preparation.get('runtime_qualified') is not False
|
||||
or binding.get('format') != 'skillfactory.rootxy-training-binding.v2'
|
||||
or binding.get('artifact_root') != str(args.output) or binding.get('profile') != PROFILE):
|
||||
raise PermissionError('Exact config-only preparation/binding required')
|
||||
prep_supervisor = read(args.output / 'prepare-supervisor.json', preparation['supervisor_sha256'])
|
||||
read(args.output / 'prepare-worker-result.json', preparation['worker_result_sha256'])
|
||||
if (preparation.get('postexit_verified') is not True
|
||||
or prep_supervisor.get('success') is not True or prep_supervisor.get('cleanup_clean') is not True):
|
||||
raise PermissionError('PREP needs independent clean waited-exit completion')
|
||||
if (set(decision) != {'format', 'task_id', 'artifact_root', 'manifest_sha256', 'preparation_sha256',
|
||||
'binding_sha256', 'approved', 'parent_review', 'p2_evidence'}
|
||||
or decision['format'] != DECISION_FORMAT or decision['task_id'] != TASK_ID
|
||||
or decision['artifact_root'] != str(args.output) or decision['approved'] is not True
|
||||
or decision['manifest_sha256'] != args.trusted_manifest_sha256
|
||||
or decision['preparation_sha256'] != grant['preparation_sha256']
|
||||
or decision['binding_sha256'] != grant['binding_sha256']
|
||||
or not isinstance(decision['parent_review'], str) or not decision['parent_review'].strip()
|
||||
or not isinstance(decision['p2_evidence'], dict) or not decision['p2_evidence']):
|
||||
raise PermissionError('Separate parent-approved P2 evidence/feasibility decision required')
|
||||
for path, digest in decision['p2_evidence'].items():
|
||||
owner.read_trusted(path, digest)
|
||||
for relative, digest in manifest['sources'].items():
|
||||
if b.digest(args.output / 'source_snapshot' / relative) != digest:
|
||||
raise ValueError('RootXY preparation snapshot drift')
|
||||
policy = policy_check(args, now)
|
||||
total = 600 if mode == 'prepare' else 7200
|
||||
if policy['not_after_boottime'] < max(grant['not_after_boottime'], now + total + 120):
|
||||
raise PermissionError('Native policy must cover admission, full lifetime and headroom')
|
||||
return manifest, grant, policy
|
||||
|
||||
|
||||
class GuardBudget:
|
||||
"""RootXY cap/deadline facade for reviewed ownership and stdout transport."""
|
||||
def __init__(self, root, start, mode, monitor=None):
|
||||
self.root, self.start, self.mode, self.monitor = Path(root), start, mode, monitor
|
||||
self.boot = b.boot_id()
|
||||
self.deadline = start + (600 if mode == 'prepare' else 7200)
|
||||
self.reservation = {'deadline_boottime': self.deadline}
|
||||
self.interrupted = False
|
||||
self.external_growth = 0
|
||||
|
||||
def check(self, pending=0, *, cleanup=False):
|
||||
if (self.interrupted and not cleanup or b.boot_id() != self.boot
|
||||
or not self.start <= b.boottime() < self.deadline - (0 if cleanup else 120)):
|
||||
raise RuntimeError('RootXY cancelled/deadline/boot mismatch')
|
||||
if self.monitor is not None:
|
||||
self.monitor.check()
|
||||
self.external_growth = sum(max(0, size - self.monitor.baseline[path])
|
||||
for path, size in self.monitor.latest.items()
|
||||
if not Path(path).is_relative_to(self.root))
|
||||
limit = CAP - (0 if cleanup else HEADROOM + STDOUT)
|
||||
if b.artifact_size(self.root) + self.external_growth + pending > limit:
|
||||
raise RuntimeError('RootXY aggregate new-owned/native storage cutoff')
|
||||
|
||||
def write(self, path, value, *, cleanup=False):
|
||||
path = Path(path)
|
||||
if path.parent != path.parent.resolve() or not path.is_relative_to(self.root) or path.is_symlink():
|
||||
raise ValueError('Unsafe RootXY publication path')
|
||||
data = canonical(value)
|
||||
if len(data) > 16 << 20:
|
||||
raise RuntimeError('Bounded RootXY publication exceeded')
|
||||
self.check(len(data), cleanup=cleanup)
|
||||
temporary = path.with_name(path.name + '.pending')
|
||||
b.durable_write(temporary, data)
|
||||
os.replace(temporary, path)
|
||||
b.sync_directory(path.parent)
|
||||
|
||||
|
||||
def desktop_identity(pid, *, hash_executable=True):
|
||||
"""Pin a pre-existing graphics client, never infer permission from its name."""
|
||||
if type(pid) is not int or pid <= 0:
|
||||
raise ValueError('Invalid desktop PID')
|
||||
proc = Path('/proc') / str(pid)
|
||||
birth = int((proc / 'stat').read_text().rsplit(') ', 1)[1].split()[19])
|
||||
uid = proc.stat().st_uid
|
||||
command = (proc / 'cmdline').read_bytes()
|
||||
scope = 'proc-exe'
|
||||
try:
|
||||
executable = (proc / 'exe').resolve(strict=True)
|
||||
except PermissionError:
|
||||
# Ubuntu's root-owned display server hides /proc/PID/exe from this user.
|
||||
# Explicit, narrowly scoped public-metadata identity, NOT executable proof.
|
||||
# Still requires a parent-pinned PID/birth/UID/argv and graphics-only NVML.
|
||||
if (uid != 0 or command.split(b'\0')[0] != b'/usr/lib/xorg/Xorg'
|
||||
or (proc / 'comm').read_text().strip() != 'Xorg'):
|
||||
raise
|
||||
executable = Path('/usr/lib/xorg/Xorg').resolve(strict=True)
|
||||
scope = 'root-Xorg-public-metadata; actual-executable-unverified'
|
||||
info = executable.stat()
|
||||
result = {'pid': pid, 'starttime': birth, 'uid': uid,
|
||||
'command_sha256': hashlib.sha256(command).hexdigest(),
|
||||
'executable_identity_scope': scope, 'executable': str(executable),
|
||||
'device': info.st_dev, 'inode': info.st_ino, 'size': info.st_size,
|
||||
'mtime_ns': info.st_mtime_ns}
|
||||
if hash_executable:
|
||||
result['sha256'] = b.digest(executable)
|
||||
if (birth != int((proc / 'stat').read_text().rsplit(') ', 1)[1].split()[19])
|
||||
or command != (proc / 'cmdline').read_bytes() or uid != proc.stat().st_uid
|
||||
or scope == 'proc-exe' and executable != (proc / 'exe').resolve(strict=True)):
|
||||
raise RuntimeError('Desktop process changed during identity capture')
|
||||
return result
|
||||
|
||||
|
||||
class GPUOwnership:
|
||||
"""Monitor owned GPU work plus explicitly approved existing desktop graphics.
|
||||
|
||||
Unknown/new graphics, foreign compute (even an approved desktop PID), changed
|
||||
process identity or inaccessible NVML fail closed. Desktop clients are NEVER
|
||||
cleanup-owned. This is shared display monitoring, not GPU partitioning.
|
||||
"""
|
||||
class Process(ctypes.Structure):
|
||||
_fields_ = [('pid', ctypes.c_uint), ('usedGpuMemory', ctypes.c_ulonglong),
|
||||
('gpuInstanceId', ctypes.c_uint), ('computeInstanceId', ctypes.c_uint)]
|
||||
|
||||
def __init__(self, identity):
|
||||
if (not isinstance(identity, dict) or set(identity) != {'library', 'sha256', 'uuid', 'index', 'desktop_clients'}
|
||||
or identity['index'] != 0 or type(identity['index']) is not int):
|
||||
raise ValueError('Exact GPU0 NVML identity required')
|
||||
path = Path(identity['library'])
|
||||
if not path.is_absolute() or path != path.resolve() or b.digest(path) != identity['sha256']:
|
||||
raise ValueError('NVML library identity mismatch')
|
||||
self.identity = identity
|
||||
self.lib = ctypes.CDLL(str(path))
|
||||
self.call('nvmlInit_v2')
|
||||
self.handle = ctypes.c_void_p()
|
||||
self.call('nvmlDeviceGetHandleByIndex_v2', ctypes.c_uint(0), ctypes.byref(self.handle))
|
||||
uuid = ctypes.create_string_buffer(96)
|
||||
self.call('nvmlDeviceGetUUID', self.handle, uuid, ctypes.c_uint(len(uuid)))
|
||||
if uuid.value.decode() != identity['uuid']:
|
||||
raise RuntimeError('GPU UUID mismatch')
|
||||
clients = identity['desktop_clients']
|
||||
if (not isinstance(clients, list) or len(clients) > 64
|
||||
or any(not isinstance(x, dict) or type(x.get('pid')) is not int for x in clients)
|
||||
or [x['pid'] for x in clients] != sorted({x['pid'] for x in clients})):
|
||||
raise ValueError('Exact finite desktop client identities required')
|
||||
self.desktop = {x['pid']: x for x in clients}
|
||||
actual = self.clients()
|
||||
if actual['compute'] or actual['graphics'] != set(self.desktop):
|
||||
raise RuntimeError('GPU admission compute/approved-desktop mismatch')
|
||||
for pid, expected in self.desktop.items():
|
||||
if desktop_identity(pid) != expected:
|
||||
raise RuntimeError('GPU desktop admission identity mismatch')
|
||||
|
||||
def call(self, name, *args):
|
||||
code = getattr(self.lib, name)(*args)
|
||||
if code != 0:
|
||||
raise RuntimeError('NVML query failed: ' + name + ':' + str(code))
|
||||
|
||||
def clients(self):
|
||||
result = {}
|
||||
for kind, name in (('compute', 'nvmlDeviceGetComputeRunningProcesses_v2'),
|
||||
('graphics', 'nvmlDeviceGetGraphicsRunningProcesses_v2')):
|
||||
count = ctypes.c_uint(1024)
|
||||
processes = (self.Process * 1024)()
|
||||
self.call(name, self.handle, ctypes.byref(count), processes)
|
||||
if count.value > 1024:
|
||||
raise RuntimeError('GPU ownership list overflow')
|
||||
result[kind] = {processes[i].pid for i in range(count.value)}
|
||||
return result
|
||||
|
||||
def pids(self):
|
||||
clients = self.clients()
|
||||
return clients['compute'] | clients['graphics']
|
||||
|
||||
def check(self, owned, *, final=False):
|
||||
if b.digest(self.identity['library']) != self.identity['sha256']:
|
||||
raise RuntimeError('NVML drift')
|
||||
clients = self.clients()
|
||||
active = set(owned.discover())
|
||||
graphics = clients['graphics']
|
||||
desktop = graphics & set(self.desktop)
|
||||
for pid in desktop:
|
||||
expected = self.desktop[pid]
|
||||
if not final:
|
||||
expected = {k: v for k, v in expected.items() if k != 'sha256'}
|
||||
if desktop_identity(pid, hash_executable=final) != expected:
|
||||
raise RuntimeError('GPU desktop process identity changed')
|
||||
if (clients['compute'] - active or graphics - active - desktop
|
||||
or clients['compute'] & set(self.desktop)
|
||||
or final and (clients['compute'] or graphics - desktop)):
|
||||
raise RuntimeError('GPU ownership lost or residual task/foreign GPU client')
|
||||
return {'uuid': self.identity['uuid'], 'compute_pids': sorted(clients['compute']),
|
||||
'approved_desktop_graphics_pids': sorted(desktop),
|
||||
'task_graphics_pids': sorted(graphics - desktop),
|
||||
'clean': not clients['compute'] and not (graphics - desktop),
|
||||
'scope': 'owned-compute-and-graphics.plus-pinned-desktop-graphics-only'}
|
||||
|
||||
|
||||
def worker_payload(fd):
|
||||
"""RootXY entrypoint-specific authenticated single-use inherited FD."""
|
||||
with socket.socket(fileno=fd) as channel:
|
||||
channel.settimeout(15)
|
||||
pid, uid, _ = struct.unpack('3i', channel.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12))
|
||||
command = Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0')
|
||||
entry = command[1:] # only harmless interpreter flags may precede the actual entry script
|
||||
while entry and entry[0] in (b'-u', b'-B', b'-s', b'-E', b'-I'):
|
||||
entry = entry[1:]
|
||||
script_matches = bool(entry and Path(entry[0].decode()).is_absolute()
|
||||
and Path(entry[0].decode()) == ROOT / SCRIPT)
|
||||
if (not script_matches or Path(f'/proc/{pid}/exe').resolve() != Path('/usr/bin/python3.12')
|
||||
or not owner.ancestor(pid, os.getpid()) or uid != os.getuid()
|
||||
or not (b'--run' in entry or b'--prepare' in entry)):
|
||||
raise PermissionError('Authenticated RootXY supervisor required')
|
||||
channel.sendall(str(os.getpid()).encode() + b'\n')
|
||||
with channel.makefile('rb') as stream:
|
||||
data = stream.read(1 << 20)
|
||||
if len(data) >= 1 << 20:
|
||||
raise ValueError('Oversized RootXY handoff')
|
||||
payload = json.loads(data)
|
||||
if payload['launch_identity'] != launch_identity():
|
||||
raise ValueError('RootXY launch drift')
|
||||
return payload
|
||||
|
||||
|
||||
def validate_completion(root, result, manifest, start):
|
||||
data = read(root / 'budget.json')
|
||||
expected = {'reserved_transitions': 1048576, 'reservations': 256, 'pending': None,
|
||||
'updates': {'92511': 256}, 'validation_reserved_transitions': 806400,
|
||||
'validation_attempted_control_calls': 5400, 'validation_completed_control_calls': 5400,
|
||||
'validation_evaluations_started': ['analytic_zero', 'frozen_u256', 'u0064', 'u0128',
|
||||
'u0192', 'u0256']}
|
||||
if any(data.get(k) != v or result.get('budget', {}).get(k) != v for k, v in expected.items()):
|
||||
raise ValueError('Incomplete optimization/validation debit evidence')
|
||||
if (data['clock']['start_s'] != start or data['clock']['deadline_s'] != start + 7200
|
||||
or data['clock']['boot_id'] != b.boot_id()):
|
||||
raise ValueError('Worker changed supervisor lifetime')
|
||||
physics = result['physics']
|
||||
attempted, completed, init, rollout, startup = [physics[k] for k in (
|
||||
'attempted_shared_calls', 'completed_shared_calls', 'initialization_attempted_shared_calls',
|
||||
'rollout_attempted_shared_calls', 'startup_completed_without_task_audit')]
|
||||
if (any(type(x) is not int for x in (attempted, completed, init, rollout, startup))
|
||||
or not 0 <= startup <= init <= 1000 or rollout != 67960
|
||||
or attempted != completed or completed != init + rollout or completed > 73460):
|
||||
raise ValueError('Actual shared physics completion/cap evidence mismatch')
|
||||
safety = physics['safety']
|
||||
gates = read(ROOT / manifest['training']['safety_config'])['gates']
|
||||
values = [safety[k] for k in ('max_effort_velocity_ratio', 'max_joint_excess_rad', 'max_guide_vertical_N')]
|
||||
if (safety['physics_samples'] != completed - startup or safety['finite'] is not True
|
||||
or any(type(v) not in (int, float) or not math.isfinite(v) or v < 0 for v in values)
|
||||
or values[0] > gates['normalized_effort_velocity_max']
|
||||
or values[1] > gates['joint_limit_excess_rad_max'] or values[2] != 0):
|
||||
raise ValueError('Missing/failing global physical safety evidence')
|
||||
for label in ('analytic_zero', 'frozen_u256'):
|
||||
rows = read(root / ('validation_' + label + '.json'))['rows']
|
||||
if (len(rows) != 128 or [r.get('env') for r in rows] != list(range(128))
|
||||
or any(r.get('success') is not True or r.get('functional_audit', {}).get('sim_functional_passed')
|
||||
is not True for r in rows)):
|
||||
raise ValueError('Missing original baseline functional128/128 evidence')
|
||||
|
||||
|
||||
def commit_candidate(root, result, manifest, budget):
|
||||
"""Independent validation and sole-authority last atomic commit; no worker lock."""
|
||||
# Import CPU protocol only after worker exit/cleanup; never initialize CUDA.
|
||||
selection_path = ROOT / owner.PACKAGE / 'legs/contact_cycle_sim/rootxy_selection_v2.py'
|
||||
spec = importlib.util.spec_from_file_location('_rootxy_selection_cpu', selection_path)
|
||||
selection = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(selection)
|
||||
if (result.get('status') != 'completed_provisional' or result.get('training_complete') is not True
|
||||
or result.get('accepted_skill') is not False or result.get('checkpoints_selectable') is not False
|
||||
or result.get('postclose_identity_verified') is not True):
|
||||
raise ValueError('No successful provisional worker result')
|
||||
ranked = []
|
||||
for update in (64, 128, 192, 256):
|
||||
document = read(root / f'validation_u{update:04d}.json')
|
||||
rows = document['rows']
|
||||
# Recheck original functional audit, not just worker's candidate label.
|
||||
if any(r.get('functional_audit', {}).get('sim_functional_passed') is not True for r in rows):
|
||||
raise ValueError('Missing original functional128/128 evidence')
|
||||
key = selection.candidate_key(rows, update)
|
||||
if key is not None:
|
||||
ranked.append((key, update))
|
||||
candidate = result.get('candidate')
|
||||
if not ranked:
|
||||
if candidate is not None or result.get('outcome') != 'no_candidate':
|
||||
raise ValueError('Ineligible worker candidate')
|
||||
return None
|
||||
key, update = min(ranked)
|
||||
if (not isinstance(candidate, dict) or set(candidate) != {'path', 'sha256', 'update', 'selection_key'}
|
||||
or candidate['update'] != update or candidate['path'] != f'checkpoint_u{update:04d}.pt'
|
||||
or candidate['selection_key'] != list(key) or b.digest(root / candidate['path']) != candidate['sha256']):
|
||||
raise ValueError('Candidate selection/hash mismatch')
|
||||
lock = {'format': 'skillfactory.rootxy-supervisor-candidate.v2', 'candidate': candidate,
|
||||
'manifest_file_sha256': b.digest(root / 'manifest.json'), 'accepted_skill': False,
|
||||
'authority': 'waited_exit_postexit_identity_clean_owned_GPU_cleanup_original_eligibility',
|
||||
'downstream_requires_separate_grant': True}
|
||||
budget.write(root / 'locked_candidate.json', lock, cleanup=True)
|
||||
return lock
|
||||
|
||||
|
||||
def supervise(args):
|
||||
start = b.boottime() # includes admission/startup; never reset in worker
|
||||
manifest, grant, policy = admission(args, start)
|
||||
mode = 'prepare' if args.prepare else 'run'
|
||||
if not hasattr(os, 'pidfd_open') or not hasattr(signal, 'pidfd_send_signal'):
|
||||
raise RuntimeError('pidfd supervisor required')
|
||||
owned = owner.OwnedProcesses()
|
||||
gpu = GPUOwnership(grant['gpu_identity'])
|
||||
environment = dict(os.environ, PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1',
|
||||
CUDA_VISIBLE_DEVICES=grant['gpu_identity']['uuid'])
|
||||
owner.launch_command([], environment)
|
||||
if mode == 'prepare':
|
||||
b.durable_directory(args.output)
|
||||
lock = (args.output / (mode + '-claim.lock')).open('xb')
|
||||
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
budget = GuardBudget(args.output, start, mode)
|
||||
monitor = child = capture = channels = None
|
||||
errors, handlers = [], {}
|
||||
clean = waited = False
|
||||
result = None
|
||||
filesystem = None
|
||||
postexit = None
|
||||
|
||||
def cleanup_pump():
|
||||
if capture is not None:
|
||||
capture.pump()
|
||||
if monitor is not None:
|
||||
try:
|
||||
monitor.check()
|
||||
except BaseException as error:
|
||||
if not any(e.startswith('native_cleanup:') for e in errors):
|
||||
errors.append('native_cleanup:' + str(error)[-2048:])
|
||||
|
||||
try:
|
||||
monitor = owner.NativeMonitor(policy, SimpleNamespace(boottime=b.boottime, STORAGE_SCOPE=STORAGE_SCOPE))
|
||||
budget.monitor = monitor
|
||||
handlers = {s: signal.signal(s, lambda *_: setattr(budget, 'interrupted', True))
|
||||
for s in (signal.SIGTERM, signal.SIGINT)}
|
||||
budget.write(args.output / (mode + '-reservation.json'), {
|
||||
'profile': PROFILE, 'mode': mode, 'start_boottime': start, 'deadline_boottime': budget.deadline,
|
||||
'boot_id': budget.boot, 'grant': grant,
|
||||
'reserved_optimization_environment_transitions': 0 if mode == 'prepare' else 1048576,
|
||||
'reserved_validation_environment_transitions': 0 if mode == 'prepare' else 806400,
|
||||
'reserved_rollout_shared_calls': 0 if mode == 'prepare' else 72460,
|
||||
'initialization_shared_calls_cap': 0 if mode == 'prepare' else 1000,
|
||||
'no_refund': True, 'accepted_skill': False})
|
||||
budget.write(args.output / (mode + '-manifest.json'), manifest)
|
||||
if mode == 'prepare':
|
||||
for relative, digest in manifest['sources'].items():
|
||||
destination = args.output / 'source_snapshot' / relative
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
data = (ROOT / relative).read_bytes()
|
||||
budget.check(len(data))
|
||||
b.durable_write(destination, data)
|
||||
original_manifest = Path(args.manifest).read_bytes()
|
||||
budget.check(len(original_manifest))
|
||||
b.durable_write(args.output / 'manifest.json', original_manifest)
|
||||
filesystem = owner.bound_filesystem_inputs(original_manifest)
|
||||
else:
|
||||
filesystem = owner.bound_filesystem_inputs((args.output / 'training-binding.json').read_bytes())
|
||||
owner.verify_filesystem_inputs(filesystem)
|
||||
budget.write(args.output / (mode + '-grant.json'), grant)
|
||||
if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0:
|
||||
raise RuntimeError('Subreaper setup failed')
|
||||
native = args.output / 'native'
|
||||
if native != native.resolve():
|
||||
raise ValueError('Native redirect symlink forbidden')
|
||||
native.mkdir(exist_ok=True)
|
||||
for directory in redirects(args.output, mode).values():
|
||||
Path(directory).mkdir(parents=True, exist_ok=False)
|
||||
environment.update(redirects(args.output, mode))
|
||||
channels = socket.socketpair()
|
||||
parent, child_channel = channels
|
||||
child_channel.set_inheritable(True)
|
||||
budget.check()
|
||||
child = subprocess.Popen(owner.launch_command(
|
||||
[str(ROOT / SCRIPT), '--_worker-fd', str(child_channel.fileno())], environment),
|
||||
cwd=ROOT, env=environment, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
||||
start_new_session=True, pass_fds=(child_channel.fileno(),))
|
||||
try:
|
||||
owned.attach(child)
|
||||
except BaseException:
|
||||
owned.unknown = True
|
||||
raise
|
||||
child_channel.close()
|
||||
capture = owner.StdoutCapture(child.stdout, args.output / (mode + '-stdout.log'), b)
|
||||
budget.write(args.output / 'monitor-heartbeat.json', {
|
||||
'boot_id': budget.boot, 'boottime': b.boottime(), 'external_growth': budget.external_growth})
|
||||
owner.exchange_worker(child, parent, {
|
||||
'mode': mode, 'output': str(args.output), 'start': start, 'boot_id': budget.boot,
|
||||
'manifest_sha256': args.trusted_manifest_sha256, 'grant': grant,
|
||||
'launch_identity': launch_identity()}, budget, capture, owned, monitor, b)
|
||||
next_heartbeat = 0
|
||||
while child.poll() is None or not capture.eof:
|
||||
capture.check()
|
||||
budget.check()
|
||||
gpu.check(owned)
|
||||
if b.boottime() >= next_heartbeat:
|
||||
budget.write(args.output / 'monitor-heartbeat.json', {
|
||||
'boot_id': budget.boot, 'boottime': b.boottime(), 'external_growth': budget.external_growth})
|
||||
next_heartbeat = b.boottime() + .5
|
||||
time.sleep(.01)
|
||||
child.wait(timeout=max(.01, budget.deadline - b.boottime()))
|
||||
waited = True
|
||||
result = read(args.output / (mode + '-worker-result.json'))
|
||||
if child.returncode != 0:
|
||||
raise RuntimeError('Worker nonzero exit')
|
||||
except BaseException as error:
|
||||
errors.append(str(error)[-8192:])
|
||||
finally:
|
||||
try:
|
||||
if channels:
|
||||
for channel in channels:
|
||||
channel.close()
|
||||
if child is not None:
|
||||
if capture is None and child.stdout is not None:
|
||||
child.stdout.close()
|
||||
try:
|
||||
clean = owner.cleanup_group(child, budget.deadline, b, owned, cleanup_pump)
|
||||
child.wait(timeout=max(.01, budget.deadline - b.boottime()))
|
||||
waited = True
|
||||
except BaseException as error:
|
||||
errors.append('cleanup:' + str(error)[-2048:])
|
||||
if capture is not None:
|
||||
try:
|
||||
capture.close()
|
||||
if capture.discarded or capture.error or not capture.eof:
|
||||
raise RuntimeError('Incomplete/overflowed stdout')
|
||||
except BaseException as error:
|
||||
errors.append(str(error)[-2048:])
|
||||
try:
|
||||
if not clean or not waited or child is None or child.returncode != 0 or owned.unknown:
|
||||
raise RuntimeError('No clean independently waited worker exit')
|
||||
gpu.check(owned, final=True)
|
||||
verify_sources(manifest)
|
||||
if filesystem is None:
|
||||
raise RuntimeError('Missing prelaunch filesystem identity')
|
||||
postexit = owner.verify_filesystem_inputs(filesystem)
|
||||
binding_files = owner.bound_filesystem_inputs((args.output / 'training-binding.json').read_bytes())
|
||||
owner.verify_filesystem_inputs(binding_files)
|
||||
if result is None or result.get('postclose_identity_verified') is not True:
|
||||
raise RuntimeError('Missing postclose same-object verification')
|
||||
if mode == 'prepare':
|
||||
if result.get('status') != 'prepared_config_only' or result.get('physics_calls') != 0:
|
||||
raise RuntimeError('PREP incomplete/config-only violation')
|
||||
else:
|
||||
if result.get('status') != 'completed_provisional' or result.get('training_complete') is not True:
|
||||
raise RuntimeError('Training incomplete')
|
||||
validate_completion(args.output, result, manifest, start)
|
||||
budget.check(cleanup=True)
|
||||
monitor.check(force=True)
|
||||
except BaseException as error:
|
||||
errors.append(str(error)[-4096:])
|
||||
summary = {'success': not errors, 'mode': mode, 'cleanup_clean': clean, 'waited_exit': waited,
|
||||
'returncode': None if child is None else child.returncode, 'errors': errors,
|
||||
'accepted_skill': False, 'postexit': postexit, 'owned': sorted(owned.known),
|
||||
'storage': None if monitor is None else monitor.report()}
|
||||
budget.write(args.output / (mode + '-supervisor.json'), summary, cleanup=True)
|
||||
if not errors and mode == 'prepare':
|
||||
prepared = read(args.output / 'config-preparation-provisional.json')
|
||||
prepared.update(supervisor_sha256=b.digest(args.output / 'prepare-supervisor.json'),
|
||||
worker_result_sha256=b.digest(args.output / 'prepare-worker-result.json'),
|
||||
postexit_verified=True)
|
||||
try:
|
||||
budget.write(args.output / 'preparation.json', prepared, cleanup=True)
|
||||
except BaseException:
|
||||
(args.output / 'preparation.json').unlink(missing_ok=True)
|
||||
raise
|
||||
if not errors and mode == 'run':
|
||||
# Last operation capable of committing success. Failure leaves no candidate.
|
||||
try:
|
||||
commit_candidate(args.output, result, manifest, budget)
|
||||
except BaseException as error:
|
||||
(args.output / 'locked_candidate.json').unlink(missing_ok=True)
|
||||
summary.update(success=False, errors=[*errors, 'candidate_commit:' + str(error)[-2048:]])
|
||||
budget.write(args.output / (mode + '-supervisor.json'), summary, cleanup=True)
|
||||
raise
|
||||
finally:
|
||||
for sig, handler in handlers.items():
|
||||
signal.signal(sig, handler)
|
||||
lock.close()
|
||||
return 0 if not errors else 1
|
||||
|
||||
|
||||
def preimport_binding_runtime(binding):
|
||||
"""Finite binding-required imports, before config/inventory; no optional skips."""
|
||||
for name in dict.fromkeys((*binding.REQUIRED_RUNTIME, *binding.ISAAC_PINS)):
|
||||
importlib.import_module(name)
|
||||
|
||||
|
||||
class RuntimeAPI:
|
||||
"""Actual Kit recipe; callable only from authenticated worker handoff."""
|
||||
def __init__(self, payload, budget, guard):
|
||||
self.payload, self.budget, self.guard = payload, budget, guard
|
||||
self.output = Path(payload['output'])
|
||||
self.mode = payload['mode']
|
||||
self.launcher = self.env = self.cfg = self.ledger = None
|
||||
self.started = False
|
||||
self.physics_hooks = None
|
||||
self.manifest = read(self.output / 'manifest.json', payload['manifest_sha256'])
|
||||
self.expected = None
|
||||
|
||||
def start(self):
|
||||
from isaaclab.app import AppLauncher
|
||||
app_source = self.payload['launch_identity']['app_launcher']
|
||||
if (AppLauncher.__module__ != 'isaaclab.app.app_launcher'
|
||||
or Path(sys.modules[AppLauncher.__module__].__file__).resolve() != Path(app_source['path'])
|
||||
or b.digest(app_source['path']) != app_source['sha256']):
|
||||
raise ValueError('Actual installed AppLauncher identity mismatch')
|
||||
# Retain launcher even if initialization fails after creating its app.
|
||||
self.launcher = AppLauncher.__new__(AppLauncher)
|
||||
AppLauncher.__init__(self.launcher, headless=True, device='cuda:0', fast_shutdown=True,
|
||||
kit_args=f'--/log/file={self.output / "native" / self.mode / "kit.log"}')
|
||||
self.started = True
|
||||
for name in ('rootxy_training_v2', 'rootxy_runner_v2', 'rootxy_binding_v2', 'rootxy_runtime_v2'):
|
||||
setattr(self, name, importlib.import_module(owner.LEG + name))
|
||||
self.p = self.rootxy_training_v2
|
||||
self.binding = self.rootxy_binding_v2
|
||||
eb = importlib.import_module(owner.LEG + 'evaluation_binding')
|
||||
self.eb = eb
|
||||
for name in self.binding.ROLES:
|
||||
if name != '__main__':
|
||||
importlib.import_module(name)
|
||||
# Do not depend on an evaluation recorder to trigger lazy backends.
|
||||
preimport_binding_runtime(eb)
|
||||
# Match the finite installed constructor preimports, never a whole interpreter scan.
|
||||
owner.preimport_replicator()
|
||||
owner.preimport_constructor_modules(eb)
|
||||
self.constructor_export, self.constructor_metadata = owner.physx_constructor_export(eb, resolve=True)
|
||||
self.cfg_class = importlib.import_module(owner.TASK + 'rootxy_cfg_v2').RootXYContactCycleEnvCfg
|
||||
self.environment_class = importlib.import_module(owner.TASK + 'env').SimContactCycleEnv
|
||||
|
||||
def configure(self):
|
||||
self.cfg = self.cfg_class()
|
||||
self.cfg.scene.num_envs = 128
|
||||
self.cfg.seed = 92511
|
||||
self.cfg.sim.device = 'cuda:0'
|
||||
# This helper only verifies installed stepping and explicitly forwards
|
||||
# fixed constructor video config fields; no evaluation environment created.
|
||||
owner.preforward_video_recorder(self.cfg, importlib.import_module(owner.TASK + 'evaluation_env'))
|
||||
owner.preimport_config_references(self.cfg, self.eb)
|
||||
self.binding.normalize_config(self.cfg) # exact128-env PREconstruction only
|
||||
import torch
|
||||
torch.cuda.init()
|
||||
torch.cuda.get_rng_state(torch.device('cuda:0'))
|
||||
if self.mode == 'prepare':
|
||||
self.inventory = self.binding.propose_inventory(ROOT, self.cfg, owner.proposed_native_layouts)
|
||||
self.expected = self.binding.snapshot(ROOT, self.output, self.cfg, self.inventory, self.manifest)
|
||||
else:
|
||||
self.expected = read(self.output / 'training-binding.json', self.payload['grant']['binding_sha256'])
|
||||
self.inventory = self.expected['dependency_inventory']
|
||||
if self.mode == 'run':
|
||||
outer = self
|
||||
class Ledger(self.p.RootXYBudgetLedger):
|
||||
def guard(self, reserve_bytes=0):
|
||||
outer.guard()
|
||||
outer.budget.check(reserve_bytes)
|
||||
return super().guard(reserve_bytes=reserve_bytes)
|
||||
self.ledger = Ledger(self.output, self.manifest, launch_start_s=self.payload['start'])
|
||||
self.ledger.__enter__()
|
||||
|
||||
@contextmanager
|
||||
def physics(self):
|
||||
from isaaclab.sim import SimulationContext
|
||||
with self.rootxy_runtime_v2.RootXYPhysicsHooks(
|
||||
SimulationContext, self.environment_class, self.guard,
|
||||
lambda value: self.budget.write(self.output / 'physics-counts.json', value),
|
||||
preparation=self.mode == 'prepare') as hooks:
|
||||
# Config construction/normalization itself is under zero-physics PREP hooks.
|
||||
self.configure()
|
||||
yield hooks
|
||||
|
||||
def verify(self):
|
||||
self.budget.check(cleanup=True)
|
||||
verify_sources(self.manifest)
|
||||
if self.cfg is None or self.expected is None:
|
||||
raise RuntimeError('Startup did not establish actual config identity')
|
||||
target, metadata = owner.physx_constructor_export(self.eb, expected=self.constructor_export)
|
||||
if metadata != self.constructor_metadata:
|
||||
raise ValueError('Constructor export drift')
|
||||
self.binding.verify(self.expected, root=ROOT, output=self.output, cfg=self.cfg,
|
||||
inventory=self.inventory, manifest=self.manifest)
|
||||
if self.env is not None and getattr(self.env, 'cfg', None) is not self.cfg:
|
||||
raise ValueError('Constructor substituted actual config object')
|
||||
|
||||
def prepare(self):
|
||||
self.budget.write(self.output / 'training-binding.json', self.expected)
|
||||
result = {'format': PREPARATION_FORMAT, 'status': 'prepared_config_only',
|
||||
'manifest_sha256': self.payload['manifest_sha256'],
|
||||
'binding_sha256': b.digest(self.output / 'training-binding.json'),
|
||||
'artifact_root': str(self.output), 'physics_calls': 0, 'num_envs': 128,
|
||||
'runtime_qualified': False, 'p2_decision': 'pending', 'accepted_skill': False,
|
||||
'recipe': {'device': 'cuda:0', 'train_seed': 92511, 'development_seed': 93511,
|
||||
'environment_constructed': False, 'inference_executed': False,
|
||||
'profile': PROFILE, 'hooks': self.binding.HOOKS},
|
||||
'ready_gaps': ['parent review of actual config-only binding',
|
||||
'separate approved P2 decision and fresh run grant',
|
||||
'immediate postconstructor actual-used identity and reset safety qualification']}
|
||||
self.budget.write(self.output / 'config-preparation-provisional.json', result)
|
||||
return result
|
||||
|
||||
def construct(self):
|
||||
self.guard()
|
||||
self.env = self.environment_class.__new__(self.environment_class)
|
||||
self.environment_class.__init__(self.env, cfg=self.cfg)
|
||||
|
||||
def check_actual(self):
|
||||
importlib.import_module(owner.TASK + 'rootxy_cfg_v2').verify_actual_config(self.env)
|
||||
if self.env._physics_handles_decimation:
|
||||
raise RuntimeError('Require scalar 500Hz physics stepping')
|
||||
|
||||
def safety(self):
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
gates = read(ROOT / self.p.PILOT['safety_config'])['gates']
|
||||
return PhysicalSafetyAudit(self.env, gates)
|
||||
|
||||
def learn(self):
|
||||
import torch
|
||||
from audit_contact_cycle_functional import evaluate
|
||||
trace_path = ROOT / self.p.replay.CANDIDATE
|
||||
trace_path = trace_path.parent / 'validation_u0256_trace.pt'
|
||||
if b.digest(trace_path) != self.manifest['parity_trace']['sha256']:
|
||||
raise ValueError('Saved CPU parity trace drift')
|
||||
trace = torch.load(trace_path, map_location='cpu', weights_only=True)
|
||||
observations = next(iter(trace.values()))['actor_input'][::100, 0].contiguous()
|
||||
del trace
|
||||
|
||||
def guard():
|
||||
self.guard()
|
||||
self.physics_hooks.safety_boundary()
|
||||
|
||||
def before_control(kind):
|
||||
if kind not in ('optimization', 'validation'):
|
||||
raise ValueError('Unknown control budget kind')
|
||||
guard()
|
||||
self.physics_hooks.mode = 'rollout'
|
||||
|
||||
def after_control(kind):
|
||||
self.physics_hooks.mode = 'initialization'
|
||||
guard()
|
||||
|
||||
hooks = self.rootxy_runner_v2.RuntimeHooks(
|
||||
admit=lambda manifest: {'manifest_sha256': self.p.digest(manifest), 'p2_review_approved': True,
|
||||
'sole_cleanup_owner': True, 'native_monitor_active': True},
|
||||
guard=guard, verify_identity=self.verify, before_control=before_control, after_control=after_control,
|
||||
progress=lambda value: guard(), finish=lambda value: guard())
|
||||
return self.rootxy_runner_v2.execute(self.env, ROOT, self.output, self.manifest, self.ledger,
|
||||
observations, hooks, evaluate)
|
||||
|
||||
def close_env(self):
|
||||
if self.env is not None:
|
||||
self.env.close()
|
||||
|
||||
def close_app(self, code):
|
||||
error = None
|
||||
try:
|
||||
if self.ledger is not None:
|
||||
self.ledger.__exit__()
|
||||
except BaseException as caught:
|
||||
error = caught
|
||||
app = None if self.launcher is None else getattr(self.launcher, '_app', None)
|
||||
if app is not None:
|
||||
# Fast shutdown may exit instead of returning: never pass zero after
|
||||
# another teardown failure and rely on later Python unwinding.
|
||||
app.close(exit_code=1 if error is not None else code)
|
||||
if error is not None:
|
||||
raise error
|
||||
|
||||
|
||||
def run_worker(fd):
|
||||
payload = worker_payload(fd) # no unauthenticated dictionary entrypoint
|
||||
if payload['mode'] not in ('prepare', 'run') or payload['boot_id'] != b.boot_id():
|
||||
raise PermissionError('Invalid authenticated RootXY handoff')
|
||||
root = checked_root(payload['output'])
|
||||
reservation = read(root / (payload['mode'] + '-reservation.json'))
|
||||
if (reservation['grant'] != payload['grant'] or reservation['start_boottime'] != payload['start']
|
||||
or reservation['boot_id'] != payload['boot_id']):
|
||||
raise PermissionError('RootXY reservation mismatch')
|
||||
budget = GuardBudget(root, payload['start'], payload['mode'])
|
||||
|
||||
def guard():
|
||||
heartbeat = read(root / 'monitor-heartbeat.json')
|
||||
if (heartbeat['boot_id'] != b.boot_id()
|
||||
or not 0 <= b.boottime() - heartbeat['boottime'] <= 5):
|
||||
raise RuntimeError('Native monitor heartbeat missing/stale')
|
||||
budget.external_growth = heartbeat['external_growth']
|
||||
budget.check()
|
||||
|
||||
handlers = {s: signal.signal(s, lambda *_: setattr(budget, 'interrupted', True))
|
||||
for s in (signal.SIGTERM, signal.SIGINT)}
|
||||
# Pure hook module is loaded under a private identity before Kit, not a fake
|
||||
# SkillFactory package. Actual runtime imports after AppLauncher use real paths.
|
||||
spec = importlib.util.spec_from_file_location('_rootxy_lifecycle_cpu', ROOT / owner.PACKAGE /
|
||||
'legs/contact_cycle_sim/rootxy_runtime_v2.py')
|
||||
runtime = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(runtime)
|
||||
api = RuntimeAPI(payload, budget, guard)
|
||||
verify_sources(api.manifest)
|
||||
try:
|
||||
runtime.worker_lifecycle(api, prepare=payload['mode'] == 'prepare', guard=guard, verify=api.verify,
|
||||
publish=lambda value: budget.write(
|
||||
root / (payload['mode'] + '-worker-result.json'), value, cleanup=True))
|
||||
return 0
|
||||
finally:
|
||||
for sig, handler in handlers.items():
|
||||
signal.signal(sig, handler)
|
||||
@@ -0,0 +1,119 @@
|
||||
"""Visual-only world-frame disturbance arrows; no physics or network assets."""
|
||||
|
||||
import math
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
def arrow_geometry(origins, forces, metres_per_newton=0.03):
|
||||
"""Return cylinder/cone instances along +Z, rotated using xyzw quaternions.
|
||||
|
||||
The tail is the application point and the tip is origin + scale * force.
|
||||
Zero forces are omitted (including pure torque disturbances). Inputs are
|
||||
read-only. Prototype cylinders/cones have unit radius and unit height.
|
||||
"""
|
||||
if not math.isfinite(metres_per_newton) or metres_per_newton <= 0:
|
||||
raise ValueError("Arrow scale must be finite and positive (m/N)")
|
||||
if (
|
||||
origins.ndim != 2
|
||||
or origins.shape[1] != 3
|
||||
or forces.shape != origins.shape
|
||||
or origins.device != forces.device
|
||||
or origins.dtype != forces.dtype
|
||||
or not origins.is_floating_point()
|
||||
or not torch.isfinite(origins).all()
|
||||
or not torch.isfinite(forces).all()
|
||||
):
|
||||
raise ValueError("Expected finite matching floating world positions/forces [B,3]")
|
||||
magnitude = torch.linalg.vector_norm(forces, dim=-1)
|
||||
if not torch.isfinite(magnitude).all():
|
||||
raise ValueError("Force magnitude overflow")
|
||||
active = magnitude > 1e-6
|
||||
origin, force, magnitude = origins[active], forces[active], magnitude[active]
|
||||
direction = force / magnitude[:, None]
|
||||
length = magnitude * metres_per_newton
|
||||
if not torch.isfinite(length).all():
|
||||
raise ValueError("Arrow length overflow")
|
||||
# atan2 avoids the cancellation of the [cross(z,d), 1+dot(z,d)]
|
||||
# construction near -Z; exact -Z uses a 180-degree turn around X.
|
||||
horizontal = torch.linalg.vector_norm(direction[:, :2], dim=-1)
|
||||
axis = torch.stack((-direction[:, 1], direction[:, 0], torch.zeros_like(horizontal)), dim=-1)
|
||||
axis /= horizontal.clamp_min(torch.finfo(forces.dtype).tiny)[:, None]
|
||||
axis[horizontal == 0] = axis.new_tensor([1.0, 0.0, 0.0])
|
||||
half_angle = torch.atan2(horizontal, direction[:, 2]) * 0.5
|
||||
orientation = torch.cat((axis * half_angle.sin()[:, None], half_angle.cos()[:, None]), dim=-1)
|
||||
head_length = torch.minimum(0.3 * length, length.new_tensor(0.05))
|
||||
shaft_length = length - head_length
|
||||
shaft_radius = torch.minimum(0.04 * length, length.new_tensor(0.004))
|
||||
head_radius = torch.minimum(0.12 * length, length.new_tensor(0.012))
|
||||
shaft_center = origin + direction * (shaft_length / 2)[:, None]
|
||||
head_center = origin + direction * (length - head_length / 2)[:, None]
|
||||
count = len(origin)
|
||||
return {
|
||||
"translations": torch.cat((shaft_center, head_center)),
|
||||
"orientations": torch.cat((orientation, orientation)),
|
||||
"scales": torch.cat(
|
||||
(
|
||||
torch.stack((shaft_radius, shaft_radius, shaft_length), dim=-1),
|
||||
torch.stack((head_radius, head_radius, head_length), dim=-1),
|
||||
)
|
||||
),
|
||||
"marker_indices": torch.cat(
|
||||
(
|
||||
torch.zeros(count, dtype=torch.int32, device=origins.device),
|
||||
torch.ones(count, dtype=torch.int32, device=origins.device),
|
||||
)
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def _make_markers():
|
||||
# Lazy Kit imports: pure geometry tests and non-visual runs need no renderer.
|
||||
import isaaclab.sim as sim_utils
|
||||
from isaaclab.markers import VisualizationMarkers, VisualizationMarkersCfg
|
||||
|
||||
return VisualizationMarkers(
|
||||
VisualizationMarkersCfg(
|
||||
prim_path="/Visuals/AppliedDisturbanceForce",
|
||||
markers={
|
||||
"shaft": sim_utils.CylinderCfg(
|
||||
radius=1.0,
|
||||
height=1.0,
|
||||
axis="Z",
|
||||
visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.15, 0.02)),
|
||||
collision_props=None,
|
||||
rigid_props=None,
|
||||
),
|
||||
"head": sim_utils.ConeCfg(
|
||||
radius=1.0,
|
||||
height=1.0,
|
||||
axis="Z",
|
||||
visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.15, 0.02)),
|
||||
collision_props=None,
|
||||
rigid_props=None,
|
||||
),
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class DisturbanceForceArrows:
|
||||
def __init__(self, metres_per_newton=0.03):
|
||||
if not math.isfinite(metres_per_newton) or metres_per_newton <= 0:
|
||||
raise ValueError("Arrow scale must be finite and positive (m/N)")
|
||||
self.metres_per_newton = metres_per_newton
|
||||
self.markers = _make_markers()
|
||||
self.markers.set_visibility(False)
|
||||
|
||||
@torch.no_grad()
|
||||
def update(self, state):
|
||||
# Actual last-applied disturbance ONLY, not guidance, total load, sensor
|
||||
# reaction, or the next requested/random wrench. root()[0] is world XYZ.
|
||||
geometry = arrow_geometry(state.root()[0], state.last_disturbance[:, :3], self.metres_per_newton)
|
||||
visible = len(geometry["translations"]) > 0
|
||||
self.markers.set_visibility(visible)
|
||||
if visible:
|
||||
self.markers.visualize(**geometry)
|
||||
|
||||
def close(self):
|
||||
self.markers.set_visibility(False)
|
||||
@@ -0,0 +1,303 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicitly authorized contact-cycle PPO. --prepare does not launch Kit or train.
|
||||
|
||||
Independent testing is intentionally not an option. A continuation request or a
|
||||
previous standing authorization is NOT a grant for this runner.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import shutil
|
||||
import signal
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines, finish_run, write_json
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def execute(env, cfg, args, manifest, ledger, safety):
|
||||
import torch
|
||||
from SkillFactory.legs.contact_cycle.curriculum import EpisodeSampler
|
||||
from SkillFactory.legs.contact_cycle.ppo import (
|
||||
CycleActorCritic,
|
||||
collect_rollout,
|
||||
features,
|
||||
ppo_step,
|
||||
restore_checkpoint,
|
||||
restore_rng,
|
||||
rng_state,
|
||||
rollout_diagnostics,
|
||||
save_checkpoint,
|
||||
selection_score,
|
||||
)
|
||||
from SkillFactory.legs.contact_cycle.training import artifact_bytes, save_trace_subset, source_identity
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import CycleRecorder
|
||||
|
||||
torch.set_num_threads(4)
|
||||
s = state(env)
|
||||
if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0
|
||||
or env.cfg.sim.physics.enable_external_forces_every_iteration):
|
||||
raise RuntimeError("Require validated floating-root PGS/external-force physics settings")
|
||||
stop = {"requested": False}
|
||||
signal.signal(signal.SIGTERM, lambda *_: stop.update(requested=True))
|
||||
signal.signal(signal.SIGINT, lambda *_: stop.update(requested=True))
|
||||
runtime = {"torch": str(torch.__version__), "device": str(env.device), "physics_dt": env.physics_dt,
|
||||
"control_dt": env.step_dt, "num_envs": env.num_envs}
|
||||
identity = {"manifest": manifest, "runtime": runtime}
|
||||
runtime_path = args.output/"runtime_identity.json"
|
||||
if runtime_path.exists() and json.loads(runtime_path.read_text()) != runtime:
|
||||
raise RuntimeError("Runtime identity changed across resume")
|
||||
ledger.write_json(runtime_path, runtime)
|
||||
thresholds = json.loads((ROOT/cfg["proposal"]).read_text())["gates"]
|
||||
algo = {**cfg["algorithm"], "horizon": cfg["horizon"]}
|
||||
|
||||
def guard():
|
||||
ledger.guard()
|
||||
safety.check()
|
||||
if stop["requested"]:
|
||||
raise InterruptedError("Operator requested stop; resume from last atomic learner checkpoint")
|
||||
|
||||
def check_source():
|
||||
guard()
|
||||
if source_identity(ROOT, cfg, args.config) != manifest["sources"]:
|
||||
raise RuntimeError("Training inputs changed; refusing continuation/checkpoint")
|
||||
if args.console_log is not None and critical_log_lines(args.console_log):
|
||||
raise RuntimeError("Critical runtime log; refusing successful completion")
|
||||
|
||||
def save(path, model, optimizer, seed_identity, update, sampler, best):
|
||||
guard()
|
||||
remaining = cfg["max_artifact_bytes"] - artifact_bytes(args.output) - 1024*1024
|
||||
save_checkpoint(path, model, optimizer, seed_identity, update, sampler, best, ledger.data,
|
||||
max_bytes=remaining)
|
||||
ledger.guard()
|
||||
|
||||
candidates = []
|
||||
for seed in cfg["seeds"]:
|
||||
guard()
|
||||
seed_dir = args.output/f"seed_{seed}"
|
||||
seed_dir.mkdir(exist_ok=True)
|
||||
seed_identity = {**identity, "seed": seed}
|
||||
torch.manual_seed(seed)
|
||||
torch.cuda.manual_seed_all(seed)
|
||||
model = CycleActorCritic(cfg["algorithm"]["hidden"], cfg["algorithm"]["initial_std"]).to(env.device)
|
||||
optimizer = torch.optim.Adam(model.parameters(), lr=cfg["algorithm"]["learning_rate"], eps=1e-5)
|
||||
sampler = EpisodeSampler(s.cc, env.num_envs, seed)
|
||||
latest = seed_dir/"latest.pt"
|
||||
update, best = 0, None
|
||||
if latest.exists():
|
||||
checkpoint = restore_checkpoint(latest, model, optimizer, seed_identity, sampler)
|
||||
update, best = checkpoint["update"], checkpoint["best"]
|
||||
if ledger.data["pending"] is not None and ledger.data["pending"]["seed"] != seed:
|
||||
if update != cfg["updates_per_seed"]:
|
||||
raise RuntimeError("Unexpected unfinished earlier seed")
|
||||
else:
|
||||
ledger.reconcile_checkpoint(seed, update)
|
||||
s.set_sampler(sampler)
|
||||
x = features(env.reset()[0]["policy"])
|
||||
if not latest.exists():
|
||||
save(latest, model, optimizer, seed_identity, 0, sampler, None)
|
||||
|
||||
def log(row):
|
||||
ledger.append_json(seed_dir/"metrics.jsonl", row)
|
||||
|
||||
@torch.no_grad()
|
||||
def validation():
|
||||
check_source()
|
||||
saved_rng = rng_state()
|
||||
validation_sampler = EpisodeSampler(s.cc, env.num_envs, cfg["validation_seed"], split="validation")
|
||||
s.set_sampler(validation_sampler)
|
||||
observation = env.reset()[0]["policy"]
|
||||
inputs = {"scenario": s.scenarios.cpu().tolist(), "commands": s.schedule.commands.cpu().tolist(),
|
||||
"points": s.sampled_points.cpu().tolist(), "delays": s.schedule.delays.cpu().tolist()}
|
||||
if set(inputs["scenario"]) != set(range(4)):
|
||||
raise RuntimeError("Validation family coverage missing")
|
||||
steps = round(sum(s.cc["phase_durations_s"])/env.step_dt)
|
||||
try:
|
||||
with CycleRecorder(env) as recorder:
|
||||
for step in range(steps):
|
||||
guard()
|
||||
action, mean, _, _ = model.act(features(observation), deterministic=True)
|
||||
recorder.before_control(observation, action, mean,
|
||||
model.log_std.clamp(math.log(0.02), math.log(0.5)).exp())
|
||||
obs, _, terminated, truncated, _ = env.step(action)
|
||||
recorder.end_control(terminated, truncated, step == steps - 1)
|
||||
observation = obs["policy"]
|
||||
rows, trace = recorder.summarize(thresholds)
|
||||
score = selection_score(rows)
|
||||
row = {"kind": "validation_only", "seed": seed, "validation_seed": cfg["validation_seed"],
|
||||
"update": update, "reservation": ledger.data["reservations"], "score": score,
|
||||
"failure_fraction": sum(not r["success"] for r in rows)/len(rows),
|
||||
"inputs": inputs, "rows": rows, "accepted_skill": False}
|
||||
label = f"validation_u{update:04d}_r{ledger.data['reservations']:06d}"
|
||||
ledger.write_json(seed_dir/f"{label}.json", row)
|
||||
# Keep bounded real failure traces: first four failed first episodes (or first four successes).
|
||||
selected = ([r["env"] for r in rows if not r["success"]] or list(range(env.num_envs)))[:4]
|
||||
save_trace_subset(seed_dir/f"{label}_trace.pt", trace, selected, recorder.stops,
|
||||
lambda size: ledger.guard(reserve_bytes=size + 1024*1024))
|
||||
save_trace_subset(seed_dir/f"{label}_initial.pt",
|
||||
{k: v[None] for k, v in recorder.initial_state.items()}, selected,
|
||||
[1]*env.num_envs, lambda size: ledger.guard(reserve_bytes=size + 1024*1024))
|
||||
ledger.guard()
|
||||
return row
|
||||
finally:
|
||||
s.set_sampler(sampler)
|
||||
restore_rng(saved_rng)
|
||||
|
||||
while update < cfg["updates_per_seed"]:
|
||||
check_source()
|
||||
sampler.set_update(update + 1)
|
||||
ledger.reserve_rollout(seed, update + 1)
|
||||
data, x, failures, timeouts = collect_rollout(env, model, x, algo, guard)
|
||||
guard()
|
||||
diagnostic_row = rollout_diagnostics(data, algo)
|
||||
if diagnostic_row["missing_diagnostic_samples"]:
|
||||
raise RuntimeError("Missing control-clock diagnostics; refusing optimizer update")
|
||||
learning = ppo_step(model, optimizer, data, algo)
|
||||
update += 1
|
||||
if update % cfg["validation_interval"] == 0:
|
||||
validation_row = validation()
|
||||
log(validation_row)
|
||||
if best is None or validation_row["score"] < best["score"]:
|
||||
relative = f"candidate_u{update:04d}_r{ledger.data['reservations']:06d}.pt"
|
||||
best = {"update": update, "score": validation_row["score"], "path": relative,
|
||||
"failure_fraction": validation_row["failure_fraction"]}
|
||||
save(seed_dir/relative, model, optimizer, seed_identity, update, sampler, best)
|
||||
best["sha256"] = hashlib.sha256((seed_dir/relative).read_bytes()).hexdigest()
|
||||
x = features(env.reset()[0]["policy"])
|
||||
# Checkpoint BEFORE durable progress commit; ledger reconciles the crash window on resume.
|
||||
check_source()
|
||||
save(latest, model, optimizer, seed_identity, update, sampler, best)
|
||||
ledger.commit_update(seed, update)
|
||||
log({"kind": "training", "update": update, "seed": seed, "learner": learning,
|
||||
"terminated": failures, "timeouts": timeouts, "curriculum_stage": sampler.stage,
|
||||
"episode_counts": sampler.episodes.tolist(),
|
||||
"diagnostics": diagnostic_row,
|
||||
"reserved_transitions": ledger.data["reserved_transitions"]})
|
||||
if best is None or hashlib.sha256((seed_dir/best["path"]).read_bytes()).hexdigest() != best["sha256"]:
|
||||
raise RuntimeError("Missing/changed fixed candidate")
|
||||
candidates.append({"seed": seed, **best, "path": str((seed_dir/best["path"]).relative_to(args.output))})
|
||||
check_source()
|
||||
scope = ("diagnostic_pilot" if cfg["format"] == "skillfactory.contact-cycle-diagnostic-training.v1"
|
||||
else "three_seed_study")
|
||||
locked = {"candidates": candidates, "training_scope": scope,
|
||||
"accepted_skill": False, "independent_test_executed": False,
|
||||
"validation_screen_passed": all(c["failure_fraction"] <= 0.05 for c in candidates),
|
||||
"rule": "Never reselect these candidates using independent test outcomes"}
|
||||
path = args.output/"locked_candidates.json"
|
||||
if path.exists() and json.loads(path.read_text()) != locked:
|
||||
raise RuntimeError("Cannot overwrite a previous candidate lock")
|
||||
ledger.write_json(path, locked)
|
||||
return {"passed": True, "training_complete": True, "training_scope": scope,
|
||||
"accepted_skill": False, "locked": locked,
|
||||
"budget": ledger.data}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
mode = parser.add_mutually_exclusive_group(required=True)
|
||||
mode.add_argument("--prepare", action="store_true")
|
||||
mode.add_argument("--train", action="store_true")
|
||||
parser.add_argument("--config", type=Path, default=ROOT/"configs/contact_cycle/training_v1.json")
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--authorization", type=Path)
|
||||
parser.add_argument("--resume", action="store_true")
|
||||
parser.add_argument("--console-log", type=Path)
|
||||
parser.add_argument("--headless", action="store_true")
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
args = parser.parse_args()
|
||||
frozen = (ROOT/"releases").resolve()
|
||||
if args.output.resolve() == frozen or frozen in args.output.resolve().parents:
|
||||
parser.error("Preparation/training must not write into frozen releases")
|
||||
from SkillFactory.legs.contact_cycle.training import (
|
||||
BudgetLedger,
|
||||
digest,
|
||||
make_manifest,
|
||||
require_authorization,
|
||||
)
|
||||
|
||||
manifest = make_manifest(ROOT, args.config)
|
||||
cfg = manifest["training"]
|
||||
if args.prepare:
|
||||
if args.resume or args.authorization is not None:
|
||||
parser.error("Preparation neither resumes nor authorizes training")
|
||||
snapshot_size = sum((ROOT/p).stat().st_size for p in manifest["sources"])
|
||||
if snapshot_size + len(json.dumps(manifest).encode()) + 1024*1024 > cfg["max_artifact_bytes"]:
|
||||
raise ValueError("Preparation snapshots exceed total artifact budget")
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
write_json(args.output/"manifest.json", manifest)
|
||||
for relative in manifest["sources"]:
|
||||
target = args.output/"source_snapshot"/relative
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(ROOT/relative, target)
|
||||
write_json(args.output/"preparation.json", {"prepared": True, "authorized": False, "trained": False,
|
||||
"manifest_sha256": digest(manifest)})
|
||||
print(f"Prepared only; no Kit/optimizer/independent test: {args.output}", flush=True)
|
||||
return
|
||||
# Fail BEFORE importing/launching Isaac/Kit and BEFORE starting the budget clock.
|
||||
grant = require_authorization(args.authorization, manifest)
|
||||
if not (args.output/"manifest.json").is_file() or json.loads((args.output/"manifest.json").read_text()) != manifest:
|
||||
raise ValueError("Use an immutable prepared directory matching the current sources/config")
|
||||
if (args.output/"budget.json").exists() != args.resume:
|
||||
raise ValueError("Existing experiment requires --resume; new experiment must not use it")
|
||||
if not args.headless:
|
||||
raise ValueError("Initial authorized run is headless only")
|
||||
if args.console_log is None or not args.console_log.resolve().is_relative_to(args.output.resolve()):
|
||||
raise ValueError("Supervised console log must be recorded inside the shared artifact budget directory")
|
||||
app = env = safety = None
|
||||
result = {"passed": False, "training_complete": False, "accepted_skill": False,
|
||||
"training_scope": ("diagnostic_pilot"
|
||||
if cfg["format"] == "skillfactory.contact-cycle-diagnostic-training.v1"
|
||||
else "three_seed_study")}
|
||||
code = 1
|
||||
with BudgetLedger(args.output, manifest) as ledger:
|
||||
authorization_record = args.output/"authorization_record.json"
|
||||
if authorization_record.exists() and json.loads(authorization_record.read_text()) != grant:
|
||||
raise ValueError("Cannot replace the experiment's authorization record")
|
||||
ledger.write_json(authorization_record, grant)
|
||||
previous_result = args.output/"result.json"
|
||||
if previous_result.exists():
|
||||
history = args.output/"attempt_history"
|
||||
history.mkdir(exist_ok=True)
|
||||
number = len(list(history.glob("result_*.json"))) + 1
|
||||
ledger.write_json(history/f"result_{number:04d}.json", json.loads(previous_result.read_text()))
|
||||
try:
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
app = AppLauncher(headless=True, device=args.device).app
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.env import ContactCycleEnv
|
||||
|
||||
environment_cfg = ContactCycleEnvCfg(cycle_config=str(ROOT/cfg["task_config"]))
|
||||
environment_cfg.scene.num_envs = cfg["num_envs"]
|
||||
environment_cfg.sim.device = args.device
|
||||
environment_cfg.seed = cfg["seeds"][0]
|
||||
env = ContactCycleEnv(cfg=environment_cfg)
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
|
||||
thresholds = json.loads((ROOT/cfg["proposal"]).read_text())["gates"]
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
result = execute(env, cfg, args, manifest, ledger, safety)
|
||||
code = 0
|
||||
except BaseException:
|
||||
result["error"] = traceback.format_exc()
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
result["budget"] = ledger.data
|
||||
if safety is not None:
|
||||
result["physics_safety"] = safety.report()
|
||||
if app is not None:
|
||||
finish_run(app, env, args.output/"result.json", result, code)
|
||||
else:
|
||||
write_json(args.output/"result.json", result)
|
||||
if code:
|
||||
sys.exit(code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,291 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fresh 46D residual PPO pilot. --prepare neither launches Kit nor authorizes learning.
|
||||
|
||||
Only a parent-bound final manifest grant permits --train. No resume/warm-start CLI.
|
||||
The sole actual training launch must be supervised with a <=3480s outer timeout.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import signal
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def execute(env, cfg, args, manifest, ledger, guard):
|
||||
import torch
|
||||
from audit_contact_cycle_functional import evaluate
|
||||
from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic
|
||||
from SkillFactory.legs.contact_cycle_sim.ppo import (
|
||||
features,
|
||||
save_checkpoint,
|
||||
selection_key,
|
||||
)
|
||||
from SkillFactory.legs.contact_cycle_sim.training import (
|
||||
MODEL_CONFIG,
|
||||
artifact_bytes,
|
||||
checkpoint_then_commit,
|
||||
choose_candidate,
|
||||
isolated_validation_rng,
|
||||
optimize_reserved_rollout,
|
||||
parameter_deltas,
|
||||
save_trace_subset,
|
||||
source_identity,
|
||||
validation_rows,
|
||||
verify_snapshot,
|
||||
)
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
||||
|
||||
torch.set_num_threads(4)
|
||||
s = state(env)
|
||||
if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0
|
||||
or env.cfg.sim.physics.enable_external_forces_every_iteration
|
||||
or env.cfg.terminations.failed is None or env.physics_dt != .002 or env.step_dt != .01
|
||||
or env.num_envs != 128 or env.cfg.episode_length_s != 9):
|
||||
raise RuntimeError("Require unchanged floating-root PGS, active failures, 500Hz physics and 9s nominal task")
|
||||
runtime = {"torch": str(torch.__version__), "device": str(env.device), "physics_dt": env.physics_dt,
|
||||
"control_dt": env.step_dt, "num_envs": env.num_envs}
|
||||
identity = {"manifest": manifest, "runtime": runtime, "seed": cfg["seeds"][0], "model_config": MODEL_CONFIG}
|
||||
ledger.write_json(args.output / "runtime_identity.json", identity)
|
||||
functional = json.loads((ROOT / cfg["functional_config"]).read_text())
|
||||
algo = {**cfg["algorithm"], "horizon": cfg["horizon"]}
|
||||
seed = cfg["seeds"][0]
|
||||
torch.manual_seed(seed)
|
||||
torch.cuda.manual_seed_all(seed)
|
||||
model = SimResidualActorCritic(**MODEL_CONFIG).to(env.device)
|
||||
initial = {k: v.detach().cpu().clone() for k, v in model.named_parameters()}
|
||||
optimizer = torch.optim.Adam(model.parameters(), lr=algo["learning_rate"], eps=algo["adam_eps"])
|
||||
best = None
|
||||
|
||||
def check_source():
|
||||
guard()
|
||||
if source_identity(ROOT, cfg, args.config) != manifest["sources"]:
|
||||
raise RuntimeError("Bound sources/assets/config changed; refusing optimization or publication")
|
||||
verify_snapshot(args.output, manifest)
|
||||
if critical_log_lines(args.console_log):
|
||||
raise RuntimeError("Critical simulator console log")
|
||||
|
||||
def save(path, update):
|
||||
check_source()
|
||||
remaining = cfg["max_artifact_bytes"] - artifact_bytes(args.output) - 1048576
|
||||
size = save_checkpoint(path, model, optimizer, identity, update, best, ledger.data,
|
||||
MODEL_CONFIG, max_bytes=remaining)
|
||||
ledger.guard()
|
||||
return size
|
||||
|
||||
@torch.no_grad()
|
||||
def validation(update):
|
||||
check_source()
|
||||
with isolated_validation_rng(env, cfg["validation_seed"], seed):
|
||||
observation = features(env.reset()[0]["policy"])
|
||||
if bool(s.scenarios.any() or s.stand_only.any()):
|
||||
raise RuntimeError("Validation must contain only nominal complete cycles")
|
||||
returns = torch.zeros(env.num_envs, device=env.device)
|
||||
live = torch.ones(env.num_envs, dtype=torch.bool, device=env.device)
|
||||
with SimCycleRecorder(env) as recorder:
|
||||
for step in range(900):
|
||||
guard()
|
||||
action, mean, _, _ = model.act(observation, deterministic=True)
|
||||
recorder.before_control(observation, action, mean,
|
||||
model.log_std.clamp(math.log(.02), math.log(.5)).exp())
|
||||
obs, reward, terminated, truncated, _ = env.step(action)
|
||||
if not torch.isfinite(reward).all():
|
||||
raise RuntimeError("Nonfinite validation reward")
|
||||
returns += reward * live
|
||||
live &= ~(terminated | truncated)
|
||||
recorder.end_control(terminated, truncated, step == 899)
|
||||
observation = features(obs["policy"])
|
||||
trace = {k: torch.stack([frame[k] for frame in recorder.frames]) for k in recorder.frames[0]}
|
||||
# Recorded buffers are copies; release the original per-frame storage before serialization.
|
||||
recorder.frames.clear()
|
||||
rows = validation_rows(trace, recorder.stops, recorder.early, s.nominal, functional, evaluate)
|
||||
for row, total in zip(rows, returns.cpu().tolist()):
|
||||
row["episode_return"] = total
|
||||
result = {"kind": ("untrained_zero_residual_network" if update == 0
|
||||
else "analytic_reference_plus_learned_residual"),
|
||||
"trained": update > 0, "update": update, "seed": seed,
|
||||
"validation_seed": cfg["validation_seed"], "rows": rows,
|
||||
"selection_key": list(selection_key(rows)), "parameter_deltas": parameter_deltas(model, initial),
|
||||
"accepted_skill": False, "independent_test_executed": False,
|
||||
"audit_controller_kind": "learned_policy_metadata_interface_only"}
|
||||
label = f"validation_u{update:04d}"
|
||||
ledger.write_json(args.output / f"{label}.json", result)
|
||||
selected = ([r["env"] for r in rows if not r["success"]] or list(range(env.num_envs)))[:4]
|
||||
def reserve(size):
|
||||
ledger.guard(reserve_bytes=size + 1048576)
|
||||
save_trace_subset(args.output / f"{label}_trace.pt", trace, selected, recorder.stops, reserve)
|
||||
save_trace_subset(args.output / f"{label}_initial.pt",
|
||||
{k: v[None] for k, v in recorder.initial_state.items()}, selected,
|
||||
[1] * env.num_envs, reserve)
|
||||
check_source()
|
||||
return result
|
||||
|
||||
save(args.output / "untrained_u0000.pt", 0)
|
||||
baseline = validation(0)
|
||||
if not all(r["success"] for r in baseline["rows"]) or len(baseline["rows"]) != 128:
|
||||
raise RuntimeError("u0 did not pass 128/128 functional cycles; stop before the first training rollout")
|
||||
comparisons = []
|
||||
x = features(env.reset()[0]["policy"])
|
||||
for update in range(1, cfg["updates_per_seed"] + 1):
|
||||
x, failures, timeouts, learning = optimize_reserved_rollout(
|
||||
env, model, optimizer, x, algo, seed, update, ledger, guard, check_source)
|
||||
if update in cfg["validation_updates"]:
|
||||
row = validation(update)
|
||||
if choose_candidate(best, update, row["rows"]):
|
||||
name = f"candidate_u{update:04d}.pt"
|
||||
best = {"update": update, "selection_key": row["selection_key"], "path": name}
|
||||
save(args.output / name, update)
|
||||
best["sha256"] = hashlib.sha256((args.output / name).read_bytes()).hexdigest()
|
||||
comparisons.append({"update": update, "selection_key": row["selection_key"],
|
||||
"parameter_deltas": row["parameter_deltas"],
|
||||
"mean_height_rms_m": sum(r["height_rms_m"] for r in row["rows"]) / 128,
|
||||
"mean_return": sum(r["episode_return"] for r in row["rows"]) / 128,
|
||||
"mean_actual_residual_rms_rad": sum(
|
||||
r["actual_residual_rms_rad"] for r in row["rows"]) / 128})
|
||||
x = features(env.reset()[0]["policy"])
|
||||
# Pending reservation remains charged on ANY failure; no automatic recovery.
|
||||
latest_bytes = checkpoint_then_commit(
|
||||
ledger, seed, update, lambda: save(args.output / "latest.pt", update))
|
||||
ledger.append_json(args.output / "metrics.jsonl", {"update": update, "learner": learning,
|
||||
"failures": failures, "timeouts": timeouts, "latest_checkpoint_bytes": latest_bytes,
|
||||
"reserved_transitions": ledger.data["reserved_transitions"]})
|
||||
check_source()
|
||||
if (best is None or best["update"] == 0
|
||||
or hashlib.sha256((args.output / best["path"]).read_bytes()).hexdigest() != best["sha256"]):
|
||||
raise RuntimeError("Missing or changed trained candidate")
|
||||
locked = {"candidate": best, "accepted_skill": False, "independent_test_executed": False,
|
||||
"training_scope": "nominal_simulation_residual_pilot",
|
||||
"rule": "failure fraction then all-row cost; earliest tie; never reselect from later outcomes"}
|
||||
ledger.write_json(args.output / "locked_candidate.json", locked)
|
||||
ledger.write_json(args.output / "baseline_comparison.json", {
|
||||
"baseline_kind": baseline["kind"], "baseline_selection_key": baseline["selection_key"],
|
||||
"baseline_mean_height_rms_m": sum(r["height_rms_m"] for r in baseline["rows"]) / 128,
|
||||
"baseline_mean_return": sum(r["episode_return"] for r in baseline["rows"]) / 128,
|
||||
"baseline_mean_actual_residual_rms_rad": sum(r["actual_residual_rms_rad"] for r in baseline["rows"]) / 128,
|
||||
"updated_models": comparisons, "selection_changed_by_comparison": False,
|
||||
"note": "Reference already passed at u0. Nonzero parameter deltas alone do not demonstrate improvement.",
|
||||
"accepted_skill": False})
|
||||
return {"passed": True, "training_complete": True, "accepted_skill": False, "locked": locked}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
mode = parser.add_mutually_exclusive_group(required=True)
|
||||
mode.add_argument("--prepare", action="store_true")
|
||||
mode.add_argument("--train", action="store_true")
|
||||
parser.add_argument("--config", type=Path, default=ROOT / "configs/contact_cycle/sim_training_v1.json")
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--authorization", type=Path)
|
||||
parser.add_argument("--console-log", type=Path)
|
||||
parser.add_argument("--supervisor-report", type=Path)
|
||||
parser.add_argument("--headless", action="store_true")
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
args = parser.parse_args(argv)
|
||||
from SkillFactory.legs.contact_cycle_sim.training import (
|
||||
FreshBudgetLedger,
|
||||
assert_no_conflicting_processes,
|
||||
make_manifest,
|
||||
prepare,
|
||||
require_authorization,
|
||||
validate_prepared,
|
||||
writable_output,
|
||||
)
|
||||
|
||||
args.output = writable_output(ROOT, args.output)
|
||||
if args.prepare:
|
||||
if args.authorization is not None or args.console_log is not None or args.supervisor_report is not None:
|
||||
parser.error("Preparation is not authorization or a supervised launch")
|
||||
prepare(ROOT, args.config, args.output)
|
||||
print(f"Prepared only; no Kit, budget or optimizer: {args.output}", flush=True)
|
||||
return
|
||||
# Every gate here runs BEFORE importing Kit and BEFORE the ledger clock starts.
|
||||
manifest = make_manifest(ROOT, args.config)
|
||||
grant = require_authorization(args.authorization, manifest)
|
||||
validate_prepared(ROOT, args.output, manifest)
|
||||
if not args.headless:
|
||||
raise ValueError("Pilot must be headless and externally supervised")
|
||||
if (args.console_log is None or args.console_log.resolve() != args.output / "console.log"
|
||||
or args.supervisor_report is None or args.supervisor_report.resolve() != args.output / "supervisor.json"
|
||||
or not args.console_log.is_file() or not args.supervisor_report.is_file()):
|
||||
raise ValueError("Require live supervisor console.log and supervisor.json inside artifact budget")
|
||||
supervisor = json.loads(args.supervisor_report.read_text())
|
||||
if supervisor.get("status") != "running" or supervisor.get("passed") is not False:
|
||||
raise ValueError("Require a running external supervisor")
|
||||
assert_no_conflicting_processes()
|
||||
cfg = manifest["training"]
|
||||
stop = {"requested": False}
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
signal.signal(sig, lambda *_: stop.update(requested=True))
|
||||
app = env = safety = None
|
||||
result = {"passed": False, "training_complete": False, "accepted_skill": False}
|
||||
code = 1
|
||||
with FreshBudgetLedger(args.output, manifest) as ledger:
|
||||
def guard():
|
||||
ledger.guard()
|
||||
if stop["requested"]:
|
||||
raise InterruptedError("Cooperative stop; pending work remains charged; no retry/resume")
|
||||
if safety is not None:
|
||||
safety.check()
|
||||
|
||||
ledger.write_json(args.output / "authorization_record.json", grant)
|
||||
try:
|
||||
guard()
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
app = AppLauncher(headless=True, device=args.device).app
|
||||
guard()
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv
|
||||
|
||||
environment_cfg = SimContactCycleEnvCfg(residual_config=str(ROOT / cfg["task_config"]))
|
||||
environment_cfg.scene.num_envs = cfg["num_envs"]
|
||||
environment_cfg.sim.device = args.device
|
||||
environment_cfg.seed = cfg["seeds"][0]
|
||||
env = SimContactCycleEnv(cfg=environment_cfg)
|
||||
thresholds = json.loads((ROOT / cfg["safety_config"]).read_text())["gates"]
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
result = execute(env, cfg, args, manifest, ledger, guard)
|
||||
guard()
|
||||
code = 0
|
||||
except BaseException:
|
||||
result.update(passed=False, error=traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
if env is not None:
|
||||
try:
|
||||
env.close()
|
||||
except BaseException:
|
||||
result.update(passed=False, shutdown_error=traceback.format_exc())
|
||||
code = 1
|
||||
result["budget"] = ledger.data
|
||||
if safety is not None:
|
||||
result["physics_safety"] = safety.report()
|
||||
try:
|
||||
guard()
|
||||
if critical_log_lines(args.console_log):
|
||||
raise RuntimeError("Critical console log during shutdown")
|
||||
except BaseException:
|
||||
result.update(passed=False, final_guard_error=traceback.format_exc())
|
||||
code = 1
|
||||
result["status"] = "completed" if code == 0 else "failed"
|
||||
# Never bypass the artifact/deadline guard to publish a success on failure.
|
||||
try:
|
||||
ledger.write_json(args.output / "result.json", result)
|
||||
finally:
|
||||
if app is not None:
|
||||
app.close(exit_code=code)
|
||||
if code:
|
||||
sys.exit(code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""RootXY-v2 dedicated supervised adapter; no mode authorizes itself.
|
||||
|
||||
--check-only/--plan are read-only CPU operations. --prepare is separately granted
|
||||
Kit config-only preparation (zero environments/physics). --run requires exact
|
||||
preparation plus new parent grant and approved P2 decision. Neither runs by default.
|
||||
"""
|
||||
import argparse
|
||||
import importlib
|
||||
import importlib.util
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def cpu_protocol():
|
||||
# Real file-backed private CPU namespace, not fake SkillFactory registration.
|
||||
# Never reuse this namespace for runtime binding or simulator construction.
|
||||
name = "_rootxy_cpu_legs"
|
||||
if name not in sys.modules:
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
name, ROOT / "source/SkillFactory/SkillFactory/legs/__init__.py",
|
||||
submodule_search_locations=[str(ROOT / "source/SkillFactory/SkillFactory/legs")])
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[name] = module
|
||||
spec.loader.exec_module(module)
|
||||
return importlib.import_module(name + ".contact_cycle_sim.rootxy_training_v2")
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
mode = parser.add_mutually_exclusive_group(required=True)
|
||||
mode.add_argument("--check-only", action="store_true")
|
||||
mode.add_argument("--plan", action="store_true")
|
||||
mode.add_argument("--_worker-fd", type=int, help=argparse.SUPPRESS)
|
||||
mode.add_argument("--prepare", action="store_true")
|
||||
mode.add_argument("--run", action="store_true")
|
||||
parser.add_argument("--config", type=Path, default=ROOT / "configs/contact_cycle/sim_training_rootxy_v2.json")
|
||||
parser.add_argument("--output", type=Path)
|
||||
parser.add_argument("--authorization", type=Path)
|
||||
parser.add_argument("--trusted-authorization-sha256")
|
||||
parser.add_argument("--manifest", type=Path)
|
||||
parser.add_argument("--trusted-manifest-sha256")
|
||||
parser.add_argument("--storage-policy", type=Path)
|
||||
parser.add_argument("--trusted-storage-policy-sha256")
|
||||
parser.add_argument("--p2-decision", type=Path)
|
||||
args = parser.parse_args(argv)
|
||||
if args._worker_fd is not None:
|
||||
import rootxy_launch_v2 as adapter
|
||||
return adapter.run_worker(args._worker_fd)
|
||||
if args.run or args.prepare:
|
||||
if args.output is None or any(getattr(args, k) is None for k in (
|
||||
'authorization', 'trusted_authorization_sha256', 'manifest', 'trusted_manifest_sha256',
|
||||
'storage_policy', 'trusted_storage_policy_sha256')) or args.run and args.p2_decision is None:
|
||||
raise PermissionError('RootXY-v2 admission INCOMPLETE: exact new manifest/preparation/native policy/'
|
||||
'parent grant and RUN P2 decision required; no launch authorized')
|
||||
if args.config.resolve() != ROOT / 'configs/contact_cycle/sim_training_rootxy_v2.json':
|
||||
raise ValueError('Fixed RootXY config required')
|
||||
import rootxy_launch_v2 as adapter
|
||||
return adapter.supervise(args)
|
||||
if args.authorization is not None:
|
||||
parser.error("CPU checks/preparation never accept launch authorization")
|
||||
if args.output is not None:
|
||||
parser.error('CPU check/plan writes no output')
|
||||
protocol = cpu_protocol()
|
||||
manifest = protocol.make_manifest(ROOT, args.config)
|
||||
if args.plan:
|
||||
print(json.dumps(manifest, sort_keys=True, separators=(',', ':'), allow_nan=False))
|
||||
else:
|
||||
print(json.dumps({'checked': True, 'prepared': False, 'authorized': False,
|
||||
'launch_integration_complete': True, 'runtime_qualified': False,
|
||||
'manifest_sha256': protocol.digest(manifest),
|
||||
'task_id': protocol.TASK_ID, 'accepted_skill': False}))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+49
-31
@@ -17,12 +17,7 @@ from pathlib import Path
|
||||
from runtime_common import critical_log_lines, write_json
|
||||
|
||||
|
||||
def main():
|
||||
import torch
|
||||
from SkillFactory.legs.policy import LegActorCritic, load_policy, ppo_update, save_policy
|
||||
from SkillFactory.legs.spec import load_bundle
|
||||
from SkillFactory.legs.tokens import FEATURE_SCHEMA
|
||||
|
||||
def parse_args(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--bundles", type=Path, nargs="+", required=True)
|
||||
parser.add_argument("--num_envs", type=int, default=16, help="Per-asset environment count")
|
||||
@@ -32,14 +27,60 @@ def main():
|
||||
parser.add_argument("--episode-length", type=float, default=0.2)
|
||||
parser.add_argument("--seed", type=int, default=42)
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
parser.add_argument(
|
||||
"--viz",
|
||||
"--visualizer",
|
||||
choices=("none", "kit"),
|
||||
help="Worker visualization (omitted: worker default). Kit windows restart for each asset/update.",
|
||||
)
|
||||
parser.add_argument("--output", type=Path, default=Path("logs/leg_interface/train_smoke"))
|
||||
args = parser.parse_args()
|
||||
args = parser.parse_args(argv)
|
||||
if (
|
||||
min(args.num_envs, args.max_joints, args.horizon, args.updates) < 1
|
||||
or not math.isfinite(args.episode_length)
|
||||
or args.episode_length <= 0
|
||||
):
|
||||
parser.error("Counts and episode length must be positive")
|
||||
return args
|
||||
|
||||
|
||||
def worker_command(args, bundle_path, weights, directory, seed):
|
||||
command = [
|
||||
sys.executable,
|
||||
str(Path(__file__).with_name("leg_worker.py")),
|
||||
"--bundle",
|
||||
str(bundle_path.resolve()),
|
||||
"--mode",
|
||||
"rollout",
|
||||
"--weights",
|
||||
str(weights.resolve()),
|
||||
"--num_envs",
|
||||
str(args.num_envs),
|
||||
"--max-joints",
|
||||
str(args.max_joints),
|
||||
"--horizon",
|
||||
str(args.horizon),
|
||||
"--episode-length",
|
||||
str(args.episode_length),
|
||||
"--seed",
|
||||
str(seed),
|
||||
"--device",
|
||||
args.device,
|
||||
"--output",
|
||||
str(directory.resolve()),
|
||||
]
|
||||
if args.viz is not None:
|
||||
command.extend(["--viz", args.viz])
|
||||
return command
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
import torch
|
||||
from SkillFactory.legs.policy import LegActorCritic, load_policy, ppo_update, save_policy
|
||||
from SkillFactory.legs.spec import load_bundle
|
||||
from SkillFactory.legs.tokens import FEATURE_SCHEMA
|
||||
|
||||
args.output.mkdir(parents=True, exist_ok=True)
|
||||
report = {
|
||||
"passed": False,
|
||||
@@ -73,30 +114,7 @@ def main():
|
||||
directory = args.output / f"update_{update:04d}" / part_id
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
log_path = directory / "worker.log"
|
||||
command = [
|
||||
sys.executable,
|
||||
str(Path(__file__).with_name("leg_worker.py")),
|
||||
"--bundle",
|
||||
str(bundle_path.resolve()),
|
||||
"--mode",
|
||||
"rollout",
|
||||
"--weights",
|
||||
str(weights.resolve()),
|
||||
"--num_envs",
|
||||
str(args.num_envs),
|
||||
"--max-joints",
|
||||
str(args.max_joints),
|
||||
"--horizon",
|
||||
str(args.horizon),
|
||||
"--episode-length",
|
||||
str(args.episode_length),
|
||||
"--seed",
|
||||
str(args.seed + index + update * 100),
|
||||
"--device",
|
||||
args.device,
|
||||
"--output",
|
||||
str(directory.resolve()),
|
||||
]
|
||||
command = worker_command(args, bundle_path, weights, directory, args.seed + index + update * 100)
|
||||
with log_path.open("w") as log:
|
||||
child = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT)
|
||||
critical = critical_log_lines(log_path)
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Development-only physical preflight. No optimizer, checkpoint selection or holdout access."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import shutil
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
|
||||
def evaluate(env, args, thresholds):
|
||||
import torch
|
||||
from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate
|
||||
from SkillFactory.legs.contact_cycle.metrics import summarize
|
||||
from SkillFactory.legs.tokens import LegKinematics
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.mdp import failed, state
|
||||
|
||||
s = state(env)
|
||||
fk = LegKinematics(s.bundle, env.device)
|
||||
nominal = s.nominal.expand(env.num_envs, -1)
|
||||
q_ik = nominal.clone()
|
||||
limits = torch.tensor([[j["limits"]["lower"], j["limits"]["upper"]] for j in s.bundle["joints"]], device=env.device)
|
||||
speeds = torch.tensor([j["limits"]["velocity"] for j in s.bundle["joints"]], device=env.device)
|
||||
stiffness = torch.tensor([j["kp"] for j in s.bundle["spec"]["joints"]], device=env.device)
|
||||
trace = {}
|
||||
|
||||
def foot(q):
|
||||
transform = fk.forward(q)[s.bundle["foot"]["body"]]
|
||||
return transform[:, :3, 3] + (transform[:, :3, :3] @ s.offset[:, :, None]).squeeze(-1)
|
||||
|
||||
def jacobian(q):
|
||||
columns = []
|
||||
for j in range(3):
|
||||
delta = torch.zeros_like(q)
|
||||
delta[:, j] = 0.0005
|
||||
columns.append((foot(q+delta)-foot(q-delta))/0.001)
|
||||
return torch.stack(columns, -1)
|
||||
|
||||
original = env.scene.update
|
||||
|
||||
def observe(dt):
|
||||
original(dt)
|
||||
if dt <= 0:
|
||||
return
|
||||
phase, progress, target, desired, _ = s.reference()
|
||||
pos = s.root()[0]
|
||||
excess = torch.maximum(limits[:, 0]-s.q(), s.q()-limits[:, 1]).clamp(min=0).amax(-1)
|
||||
ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints]/s.effort).abs().amax(-1),
|
||||
(s.robot.data.joint_vel.torch[:, s.joints]/speeds).abs().amax(-1))
|
||||
mass = s.robot.data.body_mass.torch
|
||||
gravity = torch.zeros(env.num_envs, s.robot.num_bodies, 3, device=env.device)
|
||||
gravity[..., 2] = -9.81 * mass
|
||||
com = s.robot.data.body_com_pos_w.torch
|
||||
payload_force = (s.external + s.guide + s.auxiliary)[:, s.payload, :3] + gravity[:, s.payload]
|
||||
payload_torque = (s.external + s.guide + s.auxiliary)[:, s.payload, 3:]
|
||||
expected_mount = torch.cat((payload_force, payload_torque + torch.cross(
|
||||
com[:, s.payload] - s.robot.data.body_link_pos_w.torch[:, s.anchor], payload_force, dim=-1)), -1)
|
||||
values = {
|
||||
"time": s.time, "phase": phase, "progress": progress, "scenario": s.scenarios,
|
||||
"foot": s.foot_center(), "target": target, "desired_force": desired, "weight": s.weight,
|
||||
"normal": s.contact(), "friction": s.friction(), "gap": s.foot_center()[:, 2]-s.bundle["foot"]["radius"],
|
||||
"slip_speed": s.slip_velocity(), "height_error": (pos[:, 2]-s.port_reference()[0][:, 2]).abs(),
|
||||
"joint_excess": excess, "limit_ratio": ratio, "joint_pos": s.q(),
|
||||
"joint_vel": s.robot.data.joint_vel.torch[:, s.joints],
|
||||
"applied_torque": s.robot.data.applied_torque.torch[:, s.joints],
|
||||
"base_state": torch.cat(s.root(), -1), "mount_wrench": s.mount_wrench(),
|
||||
"external": s.external, "guide": s.guide, "auxiliary": s.auxiliary,
|
||||
"contact_latch": s.latch.contact, "event_fired": s.schedule.fired,
|
||||
"event_envelope": s.schedule.envelope, "event_integral": s.schedule.integral,
|
||||
"event_completed": s.schedule.completed, "would_fail": failed(env), "tilt": s.tilt(),
|
||||
"linear_momentum": (mass[..., None] * s.robot.data.body_com_lin_vel_w.torch).sum(1),
|
||||
"total_external_force": (s.external[..., :3] + s.guide[..., :3] + s.auxiliary[..., :3] + gravity).sum(1)
|
||||
+ s.contact() + s.friction(),
|
||||
"mount_world": s.mount_world(), "expected_static_mount_world": expected_mount,
|
||||
}
|
||||
for k, v in values.items():
|
||||
trace.setdefault(k, []).append(v.detach().cpu().clone())
|
||||
|
||||
env.scene.update = observe
|
||||
steps = round(sum(s.cc["phase_durations_s"]) / env.step_dt)
|
||||
observations, actions = [], []
|
||||
try:
|
||||
for _ in range(steps):
|
||||
# Analytic engineering controller with privileged root pose/FK. Not a deployable learned policy.
|
||||
_, _, target, fn, _ = s.reference()
|
||||
pos, quat, _, _ = s.root()
|
||||
local = inverse_rotate(quat, target-pos)
|
||||
for _ in range(3):
|
||||
j = jacobian(q_ik)
|
||||
dq = torch.linalg.solve(j @ j.transpose(-1, -2) + torch.eye(3, device=env.device)*1e-5,
|
||||
(local-foot(q_ik))[:, :, None])
|
||||
q_ik += (j.transpose(-1, -2) @ dq).squeeze(-1).clamp(-0.08, 0.08)
|
||||
q_ik.clamp_(min=nominal-0.5, max=nominal+0.5)
|
||||
f = torch.zeros_like(local)
|
||||
f[:, 2] = fn
|
||||
torque = -(jacobian(q_ik).transpose(-1, -2) @ inverse_rotate(quat, f)[:, :, None]).squeeze(-1)
|
||||
command = q_ik + torque / stiffness
|
||||
action = ((command-nominal)/0.5).clamp(-1, 1)
|
||||
if args.controller == "disabled":
|
||||
action.zero_()
|
||||
obs, _, terminated, truncated, _ = env.step(action.float())
|
||||
if terminated.any() or truncated.any():
|
||||
raise RuntimeError("Physical validation must retain failures without automatic reset")
|
||||
observations.append(obs["policy"].cpu().clone())
|
||||
actions.append(action.cpu().clone())
|
||||
finally:
|
||||
env.scene.update = original
|
||||
data = {k: torch.stack(v) for k, v in trace.items()}
|
||||
torch.save(data, args.output / "physics_trace.pt")
|
||||
torch.save({"policy": torch.stack(observations) if observations else [],
|
||||
"action": torch.stack(actions) if actions else []}, args.output / "control_trace.pt")
|
||||
rows = summarize(data, env.physics_dt, thresholds, s.cc["scenarios"])
|
||||
momentum_residual = ((data["linear_momentum"][1:] - data["linear_momentum"][:-1]) / env.physics_dt
|
||||
- data["total_external_force"][1:])
|
||||
momentum_rms = momentum_residual.square().mean(0).sqrt().norm(dim=-1)
|
||||
static = (data["time"][:, 0] >= 2.0) & (data["time"][:, 0] < 2.8)
|
||||
sensor_residual = (data["mount_world"][static].mean(0)
|
||||
- data["expected_static_mount_world"][static].mean(0)).abs()
|
||||
expected_events = torch.tensor([[i == e["scenario"] for e in s.cc["events"]]
|
||||
for i in s.scenarios.tolist()], device="cpu")
|
||||
# Reset probes are outside the physics trial. Live nonzero wrench + selective reset tests persistence.
|
||||
before = {"root": s.robot.data.root_state_w.torch.clone(), "q": s.q().clone(),
|
||||
"time": s.time.clone(), "fired": s.schedule.fired.clone(), "sensor": s.sensed.clone()}
|
||||
ids = torch.arange(0, env.num_envs, 2, device=env.device)
|
||||
other = torch.arange(1, env.num_envs, 2, device=env.device)
|
||||
composer = s.robot.permanent_wrench_composer
|
||||
seeded = torch.arange(1, env.num_envs + 1, device=env.device).float()[:, None, None]
|
||||
seeded = seeded.expand(-1, s.robot.num_bodies, 3).contiguous()
|
||||
composer.set_forces_and_torques_index(forces=seeded, torques=seeded*0.1, positions=None, is_global=True)
|
||||
before_force, before_torque = composer.out_force_b.torch.clone(), composer.out_torque_b.torch.clone()
|
||||
env.reset(env_ids=ids)
|
||||
composer_reset = bool((composer.out_force_b.torch[ids] == 0).all()
|
||||
and (composer.out_torque_b.torch[ids] == 0).all()
|
||||
and torch.equal(before_force[other], composer.out_force_b.torch[other])
|
||||
and torch.equal(before_torque[other], composer.out_torque_b.torch[other]))
|
||||
isolated = all(torch.equal(a[other], b[other]) for a, b in (
|
||||
(before["root"], s.robot.data.root_state_w.torch), (before["q"], s.q()),
|
||||
(before["time"], s.time), (before["fired"], s.schedule.fired), (before["sensor"], s.sensed)))
|
||||
cleared = bool((s.external[ids] == 0).all() and (s.auxiliary[ids] == 0).all()
|
||||
and (s.time[ids] == 0).all() and not s.schedule.fired[ids].any()
|
||||
and (s.action_delay.data[:, ids] == 0).all() and not s.latch.contact[ids].any()
|
||||
and (s.schedule.integral[ids] == 0).all()
|
||||
and (s.sensor_delay.data[:, ids] == s.raw_sensors()[ids]).all()
|
||||
and (s.sensed[ids] == s.raw_sensors()[ids]).all())
|
||||
snapshot = s.policy().clone()
|
||||
env.reset(env_ids=[])
|
||||
empty = torch.equal(snapshot, s.policy())
|
||||
env.cfg.episode_length_s = 3 * env.step_dt
|
||||
env.reset()
|
||||
for _ in range(3):
|
||||
obs, _, terminated, truncated, info = env.step(torch.zeros(env.num_envs, 3, device=env.device))
|
||||
terminal = bool(truncated.all() and not terminated.any() and info["terminal_observation"] is not None
|
||||
and info["terminal_observation"]["policy"].shape == (env.num_envs, 32)
|
||||
and (s.time == 0).all() and not s.schedule.fired.any())
|
||||
before = s.time.clone()
|
||||
rejected = False
|
||||
try:
|
||||
env.step(torch.full((env.num_envs, 3), float("nan"), device=env.device))
|
||||
except ValueError:
|
||||
rejected = torch.equal(before, s.time)
|
||||
expected_integral = torch.tensor([e["duration_s"] - e["ramp_s"] for e in s.cc["events"]])
|
||||
delivered = (data["event_integral"][-1] - expected_events * expected_integral).abs() <= 0.002
|
||||
gates = {"floating_root": not s.robot.is_fixed_base, "finite_trace": all(r["finite"] for r in rows),
|
||||
"full_phases": all(r["complete"] for r in rows), "indexed_reset": isolated and cleared,
|
||||
"empty_reset": empty, "composer_reset": composer_reset,
|
||||
"timeout_terminal": terminal, "bad_action_rejected": rejected,
|
||||
"guide_vertical_zero": bool((data["guide"][..., 2] == 0).all()),
|
||||
"all_scenario_metrics": all(r["success"] for r in rows),
|
||||
"event_coverage": torch.equal(data["event_fired"][-1], expected_events)
|
||||
and torch.equal(data["event_completed"][-1], expected_events) and bool(delivered.all()),
|
||||
"scenario_coverage": set(data["scenario"][0].tolist()) == set(range(len(s.cc["scenarios"]))),
|
||||
"linear_momentum_balance": bool((momentum_rms <= 5.0).all()),
|
||||
"static_mount_force": bool((sensor_residual[:, :3] <= 1.5).all()),
|
||||
"static_mount_torque": bool((sensor_residual[:, 3:] <= 0.3).all())}
|
||||
return {"passed": all(gates.values()), "accepted_skill": False, "trained_skill": False,
|
||||
"kind": "development_physics_preflight", "controller": args.controller,
|
||||
"controller_uses_privileged_state": True, "seed": args.seed,
|
||||
"gates": gates, "rows": rows, "physics_dt": env.physics_dt, "control_dt": env.step_dt,
|
||||
"physics_steps": len(data["time"]), "support_enabled": env.cfg.auxiliary_enabled,
|
||||
"effective_mass_kg": dict(zip(s.robot.body_names, s.robot.data.body_mass.torch[0].tolist())),
|
||||
"event_triggered_per_env": data["event_fired"][-1].tolist(),
|
||||
"event_integral_s_per_env": data["event_integral"][-1].tolist(),
|
||||
"event_completed_per_env": data["event_completed"][-1].tolist(),
|
||||
"linear_momentum_balance_rms_N_per_env": momentum_rms.tolist(),
|
||||
"static_mount_residual_N_Nm_per_env": sensor_residual.tolist(),
|
||||
"note": "Preflight is not independent acceptance, multi-seed training, or hardware generalization."}
|
||||
|
||||
|
||||
def main():
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
from isaaclab_tasks.utils import add_launcher_args, launch_simulation
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, default=Path("configs/contact_cycle/go2_fl_v1.json"))
|
||||
parser.add_argument("--thresholds", type=Path, default=Path("configs/contact_cycle/acceptance_v1.json"))
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--num_envs", type=int, default=4)
|
||||
parser.add_argument("--seed", type=int, default=90101)
|
||||
parser.add_argument("--no-auxiliary", action="store_true")
|
||||
parser.add_argument("--controller", choices=("ik", "disabled"), default="ik")
|
||||
add_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
if args.num_envs < 4:
|
||||
parser.error("At least four environments required for development scenarios")
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
from SkillFactory.legs.contact_cycle.config import load_config, require_scenario_coverage
|
||||
|
||||
require_scenario_coverage(load_config(args.config), args.num_envs)
|
||||
sources = [Path(__file__), args.config, args.thresholds, root/"scripts/runtime_common.py",
|
||||
root/"scripts/supervise_sim.py", root/"configs/standing/go2_fl_support_v0.json",
|
||||
root/"assets/generated/support/go2_fl_3kg_v0/support.json",
|
||||
root/"assets/generated/legs/go2_fl/manifest.json"]
|
||||
sources += list((root/"source/SkillFactory/SkillFactory").rglob("*.py"))
|
||||
identity = {str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in sources}
|
||||
for p in sources:
|
||||
relative = (p.resolve().relative_to(root) if p.resolve().is_relative_to(root)
|
||||
else Path("external")/hashlib.sha256(p.read_bytes()).hexdigest()/p.name)
|
||||
target = args.output/"source_snapshot"/relative
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(p, target)
|
||||
write_json(args.output/"identity.json", identity)
|
||||
write_json(args.output/"invocation.json", {"argv": sys.argv, "thresholds": json.loads(args.thresholds.read_text())})
|
||||
write_json(args.output/"result.json", {"passed": False, "status": "running", "accepted_skill": False})
|
||||
app = AppLauncher(args).app
|
||||
env, result, code = None, {"passed": False, "accepted_skill": False}, 1
|
||||
try:
|
||||
import gymnasium as gym
|
||||
import SkillFactory.tasks # noqa: F401
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg
|
||||
|
||||
cfg = ContactCycleEnvCfg(cycle_config=str(args.config))
|
||||
cfg.scene.num_envs = args.num_envs
|
||||
cfg.seed = args.seed
|
||||
cfg.auxiliary_enabled = not args.no_auxiliary
|
||||
cfg.episode_length_s += 1
|
||||
cfg.terminations.failed = None # Full failed trajectory retained for audit, never censored by reset.
|
||||
if args.controller == "disabled":
|
||||
cfg.scene.robot.actuators["leg"].stiffness = 0
|
||||
cfg.scene.robot.actuators["leg"].damping = 0
|
||||
with launch_simulation(cfg, args):
|
||||
env = gym.make("SkillFactory-Go2FL-ContactCycle-v1", cfg=cfg).unwrapped
|
||||
env.reset(seed=args.seed)
|
||||
result = evaluate(env, args, json.loads(args.thresholds.read_text())["gates"])
|
||||
write_json(args.output/"config.json", cfg.cycle_parameters)
|
||||
if any(hashlib.sha256(Path(p).read_bytes()).hexdigest() != digest for p, digest in identity.items()):
|
||||
raise RuntimeError("Bound source/config changed during physical trial")
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
result.update(passed=False, error=traceback.format_exc())
|
||||
print(result["error"], file=sys.stderr)
|
||||
finally:
|
||||
finish_run(app, env, args.output/"result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,298 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Non-learning PhysX integration: sampled loads, first-episode recorder, 32D timeout rollout.
|
||||
|
||||
Uses a privileged analytic controller and an UNTRAINED network for API checks.
|
||||
No optimizer, learner checkpoint selection, training grant or independent test.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
|
||||
def run(env, output):
|
||||
import torch
|
||||
import warp as wp
|
||||
from SkillFactory.legs.contact_cycle.curriculum import EpisodeSampler, assert_sample_domain
|
||||
from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate
|
||||
from SkillFactory.legs.contact_cycle.ppo import CycleActorCritic, collect_rollout, features
|
||||
from SkillFactory.legs.tokens import LegKinematics
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import CycleRecorder
|
||||
|
||||
s = state(env)
|
||||
sampler = EpisodeSampler(s.cc, env.num_envs, 90211, update=1500)
|
||||
s.set_sampler(sampler)
|
||||
env.reset()
|
||||
sample = {"scenario": s.scenarios.cpu().clone(), "stand": s.stand_only.cpu().clone(),
|
||||
"stage": s.sampled_stage.cpu().clone(), "commands": s.schedule.commands.cpu().clone(),
|
||||
"points": s.sampled_points.cpu().clone(), "delays": s.schedule.delays.cpu().clone()}
|
||||
assert_sample_domain(s.cc, sample)
|
||||
torch.save(sample, output/"episode_inputs.pt")
|
||||
fk = LegKinematics(s.bundle, env.device)
|
||||
nominal = s.nominal.expand(env.num_envs, -1)
|
||||
q_ik = nominal.clone()
|
||||
stiffness = torch.tensor([j["kp"] for j in s.bundle["spec"]["joints"]], device=env.device)
|
||||
|
||||
def foot(q):
|
||||
transform = fk.forward(q)[s.bundle["foot"]["body"]]
|
||||
return transform[:, :3, 3] + (transform[:, :3, :3] @ s.offset[:, :, None]).squeeze(-1)
|
||||
|
||||
def jacobian(q):
|
||||
columns = []
|
||||
for j in range(3):
|
||||
delta = torch.zeros_like(q)
|
||||
delta[:, j] = 0.0005
|
||||
columns.append((foot(q+delta)-foot(q-delta))/0.001)
|
||||
return torch.stack(columns, -1)
|
||||
|
||||
def analytic_action(early_env=None):
|
||||
nonlocal q_ik
|
||||
_, _, target, fn, _ = s.reference()
|
||||
if early_env is not None and s.time[early_env] >= 3.8:
|
||||
target[early_env, 2] += 0.03 # physical negative control, not edited sensor data
|
||||
pos, quat, _, _ = s.root()
|
||||
local = inverse_rotate(quat, target-pos)
|
||||
for _ in range(3):
|
||||
j = jacobian(q_ik)
|
||||
dq = torch.linalg.solve(j @ j.transpose(-1, -2) + torch.eye(3, device=env.device)*1e-5,
|
||||
(local-foot(q_ik))[:, :, None])
|
||||
q_ik += (j.transpose(-1, -2) @ dq).squeeze(-1).clamp(-0.08, 0.08)
|
||||
q_ik.clamp_(min=nominal-0.5, max=nominal+0.5)
|
||||
force = torch.zeros_like(local)
|
||||
force[:, 2] = fn
|
||||
torque = -(jacobian(q_ik).transpose(-1, -2) @ inverse_rotate(quat, force)[:, :, None]).squeeze(-1)
|
||||
return ((q_ik + torque/stiffness - nominal)/0.5).clamp(-1, 1).float()
|
||||
|
||||
steps = round(sum(s.cc["phase_durations_s"])/env.step_dt)
|
||||
with CycleRecorder(env) as recorder:
|
||||
initial_snapshot = recorder.initial_state
|
||||
for k in range(steps):
|
||||
action = analytic_action()
|
||||
recorder.before_control(None, action)
|
||||
_, _, terminated, truncated, _ = env.step(action)
|
||||
torch.testing.assert_close(recorder.frames[-1]["requested_action"], action.cpu(), rtol=0, atol=0)
|
||||
recorder.end_control(terminated, truncated, k == steps-1)
|
||||
if terminated.any() or truncated.any():
|
||||
raise RuntimeError("Development sampled trace must not silently auto-reset")
|
||||
thresholds = json.loads(Path("configs/contact_cycle/acceptance_v1.json").read_text())["gates"]
|
||||
rows, trace = recorder.summarize(thresholds)
|
||||
torch.save(trace, output/"sampled_physics_trace.pt")
|
||||
torch.save(initial_snapshot, output/"sampled_initial_state.pt")
|
||||
initialization_checks = {
|
||||
"configured_on_plane": env.cfg.scene.robot.init_state.pos[2] == env.cfg.anchor_height,
|
||||
"measured_reset_gap": bool((initial_snapshot["gap"].abs() <= 1e-5).all()),
|
||||
"all_acquired_load": all(r["contact_acquired"] for r in rows),
|
||||
"no_initialization_failure": not bool(trace["acquisition_failed"].any()),
|
||||
"initial_snapshot_separate": bool((initial_snapshot["time"] == 0).all()) and len(trace["time"]) == 4500,
|
||||
}
|
||||
expected = torch.tensor([[e["scenario"] == int(sc) for e in s.cc["events"]] for sc in sample["scenario"]])
|
||||
event_completed = torch.equal(s.schedule.completed.cpu(), expected)
|
||||
integral = torch.tensor([e["duration_s"]-e["ramp_s"] for e in s.cc["events"]])
|
||||
delivered = bool(((s.schedule.integral.cpu() - expected*integral).abs() <= 0.002).all())
|
||||
# Selective reset must not resample another environment, its RNG counters or backend load.
|
||||
old_commands, old_points = s.schedule.commands.clone(), s.sampled_points.clone()
|
||||
old_counts = sampler.episodes.clone()
|
||||
composer = s.robot.permanent_wrench_composer
|
||||
old_force = composer.out_force_b.torch.clone()
|
||||
buffer_names = ("global_force_w", "global_force_at_com_w", "global_torque_w", "local_force_b", "local_torque_b")
|
||||
before_buffers = {name: wp.to_torch(getattr(composer, name)).clone() for name in buffer_names}
|
||||
ids = torch.tensor([1, 3], dtype=torch.int32, device=env.device)
|
||||
others = torch.tensor([i for i in range(env.num_envs) if i not in (1, 3)], device=env.device)
|
||||
env.reset(env_ids=ids)
|
||||
reset_checks = {
|
||||
"other_commands_unchanged": torch.equal(old_commands[others], s.schedule.commands[others]),
|
||||
"other_points_unchanged": torch.equal(old_points[others], s.sampled_points[others]),
|
||||
"other_counters_unchanged": torch.equal(old_counts[others.cpu()], sampler.episodes[others.cpu()]),
|
||||
"selected_counters_advance_once": bool((sampler.episodes[ids.cpu()] == old_counts[ids.cpu()] + 1).all()),
|
||||
"selected_points_resampled": not torch.equal(old_points[ids], s.sampled_points[ids]),
|
||||
}
|
||||
for name in buffer_names:
|
||||
after = wp.to_torch(getattr(composer, name))
|
||||
reset_checks[f"{name}_isolated"] = torch.equal(before_buffers[name][others], after[others])
|
||||
reset_checks[f"{name}_selected_zero"] = bool((after[ids] == 0).all())
|
||||
# Do not compare stale cached body-frame output to recomposition at the newest body pose.
|
||||
# The world/local INPUT buffers are the persistent applied-command source of truth.
|
||||
cache_difference = float((old_force[others] - composer.out_force_b.torch[others]).abs().max())
|
||||
isolated = all(reset_checks.values())
|
||||
# Zero normalized actions must not inherit a 3mm airborne reset and fail the 10ms monitor.
|
||||
s.set_sampler(None)
|
||||
env.reset()
|
||||
zero_failed = False
|
||||
with CycleRecorder(env) as zero_recorder:
|
||||
for _ in range(25):
|
||||
action = torch.zeros(env.num_envs, 3, device=env.device)
|
||||
zero_recorder.before_control(None, action)
|
||||
_, _, terminated, truncated, _ = env.step(action)
|
||||
zero_failed |= bool(terminated.any() or truncated.any())
|
||||
initialization_checks["zero_action_startup"] = not zero_failed and bool(s.acquisition.acquired.all())
|
||||
torch.save({k: torch.stack([f[k] for f in zero_recorder.frames]) for k in zero_recorder.frames[0]},
|
||||
output/"zero_action_startup.pt")
|
||||
# Invalid physical reset probe: lift only env0's root 30mm, with a separately declared 20ms deadline.
|
||||
# This shortened diagnostic is not a production-policy episode or a relaxed acceptance gate.
|
||||
from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES
|
||||
|
||||
env.reset()
|
||||
pose = s.robot.data.root_state_w.torch[:1, :7].clone()
|
||||
pose[:, 2] += .03
|
||||
s.robot.write_root_pose_to_sim(pose, env_ids=torch.tensor([0], device=env.device))
|
||||
original_deadline = s.acquisition.deadline
|
||||
s.acquisition.deadline = .02
|
||||
try:
|
||||
with CycleRecorder(env) as invalid_recorder:
|
||||
for _ in range(2):
|
||||
action = torch.zeros(env.num_envs, 3, device=env.device)
|
||||
invalid_recorder.before_control(None, action)
|
||||
_, _, terminated, _, info = env.step(action)
|
||||
flags = info["cycle_step"]["failure_flags"][0]
|
||||
initialization_checks["invalid_start_times_out"] = bool(
|
||||
terminated[0] and flags[FAILURE_NAMES.index("contact_acquisition_timeout")])
|
||||
initialization_checks["invalid_start_not_fake_departure"] = not bool(
|
||||
flags[FAILURE_NAMES.index("premature_separation")])
|
||||
initialization_checks["autoreset_clears_acquisition"] = not bool(s.acquisition.failed[0]) and s.time[0] == 0
|
||||
torch.save({k: torch.stack([f[k] for f in invalid_recorder.frames]) for k in invalid_recorder.frames[0]},
|
||||
output/"invalid_start_negative.pt")
|
||||
finally:
|
||||
s.acquisition.deadline = original_deadline
|
||||
initialization_checks = {k: bool(v) for k, v in initialization_checks.items()}
|
||||
# Separate physical negative control: lift env0 early while unloading.
|
||||
# Keep actual termination enabled: capture before autoreset, then require that reset clears only env0.
|
||||
s.set_sampler(None)
|
||||
env.reset()
|
||||
s.scenarios[:] = 2
|
||||
q_ik = nominal.clone()
|
||||
with CycleRecorder(env) as early_recorder:
|
||||
for k in range(round(4.3/env.step_dt)):
|
||||
action = analytic_action(early_env=0)
|
||||
early_recorder.before_control(None, action)
|
||||
_, _, terminated, truncated, _ = env.step(action)
|
||||
early_recorder.end_control(terminated, truncated, False)
|
||||
if terminated.any() or truncated.any():
|
||||
break
|
||||
early_trace = {key: torch.stack([f[key] for f in early_recorder.frames]) for key in early_recorder.frames[0]}
|
||||
early_checks = {
|
||||
"premature_detected_at_physics_rate": bool(early_trace["premature_separation"][:, 0].any()),
|
||||
"actual_termination": bool(terminated[0]) and not bool(terminated[1:].any() or truncated.any()),
|
||||
"no_synthetic_delayed_pulse": not bool(early_trace["event_fired"][:, 0].any()),
|
||||
"other_envs_not_premature": not bool(early_trace["premature_separation"][:, 1:].any()),
|
||||
"autoreset_clears_failure": not bool(s.separation.premature[0]) and s.time[0] == 0,
|
||||
"other_episode_clocks_not_reset": bool((s.time[1:] > 3.8).all()),
|
||||
"terminal_snapshot": (env.extras.get("terminal_ids") is not None
|
||||
and env.extras["terminal_ids"].cpu().tolist() == [0]),
|
||||
}
|
||||
early_checks = {key: bool(value) for key, value in early_checks.items()}
|
||||
early_time = float(early_trace["time"][-1, 0])
|
||||
torch.save({key: value[:, :2].clone() for key, value in early_trace.items()}, output/"early_lift_negative_trace.pt")
|
||||
# API test only: force a short TIMEOUT to check terminal snapshots/32D GAE inputs.
|
||||
# This shortened episode is NOT included in task metrics or counted as skill acceptance.
|
||||
s.set_sampler(None)
|
||||
env.cfg.episode_length_s = 2*env.step_dt
|
||||
x = features(env.reset()[0]["policy"])
|
||||
torch.manual_seed(90212)
|
||||
model = CycleActorCritic(hidden=16).to(env.device)
|
||||
before = {k: v.clone() for k, v in model.state_dict().items()}
|
||||
# Isolate timeout/bootstrap API from random untrained early-lift failures. ONLY this 0.02s API fixture
|
||||
# disables the failed term; the full 9s positive run and early-lift negative above use the real term.
|
||||
from isaaclab.managers import TerminationTermCfg
|
||||
|
||||
original_failed = env.termination_manager.get_term_cfg("failed")
|
||||
env.termination_manager.set_term_cfg("failed", TerminationTermCfg(
|
||||
func=lambda e: torch.zeros(e.num_envs, dtype=torch.bool, device=e.device)))
|
||||
try:
|
||||
data, _, failures, timeouts = collect_rollout(env, model, x, {"horizon": 4, "failure_penalty": 10.})
|
||||
finally:
|
||||
env.termination_manager.set_term_cfg("failed", original_failed)
|
||||
canonical_terminal = env.extras["terminal_ids"].dtype == torch.int64
|
||||
no_learning = all(torch.equal(before[k], v) for k, v in model.state_dict().items())
|
||||
x = features(env.reset()[0]["policy"])
|
||||
with CycleRecorder(env) as actor_recorder:
|
||||
with torch.no_grad():
|
||||
action, mean, _, _ = model.act(x, deterministic=True)
|
||||
actor_recorder.before_control(x, action, mean, model.log_std.exp())
|
||||
env.step(action)
|
||||
actor_frame = actor_recorder.frames[0]
|
||||
actor_logging = (bool(actor_frame["actor_metadata_available"].all())
|
||||
and torch.equal(actor_frame["actor_input"], x.cpu())
|
||||
and torch.equal(actor_frame["requested_action"], action.cpu())
|
||||
and torch.equal(actor_frame["actor_mean"], mean.cpu()))
|
||||
torch.save({"initial": actor_recorder.initial_state, "first_tick": actor_frame}, output/"actor_diagnostic_api.pt")
|
||||
gates = {"domain_audit": True, "scenario_coverage": set(sample["scenario"].tolist()) == {0, 1, 2, 3},
|
||||
"full_sampled_duration": len(trace["time"]) == round(9/env.physics_dt),
|
||||
"all_sampled_analytic_metrics": all(r["success"] for r in rows),
|
||||
"event_delivery": event_completed and delivered, "indexed_sampler_reset": isolated,
|
||||
"initialization_contract": all(initialization_checks.values()),
|
||||
"early_lift_negative_control": all(early_checks.values()),
|
||||
"timeout_rollout": failures == 0 and timeouts == 2*env.num_envs and bool(data["done"].any()),
|
||||
"canonical_terminal_ids": canonical_terminal,
|
||||
"actor_diagnostic_logging": actor_logging,
|
||||
"terminal_metadata_not_stale": (env.extras["terminal_ids"] is None
|
||||
and env.extras["terminal_failure_flags"] is None),
|
||||
"analytic_actions_logged": (bool(trace["control_action_available"].all())
|
||||
and not bool(trace["actor_metadata_available"].any())),
|
||||
"rollout_diagnostic_coverage": bool((data["phase"] >= 0).all()),
|
||||
"finite_rollout": all(torch.isfinite(v).all().item() for v in data.values()),
|
||||
"no_parameter_update": no_learning}
|
||||
torch.save(data, output/"untrained_timeout_api_rollout.pt")
|
||||
return {"passed": all(gates.values()), "gates": gates, "rows": rows, "accepted_skill": False,
|
||||
"trained_skill": False, "optimizer_steps": 0, "independent_test_executed": False,
|
||||
"sampled_development_seed": 90211, "timeout_api_test_episode_s": env.cfg.episode_length_s,
|
||||
"timeout_api_failed_term_disabled": True,
|
||||
"timeout_api_failures": failures, "timeout_api_timeouts": timeouts,
|
||||
"reset_checks": reset_checks, "cached_vs_recomposed_body_force_difference_N": cache_difference,
|
||||
"initialization_checks": initialization_checks,
|
||||
"invalid_start_probe": {"root_raise_m": .03, "deadline_s": .02, "production_deadline_s": original_deadline},
|
||||
"early_lift_checks": early_checks, "early_lift_probe_end_s": early_time,
|
||||
"early_lift_max_unload_gap_m": float(early_trace["gap"][:, 0][early_trace["phase"][:, 0] == 1].max()),
|
||||
"note": "Analytic development, early-lift negative control and short timeout API. Not skill acceptance."}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
AppLauncher.add_app_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
files = list((root/"source/SkillFactory").rglob("*.py")) + [Path(__file__)]
|
||||
files += list((root/"configs/contact_cycle").glob("*.json"))
|
||||
identity = {str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in files}
|
||||
write_json(args.output/"identity.json", identity)
|
||||
launcher = AppLauncher(args)
|
||||
env = None
|
||||
result = {"passed": False, "accepted_skill": False, "trained_skill": False}
|
||||
code = 1
|
||||
try:
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.env import ContactCycleEnv
|
||||
|
||||
cfg = ContactCycleEnvCfg()
|
||||
cfg.scene.num_envs = 16
|
||||
cfg.sim.device = args.device
|
||||
cfg.seed = 90211
|
||||
cfg.episode_length_s = sum(cfg.cycle_parameters["phase_durations_s"]) + 1
|
||||
env = ContactCycleEnv(cfg=cfg)
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
|
||||
thresholds = json.loads((root/"configs/contact_cycle/acceptance_v1.json").read_text())["gates"]
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
result = run(env, args.output)
|
||||
safety.check()
|
||||
result["physics_safety"] = safety.report()
|
||||
if any(hashlib.sha256(Path(p).read_bytes()).hexdigest() != h for p, h in identity.items()):
|
||||
raise RuntimeError("Source changed during runtime test")
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
result["error"] = traceback.format_exc()
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
finish_run(launcher.app, env, args.output/"result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Nonoptimizing 46D learner API probe: 8 envs, two 32-control short fixtures.
|
||||
|
||||
No optimizer, grant, training ledger, checkpoint load or functional-skill claim.
|
||||
Production failure term and 200ms acquisition deadline remain enabled.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import signal
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def model_hash(model):
|
||||
h = hashlib.sha256()
|
||||
for name, value in sorted(model.state_dict().items()):
|
||||
h.update(name.encode())
|
||||
h.update(value.detach().cpu().contiguous().numpy().tobytes())
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
def run(env, output, guard):
|
||||
import torch
|
||||
from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES
|
||||
from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features
|
||||
from SkillFactory.legs.contact_cycle_sim.ppo import bootstrap_values, collect_rollout, rollout_diagnostics
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
||||
|
||||
torch.set_num_threads(4)
|
||||
torch.manual_seed(91502)
|
||||
model = SimResidualActorCritic().to(env.device)
|
||||
before = model_hash(model)
|
||||
s = state(env)
|
||||
if (s.robot.is_fixed_base or env.cfg.terminations.failed is None or s.acquisition.deadline != .2
|
||||
or env.num_envs != 8 or env.cfg.episode_length_s != .3):
|
||||
raise RuntimeError("Require declared bounded fixture with original active failure/deadline contract")
|
||||
gates = {}
|
||||
fixtures = {}
|
||||
total_controls = 0
|
||||
for name in ("timeout", "true_failure"):
|
||||
x = features(env.reset()[0]["policy"])
|
||||
if name == "true_failure":
|
||||
pose = s.robot.data.root_state_w.torch[:1, :7].clone()
|
||||
pose[:, 2] += .5
|
||||
s.robot.write_root_pose_to_sim(pose, env_ids=torch.tensor([0], device=env.device))
|
||||
x = features(env.observation_manager.compute(update_history=False)["policy"])
|
||||
events, expected_values, terminals = [], [], []
|
||||
current = x.clone()
|
||||
with SimCycleRecorder(env) as recorder:
|
||||
class ObservedEnv:
|
||||
num_envs = env.num_envs
|
||||
device = env.device
|
||||
step_dt = env.step_dt
|
||||
|
||||
def step(self, action):
|
||||
nonlocal current, total_controls
|
||||
with torch.no_grad():
|
||||
dist, _ = model.distribution_value(current)
|
||||
recorder.before_control(current, action, dist.mean, dist.stddev)
|
||||
obs, reward, failed, timeout, info = env.step(action)
|
||||
total_controls += 1
|
||||
recorder.end_control(failed, timeout, False)
|
||||
next_x = features(obs["policy"])
|
||||
values = bootstrap_values(model, next_x, failed, timeout, info)
|
||||
expected_values.append(values.detach().clone())
|
||||
event = {"control": total_controls, "failed": failed.cpu().tolist(),
|
||||
"timeout": timeout.cpu().tolist(),
|
||||
"failure_flags": info["cycle_step"]["failure_flags"].cpu().tolist(),
|
||||
"times_after_reset_s": s.time.cpu().tolist()}
|
||||
if (failed | timeout).any():
|
||||
ids = info["terminal_ids"]
|
||||
terminal = features(info["terminal_observation"]["policy"])
|
||||
pre = recorder.frames[-1]["policy_snapshot"][ids.cpu()]
|
||||
event["terminal_ids"] = ids.cpu().tolist()
|
||||
event["terminal46"] = terminal.shape == (len(ids), 46)
|
||||
event["terminal_matches_prereset"] = bool(torch.allclose(
|
||||
terminal.cpu(), pre, atol=1e-6, rtol=0))
|
||||
event["terminal_differs_from_reset"] = bool((terminal != next_x[ids]).any(-1).all())
|
||||
event["failure_bootstrap_zero"] = bool((values[failed] == 0).all())
|
||||
timeout_only = timeout & ~failed
|
||||
selected = timeout_only[ids]
|
||||
with torch.no_grad():
|
||||
target = model.value(terminal[selected]) if selected.any() else values[:0]
|
||||
event["timeout_bootstrap_matches"] = bool(torch.allclose(
|
||||
values[ids[selected]], target, atol=1e-6, rtol=0))
|
||||
terminals.append({"ids": ids.cpu().clone(), "pre_reset": terminal.cpu().clone(),
|
||||
"post_reset": next_x[ids].cpu().clone(),
|
||||
"bootstrap": values[ids].cpu().clone()})
|
||||
events.append(event)
|
||||
current = next_x.clone()
|
||||
guard()
|
||||
return obs, reward, failed, timeout, info
|
||||
|
||||
data, _, failures, timeouts = collect_rollout(
|
||||
ObservedEnv(), model, x, {"horizon": 32, "failure_penalty": 10.}, guard)
|
||||
trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]}
|
||||
actual = trace["processed_target"] - s.nominal.cpu() - .5 * trace["reference_used_normalized"]
|
||||
with torch.no_grad():
|
||||
logp, _ = model.evaluate(data["features"].flatten(0, 1), data["latent"].flatten(0, 1))
|
||||
logp_error = float((logp.reshape_as(data["log_prob"]) - data["log_prob"]).abs().max())
|
||||
terminal_events = [e for e in events if "terminal_ids" in e]
|
||||
checks = {
|
||||
"finite_rollout": all(bool(torch.isfinite(v).all()) for v in data.values()),
|
||||
"features46": data["features"].shape == (32, 8, 46),
|
||||
"phase_cause_finite": data["failure_flags"].shape == (32, 8, 8)
|
||||
and bool(((data["phase"] >= 0) & (data["phase"] <= 5)).all()),
|
||||
"logprob_recompute": logp_error < 2e-4,
|
||||
"bootstrap_matches": bool(torch.allclose(
|
||||
data["next_value"], torch.stack(expected_values), atol=1e-6, rtol=0)),
|
||||
"terminal_events_present": bool(terminal_events),
|
||||
"terminal_contract": all(all(e[k] for k in (
|
||||
"terminal46", "terminal_matches_prereset", "terminal_differs_from_reset",
|
||||
"failure_bootstrap_zero", "timeout_bootstrap_matches")) for e in terminal_events),
|
||||
"actual_delta_matches": bool(torch.allclose(actual, trace["effective_residual_rad"], atol=1e-6, rtol=0)),
|
||||
"actual_delta_bound": float(actual.abs().max()) <= .050001,
|
||||
"template_bound": bool(((trace["processed_target"] - s.nominal.cpu()).abs() <= .500001).all()),
|
||||
"actor_metadata46": trace["actor_input"].shape[-1] == 46 and bool(trace["actor_metadata_available"].all()),
|
||||
"failure_term_still_enabled": env.cfg.terminations.failed is not None and s.acquisition.deadline == .2,
|
||||
}
|
||||
if name == "timeout":
|
||||
checks["genuine_timeouts_only"] = failures == 0 and timeouts == 8
|
||||
else:
|
||||
failure_events = [e for e in events if any(e["failed"])]
|
||||
checks["genuine_env0_failure"] = (failures == 1 and len(failure_events) == 1
|
||||
and failure_events[0]["failed"][0])
|
||||
checks["other_envs_unaffected"] = not any(any(e["failed"][1:]) for e in events)
|
||||
checks["production_acquisition_failure"] = bool(failure_events) and failure_events[0]["failure_flags"][0][
|
||||
FAILURE_NAMES.index("contact_acquisition_timeout")]
|
||||
checks["other_clocks_not_reset_on_failure"] = bool(failure_events) and all(
|
||||
t >= .19 for t in failure_events[0]["times_after_reset_s"][1:])
|
||||
checks["failure_row_zero_bootstrap"] = bool((data["next_value"][data["failure_flags"].any(-1)] == 0).all())
|
||||
gates[name] = all(checks.values())
|
||||
diagnostics = rollout_diagnostics(data, {"gamma": .99, "gae_lambda": .95})
|
||||
torch.save({"data": {k: v.cpu().clone() for k, v in data.items()}, "terminal_events": terminals},
|
||||
output / f"{name}_api.pt")
|
||||
# Entire fixture is bounded to 160 physics frames x8, not a complete-cycle claim.
|
||||
torch.save(trace, output / f"{name}_trace.pt")
|
||||
torch.save(recorder.initial_state, output / f"{name}_initial.pt")
|
||||
fixtures[name] = {"checks": checks, "failures": failures, "timeouts": timeouts,
|
||||
"logprob_max_abs_error": logp_error, "max_actual_residual_rad": float(actual.abs().max()),
|
||||
"events": events, "diagnostics": diagnostics}
|
||||
after = model_hash(model)
|
||||
gates["parameters_unchanged"] = before == after
|
||||
return {"passed": all(gates.values()), "gates": gates, "fixtures": fixtures,
|
||||
"model_sha256_before": before, "model_sha256_after": after,
|
||||
"model_seed": 91502, "environment_seed": 93502,
|
||||
"optimizer_steps": 0, "training_transitions": 0, "nonoptimizing_api_transitions": total_controls * 8,
|
||||
"physics_steps_per_env": total_controls * env.cfg.decimation,
|
||||
"aggregate_physics_transitions": total_controls * env.cfg.decimation * 8,
|
||||
"fixture_episode_s": .3, "failure_root_raise_m": .5, "acquisition_deadline_s": .2,
|
||||
"failure_term_disabled": False, "accepted_skill": False, "trained_residual_policy": False,
|
||||
"functional_claim": False}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
parser.add_argument("--headless", action="store_true", required=True)
|
||||
args = parser.parse_args()
|
||||
from SkillFactory.legs.contact_cycle_sim.training import (
|
||||
CONFIG_PATH,
|
||||
assert_no_conflicting_processes,
|
||||
make_manifest,
|
||||
writable_output,
|
||||
)
|
||||
|
||||
args.output = writable_output(ROOT, args.output)
|
||||
assert_no_conflicting_processes()
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
manifest = make_manifest(ROOT, ROOT / CONFIG_PATH)
|
||||
write_json(args.output / "identity.json", manifest)
|
||||
stop = {"requested": False}
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
signal.signal(sig, lambda *_: stop.update(requested=True))
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
app = AppLauncher(headless=True, device=args.device).app
|
||||
env = safety = None
|
||||
result = {"passed": False, "accepted_skill": False, "optimizer_steps": 0, "training_transitions": 0}
|
||||
code = 1
|
||||
try:
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv
|
||||
|
||||
cfg = SimContactCycleEnvCfg()
|
||||
cfg.scene.num_envs = 8
|
||||
cfg.sim.device = args.device
|
||||
cfg.seed = 93502
|
||||
cfg.episode_length_s = .3 # API only; production acquisition deadline and failure term UNCHANGED
|
||||
env = SimContactCycleEnv(cfg=cfg)
|
||||
thresholds = json.loads((ROOT / "configs/contact_cycle/acceptance_v1.json").read_text())["gates"]
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
def guard():
|
||||
if stop["requested"]:
|
||||
raise InterruptedError("Probe interrupted")
|
||||
safety.check()
|
||||
|
||||
result = run(env, args.output, guard)
|
||||
guard()
|
||||
result["physics_safety"] = safety.report()
|
||||
if manifest != make_manifest(ROOT, ROOT / CONFIG_PATH):
|
||||
raise RuntimeError("Identity changed during API probe")
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
result.update(passed=False, error=traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
if safety is not None:
|
||||
result["physics_safety"] = safety.report()
|
||||
finish_run(app, env, args.output / "result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,196 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Supervised non-learning physics verification of the simulation reference/residual task."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def run(env, output):
|
||||
import torch
|
||||
from audit_contact_cycle_functional import evaluate
|
||||
from SkillFactory.legs.contact_cycle_sim.config import FEATURE_VERSION, pose_cost
|
||||
from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
||||
|
||||
s = state(env)
|
||||
if s.robot.is_fixed_base or env.cfg.terminations.failed is None:
|
||||
raise RuntimeError('Require floating root and active physical failure termination')
|
||||
x = features(env.reset()[0]['policy'])
|
||||
zero = torch.zeros(env.num_envs, 3, device=env.device)
|
||||
gates = {'policy46': x.shape == (env.num_envs, 46), 'nominal_only': not bool(s.scenarios.any())}
|
||||
try:
|
||||
s.set_sampler(object())
|
||||
except ValueError:
|
||||
gates['old_sampler_rejected'] = True
|
||||
else:
|
||||
gates['old_sampler_rejected'] = False
|
||||
rewards = []
|
||||
with SimCycleRecorder(env) as recorder:
|
||||
initial = recorder.initial_state
|
||||
for k in range(900):
|
||||
recorder.before_control(None, zero)
|
||||
observation, reward, terminated, truncated, _ = env.step(zero)
|
||||
recorder.end_control(terminated, truncated, k == 899)
|
||||
if terminated.any() or (k != 899 and truncated.any()):
|
||||
raise RuntimeError(f'Zero-residual reference failed at control step{k}; no repaired autoresets')
|
||||
features(observation['policy'])
|
||||
rewards.append(reward.detach().cpu())
|
||||
trace = {key: torch.stack([frame[key] for frame in recorder.frames]) for key in recorder.frames[0]}
|
||||
torch.save(trace, output / 'zero_residual_trace.pt')
|
||||
torch.save(initial, output / 'zero_residual_initial.pt')
|
||||
torch.save(torch.stack(rewards), output / 'control_rewards.pt')
|
||||
functional = json.loads((ROOT / 'configs/contact_cycle/simulation_functional_v1.json').read_text())
|
||||
rows = [evaluate({k: v[:, e:e+1] for k, v in trace.items()}, functional, 'analytic_reference')
|
||||
for e in range(env.num_envs)]
|
||||
gates['full_functional_cycle'] = all(row['sim_functional_passed'] for row in rows)
|
||||
gates['zero_means_reference'] = bool((trace['effective_residual_rad'] == 0).all())
|
||||
gates['no_hidden_disturbance'] = not bool(trace['external'].any())
|
||||
# The complete9s trajectory was captured before the normal timeout reset, not after.
|
||||
gates['terminal46_at_timeout'] = env.extras['terminal_observation']['policy'].shape == (env.num_envs, 46)
|
||||
gates['all_timeouts_no_failures'] = bool(truncated.all()) and not bool(terminated.any())
|
||||
|
||||
env.reset()
|
||||
stored = s.controller.q_ik.clone(), s.controller.refreshes.clone(), s.controller.action.clone()
|
||||
for _ in range(5):
|
||||
env.observation_manager.compute(update_history=False)
|
||||
gates['observation_reads_do_not_advance_reference'] = all(
|
||||
torch.equal(a, b) for a, b in zip(stored, [s.controller.q_ik, s.controller.refreshes, s.controller.action]))
|
||||
before = s.substeps.clone()
|
||||
try:
|
||||
env.step(torch.full_like(zero, float('nan')))
|
||||
except ValueError:
|
||||
gates['invalid_action_no_physics'] = torch.equal(before, s.substeps)
|
||||
else:
|
||||
gates['invalid_action_no_physics'] = False
|
||||
|
||||
gates['outer_rejection_history_finite'] = bool(torch.isfinite(env.action_manager.action).all()
|
||||
and torch.isfinite(env.action_manager.prev_action).all())
|
||||
# Low-level manager entry bypasses the outer env.step input guard and stores input before terms.
|
||||
try:
|
||||
env.action_manager.process_action(torch.full_like(zero, float('nan')))
|
||||
except ValueError:
|
||||
gates['manager_rejection_rolls_back'] = bool(torch.isfinite(env.action_manager.action).all()
|
||||
and torch.equal(env.action_manager.action,
|
||||
env.action_manager.get_term('joint_position').raw_actions))
|
||||
else:
|
||||
gates['manager_rejection_rolls_back'] = False
|
||||
gates['reward_after_rejected_input_finite'] = True
|
||||
# Deliberately beyond normalized actor bounds; projection must still keep <=.05rad residual.
|
||||
request = torch.tensor([100., -100., 100.], device=env.device).expand_as(zero)
|
||||
with SimCycleRecorder(env) as boundary:
|
||||
boundary_terminated = 0
|
||||
for _ in range(20):
|
||||
boundary.before_control(None, request)
|
||||
_, reward, terminated, _, _ = env.step(request)
|
||||
gates['reward_after_rejected_input_finite'] &= bool(torch.isfinite(reward).all()
|
||||
and torch.isfinite(env.action_manager.prev_action).all())
|
||||
boundary_terminated += int(terminated.sum())
|
||||
bound_trace = {k: torch.stack([f[k] for f in boundary.frames]) for k in boundary.frames[0]}
|
||||
torch.save(bound_trace, output / 'residual_boundary_trace.pt')
|
||||
actual_delta = (bound_trace['processed_target'] - s.nominal.cpu()
|
||||
- .5 * bound_trace['reference_used_normalized'])
|
||||
maximum = float(actual_delta.abs().max())
|
||||
gates['actual_target_residual_bound'] = maximum <= s.residual_cfg['residual_limit_rad'] + 1e-6
|
||||
gates['delta_log_matches_applied_target'] = bool(torch.allclose(
|
||||
actual_delta, bound_trace['effective_residual_rad'], rtol=0, atol=1e-6))
|
||||
|
||||
others = torch.arange(1, env.num_envs, device=env.device)
|
||||
buffers = [s.controller.q_ik, s.controller.action, s.controller.refreshes, s.residual_request,
|
||||
s.reference_used, s.effective_residual_rad, s.substeps]
|
||||
copies = [b[others].clone() for b in buffers]
|
||||
env.reset(env_ids=torch.tensor([0], device=env.device, dtype=torch.int32))
|
||||
gates['indexed_reset_isolated'] = all(torch.equal(old, b[others]) for old, b in zip(copies, buffers))
|
||||
gates['indexed_residual_cleared'] = not bool(s.residual_request[0].any() or s.reference_used[0].any()
|
||||
or s.effective_residual_rad[0].any())
|
||||
|
||||
# A fresh untrained46D network checks the interface; no old checkpoint/optimizer is used.
|
||||
model = SimResidualActorCritic().to(env.device)
|
||||
x = features(env.reset()[0]['policy'])
|
||||
with torch.no_grad():
|
||||
mean_action = model.act(x, deterministic=True)[0]
|
||||
gates['untrained_actor_is_zero_residual'] = not bool(mean_action.any())
|
||||
gates['sim_truth_features_match'] = bool(torch.allclose(x[:, 32:35], s.errors()[0])
|
||||
and torch.allclose(x[:, 35:38], s.errors()[1])
|
||||
and torch.allclose(x[:, 38], s.contact()[:, 2]))
|
||||
for _ in range(2):
|
||||
env.step(zero)
|
||||
gates['reference_control_clock'] = bool((s.controller.refreshes == 3).all()) # reset + two controls
|
||||
c = s.residual_cfg['reward']
|
||||
example_cost = pose_cost(torch.tensor([[0., 0., -.0996]]), torch.zeros(1, 3), c)[0].item()
|
||||
gates['sinking_not_high_reward_counterexample'] = .9255 - c['position_weight'] * example_cost < 0
|
||||
from SkillFactory.tasks.manager_based.contact_cycle import mdp as old_mdp
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.loaded_leg.cfg import LoadedLegEnvCfg
|
||||
|
||||
old, standing = ContactCycleEnvCfg(), LoadedLegEnvCfg()
|
||||
gates['historical_configs_unmodified'] = (
|
||||
old.actions.joint_position.class_type is old_mdp.CycleAction
|
||||
and not hasattr(old.rewards, 'root_position') and old.scene is not env.cfg.scene
|
||||
and standing.scene.robot.init_state.pos[2]
|
||||
== standing.anchor_height + standing.support_parameters['initial_foot_clearance_m'])
|
||||
return {'passed': all(gates.values()), 'gates': gates, 'rows': rows, 'accepted_skill': False,
|
||||
'sim_functional_passed': gates['full_functional_cycle'], 'controller_kind': 'analytic_reference',
|
||||
'trained_residual_policy': False, 'optimizer_steps': 0, 'feature_version': FEATURE_VERSION,
|
||||
'max_actual_residual_rad': maximum, 'boundary_terminated': boundary_terminated,
|
||||
'zero_residual_cycle_s': 9, 'untrained_network_api_only': True,
|
||||
'task_reward_minus_height_cost_at_minus99_6mm': .9255 - c['position_weight'] * example_cost,
|
||||
'note': 'Nominal reference/residual interface verification, not evidence of learned-policy success.'}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
AppLauncher.add_app_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
frozen = (ROOT / 'releases').resolve()
|
||||
if args.output.resolve() == frozen or frozen in args.output.resolve().parents:
|
||||
parser.error('Verification must not write into frozen releases')
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
from SkillFactory.legs.contact_cycle.training import load_training_config, source_identity
|
||||
|
||||
old_cfg = ROOT / 'configs/contact_cycle/training_v1.json'
|
||||
identity = source_identity(ROOT, load_training_config(old_cfg, ROOT), old_cfg)
|
||||
extras = [Path(__file__), ROOT / 'scripts/audit_contact_cycle_functional.py',
|
||||
*list((ROOT / 'configs/contact_cycle').glob('*.json'))]
|
||||
identity.update({str(p.resolve().relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in extras})
|
||||
write_json(args.output / 'identity.json', identity)
|
||||
app = AppLauncher(args).app
|
||||
env = None
|
||||
result = {'passed': False, 'accepted_skill': False, 'optimizer_steps': 0}
|
||||
code = 1
|
||||
try:
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv
|
||||
|
||||
cfg = SimContactCycleEnvCfg()
|
||||
cfg.scene.num_envs = 8
|
||||
cfg.sim.device = args.device
|
||||
cfg.seed = 90311
|
||||
env = SimContactCycleEnv(cfg=cfg)
|
||||
thresholds = json.loads((ROOT / 'configs/contact_cycle/acceptance_v1.json').read_text())['gates']
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
result = run(env, args.output)
|
||||
safety.check()
|
||||
result['physics_safety'] = safety.report()
|
||||
if any(hashlib.sha256((ROOT / f).read_bytes()).hexdigest() != h for f, h in identity.items()):
|
||||
raise RuntimeError('Source changed during test')
|
||||
code = 0 if result['passed'] else 2
|
||||
except BaseException:
|
||||
result['error'] = traceback.format_exc()
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
finish_run(app, env, args.output / 'result.json', result, code)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,343 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One supervised, bounded, non-learning nominal residual matrix. No model construction."""
|
||||
import argparse
|
||||
import fcntl
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
WINDOW_ROOT = ROOT / 'logs/contact_cycle/autonomous_sim_resume_v1'
|
||||
WINDOW = WINDOW_ROOT / 'window.json'
|
||||
WINDOW_SHA256 = '511d3f98a2c9ec7b78b1cb7f8f72e28ef06de6dcf96ecf2493ac7399d1eefbc8'
|
||||
OUTPUT_ROOT = WINDOW_ROOT / 'phase01_nominal_residual'
|
||||
PLAN = ROOT / 'configs/contact_cycle/simulation_stress_v1.json'
|
||||
|
||||
|
||||
def sha256(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
class WindowGuard:
|
||||
"""Same-boot absolute deadline; wall UTC never authorizes a launch or control."""
|
||||
|
||||
def __init__(self):
|
||||
if sha256(WINDOW) != WINDOW_SHA256:
|
||||
raise RuntimeError('Window contract hash changed')
|
||||
self.window = json.loads(WINDOW.read_text())
|
||||
self.last_disk_check = -float('inf')
|
||||
self.bytes = 0
|
||||
|
||||
def check(self, reserve=0, force_disk=False):
|
||||
now = time.clock_gettime(time.CLOCK_BOOTTIME)
|
||||
boot = Path('/proc/sys/kernel/random/boot_id').read_text().strip()
|
||||
remaining = self.window['deadline_boottime'] - now
|
||||
if boot != self.window['boot_id'] or remaining <= self.window['cleanup_margin_s']:
|
||||
raise RuntimeError('Boot/deadline guard tripped; no additional physics authorized')
|
||||
if force_disk or reserve or now - self.last_disk_check >= 1:
|
||||
self.bytes = sum(p.stat().st_size for p in WINDOW_ROOT.rglob('*') if p.is_file())
|
||||
self.last_disk_check = now
|
||||
# Reserve space for supervisor logs and final shutdown/error reports.
|
||||
if self.bytes + reserve >= self.window['max_new_artifact_bytes'] - 8 * 1024**2:
|
||||
raise RuntimeError('Artifact guard tripped')
|
||||
return remaining
|
||||
|
||||
|
||||
def verify_hashes(identity):
|
||||
changed = [name for name, digest in identity.items() if sha256(ROOT / name) != digest]
|
||||
if changed:
|
||||
raise RuntimeError(f'Protected/registered inputs changed: {changed}')
|
||||
|
||||
|
||||
def capture_identity():
|
||||
protected = json.loads((WINDOW_ROOT / 'protected_inputs_before.json').read_text())
|
||||
verify_hashes(protected)
|
||||
files = {ROOT / name for name in protected}
|
||||
files.update(p for p in (ROOT / 'source/SkillFactory').rglob('*')
|
||||
if p.is_file() and '__pycache__' not in p.parts and p.suffix != '.pyc')
|
||||
support = json.loads((ROOT / 'configs/standing/go2_fl_support_v0.json').read_text())
|
||||
for key in ('source_bundle', 'support_asset'):
|
||||
files.update(p for p in (ROOT / support[key]).rglob('*') if p.is_file())
|
||||
files.update([Path(__file__), PLAN, WINDOW, WINDOW_ROOT / 'protected_inputs_before.json',
|
||||
ROOT / 'scripts/runtime_common.py', ROOT / 'scripts/supervise_sim.py',
|
||||
ROOT / 'tests/test_contact_cycle_sim_stress.py', ROOT / 'docs/contact_cycle_sim_stress.md'])
|
||||
return {str(p.relative_to(ROOT)): sha256(p) for p in sorted(files)}
|
||||
|
||||
|
||||
def assert_no_kit():
|
||||
for p in Path('/proc').glob('[0-9]*/cmdline'):
|
||||
try:
|
||||
args = p.read_bytes().split(b'\0')
|
||||
except (FileNotFoundError, PermissionError, ProcessLookupError):
|
||||
continue
|
||||
# Match executable/launcher tokens, not incidental substrings such as polkit.
|
||||
if args and (Path(os.fsdecode(args[0])).name in ('kit', 'isaac-sim', 'isaac-sim.sh')
|
||||
or any(a.endswith(b'/isaaclab.sh') for a in args)):
|
||||
raise RuntimeError(f'Unrelated simulator process conflict: {p.parent.name}')
|
||||
|
||||
|
||||
def supervise(output):
|
||||
guard = WindowGuard()
|
||||
guard.check(force_disk=True)
|
||||
OUTPUT_ROOT.mkdir(parents=True, exist_ok=True)
|
||||
with (OUTPUT_ROOT / 'writer.lock').open('a') as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
assert_no_kit()
|
||||
output.mkdir(parents=True, exist_ok=False)
|
||||
identity = capture_identity()
|
||||
registration = {'window_sha256': WINDOW_SHA256, 'registered_boottime': time.clock_gettime(time.CLOCK_BOOTTIME),
|
||||
'plan_sha256': sha256(PLAN), 'plan': json.loads(PLAN.read_text()), 'identity': identity,
|
||||
'omissions': [], 'optimizer_steps': 0, 'policy_models_constructed': 0}
|
||||
write_json(output / 'preregistration.json', registration)
|
||||
remaining = guard.check(force_disk=True) # immediately before the sole Kit launch
|
||||
timeout = min(2400, int(remaining - guard.window['cleanup_margin_s'] - 10))
|
||||
if timeout <= 0:
|
||||
raise RuntimeError('No safe launch time remains')
|
||||
command = [sys.executable, str(ROOT / 'scripts/supervise_sim.py'),
|
||||
'--log', str(output / 'simulator.log'), '--report', str(output / 'supervisor.json'),
|
||||
'--timeout', str(timeout), '--', '/home/cen/IsaacLab/isaaclab.sh', '-p',
|
||||
str(Path(__file__).resolve()), '--output', str(output), '--headless']
|
||||
write_json(output / 'launch.json', {'command': command, 'remaining_s': remaining, 'timeout_s': timeout,
|
||||
'preregistration_sha256': sha256(output / 'preregistration.json')})
|
||||
environment = dict(os.environ, PYTHONPATH=str(ROOT / 'source/SkillFactory'), PYTHONUNBUFFERED='1')
|
||||
environment.pop('CONDA_PREFIX', None)
|
||||
environment.pop('VIRTUAL_ENV', None)
|
||||
completed = subprocess.run(command, env=environment, check=False)
|
||||
verify_hashes(identity)
|
||||
if sha256(output / 'preregistration.json') != json.loads((output / 'launch.json').read_text())[
|
||||
'preregistration_sha256']:
|
||||
raise RuntimeError('Preregistration changed during the run')
|
||||
write_json(output / 'integrity_after.json', {'unchanged': True, 'files_checked': len(identity),
|
||||
'remaining_s': guard.check(force_disk=True),
|
||||
'window_artifact_bytes': guard.bytes})
|
||||
return completed.returncode
|
||||
|
||||
|
||||
def claim_execution(output):
|
||||
"""Consume an attempt once, before Kit; never overwrite completed/partial evidence."""
|
||||
if (output / 'result.json').exists() or any(path.is_dir() for path in output.iterdir()):
|
||||
raise FileExistsError('Attempt already contains runtime evidence; use a fresh output')
|
||||
report = output / 'supervisor.json'
|
||||
if report.exists():
|
||||
try:
|
||||
supervisor = json.loads(report.read_text())
|
||||
except (ValueError, UnicodeError) as error:
|
||||
raise FileExistsError('Existing supervisor evidence is malformed; preserve attempt') from error
|
||||
# The unchanged supervisor writes precisely this initial report BEFORE spawning us.
|
||||
initial_running = (isinstance(supervisor, dict) and set(supervisor) == {'passed', 'status', 'command'}
|
||||
and supervisor['passed'] is False and supervisor['status'] == 'running'
|
||||
and isinstance(supervisor['command'], list) and bool(supervisor['command'])
|
||||
and all(isinstance(arg, str) for arg in supervisor['command']))
|
||||
if not initial_running:
|
||||
raise FileExistsError('Existing supervisor report is not a fresh running report')
|
||||
# O_EXCL also prevents simultaneous internal invocations or retries after startup failure.
|
||||
with (output / 'execution_started.json').open('x') as stream:
|
||||
json.dump({'pid': os.getpid(), 'started_boottime': time.clock_gettime(time.CLOCK_BOOTTIME)}, stream)
|
||||
|
||||
|
||||
def save_tensor(path, value, guard):
|
||||
import torch
|
||||
|
||||
stream = io.BytesIO()
|
||||
torch.save(value, stream)
|
||||
guard.check(reserve=stream.tell())
|
||||
with path.open('xb') as out:
|
||||
out.write(stream.getbuffer())
|
||||
|
||||
|
||||
def summarize_episode(episode, e, recorder, history, plan, state, functional):
|
||||
import torch
|
||||
from audit_contact_cycle_functional import evaluate
|
||||
from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES
|
||||
from SkillFactory.legs.contact_cycle_sim.stress import first_failure
|
||||
|
||||
row = evaluate(episode, functional, 'analytic_reference')
|
||||
row.update(env=e, controller_kind='analytic_reference_with_scripted_residual',
|
||||
first_episode_samples=len(episode['time']),
|
||||
phase_sample_counts={str(p): int((episode['phase'] == p).sum()) for p in range(6)},
|
||||
first_failure=first_failure(episode, FAILURE_NAMES),
|
||||
control_boundary_stop_s=float(episode['time'][-1, 0]) if recorder.stops[e] is not None else None,
|
||||
early_stop=bool(recorder.early[e]))
|
||||
stop = (recorder.stops[e] or len(recorder.frames)) // 5
|
||||
h = {k: v[:stop, e:e+1].clone().contiguous() for k, v in history.items()}
|
||||
row['terminated'] = bool(h['terminated'].any())
|
||||
row['truncated'] = bool(h['truncated'].any())
|
||||
row['termination_physics_tick'] = len(episode['time']) if row['terminated'] or row['truncated'] else None
|
||||
row['termination_causes'] = ([name for j, name in enumerate(FAILURE_NAMES)
|
||||
if episode['failure_flags'][-1, 0, j]] if row['terminated'] else
|
||||
['timeout'] if row['truncated'] else [])
|
||||
target = episode['processed_target']
|
||||
nominal = state.nominal.cpu()
|
||||
scale = state.env.cfg.support_parameters['action_scale_rad']
|
||||
delta = target - nominal - scale * episode['reference_used_normalized']
|
||||
row['max_actual_residual_rad'] = float(delta.abs().max())
|
||||
row['max_requested_residual_rad'] = float(episode['residual_request_normalized'].abs().max()) * .05
|
||||
row['max_absolute_target_offset_rad'] = float((target - nominal).abs().max())
|
||||
row['checks'] = {
|
||||
'actual_delta_bounded': row['max_actual_residual_rad'] <= plan['residual_limit_rad'] + 1e-6,
|
||||
'applied_target_original_bounds': row['max_absolute_target_offset_rad'] <= scale + 1e-6,
|
||||
'effective_delta_matches_target': bool(torch.allclose(delta, episode['effective_residual_rad'],
|
||||
rtol=0, atol=1e-6)),
|
||||
'finite_reward_action_history': all(bool(torch.isfinite(v).all()) for v in h.values()),
|
||||
'finite_physics_trace': all(bool(torch.isfinite(v).all()) for v in episode.values()),
|
||||
'nominal_no_external_disturbance': not bool(episode['scenario'].any() or episode['external'].any()),
|
||||
'no_actor_metadata': not bool(episode['actor_metadata_available'].any()),
|
||||
'termination_has_physics_cause': not row['terminated'] or row['first_failure'] is not None,
|
||||
}
|
||||
row['success'] = (row['sim_functional_passed'] and not row['early_stop']
|
||||
and not row['terminated'] and all(row['checks'].values()))
|
||||
return row, h
|
||||
|
||||
|
||||
def run_case(env, case, plan, output, guard):
|
||||
import torch
|
||||
from SkillFactory.legs.contact_cycle_sim.stress import gate_residual, residual_signals
|
||||
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
||||
|
||||
guard.check(force_disk=True)
|
||||
output.mkdir(exist_ok=False)
|
||||
env.reset(seed=plan['environment_seed'])
|
||||
s = state(env)
|
||||
if s.robot.is_fixed_base or env.cfg.terminations.failed is None or bool(s.scenarios.any()):
|
||||
raise RuntimeError('Require floating root, original failure termination and nominal scenarios')
|
||||
requests = residual_signals(plan, case).to(env.device)
|
||||
histories = {k: [] for k in ('reward', 'request', 'action', 'previous_action', 'terminated', 'truncated')}
|
||||
thresholds = json.loads((ROOT / 'configs/contact_cycle/acceptance_v1.json').read_text())['gates']
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety, SimCycleRecorder(env) as recorder:
|
||||
save_tensor(output / 'initial.pt', recorder.initial_state, guard)
|
||||
for k in range(plan['controls_per_case']):
|
||||
guard.check()
|
||||
request = requests[k]
|
||||
if case['kind'] == 'gated_sine':
|
||||
request = gate_residual(request, s.reference()[0], plan['gated_phases'])
|
||||
recorder.before_control(None, request)
|
||||
_, reward, terminated, truncated, _ = env.step(request)
|
||||
recorder.end_control(terminated, truncated, k == plan['controls_per_case'] - 1)
|
||||
for name, value in zip(histories, (reward, request, env.action_manager.action,
|
||||
env.action_manager.prev_action, terminated, truncated)):
|
||||
histories[name].append(value.detach().cpu().clone())
|
||||
safety.check()
|
||||
if (k + 1) % 100 == 0:
|
||||
print(f"{case['id']}: {k + 1}/900 controls", flush=True)
|
||||
history = {k: torch.stack(v) for k, v in histories.items()}
|
||||
trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]}
|
||||
functional = json.loads((ROOT / plan['functional_config']).read_text())
|
||||
rows, first_histories = [], {}
|
||||
for e, stop in enumerate(recorder.stops):
|
||||
episode = {k: v[:stop, e:e+1] for k, v in trace.items()}
|
||||
row, h = summarize_episode(episode, e, recorder, history, plan, s, functional)
|
||||
rows.append(row)
|
||||
first_histories[str(e)] = h
|
||||
selected = [r['env'] for r in rows if not r['success']][:4]
|
||||
complete = [r['env'] for r in rows if r['first_episode_samples'] == 4500 and not r['early_stop']]
|
||||
successful = [r['env'] for r in rows if r['success']]
|
||||
representative = (successful or complete)[:1]
|
||||
selected = sorted(set(selected + representative))
|
||||
subset = {str(e): {k: v[:recorder.stops[e], e:e+1].clone().contiguous() for k, v in trace.items()}
|
||||
for e in selected}
|
||||
save_tensor(output / 'first_episode_traces.pt', subset, guard)
|
||||
save_tensor(output / 'first_episode_control_histories.pt', first_histories, guard)
|
||||
result = {'case': case, 'rows': rows, 'success_count': sum(r['success'] for r in rows),
|
||||
'environment_count': env.num_envs, 'saved_trace_environments': selected,
|
||||
'physics_safety': safety.report(),
|
||||
'harness_verified': all(all(r['checks'].values()) for r in rows),
|
||||
'force_provenance': {'normal': 'PhysX foot_contact net normal force, world frame',
|
||||
'external': 'declared event wrench buffers; nominal all zero',
|
||||
'guide': 'declared auxiliary fixture, no vertical guide force',
|
||||
'auxiliary': 'declared phase-dependent vertical support with r cross F moments'},
|
||||
'remaining_window_s': guard.check(force_disk=True)}
|
||||
write_json(output / 'result.json', result)
|
||||
print(f"CASE_RESULT {case['id']}: {result['success_count']}/8", flush=True)
|
||||
return result
|
||||
|
||||
|
||||
def run_matrix(env, output, guard, registration):
|
||||
results = []
|
||||
plan = registration['plan']
|
||||
for case in plan['cases']:
|
||||
verify_hashes(registration['identity'])
|
||||
result = run_case(env, case, plan, output / case['id'], guard)
|
||||
results.append(result)
|
||||
write_json(output / 'matrix_progress.json', {
|
||||
'completed_cases': [{'id': r['case']['id'], 'success_count': r['success_count']} for r in results],
|
||||
'planned_case_count': len(plan['cases']), 'remaining_window_s': guard.check()})
|
||||
baseline = all(r['success_count'] == 8 for r in results if r['case']['kind'] == 'zero')
|
||||
verified = all(r['harness_verified'] for r in results)
|
||||
stress = [r for r in results if r['case']['kind'] != 'zero']
|
||||
return {'passed': verified and baseline, 'passed_means': 'harness verified AND both baselines; NOT stress success',
|
||||
'harness_verified': verified, 'baseline_passed': baseline,
|
||||
'case_count': len(results), 'environment_episodes': 8 * len(results),
|
||||
'stress_case_count': len(stress), 'stress_full_success_cases': sum(r['success_count'] == 8 for r in stress),
|
||||
'stress_success_episodes': sum(r['success_count'] for r in stress),
|
||||
'case_counts': {r['case']['id']: r['success_count'] for r in results},
|
||||
'controller_kind': 'analytic_reference_with_scripted_residual', 'accepted_skill': False,
|
||||
'trained_residual_policy': False, 'optimizer_steps': 0, 'policy_models_constructed': 0,
|
||||
'remaining_window_s': guard.check(force_disk=True),
|
||||
'limitations': ['Nominal development-only deterministic environment; no external disturbances.',
|
||||
'Gated lift/swing domain is not the all-phase domain.',
|
||||
'No learned-policy, robustness, recovery, hardware or release claim.',
|
||||
'First failure onset is 500Hz; actual environment termination/reset is control-rate.',
|
||||
'First four failures plus representative complete traces; all eight rows/histories.']}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
parser.add_argument('--supervise', action='store_true')
|
||||
known, _ = parser.parse_known_args()
|
||||
output = known.output.resolve()
|
||||
if output.parent != OUTPUT_ROOT:
|
||||
parser.error(f'Output must be a fresh attempt directory directly inside {OUTPUT_ROOT}')
|
||||
if known.supervise:
|
||||
raise SystemExit(supervise(output))
|
||||
guard = WindowGuard()
|
||||
guard.check(force_disk=True)
|
||||
registration = json.loads((output / 'preregistration.json').read_text())
|
||||
launch = json.loads((output / 'launch.json').read_text())
|
||||
if (sha256(output / 'preregistration.json') != launch['preregistration_sha256']
|
||||
or sha256(PLAN) != registration['plan_sha256']):
|
||||
raise RuntimeError('Preregistration changed before launch')
|
||||
verify_hashes(registration['identity'])
|
||||
guard.check(reserve=1024)
|
||||
claim_execution(output)
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
AppLauncher.add_app_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
guard.check() # last pre-Kit authorization check
|
||||
app = AppLauncher(args).app
|
||||
env = None
|
||||
result = {'passed': False, 'harness_verified': False, 'baseline_passed': False, 'optimizer_steps': 0}
|
||||
code = 1
|
||||
try:
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv
|
||||
|
||||
cfg = SimContactCycleEnvCfg()
|
||||
cfg.scene.num_envs = registration['plan']['num_envs']
|
||||
cfg.sim.device = args.device
|
||||
cfg.seed = registration['plan']['environment_seed']
|
||||
guard.check()
|
||||
env = SimContactCycleEnv(cfg=cfg)
|
||||
result = run_matrix(env, output, guard, registration)
|
||||
verify_hashes(registration['identity'])
|
||||
code = 0 if result['passed'] else 2
|
||||
except BaseException:
|
||||
result.update(passed=False, harness_verified=False, error=traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
finish_run(app, env, output / 'result.json', result, code)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent real PhysX impulse oracle: native point load vs COM-reduced load.
|
||||
|
||||
Three identical isotropic bodies: correct reduction, native off-center force,
|
||||
and deliberately double-counted lever arm (negative control). No training.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import math
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
|
||||
def run(args):
|
||||
import torch
|
||||
from isaaclab_physx.physics import PhysxCfg
|
||||
from SkillFactory.legs.contact_cycle.mechanics import reduce_to_com, rotate
|
||||
|
||||
import isaaclab.sim as sim_utils
|
||||
from isaaclab.assets import RigidObject, RigidObjectCfg
|
||||
from isaaclab.sim import SimulationCfg, build_simulation_context
|
||||
|
||||
dt, mass, radius = 0.002, 2.0, 0.1
|
||||
cfg = SimulationCfg(dt=dt, device=args.device, gravity=(0, 0, 0),
|
||||
physics=PhysxCfg(solver_type=0, enable_external_forces_every_iteration=False))
|
||||
with build_simulation_context(sim_cfg=cfg) as sim:
|
||||
sim._app_control_on_stop_handle = None
|
||||
for i in range(3):
|
||||
sim_utils.create_prim(f"/World/Fixture_{i}", "Xform", translation=(2*i, 0, 0))
|
||||
body = RigidObject(RigidObjectCfg(
|
||||
prim_path="/World/Fixture_.*/Sphere",
|
||||
spawn=sim_utils.SphereCfg(radius=radius,
|
||||
rigid_props=sim_utils.RigidBodyPropertiesCfg(linear_damping=0, angular_damping=0),
|
||||
mass_props=sim_utils.MassPropertiesCfg(mass=mass)),
|
||||
))
|
||||
sim.reset()
|
||||
pos = torch.tensor([[2*i, 0., 0.] for i in range(3)], device=args.device)
|
||||
quat = torch.tensor([[0.2, -0.3, 0.4, math.sqrt(0.71)]], device=args.device).expand(3, -1)
|
||||
body.write_root_pose_to_sim(torch.cat((pos, quat), -1))
|
||||
body.write_root_velocity_to_sim(torch.zeros(3, 6, device=args.device))
|
||||
body.update(0)
|
||||
local_f = torch.tensor([[1., -2, 3]], device=args.device).expand(3, -1)
|
||||
local_tau = torch.tensor([[0.02, -0.03, 0.04]], device=args.device).expand(3, -1)
|
||||
lever = rotate(quat, torch.tensor([[0.04, 0.03, -0.02]], device=args.device).expand(3, -1))
|
||||
point = pos + lever
|
||||
reduced = reduce_to_com(local_f, local_tau, quat, point, pos)
|
||||
force = reduced[:, :3]
|
||||
torques = reduced[:, 3:].clone()
|
||||
positions = pos.clone()
|
||||
# native point oracle, and intentional double r x F negative control
|
||||
torques[1] = rotate(quat, local_tau)[1]
|
||||
positions[1:] = point[1:]
|
||||
body.permanent_wrench_composer.set_forces_and_torques_index(
|
||||
forces=force[:, None], torques=torques[:, None], positions=positions[:, None], is_global=True)
|
||||
body.write_data_to_sim()
|
||||
sim.step()
|
||||
body.update(dt)
|
||||
velocity = body.data.root_com_vel_w.torch.clone()
|
||||
inertia = 0.4 * mass * radius**2
|
||||
expected = torch.cat((force * dt/mass, reduced[:, 3:] * dt/inertia), -1)
|
||||
native_difference = (velocity[0]-velocity[1]).abs().max().item()
|
||||
oracle_error = (velocity[:2]-expected[:2]).abs().max().item()
|
||||
negative_separation = (velocity[2]-expected[2]).abs().max().item()
|
||||
body.permanent_wrench_composer.reset()
|
||||
body.write_data_to_sim()
|
||||
sim.step()
|
||||
body.update(dt)
|
||||
cleared_delta = (body.data.root_com_vel_w.torch-velocity).abs().max().item()
|
||||
gates = {"native_point_equivalence": native_difference <= 2e-5,
|
||||
"newton_euler_impulse": oracle_error <= 2e-5,
|
||||
"double_lever_negative_control_detected": negative_separation >= 0.001,
|
||||
"clear_stops_acceleration": cleared_delta <= 2e-5}
|
||||
return {"passed": all(gates.values()), "accepted_skill": False, "gates": gates,
|
||||
"native_point_max_velocity_difference": native_difference,
|
||||
"analytical_max_velocity_error": oracle_error,
|
||||
"double_lever_error": negative_separation, "clear_max_velocity_change": cleared_delta,
|
||||
"measured_twist_world": velocity.tolist(), "expected_twist_world": expected.tolist(),
|
||||
"mass_kg": mass, "inertia_kg_m2": inertia, "dt_s": dt,
|
||||
"note": "Rigid-body force/torque API oracle only; not a Go2 tracking or sim-to-real certificate."}
|
||||
|
||||
|
||||
def main():
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
AppLauncher.add_app_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
files = [Path(__file__), root/"source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py",
|
||||
root/"scripts/runtime_common.py", root/"scripts/supervise_sim.py"]
|
||||
write_json(args.output/"identity.json", {str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in files})
|
||||
write_json(args.output/"result.json", {"passed": False, "status": "running"})
|
||||
app = AppLauncher(args).app
|
||||
result, code = {"passed": False}, 1
|
||||
try:
|
||||
result = run(args)
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
result.update(error=traceback.format_exc())
|
||||
print(result["error"])
|
||||
finally:
|
||||
finish_run(app, None, args.output/"result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user