Repository navigation
Adding support for collate_fn parameter in ParallelAwareDataloader class - #1021
Conversation
tianyu-l
left a comment
There was a problem hiding this comment.
looks good to me, pls fix linting and nit comments
| dp_rank: int, | ||
| dp_world_size: int, | ||
| batch_size: int, | ||
| collate_fn:Optional[Callable]=None, |
There was a problem hiding this comment.
could you fix linting by pre-commit run --all-files
There was a problem hiding this comment.
Fixed. Thank you!
| self.batch_size = batch_size | ||
| super().__init__(dataset, batch_size) | ||
| self.collate_fn = collate_fn |
There was a problem hiding this comment.
can we remove these two lines?
There was a problem hiding this comment.
Hi @tianyu-l , I remove the line self.collate_fn = collate_fn. We are not using this in this class, so we don't need to add it as instance attribute. Thank you for pointing out.
I hope I understood your feedback correctly.
|
We are still missing the logger import, right?
|
tianyu-l
left a comment
There was a problem hiding this comment.
LGTM. Can we add the logger import
from torchtitan.tools.logging import logger
according to @TJ-Solergibert ?
Added as suggested by @TJ-Solergibert . Thank you for the review and approval @tianyu-l ! |
…ass (pytorch#1021) Currently there is no provision to provide a custom collate function to the dataloader in torchtitan. This PR aims to address that. For example, if users are having multimodal datasets, they might want to collate the data in a specific way. They can't use the provided `build_hf_dataloader` function from `hf_dataset.py` today because the `ParallelAwareDataloader` class inside it does not take a `collate_fn` parameter. We can solve this by simply accepting a `collate_fn` parameter in the `__init__` of the `ParallelAwareDataloader` class. This class is inheriting from `StatefulDataLoader` where the collate function is already supported! All that we have to do is to accept this parameter and pass it while calling the init of the base class from ParallelAwareDataloader. An example usage after this change would be: ``` def build_hf_dataloader( dp_world_size: int, dp_rank: int, tokenizer: Tokenizer, job_config: JobConfig, infinite: bool = True, ) -> ParallelAwareDataloader: """Build a data loader for HuggingFace datasets.""" dataset_name = job_config.training.dataset dataset_path = job_config.training.dataset_path batch_size = job_config.training.batch_size seq_len = job_config.training.seq_len hf_ds = HuggingFaceDataset( dataset_name=dataset_name, dataset_path=dataset_path, tokenizer=tokenizer, seq_len=seq_len, dp_rank=dp_rank, dp_world_size=dp_world_size, infinite=infinite, ) # from <my data utils> import custom_collate_fn return ParallelAwareDataloader( dataset=hf_ds, dp_rank=dp_rank, dp_world_size=dp_world_size, batch_size=batch_size, collate_fn=custom_collate_fn ) ```
…ass (pytorch#1021) Currently there is no provision to provide a custom collate function to the dataloader in torchtitan. This PR aims to address that. For example, if users are having multimodal datasets, they might want to collate the data in a specific way. They can't use the provided `build_hf_dataloader` function from `hf_dataset.py` today because the `ParallelAwareDataloader` class inside it does not take a `collate_fn` parameter. We can solve this by simply accepting a `collate_fn` parameter in the `__init__` of the `ParallelAwareDataloader` class. This class is inheriting from `StatefulDataLoader` where the collate function is already supported! All that we have to do is to accept this parameter and pass it while calling the init of the base class from ParallelAwareDataloader. An example usage after this change would be: ``` def build_hf_dataloader( dp_world_size: int, dp_rank: int, tokenizer: Tokenizer, job_config: JobConfig, infinite: bool = True, ) -> ParallelAwareDataloader: """Build a data loader for HuggingFace datasets.""" dataset_name = job_config.training.dataset dataset_path = job_config.training.dataset_path batch_size = job_config.training.batch_size seq_len = job_config.training.seq_len hf_ds = HuggingFaceDataset( dataset_name=dataset_name, dataset_path=dataset_path, tokenizer=tokenizer, seq_len=seq_len, dp_rank=dp_rank, dp_world_size=dp_world_size, infinite=infinite, ) # from <my data utils> import custom_collate_fn return ParallelAwareDataloader( dataset=hf_ds, dp_rank=dp_rank, dp_world_size=dp_world_size, batch_size=batch_size, collate_fn=custom_collate_fn ) ```
Currently there is no provision to provide a custom collate function to the dataloader in torchtitan. This PR aims to address that.
For example, if users are having multimodal datasets, they might want to collate the data in a specific way. They can't use the provided
build_hf_dataloaderfunction fromhf_dataset.pytoday because theParallelAwareDataloaderclass inside it does not take acollate_fnparameter.We can solve this by simply accepting a
collate_fnparameter in the__init__of theParallelAwareDataloaderclass. This class is inheriting fromStatefulDataLoaderwhere the collate function is already supported! All that we have to do is to accept this parameter and pass it while calling the init of the base class from ParallelAwareDataloader.An example usage after this change would be: