|
| 1 | +"""Regression: ManiSkill dense rewards index Franka joints by the sorted-name contract. |
| 2 | +
|
| 3 | +MetaSim reports ``RobotState.joint_pos`` / ``joint_vel`` in alphabetically-sorted |
| 4 | +joint-name order. For the Franka Panda that order is |
| 5 | +``[panda_finger_joint1, panda_finger_joint2, panda_joint1..panda_joint7]`` — i.e. |
| 6 | +the two gripper fingers are columns 0-1 and the seven arm joints are columns 2-8 |
| 7 | +(see ``roboverse_pack/robots/franka_cfg.py`` ``ee_joint_indices = [0, 1]``). |
| 8 | +
|
| 9 | +The dense reward functions used to slice ``joint_pos[:, 7:9]`` for the gripper and |
| 10 | +``joint_vel[:, :7]`` for the arm — the *URDF* declaration order — so ``is_grasped``, |
| 11 | +``gripper_width`` and the static/velocity terms were computed from the wrong joints |
| 12 | +on every backend. These tests pin the corrected indexing without needing a |
| 13 | +simulator backend or downloaded assets (``_reward`` is a pure function of the |
| 14 | +passed ``env_states``). |
| 15 | +
|
| 16 | +Backend-free by design: no existing ``tests/test_maniskill_*`` file is usable here |
| 17 | +because they all ``pytest.importorskip("mani_skill")`` at module load. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import torch |
| 23 | + |
| 24 | +from metasim.types import ObjectState, RobotState |
| 25 | + |
| 26 | + |
| 27 | +def _pick_cube_states(finger_val: float, arm_tail_val: float = 1.5): |
| 28 | + """Build a minimal env_states for PickCube where the cube sits at the TCP. |
| 29 | +
|
| 30 | + Fingers occupy sorted columns 0-1; ``arm_tail_val`` is written to columns 7-8 |
| 31 | + (the columns the buggy code mistook for the gripper) so a regression cannot |
| 32 | + pass by reading the arm. ``finger_val`` per finger: 0.01 -> closed/grasped, |
| 33 | + 0.04 -> open. |
| 34 | + """ |
| 35 | + import roboverse_pack.tasks.maniskill.pick_cube_v1 as M |
| 36 | + |
| 37 | + jp = torch.zeros(1, 9) |
| 38 | + jp[0, 0] = finger_val |
| 39 | + jp[0, 1] = finger_val |
| 40 | + jp[0, 7] = arm_tail_val |
| 41 | + jp[0, 8] = arm_tail_val |
| 42 | + body_state = torch.zeros(1, 2, 13) |
| 43 | + body_state[0, 1, 3] = 1.0 # panda_hand quaternion = wxyz identity, at origin |
| 44 | + rs = RobotState( |
| 45 | + root_state=torch.zeros(1, 13), |
| 46 | + body_names=["panda_link0", "panda_hand"], |
| 47 | + body_state=body_state, |
| 48 | + joint_pos=jp, |
| 49 | + joint_vel=torch.zeros(1, 9), |
| 50 | + ) |
| 51 | + cube = torch.zeros(1, 13) |
| 52 | + cube[0, :3] = torch.tensor(M._TCP_OFFSET) # cube exactly at the TCP -> reaching satisfied |
| 53 | + goal = torch.zeros(1, 13) |
| 54 | + goal[0, 2] = 0.5 # goal far away -> not placed -> no success override |
| 55 | + |
| 56 | + class _S: |
| 57 | + pass |
| 58 | + |
| 59 | + s = _S() |
| 60 | + s.robots = {"franka": rs} |
| 61 | + s.objects = {"cube": ObjectState(root_state=cube), "goal_site": ObjectState(root_state=goal)} |
| 62 | + return s |
| 63 | + |
| 64 | + |
| 65 | +def test_pick_cube_grasp_reward_reads_finger_columns(): |
| 66 | + """Closing the fingers (sorted cols 0-1) must register as a grasp. |
| 67 | +
|
| 68 | + On the pre-fix code (``joint_pos[:, 7:9]``) the reward read arm columns 7-8 |
| 69 | + (here a large constant), so it never saw the grasp and both branches scored |
| 70 | + identically — this asserts the gap that only the corrected indexing produces. |
| 71 | + """ |
| 72 | + from roboverse_pack.tasks.maniskill.pick_cube_v1 import PickCubeV1DenseTask |
| 73 | + |
| 74 | + grasped = float(PickCubeV1DenseTask._reward(None, _pick_cube_states(0.01))[0]) |
| 75 | + ungrasped = float(PickCubeV1DenseTask._reward(None, _pick_cube_states(0.04))[0]) |
| 76 | + |
| 77 | + assert grasped > ungrasped + 0.9, ( |
| 78 | + f"grasp (fingers closed) should raise the reward via the is_grasped term; " |
| 79 | + f"got grasped={grasped:.4f} ungrasped={ungrasped:.4f}. A near-zero gap means " |
| 80 | + f"the reward is reading arm columns, not the gripper fingers (cols 0-1)." |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def test_no_maniskill_reward_uses_urdf_order_finger_slice(): |
| 85 | + """Guard the whole dense-reward family against the URDF-order regression. |
| 86 | +
|
| 87 | + The five sibling tasks (stack_cube, poke_cube, place_sphere, pick_single_ycb, |
| 88 | + lift_peg_upright) share the same Franka layout but build different object |
| 89 | + states, so this asserts the corrected slice convention at the source level |
| 90 | + rather than re-deriving each task's full env_states. |
| 91 | + """ |
| 92 | + import pathlib |
| 93 | + |
| 94 | + pack = pathlib.Path(__file__).resolve().parents[1] / "roboverse_pack" / "tasks" / "maniskill" |
| 95 | + offenders = [] |
| 96 | + for py in pack.glob("*.py"): |
| 97 | + text = py.read_text() |
| 98 | + if "rs.joint_pos[:, 7:9]" in text or "rs.joint_vel[:, :7]" in text: |
| 99 | + offenders.append(py.name) |
| 100 | + assert not offenders, ( |
| 101 | + f"these files slice Franka joints in URDF order (gripper 7:9 / arm :7) " |
| 102 | + f"instead of the sorted-name order (gripper 0:2 / arm 2:9): {offenders}" |
| 103 | + ) |
0 commit comments