Repository navigation
Description
Describe the bug
sota-implementations/offline_to_online/train.py stops on its first online batch, for three separate reasons:
SACTrainerregistersUTDRHookwhen logging is enabled (the default), and the hook readsreplay_buffer.write_count.OfflineToOnlineReplayBufferhas nowrite_count.OfflineToOnlineReplayBuffer.sampleconcatenates an offline and an online sample. The D4RL/Minari datasets areTensorDictReplayBuffersubclasses, which write anindexentry into every sample; the onlineReplayBuffersample has none, so the concat raises.OfflineToOnlineReplayBufferHookaligns the stored keys, butindexis only added at sample time.- The example env has no
RewardSum, so the trainer's defaultr_totallogging (episode_reward_key="reward_sum") getsNoneand raisesAttributeError: '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
- Give
OfflineToOnlineReplayBufferwrite_count(the online buffer's, since the offline dataset is never written to) andbatch_size. - 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:SliceSampleralso reports data keys such asdone. - Add
RewardSum(in_keys=["reward"], out_keys=["reward_sum"])to the example env, as thesac_trainerrecipe 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