According to recent reports, you've got everything ready - data, model, a beefy GPU setup. You hit "run" and... wait. And wait some more. Your GPUs are barely breaking a sweat while your wallet's getting lighter by the hour.
It has been revealed that sound familiar? We've been there. After some detective work on our nanoVLM project, we discovered the real culprit wasn't our model or hardware, it was our data pipeline being incredibly wasteful.
It has been revealed that in this post we build an efficient pipeline in five stages. In each stage we add or remove from the previous step and comment on what went right and what did not. (Related: How to Build a Healthcare Robot from Simulation to Depl...)
According to recent reports, to make it easier to follow the data preparation tasks, we created a separate repo laser-focused on the data pipeline only. We hope this will be much easier to understand that reading the code once integrated with the nanoVLM repository. In addition, this could be useful to bootstrap other data pipelines!
Repository: https://github.com/ariG23498/mmdp.
To follow along, all you need to do is clone the repository. It contains the final data preparation tasks, but it's designed to showcase each step of the way.
Before optimizing anything, we need to understand what we are working with. Our multimodal dataset has images, text prompts, and responses. (Related: Open Responses: What you need to know...)
According to recent reports, getting familiar with your training data is crucial for success. The previous script shows a random sample each time you run it; you may want to copy the snippet to a notebook and run it multiple times to get a feeling about the data.
Our first training attempt used the obvious (and very frequent) approach:.
Recent findings suggest that the results were painful. Look at this visualization:.
See all that gray? That's padding. That's the GPU processing absolutely nothing while you pay for compute time. We were wasting roughly 60% of our batch on empty tokens.
Our next move was simple. Set a global maximum length and stick to it. If a sample was too long, we'd just drop it.
According to recent reports, as you might have noticed that the batch now has one sample less. This is due to the filtering process. This helped, but we were still padding everything to the same fixed length regardless of actual content. Better than before, but still wasteful.
It has been revealed that now we’re ready to rethink batching entirely. Padding is the enemy, and we need a strategy to minimize it while maximizing how much data we can fit into each batch. Enter the knapsack problem, a classic from computer science that’s perfect for this.
Industry observers note that imagine you’re packing a backpack for a hike. It can only hold so much weight, and you want to cram in as many useful items as possible. In our case:.
Sources indicate that to test this idea, we start with a toy dataset: just a list of numbers from 1 to 25, each representing a sequence length. This lets us experiment without the complexity of images and text.
Recent findings suggest that most PyTorch datasets are map-style (you access them with dataset[i]). But for dynamic batching, we need something more flexible. So, we built an iterable-style dataset by subclassing torch.utils.data.IterableDataset. This lets us generate batches on the fly and handle tricks like sharding data across multiple workers:.
Packing sequences can be slow, especially if we’re sorting or shuffling. To keep things moving, we use a producer-consumer pattern using Python queues:.
The producer thread packs batches and puts them in a queue, while the main thread pulls them out as needed. This overlap keeps the pipeline flowing smoothly.
First, we try a simple greedy packing strategy:.
In a recent development, this walks through the data sequentially, adding items to a pack until it’s full, then starting a newly introduced one. It’s fast but not perfect. Here’s what the batches look like:.
It has been revealed that notice how later batches get sparse? We’re leaving gaps.
Let’s try a smarter approach: bin-packing (specifically, First Fit Decreasing):.
Industry observers note that this sorts sequences by length (longest first) and tries to fit each one into the first pack that has room. If none fits, it starts a upcoming pack. The result?
It has been revealed that these batches are much tighter, with less wasted space. It’s like playing Tetris with your data, fitting pieces together snugly.
Now for the real deal, applying knapsack packing to our multimodal dataset.
Industry observers note that we’re back to images, prompts, and responses, and we need to pack them efficiently while respecting both token limits and image budgets. Image budgeting is done so that images per sample are balanced. We would like to avoid the case where one GPU needs to process way more images than another.
Our latest ConstantLengthDataset class handles the heavy lifting. Here’s how it works, compared to Stage 4:.
The ConstantLengthDataset does it all:.
It has been revealed that look at that! The gray (padding) is minimal, and the batches are dense with useful data. It’s like packing a suitcase so well you can still zip it up without sitting on it.
The image might seem unintuive at the first glance, but let us look at the image side by side with constrained padding.
According to recent reports, here you will notice that the samples in knapsack are more evenly distributed. We also do not run into the concern of having less samples in the batch due to filtering.
What started as a simple "why is training so slow?" investigation led to a complete rethink of how we handle multimodal data.
The balanced knapsack strategy for data pipeline comes from the Eagle 2: Building Post-Training Data Strategies from Scratch for Frontier Vision-Language Models paper from NVIDIA.
Happy training (and may your GPUs stay busy)!
Thank you for the great work! Can I suggest a few things? 🤗.
But again, really nice work on this blog post and on Picotron guys!
In a recent development, these are absolutely great pointer. Would you like to add your suggestions to the blog post directly? That way we could have you as one of the authors of the blog post (with due credits.).
You can edit this page: https://github.com/huggingface/blog/blob/main/mmdp.md.