---
title: "FlashAttention — フラッシュアテンション"
source: "https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3"
wiki: "systems-analysis.info/int"
article: "FlashAttention_—_フラッシュアテンション"
language: "ja"
categories:
  - "Category:Japanese"
  - "Category:Large language models"
  - "Category:LLM evaluation"
  - "Category:Machine learning"
revision_id: 2271
wiki_created_at: 2026-09-06T23:01:18Z
wiki_modified_at: 2026-09-06T23:01:18Z
downloaded_at: 2026-09-07T22:50:15Z
---

# FlashAttention — フラッシュアテンション

**FlashAttention**は、トランスフォーマーのスケールドドット積アテンションを、近似を用いずに高速かつメモリ効率よく計算するためのアルゴリズムです。大規模言語モデル（LLM）の訓練や推論において、長いシーケンスを扱う際の主要なボトルネックであるGPUメモリへの読み書きを削減することを目的としています。この手法は、2022年にTri Dao、Daniel Y. Fu、Stefano Ermon、Atri Rudra、Christopher Réによって発表されました<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_main-1)</sup>。

FlashAttentionの中核的なアイデアは、GPUのメモリ階層を考慮してアテンション計算を再構成することにあります。これにより、低速な大容量メモリへのアクセス回数を抑え、標準的なアテンション実装で問題となるメモリ帯域幅の制約を緩和します。

## 標準的なアテンションの問題点

トランスフォーマーにおける標準的な自己アテンション機構は、一般に次の式で表されます。

$$
\text{Attention}(Q,K,V) = \text{softmax}\left( \frac{QK^{T}}{\sqrt{d_{k}}} \right)V
$$

ここで、$Q$、$K$、$V$はそれぞれクエリ、キー、バリューの行列です。

単純な実装では、シーケンス長を$N$とすると、$N \times N$のアテンションスコア行列を明示的に計算し、メモリに保持します。そのため、時間計算量は$O(N^{2})$となり、中間アテンション行列を保存する場合のメモリ使用量も$O(N^{2})$になります<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_main-1)</sup>。これは長いコンテキストを扱うLLMで特に大きな問題になります。

主な問題は次の2点です。

1.  **メモリ使用量が大きい**: $N \times N$のアテンション行列は、シーケンス長が長くなるほど急速に巨大化します。
2.  **入出力（I/O）の負荷が高い**: 実際の実行時間では、演算回数だけでなく、GPUメモリ階層間の読み書きが大きなボトルネックになります。

### GPUのメモリ階層

FlashAttentionは、GPUにおける高速だが小容量のオンチップメモリと、低速だが大容量のHBM（High Bandwidth Memory）の差を利用します。たとえばNVIDIA A100では、オンチップSRAMは容量が限られる一方で帯域幅が非常に高く、HBMは容量が大きい一方でオンチップメモリより帯域幅が低くなります<sup>[\[2\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-openreview_fa1-2)</sup>。

標準的なアテンション実装は、大きな中間行列をHBMに書き込み、再び読み出す必要があります。そのため、アテンション計算はしばしば**メモリ帯域幅に制約される**（memory-bound）処理になります。

## FlashAttentionの主要な技術

FlashAttentionは**I/O-aware**（I/Oを意識した）アルゴリズムであり、HBMへの読み書きを減らすことでアテンション計算を高速化します。主な技術は、タイリング、オンラインSoftmax、カーネル融合です。

### タイリングとブロック処理

FlashAttentionは、$Q$、$K$、$V$の行列全体を一度に処理するのではなく、オンチップメモリに収まる小さなブロック（**タイル**）に分割して処理します。各ブロックをSRAMに読み込み、そのブロックに対するアテンション計算を実行し、出力を段階的に更新します。

この処理により、完全な$N \times N$のアテンション行列をHBMに保存する必要がなくなります<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_main-1)</sup>。結果として、長いシーケンスに対してもメモリ使用量を大きく削減できます。

### オンラインSoftmax計算

FlashAttentionの重要な要素は、Softmaxをブロック単位で正確に計算するオンラインアルゴリズムです。通常のSoftmaxでは、正規化のために入力ベクトル全体を参照する必要があります。FlashAttentionでは、各行について現在までの最大値と指数和を保持し、新しいブロックを処理するたびにこれらを更新します。

この方法により、アテンション行列全体を一度に保持しなくても、標準的なアテンションと同じ数式に基づく結果を得ることができます。ただし、実際のGPU実装では浮動小数点演算の順序が変わるため、丸め誤差の差は生じ得ます<sup>[\[2\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-openreview_fa1-2)</sup>。

### CUDAカーネルの融合

FlashAttentionでは、行列積$QK^{T}$、マスキング、Softmax、$V$との乗算といった複数の処理を、可能な限り融合されたGPUカーネル内で実行します。これにより、中間結果をHBMに何度も書き戻す必要がなくなり、メモリ読み書きの回数が削減されます。

標準的な実装が複数のカーネル呼び出しと中間テンソルの保存を必要とするのに対し、FlashAttentionはブロック単位でデータを読み込み、オンチップメモリ上で計算を進め、最終的な出力を効率よく書き出します。

## 理論的および実践的な効率性

### 計算量とI/O最適性

FlashAttentionは、厳密なアテンション計算の時間計算量$O(N^{2})$そのものを$O(N)$にするわけではありません。$QK^{T}$に対応する組み合わせは依然として二乗個存在するためです。一方で、明示的な$N \times N$アテンション行列を保存しないため、アテンション計算に必要な補助的メモリを$O(N^{2})$から$O(N)$に削減できます<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_main-1)</sup>。

また、FlashAttentionの論文では、2階層のメモリモデルにおいて、一定範囲のSRAMサイズに対してHBMアクセス量が理論的に最適であることが示されています<sup>[\[2\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-openreview_fa1-2)</sup>。これは、単に演算を減らすのではなく、メモリ階層に合わせてデータ移動を最小化する設計であることを意味します。

### 実験結果

最初のFlashAttention論文では、複数のモデルとタスクで高速化とメモリ削減が報告されました<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_main-1)</sup>。

- **高速化**:
  - BERT-large（シーケンス長512）では、MLPerf 1.1の訓練速度記録と比較してエンドツーエンドで約15%高速化。
  - GPT-2（シーケンス長1K）では約3倍の高速化。
  - Long Range Arenaタスク（シーケンス長1K〜4K）では約2.4倍の高速化。
- **メモリ削減**: 正確なアテンションのベースライン実装と比較して、長いシーケンスで大幅なメモリ削減を実現。
- **長文処理への効果**: より長いコンテキストを扱えるようになったことで、GPT-2のパープレキシティ改善や長文分類タスクの精度向上も報告されています。

## 進化と関連技術

FlashAttentionの考え方は、その後のGPU向けアテンションカーネルやLLM推論エンジンに大きな影響を与えました。

### FlashAttention-2（2023年）

**FlashAttention-2**は、FlashAttentionの並列化とワーク分割を改善した第2世代の手法です。オリジナルのFlashAttentionは、NVIDIA A100上で理論ピーク性能の約25〜40%にとどまる場合がありました。FlashAttention-2は、非行列積演算の削減、スレッドブロック間の並列化、ワープ間の作業分担の改善により、GPU利用効率を高めました<sup>[\[3\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_fa2-3)</sup>。

主な特徴は次の通りです。

- FlashAttentionと比較して、およそ2倍の高速化を報告。
- NVIDIA A100上で理論ピークFLOPSの約50〜73%に到達。
- ヘッド次元256、Multi-Query Attention（MQA）、Grouped-Query Attention（GQA）などへの対応を拡大。

### FlashAttention-3（2024年）

**FlashAttention-3**は、NVIDIA Hopper世代のGPU、特にH100向けに最適化された手法です<sup>[\[4\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_fa3-4)</sup>。Tensor CoreとTMA（Tensor Memory Accelerator）の非同期実行、warp specialization、FP8低精度演算などを活用します。

PyTorch Blogでも、FlashAttention-3はH100上でFlashAttention-2に対してFP16で約1.5〜2.0倍高速化し、最大約740 TFLOPS、FP8では約1.2 PFLOPSに近い性能を達成したと報告されています<sup>[\[5\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-pytorch_blog_fa3-5)</sup>。

### 推論向けの関連実装

FlashAttentionの設計思想は、LLM推論向けのカーネルやランタイムにも影響を与えています。

- **FlashInfer**（2025年）: LLM推論サービング向けのカスタマイズ可能なアテンションエンジンです。KVキャッシュの形式の多様性、JITコンパイル、リクエストの動的性質を考慮したスケジューリングなどに焦点を当てています<sup>[\[6\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-arxiv_flashinfer-6)</sup>。
- **FlashMLA**（2025年）: DeepSeekが公開したMulti-head Latent Attention（MLA）向けの最適化カーネルです。DeepSeek-V3系列などのモデルで用いられるMLAの推論、特にデコード段階の効率化を目的としています<sup>[\[7\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-github_flashmla-7)</sup>。

## 業界とエコシステムへの影響

FlashAttentionは、長コンテキストのトランスフォーマーを実用的に訓練・推論するための基盤技術の一つとなりました。PyTorchのscaled dot product attention（SDPA）やHugging Face Transformersでは、ハードウェアやデータ型が対応している場合にFlashAttention系のバックエンドを利用できます<sup>[\[8\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-pytorch_sdpa-8)[\[9\]](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_note-hf_attention-9)</sup>。

ただし、FlashAttentionはアテンションの理論的な二乗計算量そのものをなくす手法ではありません。主な利点は、メモリ使用量とGPUメモリ階層間のデータ移動を削減し、既存の正確なアテンション計算を実機上で大幅に効率化する点にあります。そのため、長いコンテキスト、マルチモーダル入力、高解像度画像、コード生成、音声・動画処理など、長いシーケンスを必要とする多くの応用で重要な最適化技術となっています。

## 外部リンク

- <a href="https://github.com/Dao-AILab/flash-attention" class="external text" rel="nofollow">公式FlashAttention GitHubリポジトリ</a>
- <a href="https://tridao.me/blog/2024/flash3/" class="external text" rel="nofollow">FlashAttention-3に関するTri Dao氏の解説</a>

## 脚注

1.  <span id="cite_note-arxiv_main-1">↑ <sup>[1.0](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_main_1-0)</sup> <sup>[1.1](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_main_1-1)</sup> <sup>[1.2](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_main_1-2)</sup> <sup>[1.3](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_main_1-3)</sup> <sup>[1.4](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_main_1-4)</sup> Dao, Tri, et al. “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness.” *arXiv:2205.14135*, 2022. <a href="https://arxiv.org/abs/2205.14135" class="external autonumber" rel="nofollow">[1]</a></span>
2.  <span id="cite_note-openreview_fa1-2">↑ <sup>[2.0](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-openreview_fa1_2-0)</sup> <sup>[2.1](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-openreview_fa1_2-1)</sup> <sup>[2.2](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-openreview_fa1_2-2)</sup> Dao, Tri, et al. “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness.” *OpenReview*. <a href="https://openreview.net/forum?id=H4DqfPSibmx" class="external autonumber" rel="nofollow">[2]</a></span>
3.  <span id="cite_note-arxiv_fa2-3">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_fa2_3-0) Dao, Tri. “FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning.” *arXiv:2307.08691*, 2023. <a href="https://arxiv.org/abs/2307.08691" class="external autonumber" rel="nofollow">[3]</a></span>
4.  <span id="cite_note-arxiv_fa3-4">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_fa3_4-0) Shah, Jay, et al. “FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision.” *arXiv:2407.08608*, 2024. <a href="https://arxiv.org/abs/2407.08608" class="external autonumber" rel="nofollow">[4]</a></span>
5.  <span id="cite_note-pytorch_blog_fa3-5">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-pytorch_blog_fa3_5-0) Shah, Jay, et al. “FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision.” *PyTorch Blog*, 2024. <a href="https://pytorch.org/blog/flashattention-3/" class="external autonumber" rel="nofollow">[5]</a></span>
6.  <span id="cite_note-arxiv_flashinfer-6">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-arxiv_flashinfer_6-0) Ye, Zihao, et al. “FlashInfer: Efficient and Customizable Attention Engine for LLM Inference Serving.” *arXiv:2501.01005*, 2025. <a href="https://arxiv.org/abs/2501.01005" class="external autonumber" rel="nofollow">[6]</a></span>
7.  <span id="cite_note-github_flashmla-7">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-github_flashmla_7-0) “FlashMLA: Efficient Multi-head Latent Attention Kernels.” *GitHub - deepseek-ai/FlashMLA*. <a href="https://github.com/deepseek-ai/FlashMLA" class="external autonumber" rel="nofollow">[7]</a></span>
8.  <span id="cite_note-pytorch_sdpa-8">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-pytorch_sdpa_8-0) “torch.nn.functional.scaled_dot_product_attention.” *PyTorch Documentation*. <a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html" class="external autonumber" rel="nofollow">[8]</a></span>
9.  <span id="cite_note-hf_attention-9">[↑](https://systems-analysis.info/int/FlashAttention_%E2%80%94_%E3%83%95%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%86%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%B3#cite_ref-hf_attention_9-0) “Attention backends.” *Hugging Face Transformers Documentation*. <a href="https://huggingface.co/docs/transformers/en/attention_interface" class="external autonumber" rel="nofollow">[9]</a></span>
