Code reference¶
Auto-generated from source docstrings — the single source of truth. See the API guide for the conceptual contract.
Registry¶
robot_safety_sandbox.registry ¶
Task registry: one place future work looks to run or add benchmark tasks.
from robot_safety_sandbox import make_tensor, list_tasks
env = make_tensor("go2_gap_chain", num_envs=2048) # -> TensorVecEnv
model = ReachAvoidPPO1P("MlpPolicy", env, normalize_obs=True, ...)
A :class:TaskSpec pins everything a benchmark run needs: the mjlab cfg
builder (spawn events + curricula), the reach-avoid margins, the action dims,
and the task's mode. Curriculum LINEAGE (landing -> crossing -> chain) is
deliberately NOT a field here: it lives in docs/log/experiments.md, which is
where it is actually maintained.
Here's a MAP to navigate the codebase — Mode. Algorithm. Players.
M = Mode Safety | ReachAvoid | Cumulative the Bellman operator
A = Algorithm PPO | SAC | A2C | DQN the RL update rule
P = Players 1P | 2P single-player | zero-sum
A learner's NAME is those three letters concatenated in that order —
SafetyPPO1P, ReachAvoidSAC2P — and :func:algo_name is that
concatenation and nothing else. No lookup table, no per-task override:
M comes from the TASK its ``mode`` (below), a property of its margins
A comes from the RUN the trainer family (on_policy -> PPO, off_policy -> SAC)
P comes from the RUN the ``--adversary`` flag
Every task declares exactly one axis of its own, its mode — the safety_sb3
BACKUP it is trained under (see :data:MODES):
mode="safety" AVOID: V = min(g, gamma V') margins, no l mode="reach-avoid" REACH_AVOID: V = min(g, max(l, gamma V')) margins with l mode="cumulative" CUMULATIVE: V = r + gamma (1-d) V' dense env reward, i.e. the plain-RL TASK policy a filter wraps (margin_fn=None; envs are auto-built in dense mode, trained with STOCK SB3).
A full filter experiment needs both layers: a cumulative task policy (pi_task) and a safety task supplying the certificate V(s) + fallback.
The two SAFETY modes give four learners per algorithm, one per (M, P) cell — the mode is a property of the TASK's margins, the player count a property of the RUN:
avoid (no l) reach-avoid (real l)
single-player SafetyPPO1P ReachAvoidPPO1P
two-player SafetyPPO2P ReachAvoidPPO2P
mode picks the COLUMN, --adversary the row. CUMULATIVE has no P axis at
all: it is stock stable_baselines3 ("PPO" / "SAC", no suffix) and
there is no two-player cumulative game.
NB the 2P cell is a DIFFERENT ALGORITHM in the two families, not the same game
with a different optimizer: *SAC2P is the minimax game on one shared
joint-action critic Q(s, [a_ctrl, a_dstb]), while *PPO2P is an
alternating best-response approximation with two independent V(s) nets, two
rollout buffers and a phase machine. See docs/API.md.
TaskSpec
dataclass
¶
Source code in robot_safety_sandbox/registry.py
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 | |
register ¶
register(spec)
Source code in robot_safety_sandbox/registry.py
141 142 143 144 | |
spec ¶
spec(task_id)
Source code in robot_safety_sandbox/registry.py
155 156 157 158 159 160 161 | |
list_tasks ¶
list_tasks(mode=None)
Registered task ids, optionally filtered to one :data:MODES entry.
Source code in robot_safety_sandbox/registry.py
147 148 149 150 151 152 | |
algo_name ¶
algo_name(task_id, adversary=False, family='on_policy')
The learner CLASS NAME for running task_id — the MAP, spelled out.
It is a FORMULA, not a lookup: Mode + Algorithm + Players,
concatenated. Nothing in the registry can override it; a task supplies only
its mode.
algo_name("go2_stabilize") -> ReachAvoidPPO1P
algo_name("go2_stabilize", adversary=True) -> ReachAvoidPPO2P
algo_name("go2_stabilize", family="off_policy") -> ReachAvoidSAC1P
algo_name("digit_stabilize_avoid", adversary=True) -> SafetyPPO2P
Names only — this module never imports safety_sb3, so a cumulative-only install (dense reward + vanilla SB3, see base.py) still imports the registry.
mode=CUMULATIVE returns the bare algorithm ("PPO" / "SAC"): the
plain-RL task policy trains with STOCK stable_baselines3, which keeps the
checkpoint a vanilla SB3 zip. It has no P axis — there is no two-player
cumulative game.
Two pairings are refused rather than resolved:
- a two-player CUMULATIVE run — no such game exists;
- a reach-avoid learner on an avoid-only task (no target set). Pinning l to a constant does NOT make the reach-avoid backup compute the avoid value — a negative l empties the safe set, a non-negative one strips the lookahead — so it has no valid formulation and must not be reachable by accident. See margins.py.
Source code in robot_safety_sandbox/registry.py
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 | |
make_tensor ¶
make_tensor(task_id, num_envs=2048, device='cuda:0', adversary=False, end_criterion=None, cfg_overrides=None, **kw)
GPU-resident env (primary path; pair with safety_sb3 PPO learners).
end_criterion (None -> the task's TaskSpec value; else an explicit
override, one of :data:END_CRITERIA) sets WHEN the episode ends from (g, l).
cfg_overrides (dict) forwards experiment-level env/task params to the
task's cfg_builder, overriding the values baked into its registration (e.g.
{"gate_close_rate": 0.003}) -- lets a config recipe tune the env without
editing the task or the trainer flags.
Source code in robot_safety_sandbox/registry.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
make_numpy ¶
make_numpy(task_id, num_envs=64, device='cuda:0', adversary=False, end_criterion=None, cfg_overrides=None, **kw)
Classic SB3 VecEnv (for the SAC family / stock SB3 tooling).
end_criterion (None -> the task's TaskSpec value; else an override) sets
WHEN the episode ends from (g, l). cfg_overrides (dict) forwards env/task
params to the task's cfg_builder. See :func:make_tensor.
Source code in robot_safety_sandbox/registry.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
Margins¶
robot_safety_sandbox.margins ¶
Composable reach-avoid margin library (batched torch, mjlab scene API).
A task's margin_fn(env) -> (g, l) is composed from these terms:
g (avoid) : stay out of the failure set — g < 0 == failure
l (reach) : arrive in the target set — l >= 0 == reached
Conventions: margins are signed distances normalized to O(1); the g terminal
anchor and clamping live in :mod:base / the buffers, not here. All terms are
validated on the Go2 parkour tasks (ported from the reference wrappers).
compose ¶
compose(g_fn, l_fn=None, clamp=CLAMP)
margin_fn from a g term and an l term (clamped for value regression).
l_fn=None declares an AVOID-ONLY task: there is no target set. The bridge
still has to hand the learner an l channel (step_tensor returns a
5-tuple), so a zero placeholder is emitted — but the returned margin_fn is
tagged has_target = False, and it is ONLY valid under an avoid learner
(the Safety* half of the MAP, 1P or 2P), which ignores l. Feeding it to a
reach-avoid learner (ReachAvoid*) is the degenerate l_zero case above;
the tag exists so that mistake raises instead of training silently — see
registry.algo_name.
Source code in robot_safety_sandbox/margins.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
avoid_only ¶
avoid_only(margin_fn)
Strip the reach target off an existing margin_fn -> avoid-only twin.
For twin pairs that share one (g, l) builder and differ only in the backup:
the avoid twin keeps g verbatim and declares no target. Same contract as
compose(g_fn) — the l channel is a zero placeholder, has_target is
False, and only an avoid learner may consume it.
Source code in robot_safety_sandbox/margins.py
226 227 228 229 230 231 232 233 234 235 236 237 238 | |
Bridges¶
robot_safety_sandbox.base ¶
Base classes: run any mjlab task with safety_sb3 (tensor or numpy path).
The zoo's env contract (matches what every safety_sb3 learner consumes):
reward = g(s) the physical safety margin (NEVER normalize/reshape it)
l_x = l(s) the target margin (zeros for avoid-only tasks)
dones = terminated | truncated (mjlab auto-resets internally)
timeouts = truncated & ~terminated (no value bootstrap: g is absolute)
A task is fully specified by two callables (see :mod:registry):
cfg_builder(play: bool) -> ManagerBasedRlEnvCfg # the mjlab env
margin_fn(env) -> (g, l) batched tensors # the reach-avoid margins
Everything else here is plumbing: :class:MjlabTensorSafetyEnv exposes the
task as a safety_sb3 TensorVecEnv (GPU-resident, ~50k steps/s on a 12GB
card at 2048 envs); :class:MjlabNumpySafetyEnv as a classic SB3 VecEnv
(for the SAC family / plain SB3 tooling). Curriculum levels and task metrics
(mjlab extras['log']) are forwarded via metrics() so training logs
always show curriculum progression — silent curriculum stalls were the single
biggest source of lost weeks in the reference tasks.
Porting a new task = write a cfg_builder (spawn events + curricula, plain
mjlab) + a margin_fn (compose from :mod:margins), then register() it.
MjlabTensorSafetyEnv ¶
Bases: _MjlabCore, TensorVecEnv
GPU-resident bridge (primary): torch end-to-end, no numpy bounce. Pair with a safety_sb3 1P learner, SafetyPPO1P / ReachAvoidPPO1P (auto-detected).
Source code in robot_safety_sandbox/base.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
MjlabNumpySafetyEnv ¶
Bases: _MjlabCore, VecEnv
Classic numpy SB3 VecEnv bridge (for the SAC family / stock SB3
tooling). Slower (device<->host each step); prefer the tensor bridge for
on-policy training.
Source code in robot_safety_sandbox/base.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
build_task_cfg ¶
build_task_cfg(cfg_builder, margin_fn, num_envs, drop_events=('push_robot',), dense=False, end_criterion='failure', cfg_overrides=None, dense_margins=False)
Assemble an mjlab cfg for the zoo: task cfg + the margin hook.
drop_events removes events a learned adversary replaces (default: the
random push). dense=True is the STAGE-1 locomotion mode: the reward stays
the env's own dense reward stack (_r) and the safety hook is NOT added, so a
stock PPO shapes a proper gait (reach-avoid g/l are not used here).
end_criterion (see :data:registry.END_CRITERIA) sets WHEN episodes end,
UNIFORMLY across tasks. The task cfg's own terminations already encode the
FAILURE set (fell_over / illegal_contact / ... -> the reward hook anchors g<0
there) plus the env timeout, so:
* "failure" : add nothing. The task's existing terminations stand as-is
-> bit-identical to pre-end_criterion behavior. Reaching
the target does not end the episode; the agent reaches
deeper (l keeps climbing to the g ceiling).
* "reach-avoid" : ALSO add the uniform success DoneTerm (g>=0 AND l>=0) on
top of the failure terminations -> reach-and-stop.
* "timeout" : strip every non-time_out (failure) termination so ONLY the
env timeout ends episodes, and add no success term (pure
value-learning / diagnostic). g is still anchored nowhere,
so failures live on in the margin, they just don't reset.
Source code in robot_safety_sandbox/base.py
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 | |