Hello everyone. Thank you for joining me here. Yeah, today I'm going to talk about Ray Actors, Vision Tokens, and the GIL. Engineering a multimodal SFT data pipeline that keeps the GPUs busy.
So there's a lot of talk about how to optimize training paradigms, but I want to give a different perspective on the whole ecosystem of what it takes to get a model trained. And so the question I'm going to be asking here is: does your training loop really depend on the GPUs right now? Is it blocked on the GPUs? And I think we're going to learn that in this experiment, the hardest part of optimizing multimodal training throughput is not necessarily kernel optimization. It is making sure that the GPUs are constantly fed with data.
So let's go over the overview of what this topic will be about. We will establish a baseline multimodal training pipeline. And then we will encounter bottlenecks and address them one at a time. Loosely they are: one, concurrency. How fast can you process one image before feeding it to the model? Two, prefetching. How can we make sure that we have data on demand? Three, how can we minimize data copies across different processes? And four, additional framework optimizations. Just tweaking your settings.
So let's set up our premise. We all know that GPUs incur expensive costs and we generally want to maximize their utilization while we have them at hand. Multimodal processing is different than text only because there's a lot of CPU intensive operations that have to happen before you actually feed the model the data. So whether that is loading, resizing, or normalizing images using a library like PIL. For audio, you do a lot of loading and cropping using NumPy arrays. And then videos are some sort of combination of both. And it's also true that usually your training clusters have a lot more CPUs than GPUs at hand. And yet the GPU is what is starving in this pipeline.
So today we're going to introduce this equation called wait time ratio, which I formulate as the time it takes to prep your data and transfer it to the GPUs, divided by the time it takes to do both of those and the train as well. So the denominator is your total pipeline. We're going to see that during our run we will go from a utilization of 15% to utilization of 90%. So our baseline spends 85% of its time on data acquisition, whereas after we do these optimizations less than 10%. And so for the scope of this talk, GPUs are not the goal. Making sure that data is never the bottleneck is the goal.
And so here's a little bit of background as to what our stack will look like. We kind of take a QWEN 3 VL base model and try to train it on the paradigms of Chat VQA and multi-turn VLM conversations. So to recap, before the GPUs do any math, we have to load the data, we have to transform the data, and then we have to transport it to the GPUs. Okay, this is an overview of what our data actually looks like. So we will be using JSON-L files, and then images are linked relatively to where the example lives. And then we're going to assume that our data is just stored in S3. In this example, it seems like image 33471.jpg seems to be a picture of a blue bus.
Yeah, so this is our system layout. On the left, we have a data generator, which is responsible for the data loading and data transporting. And that's labeled as fan out to the Megatron data parallel ranks. So the parallel ranks receive partitions of the data as decided by the coordinator. It collates it, performs forward pass and backward pass on this data. So Megatron takes care of optimizing the model weights across the DP ranks, and we're going to assume that it's working as a black box.
So let's start with the naive baseline path. For each example, we fetch the images from object store. We tokenize it, we resize the images, normalize them, and then feed them to the model. Yeah, and then when we do that, we can see that there's serial operations in the red, which is namely getting all the images one by one and then processing them one by one. So if your trainer is asking for a batch size of 256, then you have to serially process all these images.
And then we can see that our train time is somewhere between 15 to 20 seconds. However, the time spent to actually fetch the data is more than 100 seconds. So that's why we result in only 15% utilization of our GPUs. And then you can even see a profiling flame graph that we spend most of the time doing S3 loads. And then you can see a little tail at the end about loading the actual image on the PIL library. So okay, solution one. Let's try to add some concurrency to this. Instead of those red singular serial processes, let's try to parallelize it by having a worker pool. So when you request 256 examples, maybe let's just process all of those at once.
Yeah. So for example, first sample, we have two costs. One is fetching the images. Two is processing. So that's decoding, vision pre-processing, and then tokenizing your text. Samples are very independent. So this just means it's embarrassingly parallelizable. And so yeah, you have design decisions here. What forms of concurrency do you want?
So there's asynchronous programming, which is suitable for IO work, such as making SQL queries or performing network requests. There's threads, which you can use for CPU intensive workloads that relinquish the Python GIL, the global interpreter lock. So it's not straightforward what operations actually release the lock. So you have to do a little more investigation. But common uses are like using Hugging Face Transformers tokenizers, using the PIL Transforms API, or some Pandas operations. Processes are more heavyweight, but also provide total isolation between your workloads. So you can leverage multiple cores on your machine as you finish your work.
Yeah, doing a deep dive on the right solution would warrant its own talk, but there's plenty of online resources that can help you decide for your use case. What's important for our solution is that we need to match our bottleneck, not simply going with a lever that's worked for us before. So we'll choose async IO for fetching images here, and then use Ray actors as our processors, as our multi-processing library. Ray actors are essentially the same as general processes, but they offer us APIs to communicate across workers that make it so we don't have to reinvent the wheel.
So yeah, your process is only as slow as your slowest link. And we can see that our wait time ratio has gone down on the second example, which is in the pink. You can see that the time spent fetching data per batch has gone around 40 seconds. So this gives us a 25% speed up. However, as you can see, there's a huge gap that starts generating, especially as we're going through many steps. So can we somehow limit our bottleneck of waiting on the trainer to request before provisioning our worker pool to fetch the data? Can we just preemptively fetch our data?
So we came up with solution two, which was that we have a background thread that fills up a queue ahead of time, and it always stays ahead of the consumer. This means that your producer is producing examples at a multiplier of your consumer, and so the trainer is never bottlenecked on actually requesting the data. When it requests data, it is available on demand.
There's a caveat to prefetching. It makes your model and data loader checkpointing and resumption a little intricate process, and it can also suffer from a cold start period in which your queue is actually getting filled up so it can be ready for training. But if you work around those details, I think prefetching is a great idea. Yeah, so I think making sure your producer is always ahead of your consumer will pay great dividends for you here.
Unsurprisingly, we achieved close to zero seconds of data fetching and processing per batch because we've already fetched data and processed it way before the trainer even needs it. And so we've effectively solved both the data load and the transform bottlenecks. So after these two mitigations, we thought we were done. Throughput up, GPUs fed.
However, we observed that the data transport costs still linger. In fact, they've gone up. So every sample transfers a large amount of image arrays across process boundaries multiple times. Every sample is pickled on the worker, copied to the coordinator, and then re-pickled onto the trainer. Per sample, per step, we have a memory copy that nobody really asks for. So how this manifests is that your data generator can often OOM. And multimodal tensors are huge. They're megabytes per sample. And so by default, every sample is returned by value, which means that these expensive arrays are copied over from process to process.
So a fix for this, you can come up with an object store. This is a nice convenience method that Ray provides you. Each sample carries an object reference, which is just a pointer to the data. And then the driver just holds the pointer. The tensor never round trips to the head node. And the Megatron data parallel ranks, they can just directly call the Ray store to fetch the data as they need. So with every choice of your parallelism and concurrency solution, you always have to think about the added communication costs. So plan wisely around that. Okay. Yeah.
And we were able to eliminate an extraneous put object call that our prefetcher solution had that we did not have to deal with by using the Ray store. And we can see that our communication costs go down and the ratio of the time spent waiting also goes down. So to recap, we took a serial baseline. We added concurrency for data loading and processing. We added prefetching to stay ahead of training requests. And we added the Ray store to minimize data transfer. We went from a baseline wait ratio of 85% to 20%. And you can see that the sawtooth pattern that you see in the top graph is alleviated to a large extent, which means our utilization of the GPUs has gone up.
So when we're scaling up, now that we have a good multimodal training pipeline, let's try to scale it up to a production run. While doing that, we observed our wait time ratio for the same pipeline went up when all we did was scale some numbers. So we just added four times the data parallel ranks. We added 200 data streams. And then batch size is X by four.
So it turns out that since the data generator and the worker pool are co-located for fast data transfer, what happens is that there's a bottleneck on the network interface card of that node, which means that while we're packing all of this working and processing into specific nodes, their network throughput is not able to keep up with the demand of the training pipeline.
So at this point, we did a little bit of digging into the frameworks that we are using. And we saw that by default, Ray schedules workers on this. It prefers to schedule workers on the same nodes and co-locate workers, which means that it's easy to bottleneck one machine before you move to another machine. So what we did was consider a possibility where we disaggregate this whole process, where workers are spread across multiple nodes, so they can do their own image processing and storing, and in a lazy, just-in-time manner are retrieved and used by our DP rings.
So yeah, and then, so these are the two parameters that we kind of tuned. One is the zero-copy retrieval, and second is a spread scheduling mechanism. Neither of these actually helped in a small-scale experiment, but together they had a synergistic effect in a large-scale experiment. Yeah, so a config can do poorly at a small scale, but can do really well at a larger scale. So you should always think about how to ablate across your scaling ladders. And these were the two flags that ended up making a huge difference in how fast our data processing pipeline was.
And so now, this is the final pipeline that we've accomplished: a concurrent, eager, and a data-access pattern-aware system that shares the training across loads and enables the GPU to stay warm while providing smoothness and stability in training. Overall, we achieved a 50% additional throughput at scale just from these two flags. It's the same knob, opposite verdict, set by the bottleneck. So these are the learnings we get from this talk, if nothing else. GPUs are expensive, but sometimes the biggest battles happen outside of the GPU. So first ask yourself: are you CPU-bound or GPU-bound? Are you IO-bound or compute-bound?
And then solving one bottleneck often exposes another. So first it was throughput for us. Then it became wait time. Then it became memory. Then it became retrieval costs. Classic moves are not the end. You know, parallelism and prefetching matter, and I think a lot of content has covered this, but so do the semantics of the frameworks that you're using and the constraints that you're dealing with. And scale always changes the answer. Tuning parameters like zero copy waste and spreading workers become important only at larger scales, and they may not manifest as obvious improvements at smaller levels. Overall, profiling your baseline helps you move the needle.
Yeah, thank you, everyone, for attending the talk. If you have any questions, I'm happy to answer them.
Thanks. So, for example, first sample, we have two costs. One is fetching the images. Two is processing. So, that's like decoding, vision pre-processing, and then tokenizing your text. Samples are very independent. So, this just means it's like embarrassingly paralyzable. And so, yeah, you have a design decisions here. What forms of concurrency do you want? So, there's asynchronous programming, which is suitable for like IO work, such as making SQL queries or like performing network requests. There's threads, which you can use for like CPU intensive workloads that relinquish the Python guild, like the global interpreter lock.
So, it's not straightforward what operations actually release the lock. So, you have to do a little more of your investigation. But common uses are like using Hugging Face Transformers tokenizers, using the Pill Transforms API, or like some Pandas operations. Processes are more heavyweight, but also provide total isolation between your workloads. So, you can leverage multiple cores on your machine as you finish your work. Yeah, doing a deep dive on the right solution would warrant its own talk, but like there's plenty of online resources that can help you decide for your use case.
What's important for our solution is that we need to match our bottleneck, not simply going with a lever that's worked for us before. So, we'll choose async IO for fetching images here, and then use ray actors as our processors, as our process, multi-processing library. Reactors are essentially the same as like general processes, but they offer us APIs to communicate across workers that just make it so that we don't have to reinvent the wheel. So, yeah, your process is only as slow as your slowest link. And we can see that our wait time ratio has gone down on the second example, which is in the pink.
You can see that the time spent fetching data per batch has gone basically, yeah, around like 40 seconds. So, this gives us a 25% speed up. However, yeah, as you can see, there's a huge gap that starts generating, especially as we're going through many steps.
So, can we somehow limit our bottleneck of waiting on the trainer to request before provisioning our worker pool to fetch the data? So, can we just like preemptively fetch our data? So, we came up with solution two, which was that we have a background thread that fills up a queue ahead of time, and it always stays ahead of the consumer, which means that your producer is producing examples at a multiplier of your consumer, and so the trainer is never bottlenecked on actually requesting the data. When it requests data, it is available on demand. There's a caveat to prefetching.
It makes your model and data loader checkpointing and resumption like a little intricate of a process, and it can also suffer from a cold start period in which your queue is actually getting filled up so it can be ready for training. But if you work around those details, I think prefetching is a great idea.
Yeah, so I think making sure your producer is always ahead of your consumer will pay great dividends for you here. Unsurprisingly, we achieved close to zero seconds of data fetching and processing per batch because we've already fetched data and processed it way before the trainer even needs it. And so, we've effectively solved both the data load and the transform bottlenecks. So, after these two mitigations, we thought we were done. Throughput, up, GPUs, fed. However, we observed that the data transport costs still linger. In fact, they've gone up. So, every sample transfers a large amount of, like, image arrays across process boundaries multiple times.
Every sample is pickled on the worker, copied to the coordinator, and then re-pickled onto the trainer. Per sample, per step, we have a mem copy that nobody really asks for. So, how this manifests is that your data generator can often oom. And so, multimodal tensors are huge. They're megabytes per sample. And so, by default, every sample is returned by value, which means that these expensive arrays are copied over from process to process. So, a fix for this, you can come up with an object store. This is a nice convenience method that Ray provides you. Each sample carries an object reference, which is just a pointer to the data. And then the driver just holds the pointer.
The tensor never round trips to the head node. And the Megatron data parallel ranks, they can just directly call the race store to fetch the data as they need. So, with every choice of your parallelism and concurrency solution, you always have to think about the added communication costs. So, plan wisely around that. Okay. Okay. Yeah. And we were able to eliminate an extraneous put object kind of call that our prefetcher solution had that we did not have to deal with by using the Ray store. And we can see that our communication costs go down and the ratio of the time spent waiting also goes down. So, to recap, we took a serial baseline.
We added concurrency for data loading and processing. We added prefetching to stay ahead of training requests. And we added the Ray store to minimize data transfer. We went from a baseline weight ratio of 85% to 20%. And you can see that the sawtooth pattern that you see in the top graph is alleviated to a large extent, which means our utilization of the GPUs has gone up. So, when we're scaling up, now that we have a good multimodal training pipeline, let's try to scale it up to a production run. While doing that, we observed our wait time ratio for the same pipeline went up when all we did was scale some numbers. So, we just added four times the data parallel rings.
We added like 200 data streams. And then batch size is X by four. So, it turns out that since the data generator and the worker pool are co-located for fast data transfer, what happens is that there's a bottleneck on the network interface card of that node, which means that while we're packing all of this working and processing into specific nodes, that their network throughput is not able to keep up with the demand of the training pipeline.
So, at this point, we did a little bit of digging into the frameworks that we are using. And we saw that by default, Ray schedules workers on this. It prefers to schedule workers on the same nodes and co-locate workers, which means that it's easy to bottleneck one machine before you move to another machine. So, what we did was consider a possibility where we disaggregate this whole process, where workers are spread across multiple nodes, so they can do their own image processing and storing, and in a lazy, just-in-time manner are retrieved and used by our DP rings. So, yeah, and then, so these are the two parameter, two flags that we kind of tuned.
One is the zero-copy retrieval, and second is a spread scheduling mechanism. Neither of these actually helped in a small-scale experiment, but together they had a synergistic effect in a large-scale experiment. Yeah, so a config can lose, a config can do poorly at a small scale, but can do really good at a larger scale. So, you should always think about how to ablate across your scaling ladders. And these were the two flags that ended up making a huge difference in how fast our data processing pipeline was. And so now, this is the final pipeline that we've accomplished, a concurrent, eager, and a data-access pattern-aware system that shares the training across loads
and enables that GPU stay warm while providing smoothness and stability in training. Overall, we achieved a 50% additional throughput at scale just from these two flags. It's the same knob, opposite verdict, set by the bottleneck. So, these are the learnings we get from this talk, if nothing else. GPUs are expensive, but sometimes the biggest battles happen outside of the GPU. So, first ask yourself, are you CPU-bound or GPU-bound? Are you IO-bound or compute-bound? And then solving one bottleneck often exposes another. So, first it was throughput for us. Then it became wait time. Then it became memory. Then it became retrieval costs. Classic moves are not the end.
You know, parallelism and prefetching matter, and I think a lot of content has covered this, but so do the semantics of the frameworks that you're using and the constraints that you're dealing with. And scale always changes the answer. Tuning parameters like zero copy waste and spreading workers become important only at larger scales, and they may not manifest as obvious improvements at smaller levels. Overall, profiling your baseline helps you move the needle. Yeah, thank you, everyone, for attending the talk. If you have any questions, I'm happy to answer them. Thanks.
Bye. Bye.
Bye.
Bye. Bye. Bye. Bye. Bye. ! Thank you.