Fast Video Generation with Sliding Tile Attention
Diffusion Transformers (DiTs) with 3D full attention power state-of-the-art video generation, but suffer from prohibitive compute cost -- when generating just a 5-second 720P video, attention alone takes 800 out of 945 seconds of total inference time. This paper introduces sliding tile attention (STA) to address this challenge. STA leverages the observation that attention scores in pretrained video diffusion models predominantly concentrate within localized 3D windows. By sliding and attending over the local spatial-temporal region, STA eliminates redundancy from full attention. Unlike traditional token-wise sliding window attention (SWA), STA operates tile-by-tile with a novel hardware-aware sliding window design, preserving expressiveness while being hardware-efficient. With careful kernel-level optimizations, STA offers the first efficient 2D/3D sliding-window-like attention implementation, achieving 58.79% MFU. Precisely, STA accelerates attention by 2.8-17x over FlashAttention-2 (FA2) and 1.6-10x over FlashAttention-3 (FA3). On the leading video DiT, HunyuanVideo, STA reduces end-to-end latency from 945s (FA3) to 685s without quality degradation, requiring no training. Enabling finetuning further lowers latency to 268s with only a 0.09% drop on VBench.
Discussion
Host: Hello everyone, and welcome back to the podcast! I'm your host, Leo, and I'm super excited about today's episode. We're diving into the world of video generation, specifically how to make it faster and more efficient. It's a hot topic, especially with AI models like Sora making waves, but also demanding huge computational resources. We have to talk about this topic today.
Guest: Hey Leo, thanks for having me! Yeah, video generation is exploding, but the reality is, for many researchers and smaller companies, the sheer cost of running these models is a massive barrier. So, anything that can make it faster and cheaper is a huge deal.
Host: Exactly! It's not just about bragging rights for the biggest and best model; it's about democratizing access to this technology. So, today, we're going to unpack a really interesting research paper titled 'Fast Video Generation with Sliding Tile Attention.' It tackles this very problem of computational cost. Now, the title itself is a mouthful, 'Sliding Tile Attention,' but don't let that scare you off. We'll break it down step by step.
Guest: And it's crucial to remember that behind these technical terms are very practical ideas and insights. The core idea behind the paper really can make video generation accessible to more people.
Host: Alright, so to start, let's talk about the problem this paper is trying to solve. As the paper highlights, Diffusion Transformers, or DiTs, have become the go-to architecture for high-resolution video generation. But they come with a major drawback: they're incredibly computationally expensive.
Guest: That's the 3D attention mechanism, right? It allows the model to understand the relationships between different parts of the video, both spatially and temporally, across all those frames. So you might have a sequence of frames, and each frame has its own sequence of visual tokens. The attention is then performed on the entire sequence of tokens. All of this takes a lot of processing power!
Host: Exactly! It's the heart of what makes these models so powerful, but also what makes them so slow. The paper mentions that when generating just a 5-second 720p video, attention alone can take up a huge chunk of the total inference time. They even give an example of using HunyuanVideo, a leading video DiT. Even with a high-end H100 GPU and FlashAttention 3, it still takes 16 minutes to generate a 5-second clip. That's wild!
Guest: And that's just inference time, right? We're not even talking about the training time which would be much, much longer. This just underscores the need for efficiency. If it takes that long to generate a short clip, imagine trying to create anything longer or at a higher resolution. The costs would be astronomical!
Host: So, how do they tackle this problem? Their key insight is that video data is inherently redundant. Adjacent frames don't change much, and pixels close to each other in a frame are highly correlated. They hypothesize that treating every token independently in 3D attention is unnecessary, and that this redundancy can be exploited to speed things up.
Guest: It's a really intuitive point. Think about it, if you're watching a video of someone walking, the background probably isn't changing much from frame to frame. Do you really need to re-calculate the attention scores for every single pixel in the background for every single frame? Probably not.
Host: Yeah, it's like, why are we paying attention to everything equally when some things are way more important than others? To prove this, the authors visualized the attention scores of HunyuanVideo. What they found was a very clear 3D locality pattern: queries tend to assign higher attention scores to keys that are spatially and temporally nearby. I think that's a very crucial insight.
Guest: That's the core of the argument, I think! Instead of each token attending to every other token in the entire video, the attention is concentrated in a small, localized window. The paper uses a metric called 'attention recall' to quantify this. They found that a local window covering only a small percentage of the total token space accounts for a large percentage of the total attention score. That's huge!
Host: This is so exciting! And the conclusion they draw from this observation is that sliding window attention, or SWA, could be a good alternative to full 3D attention. It can reduce computational cost by only attending to keys within a fixed window while still retaining expressiveness.
Guest: Which makes perfect sense! Why calculate attention scores for the whole sequence when the majority of the attention is focused on nearby regions anyway? But, and there's always a but, simply using existing SWA implementations wasn't enough. They found that existing implementations fail to translate FLOP reductions into proportional wall-clock speedups.
Host: Right. Just cutting the number of operations doesn't automatically translate to real-world faster performance. So, what's the bottleneck?
Guest: Well, the problem lies in the way higher-order, like 2D or 3D, sliding window attention creates a highly irregular attention mask. This irregularity leads to wasted computations and significant masking overhead, making the computation GPU-unfriendly and resulting in poor hardware utilization. It's like trying to fit a square peg into a round hole, the hardware just isn't optimized for that type of computation.
Host: Okay, so the existing sliding window approaches create a mess of computations that the GPU can't handle efficiently. That makes sense. So, this is where 'Sliding Tile Attention' comes in! They've developed a hardware-aware attention mechanism that rethinks sliding window computation via system-algorithm co-design.
Guest: Exactly! They are rethinking the way we do sliding windows from the ground up to better match how GPUs actually work. It's a clever approach that marries the theoretical benefits of sparse attention with the practical realities of hardware limitations.
Host: Okay, so instead of sliding over contiguous tokens, STA operates tile-by-tile. Can you explain what a tile is in this context and how it helps with efficiency?
Guest: Sure! A tile is basically a contiguous group of tokens forming a spatial-temporal cube. Its size is determined by the block size in FlashAttention. So, instead of sliding the window one token at a time, STA slides it tile by tile. This enables more efficient memory access and parallelism while preserving the 3D locality.
Host: Got it! So, by operating on these larger tiles, they can leverage the way FlashAttention works to speed things up. But how does this eliminate the need for explicit attention masking?
Guest: That's one of the key innovations. Because STA slides over tiles instead of individual tokens, it eliminates the need for explicit attention masking at the computation stage. The sparse attention mask is managed entirely by the producer warpgroups, while the computation on the consumer warpgroups remains dense and hardware-efficient.
Host: Okay, so the producer warpgroups handle the loading of data from memory while the consumer warpgroups do the actual attention calculation. This division of labor allows the sparse attention mask to be managed efficiently without slowing down the core computation.
Guest: Precisely! The producer warpgroups act as asynchronous data loaders, pre-processing the data and managing the sparse mask so that the consumer warpgroups can focus on dense, efficient computation. It's like having a dedicated team preparing the ingredients so the chef can focus on cooking.
Host: So, STA is not only more efficient in terms of FLOPs, but also more hardware-friendly because it reduces the overhead of mask evaluation. The authors say it's the first higher-order sliding-window-like attention to achieve wall-clock speedups proportional to sparsity.
Guest: And that's a significant achievement. It means that the more sparse the attention, the faster the computation becomes. It's a linear relationship, which is what you want. But it is not only about efficient computation! I think finding an optimal window size is crucial to preserve the video generation quality.
Host: That's right. It's not just about speed; we also need to make sure the video quality doesn't suffer. How do they determine the right window size?
Guest: They found that different attention heads exhibit specialized locality patterns. Some heads focus on finer details in a small area, while others capture broader context at a larger window. The critical point is that this head specialization remains agonistic to prompts. That is to say, for different prompts, the head specialization is consistent.
Host: Okay, so different attention heads are responsible for different things, and their locality patterns are consistent across different prompts. How do they use this to configure the optimal window size per head?
Guest: They developed a method to automatically configure the optimal window size per head via profiling. By analyzing the attention patterns of each head, they can strike a balance between efficiency and quality.
Host: So, what were the results? Did STA actually make a difference?
Guest: Absolutely! With STA, HunyuanVideo can generate a 5-second 720P video much faster. The paper reports significant end-to-end speedups over FlashAttention-2 and FlashAttention-3. By fine-tuning the diffusion models, they unlock even greater efficiency.
Host: Those are some impressive speedups! But, let's dig a little deeper into the technical details. The paper mentions something about FlashAttention block sizes and how they relate to the tile size in STA. Can you break that down for us?
Guest: Sure! FlashAttention works by dividing the input sequence into smaller blocks. These blocks are loaded into the GPU's SRAM for computation. STA sets the tile size to match the block size in FlashAttention, which helps maximize hardware utilization. This ensures that the data being processed by FlashAttention is already in the optimal format, reducing overhead and improving performance.
Host: That makes a lot of sense. It's all about aligning the algorithm with the underlying hardware. But, the paper also talks about dense blocks, empty blocks, and mixed blocks in the attention map. What's the difference between these, and how does STA minimize the mixed blocks?
Guest: In the world of sparse attention, dense blocks are the ideal scenario. They fully utilize the computational resources as they contain valid attention scores for all elements. Empty blocks, on the other hand, are the opposite, containing only masked-out values, which can be skipped entirely, saving computation. Mixed blocks, however, are tricky. They contain a mix of valid and masked-out attention scores. While they are sparser than dense blocks, they still require computation for the entire block before the mask is applied. This overhead makes them less efficient than both dense and empty blocks. By operating tile-by-tile and ensuring that all queries within the same tile attend to the same set of keys, STA eliminates mixed blocks and focuses on optimizing the processing of dense blocks.
Host: So, STA is designed to create a more structured attention pattern, leading to mostly dense blocks and some empty blocks, while avoiding the inefficient mixed blocks. So, what kind of kernel-level optimizations did they make to implement STA efficiently? I remember the paper mentioned something about FlexAttention and ThunderKittens?
Guest: They can optimize the sparse attention masks by disaggregating the inter-block mask logic from the compute kernels. Their implementation splits the threadblock into compute warpgroups and data warpgroups. Each compute warpgroup is responsible for calculating one query block, while the data warpgroup is responsible for asynchronously loading the KV blocks from HBM to SRAM.
Host: It sounds like they've really thought about every aspect of the implementation, from the high-level algorithm down to the low-level kernel optimizations.
Guest: Exactly! And that's what makes this work so impactful. It's not just a clever idea; it's a well-engineered solution that takes into account the realities of modern GPU architecture.
Host: Okay, so we've talked about the problem, the method, and some of the implementation details. Now, let's move on to the experiments. How did they evaluate STA, and what were the key findings?
Guest: They evaluated STA on HunyuanVideo, which is a state-of-the-art open video DiT. They measured efficiency using metrics like MFU and latency, and they assessed video quality through human evaluation and automated metrics like VBench. One key aspect of their evaluation was comparing STA to other sparse attention methods, including CLEAR, NATTEN, and Swin.
Host: And how did STA stack up against these other methods?
Guest: The results showed that STA outperformed the other methods in both efficiency and quality. CLEAR and NATTEN suffered from efficiency issues, while Swin resulted in quality degradation. STA, on the other hand, achieved significant speedups with minimal quality loss. They also conducted human evaluations to compare the visual quality of videos generated by different models.
Host: And what did the human evaluators think?
Guest: The human evaluators preferred videos generated by STA over those generated by other methods, indicating that STA was able to maintain high video quality while significantly reducing computation time.
Host: That's really compelling! So, STA is not only faster but also produces videos that people find more visually appealing. Before we delve into the related work and conclusion, I was wondering if we could revisit the concept of fine-tuning that you mentioned earlier. Can you elaborate on how fine-tuning helps STA achieve even greater sparsity and efficiency?
Guest: Yes, let's do it. The idea behind fine-tuning is that after pre-training a video diffusion model with full attention, we can adapt it to STA by training it with a fixed window size and a high sparsity level. Now, STA already benefits from training-free mask search in pre-trained video DiTs, since it already exploits the 3D locality pattern and head specialization. However, if we restrict the model to a local window and fine-tune it, receptive fields can be expanded through stacked transformer layers. It can be learned efficiently with minimal training overhead.
Host: Ah, okay, so it's like further specializing the model for the STA architecture. What are the loss functions during fine-tuning?
Guest: The authors used a combination of loss functions during fine-tuning. The loss function can be split into the attention distillation loss, final layer loss and a data loss following the flow matching formulation. This ensures that each sparse attention layer approximates its corresponding dense attention teacher.