---
title: "FlashAttention-2 (TH)"
source: "https://systems-analysis.info/int/FlashAttention-2_(TH)"
wiki: "systems-analysis.info/int"
article: "FlashAttention-2_(TH)"
language: "th"
categories:
  - "Category:Large language models"
  - "Category:Machine learning"
  - "Category:Thai"
revision_id: 2212
wiki_created_at: 2026-09-06T23:00:23Z
wiki_modified_at: 2026-09-06T23:00:23Z
downloaded_at: 2026-09-07T22:49:52Z
---

# FlashAttention-2 (TH)

**FlashAttention-2** — คืออัลกอริทึมที่ได้รับการปรับปรุงซึ่งออกแบบมาเพื่อคำนวณกลไก attention ในโมเดลภาษาขนาดใหญ่ (LLM) อัลกอริทึมนี้พัฒนาโดย **Tri Dao** และนักวิจัยจากมหาวิทยาลัยสแตนฟอร์ด และได้รับการนำเสนอในเดือนกรกฎาคม ปี 2023<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_main-1)</sup> เป้าหมายหลักของมันคือการเร่งความเร็วการฝึกและการอนุมาน (inference) ของโมเดล transformer อย่างมีนัยสำคัญ ด้วยการใช้ทรัพยากรฮาร์ดแวร์ GPU อย่างมีประสิทธิภาพมากขึ้น ในขณะที่ยังคงรักษาความเหมือนกันอย่างสมบูรณ์กับการคำนวณของกลไก attention มาตรฐาน กล่าวคือ **ไม่สูญเสียความแม่นยำ**

FlashAttention-2 เป็นการพัฒนาต่อยอดจากอัลกอริทึม **FlashAttention** ที่นำเสนอโดยทีมเดียวกันในปี 2022 เวอร์ชันใหม่นี้แก้ปัญหาการโหลด GPU ที่ไม่สมบูรณ์ซึ่งพบในรุ่นก่อนหน้า และบรรลุความเร็วที่เพิ่มขึ้นเกือบสองเท่าเมื่อเทียบกับเวอร์ชันแรก

## บริบทและที่มา: ปัญหา attention ใน transformer

กลไก self-attention มาตรฐานเป็นจุดคอขวดเมื่อทำงานกับลำดับข้อความที่ยาวใน transformer ความซับซ้อนในการคำนวณและการใช้หน่วยความจำเพิ่มขึ้นแบบ **กำลังสอง** (O(N²)) ตามความยาวของลำดับ (N) ซึ่งกำหนดข้อจำกัดร้ายแรงต่อความยาว context สูงสุดและความสามารถในการขยายขนาดของ LLM<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_main-1)</sup>

เพื่อแก้ปัญหานี้ อัลกอริทึม **FlashAttention** จึงถูกนำเสนอในปี 2022<sup>[\[2\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-huggingface_optim-2)</sup> แนวคิดหลักของมันได้แก่:

- **การคำนึงถึงลำดับชั้นหน่วยความจำของ GPU (IO-awareness)**: อัลกอริทึมลดการดำเนินการอ่าน/เขียนที่มีต้นทุนสูงระหว่างหน่วยความจำ GPU ที่ช้า (HBM) และหน่วยความจำสถิตที่เร็ว (SRAM) บนชิป
- **การประมวลผลแบบบล็อก (tiling)**: การคำนวณถูกแบ่งออกเป็นบล็อกเล็กๆ (tile) ซึ่งประมวลผลใน SRAM ที่เร็ว ทำให้หลีกเลี่ยงการสร้างเมทริกซ์ attention เต็มรูปแบบในหน่วยความจำได้

สิ่งนี้ทำให้บรรลุการเติบโตของการใช้หน่วยความจำแบบ **เชิงเส้น** (O(N)) และความเร็วที่เพิ่มขึ้น 2–4 เท่าเมื่อเทียบกับการนำไปใช้งานมาตรฐาน<sup>[\[2\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-huggingface_optim-2)</sup> FlashAttention ได้รับการยอมรับอย่างแพร่หลายและช่วยให้เกิดโมเดลที่มี context ที่ยาวขึ้นอย่างมีนัยสำคัญ เช่น จาก 2–4 พัน token (GPT-3) เป็น 128,000 token (GPT-4) และมากกว่านั้น<sup>[\[3\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-tridao_blog_fa3-3)</sup> ตัวอย่างเช่น ในโมเดล **Falcon-40B** การใช้ FlashAttention ทำให้ inference เร็วขึ้น 3 เท่า และประสิทธิภาพการสร้างโดยรวมเร็วขึ้น 5 เท่าเมื่อเทียบกับ GPT-3<sup>[\[4\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-e2e_analysis-4)</sup>

## การพัฒนาและเป้าหมายของ FlashAttention-2

แม้จะประสบความสำเร็จ FlashAttention เวอร์ชันแรกยังไม่ได้ใช้ทรัพยากรการคำนวณของ GPU อย่างเต็มที่ บนการ์ดจอ **NVIDIA A100** ประสิทธิภาพอยู่ที่เพียง **25–40%** ของค่าสูงสุดทางทฤษฎี (FLOPs/s)<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_main-1)</sup> สาเหตุหลักคือการโหลด Streaming Multiprocessors ที่ไม่เหมาะสมและการดำเนินการกับ shared memory ที่มีเกินความจำเป็น<sup>[\[5\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-openreview_fa2-5)</sup>

เป้าหมายของ **FlashAttention-2** คือการเร่งความเร็วการคำนวณให้มากขึ้นโดยการกระจายงานแบบขนานที่มีประสิทธิภาพมากขึ้นและลดการดำเนินการเสริม อัลกอริทึมได้รับการเขียนใหม่ทั้งหมดโดยใช้ primitive ระดับต่ำของไลบรารี **NVIDIA CUTLASS 3.x** เพื่อให้ได้ประสิทธิภาพสูงสุด<sup>[\[6\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-hazyresearch_blog-6)</sup>

## สถาปัตยกรรมทางเทคนิคและหลักการทำงาน

FlashAttention-2 นำเสนอการปรับปรุงสำคัญสามประการเพื่อเพิ่ม parallelism และประสิทธิภาพ<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_main-1)</sup>:

### 1. การลดการดำเนินการที่ไม่ใช่เมทริกซ์

อัลกอริทึมลดจำนวนการดำเนินการเลขทศนิยมเสริมที่ไม่ใช่การคูณเมทริกซ์ (non-matmul FLOPs) เนื่องจาก tensor core ของ GPU ได้รับการปรับให้เหมาะสมสำหรับการดำเนินการเมทริกซ์ (GEMM) โดยเฉพาะและรันได้เร็วกว่าถึง 16 เท่า การเปลี่ยนแปลงนี้จึงทำให้สามารถใช้บล็อก GPU ที่มีประสิทธิภาพสูงสุดในช่วงเวลาส่วนใหญ่ได้

### 2. Parallelism ที่ได้รับการปรับปรุง

ใน FlashAttention ดั้งเดิม การทำงานบน "head" attention หนึ่งตัวไม่ได้รับการกระจายแบบขนาน ซึ่งนำไปสู่การว่างงานเมื่อลำดับยาวและ batch size เล็ก FlashAttention-2 นำเสนอ **inter-block parallelism**: บัดนี้การคำนวณสำหรับ attention head หนึ่งตัวถูกกระจายระหว่าง Streaming Multiprocessors ต่างๆ ของ GPU ซึ่งเพิ่มการโหลดอย่างมีนัยสำคัญ

### 3. การแบ่งงานที่ได้รับการปรับปรุงภายในบล็อก

ในระดับของบล็อกการคำนวณหนึ่งบล็อก งานถูกกระจายใหม่ระหว่างกลุ่มของ thread (warp) เพื่อลดการแลกเปลี่ยนข้อมูลผ่าน shared memory สิ่งนี้ลดจำนวนการดำเนินการอ่าน/เขียนที่ซ้ำซ้อนซึ่งจำเป็นสำหรับการ normalize Softmax

## ประสิทธิภาพและความมีประสิทธิผล

ด้วยการปรับปรุงทางสถาปัตยกรรม FlashAttention-2 แสดงให้เห็นการเพิ่มประสิทธิภาพอย่างมีนัยสำคัญ:

- **ความเร็วเพิ่มขึ้นสองเท่า**: อัลกอริทึมทำงานเร็วกว่าประมาณ **2 เท่า** เมื่อเทียบกับ FlashAttention เวอร์ชันแรก<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_main-1)</sup>
- **การใช้งาน GPU สูง**: บน GPU **NVIDIA A100** บรรลุ **50–73%** ของ throughput สูงสุดทางทฤษฎี (TFLOPs) ซึ่งใกล้เคียงกับประสิทธิภาพของการดำเนินการคูณเมทริกซ์ที่ได้รับการปรับปรุง (GEMM)<sup>[\[1\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_main-1)</sup>
- **ความเร็วในการคำนวณที่สร้างสถิติใหม่**:
  - บน GPU **A100** บรรลุความเร็วสูงถึง **225 TFLOP/s** ในรอบการฝึกแบบ end-to-end ของโมเดลประเภท GPT ซึ่งสอดคล้องกับ 72% การใช้งาน compute unit เพื่อเปรียบเทียบ attention มาตรฐานในเงื่อนไขเดียวกันโหลด GPU ต่ำกว่า 100 TFLOP/s<sup>[\[7\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_pdf-7)</sup>
  - บน GPU **H100** ประสิทธิภาพสูงถึง **335 TFLOP/s**<sup>[\[7\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_pdf-7)</sup>

การเพิ่มประสิทธิภาพดังกล่าวทำให้สามารถ เช่น ฝึกโมเดลที่มี context window **16,000** token ในเวลาเดียวกับที่เคยต้องใช้สำหรับ window **8,000** token<sup>[\[5\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-openreview_fa2-5)</sup> สิ่งสำคัญคืออัลกอริทึมยังคง **แม่นยำ** และเป็น deterministic ดังนั้นการใช้งานมันจึงไม่ส่งผลต่อคุณภาพการทำนายของโมเดล<sup>[\[8\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-raschka_blog-8)</sup>

## การประยุกต์ใช้และการผสานรวมในระบบนิเวศ

FlashAttention-2 กลายเป็นเครื่องมือมาตรฐานในระบบนิเวศ LLM อย่างรวดเร็ว มันถูกผสานรวมเข้ากับ framework และไลบรารียอดนิยมหลายแห่ง:

- **PyTorch**: รองรับแบบ native
- **Hugging Face Transformers**: การรองรับเปิดใช้งานด้วยพารามิเตอร์ \`attn_implementation="flash_attention_2"\` เมื่อโหลดโมเดล<sup>[\[9\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-linkedin_younes-9)</sup> เข้ากันได้กับสถาปัตยกรรมหลายสิบแบบ (GPT, Llama, Falcon, BERT และอื่นๆ)<sup>[\[10\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-huggingface_gpu_infer-10)</sup>
- **TensorRT-LLM**, **xFormers** และ **Triton**: อัลกอริทึมได้รับการนำไปใช้งานสำหรับแพลตฟอร์มเหล่านี้ ซึ่งทำให้มีการประยุกต์ใช้อย่างกว้างขวาง<sup>[\[7\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_pdf-7)</sup>

การผสานรวมนี้ทำให้สามารถนำ FlashAttention-2 มาใช้ร่วมกับวิธีการปรับปรุงอื่นๆ ได้อย่างง่ายดาย เช่น การ quantization (GPTQ, QLoRA) และการ fine-tuning อย่างมีประสิทธิภาพ (PEFT)<sup>[\[9\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-linkedin_younes-9)</sup>

## การเปรียบเทียบกับเวอร์ชันถัดไป

### FlashAttention-3

การวิจัยในด้านการปรับปรุง attention ยังคงดำเนินต่อไป ในเดือนกรกฎาคม 2024 Tri Dao นำเสนอ **FlashAttention-3** ซึ่งมุ่งเป้าไปที่การใช้ประโยชน์จากความสามารถของสถาปัตยกรรม GPU **NVIDIA Hopper** (H100/H200) นวัตกรรมสำคัญ<sup>[\[3\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-tridao_blog_fa3-3)</sup>:

- **การรองรับ FP8**: ใช้การคำนวณทศนิยม 8 บิตเพื่อเร่งความเร็วเพิ่มเติม
- **การดำเนินการแบบ asynchronous**: ใช้ความสามารถแบบ asynchronous ของ GPU อย่างมีประสิทธิภาพมากขึ้น

FlashAttention-3 ให้ความเร็วเพิ่มขึ้น **1.5–2 เท่า** เมื่อเทียบกับ FlashAttention-2 บน GPU H100 โดยบรรลุประสิทธิภาพสูงถึง **740 TFLOP/s** (75% ของค่าสูงสุดทางทฤษฎี)<sup>[\[11\]](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_note-arxiv_fa3-11)</sup>

## เอกสารอ้างอิง

- Dao, T. (2023). *FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning*. arXiv:2307.08691.
- Dao, T. et al. (2022). *FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness*. arXiv:2205.14135.
- Shah, J. et al. (2024). *FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision*. arXiv:2407.08608.
- Kwon, W. et al. (2023). *Efficient Memory Management for Large Language Model Serving with PagedAttention*. arXiv:2309.06180.
- Ye, Z. et al. (2025). *FlashInfer: Efficient and Customizable Attention Engine for LLM Inference Serving*. arXiv:2501.01005.
- Chen, Y. et al. (2023). *FlashDecoding++: Faster Large Language Model Inference on GPUs*. arXiv:2311.01282.
- Liu, Y. et al. (2024). *FastAttention: Extending FlashAttention-2 to NPUs and Low-Resource GPUs*. OpenReview: 76NYyOrnfk.
- Dege, P. et al. (2025). *FlashMLA-ETAP: Efficient Transpose Attention Pipeline for Accelerating MLA Inference on NVIDIA H20 GPUs*. arXiv:2506.01969.
- Wang, G. et al. (2024). *FlashMask: Efficient and Rich Mask Extension of FlashAttention*. OpenReview: rog0J435OO.
- Abbott, V.; Zardini, G. (2025). *FlashAttention on a Napkin: A Diagrammatic Approach to Deep Learning IO-Awareness*. arXiv:2412.03317.

## หมายเหตุ

1.  <span id="cite_note-arxiv_main-1">↑ <sup>[1.0](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_main_1-0)</sup> <sup>[1.1](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_main_1-1)</sup> <sup>[1.2](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_main_1-2)</sup> <sup>[1.3](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_main_1-3)</sup> <sup>[1.4](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_main_1-4)</sup> <sup>[1.5](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_main_1-5)</sup> Дао, Три. «FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning». *arXiv:2307.08691* \[cs.LG\], 17 июля 2023 г. <a href="https://arxiv.org/abs/2307.08691" class="external autonumber" rel="nofollow">[1]</a></span>
2.  <span id="cite_note-huggingface_optim-2">↑ <sup>[2.0](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-huggingface_optim_2-0)</sup> <sup>[2.1](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-huggingface_optim_2-1)</sup> «Optimizing LLMs for Speed and Memory». *Hugging Face Documentation*. <a href="https://huggingface.co/docs/transformers/v4.42.0/en/llm_tutorial_optimization" class="external autonumber" rel="nofollow">[2]</a></span>
3.  <span id="cite_note-tridao_blog_fa3-3">↑ <sup>[3.0](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-tridao_blog_fa3_3-0)</sup> <sup>[3.1](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-tridao_blog_fa3_3-1)</sup> Дао, Три. «FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision». *Tri Dao's Blog*. <a href="https://tridao.me/blog/2024/flash3/" class="external autonumber" rel="nofollow">[3]</a></span>
4.  <span id="cite_note-e2e_analysis-4">[↑](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-e2e_analysis_4-0) «FlashAttention vs FlashAttention-2 - an Analysis». *E2E Networks Blog*. <a href="https://www.e2enetworks.com/blog/shades-of-attention-flashattention-vs-flashattention-2-a-comprehensive-study" class="external autonumber" rel="nofollow">[4]</a></span>
5.  <span id="cite_note-openreview_fa2-5">↑ <sup>[5.0](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-openreview_fa2_5-0)</sup> <sup>[5.1](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-openreview_fa2_5-1)</sup> «FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning». *OpenReview*. <a href="https://openreview.net/forum?id=mZn2Xyh9Ec" class="external autonumber" rel="nofollow">[5]</a></span>
6.  <span id="cite_note-hazyresearch_blog-6">[↑](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-hazyresearch_blog_6-0) «FlashAttention-2». *Hazy Research, Stanford University*. <a href="https://hazyresearch.stanford.edu/blog/2023-07-17-flash2" class="external autonumber" rel="nofollow">[6]</a></span>
7.  <span id="cite_note-arxiv_pdf-7">↑ <sup>[7.0](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_pdf_7-0)</sup> <sup>[7.1](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_pdf_7-1)</sup> <sup>[7.2](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_pdf_7-2)</sup> Дао, Три. «FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning» (PDF). *arXiv:2307.08691*. <a href="https://arxiv.org/pdf/2307.08691.pdf" class="external autonumber" rel="nofollow">[7]</a></span>
8.  <span id="cite_note-raschka_blog-8">[↑](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-raschka_blog_8-0) Рашка, Себастьян. «Llama 2 and FlashAttention 2». *Ahead of AI Magazine*. <a href="https://magazine.sebastianraschka.com/p/research-highlights-in-three-sentences" class="external autonumber" rel="nofollow">[8]</a></span>
9.  <span id="cite_note-linkedin_younes-9">↑ <sup>[9.0](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-linkedin_younes_9-0)</sup> <sup>[9.1](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-linkedin_younes_9-1)</sup> Белькада, Юнес. «Faster and more memory efficient models with Flash Attention 2!». *LinkedIn*. <a href="https://www.linkedin.com/posts/younes-belkada-b1a903145_flashattention-llms-activity-7112061650106474496-1dzz" class="external autonumber" rel="nofollow">[9]</a></span>
10. <span id="cite_note-huggingface_gpu_infer-10">[↑](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-huggingface_gpu_infer_10-0) «GPU inference». *Hugging Face Documentation*. <a href="https://huggingface.co/docs/transformers/v4.39.0/perf_infer_gpu_one" class="external autonumber" rel="nofollow">[10]</a></span>
11. <span id="cite_note-arxiv_fa3-11">[↑](https://systems-analysis.info/int/FlashAttention-2_(TH)#cite_ref-arxiv_fa3_11-0) Дао, Три, и др. «FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision». *arXiv:2407.08608* \[cs.LG\], 11 июля 2024 г. <a href="https://arxiv.org/abs/2407.08608" class="external autonumber" rel="nofollow">[11]</a></span>
