The BucketIterator
creates batches with sequences of the same or similar lengths. Or you can create your own Sampler to create batches with sequences of the same length; see this post as an example.
The comments from @googlebot are of course valid, but it’s generally a good idea to avoid batches where the lengths of the sequences vary a great deal.