Skip to content

add sequential sampling#42265

Closed
jiosephlee wants to merge 0 commit into
huggingface:mainfrom
jiosephlee:main
Closed

add sequential sampling#42265
jiosephlee wants to merge 0 commit into
huggingface:mainfrom
jiosephlee:main

Conversation

@jiosephlee

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds Sequential Sampling! All huggingface trainers default to random sampling, and there is no way to allow a sequential sampling, which is valuable when the user has a custom data curriculum, for instance.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Rocketknight1

Copy link
Copy Markdown
Member

cc @SunMarc

@SunMarc

SunMarc commented Nov 19, 2025

Copy link
Copy Markdown
Member

Hey @jiosephlee, I don't mind adding this but did you manage to do an experiment where this leads to better training overall ?

@jiosephlee

Copy link
Copy Markdown
Contributor Author

Hey @SunMarc ! Sequential sampling in and of itself wouldn't improve training, but yes, I'm doing research currently where a well-designed curriculum can lead to better performance. There are also prior works that support this e.g. https://arxiv.org/abs/2405.07490. A common way of organizing data in this field is from easy to hard: https://dl.acm.org/doi/abs/10.1145/3589335.3641257

@SunMarc

SunMarc commented Nov 20, 2025

Copy link
Copy Markdown
Member

Maybe we can first gauge the interest from the community before merging this ? TrainingArgs is already super bloated so I prefer to not add any args that have low usage. Could you create an issue so that we see if folks are interested in ?

@jiosephlee

Copy link
Copy Markdown
Contributor Author

@SunMarc, that totally makes sense. I just created an issue, and I can re-bump this PR once it's garnered some interest.

I know folks like @qgallouedec have wanted this as well, if this helps clarify the need for this feature (correct me if I'm wrong, Quentin!)

@qgallouedec

Copy link
Copy Markdown
Member

Thznks!

Yes, to give more context:

Currently, if I want to sample my data in order, I can't. The PR in the current state is a solution, but in my opinion, sequential sampling should be the default.

If we agree on this, I see two options

  1. Add a parameter such as random_sampling (False by default) that would enable random sampling. But that would add another argument to TrainingArguments.

  2. (Better, in my opinion) Simply replace the random sampler with a sequential sampler, and if the user wants random sampling, they can simply shuffle their dataset beforehand, which amounts to the same thing. No new arg.

In both cases it would change the default behavior of the trainer

@jiosephlee

jiosephlee commented Dec 24, 2025

Copy link
Copy Markdown
Contributor Author

@qgallouedec I will +1 on the default behavior change. The default behavior is likely unexpected for many, especially for those coming from PyTorch, which also has the shuffle off as default https://docs.pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader

Concerning (2), there is an issue that would arise from forgoing RandomSampler. RandomSampler has a nice property where you can reset the shuffle at each epoch, and it appears to be used in Trainer.

epoch_dataloader.set_epoch(epoch)

Otherwise, it seems difficult to implement epoch-level shuffling outside of Trainer.

@SunMarc

SunMarc commented Jan 5, 2026

Copy link
Copy Markdown
Member

Currently, if I want to sample my data in order, I can't. The PR in the current state is a solution, but in my opinion, sequential sampling should be the default.

Can you explain why ? For the training dataset, it should be better to have have random sampling in general no ?

Maybe as a first set, we can create a new arg called train_sampler and we remove that the same time group_by_length arg. In this new arg, we will have three choices random (default), group_by_length and sequential wdyt ? Since v5 is not out yet, if we are quick enough, we won't have to do dep cycle regarding group_by_length arg. cc @jiosephlee

Happy new year btw ;)

@jiosephlee

Copy link
Copy Markdown
Contributor Author

@SunMarc happy new year :)

And that's a pretty clean suggestion! I took a stab at refactoring it based on what you said. I took the liberty of changing one of the ValueErrors to a warning because the IterableDataset incompatibility seems to be an issue for any Sampler, afaik, which is why samplers are entirely skipped in line 1037.

@qgallouedec

Copy link
Copy Markdown
Member

Yes, that sounds good to me too. With one small reservation about the name. Because this argument would not take a sampler, but rather a strategy. So maybe sampling_strategy?

@qgallouedec

Copy link
Copy Markdown
Member

Can you explain why ? For the training dataset, it should be better to have random sampling in general no ?

Yes indeed. The motivation was mainly to avoid the surprise caused by the fact that the PyTorch loader is sequential by default, but shuffling by default is also a good default.

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks ! Just a few nits

Comment thread src/transformers/trainer.py Outdated
Comment on lines +674 to +677
logger.warning(
f"The `train_sampler='{args.train_sampler}'` option is ignored when using an `IterableDataset`. "
"Samplers cannot be used with IterableDataset as they require indexed access to the dataset."
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is indeed true. sampler field will be ignored when using IterableDataset. Since this behavior is silently ignored by torch, maybe we can just throw logger.info instead of a warning.

Comment thread src/transformers/training_args.py Outdated
Comment thread src/transformers/training_args.py Outdated
Comment on lines +579 to +580
train_sampler (`str`, *optional*, defaults to `"random"`):
The sampler to use for the training dataloader. Possible values are:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe specify here instead that it only works when the dataset is not an interabledataset

Comment thread src/transformers/training_args.py Outdated
group_by_length (`bool`, *optional*, defaults to `False`):
Whether or not to group together samples of roughly the same length in the training dataset (to minimize
padding applied and be more efficient). Only useful if applying dynamic padding.
train_sampler (`str`, *optional*, defaults to `"random"`):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

as quentin suggested, maybe we should switch to train_sampling_strategy

@jiosephlee

Copy link
Copy Markdown
Contributor Author

Moved this to new PR #43138

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.

4 participants