"""Pinned PiPER/SO101 CPU mechanics smoke tests, without hardware control. Fetch is the only network operation. Audit, native stepping and inspection are offline. Simulation asset licenses do not establish the openness of hardware. """ from __future__ import annotations import argparse from hashlib import sha256 import html import json import math from pathlib import Path, PurePosixPath import platform import time from urllib.request import Request, urlopen from . import reference_sim as sim LOCK_PATH = Path(__file__).with_name('robot_profiles.lock.json') SCHEMA = 'dvidia.robot-profile-benchmark/v1' INTEGRITY_SCHEMA = 'dvidia.robot-profile-integrity/v1' CASES = ('hold', 'arm_sweep', 'gripper_cycle') SCOPE = ('Unloaded native CPU mechanics smoke: authored position servos, upstream gravity settings, ' 'privileged joint/link state and synthetic commands. No object task, learned skill, measured ' 'hardware response, contact calibration or physical robot qualification.') def _lock(): return json.loads(LOCK_PATH.read_text(encoding='utf-8')) def _profile(name): for item in _lock()['profiles']: if item['id'] == name: return item raise ValueError('Profile must be piper or so101') def list_profiles(): """Return explicit axes, units and asset-license claims without native imports.""" return [{key: value for key, value in item.items() if key != 'files'} for item in _lock()['profiles']] def _payload(profile, directory): item, root = _profile(profile), Path(directory) if root.is_symlink() or not root.is_dir(): raise ValueError('Assets require an existing non-symlink directory') blobs = {} for record in item['files']: path = root / record['path'] if any(parent.is_symlink() for parent in path.parents if parent != root and root in parent.parents): raise ValueError('Asset subdirectories must not be symlinks') content = sim._regular_bytes(path, record['bytes']) if sha256(content).hexdigest() != record['sha256']: raise ValueError('Asset digest mismatch: '+record['path']) blobs[record['path']] = content return item, blobs def verify_assets(profile, directory): item, blobs = _payload(profile, directory) return {'profile': item['id'], 'repository': _lock()['repository'], 'commit': _lock()['commit'], 'files_verified': len(blobs), 'bytes_verified': sum(map(len, blobs.values())), 'lock_sha256': sha256(LOCK_PATH.read_bytes()).hexdigest(), 'simulation_asset_license': item['simulation_asset_license']} def fetch_assets(profile, output): """Explicitly fetch the fixed packaged allowlist into a fresh asset root.""" item, lock = _profile(profile), _lock() destination = Path(output) if destination.is_symlink() or (destination.exists() and not destination.is_dir()): raise ValueError('Asset root must be a plain directory') destination.mkdir(parents=True, exist_ok=True) (destination/item['folder']).mkdir(exist_ok=False) for record in item['files']: url = f"https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/{lock['commit']}/{record['path']}" with urlopen(Request(url, headers={'User-Agent': 'DVIDIA-Robot-Sim/0.1'}), timeout=30) as response: if response.geturl() != url: raise ValueError('Pinned asset download redirected') content = response.read(record['bytes']+1) if len(content) != record['bytes'] or sha256(content).hexdigest() != record['sha256']: raise ValueError('Downloaded asset differs from packaged lock: '+record['path']) path = destination / record['path'] path.parent.mkdir(parents=True, exist_ok=True) with path.open('xb') as stream: stream.write(content) return verify_assets(profile, destination) def build_model(profile, directory, *, timestep=.001): dt = sim._timestep(timestep) item, blobs = _payload(profile, directory) model_spec = sim._spec(blobs, item['folder'], item['model']) model_spec.option.timestep = dt model = model_spec.compile() mj, _ = sim._native_imports() actual = {name: int(getattr(model, name)) for name in ('nq', 'nv', 'nu', 'neq')} if actual != item['expected_dimensions']: raise ValueError('Native profile mechanical dimensions differ from the pinned contract') if [mj.mj_id2name(model, mj.mjtObj.mjOBJ_ACTUATOR, i) for i in range(model.nu)] != item['actuator_names']: raise ValueError('Native profile actuator order differs from the pinned contract') expected_joints = item['arm_joint_names']+item['gripper_joint_names'] if [mj.mj_id2name(model, mj.mjtObj.mjOBJ_JOINT, i) for i in range(model.njnt)] != expected_joints: raise ValueError('Native profile joint order differs from the pinned contract') if mj.mj_name2id(model, mj.mjtObj.mjOBJ_BODY, item['tool_frame_body']) <= 0: raise ValueError('Native profile terminal link body is missing') for index, unit in enumerate(item['native_command_units']): joint = int(model.actuator_trnid[index, 0]) expected = mj.mjtJoint.mjJNT_HINGE if unit == 'rad' else mj.mjtJoint.mjJNT_SLIDE if model.jnt_type[joint] != expected or not model.actuator_ctrllimited[index]: raise ValueError('Native command units/limits differ from the pinned contract') return model def audit(profile, directory, *, timestep=.001): metadata = verify_assets(profile, directory) messages = [] with sim.native_warnings(messages): model = build_model(profile, directory, timestep=timestep) if messages: raise ValueError('Native profile compilation emitted warnings: '+str(messages)) mj, _ = sim._native_imports() return {'schema': 'dvidia.robot-profile-audit/v1', 'profile': list_profiles()[0 if profile == 'piper' else 1], 'assets': metadata, 'native_version': mj.__version__, 'compiled_model_sha256': sim.model_digest(model), 'dimensions': {name: int(getattr(model, name)) for name in ('nq', 'nv', 'nu', 'neq', 'ngeom', 'nbody')}, 'actuator_ctrlrange_native': model.actuator_ctrlrange.tolist(), 'actuator_forcerange_native': model.actuator_forcerange.tolist(), 'gravity_compensated_bodies': int(sum(model.body_gravcomp != 0)), 'scope': SCOPE, 'task_qualified': False, 'physical_robot_ready': False} def _bounded(value, name, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)): raise ValueError(name+' must be finite numeric data') try: number = float(value) except (OverflowError, ValueError, TypeError) as exc: raise ValueError(name+' must be finite numeric data') from exc if not math.isfinite(number) or not low <= number <= high: raise ValueError(name+' outside bounded range') return number def _duration(value): number = _bounded(value, 'duration', .1, 2.) if not math.isclose(number/sim.CONTROL_DT, round(number/sim.CONTROL_DT), abs_tol=1e-9, rel_tol=0): raise ValueError('Duration must contain complete 20 ms control boundaries') return number def _sample(model, data, item, elapsed): mj, np = sim._native_imports() if not all(np.isfinite(value).all() for value in (data.qpos, data.qvel, data.qacc, data.xpos)): raise RuntimeError('Nonfinite native profile state') body = mj.mj_name2id(model, mj.mjtObj.mjOBJ_BODY, item['tool_frame_body']) return {'t_seconds': elapsed, 'qpos_native': data.qpos.tolist(), 'qvel_native_per_second': data.qvel.tolist(), 'arm_joint_position_rad': data.qpos[:item['arm_axes']].tolist(), 'arm_joint_velocity_rad_s': data.qvel[:item['arm_axes']].tolist(), 'gripper_joint_position_native': data.qpos[item['arm_axes']:].tolist(), 'terminal_link_position_m': data.xpos[body].tolist(), 'terminal_link_quaternion_wxyz': data.xquat[body].tolist(), 'actuator_force_native': data.actuator_force.tolist(), 'contact_count': int(data.ncon), 'issued_controls_native': None, 'observation_origin': 'privileged_native_simulator_state'} def _attempt(profile, assets, case, repeat, duration, timestep): started, latencies = time.perf_counter(), [] item = _profile(profile) result = {'id': f'{case}-{repeat}', 'case': case, 'repeat': repeat, 'valid': False, 'error': None, 'warnings': [], 'simulated_seconds': 0., 'trajectory': [], 'timing': {}, 'metrics': {}} try: with sim.native_warnings(result['warnings']): model = build_model(profile, assets, timestep=timestep) mj, np = sim._native_imports() data = mj.MjData(model) if model.nkey: mj.mj_resetDataKeyframe(model, data, 0) else: data.ctrl[:] = data.qpos[model.jnt_qposadr[model.actuator_trnid[:, 0]]] base = data.ctrl.copy() mj.mj_forward(model, data) result['compiled_model_sha256'] = sim.model_digest(model) result['runtime'] = {'mujoco_version': mj.__version__, 'numpy_version': np.__version__} result['native_model_settings'] = {'gravity_m_s2': model.opt.gravity.tolist(), 'gravity_compensated_bodies': [ {'name': mj.mj_id2name(model, mj.mjtObj.mjOBJ_BODY, i), 'fraction': float(model.body_gravcomp[i])} for i in range(model.nbody) if model.body_gravcomp[i] != 0]} result['trajectory'].append(_sample(model, data, item, 0.)) for index in range(round(duration/sim.CONTROL_DT)): tick_started = time.perf_counter() commands = base.copy() phase = math.sin(math.pi*(index+1)*sim.CONTROL_DT/duration) if case == 'arm_sweep': commands[:item['arm_axes']] += .08*phase elif case == 'gripper_cycle': commands[-1] += (.012 if item['id'] == 'piper' else .15)*phase commands = np.clip(commands, model.actuator_ctrlrange[:, 0], model.actuator_ctrlrange[:, 1]) result['trajectory'][-1]['issued_controls_native'] = commands.tolist() data.ctrl[:] = commands for _ in range(round(sim.CONTROL_DT/timestep)): mj.mj_step(model, data) mj.mj_forward(model, data) _sample(model, data, item, 0.) if result['warnings'] or any(warning.number for warning in data.warning): raise RuntimeError('Native profile emitted a solver warning') result['simulated_seconds'] = (index+1)*sim.CONTROL_DT result['trajectory'].append(_sample(model, data, item, result['simulated_seconds'])) latencies.append(time.perf_counter()-tick_started) result['valid'] = not result['warnings'] errors = [abs(row['qpos_native'][int(model.jnt_qposadr[model.actuator_trnid[j, 0]])]-row['issued_controls_native'][j]) for row in result['trajectory'][:-1] for j in range(item['arm_axes'])] result['metrics'] = {'max_before_step_arm_target_error_rad': max(errors, default=0.), 'max_recorded_contact_count': max(row['contact_count'] for row in result['trajectory']), 'scope': 'Numerical state/command diagnostics only; no task success metric'} except Exception as exc: result['error'] = {'type': type(exc).__name__, 'message': str(exc)[:500]} elapsed = time.perf_counter()-started result['timing'] = {'episode_wall_seconds': elapsed, 'control_step_seconds': sim._summary(latencies) if latencies else None, 'control_boundaries': len(latencies), 'missed_control_boundaries': sum(x > sim.CONTROL_DT for x in latencies), 'simulated_seconds_per_wall_second': result['simulated_seconds']/elapsed, 'scope': 'Wall computation including compilation/state snapshots; not real-time sensor-to-actuator latency'} return result def _json_bytes(value): return (json.dumps(value, allow_nan=False, indent=2)+'\n').encode() def _record(path, content): return {'path': path, 'bytes': len(content), 'sha256': sha256(content).hexdigest()} def benchmark(profile, assets, output, *, duration=.4, timestep=.001, repeats=1): """Seal every bounded unloaded attempt, including numerical/setup failures.""" item, duration, timestep = _profile(profile), _duration(duration), sim._timestep(timestep) if type(repeats) is not int or not 1 <= repeats <= 3: raise ValueError('repeats must be an integer in [1,3]') metadata, blobs = verify_assets(profile, assets), _payload(profile, assets)[1] destination = Path(output) destination.mkdir(parents=True, exist_ok=False) report = {'schema': SCHEMA, 'profile': {key: value for key, value in item.items() if key != 'files'}, 'scope': SCOPE, 'task_qualified': False, 'physical_robot_ready': False, 'assets': metadata, 'source': {'implementation_sha256': sha256(Path(__file__).read_bytes()).hexdigest(), 'reference_sim_sha256': sha256(Path(sim.__file__).read_bytes()).hexdigest(), 'lock_sha256': sha256(LOCK_PATH.read_bytes()).hexdigest(), 'asset_commit': _lock()['commit']}, 'settings': {'duration_seconds': duration, 'timestep_seconds': timestep, 'control_dt_seconds': sim.CONTROL_DT, 'repeats': repeats, 'cases': list(CASES)}, 'platform': {'system': platform.system(), 'machine': platform.machine(), 'python': platform.python_version()}, 'attempts': []} for case in CASES: for repeat in range(repeats): attempt = _attempt(profile, assets, case, repeat, duration, timestep) report['attempts'].append(attempt) path = destination/f'attempt-{attempt["id"]}.json' path.write_bytes(_json_bytes(attempt)) report['summary'] = {'attempt_count': len(report['attempts']), 'valid_attempts': sum(bool(x['valid']) for x in report['attempts'])} compiled = next((attempt for attempt in report['attempts'] if 'runtime' in attempt), None) report['platform'].update(mujoco_version=compiled['runtime']['mujoco_version'] if compiled else None, numpy_version=compiled['runtime']['numpy_version'] if compiled else None) report['native_model_settings'] = compiled['native_model_settings'] if compiled else None rows = ''.join(f'
{html.escape(SCOPE)}
' f'{item["arm_axes"]} arm axes + {item["gripper_axes"]} gripper command. Simulation asset license: {html.escape(item["simulation_asset_license"])}.
' f'Native command units: {html.escape(", ".join(item["native_command_units"]))}. {html.escape(item["gripper_command"])}
' f'Complete sealed numerical receipt · Original asset license
' f'| Attempt | Valid physics | Simulated seconds | Error |
|---|