
Matthew Mayo
· 2 min read
Batching by Length Instead of Looping Item by Item for SLM Optimization
Previous articles in this series discussed constraining output space as well as reusing the prompt prefix with a key-value cache, both framed as approaches to small language model (SLM) narrow automation optimization. Let's finish this series off with the third entry, focused on batching by length instead of looping item by item.
As in our previous articles, all benchmarks below use Qwen2.5-0.5B-Instruct in float16 through Hugging Face Transformers, running on an M2 Macbook Air with 24GB RAM and a 16-core Neural Engine.
Don't forget to set up a Python environment and install your requirements:
pip install torch transformers accelerate
We will continue to use the support ticket framing from our first article.
Processing one ticket per forward pass is the single largest source of waste in the whole pipeline. At batch size 1, a small model is memory-bandwidth bound rather than compute-bound: the hardware streams every weight out of memory in order to serve one sequence, then does it again for the next one, and the arithmetic units sit mostly idle in between. This is true on a GPU and it is true on the CPU we have been using throughout this series, which is where a 0.5B model most often actually runs.
Batching amortizes that weight read across many sequences. But the obvious implementation introduces its own waste, since sequences in a batch must be padded to a common length. Real-world text has a long tail: if the longest item in your dataset is a few hundred tokens and the median is well under a hundred, padding every batch to the global maximum means most of what you compute is padding.
The answer is to sort by token length before forming batches, so each batch contains similarly sized items and pads to its own local maximum.
Looping Item by Item
Here is the per-item baseline on a realistic length distribution, with the constrained scoring from the first article in this series carried forward so that each item costs exactly one forward pass:
Output:
Batching by Length
Output:
Original source
This story was published by KDnuggets and written by Matthew Mayo. SyncAI.news shows a preview; the complete article is on the publisher's site.
Read the full story on kdnuggets.com


