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

Conversation

flishwang
Copy link

@flishwang flishwang commented May 16, 2025

Checklist Before Starting

  • Search for similar PR(s).
    I found nothing similar

What does this PR do?

Simplify the usage of DataProto

I modified __getitem__ and __setitem__ so that we can:
(1) use data['input_ids'] instead of data.non_tensor_batch['input_ids'] or data.batch['input_ids'] if 'input_ids' exists in data's batch, non_tensor_batch or meta_info.
(2) update data by data[1:5] = new_data[2:6] if there's no extra keys in new_data.

Specific Changes

(1) Update __getitem__ for DataProto
(2) Add __setitem__ for DataProto

API

  • data[key] for DataProto data
    Before: data['key'] will raise a ValueError
    After: it will returns data.batch['key'] or data.non_tensor_batch['key'] or data.meta_info['key'] if key in any of them.

  • data[index]=other_data for DataProto data and other_data
    Before: data[index]=other_data will raise an Exception as there's no __setitem__ method for DataProto
    After: the values in rows of index (which could be a integer, ndarray or slice) will updated by other_data.

Checklist Before Submitting

  • Read the Contribute Guide.
  • Apply pre-commit checks.
  • Add [BREAKING] to the PR title if it breaks any API.
  • Update the documentation about your changes in the docs.
  • Add CI test(s) if neccessary.

Modify __getitem__ and __setitem__ so that we can:
(1) use data['input_ids'] instead of data.non_tensor_batch['input_ids'] or data.batch['input_ids'] if 'input_ids' exists in data's batch, non_tensor_batch or meta_info
(2) update data by data[1:5] = new_data[2:6] if all keys of new_data are in data.
@CLAassistant
Copy link

CLAassistant commented May 16, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants