Giter VIP home page Giter VIP logo

ring-flash-attention's Introduction

Ring Flash Attention

This repo implements the RingAttention with FlashAttention. Currently, this repo implements:

  • ring_flash_attn_func: ring attention version of flash_attn_func
  • ring_flash_attn_varlen_func: ring attention version of flash_attn_varlen_func
  • zigzag_ring_flash_attn_func: an optimized version of ring_flash_attn_func, see issue#2
  • zigzag_ring_flash_attn_varlen_func: an optimized version of ring_flash_attn_varlen_func
  • stripe_flash_attn_func: stripe attention version of ring_flash_attn_func, the block size is set to 1 to use flash_attn api.

Note that

  • all function has the *_func, *_kvpacked_func, *_qkvpacked_func variant implemented.
  • the varlen versions only support passing one cu_seqlens.

The main idea is to use the softmax_lse output from the flash attention kernels.

The current performance on 8xH800 is (benchmark/benchmark_qkvpacked_func.py):

GPU theoretic flash_attn ring_attn zigzag_ring stripe_attn
fwd only (iter/sec) 8xH800 2418.4 / 8 = 302.3 208.0 283.0 259.6
68.8% 93.6% 85.9%
fwd + bwd (iter/sec) 8xH800 705.2 / 8 = 88.2 54.3 75.7 76.9
61.5% 85.9% 87.2%
fwd only (iter/sec) 8xA100 1545.9 / 8 = 193.2 124.4 179.0 163.9
64.3% 92.7% 84.8%
fwd + bwd (iter/sec) 8xA100 470.6 / 8 = 58.8 33.3 49.5 45.9
56.6% 84.1% 78.1%

Note that

  • when running the benchmark with with 8 gpu, the flash attn code is running with 1/8 computation of ring attention.
  • nvlink between GPUs are required for high performance.
  • the varlen versions are slow at the moment, please use the non-varlen version if possible.

Limits

There are some arithmetic errors with the current implementation. The reason for them is probably that flash attention will return bf16 value for each block, so we cannot accumluate the values with the original fp32 ones.

And also because we need to save extra fp32 buffer during computation, the memory usage would be higher than theoretic limit.

TODOs

  • Implement ring_flash_attn_varlen_qkvpacked_func
  • Implement zigzag_ring_flash_attn_qkvpacked_func issue#2
  • Implement stripe_flash_attn_qkvpacked_func
  • Implement zigzag_ring_flash_attn_varlen_qkvpacked_func
  • Implement *_kvpacked_func and *_func variant for all APIs
  • Optimize *_varlen_func
  • Try to upstream to flash attention.

Test

torchrun --nproc_per_node 8 test/test_ring_flash_attn_func.py
torchrun --nproc_per_node 8 test/test_ring_flash_attn_varlen_func.py
torchrun --nproc_per_node 8 test/test_zigzag_ring_flash_attn_func.py
torchrun --nproc_per_node 8 test/test_zigzag_ring_flash_attn_varlen_func.py
torchrun --nproc_per_node 8 test/test_stripe_flash_attn_func.py

Benchmark

torchrun --nproc_per_node 8 benchmark/benchmark_qkvpacked_func.py
torchrun --nproc_per_node 8 benchmark/benchmark_varlen_qkvpacked_func.py

ring-flash-attention's People

Contributors

zhuzilin avatar reyoung avatar yuxin-cv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.