File size: 17,916 Bytes
6aa6c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | """RoboDojo Direct v2 sensors and EEF control behind the SIM service.
Robot-only kinematics is a fixed benchmark control conversion, not an action
proposer. The public server exposes only ``DirectEEFBenchmarkAdapter`` with
the versioned robot-control ACK enabled.
"""
from __future__ import annotations
import json
import hashlib
import math
from numbers import Real
import os
from pathlib import Path
from pathlib import PurePosixPath
import subprocess
from typing import Any, Mapping, Sequence
from benchkit.contracts import ActionSpec, BenchmarkDescriptor, EpisodeSpec, Observation, ObservationSpec, Transition
from benchkit.errors import ActionRejected, CompatibilityError, ProtocolError
from benchkit.validation import ValidationIssue, validate_action_vector
from benchmarks.simBenchmarks.RoboDojo.bench_server.native_worker_client import CAMERAS, RoboDojoEpisode, SEED, validate_tick_result
from benchmarks.simBenchmarks.RoboDojo.environment.reference_eef_runtime import load_runtime, pins
from benchmarks.simBenchmarks.RoboDojo.bench_server.protocol.robot_feedback import (
SCHEMA as ROBOT_FEEDBACK_SCHEMA, validate_robot_feedback,
)
from benchmarks.simBenchmarks.RoboDojo.bench_server.protocol.task_catalog import load_catalog, task_spec
CONTROL_ALIGNMENT = "gpt-as-policy-direct/8f3d362b"
TARGET_QUATERNION_TOLERANCE = 1e-4
TASK = "classify_objects"
TASK_SPEC = task_spec(TASK)
NATIVE_INSTRUCTION = TASK_SPEC["native_instruction"]
INSTRUCTION = TASK_SPEC["public_instruction"]
HORIZON = TASK_SPEC["native_horizon"]
INTERFACE_PROTOCOL = load_catalog()["interface_protocol"]
POSE_FIELDS = ("x_m", "y_m", "z_m", "qw", "qx", "qy", "qz")
STATE_NAMES = tuple(name for side in ("left", "right")
for name in (*(f"{side}_joint{i}_rad" for i in range(1, 7)), f"{side}_gripper_open_01")) + tuple(
f"{side}_eef_{field}" for side in ("left", "right") for field in POSE_FIELDS)
ACTION_SPEC = ActionSpec(mode="robodojo_dual_x5_world_eef_target_16", size=16,
names=tuple(name for side in ("left", "right") for name in (
*(f"{side}_eef_{field}" for field in POSE_FIELDS), f"{side}_gripper_open_01")),
control_hz=25.0, frame="world-frame measured ARX X5 link6 poses, not a gripper-tip offset",
rotation="absolute unit quaternion wxyz for each arm",
gripper="absolute continuous [0,1], 0 closed / 1 open; pinned native conversion")
OBSERVATION_SPEC = ObservationSpec(cameras=CAMERAS, state_names=STATE_NAMES,
modalities=("rgb", "language", "proprioception"), privileged=False,
control_feedback_schema=ROBOT_FEEDBACK_SCHEMA)
def validate_action(values: Sequence[float]) -> tuple[float, ...]:
tolerance = TARGET_QUATERNION_TOLERANCE
vector = ACTION_SPEC.validate(values)
for offset in (0, 8):
norm = math.sqrt(sum(value * value for value in vector[offset + 3:offset + 7]))
if abs(norm - 1.0) > tolerance:
raise ProtocolError("EEF target quaternion must be unit length; no silent normalization")
if not 0 <= vector[offset + 7] <= 1:
raise ProtocolError("EEF gripper target must be in [0,1]")
return vector
def validate_direct_tick_result(message: Mapping[str, Any], expected_targets: int, *, horizon: int = HORIZON):
"""Reuse strict native counter/reward checks, with reference timeout flags."""
metrics = message.get("metrics")
if not isinstance(metrics, Mapping):
raise ProtocolError("native RPC metrics must be an object")
if any(type(message.get(name)) is not bool for name in ("terminated", "truncated")):
raise ProtocolError("native RPC terminal flags must be boolean")
if type(horizon) is not int or horizon <= 0 or type(expected_targets) is not int or not 1 <= expected_targets <= horizon:
raise ProtocolError("reference control count is outside native horizon")
legacy_projection = {**message, "terminated": metrics.get("native_end_flag"), "truncated": False}
success, ended, _, reward = validate_tick_result(legacy_projection, expected_targets)
if expected_targets == horizon and not ended:
raise ProtocolError("native horizon reached without native termination")
truncated = bool(ended and not success and expected_targets >= horizon)
terminated = bool(ended and not truncated)
if message["terminated"] is not terminated or message["truncated"] is not truncated:
raise ProtocolError("reference native terminal flags disagree")
return success, terminated, truncated, reward
def public_state(values: Sequence[float]) -> tuple[float, ...]:
if not isinstance(values, (tuple, list)) or len(values) != len(STATE_NAMES):
raise ProtocolError("expected measured qpos14 and EEF14, in the declared fixed order")
if any(isinstance(v, bool) or not isinstance(v, Real) or not math.isfinite(float(v)) for v in values):
raise ProtocolError("proprioception must contain finite real measurements")
result = tuple(float(v) for v in values)
for start in (17, 24):
if abs(math.sqrt(sum(v * v for v in result[start:start + 4])) - 1) > 1e-4:
raise ProtocolError("measured EEF quaternion is not unit length")
return result
def open_frame(root: Path, relative: str):
"""Open only a regular relative path, rejecting every symlink component."""
if not isinstance(relative, str):
raise ProtocolError("invalid worker image path")
name = PurePosixPath(relative)
if (not relative or relative == "." or "\\" in relative or name.is_absolute()
or ".." in name.parts or name.as_posix() != relative):
raise ProtocolError("worker image escaped its artifact directory")
parent = os.open(root, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
for part in name.parts[:-1]:
child = os.open(part, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent)
os.close(parent)
parent = child
fd = os.open(name.parts[-1], os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=parent)
finally:
os.close(parent)
import stat
if not stat.S_ISREG(os.fstat(fd).st_mode):
os.close(fd)
raise ProtocolError("worker image is not a regular file")
return os.fdopen(fd, "rb")
class ReferenceEEFEpisode(RoboDojoEpisode):
"""Reuse only correlated private RPC and acknowledged process cleanup."""
def _observation(self, result: Mapping[str, Any]) -> Observation:
from PIL import Image
import numpy as np
if result.get("instruction") != self.benchmark.instruction or set(result.get("frames", {})) != set(CAMERAS):
raise ProtocolError("unexpected task language or camera channels")
images = {}
for camera in CAMERAS:
with open_frame(self.artifact_dir, result["frames"][camera]) as stream, Image.open(stream) as image:
rgb = np.asarray(image).copy()
if rgb.dtype != np.uint8 or rgb.shape != (480, 640, 3):
raise ProtocolError("reference cameras require original uint8 480x640x3 RGB")
images[camera] = rgb
# No worker metrics, layout fields, object identities or calibration are
# copied. State is measured joints/EEF and native last gripper commands.
state = public_state(result["state"])
feedback = None
if self.steps:
feedback = validate_robot_feedback(result.get("control_feedback"),
expected_control_target=self.steps, expected_robot_state=state)
elif "control_feedback" in result:
raise ProtocolError("robot feedback was not declared or no control was acknowledged")
if feedback is None:
return Observation(self.steps, self.benchmark.instruction, images, state)
return Observation(self.steps, self.benchmark.instruction, images, state, control_feedback=feedback)
def reset(self) -> Observation:
if self.process is not None or self.artifact_dir is None:
raise ProtocolError("bind artifacts before the one permitted reset")
benchmark = self.benchmark
document = benchmark.runtime_document()
repo = Path(__file__).resolve().parents[4]
worker = Path(__file__).resolve().parents[1] / "environment" / benchmark.worker_filename
if not benchmark.python.is_file() or not worker.is_file():
raise CompatibilityError("missing native interpreter or reference EEF worker")
accepted = os.environ.get("OMNI_KIT_ACCEPT_EULA", "N").lower() in ("y", "yes", "1")
marker = benchmark.python.parent.parent / "lib/python3.11/site-packages/isaacsim/kit/EULA_ACCEPTED"
if marker.is_file():
accepted = accepted or (marker.read_text().splitlines() or [""])[0].strip().lower() in ("y", "yes", "1")
if not accepted:
raise CompatibilityError("Isaac Sim licence acceptance is unresolved")
env = os.environ.copy()
for name in ("CUDA_VISIBLE_DEVICES", "ROBODOJO_USD_ASSET_PREFIX", "EVAL_NUM", "RSI_SIM_SERVER_TOKEN"):
env.pop(name, None)
env.update(PYTHONPATH=os.pathsep.join((str(repo), str(benchmark.source_root),
*benchmark.worker_pythonpath(document))),
PYTHONNOUSERSITE="1", PYTHONDONTWRITEBYTECODE="1", PYTHONUNBUFFERED="1",
OMP_NUM_THREADS="2", MKL_NUM_THREADS="2", XDG_CACHE_HOME=str(self.artifact_dir / "cache"),
TORCH_EXTENSIONS_DIR=str(self.artifact_dir / "cache/torch_extensions"))
self.log = (self.artifact_dir / "native_worker.log").open("x")
self.process = subprocess.Popen([str(benchmark.python), "-B", str(worker)],
cwd=benchmark.source_root, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=self.log, text=True, bufsize=1)
request = {"op": "reset", "task": benchmark.task, "layout_id": self.episode.metadata["layout_id"],
"runtime_manifest": str(benchmark.runtime_manifest),
"runtime_manifest_sha256": benchmark.runtime_manifest_sha256,
"artifact_dir": str(self.artifact_dir), "gpu_id": benchmark.gpu_id,
"diagnostic": benchmark.diagnostic}
return self._observation(self._rpc(request))
def _accept_step(self, result: Mapping[str, Any]) -> Transition:
success, terminated, truncated, reward = validate_direct_tick_result(result, self.steps + 1,
horizon=self.benchmark.native_horizon)
self.steps += 1
self.finished = terminated or truncated
return Transition(self._observation(result), reward, terminated, truncated,
{"native_success": success, "control_targets": result["metrics"]["control_targets"],
"physics_ticks": result["metrics"]["physics_ticks"]})
def step(self, action: Sequence[float]):
if self.finished:
raise ProtocolError("cannot step a finished episode")
return self._accept_step(self._rpc({"op": "step", "action": validate_action(action)}))
def metrics(self) -> Mapping[str, Any]:
return {**self.latest, "adapter_step_returns": self.steps,
"track": self.benchmark.descriptor.protocol_version,
"diagnostic_only": self.benchmark.diagnostic,
"reference_native_horizon": self.benchmark.native_horizon, "native_horizon": self.benchmark.native_horizon,
"native_step_lim_override": None}
class DirectEEFBenchmarkAdapter:
observation_spec = OBSERVATION_SPEC
action_spec = ACTION_SPEC
protocol_version = INTERFACE_PROTOCOL
task = TASK
instruction = INSTRUCTION
native_horizon = HORIZON
allowed_layout_ids = tuple(TASK_SPEC["panels"]["fast"]["layout_ids"])
panel_name = TASK_SPEC["panels"]["fast"]["id"]
worker_filename = "native_eef_worker.py"
def runtime_document(self):
return load_runtime(self.runtime_manifest, self.runtime_manifest_sha256)
@staticmethod
def worker_pythonpath(document):
return [document["recording_support"]]
@staticmethod
def pinned_snapshot():
return pins()
@staticmethod
def select_task_spec(task_id):
spec = task_spec(task_id)
if task_id != TASK:
raise ValueError("task is declared but has no accepted native Direct worker")
snapshot_path = Path(__file__).resolve().parents[1] / "environment/reference_eef_snapshot.json"
if hashlib.sha256(snapshot_path.read_bytes()).hexdigest() != spec["layout_binding"]["snapshot_sha256"]:
raise ValueError("task catalog and frozen layout snapshot differ")
return spec
def layout_inventory_digest(self):
return self.pinned_snapshot()["classify_layout_inventory_sha256"]
@classmethod
def validate_action_input(cls, values):
"""Pure public action checks, before the service consumes a sequence.
Per-decision motion limits/binary-gripper preferences belong to the
model, not this benchmark. No native state or task feedback is read.
"""
tolerance = TARGET_QUATERNION_TOLERANCE
vector = validate_action_vector(ACTION_SPEC, values)
for side, offset in (("left", 0), ("right", 8)):
norm = math.hypot(*vector[offset + 3:offset + 7])
if not math.isfinite(norm) or abs(norm - 1.0) > tolerance:
raise ActionRejected(ValidationIssue("quaternion_not_unit", field=f"{side}.quaternion_norm",
actual=norm if math.isfinite(norm) else None, limit=tolerance))
if not 0 <= vector[offset + 7] <= 1:
raise ActionRejected(ValidationIssue("out_of_bounds", field=f"{side}.opening",
actual=vector[offset + 7], limit=0 if vector[offset + 7] < 0 else 1))
return vector
def __init__(self, *, runtime_manifest: str, runtime_manifest_sha256: str,
task_id: str = TASK,
panel: str = "fast", gpu_id: int = 0,
worker_timeout: float = 1200, diagnostic: bool = False) -> None:
self.task_spec = self.select_task_spec(task_id)
self.task = task_id
self.native_horizon = self.task_spec["native_horizon"]
if not isinstance(panel, str) or panel not in self.task_spec["panels"]:
raise ValueError("panel must be a named task-catalog panel")
selected_panel = self.task_spec["panels"][panel]
self.allowed_layout_ids = tuple(selected_panel["layout_ids"])
self.panel = panel
self.panel_name = selected_panel["id"]
self.instruction = self.task_spec["public_instruction"]
self.robot_feedback = ROBOT_FEEDBACK_SCHEMA
if type(gpu_id) is not int or gpu_id < 0 or type(diagnostic) is not bool:
raise ValueError("invalid GPU or diagnostic selection")
if isinstance(worker_timeout, bool) or not isinstance(worker_timeout, Real) or not math.isfinite(worker_timeout) or worker_timeout <= 0:
raise ValueError("worker_timeout must be finite and positive")
self.runtime_manifest = Path(runtime_manifest).absolute()
self.runtime_manifest_sha256 = runtime_manifest_sha256
document = self.runtime_document()
self.source_root = self.runtime_manifest.parent.resolve() / "RoboDojo"
self.python = Path(document["python"])
self.layout_ids = self.allowed_layout_ids
self.gpu_id, self.worker_timeout, self.diagnostic = gpu_id, float(worker_timeout), diagnostic
self.budget_mode, self.max_policy_calls, self.replan_interval = "horizon", None, None
self.environment_step_cap = self.native_horizon
self.native_step_lim_override = None
track = self.protocol_version
if diagnostic:
track += "-infrastructure-only"
self.descriptor = BenchmarkDescriptor("robodojo", "ee67a146-native", track, self.pinned_snapshot()["native_revision"])
def episodes(self, seed: int = SEED, limit: int | None = None) -> Sequence[EpisodeSpec]:
if type(seed) is not int or seed != SEED:
raise ProtocolError("use the declared RSI seed; native startup/eval seed is0")
if limit is not None and (type(limit) is not int or not 1 <= limit <= len(self.layout_ids)):
raise ValueError("limit must select a nonempty prefix of the frozen schedule")
native_seed = self.task_spec["native_eval_seed"]
simulator_seed = self.task_spec["simulator_initial_seed"]
result = tuple(EpisodeSpec(f"{self.task}/arx_x5/eval-seed{native_seed}/layout{i}", self.task, seed, self.instruction,
{"native_eval_seed": native_seed, "simulator_initial_seed": simulator_seed, "reset_seed": i, "layout_id": i,
"layout_inventory_sha256": self.layout_inventory_digest(),
"native_horizon": self.native_horizon,
"runtime_manifest_sha256": self.runtime_manifest_sha256,
"panel": self.panel, "panel_id": self.panel_name,
"official_full_protocol": self.panel == "full",
"robot_feedback_schema": self.robot_feedback,
"control_alignment": CONTROL_ALIGNMENT,
"target_quaternion_tolerance": TARGET_QUATERNION_TOLERANCE}) for i in self.layout_ids)
return result if limit is None else result[:limit]
def open_episode(self, episode: EpisodeSpec) -> ReferenceEEFEpisode:
if episode not in self.episodes():
raise ProtocolError("episode was not in the immutable service schedule")
return ReferenceEEFEpisode(self, episode)
def make_reference_eef(parameters: Mapping[str, Any]) -> DirectEEFBenchmarkAdapter:
"""Construct the single Direct v2 adapter without protocol selection."""
values = dict(parameters)
if "protocol_track" in values or "robot_feedback" in values:
raise ValueError("Direct v2 protocol and robot feedback are fixed")
return DirectEEFBenchmarkAdapter(**values)
|