Skip to content

Adding support for collate_fn parameter in ParallelAwareDataloader class - #1021

Merged
tianyu-l merged 5 commits into
pytorch:mainfrom
abdulmuneer:data_collator_pass
Mar 28, 2025
Merged

tianyu-l merged 5 commits into
pytorch:mainfrom
abdulmuneer:data_collator_pass

Conversation

@abdulmuneer

Copy link
Copy Markdown
Contributor

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 
    )
    ```

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 27, 2025
@tianyu-l
tianyu-l requested review from pbontrager and tianyu-l March 27, 2025 18:45

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me, pls fix linting and nit comments

Comment thread torchtitan/components/dataloader.py Outdated
dp_rank: int,
dp_world_size: int,
batch_size: int,
collate_fn:Optional[Callable]=None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you fix linting by pre-commit run --all-files

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thank you!

Comment thread torchtitan/components/dataloader.py Outdated
Comment on lines +60 to +61
self.batch_size = batch_size
super().__init__(dataset, batch_size)
self.collate_fn = collate_fn

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove these two lines?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@abdulmuneer
abdulmuneer requested a review from tianyu-l March 28, 2025 12:52
@TJ-Solergibert

Copy link
Copy Markdown
Contributor

We are still missing the logger import, right?

from torchtitan.tools.logging import logger

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Can we add the logger import

from torchtitan.tools.logging import logger

according to @TJ-Solergibert ?

@abdulmuneer

Copy link
Copy Markdown
Contributor Author

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 !

@tianyu-l
tianyu-l merged commit c7a8a5a into pytorch:main Mar 28, 2025
@abdulmuneer
abdulmuneer deleted the data_collator_pass branch March 28, 2025 22:34
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
…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 
    )
    ```
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
…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 
    )
    ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants