Skip to content

[BUG] SAC / DiscreteSAC with skip_done_states=True don't bootstrap truncated steps #4755

Open

Description

@Bndrwe

Describe the bug

With skip_done_states=True, SACLoss and DiscreteSACLoss pick which next states to skip using the done key. A step cut off by a time limit is done but not terminated, so it gets skipped too, and its target comes out wrong with no error:

  • DiscreteSACLoss uses the bare reward (no bootstrap at all).
  • SACLoss runs the target critic on a zero action with zero log-prob instead of an action sampled from the policy.

The docstring says only terminating states are skipped, and TQCLoss already uses terminated for the same flag (with a test for this case), so this looks like a leftover from #2613.

To Reproduce

import torch
from tensordict import TensorDict
from tensordict.nn import TensorDictModule
from torchrl.data import OneHot
from torchrl.modules import ProbabilisticActor, OneHotCategorical
from torchrl.objectives import DiscreteSACLoss

torch.manual_seed(0)
n_obs, n_act, B = 3, 4, 8
actor = ProbabilisticActor(
    TensorDictModule(torch.nn.Linear(n_obs, n_act), ["observation"], ["logits"]),
    in_keys=["logits"], distribution_class=OneHotCategorical,
    spec=OneHot(n_act), return_log_prob=False,
)
qvalue = TensorDictModule(torch.nn.Linear(n_obs, n_act), ["observation"], ["action_value"])
loss = DiscreteSACLoss(actor, qvalue, num_actions=n_act, action_space="one-hot")

truncated = torch.zeros(B, 1, dtype=torch.bool)
truncated[:4] = True  # time limit hit, episode NOT terminated
td = TensorDict({
    "observation": torch.randn(B, n_obs),
    "action": torch.nn.functional.one_hot(torch.randint(n_act, (B,)), n_act),
    "next": {
        "observation": torch.randn(B, n_obs),
        "reward": torch.randn(B, 1),
        "done": truncated.clone(),
        "truncated": truncated.clone(),
        "terminated": torch.zeros(B, 1, dtype=torch.bool),
    },
}, [B])

with torch.no_grad():
    a = loss._compute_target(td.clone())
    loss.skip_done_states = True
    b = loss._compute_target(td.clone())
print("max diff on truncated rows:", (a - b)[:4].abs().max().item())
print("max diff on other rows:    ", (a - b)[4:].abs().max().item())
print("skip=True target == bare reward on truncated rows:",
      torch.allclose(b[:4], td["next", "reward"][:4].view(-1)))
max diff on truncated rows: 1.1633509397506714
max diff on other rows:     1.1920928955078125e-07
skip=True target == bare reward on truncated rows: True

Expected behavior

Turning on skip_done_states should only change speed, not results. Truncated steps should still be bootstrapped; only terminated ones should be skipped.

System info

torchrl from source at main (df1db3d), torch 2.15.0, numpy 2.5.5, Python 3.12.3, Linux.

Reason and Possible fixes

Mask on terminated (falling back to done when there's no terminated key), like TQCLoss. I have a fix with tests and will open a PR.

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions