Skip to content

Add predicate-triggered run-time variations - #1842

Open
xyao-nv wants to merge 1 commit into
mainfrom
xyao/feature/predicate-triggered-variations
Open

xyao-nv wants to merge 1 commit into
mainfrom
xyao/feature/predicate-triggered-variations

Conversation

@xyao-nv

@xyao-nv xyao-nv commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Add PredicateTriggeredVariationBase, a run-time variation that changes the scene mid-episode the first control step a predicate holds. Add ObjectJerkVariation, which adds a sampled root velocity change to an object, for example when the gripper grasps it.

Why this change?

Run-time variations apply only at reset, so the scene is fixed once the policy starts acting. Robustness tests often need a disturbance at a specific moment of the rollout, such as a jolt to the object when the gripper closes on it. There is no way to say "when this condition first holds, change the scene".

What changes?

PredicateTriggeredVariationBase is a RunTimeVariationBase with a trigger_predicate. The predicate takes the same forms as task progress predicates: a callable predicate(env), a TerminationTermCfg, or a TrueForConsecutiveStepsCfg that requires several consecutive true steps.

  • Each episode draws its value at reset and holds it until the trigger fires. Every episode therefore has exactly one recorded sample, whether or not the trigger fires. VariationRecorder, recorded variation replay and per-variation seeding work without changes.
  • The trigger fires at most once per episode, on the first control step the predicate holds. The check runs after the control step, so the change acts from the next physics step. The policy first observes it one control step later.
  • A subclass implements build_event_cfg by passing a function apply_sample(env, env_ids, samples) to _build_triggered_event_cfg.

The event term ApplySampleWhenPredicateHolds runs in a new event mode, "predicate_triggered". Its reset() draws the episode samples and re-arms the trigger. After super().step(), IsaacLabArenaManagerBasedRLEnv.step() applies this mode to the active environments that did not start an episode during the step. The call returns early when no variation uses the mode.

ObjectJerkVariation adds a sampled world-frame [vx, vy, vz, wx, wy, wz] to the object's root velocity. The default range is ±0.3 m/s and ±1 rad/s. --list_variations shows these variations as run-time, on predicate.

Limitations:

  • One trigger per episode, because VariationRecorder stores one sample per episode.
  • The sampler must draw with sample(num_samples, env_ids), as continuous and Bernoulli samplers do. Choice and sequential samplers are not supported.
  • The trigger predicate is code, so the variation is added in the environment definition. Hydra overrides only enable it and set its distribution.
  • Arena has no gripper–object contact predicate yet. The example below triggers on gripper_not_grasping instead.

Usage example

from isaaclab_arena.tasks.predicates.gripper import gripper_not_grasping
from isaaclab_arena.variations.object_jerk_variation import ObjectJerkVariation


def cube_grasped(env):
    return ~gripper_not_grasping(
        env, embodiment.get_gripper(), grasp_width_m=0.05, gap_band_m=0.01, stall_margin_m=0.002
    )


cube.add_variation(ObjectJerkVariation(cube.name, trigger_predicate=cube_grasped))

Like other variations, it is disabled by default. Enable it and set its range with Hydra overrides:

cube.jerk.enabled=true "cube.jerk.sampler_cfg.low=[-0.5,-0.5,0.0,0.0,0.0,0.0]" "cube.jerk.sampler_cfg.high=[0.5,0.5,0.3,0.0,0.0,0.0]"

Validation

The new simulation test triggers on episode length, not on a grasp. This PR does not include a pick-and-place rollout with a grasp trigger.

Let a variation change the scene mid-episode, the first control step a
predicate holds, for example jerking an object once the gripper grasps
it. Each episode draws its value at reset, so recording, replay and
seeding treat these variations like any other run-time variation.

Signed-off-by: Xinjie Yao <[email protected]>
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The changes affect simulation behavior in environments that enable predicate-triggered variations.

The PR appears safe to merge, with a non-blocking request for partial-reset test coverage.

Findings

  1. P2 Partial resets lack coverage ▶

Summary

Adds once-per-episode variations that wait for a predicate before changing the scene.

  • Run-time variations can wait for a predicate during an episode.
  • Object jerks add a sampled velocity change when their trigger fires.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Episode reset] --> B[Draw and store sample]
    B --> C[Arm trigger]
    C --> D[Finish control step]
    D --> E{Active and not just reset?}
    E -- No --> D
    E -- Yes --> F{Pending and predicate holds?}
    F -- No --> D
    F -- Yes --> G[Apply stored sample]
    G --> H[Disarm until next reset]
Loading

Reviews (1) · Last reviewed commit: "Add predicate-triggered run-time variati..." · Reviewed by Greptile

Comment on lines +175 to +184
env = ArenaEnvBuilder(_get_jerk_test_environment(enabled=True), builder_cfg).make_registered()
try:
assert PREDICATE_TRIGGERED_EVENT_MODE in env.unwrapped.event_manager.available_modes
env.reset()
actions = torch.zeros(env.action_space.shape, device=env.unwrapped.device)
velocities_x = []
with torch.inference_mode():
for _ in range(TRIGGER_STEP + 3):
env.step(actions)
velocities_x.append(_get_sphere_velocity_x(env))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Partial resets lack coverage

The reset test calls term.reset() directly, while this simulation test uses one environment without starting another episode. Neither checks the new connection between step(), _reset_env_ids, and the event manager. A mistake there could reuse an old sample or fire a trigger too early without failing these tests.

Add a test with two environments where one resets while the other continues. Check that the new episode draws one sample and skips its trigger on the reset step, while the continuing episode keeps its sample and trigger state.

device=self.device,
)
else:
self._predicate = _create_predicate_from_config(trigger_predicate, env)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Class-based TerminationTermCfg trigger is built before it's copied

trigger_predicate sits in EventTermCfg.params, so Isaac Lab's event manager walks into it while processing this term (_resolve_param_value → _process_term_cfg_at_play in managers/manager_base.py). If its func is a class, the manager constructs it as func(cfg=..., env=...) before ApplySampleWhenPredicateHolds is created. _create_predicate_from_config then runs copy.deepcopy on a cfg whose func is already a constructed object. For a normal ManagerTermBase predicate, which stores self._env, that copy walks into the live env.

Only the bare TerminationTermCfg form is affected:

  • Progress tracking copies the cfg before it constructs anything.
  • TrueForConsecutiveStepsCfg is a plain dataclass that Isaac Lab doesn't walk into.

The manager has already resolved this cfg, so could this branch use it directly, e.g. functools.partial(trigger_predicate.func, **trigger_predicate.params)? A test that builds an env with TerminationTermCfg(func=SomePredicateClass) as the trigger would cover this. The current unit tests construct the term directly and pass plain callables.

env_ids = env_ids_to_tensor(env_ids, self.num_envs, self.device)
if len(env_ids) == 0:
return
samples = self._sampler.sample(num_samples=len(env_ids), env_ids=env_ids)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Recorded value doesn't say whether it was applied

The value is drawn here at reset, so record_variation_samples writes a value for this variation into every episode result. That includes episodes where the trigger never held, for example when the gripper never grasped. Nothing records whether or when the trigger fired, so results can't separate "failed after the jerk" from "no jerk was applied". A Bernoulli-sampled trigger variation would also put those episodes in the wrong group in build_categorical_variation_table.

Could the term keep the control step it fired at (or None) for each episode, and write it to the episode result next to the sample? TrackedEventOccurrence.step records events in a similar way.

PREDICATE_TRIGGERED_EVENT_MODE = "predicate_triggered"
"""Event mode the Arena env applies after every control step to the environments continuing their episode."""

TriggerPredicate = Callable | TerminationTermCfg | TrueForConsecutiveStepsCfg

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Reuse the progress-tracking predicate helpers

This duplicates existing progress-tracking code:

  • TriggerPredicate is the same union as Predicate in progress_tracking/progress_tracking_utils.py:18.
  • The assert in __init__ repeats _is_predicate plus the not-a-class check from TrackedEventCfg.
  • Lines 100–109 repeat the TrueForConsecutiveStepsCfg branch in CompletionCriteriaRunner.__init__ (progress_tracker.py:157-169).

The module also imports two private names, _create_predicate_from_config and _TrueForConsecutiveSteps, whose docstrings say they belong to CompletionCriteriaRunner.

Could we import Predicate, and move the step that builds the per-env check and its optional streak into one public helper in progress_tracking/ that both places call?

write_root_velocity_to_sim(env, asset, root_velocity_W, env_ids)


class ObjectJerkVariation(PredicateTriggeredVariationBase):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 "Jerk" means something else in physics

This applies a velocity change, which is an impulse. In physics, jerk is the rate of change of acceleration. Isaac Lab calls the same operation push_by_setting_velocity. The class name and the Hydra key (cube.jerk.enabled) become public config, so would ObjectPushVariation with the default name "push" be clearer before people start using it?


# The predicate first holds after step TRIGGER_STEP, so the jerk shows from the next step on.
before_jerk = velocities_x[: TRIGGER_STEP - 1]
after_jerk = velocities_x[TRIGGER_STEP:]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Skipped sample hides a one-step-late jerk

The test skips velocities_x[TRIGGER_STEP - 1]. With PhysX, the velocity write updates root_com_vel_w immediately, so each read after env.step() already includes any jerk applied at the end of that step. If the jerk fired one control step late (after step 4), index 3 would still read 1.5 and the test would pass.

Position only changes through physics, so checking it pins when the jerk took effect. For example, record root_pos_w[0, 0] each step and assert:

  • x is unchanged through step TRIGGER_STEP.
  • x has moved about JERK_VELOCITY_X * env.unwrapped.step_dt after step TRIGGER_STEP + 1.

@arena-review-bot

Copy link
Copy Markdown
Contributor

🤖 Isaac Lab-Arena Review Bot

Summary

This PR adds three pieces:

  • PredicateTriggeredVariationBase, an event term that draws a value at reset and applies it on the first control step its predicate holds.
  • A hook in IsaacLabArenaManagerBasedRLEnv.step() that runs that event mode after every step for episodes that continue.
  • ObjectJerkVariation, which adds a sampled root velocity to an object.

The approach fits the existing recorder, replay and per-variation seeding paths. Two things need fixing:

  • A class-based TerminationTermCfg trigger is deep-copied after Isaac Lab has already constructed it.
  • The episode results don't show whether the trigger fired.

Findings

🟡 Warning: isaaclab_arena/variations/predicate_triggered_variation.py:109 — Isaac Lab's event manager walks EventTermCfg.params and constructs a class-based TerminationTermCfg trigger before this term exists. _create_predicate_from_config then deep-copies the already-built object. For a normal ManagerTermBase predicate, that object holds self._env, so the copy walks into the live env.

🟡 Warning: isaaclab_arena/variations/predicate_triggered_variation.py:118 — Every episode result gets a sampled value, including episodes where the trigger never held. Nothing records whether the trigger fired or at which step, so results can't separate "failed after the jerk" from "no jerk was applied".

🔵 Improvement: isaaclab_arena/variations/predicate_triggered_variation.py:39 — TriggerPredicate repeats Predicate, and the term repeats CompletionCriteriaRunner's consecutive-steps branch. It also imports two private progress-tracking names. A shared public helper would remove the duplication.

🔵 Improvement: isaaclab_arena/variations/object_jerk_variation.py:64 — The variation applies a velocity change (an impulse). In physics, "jerk" means the rate of change of acceleration. Isaac Lab calls this operation a push, and the name becomes a public Hydra key.

🔵 Improvement: docs/pages/concepts/variations/variations.rst — The doc's table says run-time variations change "When an environment resets". One sentence about mid-episode, predicate-triggered variations would match the run-time, on predicate label that --list_variations now prints.

Test Coverage and CI Cost

  • test_object_jerk_variation_fires_on_predicate skips the sample at index TRIGGER_STEP - 1. Because PhysX makes a velocity write readable right away, a jerk that fired one control step late would still pass. See the inline comment at test line 188.
  • Lines 172–173 check that a disabled variation adds no event term. _test_disabled_runtime_variation_not_in_events_cfg in test_run_time_variations.py already covers this, since the builder skips disabled variations before looking at their type. Dropping the check saves one env-cfg build.
  • Greptile already flagged that no test covers a partial reset through _apply_predicate_triggered_events, so I haven't repeated it.
  • When I reviewed, the pre-commit and uv-detection checks were still queued. I didn't run the simulation tests.

Verdict

Minor fixes needed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant