Safety filters
INFO
本页尚未翻译。 阅读英文版
Nakamura, Peters and Bajcsy, Generalizing Safety Beyond Collision-Avoidance via Latent-Space Reachability Analysis, 2025. Paper · Code · Notebook
torch_world.safety puts a safety layer on top of any WorldModel: it does not change the model and does not care how it was trained. Safety is defined by examples in the model's latent space, made predictive by Hamilton-Jacobi reachability in imagination, and enforced by a filter in front of any policy: a planner, a learned policy or a person at a teleoperation console.
Three pieces, each usable alone:
| piece | what it is | trained on |
|---|---|---|
FailureMargin | a classifier read as a signed margin | encoded frames labeled safe or failure |
SafetyValue | the safety value | imagined transitions of the frozen world model |
SafetyFilter | the least-restrictive filter in front of a task policy | nothing: it combines the two above |
Failure margin
Failure is whatever the labels say: a collision, a spill, a block knocked over, a robot in a forbidden zone. An MLP on the frozen latents scores each frame, and
from torch.utils.data import DataLoader, TensorDataset
from torch_world.safety import FailureMargin
from torch_world.utils import fit
with torch.no_grad():
latents = model.encode(pixels) # (N, T, D) from the frozen world model
margin = FailureMargin(D, hidden=(256, 256)) # delta = 0.75, the reference code's value
loader = DataLoader(TensorDataset(latents.flatten(0, 1), failure.flatten()), batch_size=512, shuffle=True)
fit(margin, loader, epochs=5, lr=1e-3, inputs=lambda b: b) # {"loss", "safe_loss", "failure_loss", "accuracy"}
l = margin.margin(z) # (B,), negative = failureLatents of any shape work: pass latent_shape=(196, 384) for patch tokens, and they are flattened.
Safety value by reachability in imagination
from torch_world.safety import SafetyValue, fit_safety_value
value = SafetyValue(D, action_dim=2, hidden=(512, 512, 512, 512), gamma=0.9999)
log = fit_safety_value(value, model, margin, starts=latents.flatten(0, 1), iterations=60_000, warmup=5_000)
value.value(z) # (B,) safety value
value.action(z) # (B, 2) safety policyfit_safety_value freezes the world model and the margin, and runs envs imagined episodes in parallel. Each starts from a latent of starts (encoded dataset frames) and runs horizon steps of the model (the paper's 25, to limit drift) before it restarts. Transitions go to a replay buffer; one update follows every envs transitions, as in the reference code (8 per update). Exploration and warm-up follow the reference code too: the actor's action with Gaussian noise, replaced by a random action whenever the critic deems that one safe, and warmup iterations of random actions with imagine_step (model.rollout from a single context latent).
The filter
The least-restrictive filter lets the task policy act as long as the latent it leads to keeps a safety value above a threshold
from torch_world import evaluation
from torch_world.safety import SafetyFilter
safety = SafetyFilter(model, value, threshold=0.4) # the paper's 0.4 in simulation, 0.3 on hardware
a, overridden = safety(z, task_actions) # (B, A), (B,)
policy = safety.wrap(evaluation.goal_policy(model, planner, goal_images)) # observation -> action
run = evaluation.goal_reaching(env, {"CEM + filter": policy}, start, goal, steps=60)
policy.overrides # per step, which episodes the safety policy took overrule="q" checks
In the notebook
safety/latent_safety_filter draws two red pools on PLDM's Two-Rooms floor, trains PLDM on random-action episodes, the margin on frames labeled by whether the point is inside a pool, and the value function in PLDM's imagination. A goal-reaching CEM planner that knows nothing of the pools then runs with and without the filter.
The run uses 100 tasks, 60 steps each, with success counted when the goal is reached at any step. Rates carry 95% Wilson intervals. "Failure" means the point entered a pool at any step.
| policy | goal reached | failure | safe success |
|---|---|---|---|
| CEM | 94% [88, 97] | 81% [72, 87] | 17% [11, 26] |
| CEM + filter (ε = 0.4) | 53% [43, 62] | 78% [69, 85] | 17% [11, 26] |
| random | 3% [1, 8] | 78% [69, 85] | 0% [0, 4] |
| random + filter | 1% [0, 5] | 52% [42, 62] | 0% [0, 4] |
The margin and the value function come out right. The margin catches 100% of held-out failures with 1% false alarms. The unsafe set of V matches the pools, which is the exact answer for a point that can stop at once. The safety policy never steps into a pool. The filter, though, does not keep CEM out of the pools, and it halves goal reaching. Thresholds from 0 to 0.6 and the Q rule all leave failures at 72–80%.
The notebook traces the cause to the world model. One step from the edge of a pool, V of the encoded real next frame flags 100% of the moves that enter the pool, but V of PLDM's predicted next latent flags only 65%. A planner that keeps pushing toward the door eventually finds one of the moves the prediction gets wrong. The filter can be no better than the one-step model it consults, and here that model was trained on random-action data alone.
Differences from the paper
- World models. The paper uses a Dreamer RSSM in simulation and DINO-WM on hardware. Here any
WorldModelworks; the notebook uses PLDM. The value function sees one latent at a time and imagines withmodel.rolloutfrom that latent alone, so for models with a history of several frames (DINO-WM, LeWM) the imagined step uses a one-frame context. The RSSM's recurrent state carries the history that this does not. - When the margin is trained. In simulation the paper trains the classifier jointly with the world model; on hardware, after it, on frozen latents. Here it is always the latter.
- Margin details. The paper does not report
; the default is the reference code's 0.75. The reference computes the backup with the margin of the next latent as the per-step reward; this follows the paper's equation, with of the current latent. - Value function. DDPG only (the paper's continuous-action setting); the DDQN variant for discrete actions is not included, and neither is the SQRL baseline. The paper's DDPG uses four hidden layers of 512 (the default here); the reference code's configuration uses three of 128. The world model is treated as deterministic: for stochastic models, the expectation over
is estimated with one sample per transition. - Filter. The paper's rule.
rule="q"is an addition.
