-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add from_dict to HFDatasetDataModule #11559
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
Merged
ericharper
merged 7 commits into
main
from
akoumparouli/make_HFDatasetDataModule_arg_accept_path_or_dataset
Dec 12, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90a1536
Add from_dict method
akoumpa 6672419
add test_load_from_dict
akoumpa a10326e
fix
akoumpa 0644c57
fix
akoumpa 8e82e57
add test_load_from_dict
akoumpa 11f1202
fix
akoumpa 329d737
Apply isort and black reformatting
akoumpa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,6 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import datasets.dataset_dict | ||
| import lightning.pytorch as pl | ||
| import torch | ||
| from datasets import load_dataset | ||
|
|
@@ -22,46 +21,51 @@ | |
| from nemo.utils import logging | ||
|
|
||
|
|
||
| def make_dataset_splits(path, split, split_aliases, kwargs): | ||
| def make_dataset_splits(dataset, split, split_aliases): | ||
| """ | ||
| Loads a dataset with datasets.load_dataset and | ||
| returns a dictionary containing all dataset splits. | ||
| Given a dataset (e.g. from datasets.load_dataset or datasets.Dataset.from_dict) it | ||
| returns a dictionary containing the corresponding dataset splits. | ||
|
|
||
| For example: | ||
|
|
||
| ans = make_dataset_splits("dataset-id") | ||
| $ ds = load_dataset("dataset-id") | ||
| $ print(ds) | ||
| > DatasetDict({ | ||
| > train: Dataset({ | ||
| > features: ['id', 'title', 'context', 'question', 'answers'], | ||
| > num_rows: 87599 | ||
| > }) | ||
| > validation: Dataset({ | ||
| > features: ['id', 'title', 'context', 'question', 'answers'], | ||
| > num_rows: 10570 | ||
| > }) | ||
| > }) | ||
|
|
||
| In this case the value of `ans` (returned value) will be: | ||
| $ ds = load_dataset("dataset-id") | ||
| $ ans = make_dataset_splits(ds) | ||
|
|
||
| # `ds` contains the following | ||
| $ print(ds) | ||
| > DatasetDict({ | ||
| > train: Dataset({ | ||
| > features: ['id', 'title', 'context', 'question', 'answers'], | ||
| > num_rows: 87599 | ||
| > }) | ||
| > validation: Dataset({ | ||
| > features: ['id', 'title', 'context', 'question', 'answers'], | ||
| > num_rows: 10570 | ||
| > }) | ||
| > }) | ||
|
|
||
| # In this case the value of `ans` (returned value) will be: | ||
| $ print(ans) | ||
| > { | ||
| > "train": Dataset .. (with 87599 rows), | ||
| > "val": Dataset .. (with 10570 rows), | ||
| > } | ||
| """ | ||
| dataset = load_dataset(path, split=split, **kwargs) | ||
| from datasets import Dataset, DatasetDict | ||
|
|
||
| split_names = ['train', 'test', 'val'] | ||
| dataset_splits = {split: None for split in split_names} | ||
| dataset_splits = {_split: None for _split in split_names} | ||
|
|
||
| alias_to_split = {} | ||
| for split_name, _split_aliases in split_aliases.items(): | ||
| assert split_name in split_names | ||
| for alias in _split_aliases: | ||
| alias_to_split[alias] = split_name | ||
|
|
||
| if isinstance(dataset, datasets.dataset_dict.DatasetDict): | ||
| if isinstance(dataset, Dataset): | ||
| assert isinstance(split, str), "Expected split to be a string, but got " + str(type(split)) | ||
| dataset_splits[split] = dataset | ||
| elif isinstance(dataset, DatasetDict): | ||
| dataset_split_names = dataset.keys() | ||
| logging.info(f"HF dataset has the following splits: {dataset_split_names}") | ||
| for alias_split_name, split in dataset.items(): | ||
|
|
@@ -89,9 +93,8 @@ def make_dataset_splits(path, split, split_aliases, kwargs): | |
| else: | ||
| raise ValueError("Expected split name to be None, str or a list") | ||
|
|
||
| assert ( | ||
| sum(map(lambda x: x is not None, dataset_splits.values())) > 0 | ||
| ), "Expected at least one dataset to have been initialized" | ||
| num_init_splits = sum(map(lambda x: x is not None, dataset_splits.values())) | ||
| assert num_init_splits > 0, f"Expected at least one split to have been initialized {num_init_splits}" | ||
| return dataset_splits | ||
|
|
||
|
|
||
|
|
@@ -111,9 +114,9 @@ class HFDatasetDataModule(pl.LightningDataModule): | |
|
|
||
| def __init__( | ||
| self, | ||
| path, | ||
| collate_fn=None, | ||
| path_or_dataset, | ||
| split=None, | ||
| collate_fn=None, | ||
| num_workers=2, | ||
| pin_memory=True, | ||
| persistent_workers=True, | ||
|
|
@@ -130,16 +133,26 @@ def __init__( | |
| ) -> None: | ||
| super().__init__() | ||
| assert pad_token_id is not None | ||
|
|
||
| logging.info(f"Loading HF dataset from {path}") | ||
| from datasets import Dataset, DatasetDict | ||
|
|
||
| # A dataset usually will have several splits (e.g. train, val, test, etc). | ||
| # We map synonym names to canonical names (train, test, val). | ||
| # A synonym can be a prefix/suffixed word e.g. train <> training. | ||
| split_aliases = {'train': train_aliases, 'test': test_aliases, 'val': val_aliases} | ||
|
|
||
| # self.dataset_splits will hold the actual dataset for each split. | ||
| self.dataset_splits = make_dataset_splits(path, split, split_aliases, kwargs) | ||
| if isinstance(path_or_dataset, str): | ||
| logging.info(f"Loading HF dataset from {path_or_dataset}") | ||
| dataset = load_dataset(path_or_dataset, split=split, **kwargs) | ||
| elif isinstance(path_or_dataset, Dataset) or isinstance(path_or_dataset, DatasetDict): | ||
| logging.info(f"Using passed HF dataset {str(path_or_dataset)}") | ||
| dataset = path_or_dataset | ||
| else: | ||
| raise ValueError( | ||
| "Expected `path_or_dataset` to be str, Dataset, DatasetDict, but got " + str(type(path_or_dataset)) | ||
| ) | ||
|
|
||
| self.dataset_splits = make_dataset_splits(dataset, split, split_aliases) | ||
|
|
||
| if collate_fn is None: | ||
| self._collate_fn = lambda x: HFDatasetDataModule.collate_fn(x, pad_token_id=self.pad_token_id) | ||
|
|
@@ -157,6 +170,13 @@ def __init__( | |
| self.use_mcore_sampler = use_mcore_sampler | ||
| self.mcore_dataloader_type = mcore_dataloader_type | ||
|
|
||
| @staticmethod | ||
| def from_dict(dataset_dict, split, **kwargs): | ||
| from datasets import Dataset | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move all |
||
|
|
||
| dataset = Dataset.from_dict(dataset_dict) | ||
| return HFDatasetDataModule(path_or_dataset=dataset, split=split, **kwargs) | ||
|
|
||
| @staticmethod | ||
| def collate_fn(batch, pad_token_id=0): | ||
| def batchify(tensor): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to top level? Same as the other comment