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.

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. (Related: Industry News: Woman forced to jump in stranger’s car a...)

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: Find Orphan Pages and Buried Content: Build an Internal...)

Industry observers note that 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.

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:.

It has been revealed 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.

In a recent development, 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.

It has been revealed that 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.

Recent findings suggest 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.

In a recent development, 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:.

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.

Sources indicate 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:.

It has been revealed that 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:.

In a recent development, 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:.

Recent findings suggest that 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 new pack. The result?

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.

It has been revealed 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 new ConstantLengthDataset class handles the heavy lifting. Here’s how it works, compared to Stage 4:.

The ConstantLengthDataset does it all:.

Sources indicate 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.

In a recent development, here you will notice that the samples in knapsack are more evenly distributed. We also do not run into the glitch 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)!

Industry observers note that 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!

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.

→ Read original article