Giter Club home page Giter Club logo

flashattention.c's Introduction

FlashAttention.C

A minimal Flashattention implementation in pure Cuda C. It consistently performs as fast as Pytorch or faster for settings where the sequence length is limiting.

Inspired by recent efforts like: flashattention minimal, the goal of this project is to provide a readable implementation in pure Cuda, whilst also being fast and scalable.

Implementation:

Right now I have implemented two kernels, one is a naive tiling approach that suffers from SMEM stalls. The second uses thread coarsening (tiling on a single thread level in its registers) and reduces the SMEM stalls effectively.

Benchmarks:

These are results profiled on a RTX 3060:

  • Batchsize=2, Heads=8, Head Dim. = 64, Sequence Length=8192: Pytorch=120 ms, Ours=119 ms
  • Batchsize=2, Heads=8, Head Dim. = 32, Sequence Length=8192: Pytorch=117 ms, Ours=62 ms
  • Batchsize=8, Heads=16, Head Dim. = 64, Sequence Length=1024: Pytorch=24 ms, Ours=22 ms
  • Batchsize=8, Heads=16, Head Dim. = 32, Sequence Length=1024: Pytorch=22 ms, Ours=15 ms

Run:

To test against pytorch, run:

python bench_flashattention.py

or if you want to run it without pytorch wrapping run:

nvcc test.cu -o test
./test

Experimental:

Instead of caching the whole Q one can get some SMEM size savings by setting CACHE_Q to 0 in flashattention.cu and now must change uncomment line 150. This results in more SMEM stores but the gained occupancy by reducing the size of the SMEM has shown increase in performance in our tests.

Todo:

  • Optimize the Softmax part to let threads collaborate on computing the row-wise max instead. Getting down from linea complexity to log2 complexity with minimal overhead should be easily possible. As the batchsize are small overhead needs to be very low to make it work.
  • Optimize for lower SMEM footprint (maybe don't cache full Value rows): DONE (20% speedup on 3060)
  • Optimize for using larger TN,TM > 4. For this probably need to optimize resister usage do lower because right now the kernel does not execute on 8x8 register tiles

flashattention.c's People

Contributors

kilianhae avatar simonguozirui avatar simonguo-cohere avatar

Stargazers

 avatar preferenceforxlh avatar Hongwei Chen avatar  avatar  avatar Lee Hyun Joon  avatar Narain  avatar Piotr Mazurek avatar Paul Hyunbin  Cho avatar  avatar

Watchers

 avatar  avatar

flashattention.c's Issues

about some errors in A100

Hi~I run this project's silly_attn on A100, it occurs an error: too many resources requested for launch, and when I change the d to 32, this error fixed;
but with Br&Bc = 32, d = 64, silly attn only use 28kb shared mem, A100 has 48kb shared mem, how can this error happen?

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.