Markdown source

Transcript: Road to 5 Million Tokens: Breaking Barriers in Long Context Training — Max Ryabinin, Together AI

Source Video

Local Cache

Transcript

Everyone, my name is Max. I am VP of research and development at Together AI. And today, I'm going to tell you about our research project, which is called Road to 5 million sequence length, breaking memory barriers in context parallelism. So, to begin, uh I'll first say a few words about Together AI and who we are.

Together AI is an AI native cloud, which provides services and infrastructure for AI developers and builders at all stages of development, starting from uh creating a model, where you just might need a GPU cluster with heavily optimized computes and uh highly reliable uh systems, uh all the way through model shaping, where you can take existing models and customize them for your tasks in terms of performance, in terms of speed, in terms of quality, through services such as the fine-tuning or uh reinforcement learning.

Also, we are an inference provider, so if you have an app which is reliant on open-source model inference, uh you can uh work with us and we'll provide you with the fastest way to launch and use AI models with more than 200 models in our portfolio, uh options for deployment, which include serverless and dedicated inference, and a ton of advanced optimizations, which I will not be able to speak about today. Um the purpose of this talk is uh focused on model training, customization, and fine-tuning in particular. And uh I'll start by asking a question.

Uh I think in the last few months or like at least a year or so, we're seeing a lot of interest in the community, uh both on the system side and on the research side, in training long context models. Um the primary reasons for that are twofold, I would say. First of all, with the uh like explosion in popularity of agents, you can see a lot of different applications where you might want to put as many tokens as you want in your context, and you want the model to leverage that context effectively.

Um second, with the development of applications such as video generation, you might often need to keep track of multiple uh like uh different frames, which or like even uh multiple frames per second, which can um occupy quite a few tokens in your context pretty quickly. And you also need models that have good uh sense of uh temporal consistency, which means that they are able to see what was happening a few seconds or ideally a few minutes ago. Um to do that all effectively, you need to make sure that the models are able to process that context and work with it correctly at the training time.

Um but even if you're not at the scales of like millions of uh tokens in the context length, it's still quite important to understand where the memory goes, because who knows, maybe you might be able to reinvest it in some other ways and speed up your training overall. So, the problem here is that if you are taking a standard transformer-based language model and trying to extend its context, you can run into two bottlenecks. Bottleneck number one is that you are faced with quadratic computation, because uh long story short, for transformer-based models, uh you have pairwise interactions across all the elements in a sequence. The second problem is more insidious, one might say.

Uh as you continue scaling your context, your memory keeps growing linearly, which is not as bad, but still pretty difficult to deal with, unless you apply a range of specific techniques. Um and this is an example from Hugging Face's blog post on model training, which shows that the sequence length growth can affect your memory limits pretty considerably. Um yeah, here's the slide. And our goal of that project was to see how far exactly we are able to get with a range of existing techniques that are pretty well known to some in the community, as well as some further optimizations that we wanted to leverage to push this a bit further ahead.

So, um let's say you're taking a model which is a standard Llama 3B architecture, you're trying to fit 3 million train tokens into your context, and you're taking uh all of this on an 8 uh x H100 GPU node. Um the first stage you'll see is that even with uh just model parameters, you're not able to fit uh it into the GPU. Uh you run out of memory just by trying to place the model. Of course, the next stage is to apply fully sharded data parallelism, where all the parameters are like basically chunked across the eight GPUs that you have, which is great, but uh still doesn't solve the problem.

You see that uh the memory usage for the model drops quite significantly, but you still are running out of memory because of all the attention activations. The next point that uh we've leveraged, and I encourage you to use as well, is uh taking advantage of uh context parallelism. In particular, there is a pretty well-known technique called DeepSpeed Ulysses, first uh introduced by Microsoft. The idea is that instead of computing all of your multi-head attention like on every GPU separately for the whole sequence, you can do something more clever.

In particular, you can try to compute the attention for different heads at different points in time, uh or like on different GPUs, uh through communicating these activations as uh they are required, in such a way that one GPU is only responsible for one attention head here, but it's still computing the attention over the whole sequence. Um that technique is quite effective at uh addressing the problem, and it also allows you to utilize the best possible attention implementation, like Flash Attention 1 2 3 4, um to optimize that part of the computation. And then you aggregate the results as uh you would have uh previously.

So, if you apply Ulysses context parallelism, the utilization drops quite significantly, like approximately 8x uh here, as uh it should, but we are still quite far from our goal of being able to fit that onto just a single uh H100 node. So, what happens next is that we can try to recompute the activations as they uh are needed to us at the backward pass. That technique is known as activation checkpointing, and uh it's available in pretty much all of the deep learning frameworks these days that you could use. You just need to enable it in a correct way that does not uh impose too much of a computational burden on you.

Um with that, with activation checkpointing, you can drop the activation usage by like a further factor of eight, but still something else needs to be done. Uh the next optimization is um also connected to the storage of activations. You can try to uh store some of the inputs to each transformer block, not on the GPU, but instead offload them to CPU when they are not required. Uh this is not very impactful for the performance, because you can offload uh it and prefetch when you are trying to backpropagate to that to the corresponding layer.

Um this optimization, to the best of our knowledge, was first implemented uh by Unsloth, and uh it allows you to drastically expand the context window. Um the next point is that you're getting uh with offloading next to 37 uh gigabytes of data, but then comes the other part of offload memory usage. Uh what happens next is that you can essentially tile all all of the computations across the sequence length in case they are element-wise. So, all the loss computations, all the MLPs, they can be chunked to avoid creating these huge buffers that would be 3 million along one of the dimensions.

Um that's Arctic sequence length training, and even with these optimizations, you are finally getting to a point where 3 million is possible. But, what if you wanted to go further? And here we actually need to do something else, which is the primary optimization we've done in our work dubbed the Untitled Ulysses. And you could describe it as a further deeper analysis and expansion of this context parallelism technique. So, what we found was that even trying to compute one set of heads at a time is already enough to saturate the computational capacity of the GPU with within one iteration.

Which means that if you have multiple different heads scheduled to be executed on one GPU, you can divide it in chunks and then essentially iterate through these chunks over time. So, we have one group of heads which are being recomputed, then you compute attention over them, you store the partial result, then you follow up with the next stage, which can reuse all the buffers you've allocated at the previous stage. Um so, the advantage here is that instead of allocating this huge buffer as you would have before like here in this slide, you allocate a buffer which is smaller, but you reuse it across like two or more different iterations.

And that allows it to further save on the activation memory for your training without any significant impact to the throughput at small scales. So, here you can see the results that we've measured across different context parallelism techniques. As you can see, both at the 8 billion scale and at the 32 billion scale, we are matching quite closely the most memory optimized implementations of transformer training while being able to scale even further like 5 million tokens and sometimes even being more performant at small at shorter context lengths. Um the relation between the chunk size or the number of heads you compute at the same time and the throughput is quite straightforward.

So, if your chunk is larger, your memory utilization is higher, but at the same time you can run the whole model a bit faster. Um so, by stacking all of these techniques together and applying U-Pipe on top, you can, for example, free up a bit of additional memory in your training if you need it and reinvest it somewhere else, for example, among the stages. Or you could say that we're interested in training across not 3 by 5 million context lengths and then you pipe is the technique that will save you by contrast to everything else.

So, as a takeaway, I think one of the things that could be quite insightful here is that training models with large context lengths is a very interesting and challenging goal, but the bottlenecks might appear where you least expect. So, tooling like the PyTorch profiler, which we elaborate on on in our paper, or other or other techniques can help you a lot. And also check out our paper for more results. All of that is public at the moment and we have an upcoming thread which will illustrate the method in more depth. Thank you very much for listening and now we are ready for questions. Thank you.

Do you guys have any questions cuz we're together and I am employees. So, who did you join it from the middle so we lack some certain context? I'm just curious about the so the QKV that was quantization parameters is that correctly Uh not exactly. It was just the query, key, and value matrices of the transformer layer. So, you multiply them here in the attention part, which creates most of the complexity because all of the queries have to like result in all of these pairwise active interactions with like keys.

Uh and the problem is that if you have a a sequence which is like 3 million in length, it means that technically like in the standard most vanilla way, you would have just like allocated that whole big tensor which has 3 million in which is 3 million in size along one of the axis. And like that's pretty significant as you could imagine, which means that you have to resort to like not just one technique, which is U-Pipe, but a range of other approaches to somehow help you like uh leverage like execute these computations without running out of your memory.

So, yeah, that's the key idea and the key challenge of working with transformers at the scale. Cool. Um in that case, thank you very much for questions and for listening.