rl
rl copied to clipboard
[BUG] RuntimeError: There are more tensors (len(tensors)=3) than out_keys (out_keys=['logits'])
Describe the bug
When running the example ./examples/rlhf/train.py, we encountered this runtime error.
RuntimeError: There are more tensors (len(tensors)=3) than out_keys (out_keys=['logits']).
To Reproduce
Steps to reproduce the behavior.
python ./examples/rlhf/train.py
Traceback (most recent call last):
File "/path/to/torchrl/rl/./examples/rlhf/train.py", line 110, in main
model(batch)
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/tensordict/nn/common.py", line 328, in wrapper
return func(_self, tensordict, *args, **kwargs)
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/tensordict/nn/utils.py", line 372, in wrapper
result = func(_self, tensordict, *args, **kwargs)
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/tensordict/nn/common.py", line 1218, in forward
raise err from RuntimeError(
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/tensordict/nn/common.py", line 1201, in forward
tensordict_out = self._write_to_tensordict(
File "/path/to/miniconda3/envs/torchrl/lib/python3.10/site-packages/tensordict/nn/common.py", line 1118, in _write_to_tensordict
raise RuntimeError(
RuntimeError: There are more tensors (len(tensors)=3) than out_keys (out_keys=['logits']).
System info
import torchrl, numpy, sys
print(torchrl.__version__, numpy.__version__, sys.version, sys.platform)
TorchRL 0.10.0
tensordict 0.10.0
numpy 2.2.6
sys 3.10.18 (main, Jun 5 2025, 13:14:17) [GCC 11.2.0] linux
Reason and Possible fixes
In rl/examples/rlhf/models/transformer.py init_transformer
The output of GPT2LMHeadModel mismatches TensorDictModule's out_keys whether inference is True or False.
Checklist
- [x] I have checked that there is no similar issue in the repo (required)
- [x] I have read the documentation (required)
- [x] I have provided a minimal working example to reproduce the bug (required)
Thanks for reporting! These scripts are mostly deprecated -- i'll put up a PR to remove them