Skip to content

update __getitem__ and __setitem__ for DataProto #1541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions verl/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,31 @@ def __getitem__(self, item):
tensor_data = self.batch[item]
non_tensor_data = {key: val[item] for key, val in self.non_tensor_batch.items()}
return DataProtoItem(batch=tensor_data, non_tensor_batch=non_tensor_data, meta_info=self.meta_info)

# # Case 4: Unsupported type
# Case 4: string - return the column in batch/non_tensor_batch or meta_info
elif isinstance(item, str):
if item in self.batch:
return self.batch[item]
elif item in self.non_tensor_batch[item]:
return self.non_tensor_batch[item]
elif item in self.meta_info:
return self.meta_info[item]
else:
raise KeyError(f'The Proto does not have key {item} in its batch or meta_info')
# # Case 5: Unsupported type
else:
raise TypeError(f"Indexing with {type(item)} is not supported")

def __setitem__(self, index, value):
extra_keys = (value.batch.keys() - self.batch.keys()) | (value.non_tensor_batch.keys() - self.non_tensor_batch.keys())
if extra_keys:
raise KeyError(f'Columns {extra_keys} exists in source but not in target, cannot update values in partial rows')
if not isinstance(value, (DataProto, DataProtoItem)):
raise ValueError('value must be a DataProto or DataProtoItem')
for target_key, target_value in value.batch.items():
self.batch[target_key][index] = target_value
for target_key, target_value in value.non_tensor_batch.items():
self.non_tensor_batch[target_key][index] = target_value

def __getstate__(self):
import io

Expand Down