wip: checkpoint paused Go2 FL four-skill development
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent CPU-only audit of complete four-skill original500Hz traces.
|
||||
|
||||
This checks physical tensors, not the actor's reward or ready/success labels.
|
||||
It never launches Kit, creates authority, alters traces or selects a candidate.
|
||||
"""
|
||||
import argparse
|
||||
import importlib.util
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
import torch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def audit(run):
|
||||
run = Path(run)
|
||||
config = json.loads((ROOT / 'configs/four_skills/v050.json').read_text())
|
||||
approval = json.loads((run / 'approval.json').read_text())
|
||||
worker = json.loads((run / 'worker-result.json').read_text())
|
||||
supervisor = json.loads((run / 'supervisor-result.json').read_text())
|
||||
trace = torch.load(run / 'trace.pt', map_location='cpu', weights_only=True)
|
||||
initial = torch.load(run / 'initial-state.pt', map_location='cpu', weights_only=True)
|
||||
reasons = []
|
||||
def require(test, reason):
|
||||
if not bool(test):
|
||||
reasons.append(reason)
|
||||
count = len(trace['tick'])
|
||||
require(count > 0, 'empty_trace')
|
||||
require(torch.equal(trace['tick'][:, 0], torch.arange(1, count + 1)), 'physical_clock_not_contiguous')
|
||||
require(count == worker.get('actual_ticks'), 'actual_call_trace_count_mismatch')
|
||||
require(all(bool(torch.isfinite(v).all()) for v in trace.values() if v.is_floating_point()), 'nonfinite_evidence')
|
||||
require(not trace['failed'].any(), 'runtime_failure_in_trace')
|
||||
require(trace['limit_ratio'].max() <= 1.00001, 'effort_velocity')
|
||||
require(trace['joint_excess'].max() <= .01, 'joint_limits')
|
||||
require(trace['height_error'].max() <= .1, 'root_height')
|
||||
require(trace['tilt'].max() <= .6, 'tilt')
|
||||
require(trace['gap'].min() >= -.002, 'penetration')
|
||||
require(trace['guide'][..., 2].abs().max() == 0, 'vertical_guide')
|
||||
if 'auxiliary' in trace:
|
||||
auxiliary = trace['auxiliary']
|
||||
az = auxiliary[..., 2]
|
||||
require(az.sum(-1).max() <= 140., 'auxiliary_force')
|
||||
require(az.min() >= -1e-6 and auxiliary[..., :2].abs().max() <= 1e-6
|
||||
and auxiliary[..., 5].abs().max() <= 1e-6
|
||||
and (auxiliary[..., 3:].norm(dim=-1) <= .06*az.clamp_min(0)+1e-6).all(), 'auxiliary_geometry')
|
||||
else:
|
||||
require(False, 'missing_auxiliary_evidence')
|
||||
require(trace['normal'][..., 2].min() >= -.5, 'negative_normal')
|
||||
require(worker.get('initialization_counters') == worker.get('final_counters'), 'reset_or_pose_write')
|
||||
require(worker['physics_counts']['initialization'] == 0, 'unexpected_initialization_physics')
|
||||
require(worker['physics_counts']['completed'] == count, 'unrecorded_physics')
|
||||
transitions = json.loads((run / 'transitions.json').read_text())
|
||||
sequence = config['sequences'][approval['sequence']]
|
||||
loops = approval.get('loops') or (1 if approval['mode'] == 'smoke' else sequence['loops'])
|
||||
require(worker.get('requested_loops') == loops, 'approved_loop_count_mismatch')
|
||||
require(approval['config_sha256'] == __import__('hashlib').sha256((ROOT / 'configs/four_skills/v050.json').read_bytes()).hexdigest(), 'approved_config_changed')
|
||||
expected_edges = [('support', 'retract'), ('retract', 'swing'), ('swing', 'plant'), ('plant', 'support')] * loops
|
||||
require([(e['from'], e['to']) for e in transitions] == expected_edges, 'missing_or_illegal_transitions')
|
||||
require(all(e['feedback_ready'] and e['feedback_hold_complete'] for e in transitions), 'switch_without_feedback')
|
||||
kinds = trace['skill'][:, 0]
|
||||
starts = [0] + (kinds[1:] != kinds[:-1]).nonzero().flatten().add(1).tolist()
|
||||
ends = starts[1:] + [count]
|
||||
require(len(starts) == 4 * loops + 1, 'incomplete_skill_segments')
|
||||
require([int(kinds[i]) for i in starts] == [0, 1, 2, 3] * loops + [0], 'actual_skill_order_mismatch')
|
||||
require(torch.equal(trace['time_s'][:, 0], trace['tick'][:, 0].float() * .002), 'time_tick_mismatch')
|
||||
require(torch.equal(trace['episode_id'][:, 0], initial['episode_id'][0].expand(count)), 'episode_changed_during_composition')
|
||||
for j, start in enumerate(starts[1:]):
|
||||
if j < len(transitions):
|
||||
require(transitions[j]['at_tick'] == int(trace['tick'][start, 0]) - 1, 'transition_tick_mismatch')
|
||||
for start, end in zip(starts, ends):
|
||||
expected_age = torch.arange(1, end - start + 1).float() * .002
|
||||
require(torch.allclose(trace['age_s'][start:end, 0], expected_age, rtol=0., atol=.001), 'segment_age_mismatch')
|
||||
age = trace['age_s'][:, 0]
|
||||
expected_phase = torch.where(kinds == 0, 0, torch.where(kinds == 1,
|
||||
torch.where(age < 1., 1, 2), torch.where(kinds == 2, 3, torch.where(age < .8, 4, 5))))
|
||||
if 'phase' in trace:
|
||||
require(torch.equal(trace['phase'][:, 0], expected_phase), 'intrinsic_phase_mismatch')
|
||||
else:
|
||||
require(False, 'missing_phase_evidence')
|
||||
require(not (((expected_phase == 2) & (age > 1.54)) & (trace['gap'][:, 0] < .02)).any(), 'original_late_lift_floor')
|
||||
require(not ((expected_phase == 3) & (trace['gap'][:, 0] < .01)).any(), 'original_swing_clearance')
|
||||
require(not (((expected_phase == 5) & (age > 2.56)) &
|
||||
(trace['normal'][:, 0, 2] < .5*trace['weight'][:, 0])).any(), 'original_reload_deadline')
|
||||
holds = sequence['holds_s']
|
||||
windows = []
|
||||
swing_index = 0
|
||||
for index, (start, end) in enumerate(zip(starts, ends)):
|
||||
kind = int(kinds[start])
|
||||
name = config['skills'][kind]
|
||||
hold = holds[kind]
|
||||
dwell = .2 if kind in (0, 3) else .1
|
||||
window = math.ceil((hold + dwell) / .002) - 1
|
||||
if end - start < window:
|
||||
require(False, f'{index}:{name}:incomplete_hold_window')
|
||||
continue
|
||||
part = slice(end - window, end)
|
||||
normal = trace['normal'][part, 0, 2]
|
||||
gap = trace['gap'][part, 0]
|
||||
weight = trace['weight'][part, 0]
|
||||
row = {'segment': index, 'skill': name, 'ticks': end-start,
|
||||
'checked_hold_dwell_s': window*.002, 'normal_min_N': float(normal.min()),
|
||||
'normal_max_N': float(normal.max()), 'gap_min_m': float(gap.min())}
|
||||
if kind in (0, 3):
|
||||
contact_fraction = float((normal >= .5 * weight).float().mean())
|
||||
require(contact_fraction >= .8, f'{index}:{name}:support_contact_fraction')
|
||||
require(bool(((normal >= .5 * weight) & (gap <= .002)).all()), f'{index}:{name}:continuous_support_dwell_and_hold')
|
||||
require(float(normal.mean()) >= .5 * float(weight.mean()), f'{index}:{name}:support_mean_load')
|
||||
row['contact_fraction'] = contact_fraction
|
||||
if kind == 3:
|
||||
error = (trace['foot'][part] - trace['target'][part]).norm(dim=-1)
|
||||
require(error.max() <= .05, f'{index}:landing_error')
|
||||
row['landing_error_max_m'] = float(error.max())
|
||||
else:
|
||||
require(bool((gap >= .01).all() and (normal <= 2.).all()), f'{index}:{name}:air_hold')
|
||||
if kind == 1:
|
||||
unload = (trace['age_s'][start:end, 0] >= .9) & (trace['age_s'][start:end, 0] <= 1.)
|
||||
first_normal = initial['normal'][0, 2] if start == 0 else trace['normal'][start-1, 0, 2]
|
||||
remain = trace['normal'][start:end, 0, 2][unload]
|
||||
require(len(remain) and remain.mean() <= .8 * first_normal, f'{index}:unload')
|
||||
else:
|
||||
command = torch.tensor(sequence['direction_m']) * (1 if swing_index % 2 == 0 else -1)
|
||||
entry = trace['foot'][start-1, 0, :2]
|
||||
displacement = trace['foot'][end-1, 0, :2] - entry
|
||||
directed = (trace['foot'][part, 0, :2] - entry) * command.sign()
|
||||
require((directed >= .5 * command.abs()).all(), f'{index}:continuous_signed_swing_hold')
|
||||
require(((displacement * command.sign()) >= .5 * command.abs()).all(), f'{index}:signed_swing')
|
||||
row['actual_xy_m'] = displacement.tolist()
|
||||
row['command_xy_m'] = command.tolist()
|
||||
swing_index += 1
|
||||
windows.append(row)
|
||||
# Reconstruct the exact private disturbance schedule, then compare to the
|
||||
# original applied wrench tensor at each pre-step clock (not reward labels).
|
||||
schedule_doc = json.loads((run / 'force-schedule.json').read_text())
|
||||
spec = importlib.util.spec_from_file_location('four_force_audit', ROOT / 'source/SkillFactory/SkillFactory/legs/four_skills/forces.py')
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
row = schedule_doc['rows'][0]
|
||||
require(schedule_doc['seed'] == approval['seed'], 'schedule_seed_not_approved')
|
||||
require(row['tier'] == approval['tier'], 'schedule_tier_not_approved')
|
||||
require(row['env_id'] == 0 and row['episode_id'] == int(initial['episode_id'][0]), 'schedule_episode_not_initial_state')
|
||||
runtime = json.loads((run / 'runtime-identity.json').read_text())
|
||||
payload_index = runtime['payload_body_index']
|
||||
require(len(runtime['body_names']) == trace['external'].shape[-2], 'body_index_layout_mismatch')
|
||||
require(runtime['body_names'][payload_index] == 'virtual_payload', 'wrong_external_force_body')
|
||||
schedule = module.ForceSchedule(schedule_doc['seed'], [row['env_id']], row['tier'], 'cpu',
|
||||
duration_s=schedule_doc['duration_s'], episode_ids=[row['episode_id']])
|
||||
require(schedule.manifest() == schedule_doc, 'force_manifest_parameters_changed')
|
||||
times = torch.cat((initial['time_s'][:1], trace['time_s'][:-1, 0]))
|
||||
# CPU batched audit is in chunks, preserving every original sample.
|
||||
maximum_force_difference = 0.
|
||||
for start in range(0, count, 2048):
|
||||
end = min(count, start + 2048)
|
||||
predicted = torch.stack([schedule.at(t.reshape(1))[0] for t in times[start:end]])
|
||||
actual = trace['external'][start:end, 0]
|
||||
expected = torch.zeros_like(actual)
|
||||
expected[:, payload_index, :3] = predicted
|
||||
maximum_force_difference = max(maximum_force_difference, float((expected - actual).abs().max()))
|
||||
require(maximum_force_difference <= 2e-6, 'sampled_force_not_actually_applied')
|
||||
tier_cap = config['force']['tiers_N'][approval['tier']]
|
||||
require(trace['external'][..., :3].norm(dim=-1).max() <= tier_cap + .000001, 'force_out_of_approved_tier_range')
|
||||
require(supervisor['success'] and supervisor['owned_cleanup'] and supervisor['waited_exit'], 'supervision_or_cleanup')
|
||||
require(worker.get('functional_success') and worker.get('completed_loops') == loops, 'incomplete_original_result')
|
||||
return {'passed': not reasons, 'failure_reasons': reasons, 'run': str(run.resolve()),
|
||||
'physics_ticks': count, 'requested_loops': loops, 'windows': windows,
|
||||
'max_force_reconstruction_difference_N': maximum_force_difference,
|
||||
'scope': 'CPU physical-tensor audit; runtime continuity separately requires original hooks/supervisor'}
|
||||
|
||||
|
||||
def compare_pair(analytic, trained):
|
||||
a, b = Path(analytic), Path(trained)
|
||||
aa = json.loads((a / 'approval.json').read_text())
|
||||
ab = json.loads((b / 'approval.json').read_text())
|
||||
conditions = {k: aa[k] == ab[k] for k in ('seed', 'sequence', 'loops', 'tier', 'config_sha256', 'mode')}
|
||||
identity_file = b / 'checkpoint-identity.json'
|
||||
trained_identity = json.loads(identity_file.read_text()) if identity_file.exists() else {}
|
||||
controllers = (aa['controller'] == 'analytic' and aa['checkpoint'] is None
|
||||
and ab['controller'] == 'trained' and bool(ab['checkpoint_sha256'])
|
||||
and trained_identity.get('sha256') == ab['checkpoint_sha256']
|
||||
and type(trained_identity.get('update')) is int and trained_identity['update'] > 0
|
||||
and trained_identity.get('identity', {}).get('config_sha256') == ab['config_sha256'])
|
||||
integrity = {name: json.loads((p / 'supervisor-result.json').read_text())['success'] for name, p in [('analytic', a), ('trained', b)]}
|
||||
sa = json.loads((a / 'force-schedule.json').read_text())
|
||||
sb = json.loads((b / 'force-schedule.json').read_text())
|
||||
ia = torch.load(a / 'initial-state.pt', weights_only=True, map_location='cpu')
|
||||
ib = torch.load(b / 'initial-state.pt', weights_only=True, map_location='cpu')
|
||||
fields = ['root', 'joint_pos', 'joint_vel', 'normal', 'foot', 'target', 'reference', 'q_ik', 'actor_observation']
|
||||
equality = {k: torch.equal(ia[k], ib[k]) for k in fields}
|
||||
ta = torch.load(a / 'trace.pt', weights_only=True, map_location='cpu')
|
||||
tb = torch.load(b / 'trace.pt', weights_only=True, map_location='cpu')
|
||||
shared = min(len(ta['tick']), len(tb['tick']))
|
||||
force_prefix_equal = torch.equal(ta['external'][:shared], tb['external'][:shared])
|
||||
return {'paired': sa == sb and all(equality.values()) and all(conditions.values()) and controllers and all(integrity.values()) and force_prefix_equal,
|
||||
'approved_conditions_equal': conditions, 'analytic_vs_frozen_trained': controllers,
|
||||
'runtime_integrity': integrity, 'common_prefix_ticks': shared, 'applied_force_common_prefix_equal': force_prefix_equal,
|
||||
'analytic_physical_audit_passed': audit(a)['passed'], 'trained_physical_audit_passed': audit(b)['passed'],
|
||||
'disturbance_schedule_equal': sa == sb,
|
||||
'observed_initial_state_equal': equality,
|
||||
'limitation': 'observed physical/controller state, not a proof of complete hidden PhysX state identity'}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p = argparse.ArgumentParser(description=__doc__)
|
||||
p.add_argument('run', type=Path)
|
||||
p.add_argument('--pair', type=Path)
|
||||
p.add_argument('--output', type=Path)
|
||||
args = p.parse_args()
|
||||
result = compare_pair(args.run, args.pair) if args.pair else audit(args.run)
|
||||
text = json.dumps(result, indent=2, allow_nan=False) + '\n'
|
||||
if args.output:
|
||||
args.output.write_text(text)
|
||||
print(text)
|
||||
raise SystemExit(0 if result.get('passed', result.get('paired', False)) else 1)
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Public-interface examples for an already supervised four-skill runtime.
|
||||
|
||||
`advance` is the certified owner's one-control-step callback. It owns actor
|
||||
inference,500Hz safety/evidence, deadlines and cleanup. The central client below
|
||||
never receives desired support loads or sampled force parameters, and cannot
|
||||
reset/teleport the robot. Do not construct an unsupervised Kit here.
|
||||
"""
|
||||
from SkillFactory.legs.four_skills.api import CentralComposer
|
||||
|
||||
|
||||
def compose(service, advance, *, direction_m=(.024, .010), holds_s=(.5, .3, .4, .5), loops=5):
|
||||
program = CentralComposer(service, {'direction_m': direction_m, 'holds_s': holds_s, 'loops': loops})
|
||||
for _ in range(12000):
|
||||
if program.update():
|
||||
return {'completed_loops': program.completed_loops, 'safe_stop': program.safely_stopped,
|
||||
'status': service.query()}
|
||||
advance()
|
||||
service.request_stop()
|
||||
raise TimeoutError('Central command budget exhausted; owner must finish safe cleanup')
|
||||
|
||||
|
||||
def hold_then_switch(service, advance):
|
||||
"""Illustrate calls individually; all transitions use actual ready feedback."""
|
||||
def wait():
|
||||
for _ in range(2000):
|
||||
status = service.query()
|
||||
if status['failed']:
|
||||
raise RuntimeError(str(status['failure_reasons']))
|
||||
if status['ready'] and status['hold_complete']:
|
||||
return status
|
||||
advance()
|
||||
raise TimeoutError('Skill feedback not ready; no forced transition or state overwrite')
|
||||
service.hold(.8)
|
||||
wait()
|
||||
service.call('retract', hold_seconds=.5)
|
||||
wait()
|
||||
service.switch('swing', direction_m=[.020, .024], hold_seconds=1.)
|
||||
wait()
|
||||
service.call('plant', hold_seconds=.7)
|
||||
wait()
|
||||
service.call('support', hold_seconds=.8)
|
||||
wait()
|
||||
service.request_stop()
|
||||
return service.query()
|
||||
@@ -0,0 +1,410 @@
|
||||
#!/usr/bin/env python3
|
||||
"""New v0.5.0 stage CLI: finite fresh runs, strict GPU/process ownership.
|
||||
|
||||
No simulation or authority is created on import. Normal callers use the public
|
||||
skill service; the private worker path requires a live parent socket handoff.
|
||||
"""
|
||||
import argparse
|
||||
import ctypes
|
||||
from datetime import datetime, timezone
|
||||
import fcntl
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import signal
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from types import SimpleNamespace
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
STAGE = ROOT / 'logs/four_skills_v050'
|
||||
CONFIG = ROOT / 'configs/four_skills/v050.json'
|
||||
START = '2026-10-16T16:07:37+08:00'
|
||||
DEADLINE = '2026-10-18T16:07:37+08:00'
|
||||
|
||||
|
||||
def clock():
|
||||
return time.clock_gettime(time.CLOCK_BOOTTIME)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def write(path, value):
|
||||
path = Path(path)
|
||||
temp = path.with_suffix(path.suffix + '.pending')
|
||||
data = json.dumps(value, sort_keys=True, indent=2, allow_nan=False).encode() + b'\n'
|
||||
with temp.open('wb') as stream:
|
||||
stream.write(data)
|
||||
stream.flush()
|
||||
os.fsync(stream.fileno())
|
||||
os.replace(temp, path)
|
||||
|
||||
|
||||
def sources():
|
||||
files = [p for base, pattern in [('source', '*.py'), ('scripts', '*.py'), ('configs', '*.json')]
|
||||
for p in (ROOT / base).rglob(pattern) if '__pycache__' not in p.parts]
|
||||
for directory in ('legs/go2_fl', 'support/go2_fl_3kg_v0', 'go2_fl'):
|
||||
files += [p for p in (ROOT / 'assets/generated' / directory).rglob('*')
|
||||
if p.is_file() and '__pycache__' not in p.parts]
|
||||
files += [ROOT / 'docs/four_skills_v050.md', ROOT / 'demo_gui.sh', ROOT / 'demo_gui_v042.sh']
|
||||
return {p.relative_to(ROOT).as_posix(): sha(p) for p in sorted(files)}
|
||||
|
||||
|
||||
def scope():
|
||||
STAGE.mkdir(exist_ok=True)
|
||||
if (STAGE / 'PAUSED.json').exists():
|
||||
raise PermissionError('Stage paused by user; a new explicit resume instruction is required')
|
||||
p = STAGE / 'scope.json'
|
||||
if not p.exists():
|
||||
remaining = datetime.fromisoformat(DEADLINE).timestamp() - time.time()
|
||||
value = {'format': 'skillfactory.four-skills-stage.v050', 'started_wall': START,
|
||||
'deadline_wall': DEADLINE, 'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(),
|
||||
'deadline_boottime': clock() + remaining, 'train_cutoff_boottime': clock() + remaining - 21600,
|
||||
'max_kit_launches': 64, 'max_reserved_physics_calls': 3000000,
|
||||
'max_owned_bytes': 120 << 30, 'user_authorized_hours': 48,
|
||||
'preserve_original_versions': True, 'scope': 'single aided Go2FL only'}
|
||||
with p.open('x') as stream:
|
||||
stream.write(json.dumps(value, indent=2) + '\n')
|
||||
value = json.loads(p.read_text())
|
||||
if value['boot_id'] != Path('/proc/sys/kernel/random/boot_id').read_text().strip():
|
||||
raise RuntimeError('Stage boot changed; stop, do not revive old grants')
|
||||
if clock() >= value['deadline_boottime']:
|
||||
raise TimeoutError('48h stage deadline reached; no new work')
|
||||
amendment = STAGE / 'scope-amendment-01.json'
|
||||
if amendment.exists():
|
||||
update = json.loads(amendment.read_text())
|
||||
if (update.get('format') != 'skillfactory.four-skills-budget-amendment.v1'
|
||||
or update.get('original_scope_sha256') != sha(p)
|
||||
or update.get('max_kit_launches') != 96):
|
||||
raise ValueError('Invalid recorded stage budget amendment')
|
||||
value['max_kit_launches'] = 96
|
||||
value['amendment_sha256'] = sha(amendment)
|
||||
return value
|
||||
|
||||
|
||||
def new_session(path, mode):
|
||||
"""Explicit future caller invocation, never revival of the48h stage grant."""
|
||||
global STAGE
|
||||
if mode not in ('gui', 'evaluate', 'probe'):
|
||||
raise PermissionError('Standalone sessions cannot authorize training')
|
||||
path = Path(path)
|
||||
if (not path.is_absolute() or path != path.resolve() or path.exists() or not path.parent.is_dir()
|
||||
or path.is_relative_to(ROOT / 'releases') or path.is_relative_to(ROOT / 'performance_v042')
|
||||
or path.is_relative_to(Path('/home/cen/sf-deliveries'))
|
||||
or (path.is_relative_to(ROOT) and not path.is_relative_to(ROOT / 'logs/four_skills_v050_sessions'))
|
||||
or (path.is_relative_to(Path('/home/cen/sf-runs')) and path.parent != Path('/home/cen/sf-runs'))):
|
||||
raise ValueError('Fresh canonical session outside protected evidence/source trees required')
|
||||
path.mkdir()
|
||||
STAGE = path
|
||||
value = {'format': 'skillfactory.four-skills-explicit-session.v050',
|
||||
'issued_wall': datetime.now().astimezone().isoformat(),
|
||||
'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(),
|
||||
'deadline_boottime': clock() + 3600, 'train_cutoff_boottime': clock(),
|
||||
'max_kit_launches': 1, 'max_reserved_physics_calls': 50000,
|
||||
'max_owned_bytes': 8 << 30, 'training_authorized': False,
|
||||
'scope': 'explicit caller invocation; not a48h-task extension or old-grant reuse'}
|
||||
write(path / 'scope.json', value)
|
||||
return value
|
||||
|
||||
|
||||
def tree_bytes(path):
|
||||
total = 0
|
||||
for base, dirs, files in os.walk(path, followlinks=False):
|
||||
dirs[:] = [d for d in dirs if not Path(base, d).is_symlink()]
|
||||
for name in files:
|
||||
p = Path(base, name)
|
||||
try:
|
||||
if p.is_symlink():
|
||||
raise RuntimeError('Symlink inside owned outputs')
|
||||
total += p.stat().st_size
|
||||
except FileNotFoundError:
|
||||
continue # atomic publication disappearance, never permission denial
|
||||
return total
|
||||
|
||||
|
||||
class Guard:
|
||||
def __init__(self, payload, *, worker=False):
|
||||
self.p = payload
|
||||
self.interrupted = False
|
||||
self.next_slow = 0
|
||||
self.worker = worker
|
||||
|
||||
def __call__(self, *, cleanup=False):
|
||||
if not cleanup and (self.interrupted or (Path(self.p['output']) / 'cancel.request').exists()
|
||||
or (Path(self.p['output']).parent / 'PAUSED.json').exists()):
|
||||
raise InterruptedError('Cooperative cancellation')
|
||||
end = self.p['deadline_boottime'] if cleanup else self.p['deadline_boottime'] - 120
|
||||
if clock() >= end:
|
||||
raise TimeoutError('Run/stage deadline; reserve cleanup')
|
||||
if cleanup or clock() < self.next_slow:
|
||||
return
|
||||
self.next_slow = clock() + 1
|
||||
if tree_bytes(self.p['output']) > self.p['max_owned_bytes']:
|
||||
raise RuntimeError('Owned artifact cap exceeded')
|
||||
if os.statvfs(self.p['output']).f_bavail * os.statvfs(self.p['output']).f_frsize < 8 << 30:
|
||||
raise RuntimeError('Minimum free disk headroom8GiB')
|
||||
for relative, digest in self.p['sources'].items():
|
||||
if sha(ROOT / relative) != digest:
|
||||
raise RuntimeError('Bound runtime source changed: ' + relative)
|
||||
|
||||
|
||||
def reserve(args, sc):
|
||||
p = STAGE / 'ledger.json'
|
||||
ledger = json.loads(p.read_text()) if p.exists() else {'launches': 0, 'reserved_physics_calls': 0, 'runs': []}
|
||||
calls = 160000 if args.mode == 'train' else 50000
|
||||
if args.mode in ('smoke', 'probe') or args.loops == 1:
|
||||
calls = 15000
|
||||
if ledger['launches'] + 1 > sc['max_kit_launches'] or ledger['reserved_physics_calls'] + calls > sc['max_reserved_physics_calls']:
|
||||
raise RuntimeError('Stage cap exhausted; do not silently extend')
|
||||
if args.mode == 'train' and clock() >= sc['train_cutoff_boottime']:
|
||||
raise TimeoutError('Final6h: no new training')
|
||||
duration = 64800 if args.mode == 'train' else 2400
|
||||
output = (STAGE / args.name).absolute()
|
||||
if (not args.name or Path(args.name).name != args.name or args.name.startswith('.')
|
||||
or output.exists() or output.parent != STAGE or output.resolve() != output):
|
||||
raise ValueError('Fresh unused simple run name required')
|
||||
if tree_bytes(STAGE) > sc['max_owned_bytes']:
|
||||
raise RuntimeError('Stage storage cap')
|
||||
checkpoint = None if args.checkpoint is None else str(args.checkpoint.resolve(strict=True))
|
||||
if args.controller == 'trained' and checkpoint is None:
|
||||
raise ValueError('Trained controller requires explicit immutable checkpoint')
|
||||
if args.controller == 'analytic' and checkpoint is not None:
|
||||
raise ValueError('Analytic controller cannot load weights')
|
||||
if args.mode == 'train' and args.controller != 'analytic':
|
||||
raise ValueError('Fresh training is not checkpoint resume')
|
||||
output.mkdir()
|
||||
payload = {'format': 'skillfactory.four-skills-run.v050', 'mode': args.mode, 'output': str(output),
|
||||
'seed': args.seed, 'sequence': args.sequence, 'loops': args.loops,
|
||||
'tier': args.tier, 'controller': args.controller, 'checkpoint': checkpoint,
|
||||
'stop_skill': args.stop_skill or ('retract' if args.mode == 'probe' else None),
|
||||
'checkpoint_sha256': None if checkpoint is None else sha(checkpoint),
|
||||
'sources': sources(), 'config_sha256': sha(CONFIG), 'boot_id': sc['boot_id'],
|
||||
'deadline_boottime': min(clock() + duration, sc['deadline_boottime']),
|
||||
'max_physics_calls': calls, 'max_owned_bytes': (32 if args.mode == 'train' else 8) << 30,
|
||||
'updates': args.updates, 'parent_pid': os.getpid(), 'issued_boottime': clock(),
|
||||
'stage_amendment_sha256': sc.get('amendment_sha256')}
|
||||
ledger['launches'] += 1
|
||||
ledger['reserved_physics_calls'] += calls
|
||||
ledger['runs'].append({'name': args.name, 'mode': args.mode, 'reserved_calls': calls,
|
||||
'output': str(output), 'refunded': False})
|
||||
write(p, ledger)
|
||||
write(output / 'approval.json', payload)
|
||||
snapshot = output / 'source_snapshot'
|
||||
for relative in payload['sources']:
|
||||
dest = snapshot / relative
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_bytes((ROOT / relative).read_bytes())
|
||||
return payload
|
||||
|
||||
|
||||
def supervise(args):
|
||||
import evaluate_cycle_sim_residual as owner
|
||||
import rootxy_launch_v2 as ownership
|
||||
from skill_launch_v4 import capture_gpu
|
||||
sc = new_session(args.new_session, args.mode) if args.new_session else scope()
|
||||
with (STAGE / 'serial.lock').open('a') as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
payload = reserve(args, sc)
|
||||
out = Path(payload['output'])
|
||||
guard = Guard(payload)
|
||||
# No child command before this exact dedicated-supervisor snapshot.
|
||||
owned = owner.OwnedProcesses()
|
||||
gpu_identity = capture_gpu()
|
||||
write(out / 'gpu-identity.json', gpu_identity)
|
||||
gpu = ownership.GPUOwnership(gpu_identity)
|
||||
environment = {k: v for k, v in os.environ.items() if k not in
|
||||
('VIRTUAL_ENV', 'CONDA_PREFIX', 'PYTHONEXE', 'PYTHONHOME', 'args')}
|
||||
environment.update(PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1',
|
||||
CUDA_VISIBLE_DEVICES=gpu_identity['uuid'],
|
||||
SKILLFACTORY_GO2_ASSET_DIR=str(ROOT / 'assets/generated/go2_fl'),
|
||||
SKILLFACTORY_LEG_BUNDLE=str(ROOT / 'assets/generated/legs/go2_fl'),
|
||||
PYTHONPATH=os.pathsep.join([str(ROOT / 'source/SkillFactory'), str(ROOT / 'scripts'),
|
||||
environment.get('PYTHONPATH', '')]))
|
||||
(out / 'native').mkdir()
|
||||
redirects = owner.native_redirects(out / 'native' / 'runtime', preparation=True)
|
||||
for p in redirects.values():
|
||||
Path(p).mkdir(parents=True, exist_ok=True)
|
||||
environment.update(redirects)
|
||||
policy = {'artifact_root': str(out), 'mount_identity': owner.mount_identity(out),
|
||||
'native_paths': owner.native_paths(out), 'shared_native_paths': owner.shared_native_paths(),
|
||||
'task_owned_cache_paths': sorted(redirects.values()), 'free_space_paths': owner.free_space_paths(out),
|
||||
'free_space_mount_identities': {p: owner.mount_identity(Path(p)) for p in owner.free_space_paths(out)},
|
||||
'check_interval_seconds': 1., 'min_free_bytes': 8 << 30,
|
||||
'max_native_growth_bytes': 16 << 30}
|
||||
write(out / 'native-policy.json', policy)
|
||||
monitor = owner.NativeMonitor(policy, SimpleNamespace(boottime=clock, STORAGE_SCOPE='four-skills-v050-monitored-not-quota'))
|
||||
child = capture = None
|
||||
errors = []
|
||||
clean = waited = False
|
||||
parent, channel = socket.socketpair()
|
||||
old_handlers = {}
|
||||
try:
|
||||
if ctypes.CDLL(None).prctl(36, 1, 0, 0, 0) != 0:
|
||||
raise RuntimeError('Subreaper setup failed')
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
old_handlers[sig] = signal.signal(sig, lambda *_: setattr(guard, 'interrupted', True))
|
||||
command = owner.launch_command([str(Path(__file__).resolve()), '--_worker-fd', str(channel.fileno())], environment)
|
||||
write(out / 'invocation.json', {'command': command, 'cwd': str(ROOT), 'launch_identity': owner.launch_identity()})
|
||||
child = subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT, start_new_session=True, pass_fds=(channel.fileno(),))
|
||||
owned.attach(child)
|
||||
channel.close()
|
||||
capture = owner.StdoutCapture(child.stdout, out / 'stdout.log', SimpleNamespace(STDOUT_BYTES=8 << 20))
|
||||
packet = json.dumps(payload).encode()
|
||||
parent.sendall(struct.pack('!I', len(packet)) + packet)
|
||||
parent.close()
|
||||
next_heartbeat = 0
|
||||
while child.poll() is None or not capture.eof:
|
||||
guard()
|
||||
capture.check()
|
||||
gpu.check(owned)
|
||||
monitor.check()
|
||||
if clock() >= next_heartbeat:
|
||||
write(out / 'supervisor-heartbeat.json', {'boottime': clock(), 'owned': sorted(owned.known),
|
||||
'gpu': gpu.clients().__str__(), 'deadline': payload['deadline_boottime']})
|
||||
next_heartbeat = clock() + 5
|
||||
time.sleep(.05)
|
||||
child.wait()
|
||||
waited = True
|
||||
if child.returncode != 0:
|
||||
errors.append('worker_nonzero:' + str(child.returncode))
|
||||
except BaseException:
|
||||
errors.append(traceback.format_exc())
|
||||
finally:
|
||||
parent.close()
|
||||
channel.close()
|
||||
try:
|
||||
if child is not None:
|
||||
if child.poll() is None and errors:
|
||||
# File cancellation survives SDK signal-handler changes;
|
||||
# allow Python evidence/checkpoint finalizers first.
|
||||
write(out / 'cancel.request', {'reason': errors[-1][-4096:]})
|
||||
until = min(clock() + 15, payload['deadline_boottime'] - 10)
|
||||
while child.poll() is None and clock() < until:
|
||||
if capture is not None:
|
||||
capture.pump()
|
||||
time.sleep(.05)
|
||||
clean = owner.cleanup_group(child, payload['deadline_boottime'], SimpleNamespace(boottime=clock),
|
||||
owned, lambda: capture.pump() if capture else None)
|
||||
child.wait(timeout=max(.1, payload['deadline_boottime'] - clock()))
|
||||
waited = True
|
||||
if capture is not None:
|
||||
capture.close()
|
||||
if capture.discarded or capture.error or not capture.eof:
|
||||
errors.append('incomplete_stdout')
|
||||
final_gpu = gpu.check(owned, final=True)
|
||||
guard(cleanup=True)
|
||||
monitor.check(force=True)
|
||||
except BaseException:
|
||||
errors.append('cleanup:' + traceback.format_exc())
|
||||
final_gpu = None
|
||||
worker = json.loads((out / 'worker-result.json').read_text()) if (out / 'worker-result.json').exists() else None
|
||||
if worker is not None and not worker.get('success'):
|
||||
errors.append('worker_reported_failure:' + str(worker.get('error', worker.get('close_error', 'incomplete')))[-4096:])
|
||||
success = bool(not errors and clean and waited and worker and worker.get('success')
|
||||
and worker.get('postclose_identity_verified') and child.returncode == 0)
|
||||
result = {'success': success, 'functional_success': bool(success and worker.get('functional_success')),
|
||||
'training_complete': bool(success and worker.get('training_complete')),
|
||||
'owned_cleanup': clean, 'waited_exit': waited,
|
||||
'returncode': None if child is None else child.returncode, 'errors': errors,
|
||||
'gpu_final': final_gpu, 'owned_identities': sorted(owned.known),
|
||||
'native_monitor': monitor.report(), 'accepted_skill': False}
|
||||
write(out / 'supervisor-result.json', result)
|
||||
for sig, handler in old_handlers.items():
|
||||
signal.signal(sig, handler)
|
||||
print(json.dumps({'output': str(out), 'success': success,
|
||||
'functional_success': result['functional_success'],
|
||||
'training_complete': result['training_complete'], 'errors': errors}), flush=True)
|
||||
return 0 if success else 1
|
||||
|
||||
|
||||
def supervisor_command_matches(command, cwd):
|
||||
if len(command) < 3:
|
||||
return False
|
||||
try:
|
||||
script = Path(command[1].decode())
|
||||
if not script.is_absolute():
|
||||
script = Path(cwd) / script
|
||||
return (script.resolve() == Path(__file__).resolve()
|
||||
and command[2].decode() in ('smoke', 'evaluate', 'train', 'gui', 'probe'))
|
||||
except (UnicodeError, OSError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def worker(fd):
|
||||
channel = socket.socket(fileno=fd)
|
||||
peer_pid, peer_uid, _ = struct.unpack('3i', channel.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12))
|
||||
import evaluate_cycle_sim_residual as owner
|
||||
command = Path(f'/proc/{peer_pid}/cmdline').read_bytes().split(b'\0')
|
||||
if (peer_uid != os.getuid() or not owner.ancestor(peer_pid, os.getpid())
|
||||
or Path(f'/proc/{peer_pid}/exe').resolve() != Path('/usr/bin/python3.12')
|
||||
or not supervisor_command_matches(command, Path(f'/proc/{peer_pid}/cwd').resolve())):
|
||||
raise PermissionError('Authenticated exact live sole ancestor supervisor required')
|
||||
channel.settimeout(30)
|
||||
def receive(n):
|
||||
chunks = b''
|
||||
while len(chunks) < n:
|
||||
part = channel.recv(n - len(chunks))
|
||||
if not part:
|
||||
raise EOFError('Incomplete parent handoff')
|
||||
chunks += part
|
||||
return chunks
|
||||
size = struct.unpack('!I', receive(4))[0]
|
||||
if size > 4 << 20:
|
||||
raise ValueError('Oversized handoff')
|
||||
payload = json.loads(receive(size))
|
||||
channel.close()
|
||||
if payload['parent_pid'] != peer_pid or payload['format'] != 'skillfactory.four-skills-run.v050':
|
||||
raise PermissionError('Wrong run identity')
|
||||
if payload != json.loads((Path(payload['output']) / 'approval.json').read_text()):
|
||||
raise PermissionError('Exact new approval required')
|
||||
claim = Path(payload['output']) / 'worker-claim'
|
||||
with claim.open('x'):
|
||||
pass
|
||||
guard = Guard(payload, worker=True)
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
signal.signal(sig, lambda *_: setattr(guard, 'interrupted', True))
|
||||
guard()
|
||||
from four_skills_runtime import run
|
||||
return run(payload, guard, write)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--_worker-fd', type=int, help=argparse.SUPPRESS)
|
||||
parser.add_argument('mode', nargs='?', choices=('smoke', 'evaluate', 'train', 'gui', 'probe', 'scope'))
|
||||
parser.add_argument('--name')
|
||||
parser.add_argument('--new-session', type=Path,
|
||||
help='Explicit fresh<=1h nontraining invocation; no old stage-grant reuse')
|
||||
parser.add_argument('--controller', choices=('analytic', 'trained'), default='analytic')
|
||||
parser.add_argument('--checkpoint', type=Path)
|
||||
parser.add_argument('--tier', choices=('none', 'small', 'medium', 'full'), default='none')
|
||||
parser.add_argument('--seed', type=int, default=115011)
|
||||
parser.add_argument('--sequence', type=int, choices=(0, 1, 2), default=0)
|
||||
parser.add_argument('--loops', type=int, choices=(1, 2, 3, 4, 5))
|
||||
parser.add_argument('--updates', type=int)
|
||||
parser.add_argument('--stop-skill', choices=('retract', 'swing'))
|
||||
args = parser.parse_args()
|
||||
if args._worker_fd is not None:
|
||||
return worker(args._worker_fd)
|
||||
if args.mode == 'scope':
|
||||
print(json.dumps(scope(), indent=2))
|
||||
return 0
|
||||
if args.mode is None or args.name is None:
|
||||
parser.error('Fresh --name and run mode required')
|
||||
if args.mode == 'train':
|
||||
args.seed = 105011
|
||||
if args.updates is not None and not 1 <= args.updates <= 256:
|
||||
parser.error('Updates in1..256')
|
||||
return supervise(args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,474 @@
|
||||
"""Authenticated Isaac worker for the additive four-skill stage.
|
||||
|
||||
Imported by four_skills.py only after private socket handoff. No old run grant,
|
||||
checkpoint identity, accepted flag or legacy task is reinterpreted.
|
||||
"""
|
||||
import importlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
TASK = 'SkillFactory.tasks.manager_based.four_skills.'
|
||||
LEG = 'SkillFactory.legs.four_skills.'
|
||||
|
||||
|
||||
def verify_project_imports():
|
||||
expected = ROOT / 'source/SkillFactory/SkillFactory'
|
||||
paths = {}
|
||||
for name, module in tuple(sys.modules.items()):
|
||||
if (name == 'SkillFactory' or name.startswith('SkillFactory.')) and getattr(module, '__file__', None):
|
||||
path = Path(module.__file__).resolve()
|
||||
if not path.is_relative_to(expected):
|
||||
raise RuntimeError('Imported project source outside current copy: ' + name)
|
||||
paths[name] = str(path)
|
||||
return paths
|
||||
|
||||
|
||||
def normalize_scene(cfg, binding):
|
||||
from SkillFactory.tasks.manager_based.four_skills.cfg import FourSkillsEnvCfg
|
||||
if type(cfg) is not FourSkillsEnvCfg or cfg.scene.num_envs not in (1, 128):
|
||||
raise ValueError('Exact new stage config, scalar or128envs required')
|
||||
if cfg.scene.clone_in_fabric is not False:
|
||||
raise ValueError('Non-fabric scene required')
|
||||
# Same pinned SDK normalization mechanics; independent new config identity,
|
||||
# not a fake RootXY instance or expanded old evaluator allowlist.
|
||||
for name in binding.NORMALIZATION_SOURCES:
|
||||
rel, expected = binding.ISAAC_PINS[name]
|
||||
p = binding._module_file(name)
|
||||
if p != binding.ISAAC_ROOT / rel or binding.sha256(p.read_bytes()) != expected:
|
||||
raise ValueError('Installed scene normalizer drift: ' + name)
|
||||
regex = '/World/envs/env_.*'
|
||||
for name in ('ground', 'light', 'robot', 'foot_contact', 'mount_wrench'):
|
||||
entity = getattr(cfg.scene, name)
|
||||
entity.prim_path = entity.prim_path.format(ENV_REGEX_NS=regex)
|
||||
if name in ('ground', 'light', 'robot'):
|
||||
entity.spawn.spawn_path = entity.prim_path.replace(regex, '/World/envs/env_0')
|
||||
elif name == 'foot_contact':
|
||||
for field in ('filter_prim_paths_expr', 'sensor_shape_prim_expr', 'filter_shape_prim_expr'):
|
||||
if hasattr(entity, field) and getattr(entity, field):
|
||||
setattr(entity, field, [p.format(ENV_REGEX_NS=regex) for p in getattr(entity, field)])
|
||||
for obj, name, value in binding._reference_updates(cfg):
|
||||
setattr(obj, name, value)
|
||||
binding.canonical_config(cfg)
|
||||
|
||||
|
||||
class Trace:
|
||||
def __init__(self, output, guard, *, full=True):
|
||||
self.output, self.guard, self.full = Path(output), guard, full
|
||||
self.frames = []
|
||||
self.initial = None
|
||||
self.maximum = {'limit_ratio': 0., 'joint_excess_rad': 0., 'height_error_m': 0.,
|
||||
'tilt_rad': 0., 'external_N': 0., 'guideZ_N': 0.}
|
||||
self.ticks = 0
|
||||
self.failure_frames = []
|
||||
|
||||
def snapshot(self, s):
|
||||
import torch
|
||||
phase, progress, target, _, fraction = s.reference()
|
||||
ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints] / s.effort).abs().amax(-1),
|
||||
(s.robot.data.joint_vel.torch[:, s.joints] / s.speed).abs().amax(-1))
|
||||
values = {'tick': s.substeps, 'time_s': s.time, 'skill': s.skill, 'age_s': s.age,
|
||||
'weight': s.weight, 'episode_id': torch.tensor(s.episode_ids, device=s.env.device),
|
||||
'phase': phase,
|
||||
'ready': s.ready, 'held_s': s.held, 'hold_required_s': s.hold_required,
|
||||
'hold_deadline_s': s.hold_deadline, 'hold_completed': s.hold_completed,
|
||||
'dwell_s': s.dwell, 'bad_hold_s': s.bad_hold,
|
||||
'unload_passed': s.unload_passed, 'unload_min_N': s.unload_min,
|
||||
'unload_initial_N': s.unload_initial, 'failed': s.failed,
|
||||
'root': torch.cat(s.root(), -1), 'joint_pos': s.q(),
|
||||
'joint_vel': s.robot.data.joint_vel.torch[:, s.joints],
|
||||
'applied_torque': s.robot.data.applied_torque.torch[:, s.joints],
|
||||
'foot': s.foot_center(), 'normal': s.contact(),
|
||||
'gap': s.foot_center()[:, 2] - s.bundle['foot']['radius'],
|
||||
'target': target, 'progress': progress, 'fixture_fraction': fraction,
|
||||
'external': s.external, 'guide': s.guide, 'auxiliary': s.auxiliary,
|
||||
'reference': s.controller.action, 'q_ik': s.controller.q_ik,
|
||||
'residual': s.residual_request, 'effective_residual_rad': s.effective_residual_rad,
|
||||
'actor_observation': s.policy(), 'limit_ratio': ratio,
|
||||
'joint_excess': torch.maximum(s.lower - s.q(), s.q() - s.upper).clamp(min=0).amax(-1),
|
||||
'height_error': (s.root()[0][:, 2] - s.base_origin[:, 2]).abs(),
|
||||
'tilt': s.tilt()}
|
||||
return {k: v.detach().cpu().clone() for k, v in values.items()}
|
||||
|
||||
def __call__(self, s):
|
||||
import torch
|
||||
# Training keeps aggregates and complete first offending snapshots,
|
||||
# evaluation retains every500Hz original tensor with no thinning.
|
||||
if self.full or bool(s.failed.any()):
|
||||
frame = self.snapshot(s)
|
||||
if self.full:
|
||||
self.frames.append(frame)
|
||||
elif len(self.failure_frames) < 256:
|
||||
self.failure_frames.append(frame)
|
||||
self.ticks += 1
|
||||
if self.full:
|
||||
values = self.frames[-1]
|
||||
for dest, src in [('limit_ratio', 'limit_ratio'), ('joint_excess_rad', 'joint_excess'),
|
||||
('height_error_m', 'height_error'), ('tilt_rad', 'tilt')]:
|
||||
self.maximum[dest] = max(self.maximum[dest], float(values[src].max()))
|
||||
self.maximum['external_N'] = max(self.maximum['external_N'], float(values['external'][..., :3].norm(dim=-1).max()))
|
||||
self.maximum['guideZ_N'] = max(self.maximum['guideZ_N'], float(values['guide'][..., 2].abs().max()))
|
||||
|
||||
def save(self):
|
||||
import torch
|
||||
self.guard(cleanup=True)
|
||||
if self.initial is not None:
|
||||
torch.save(self.initial, self.output / 'initial-state.pt')
|
||||
if self.frames:
|
||||
torch.save({k: torch.stack([f[k] for f in self.frames]) for k in self.frames[0]}, self.output / 'trace.pt')
|
||||
if self.failure_frames:
|
||||
torch.save(self.failure_frames, self.output / 'training-failure-frames.pt')
|
||||
|
||||
|
||||
class TrainingComposer:
|
||||
def __init__(self, env):
|
||||
import torch
|
||||
self.env = env
|
||||
self.loops = torch.zeros(env.num_envs, dtype=torch.long, device=env.device)
|
||||
self.episodes = list(env.s.episode_ids)
|
||||
self.skill_counts = [0] * 4
|
||||
self.transition_count = 0
|
||||
|
||||
def update(self):
|
||||
import torch
|
||||
s, service = self.env.s, self.env.service
|
||||
for i, episode in enumerate(s.episode_ids):
|
||||
if episode != self.episodes[i]:
|
||||
self.loops[i] = 0
|
||||
self.episodes[i] = episode
|
||||
allowed = s.ready & (s.held + 1e-6 >= s.hold_required) & ~s.failed & ~s.stopped
|
||||
kinds = s.skill.clone() # one decision per row, even after service mutation
|
||||
# Only public skill calls mutate reference goals. No force arguments.
|
||||
for skill in range(4):
|
||||
ids = (allowed & (kinds == skill)).nonzero().flatten()
|
||||
self.skill_counts[skill] += int((kinds == skill).sum())
|
||||
if not len(ids):
|
||||
continue
|
||||
next_skill = ('retract', 'swing', 'plant', 'support')[skill]
|
||||
self.transition_count += len(ids)
|
||||
if skill == 1:
|
||||
for variant, vector in enumerate(((.024, .010), (.020, .024), (.010, .024))):
|
||||
for sign in (1, -1):
|
||||
selected = ids[(ids % 3 == variant) & (torch.where(self.loops[ids] % 2 == 0, 1, -1) == sign)]
|
||||
if len(selected):
|
||||
service.call('swing', hold_seconds=(.3, .6, 1.)[variant],
|
||||
direction_m=[sign*x for x in vector], env_ids=selected)
|
||||
else:
|
||||
hold = (.4, 0., .5, .8)[skill]
|
||||
service.call(next_skill, hold_seconds=hold, env_ids=ids)
|
||||
if skill == 3:
|
||||
self.loops[ids] += 1
|
||||
|
||||
|
||||
def reject_probe(env):
|
||||
import torch
|
||||
def snapshot():
|
||||
s = env.s
|
||||
values = [s.q(), *s.root(), s.controller.q_ik, s.controller.action, s.skill, s.substeps,
|
||||
s.age, s.held, s.hold_required, s.ground_target, s.controller.refreshes,
|
||||
torch.get_rng_state(), *torch.cuda.get_rng_state_all()]
|
||||
return [v.detach().cpu().clone() for v in values]
|
||||
before = snapshot()
|
||||
counts = (env.runtime_hooks.completed_steps, env.runtime_hooks.reset_calls, env.runtime_hooks.pose_write_calls)
|
||||
rejected = False
|
||||
reason = None
|
||||
try:
|
||||
env.service.call('plant') # support->plant is illegal, even if ready.
|
||||
except ValueError as error:
|
||||
rejected, reason = True, str(error)
|
||||
after = snapshot()
|
||||
unchanged = all(torch.equal(a, b) for a, b in zip(before, after)) and counts == (
|
||||
env.runtime_hooks.completed_steps, env.runtime_hooks.reset_calls, env.runtime_hooks.pose_write_calls)
|
||||
if not rejected or not unchanged:
|
||||
raise RuntimeError('Illegal public switch was not rejected atomically')
|
||||
return {'rejected': rejected, 'reason': reason, 'carried_state_and_rng_equal': unchanged,
|
||||
'physical_calls_added': 0}
|
||||
|
||||
|
||||
def execute_single(env, payload, output, guard, trace, model=None):
|
||||
import torch
|
||||
from SkillFactory.legs.four_skills.api import CentralComposer
|
||||
config = env.cfg.four_parameters
|
||||
sequence = dict(config['sequences'][payload['sequence']])
|
||||
if payload['mode'] == 'smoke':
|
||||
sequence['loops'] = 1
|
||||
if payload.get('loops') is not None:
|
||||
sequence['loops'] = payload['loops']
|
||||
composer = CentralComposer(env.service, sequence)
|
||||
observation = env.reset(seed=payload['seed'])[0]['policy']
|
||||
trace.initial = trace.snapshot(env.s)
|
||||
if payload['mode'] == 'probe':
|
||||
(output / 'illegal-switch-probe.json').write_text(json.dumps(reject_probe(env), indent=2))
|
||||
initial_counts = {'resets': env.runtime_hooks.reset_calls, 'pose_writes': env.runtime_hooks.pose_write_calls}
|
||||
(output / 'force-schedule.json').write_text(json.dumps(env.s.forces.manifest(), indent=2))
|
||||
(output / 'initial-counters.json').write_text(json.dumps(initial_counts))
|
||||
outcome = {'functional_success': False, 'requested_loops': sequence['loops'], 'completed_loops': 0}
|
||||
try:
|
||||
while env.s.time[0] < 240:
|
||||
guard()
|
||||
status = env.service.query()
|
||||
if (payload.get('stop_skill') == status['skill'] and status['ready'] and not status['stop_requested']):
|
||||
env.service.request_stop()
|
||||
finished = composer.update()
|
||||
env.central_progress = {'completed_loops': composer.completed_loops, 'requested_loops': composer.loops,
|
||||
'stop_requested': env.service.query()['stop_requested']}
|
||||
if finished:
|
||||
outcome['functional_success'] = not composer.safely_stopped
|
||||
outcome['safe_stop_success'] = composer.safely_stopped
|
||||
break
|
||||
# The public switch may have changed skill/reference at this same
|
||||
# physical clock; actor must consume the post-command observation.
|
||||
observation = env.s.policy()
|
||||
with torch.no_grad():
|
||||
action = torch.zeros(1, 3, device=env.device) if model is None else model.act(observation, deterministic=True)[0]
|
||||
observation, _, terminated, _, _ = env.step(action)
|
||||
observation = observation['policy']
|
||||
if bool(terminated.any()):
|
||||
break
|
||||
outcome.update(completed_loops=composer.completed_loops, status=env.service.query(),
|
||||
transitions=composer.transitions,
|
||||
actual_ticks=env.total_ticks, maximum=trace.maximum,
|
||||
initialization_counters=initial_counts,
|
||||
final_counters={'resets': env.runtime_hooks.reset_calls,
|
||||
'pose_writes': env.runtime_hooks.pose_write_calls})
|
||||
outcome['no_rollout_reset_or_pose_write'] = initial_counts == outcome['final_counters']
|
||||
outcome['functional_success'] &= outcome['no_rollout_reset_or_pose_write']
|
||||
if outcome.get('safe_stop_success'):
|
||||
outcome['safe_stop_success'] &= (outcome['no_rollout_reset_or_pose_write']
|
||||
and outcome['status']['skill'] == 'support'
|
||||
and outcome['status']['ready'] and outcome['status']['stopped'])
|
||||
return outcome
|
||||
finally:
|
||||
trace.save()
|
||||
(output / 'transitions.json').write_text(json.dumps(composer.transitions, indent=2))
|
||||
(output / 'public-call-receipts.json').write_text(json.dumps(env.s.transitions, indent=2))
|
||||
|
||||
|
||||
def train(env, payload, output, guard, trace):
|
||||
import torch
|
||||
from SkillFactory.legs.four_skills.model import FourSkillActorCritic, save_checkpoint
|
||||
from SkillFactory.legs.standing_ppo import ppo_step
|
||||
torch.set_num_threads(4)
|
||||
cfg = env.cfg.four_parameters['training']
|
||||
torch.manual_seed(cfg['seed'])
|
||||
torch.cuda.manual_seed_all(cfg['seed'])
|
||||
model = FourSkillActorCritic().to(env.device)
|
||||
initial_actor = {k: v.detach().clone() for k, v in model.actor.named_parameters()}
|
||||
opt = torch.optim.Adam(model.parameters(), lr=cfg['algorithm']['learning_rate'], eps=cfg['algorithm']['adam_eps'])
|
||||
identity = {'task': env.cfg.four_parameters['task_id'], 'config_sha256': payload['config_sha256'],
|
||||
'source_sha256': payload['sources'], 'train_seed': cfg['seed'],
|
||||
'observation': 'four-skills-48-fixed-si-v1', 'device': torch.cuda.get_device_name(0)}
|
||||
save_checkpoint(output / 'untrained.pt', model, opt, identity, 0)
|
||||
x = env.reset(seed=cfg['seed'])[0]['policy']
|
||||
trace.initial = trace.snapshot(env.s)
|
||||
driver = TrainingComposer(env)
|
||||
driver.update()
|
||||
failures = {k: 0 for k in env.s.failure_masks}
|
||||
history = []
|
||||
total_transitions = 0
|
||||
skill_control_samples = [0] * 4
|
||||
update_limit = payload.get('updates') or cfg['updates']
|
||||
for update in range(1, update_limit + 1):
|
||||
guard()
|
||||
tier = next(t for start, end, t in cfg['tier_updates'] if start <= update <= end)
|
||||
if tier != env.s.force_tier:
|
||||
env.s.force_tier = tier
|
||||
# New training episodes at explicit curriculum boundary, not a hold
|
||||
# reset, and not a paired independent evaluation.
|
||||
x = env.reset()[0]['policy']
|
||||
data = {key: torch.empty(cfg['horizon'], env.num_envs, *shape, device=env.device, dtype=dtype)
|
||||
for key, shape, dtype in [('features', (48,), torch.float32), ('latent', (3,), torch.float32),
|
||||
('log_prob', (), torch.float32), ('value', (), torch.float32), ('reward', (), torch.float32),
|
||||
('next_value', (), torch.float32), ('done', (), torch.bool)]}
|
||||
update_failures = 0
|
||||
with torch.no_grad():
|
||||
for step in range(cfg['horizon']):
|
||||
guard()
|
||||
x = env.s.policy() # already post-command from previous boundary.
|
||||
for k, n in enumerate(torch.bincount(env.s.skill, minlength=4).cpu().tolist()):
|
||||
skill_control_samples[k] += n
|
||||
action, latent, logp, value = model.act(x)
|
||||
obs, reward, terminated, truncated, info = env.step(action)
|
||||
# Advance exactly once, then bootstrap the very state that the
|
||||
# next actor will see. Terminal snapshots remain pre-reset.
|
||||
driver.update()
|
||||
nx = env.s.policy()
|
||||
nv = model.value(nx)
|
||||
timeout = truncated & ~terminated
|
||||
if bool(timeout.any()):
|
||||
nv[timeout] = model.value(info['terminal_observation']['policy'][timeout])
|
||||
nv[terminated] = 0
|
||||
for key, v in [('features', x), ('latent', latent), ('log_prob', logp), ('value', value),
|
||||
('reward', reward), ('next_value', nv), ('done', terminated | truncated)]:
|
||||
data[key][step].copy_(v)
|
||||
for reason, count in info['failure_counts'].items():
|
||||
failures[reason] = failures.get(reason, 0) + count
|
||||
update_failures += int(terminated.sum())
|
||||
x = nx
|
||||
guard()
|
||||
# Physical rollout only, GPU optimizer only. No saved robot trace replay.
|
||||
metrics = ppo_step(model, opt, data, cfg['algorithm'])
|
||||
total_transitions += cfg['horizon'] * env.num_envs
|
||||
row = {'update': update, 'tier': tier, 'environment_transitions': total_transitions,
|
||||
'actual_physics_calls': env.total_ticks, 'episode_failures': update_failures,
|
||||
'mean_reward': float(data['reward'].mean()), 'learner': metrics,
|
||||
'skill_control_samples': skill_control_samples[:],
|
||||
'feedback_skill_samples': driver.skill_counts[:], 'transitions': driver.transition_count,
|
||||
'state_diagnostics': {'current_skill_counts': torch.bincount(env.s.skill, minlength=4).cpu().tolist(),
|
||||
'ready_count': int(env.s.ready.sum()), 'max_age_s': float(env.s.age.max()),
|
||||
'max_held_s': float(env.s.held.max()), 'min_gap_m': float((env.s.foot_center()[:, 2]-env.s.bundle['foot']['radius']).min())}}
|
||||
history.append(row)
|
||||
with (output / 'progress.jsonl').open('a') as stream:
|
||||
stream.write(json.dumps(row, allow_nan=False) + '\n')
|
||||
save_checkpoint(output / 'latest.pt', model, opt, identity, update, row)
|
||||
if update % cfg['checkpoint_interval'] == 0 or update == update_limit:
|
||||
save_checkpoint(output / f'checkpoint_u{update:04d}.pt', model, opt, identity, update, row)
|
||||
torch.save(trace.snapshot(env.s), output / f'state_u{update:04d}.pt')
|
||||
print(json.dumps({'training': row}), flush=True)
|
||||
changed = {k: float((v.detach() - initial_actor[k]).abs().max()) for k, v in model.actor.named_parameters()}
|
||||
trace.save()
|
||||
return {'training_complete': True, 'updates': len(history), 'optimizer_device': env.device,
|
||||
'environment_transitions': total_transitions, 'actual_ticks': env.total_ticks,
|
||||
'failures': failures, 'actor_parameter_max_changes': changed,
|
||||
'skill_control_samples': skill_control_samples, 'feedback_skill_samples': driver.skill_counts,
|
||||
'transition_count': driver.transition_count,
|
||||
'candidate_selection_pending_development_validation': True, 'functional_success': False}
|
||||
|
||||
|
||||
def run(payload, guard, write):
|
||||
import torch
|
||||
from isaaclab.app import AppLauncher
|
||||
output = Path(payload['output'])
|
||||
gui = payload['mode'] == 'gui'
|
||||
launcher = app = env = hooks = paint = trace = None
|
||||
result = {'success': False, 'functional_success': False, 'mode': payload['mode']}
|
||||
code = 1
|
||||
try:
|
||||
options = {'visualizer': 'kit'} if gui else {}
|
||||
launcher = AppLauncher(headless=not gui, device='cuda:0', fast_shutdown=True,
|
||||
kit_args=f'--/log/file={output / "native" / "kit.log"}', **options)
|
||||
app = launcher.app
|
||||
import evaluate_cycle_sim_residual as owner
|
||||
from SkillFactory.legs.contact_cycle_sim import evaluation_binding as binding
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_env import RuntimeHooks
|
||||
from SkillFactory.tasks.manager_based.four_skills.cfg import FourSkillsEnvCfg
|
||||
from SkillFactory.tasks.manager_based.four_skills.env import FourSkillsEnv
|
||||
for name in dict.fromkeys((*binding.REQUIRED_RUNTIME, *binding.ISAAC_PINS)):
|
||||
importlib.import_module(name)
|
||||
verify_project_imports()
|
||||
owner.preimport_replicator()
|
||||
owner.preimport_constructor_modules(binding)
|
||||
owner.physx_constructor_export(binding, resolve=True)
|
||||
if torch.cuda.get_device_name(0) != 'NVIDIA GeForce RTX 5080':
|
||||
raise RuntimeError('Only approved RTX5080, no CPU training fallback')
|
||||
cfg = FourSkillsEnvCfg()
|
||||
cfg.scene.num_envs = 128 if payload['mode'] == 'train' else 1
|
||||
cfg.training_mode = payload['mode'] == 'train'
|
||||
cfg.force_tier = payload['tier']
|
||||
cfg.seed = payload['seed']
|
||||
cfg.sim.device = 'cuda:0'
|
||||
if gui:
|
||||
from isaaclab_visualizers.kit import KitVisualizerCfg
|
||||
cfg.sim.visualizer_cfgs = [KitVisualizerCfg()]
|
||||
owner.preforward_video_recorder(cfg, importlib.import_module(owner.TASK + 'evaluation_env'))
|
||||
owner.preimport_config_references(cfg, binding)
|
||||
normalize_scene(cfg, binding)
|
||||
before = binding.canonical_config(cfg)
|
||||
(output / 'effective-config-before.json').write_bytes(before)
|
||||
trace = Trace(output, guard, full=not cfg.training_mode)
|
||||
def before_step(h):
|
||||
guard()
|
||||
if h.attempted_steps > payload['max_physics_calls']:
|
||||
raise RuntimeError('Physical call cap exhausted before call')
|
||||
with RuntimeHooks.isaac(before_step=before_step) as hooks:
|
||||
env = FourSkillsEnv(cfg, runtime_hooks=hooks, guard=guard, record=trace)
|
||||
write(output / 'runtime-identity.json', {
|
||||
'task': cfg.four_parameters['task_id'], 'torch': str(torch.__version__),
|
||||
'device': torch.cuda.get_device_name(0), 'num_envs': env.num_envs,
|
||||
'physics_dt_s': env.physics_dt, 'decimation': cfg.decimation,
|
||||
'payload_body_index': env.s.payload, 'body_names': list(env.s.robot.body_names),
|
||||
'joint_order': list(env.s.bundle['joint_order']), 'fixed_base': bool(env.s.robot.is_fixed_base),
|
||||
'effective_weight_N': env.s.weight.cpu().tolist(),
|
||||
'auxiliary_parameters': cfg.cycle_parameters['support'],
|
||||
'guide_stiffness': cfg.support_parameters['guide_stiffness'],
|
||||
'guide_damping': cfg.support_parameters['guide_damping'],
|
||||
'guide_wrench_limit': cfg.support_parameters['guide_wrench_limit']})
|
||||
actual = binding.canonical_config(cfg)
|
||||
(output / 'effective-config-actual.json').write_bytes(actual)
|
||||
if before != actual:
|
||||
raise RuntimeError('Constructor changed effective bound config')
|
||||
if gui:
|
||||
from SkillFactory.tasks.manager_based.four_skills.gui import Presentation
|
||||
paint = Presentation(app, env, output)
|
||||
paint.controller_label = payload['controller']
|
||||
env.paint = paint
|
||||
model = None
|
||||
if payload.get('checkpoint'):
|
||||
from SkillFactory.legs.four_skills.model import load_policy
|
||||
model, ckpt = load_policy(payload['checkpoint'], 'cuda:0', payload['checkpoint_sha256'])
|
||||
if (ckpt['identity'].get('config_sha256') != payload['config_sha256']
|
||||
or ckpt['identity'].get('task') != cfg.four_parameters['task_id']):
|
||||
raise RuntimeError('Frozen new-stage checkpoint/config/task identity mismatch')
|
||||
write(output / 'checkpoint-identity.json', {'sha256': payload['checkpoint_sha256'],
|
||||
'update': ckpt['update'], 'identity': ckpt['identity']})
|
||||
if cfg.training_mode:
|
||||
result.update(train(env, payload, output, guard, trace))
|
||||
else:
|
||||
result.update(execute_single(env, payload, output, guard, trace, model))
|
||||
guard()
|
||||
write(output / 'runtime-project-imports.json', verify_project_imports())
|
||||
if binding.canonical_config(env.cfg) != actual or env.cfg is not cfg:
|
||||
raise RuntimeError('Actual config changed during rollout')
|
||||
if gui:
|
||||
paint.finish(result['functional_success'], safely_stopped=result.get('safe_stop_success', False))
|
||||
result['gui'] = paint.report()
|
||||
result['physics_counts'] = {'attempted': hooks.attempted_steps, 'completed': hooks.completed_steps,
|
||||
'rollout': hooks.rollout_steps, 'initialization': hooks.initialization_steps,
|
||||
'resets': hooks.reset_calls, 'pose_writes': hooks.pose_write_calls,
|
||||
'violations': hooks.violations}
|
||||
if hooks.violations:
|
||||
raise RuntimeError('Runtime reset/pose/ownership violation')
|
||||
torch.save(trace.snapshot(env.s), output / 'terminal-state.pt')
|
||||
env.close()
|
||||
env = None
|
||||
result['success'] = True # integrity only; task function is separate.
|
||||
result['postclose_identity_verified'] = True
|
||||
code = 0
|
||||
except BaseException:
|
||||
result.update(success=False, error=traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
if hooks is not None:
|
||||
result['physics_counts'] = {'attempted': hooks.attempted_steps, 'completed': hooks.completed_steps,
|
||||
'rollout': hooks.rollout_steps, 'initialization': hooks.initialization_steps,
|
||||
'resets': hooks.reset_calls, 'pose_writes': hooks.pose_write_calls,
|
||||
'violations': hooks.violations}
|
||||
if trace is not None:
|
||||
try:
|
||||
trace.save()
|
||||
except BaseException:
|
||||
result.update(success=False, trace_save_error=traceback.format_exc())
|
||||
code = 1
|
||||
if env is not None:
|
||||
try:
|
||||
if trace is not None:
|
||||
torch.save(trace.snapshot(env.s), output / 'terminal-state.pt')
|
||||
env.close()
|
||||
except BaseException:
|
||||
result.update(success=False, close_error=traceback.format_exc())
|
||||
code = 1
|
||||
if paint is not None:
|
||||
try:
|
||||
paint.release()
|
||||
except BaseException:
|
||||
result.update(success=False, gui_release_error=traceback.format_exc())
|
||||
code = 1
|
||||
write(output / 'worker-result.json', result)
|
||||
if app is not None:
|
||||
app.close(exit_code=code)
|
||||
return code
|
||||
Reference in New Issue
Block a user