Commit c651b7b
authored
[misc] fix: support nested datastructure in dataproto to convert to tensordict (verl-project#4296)
## What does this PR do?
Fixes `ValueError: TensorDict conversion only supports... Got <class
'list'>` when converting `DataProto` with nested non-tensor data to
`TensorDict`.
**Problem:** Agent loop workflows with nested structures (lists of
lists, lists of dicts) in `non_tensor_batch` failed during
`to_tensordict()` conversion:
- `turn_scores`: `[[], [0.5, 0.8]]` - lists of varying lengths
- `reward_extra_info`: `[{"acc": 1.0}, {"acc": 0.0}]` - lists of dicts
- `raw_prompt`: `[[{"content": "...", "role": "user"}]]` - lists of
lists of dicts
- `tool_rewards`: `[[0.0], []]` - lists of lists
**Solution:** Wrap nested data in `NonTensorStack` (TensorDict's
supported type for non-tensor sequences) instead of converting to plain
Python lists.
**Impact:** Enables agent loop and multi-turn dialogue workflows to use
DataProto ↔ TensorDict conversions without errors.
---
## Test
Added 5 comprehensive tests in `tests/test_protocol_on_cpu.py`:
1. **`test_to_tensordict_with_nested_lists`** - Lists of lists (e.g.,
`turn_scores`)
2. **`test_to_tensordict_with_nested_dicts`** - Lists of dicts (e.g.,
`reward_extra_info`)
3. **`test_to_tensordict_with_complex_nested_structures`** - Lists of
lists of dicts (e.g., `raw_prompt`)
4. **`test_to_tensordict_and_back_with_nested_data`** - Round-trip data
integrity
5. **`test_to_tensordict_agent_loop_scenario`** - Real-world agent loop
scenario with all nested types
All tests verify:
- ✅ No conversion errors
- ✅ Data accessibility and correctness
- ✅ Round-trip conversion preserves data
Run tests:
```bash
pytest tests/test_protocol_on_cpu.py -k "test_to_tensordict" -v
```
---
## Design & Code Changes
### Modified Files
**1. `verl/protocol.py` (lines 1118-1133)**
```python
# Before: Plain list conversion (fails for nested structures)
tensor_batch[key] = val.tolist()
# After: Wrap in NonTensorStack
from tensordict.tensorclass import NonTensorData, NonTensorStack
tensor_batch[key] = NonTensorStack.from_list([NonTensorData(item) for item in val])
```
**2. `verl/utils/tensordict_utils.py` (lines 109-127)**
```python
# Add validation skip for NonTensorStack objects (already properly formatted)
if isinstance(val, NonTensorStack):
if batch_size is None:
batch_size = len(val)
continue
```
### Why This Works
- `NonTensorStack` is TensorDict's native type for storing sequences of
arbitrary Python objects
- Preserves nested structures (lists, dicts, complex objects) without
serialization
- Maintains batch semantics - each element corresponds to one batch
sample
- Enables round-trip conversion: `DataProto → TensorDict → DataProto`
without data loss
---
## Checklist Before Submitting
- [x] Read the Contribute Guide
- [x] Apply pre-commit checks
- [ ] Add/Update documentation (if needed - this is a bug fix, not new
API)
- [x] Add unit tests covering all code paths (5 new tests added)
- [ ] CI request (ready for review)
---
### Design & Code Changes
> Demonstrate the high-level design if this PR is complex, and list the
specific changes.
### Checklist Before Submitting
> [!IMPORTANT]
> Please check all the following items before requesting a review,
otherwise the reviewer might deprioritize this PR for review.
- [ ] Read the [Contribute
Guide](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md).
- [ ] Apply [pre-commit
checks](https://github.com/volcengine/verl/blob/main/CONTRIBUTING.md#code-linting-and-formatting):
`pre-commit install && pre-commit run --all-files --show-diff-on-failure
--color=always`
- [ ] Add / Update [the
documentation](https://github.com/volcengine/verl/tree/main/docs).
- [ ] Add unit or end-to-end test(s) to [the CI
workflow](https://github.com/volcengine/verl/tree/main/.github/workflows)
to cover all the code. If not feasible, explain why: ...
- [ ] Once your PR is ready for CI, send a message in [the `ci-request`
channel](https://verl-project.slack.com/archives/C091TCESWB1) in [the
`verl` Slack
workspace](https://join.slack.com/t/verl-project/shared_invite/zt-3855yhg8g-CTkqXu~hKojPCmo7k_yXTQ).
(If not accessible, please try [the Feishu group
(飞书群)](https://applink.larkoffice.com/client/chat/chatter/add_by_link?link_token=772jd4f1-cd91-441e-a820-498c6614126a).)
Signed-off-by: petersh6 <petershengwhu@gmail.com>1 parent d0997d2 commit c651b7b
File tree
3 files changed
+226
-1
lines changed- tests
- verl
- utils
3 files changed
+226
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
779 | 779 | | |
780 | 780 | | |
781 | 781 | | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
782 | 993 | | |
783 | 994 | | |
784 | 995 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1118 | 1118 | | |
1119 | 1119 | | |
1120 | 1120 | | |
| 1121 | + | |
| 1122 | + | |
1121 | 1123 | | |
1122 | 1124 | | |
1123 | 1125 | | |
1124 | 1126 | | |
1125 | 1127 | | |
1126 | 1128 | | |
1127 | 1129 | | |
1128 | | - | |
| 1130 | + | |
| 1131 | + | |
1129 | 1132 | | |
1130 | 1133 | | |
1131 | 1134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
113 | 124 | | |
114 | 125 | | |
115 | 126 | | |
| |||
0 commit comments