SaFormer: A Conditional Sequence Modeling Approach to Offline Safe Reinforcement Learning

Qin Zhang, Linrui Zhang, Li Shen, Haoran Xu, Bowen Wang, Bo Yuan, Yongzhe Chang, Xueqian Wang · DOI

Introduction

Reinforcement Learning (RL) agents are powerful but often unpredictable, frequently "breaking things" or violating constraints during training to maximize rewards. In real-world applications such as autonomous driving, robotics, or power grid management, this trial-and-error approach is unacceptable. Offline Safe RL attempts to mitigate this by learning from fixed, pre-collected datasets without risky environment interactions.

However, existing methods face three major hurdles: stationary constraints that require retraining for new safety thresholds, unstable optimization caused by "Bellman backups" when balancing rewards against hard costs, and inconsistent data quality where datasets contain a mix of safe and unsafe demonstrations. SaFormer addresses these challenges by reframing Safe RL as a conditional sequence modeling problem.

The SaFormer Framework

SaFormer (Safe Transformer) pivots away from traditional value-based RL. Instead of estimating future values step-by-step, it treats the problem as a sequence modeling task, similar to the Decision Transformer architecture. This approach allows safety to be treated as a controllable parameter at inference time.

The architecture introduces two critical new tokens to manage safety budgets:

  • Cost Limit ($D$): The total safety budget for the entire episode (e.g., a maximum torque limit).
  • Cost-to-Go ($C_t$): The remaining budget at the current timestep.

2. Three-Step Inference Pipeline

To ensure both high performance and strict safety, SaFormer utilizes a two-stage action proposal followed by a verification step:

The three-step inference process of SaFormer: RTG sampling, action sampling, and posterior safety verification.
  • Step 1: Sample N RTG Candidates: The model predicts a distribution of potential Reward-to-Go (RTG) based on the current state and safety budget.
  • Step 2: Sample N Action Candidates: For the sampled RTGs, the SaFormer Actor generates corresponding action distributions and samples candidate actions.
  • Step 3: Posterior Safety Verification: A transformer-based Critic evaluates these candidates. It acts as a safety net, filtering out any actions predicted to exceed the remaining cost budget. The agent then executes the action with the highest expected reward among the "verified safe" candidates.

Experimental Setup and Results

The researchers evaluated SaFormer using standard D4RL benchmarks (Hopper, Walker2d, and HalfCheetah). Since these datasets lack native safety costs, the authors relabeled them using cumulative torque (total motor effort) as the cost metric, simulating mechanical wear-and-tear constraints.

Superior Constraint Satisfaction

In undiscounted settings—the most difficult for traditional RL because every action's cost matters equally—SaFormer demonstrated superior reliability.

Comparison of cost adherence across different models. SaFormer (blue) consistently stays below the safety threshold (dashed line) compared to baselines.

As shown in the cost analysis, SaFormer (blue line) consistently maintains the cost below the specified limit (dashed line). In contrast, baselines like the standard Decision Transformer (DT) and Constrained Policy Optimization (CPQ) frequently overshoot the safety budget, failing to provide the necessary guarantees for safe deployment.

Zero-Shot Adaptation and Fine-tuning

By conditioning the model on a "Cost Limit" token, safety becomes a controllable "prompt." SaFormer successfully satisfied 91.6% of cost limits when tested on a range of different thresholds it hadn't seen during training. For extremely strict, out-of-distribution constraints, the paper introduces a fine-tuning algorithm using Maximum Entropy RL to safely adapt to tighter limits.

Conclusion

SaFormer demonstrates that sequence modeling is inherently more stable than Q-learning for safe RL. By treating safety as a prompt and employing a "look before you leap" posterior verification strategy, it achieves competitive rewards while strictly adhering to specified constraints. Future work will look toward handling multiple overlapping constraints and reducing the computational overhead of the multi-candidate sampling process.

Summary updated

BibTeX
@misc{zhang2023saformer,
  author = {Qin Zhang and Linrui Zhang and Li Shen and Haoran Xu and Bowen Wang and Bo Yuan and Yongzhe Chang and Xueqian Wang},
  title = {{SaFormer: A Conditional Sequence Modeling Approach to Offline Safe Reinforcement Learning}},
  year = {2023},
  eprint = {2301.12203},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2301.12203},
}
Figure