Skip to content

[BUG] The offline-to-online example crashes on its first online batch #4765

Backlog

Description

@sramireddy2

Describe the bug

sota-implementations/offline_to_online/train.py stops on its first online batch, for three separate reasons:

  1. SACTrainer registers UTDRHook when logging is enabled (the default), and the hook reads replay_buffer.write_count. OfflineToOnlineReplayBuffer has no write_count.
  2. OfflineToOnlineReplayBuffer.sample concatenates an offline and an online sample. The D4RL/Minari datasets are TensorDictReplayBuffer subclasses, which write an index entry into every sample; the online ReplayBuffer sample has none, so the concat raises. OfflineToOnlineReplayBufferHook aligns the stored keys, but index is only added at sample time.
  3. The example env has no RewardSum, so the trainer's default r_total logging (episode_reward_key="reward_sum") gets None and raises AttributeError: 'NoneType' object has no attribute 'max'.

The trainer tests don't hit these because they set enable_logging=False and use a plain ReplayBuffer as the offline dataset.

To Reproduce

import torch
from tensordict import TensorDict
from torchrl.data import (
    LazyTensorStorage,
    OfflineToOnlineReplayBuffer,
    TensorDictReplayBuffer,
)


def transitions(n):
    return TensorDict({"observation": torch.randn(n, 3), "action": torch.randn(n, 1)}, [n])


# Minari/D4RL datasets are TensorDictReplayBuffer subclasses.
offline = TensorDictReplayBuffer(storage=LazyTensorStorage(100))
offline.extend(transitions(100))
rb = OfflineToOnlineReplayBuffer(
    offline_dataset=offline, online_capacity=100, batch_size=8
)
rb.extend(transitions(10))

try:
    rb.write_count  # read by UTDRHook, which SACTrainer registers when logging
except AttributeError as err:
    print("write_count:", err)

try:
    rb.sample()
except KeyError as err:
    print("sample:", err)

On main at df1db3d:

write_count: 'OfflineToOnlineReplayBuffer' object has no attribute 'write_count'
sample: "tensordict 1 has keys ['action', 'observation'] but tensordict 0 has keys ['action', 'index', 'observation']; all tensordicts must have the same keys"

D4RL and Minari aren't installed here, so I also ran the example end to end on CPU with --env Pendulum-v1 and load_dataset swapped for a TensorDictReplayBuffer filled from a rollout. On main it stops at the missing reward_sum; fixing each error exposes the next (write_count, then index).

Expected behavior

The example trains with its default settings.

System info

Installed from source (main at df1db3d), Python 3.12.15, torch 2.15.0+cpu, tensordict 0.15.5, Linux (WSL2 Ubuntu 24.04).

Reason and Possible fixes

  1. Give OfflineToOnlineReplayBuffer write_count (the online buffer's, since the offline dataset is never written to) and batch_size.
  2. Before the concat, drop the keys that the offline sampler reports (e.g. index) and the online sample doesn't have. Dropping every reported key would be wrong: SliceSampler also reports data keys such as done.
  3. Add RewardSum(in_keys=["reward"], out_keys=["reward_sum"]) to the example env, as the sac_trainer recipe does.

With these three changes the example runs to the end in the setup above. I'll open a PR with tests.

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