AI Engineer Europe 2026
Road to 5 Million Tokens: Breaking Barriers in Long Context Training — Max Ryabinin, Together AI
About this talk
Together AI researcher Max Ryabinin explains how GPU memory limits constrain multi-million-token transformer training and builds a solution from fully sharded data parallelism, DeepSpeed-Ulysses context parallelism, CPU activation offloading, and chunked computations. He then introduces UPipe, the Untied Ulysses technique that processes attention heads in smaller groups to reduce activation memory and enable five-million-token contexts on an eight-H100 node, before discussing profiling and taking audience questions.
Chapters
- 0:00Introduction and Together AI's model-training infrastructure
- 2:35Long-context applications and GPU memory constraints
- 6:08DeepSpeed-Ulysses context parallelism and FlashAttention
- 8:38CPU offloading and chunked three-million-token training
- 10:26UPipe headwise chunking, five-million-token contexts, and profiling
- 13:52Audience questions and closing
Talk transcript
- 0:00
[upbeat music] Hi, everyone.
- 0:15
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 Five Million Sequence Length: Breaking Memory Barriers in Context Parallelism.
- 0:30
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, uh, 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,
- 0:55
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, uh, reinforcement learning.
- 1:15
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
- 1:40
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.
- 1:56
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.
- 2:13
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.
- 2:35
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.
- 2:57
And you also need models that have good, uh, sense of, uh, temporal consistency, which means that they're able to see what was happening a few seconds or ideally a few minutes ago.
- 3:10
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
- 3:35
who knows, maybe you might be able to reinvest it in some other ways and speed up your training overall.
- 3:42
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 [coughs] into two bottlenecks.
- 3:57
Bottleneck number one is that you are faced with quadratic computation because, well, long story short, for transformer-based models, uh, you have pairwise interactions a-across all the elements in a sequence.
- 4:11
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.
- 4:30
Um, and this is an example from Hugging Face's blog post on model training, which shows that the sequence length, uh, growth can affect your memory limits pretty considerably.
- 4:46
Um, yeah, here's the slide. And our goal of that project was to see, uh, 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, uh, further ahead.
- 5:11
So, um, let's say you're taking a model which is a standard Llama 3B architecture. You're trying to fit three million training tokens into your context, and you're taking, uh, all of this on an 8, uh, X, uh, H100 GPU node.
- 5:30
Um, the first stage you'll see is that even with, uh, just the 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.
- 5:43
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.
- 5:57
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.
- 6:08
The next point that, uh, we 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.
- 6:26
The idea is that instead of computing, uh, 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
- 6:50
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.
- 7:04
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 one, two, three, four, um, to optimize that part of the computation, and then you aggregate the results as, uh, you would've, uh, previously.
- 7:27
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.
- 7:48
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 different frameworks these days that you could use.
- 8:08
You just need to enable it in a correct way that does not, uh, impose too much of a computational burden on you.
- 8:18
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.
- 8:38
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 the...
- 8:58
it, uh, and prefetch when you are trying to back propagate 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.
- 9:16
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 out-of-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.
- 9:39
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 our tech sequence length training, and even with these optimizations, you're finally getting to a point where 3 million is possible.
- 10:00
But what if you wanted to go further? And here we actually need to do something else, which is the primary, uh, optimization we've done in our work, dubbed the Untitled Ulysses, and you could describe it as a further deeper, uh, analysis and expansion of, uh, this context parallelism technique.
- 10:26
So what we found was that even trying to compute one set of heads at a time, uh, 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, uh, one GPU, you can divide it in chunks and then, uh,
- 10:51
essentially iterate through these chunks over time. So you 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.
- 11:11
Um, so the advantage here is that instead of allocating this huge buffer as, uh, you would've before, like here in this slide, you allocate a buffer which is smaller, but you reuse it across, like, two or more different iterations.
- 11:28
And that allows you to further save on the activation memory for your training without any significant impact to the throughput at smaller scales. So here you can see the results that we've, uh, measured across different context parallelism techniques.
- 11:46
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, uh, sometimes even being more performant at small, uh, at shorter context lengths.
- 12:10
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.
- 12:29
Um, so by stacking all of these, uh, techniques together and applying UPipe on top, you can, for example, uh, free up a bit of additional memory in, uh, your training if you need it and reinvest it somewhere else, uh, for example, among the stages.
- 12:46
Or, uh, you could say that we're interested in training across not 3 by 5 million context lengths, and then, uh, UPipe is the technique that will save you, uh, by contrast to everything else.
- 13:00
So, um, as a takeaway, uh, I think one of the things that, uh, could be quite insightful here is that training models with large context lengths is a very interesting and challenging goal.
- 13:16
Uh, but the bottlenecks might appear where you least expect. So tooling like the PyTorch Profiler, which, uh, we, uh, elaborate on a ton in our paper, uh, or other te- or other techniques can help you a lot.
- 13:31
Uh, 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.
- 13:43
Thank you very much for listening, and now we are ready for questions.
- 13:52
Thank you. Do you guys have any questions 'cause we're Together AI employees? [laughs]
- 14:01
Sorry, we, we joined in from the middle, so we, we lack some certain context.
- 14:06
Yeah. Got it.
- 14:07
But I'm just curious about the, the... So the QKV-
- 14:11
Yeah
- 14:11
... that was, uh, quantization, uh, parameters. Is that correctly understood?
- 14:15
Uh, not exactly. It was just the query, key, and value matrices-
- 14:19
Okay
- 14:20
... of the transformer layer. So, uh, you multiply them here in the attention part, which, uh, creates most of the complexity because all of the queries have to, like, result in all of these pairwise activ- uh, interactions with, uh, uh, like, keys.
- 14:37
Uh, and the problem is that if you have a se- [coughs]
- 14:42
a sequence which is, like, 3 million in length, it means that technically, like, in the standard most, uh, vanilla way, you would've just, like, allocated that whole big tensor which has 3 million in...
- 14:55
Which is 3 million in size, uh, along one of the axes. 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 UPipe, but a range of other approaches to somehow help you, like, uh, lever- like, execute these computations without running out of your memory.
- 15:19
So yeah, that's the key idea and the key challenge of, uh, working with transformers at, uh, this scale.
- 15:30
Cool. Um, in that case, thank you very much for questions and for listening. [outro music]