FlashAttention for Scalable Vector Architectures

LLM MQIA AI Systems and Hardware EDIO
在 CPU 上运行 Transformer 模型的推理正变得日益重要,尤其对于小型语言模型(SLM),向量架构正逐渐成为一种极具前景的执行平台。其中,注意力模块因对内存带宽要求极高而成为主要性能瓶颈;FlashAttention 通过融合多个计算操作,提升数据局部性并减少中间结果的内存传输量,从而缓解这一问题。本文提出 FlashAttention-V——一种面向可扩展向量架构的分块式 FlashAttention 实现:它能高效适配从短向量到极长向量的各种场景,其核心机制包括——利用注意力头之间的并行性、采用跨头打包(inter-head packing)策略以充分复用超出单头维度的向量长度、并进一步优化向量寄存器利用率与内存访问局部性。我们将 FlashAttention-V 集成至 llama.cpp 的 ggml 后端,并在 TinyLlama、Llama 3.2、Qwen2.5 和 Pythia-410M 四个模型上,借助 gem5 模拟器与 Banana Pi BPI-F3 硬件平台开展评估。在 Banana Pi BPI-F3 平台上,我们验证了针对注意力头进行循环重排序(loop reordering)与循环展开(loop unrolling)是两项行之有效的优化原则:其性能增益随模型规模增大而持续提升,且在上下文较短及解码阶段尤为显著。基于模拟器的深入分析表明,在预填充(prefill)阶段,当向量长度(VL)为 512 位时,FlashAttention-V 相比标量版 FlashAttention 可实现 22 倍至 42 倍的加速;若进一步将向量处理单元扩展至 64 路(lanes)、向量长度提升至 4096 位,则还可额外获得 2 倍至 2.5 倍的性能提升。而在解码(decode)阶段,FlashAttention-V 在 512 位向量长度下相较标量版 FlashAttention 可达 8 倍至 11 倍加速;此时由于解码为单 token、内存带宽受限的执行模式,其性能对向量宽度与路数的敏感性趋于减弱。此外,我们还发现 Q8_0 量化线性层存在结构性瓶颈:在长向量执行模式下,其算术运算的摊销效率受到严重制约;该现象在 RISC-V 向量扩展(RVV)与 Arm SVE 架构上均一致出现,表明当前主流量化格式本质上制约了长向量架构的可扩展性。
Inference with transformer models on CPUs is increasingly important, especially for Small Language Models (SLMs), where vector architectures are emerging as a promising execution substrate. The attention module is a major bottleneck due to high memory bandwidth requirements; FlashAttention mitigates this by fusing operations to improve data locality and reduce intermediate memory traffic. In this paper, we present FlashAttention-V, a blocked FlashAttention for scalable vector architectures that adapts efficiently from short to very long vectors by exploiting parallelism across attention heads, inter-head packing to enable efficient utilization of vector lengths beyond the head dimension, and improving vector register utilization and memory access locality. We integrate FlashAttention-V into ggml within llama.cpp and evaluate it on TinyLlama, Llama 3.2, Qwen2.5, and Pythia-410M using gem5 and a Banana Pi BPI-F3. On the Banana Pi BPI-F3, we confirm that loop reordering and loop unrolling across attention heads are effective optimization principles, scaling performance gains with larger models and most pronounced with short contexts and during decoding. Simulation-based analysis shows that FlashAttention-V achieves 22x-42x speedup over scalar FlashAttention at 512-bit VL in prefill, with an additional 2x-2.5x gain scaling to 64 lanes and 4096-bit VL. During decode, FlashAttention-V achieves 8x-11x speedup using 512-bit vector lengths over scalar FlashAttention, with performance showing diminishing sensitivity to vector width and lane count due to single-token, memory-bound execution. We further identify structural bottlenecks in Q8_0 quantized linear layers that limit arithmetic amortization under long-vector execution, consistent across RVV and Arm SVE, indicating that current quantization formats pose a fundamental challenge to long-vector scalability.
许愿