# Data sampler based on number of tokens

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdata-sampler-based-on-number-of-tokens%2F14426%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 4, 2022, 10:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdata-sampler-based-on-number-of-tokens%2F14426 "2022-02-04T22:06:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bivouac0](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@bivouac0](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbivouac0)%5C
**Post date:** [February 4, 2022, 10:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdata-sampler-based-on-number-of-tokens%2F14426%2F1 "2022-02-04T22:06:28Z")

</div>

I’m training the large bart model and I have to set my batch size very low in order to keep from getting OOM errors (with a 24GB 3090). The issue is that my sequence lengths are highly variable. The long ones which are close to the 1024 token limit need to be no more than 2 sequences per batch but many of the others could be batched 8 or more together. Does transformers have a way to batch based on the number of tokens instead of a fixed batch size? (BTW… I’m already using DataCollatorForSeq2Seq and group\_by\_length=True for dynamic padding of batches).
