Q-learning Penalized Transformer (QPT): a training-inference consistent framework for safe offline reinforcement learningQ-learning Penalized Transformer for Safe Offline Reinforcement Learning
QPT, on DSRL's 38 tasks, uses a Q-penalized conditional Transformer to simultaneously satisfy cost constraints and improve return, and can adapt zero-shot to different cost thresholds.
- Safe offline reinforcement learning must simultaneously satisfy safety constraints, maximize return, and adhere to behavior regularization on a fixed dataset, and these three conflict with one another. Lagrangian methods fix the cost threshold during training, and changing the threshold usually requires retraining.
- QPT uses a conditional Transformer to generate actions from trajectory context and target return/cost, and uses reward and cost Q functions to form a Q-shaped penalty. At inference, the same set of Q functions selects the highest-reward action within the cost threshold, combined with data augmentation for infeasible targets.
- On DSRL's 38 tasks, the authors claim QPT is the only method with normalized cost below 1 on all tasks and the highest reward on most tasks. SafetyGym, BulletGym, and MetaDrive average reward/cost are 0.42/0.69, 0.62/0.60, and 0.71/0.84, respectively. Ablations show the components work together, and the cost threshold can be changed zero-shot.
- The authors place the theory on stylized, near-deterministic CMDPs, with proofs in Appendix A. The experiments cover 38 tasks across three DSRL environment types, with a unified cost limit of 10, and metrics averaged over 20 evaluation episodes and 3 random seeds.
- Benchmarks
- DSRLSafety-GymnasiumBullet-Safety-GymMetaDrive
- Metrics
- normalized rewardnormalized cost
The researchers propose Q-learning Penalized Transformer (QPT), a training-inference consistent framework that combines conditional sequence modeling with constraint-aware value estimation for safe offline reinforcement learning.
Paper analysis
What problem does this paper attempt to solve?
Satisfy safety constraints, maximize return, and maintain behavior regularization on offline data, while being able to change the cost threshold at deployment.
Safe offline reinforcement learning aims to learn a policy from an offline dataset that achieves both high return and satisfies cost constraints without interacting with the environment, and to allow changing the constraint threshold at deployment.
- Scenario and importance: The authors argue that offline RL is suitable for sequential decision-making where interaction is expensive or unsafe, such as autonomous driving and control systems. In real applications, maximizing a scalar reward alone is insufficient; safety constraints restrict the feasible solutions, and satisfying constraints can expand RL's applicability and improve reliability in safety-critical scenarios.
- Three-objective conflict: The authors formulate the problem as three mutually constraining objectives: satisfying safety constraints, maximizing reward, and the behavior regularization imposed by offline data. When the dataset contains unsafe trajectories, matching the behavior distribution harms constraint satisfaction; removing unsafe trajectories may discard high-return transitions. The authors state that joint optimization often leads to training instability and suboptimal safety performance, and these objectives may themselves conflict.
- Shortcomings of existing methods: The authors point out that Lagrangian-based methods often treat the threshold κ as a training constant. In the offline setting, value estimation is biased, and updates of the dual variable relative to a fixed threshold may yield unsafe or overly conservative behavior; changing the threshold post hoc destabilizes the dual variable, typically requiring retraining for each new constraint. Conditional sequence modeling can adapt to different thresholds zero-shot, but the authors state it tends to imitate the behavior distribution in the dataset, which often contains unsafe trajectories.
- Core objective: The authors want a training paradigm that allows the agent to dynamically adjust the constraint threshold at deployment, thereby controlling the degree of conservatism, without fine-tuning or retraining.
- Proposed method: The authors propose Q-learning Penalized Transformer policy (QPT), which couples conditional sequence modeling with constraint-aware value estimation in the same training–inference pipeline: during training, reward and cost Q functions are used for Q-shaped penalization; at inference, the same Q functions select the highest-reward feasible action under the cost threshold. They also provide a theoretical analysis on stylized, near-deterministic CMDPs.
What related research exists?
Related work separates offline RL, safe RL, and safe offline RL; the authors write return and cost as tunable loss terms.
- Offline reinforcement learning: Levine et al. (2020) learn a policy from a static dataset D without online interaction. Distribution shift can cause the learned policy to deviate from the behavior policy and degrade performance (Fujimoto et al., 2019). Prior work uses constrained or regularized dynamic programming to limit policy deviation (Fujimoto & Gu, 2021; Kumar et al., 2020; Kostrikov et al., 2021b). Conditional sequence modeling predicts future actions from history, constraining the policy within the behavior boundary and supporting zero-shot adaptation (Chen et al., 2021a; Hu et al., 2025c; a; b; 2026; 2024c).
- Safe reinforcement learning: Safe RL maximizes long-term reward while satisfying safety constraints (Wachi & Sui, 2020; Gu et al., 2024; Liu et al., 2025). A common approach is primal-dual, which uses Lagrange multipliers to convert the constrained problem into unconstrained optimization (Chen et al., 2021b). Correction-based methods project unsafe actions onto the safe set and incorporate domain knowledge to improve exploration safety (Zhao et al., 2021; Luo & Ma, 2021). Model-based RL is used to improve data efficiency and performance (Huang et al., 2023), and the authors state it often requires larger models to parameterize environment dynamics, thereby increasing computational complexity.
- Safe offline reinforcement learning: This direction aims to guarantee zero constraint violation at inference time by combining offline RL with safety constraints. One line uses DICE-style constrained optimization (Polosky et al., 2022; Lee et al., 2022; Kim et al., 2025), and another uses Lagrangian methods for compatibility with existing offline RL. Recent work also introduces new networks (Koirala et al., 2024b; a; Gong et al., 2025). CDT (Liu et al., 2023b; Wang & Zhou, 2024) uses sequence modeling to learn from trajectory data; TREBI (Lin et al., 2023) directly generates safe trajectories, and FISOR (Zheng et al., 2024) uses a diffusion actor to constrain actions within the feasible region. There is also a data-centric line that mitigates distribution shift and OOD behavior by constructing or augmenting datasets (Guo et al., 2025; Yao et al., 2024; Gong et al., 2025).
- Baseline methods and benchmarks: The experimental baselines fall into four categories and are compared on the Safety-Gymnasium, Bullet-Safety-Gym, and MetaDrive tasks in DSRL (Liu et al., 2023a). Q-learning-based methods are CPQ (Xu et al., 2022), BCQ-Lag (Fujimoto et al., 2019; Stooke et al., 2020), CAPS (Chemingui et al., 2025); distribution correction is COptiDICE (Lee et al., 2022; 2021); imitation learning includes BC-Safe (Liu et al., 2023a) trained only on safe trajectories, the diffusion policy FISOR (Zheng et al., 2024), and OASIS (Yao et al., 2024) with diffusion data augmentation using BCQ-Lag as the underlying policy; sequence modeling is CDT (Liu et al., 2023b) with cost-to-go tokens.
The authors position this work as: unlike the above methods, reward maximization and cost control are written as controllable loss terms, thereby directly trading off performance and safety during optimization.
How does the paper address this problem?
QPT uses a conditional Transformer to imitate trajectories, then uses reward and cost Q functions to penalize toward high reward and low violation.
QPT adds a penalty composed of a reward Q and a cost Q to the conditional Transformer's behavior cloning objective, biasing generation toward high reward and low constraint violation; at inference it reuses the same set of Q functions to select the feasible action with the highest reward under a given cost.
Problem Setup and Formalization
- CMDP: RL under safety constraints is formulated as a Constrained Markov Decision Process, with tuple M = (S, A, T, R, C, μ0). C maps state–action pairs to [0, Cmax].
- Data and Objective: Given an offline dataset D = {(s, a, r, c, s′)i}, generated by one or more unknown behavior policies. A policy π : S → A should maximize expected return while satisfying the cost constraint. Equation (1) is max_π E_{τ∼π}[R(τ)], subject to E_{τ∼π}[C(τ)] ≤ κ, where R(τ) and C(τ) are the trajectory's cumulative reward and cumulative cost, respectively, and κ ∈ [0, +∞) is the cost threshold.
- The authors' two criticisms of existing approaches: In Lagrangian duality, bias in cost estimation makes the dual variable update inaccurate relative to a fixed κ; the threshold is fixed during training, so changing the threshold requires retraining.
Conditional Transformer Policy
- Sequence: Following Decision Transformer, and adding cost-to-go as in safe offline settings. Return-to-go is r̂_t = Σ_{i=t}^T r_i, and cost-to-go is ĉ_t = Σ_{i=t}^T c_i. A length-K context sequence τ_t interleaves r̂, ĉ, s, a according to equation (2).
- Training Objective: The prediction head corresponding to the state token s_t predicts the action a_t. Continuous actions use MSE, i.e., L_DT in equation (3). The authors state this preserves behavior regularization, but it imitates unsafe trajectories in the data, and direct learning may yield an unsafe policy; filtering out unsafe trajectories removes high-reward transitions.
Q-learning Penalty
- Two Q functions: The reward Q and cost Q are evaluated by empirical Bellman, equations (4)(5). To mitigate overestimation, each uses double Q-learning: Qr_φ1, Qr_φ2 and Qc_ψ1, Qc_ψ2, each with target networks, plus a target policy π̂_θ′. Because the Transformer input includes trajectory history, Q uses n-step Bellman updates, taking the min over the two Q's in the target, see equations (6)(7).
- Joint Objective: Equation (8) is L(θ) = L_DT(θ) − L_Qr(θ) + L_Qc(θ), i.e., minimizing behavior cloning error, subtracting the reward Q (encouraging high reward), and adding the cost Q (suppressing cost). α1 and α2 are normalized by the expected absolute value of Q following the idea of Fujimoto & Gu (2021), equations (9)(10), controlled by hyperparameters η1 and η2; the Q in the denominator is used only for normalization and does not participate in differentiation. The authors state this is to enhance stitching: preferentially sampling high-reward actions while biasing toward low-cost trajectories, without discarding data.
Data Augmentation and Inference Ensemble
- Infeasible Targets: When the target return–cost pair (ρ, κ) is infeasible, equation (11) selects from D the trajectory τ* satisfying C(τ) ≤ κ with the highest return, then equation (12) shifts return-to-go and cost-to-go to that target, keeping the states and actions of τ*. The authors state this prioritizes satisfying the target cost when objectives conflict, and imitates the "most rewarding and safe" trajectory. Details are in Appendix B.2; prompt constraints are not elaborated in the main text.
- Inference: No longer manually trying return-to-go and cost-to-go. For candidate target pairs (r̂^j, ĉ^j), actions are generated in parallel with the policy, then equations (13)(14) maximize Qr_φ′(s_t, â) subject to Qc_ψ′(s_t, â) ≤ ĉ. The authors state that more candidate pairs enlarge the search space, but increase computation, and with biased Q estimates it is easier to select noisy or suboptimal pairs. The default ensemble size is 50 candidate actions. Training and inference procedures are in Algorithm 1.
Theoretical Analysis and Success Criteria
- Setup: Theorem 3.1 considers binary rewards and costs, behavior policy β, and conditional functions f^r and f^c. Assumptions include: return coverage P_β(g^r(τ) = f^r(s1) | s1) ≥ α_{f^r}, cost coverage similar with lower bound α_{f^c}; near-determinism, where transitions, rewards, or costs deviate from some functions T, R, C with probability at most ε; and f^r, f^c being consistent along trajectories.
- Conclusion: The authors state that with high probability, relative to a sequence model optimizing only equation (3), the Q penalty in equation (8) increases the probability of selecting the maximum-reward action and the minimum-cost action, and gives an upper bound on the expected return gap and an upper bound on the expected cost gap, both containing ε(1/α + 3)H^2 − Hσ. The full proof is in Appendix A.
- Evaluation Criteria: In experiments, the primary safety criterion is normalized cost ≤ 1; only after satisfying safety is higher normalized reward pursued. The raw cost limit for all tasks is set to 10, and the normalized threshold corresponds to 1.
What experiments did the paper conduct?
Across the 38 DSRL tasks, QPT's average cost is below 1, and it runs component, hyperparameter, and zero-shot threshold experiments.
Experimental Setup
- Environments and Scale: DSRL (Liu et al., 2023a) has 38 tasks in total. Table 3: Safety-Gymnasium is Mujoco, Goal/Button/Push/Circle with Point, Car (difficulty level 2, 16 tasks, 40310 trajectories), Velocity with Ant, HalfCheetah, Hopper, Swimmer, Walker2d (level 1, 5 tasks, 11399 trajectories); Bullet-Safety-Gym is PyBullet's Run, Circle, with agents Ball, Car, Drone, Ant (8 tasks, 14498 trajectories); MetaDrive is Panda3D driving, Vehicle, difficulty level 3, 9 tasks, 9000 trajectories.
- Compared Methods: CPQ, BCQ-Lag, CAPS, COptiDICE, BC-Safe, FISOR, OASIS, CDT. The authors state baseline code comes from Liu et al. (2023a) and was rerun by the authors.
- Metrics: normalized reward follows equation (15), normalizing by the empirical maximum and minimum returns r_max(M), r_min(M) on task M and then multiplying by 100; normalized cost follows equation (16) as (C_π + ε) / (κ + ε). C_normalized ≤ 1 is judged safe. The main text subsequently abbreviates these as reward and cost. If cost < 1, the configuration with the highest reward is reported; otherwise, the minimum cost is prioritized and the corresponding reward is reported.
- Protocol: Each value is the average of 20 evaluation episodes and 3 random seeds. The cost limit for all tasks is set to 10, and the normalized cost threshold in Table 1 is 1.
- QPT Hyperparameters (Table 4, all tasks): 3 layers, 8 attention heads, embedding dimension 128, context K = 10, dropout 0.1, gradient clipping 0.25, batch size 2048, learning rate 0.0001, Adam betas (0.9, 0.999). BulletGym and SafetyGym train for 100000 steps, MetaDrive for 200000 steps. Inference uses 50 candidate target pairs by default.
Main Results
Table 1 gives all 38 tasks and the three group averages. The table below lists only the three group averages (reward / cost); safety means cost < 1. Per-task numbers are in Table 1 and are not relisted item by item here.
Swipe horizontally to read the table
Group QPT BC-Safe CDT BCQ-Lag CPQ COptiDICE FISOR OASIS CAPS SafetyGym average 0.42 / 0.69 0.36 / 0.74 0.51 / 2.73 0.40 / 6.70 0.24 / 3.45 0.34 / 6.45 0.31 / 2.01 0.13 / 4.37 0.28 / 1.27 BulletGym average 0.62 / 0.60 0.58 / 0.44 0.61 / 0.95 0.60 / 1.54 0.39 / 0.29 0.48 / 2.12 0.42 / 0.14 0.52 / 1.16 0.51 / 0.72 MetaDrive average 0.71 / 0.84 0.36 / 0.13 0.42 / 0.57 0.29 / 0.64 −0.05 / 0.10 0.07 / 0.93 0.49 / 0.20 −0.02 / 0.09 0.20 / 0.10 - Authors' overall statement on QPT: The authors claim QPT is the only method that achieves satisfactory safety on all tasks and the highest reward in most cases. In Table 1, QPT's cost on each task does not exceed 0.99, and the three group average costs are 0.69, 0.60, and 0.84.
- Authors' interpretation of baselines: BC-Safe satisfies most safety requirements but is more conservative with lower reward. BCQ-Lag, CPQ, and COptiDICE behave inconsistently, swinging between over-conservatism and excessive risk; the authors give the example of CPQ achieving high reward with safety violations on CarGoal1 and CarGoal2, and near-zero cost but very low reward on MetaDrive. FISOR prioritizes safety with lower reward. CDT is more balanced, but the authors state it fails to satisfy safety on most SafetyGym tasks; in Table 1, CDT's SafetyGym average cost is 2.73, above the threshold of 1.
- Same architecture as CDT: The authors state QPT and CDT are both Transformers, and claim QPT achieves higher reward while consistently satisfying safety. According to Table 1, on SafetyGym CDT's average reward 0.51 is higher than QPT's 0.42, but CDT's average cost 2.73 exceeds 1; on BulletGym, BC-Safe's average cost 0.44 is lower than QPT's 0.60, CPQ's average cost 0.29 is even lower, and CPQ's average reward is 0.39.
Component Ablation
Table 2 reports mean ± standard deviation over 3 seeds on MetaDrive's harddense and HopperVelocity, turning on data augmentation, training-time Qr, Qc, and inference ensemble one by one.
- Full model: Exp 8 (all four components on) gives harddense reward 0.50 ± 0.02, cost 0.81 ± 0.03; HopperVelocity gives 0.88 ± 0.02, 0.45 ± 0.04. Exp 1 with no components gives 0.37 ± 0.19 / 1.00 ± 0.08 and 0.04 ± 0.02 / 1.49 ± 0.12, respectively, with the latter's cost exceeding 1.
- Authors' interpretation: Comparing Exp 2 with 3 and Exp 2 with 4, adding the Qr or Qc penalty changes reward or cost; Exp 4 (augmentation + Qc only) reduces harddense cost to 0.14 ± 0.10 and HopperVelocity cost to 0.22 ± 0.01, but HopperVelocity reward is only 0.15 ± 0.02. Comparing Exp 2 with 6 and Exp 5 with 8, inference ensemble further improves performance. Comparing Exp 1 with 2 and Exp 7 with 8, data augmentation improves cost. Exp 7 without augmentation has harddense cost 0.93 ± 0.02 and HopperVelocity reward 0.66 ± 0.03.
Hyperparameters and Zero-Shot Adaptation
- η1, η2: Figure 1 on SwimmerVelocity and HopperVelocity fixes one side and sweeps the other, with values including 0, 0.01, 0.1, 0.5, 1.0, 2.0; curves are means over 3 seeds with 20 trajectories per seed, light bands are one standard deviation, and the horizontal axis is training steps (×10^5). The authors state that with η2 fixed, increasing η1 raises normalized reward within a certain range; on HopperVelocity, reward keeps rising for η1 ≤ 1, and at η1 = 2 reward decreases and no longer improves, while cost rises within a certain range. With η1 fixed, increasing η2 gradually decreases reward and decreases cost within a certain range. The authors believe larger η2 helps produce safer policies, at the expense of reward within a specific range. Specific endpoint values in the figure are not listed point by point in the text.
- Zero-shot threshold changes: Figure 2 compares QPT with CDT on tasks including HopperVelocity, HalfCheetahVelocity, Walker2dVelocity, CarButton1, PointButton1, with the horizontal axis as Target Cost and a reference line Eval Cost = Target Cost. The authors state that Transformer-based policies can adapt zero-shot to different cost thresholds, while the earlier Q-learning baselines, whose thresholds are fixed at training time, lack this capability. Exact readings of points in the figure are not fully listed in the provided text.
- Number of candidates: Appendix C.3, Table 5 varies the number of candidate target pairs on MetaDrive harddense. reward / cost are: 1 gives 0.49 ± 0.04 / 0.84 ± 0.02; 10 gives 0.50 ± 0.02 / 0.83 ± 0.03; 30 gives 0.52 ± 0.03 / 0.90 ± 0.05; 50 gives 0.50 ± 0.02 / 0.81 ± 0.03; 100 gives 0.48 ± 0.04 / 0.80 ± 0.03. The authors state that more candidates first improve performance, then plateau and decline beyond a certain threshold; at 30, reward is highest at 0.52, and at 100, reward drops to 0.48 and cost drops to 0.80. The authors attribute the decline to increased computation and to noisy or suboptimal candidate pairs from biased Q estimates.
Other Ablations and Analysis
- The other quantitative analyses in the main text are concentrated in Table 2, Figure 1, Figure 2, and Table 5; no further ablation tables are listed separately.
- In Table 1, QPT does not have higher reward on every safety baseline: for example, on PointCircle1, COptiDICE reward is 0.78 but cost is 15.64, and FISOR reward is 0.60 but cost is 12.8; on HalfCheetahVelocity, BCQ-Lag reward is 1.04 and cost 57.06, while QPT is 1.01 / 0.03.
- On PointButton2, QPT reward is −0.01 and cost 0.88; on CarButton1 and CarButton2, QPT reward is −0.15 and −0.33, with cost 0.87 and 0.99.
- On the BulletGym average, CPQ's cost 0.29 is lower than QPT's 0.60, and BC-Safe's cost 0.44 is also lower; on the MetaDrive average, BC-Safe, OASIS, CAPS, and CPQ have lower cost than QPT, but OASIS and CPQ have negative average reward.
What are some points that can be further explored?
The theory is limited to stylized near-deterministic CMDPs; the experiments are 38 tasks on DSRL with a fixed cost limit of 10.
Limitations and future directions noted by the authors
The paper does not include a separate Limitations, Discussion, or Future Work section in the provided text. The scope restrictions explicitly stated by the authors are: the theoretical analysis is on stylized, near-deterministic CMDPs, and the improvement of Q penalization over ordinary sequence modeling objectives is discussed under mild coverage and regularity assumptions; the full proofs and discussion are in Appendix A (Section 3.5).
Experimental coverage
- The evaluated environments are Safety-Gymnasium, Bullet-Safety-Gym, and MetaDrive in DSRL, totaling 16 + 5 + 8 + 9 = 38 tasks in Table 3, with trajectory counts of 40310, 11399, 14498, and 9000, respectively.
- The comparisons are against eight baselines: CPQ, BCQ-Lag, CAPS, COptiDICE, BC-Safe, FISOR, OASIS, and CDT; the main table is Table 1, and the cost limit is set to 10 for all tasks.
- Each Table 1 value is the average over 20 evaluation episodes and 3 random seeds; Figure 1 also uses 3 seeds with 20 trajectories per seed.
- Component ablations are reported only on MetaDrive harddense and HopperVelocity (Table 2); the η1, η2 sweep is on SwimmerVelocity and HopperVelocity (Figure 1); the number of candidate target pairs is varied only on harddense with 1, 10, 30, 50, and 100 (Table 5).
- Training steps differ by environment: 100000 for BulletGym and SafetyGym, and 200000 for MetaDrive (Table 4). The paper does not report agreement with human review; the setup uses normalized reward and normalized cost, without LLM review.
Summarize the main content of the paper
QPT connects a conditional Transformer with reward/cost Q penalties into a training–inference-consistent safe offline RL method.
QPT targets safe offline RL: it learns only from offline data, simultaneously handling constraint satisfaction, return maximization, and behavior regularization, and aims to allow changing the cost threshold at deployment without retraining.
- Problem: Maximize E[R(τ)] on a CMDP with E[C(τ)] ≤ κ. Lagrangian methods fix κ during training, and offline value bias drives the dual update; pure conditional sequence modeling can change the condition, but the authors argue it imitates a data distribution containing unsafe trajectories.
- Method: A Transformer uses K steps of return-to-go, cost-to-go, state, and action as context and predicts actions with MSE. The reward Q and cost Q are learned with double Q and n-step Bellman, and the policy is penalized with −α1 Qr + α2 Qc, with α normalized by the scale of Q. Infeasible target return–cost pairs are replaced by the trajectory in the dataset with the highest return that satisfies the cost, then shifted by the target. At inference, among candidate actions whose cost Q does not exceed the target cost, the one with the largest reward Q is selected, with 50 candidates by default. Theorem 3.1 gives bounds on expected return and expected cost relative to ordinary sequence modeling under binary reward/cost, coverage, near-determinism, and consistency assumptions.
- Main results: In Table 1, over 38 tasks with cost threshold 1 and 20 episodes × 3 seeds per cell, QPT's average reward/cost on SafetyGym, BulletGym, and MetaDrive is 0.42/0.69, 0.62/0.60, and 0.71/0.84. The authors state it is the only method that is safe on all tasks and has the highest return on most tasks; in the same table, CDT's average cost on SafetyGym is 2.73, and BC-Safe is more conservative.
- Other evidence: The full configuration in Table 2 achieves 0.88/0.45 on HopperVelocity and 0.50/0.81 on harddense; removing all components gives a HopperVelocity cost of 1.49. In Table 5, harddense reward is 0.52 with 30 candidates and 0.48 with 100. In Figure 1, increasing η2 is accompanied by lower cost and lower reward; Figure 2 shows zero-shot variation with respect to the target cost.
- Authors' conclusion: The authors believe Q-penalized conditional generation can bias toward high return and low violation while retaining behavior regularization, and use the same Q functions to connect training and deployment, thereby improving both safety and return on DSRL and providing zero-shot adaptation to different constraint thresholds.