Response to Reviewer 5syF(Part 1)
Q1. How does attention handle autoregressive decoding?
Inference can be divided into two stages. The first stage involves calculating the key-value (kv) based on the input prompt. This is done using the following code:
```
# b, h, n, e, d
kv_outproduct = torch.einsum("... n e, ... n d -> ... n e d", k, v)
# 1, 1, n, 1, 1
index = torch.arange(n - 1, -1, -1).reshape(1, 1, -1, 1, 1).to(x)
decay = ratio.unsqueeze(0).unsqueeze(-1) ** index
kv_outproduct_with_decay = kv_outproduct * decay
kv = torch.sum(kv_outproduct_with_decay, dim=-3)
```
The second stage involves using the calculated kv and computing the output. This is done using the following code:
```
kv = ratio * kv + torch.einsum(
"... n d, ... n e -> ... d e",
k,
v,
)
output = torch.einsum(
"... n e, ... e d -> ... n d", q, kv
)
```
Please note that Stage 1 is executed only once, and the cost of each inference is mostly determined by Stage 2. The time and space complexity for Stage 2 are both $O(bhd)$, where b represents the batch size, h represents the number of heads, and d represents the head dimension, which is usually either 64 or 128.
We provide a speed comparison in the table below for different sequence lengths, measured in tokens/second/gpu. The larger the value, the better:
| Model/Seqlen | 512 | 1024 | 2048 | 4096 | 8192 |
| --- | --- | --- | --- | --- | --- |
| Transformer | 133.44 | 86.69 | 34.95 | 12.42 | 3.69 |
| TransnormerLLm | 274.48 | 158.41 | 86.52 | 43.97 | 22.88 |
Q2. The contribution of this work.
Scaling up a linear attention-based language model and making it faster and better than transformer-based models is a non-trivial task. It requires extensive optimization of the whole system rather than a small part of the model. As shown in this paper, we make crucial enhancements to nearly every part of the model, such as the architecture, the attention mechanism, the inference algorithm, and the model parallel implementation.
Regarding the difference between lightning attention and flash attention, our lightning attention is IO-friendly linear attention, while the flash attention is IO-friendly softmax attention. We agree that Flash Attention sheds light on our lightning attention. By incorporating this idea, we have successfully addressed the issue of slow speed in the causal scenario of Linear Attention mentioned in "Flash: Transformer Quality in Linear Time". In addition, by leveraging the characteristics of Linear Attention, our lightning attention can achieve significant efficiency improvements compared to Flash 1/2. We provide a comparison of the efficiency between our method and Flash Attention 1/2 in the following section.
Q3. The computing method of lighting attention.
In our preliminary version, we use eq 10 implementations to avoid the slow looping issue mentioned in "Flash: Transformer Quality in Linear Time". However, we have further optimized our approach by leveraging the mechanisms of linear attention, resulting in enhanced efficiency. Through these optimizations, our lightning attention demonstrates significant improvements in efficiency compared to Flash 1/2. Below, we provide an efficiency comparison between our method and Flash Attention 1/2. The speed table is measured in seconds, while the memory table is measured in MB. Smaller values indicate superior performance in both speed and memory.
Speed table(seconds):
Forward:
| Seqlen | Lightning | Flash2 | Flash1 |
| --- | --- | --- | --- |
| 1024.0 | 0.255829 | 0.188746 | 0.282317 |
| 2048.0 | 0.408914 | 0.459194 | 0.875625 |
| 4096.0 | 0.808699 | 1.552662 | 3.094903 |
| 8192.0 | 1.601662 | 5.735785 | 11.579776 |
| 16384.0 | 3.197140 | 22.200319 | 44.999168 |
| 32768.0 | 6.380953 | 86.720512 | 178.348038 |
Backward:
| Seqlen | Lightning | Flash2 | Flash1 |
| --- | --- | --- | --- |
| 1024.0 | 0.647602 | 0.526302 | 0.786245 |
| 2048.0 | 1.268677 | 1.516766 | 2.427769 |
| 4096.0 | 2.507520 | 4.948076 | 8.316556 |
| 8192.0 | 4.990168 | 17.650484 | 30.684160 |
| 16384.0 | 9.925859 | 66.620415 | 117.788673 |
| 32768.0 | 19.840511 | 258.737152 | 461.466614 |
Memory Table(MB):
Forward:
| Seqlen | Lightning | Flash2 | Flash1 |
| --- | --- | --- | --- |
| 1024.0 | 64.000488 | 80.500488 | 96.500977 |
| 2048.0 | 128.000488 | 161.000488 | 193.000977 |
| 4096.0 | 256.000488 | 322.000488 | 386.000977 |
| 8192.0 | 512.000488 | 644.000488 | 772.000977 |
| 16384.0 | 1024.000488 | 1288.000488 | 1544.000977 |
| 32768.0 | 2048.000488 | 2576.000488 | 3088.000977 |
Backward:
| Seqlen | Lightning | Flash2 | Flash1 |
| --- | --- | --- | --- |
| 1024.0 | 166.400488 | 215.400488 | 215.400488 |
| 2048.0 | 332.800488 | 430.800488 | 430.800488 |
| 4096.0 | 665.600488 | 861.600488 | 861.600488 |
| 8192.0 | 1331.200488 | 1723.200488 | 1723.200488 |
| 16384.0 | 2662.400488 | 3446.400488 | 3446.400488 |
| 32768.0 | 5324.800488 | 6892.800488 | 6892.800488 |