assests / common /source /starBenchserver /benchmarks /simBenchmarks /Robotwin /environment /runtime.py
Download common/source/starBenchserver/benchmarks/simBenchmarks/Robotwin/environment/runtime.py from JackAILab/assests: direct link, hf CLI and curl.
- Browser
- Download file 17 kB
-
https://huggingface.co/datasets/JackAILab/assests/resolve/main/common/source/starBenchserver/benchmarks/simBenchmarks/Robotwin/environment/runtime.py
- Command line
-
hf download hf://datasets/JackAILab/assests/common/source/starBenchserver/benchmarks/simBenchmarks/Robotwin/environment/runtime.py
-
curl -L -o runtime.py https://huggingface.co/datasets/JackAILab/assests/resolve/main/common/source/starBenchserver/benchmarks/simBenchmarks/Robotwin/environment/runtime.py
17 kB
| """Lazy native construction and passive instrumentation for the clean qpos track. | |
| No simulator is imported by the adapter factory or readiness command. The | |
| native task owns scene generation, control interpolation and success checks. | |
| """ | |
| from __future__ import annotations | |
| from contextlib import contextmanager | |
| import importlib | |
| import importlib.metadata | |
| import json | |
| import math | |
| from numbers import Real | |
| import os | |
| from pathlib import Path | |
| import sys | |
| from benchkit.errors import CompatibilityError, ProtocolError | |
| from benchmarks.simBenchmarks.Robotwin.environment.deployment import digest, reference_snapshot, verify | |
| NATIVE_INVENTORY_SHA256 = "ab5a73b0482d50d9c83e8702b37e7e7eb10bf7033e03a8fec59b60b708bef08c" | |
| RUNTIME_SNAPSHOT = Path(__file__).with_name("runtime_snapshot.json") | |
| RUNTIME_SNAPSHOT_SHA256 = "b1b7ecba4723d9fecdcdf79e0323817ccd05cc58cb12cd7af56ad2e4fefd4d28" | |
| BUNDLED_RENDERER = "bundled_oidn_2_0_1" | |
| COMPAT_RENDERER = "oidn_2_3_3_cuda_v1" | |
| RENDERER_RUNTIMES = (BUNDLED_RENDERER, COMPAT_RENDERER) | |
| TASK_INSTRUCTIONS = { | |
| "lift_pot": "use BOTH!!! arms to lift the pot", | |
| "click_bell": "click the <bell's top center> on the table", | |
| } | |
| JOINT_NAMES = tuple(f"f{side}_joint{i}" for side in ("l", "r") for i in range(1, 7)) | |
| STATE_NAMES = tuple(name for side, prefix in (("left", "fl"), ("right", "fr")) | |
| for name in (*(f"measured_{prefix}_joint{i}_rad" for i in range(1, 7)), | |
| f"commanded_{side}_gripper_01")) | |
| def validate_robot_state(state) -> tuple[float, ...]: | |
| """Validate the public mixed-provenance vector without clipping encoders.""" | |
| if not isinstance(state, (tuple, list)) or len(state) != len(STATE_NAMES): | |
| raise ProtocolError("RoboTwin robot state must contain 14 values in the declared order") | |
| if any(isinstance(value, bool) or not isinstance(value, Real) for value in state): | |
| raise ProtocolError("RoboTwin robot state must contain finite real numbers, not booleans") | |
| try: | |
| values = tuple(float(value) for value in state) | |
| except (OverflowError, ValueError) as exc: | |
| raise ProtocolError("RoboTwin robot state must contain finite real numbers") from exc | |
| if any(not math.isfinite(value) for value in values): | |
| raise ProtocolError("RoboTwin robot state must contain finite real numbers") | |
| if any(not 0 <= values[index] <= 1 for index in (6, 13)): | |
| raise ProtocolError("RoboTwin cached gripper commands must be in [0,1]") | |
| # Arm encoders are measurements, not accepted action targets. In particular, | |
| # do not apply the action specification's authored joint limits here. | |
| return values | |
| def robot_state(env) -> tuple[float, ...]: | |
| """Read the pinned native encoder APIs once per side; never use drive targets. | |
| Each native getter returns six measured articulation qpos entries followed | |
| by the last normalized set_gripper command, NOT a measured finger position. | |
| These getters read robot state only and do not advance native physics. | |
| """ | |
| values = [] | |
| for side in ("left", "right"): | |
| try: | |
| getter = getattr(env.robot, f"get_{side}_arm_real_jointState") | |
| row = getter() | |
| except Exception as exc: | |
| raise CompatibilityError(f"RoboTwin native {side} measured-state getter failed") from exc | |
| if not isinstance(row, (tuple, list)) or len(row) != 7: | |
| raise CompatibilityError(f"RoboTwin native {side} measured-state getter must return 7 values") | |
| values.extend(row) | |
| try: | |
| return validate_robot_state(values) | |
| except ProtocolError as exc: | |
| raise CompatibilityError("RoboTwin native robot-state values differ from the declared contract") from exc | |
| def state_provenance() -> dict: | |
| """Static sensor declaration; safe for provenance without native calls.""" | |
| return { | |
| "names": list(STATE_NAMES), | |
| "sources": ["robot.get_left_arm_real_jointState()", "robot.get_right_arm_real_jointState()"], | |
| "measured_arm_joint_indices": [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12], | |
| "measured_arm_joint_source": "left/right_entity.get_qpos(), indexed by left/right_arm_joints", | |
| "measured_arm_joint_units": "radians", | |
| "commanded_gripper_indices": [6, 13], | |
| "commanded_gripper_source": "get_left/right_gripper_val(): cached left/right_gripper_val from native set_gripper", | |
| "commanded_gripper_semantics": "last normalized command in [0,1], 0 closed / 1 open; not measured finger position", | |
| "sampling": "after setup_demo and each take_action; getter reads only, no added control or physics", | |
| "raw_joint_action_vector_used": False, | |
| "measured_arm_joint_clipping": False, | |
| } | |
| def native_directory(root: Path, jit_cache: Path | None = None): | |
| """The native archive uses relative asset paths; service calls are serial.""" | |
| previous = Path.cwd() | |
| previous_cache = os.environ.get("TORCH_EXTENSIONS_DIR") | |
| if jit_cache is not None and (not jit_cache.is_absolute() or jit_cache.resolve().is_relative_to(root.resolve())): | |
| raise CompatibilityError("native JIT cache must be an owned absolute directory outside the deployment") | |
| try: | |
| os.chdir(root) | |
| if jit_cache is not None: | |
| os.environ["TORCH_EXTENSIONS_DIR"] = str(jit_cache) | |
| yield | |
| finally: | |
| os.chdir(previous) | |
| if jit_cache is not None: | |
| if previous_cache is None: | |
| os.environ.pop("TORCH_EXTENSIONS_DIR", None) | |
| else: | |
| os.environ["TORCH_EXTENSIONS_DIR"] = previous_cache | |
| def verify_runtime(renderer_runtime: str = BUNDLED_RENDERER) -> dict: | |
| """Read files/metadata only, including the actual native TOPP implementation. | |
| This pins SAPIEN/MPLib/TOPPRA package files and dependency versions. It does | |
| not attest a GPU driver, loaded memory, every transitive package or CUDA JIT | |
| output. The receipt reports those limits instead of claiming a full lockfile. | |
| """ | |
| if renderer_runtime not in RENDERER_RUNTIMES: | |
| raise CompatibilityError("unknown RoboTwin renderer runtime track") | |
| if digest(RUNTIME_SNAPSHOT) != RUNTIME_SNAPSHOT_SHA256: | |
| raise CompatibilityError("packaged RoboTwin runtime snapshot changed") | |
| expected = json.loads(RUNTIME_SNAPSHOT.read_text()) | |
| original_sapien_rows = expected["packages"]["sapien"]["files"] | |
| if renderer_runtime == COMPAT_RENDERER: | |
| from benchmarks.simBenchmarks.Robotwin.environment import oidn_compat | |
| expected["packages"]["sapien"]["files"] = oidn_compat.package_rows(original_sapien_rows) | |
| if sys.version.split()[0] != expected["python"]: | |
| raise CompatibilityError(f"RoboTwin requires the inspected Python {expected['python']} runtime") | |
| versions = {} | |
| file_counts = {} | |
| compatibility = None | |
| for name, requirement in expected["packages"].items(): | |
| try: | |
| distribution = importlib.metadata.distribution(name) | |
| except importlib.metadata.PackageNotFoundError as exc: | |
| raise CompatibilityError(f"missing RoboTwin dependency: {name}") from exc | |
| if distribution.version != requirement["version"]: | |
| raise CompatibilityError(f"RoboTwin {name} version differs from the inspected runtime") | |
| versions[name] = distribution.version | |
| if "files" not in requirement: | |
| continue | |
| actual = {str(path) for path in (distribution.files or ()) | |
| if str(path).split("/")[0] == name and "__pycache__" not in str(path) | |
| and not str(path).endswith((".pyc", ".pyo"))} | |
| if actual != {row["path"] for row in requirement["files"]}: | |
| raise CompatibilityError(f"RoboTwin {name} package inventory differs") | |
| for row in requirement["files"]: | |
| path = Path(distribution.locate_file(row["path"])) | |
| if (not path.is_file() or path.is_symlink() or path.stat().st_size != row["size_bytes"] | |
| or digest(path) != row["sha256"]): | |
| raise CompatibilityError(f"RoboTwin native dependency content differs: {name}/{row['path']}") | |
| # Detect unrecorded source overrides as well as changed distribution rows. | |
| package = Path(distribution.locate_file(name)) | |
| disk = {path.relative_to(package.parent).as_posix() for path in package.rglob("*") | |
| if path.is_file() and "__pycache__" not in path.parts | |
| and path.suffix not in (".pyc", ".pyo")} | |
| if disk != actual: | |
| raise CompatibilityError(f"unrecorded RoboTwin native package files: {name}") | |
| file_counts[name] = len(actual) | |
| if name == "sapien" and renderer_runtime == COMPAT_RENDERER: | |
| compatibility = oidn_compat.verify_overlay(distribution, original_sapien_rows) | |
| report = {"python": sys.version.split()[0], "executable": sys.executable, | |
| "package_versions": versions, "content_pinned_package_files": file_counts, | |
| "runtime_snapshot_sha256": RUNTIME_SNAPSHOT_SHA256, | |
| "runtime_scope": "SAPIEN/MPLib/TOPPRA file content plus listed dependency versions; not a complete environment/driver/JIT attestation"} | |
| if compatibility is not None: | |
| report["renderer_compatibility"] = compatibility | |
| return report | |
| def readiness(root: Path, manifest_sha256: str, renderer_runtime: str = BUNDLED_RENDERER) -> dict: | |
| if reference_snapshot()["inventory_sha256"] != NATIVE_INVENTORY_SHA256: | |
| raise CompatibilityError("packaged RoboTwin native content identity changed") | |
| try: | |
| deployment = verify(root, manifest_sha256) | |
| except (OSError, ValueError) as exc: | |
| raise CompatibilityError("RoboTwin deployment verification failed") from exc | |
| return {"deployment": deployment, "runtime": verify_runtime(renderer_runtime), "native_resets": 0, | |
| "status": "files_verified_not_native_validated"} | |
| def verify_loaded_renderer(provenance: dict, *, require_cuda_plugin: bool) -> dict: | |
| compatibility = provenance.get("runtime", {}).get("renderer_compatibility") | |
| if compatibility is None: | |
| return {} | |
| from benchmarks.simBenchmarks.Robotwin.environment import oidn_compat | |
| return oidn_compat.verify_loaded(Path(compatibility["overlay_root"]), require_cuda_plugin=require_cuda_plugin) | |
| def resolved_native_config(root: Path, task: str, *, config_directory: str = "task_config") -> dict: | |
| import yaml | |
| with (root / config_directory / "demo_clean.yml").open() as stream: | |
| config = yaml.safe_load(stream) | |
| with (root / "assets/embodiments/aloha-agilex/config.yml").open() as stream: | |
| embodiment = yaml.safe_load(stream) | |
| with (root / config_directory / "_camera_config.yml").open() as stream: | |
| cameras = yaml.safe_load(stream) | |
| with (root / config_directory / "_eval_step_limit.yml").open() as stream: | |
| horizons = yaml.safe_load(stream) | |
| description = json.loads((root / f"description/task_instruction/{task}.json").read_text()) | |
| if (description["full_description"] != TASK_INSTRUCTIONS[task] | |
| or config["embodiment"] != ["aloha-agilex"] | |
| or horizons[task] != 400 | |
| or cameras["D435"]["h"] != 240 or cameras["D435"]["w"] != 320 | |
| or embodiment["arm_joints_name"] != [list(JOINT_NAMES[:6]), list(JOINT_NAMES[6:])] | |
| or embodiment["gripper_scale"] != [-0.01, 0.045] | |
| or embodiment["planner"] != "curobo"): | |
| raise CompatibilityError("native task/language/camera/controller configuration differs") | |
| robot_file = str(root / "assets/embodiments/aloha-agilex") | |
| # Preserve demo_clean's semantic fields. These are the native evaluator's | |
| # resolved construction fields; there is no model checkpoint or policy hook. | |
| config.update({"task_name": task, "task_config": "demo_clean", "eval_mode": True, | |
| "left_robot_file": robot_file, "right_robot_file": robot_file, | |
| "left_embodiment_config": embodiment, "right_embodiment_config": embodiment, | |
| "dual_arm_embodied": True, "head_camera_h": 240, "head_camera_w": 320, | |
| "save_data": False, "eval_video_save_dir": None}) | |
| return config | |
| def create_native(root: Path, task: str): | |
| if not sys.dont_write_bytecode: | |
| raise CompatibilityError("start RoboTwin server with python -B; native source must not gain bytecode caches") | |
| roots = {"envs": root / "envs", "curobo": root / "envs/curobo/src/curobo"} | |
| for prefix, directory in roots.items(): | |
| for name, module in tuple(sys.modules.items()): | |
| if name == prefix or name.startswith(prefix + "."): | |
| filename = getattr(module, "__file__", None) | |
| if filename is not None and not Path(filename).resolve().is_relative_to(directory): | |
| raise CompatibilityError(f"native module already imported from another root: {name}") | |
| for path in (root / "envs/curobo/src", root): | |
| if str(path) not in sys.path: | |
| sys.path.insert(0, str(path)) | |
| with native_directory(root): | |
| module = importlib.import_module(f"envs.{task}") | |
| for prefix, directory in roots.items(): | |
| loaded = sys.modules.get(prefix) | |
| if loaded is None or not Path(loaded.__file__).resolve().is_relative_to(directory): | |
| raise CompatibilityError(f"native module origin differs: {prefix}") | |
| return getattr(module, task)() | |
| def verify_compiled_control(env) -> dict: | |
| """Fail before model control if the authored 14-D contract did not compile.""" | |
| import numpy as np | |
| robot = env.robot | |
| joints = tuple(robot.left_arm_joints) + tuple(robot.right_arm_joints) | |
| names = tuple(joint.get_name() for joint in joints) | |
| limits = [np.asarray(joint.get_limits(), dtype=float) for joint in joints] | |
| if (names != JOINT_NAMES or any(row.shape != (1, 2) or not np.array_equal(row, [[-10., 10.]]) for row in limits) | |
| or list(robot.left_gripper_scale) != [-0.01, 0.045] | |
| or list(robot.right_gripper_scale) != [-0.01, 0.045] | |
| or not robot.is_dual_arm or robot.communication_flag | |
| or env.step_lim != 400 or env.take_action_cnt != 0 | |
| or not math.isclose(float(env.scene.get_timestep()), 1 / 250, rel_tol=0, abs_tol=1e-9)): | |
| raise CompatibilityError("compiled RoboTwin joint bounds, gripper, horizon or physics timestep differ") | |
| return {"joint_names": list(names), "arm_joint_bounds_radians": [-10.0, 10.0], | |
| "gripper_command_range": [0.0, 1.0], "gripper_native_scale_m": [-0.01, 0.045], | |
| "native_horizon": env.step_lim, "physics_timestep_s": float(env.scene.get_timestep()), | |
| "native_planners_initialized": True, "expert_play_once_called": False, | |
| "model_action_type": "qpos", "physics_ticks_per_control": "variable native TOPP duration"} | |
| class SceneStepCounter: | |
| """Delegating view installed after native reset; counts completed scene.step. | |
| Initialization retains the original SAPIEN scene object. Robot/planner/camera | |
| native handles are not replaced. Only the task's subsequent scene.step calls | |
| pass through this view; no clock or extra physics action is introduced. | |
| """ | |
| def __init__(self, scene): | |
| self.scene, self.steps = scene, 0 | |
| def step(self): | |
| result = self.scene.step() | |
| self.steps += 1 | |
| return result | |
| def __getattr__(self, name): | |
| return getattr(self.scene, name) | |
| class TOPPRecorder: | |
| """Observe native TOPP's existing failures; never replace its result/path.""" | |
| def __init__(self, planner, side, events): | |
| self.planner, self.side, self.events = planner, side, events | |
| def TOPP(self, *args, **kwargs): | |
| try: | |
| result = self.planner.TOPP(*args, **kwargs) | |
| except Exception as exc: | |
| self.events.append({"side": self.side, "status": "native_exception", | |
| "error_type": type(exc).__name__, "native_fallback_ticks": 50}) | |
| raise # The pinned native controller catches this, unchanged. | |
| samples = int(result[1].shape[0]) | |
| self.events.append({"side": self.side, "status": "native_zero_samples" if samples == 0 else "native_path", | |
| "samples": samples, "native_fallback_ticks": 50 if samples == 0 else 0}) | |
| return result | |
| def __getattr__(self, name): | |
| return getattr(self.planner, name) | |
| def main(argv=None): | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Read-only RoboTwin file/runtime readiness; no native imports/reset") | |
| parser.add_argument("--root", type=Path, required=True) | |
| parser.add_argument("--manifest-sha256", required=True) | |
| parser.add_argument("--renderer-runtime", choices=RENDERER_RUNTIMES, default=BUNDLED_RENDERER) | |
| args = parser.parse_args(argv) | |
| print(json.dumps(readiness(args.root, args.manifest_sha256, args.renderer_runtime), indent=2, allow_nan=False)) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |