HomeVideos

MedAI #54: FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness | Tri Dao

Now Playing

MedAI #54: FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness | Tri Dao

Transcript

1196 segments

0:02

hi everyone Welcome to our 54th session

0:06

of the main AI group exchange this week

0:08

we have three doubt from Stanford

0:10

University here with us to present his

0:13

work on Fresh attention fast and memory

0:15

efficient exact extension with IO

0:17

awareness

0:18

three is a PhD student in computer

0:21

science at Stanford co-advised by Chris

0:23

Ray and Stefano Ormond he works at the

0:26

intersection of machine learning and

0:28

systems his research interests include

0:31

sequence models with long-range memory

0:33

as well as structured matrices for

0:36

compact deep learning models his work

0:38

has received the ICM of 2022 outstanding

0:41

paper running up

0:43

thank you tree for joining us today

0:45

before we start could you let us know

0:47

how you want to take questions

0:50

yeah uh thank you see so I'm really

0:53

excited to be here uh so this is a

0:55

fairly informal talk I'm so happy to

0:57

take questions uh during during the talk

1:00

that's that's great

1:02

um and yeah thanks see and nadita too

1:05

for for organizing this I'm really

1:07

excited to be here okay so let's try to

1:10

make this session as interactive as

1:12

possible without further Ado let me help

1:14

me over to three

1:16

yeah

1:17

thanks

1:19

um yeah so we're I'm really excited to

1:21

hear uh talking about our recent work

1:24

um it's called flash attention and we

1:27

try to put all the main ideas in in the

1:29

title so uh what this is going to do is

1:33

um attention we're going to make it one

1:35

faster

1:36

two is going to be Memory efficient and

1:40

three is going to be exact so we don't

1:41

do any approximation and and the trick

1:45

is called i o awareness and I'll go into

1:47

a little bit of details

1:49

um so that although that high level

1:51

takeaway messages are in the title

1:54

all right so uh let me talk a little bit

1:57

more about the high level

1:59

um so what we're going to do is we're

2:02

going to use Iowa aware tiling

2:04

techniques to speed up attention and

2:06

attention is the the heart of

2:07

Transformers which is now the the

2:10

dominating uh dominant machine learning

2:14

architectures in many fields okay so

2:16

what does Iowa mean IO just means in and

2:19

out read some writes so the thing we're

2:21

going to exploit is that on the left I'm

2:23

showing a diagram of GPU memory in SRAM

2:27

and by exploiting SRAM which is way

2:30

faster but way smaller than the GPU

2:32

memory or hbm we're going to make things

2:34

go faster and the trick we're doing that

2:37

as I've shown in the the middle is

2:39

you're going to compute things by block

2:40

and that's called tiling and the out the

2:43

the upshot is on the right we're going

2:45

to make it faster we're going to save a

2:47

lot of memory and we're going to have

2:49

higher quality models

2:51

um so that those are the main main

2:54

takeaways

2:55

so before

2:57

um before I dive into the details so I

2:59

want to talk a little bit of about

3:01

motivation and why this could be

3:03

relevant to you know folks working in in

3:05

medical uh medical domain or medical AI

3:09

so the our motivating

3:12

um

3:13

uh idea is we want to model longer

3:15

sequences and and Chris uh my advisor

3:18

and a bunch of us put out blog post

3:19

that's Linked In the at the bottom here

3:22

about why we we care about long

3:24

sequences and there are three three main

3:26

reasons one is we want to reach new

3:29

capabilities so the anchoring

3:31

application is in natural language

3:33

processing where we want to model uh

3:36

books and plays and instruction manuals

3:38

not just paragraphs

3:40

um and the second reason is we want to

3:43

close the reality Gap and the anchoring

3:45

application here is computer vision

3:46

where we've seen that if you can model

3:48

images at higher resolution you get

3:51

better and more robust insight and high

3:53

resolution uh generally translates to

3:56

longer sequences

3:59

um and the third one which is probably

4:01

the most exciting is you want to open up

4:02

new new areas so things like stamp

4:04

series audio medical imaging video

4:08

things like that they're naturally

4:10

modeled as sequence of up to millions of

4:12

steps so that's why we care about long

4:15

sequences but the problem is that the

4:18

the dominant architecture machine

4:21

learning architecture nowadays the

4:22

Transformers uh they have trouble

4:25

scaling to Long sequences so now go into

4:27

why uh without uh the the problem we're

4:30

going to tackle is that we want to scale

4:32

Transformers to Long sequences

4:34

so that's the high level

4:37

um motivation and I'll go into three

4:40

examples uh in in some medical

4:44

applications why you should care about

4:46

uh Transformers and why should we care

4:49

about Transformer on Long sequences

4:52

the first this is in collaboration with

4:55

Armin at the uh the psychology

4:57

department so we're interested in

4:58

building for example Foundation models

5:00

for brain decoding from fmri

5:02

Readings so

5:05

um surprisingly you can use the same

5:07

Transformer model that has been used in

5:09

national language processing

5:12

um to build to build representations for

5:16

for brain decoding and arminess has a

5:19

great paper on this and uh we want to

5:23

make this faster we want to make this

5:24

more memory efficient so that we can

5:25

model let's say longer time scale when

5:29

you you do fmri decoding

5:32

um so that's one thing we're excited

5:34

about this is ongoing collaboration with

5:35

Armin the second application motivating

5:38

application is uh with Arjun who's a

5:41

future student in Subway and in medical

5:44

school where we want to model high

5:48

resolution MRI images so these are knee

5:50

MRIs and usually one of the tasks to the

5:53

task to care about is detection and

5:55

segmentation and as you see

5:58

um for Radiologists they usually care

6:00

about these tiny regions that if you

6:02

don't you can't Model A long high

6:04

resolution you can the models will tend

6:06

to miss these these regions so the goal

6:09

again is to model high resolution images

6:13

and that's like for for Transformers to

6:16

Long sequences

6:18

and the third application this is in

6:20

collaboration with Gautam

6:22

um uh is on megapixel and gigapixel uh

6:26

pathology which is a very similar

6:29

application that uh for example the the

6:31

talk last week

6:33

um yeah from Richard he also talked

6:35

about megapixel histo pathology so let's

6:38

say the the task here you might want to

6:40

predict tumor versus healthy tissue at

6:43

different image levels or you want to do

6:45

object detection for tumor lesion and

6:48

for for uh pathology images these are

6:51

usually stored at different resolutions

6:53

and you want to model and incorporate

6:55

information from different resolution we

6:58

be it at the style level is it be it at

7:01

the organelles level and things like

7:03

that so you need to be able to deal with

7:06

with a really high resolution images and

7:09

and transform Transformer nowadays

7:11

um they can't quite do this yet the

7:14

bunch of tricks you have to do but again

7:16

we we hope to make Transformer able to

7:19

model long sequences so that they can

7:21

deal with these high resolution images

7:23

in in these applications so those are

7:25

the three motivating applications uh why

7:28

we care about Transformers for for long

7:30

sequences

7:33

um so that's kind of the the motivation

7:36

um and then uh so the talk we have four

7:38

parts I'll talk about some background

7:39

then I'll talk about our our proposal

7:42

flash attention

7:44

um and then I'll I'll go into empirical

7:47

validation and finally a little bit on

7:49

on future directions and yeah feel free

7:51

to ask ask questions throughout the talk

7:53

I'll pass I'll also passed uh

7:55

periodically for uh for questions so uh

7:58

before we go into the background happy

8:00

you know I could pause here and see if

8:02

people have questions

8:07

yeah

8:10

all right so so let's let's get into the

8:12

the background uh

8:14

why Transformers have trouble modeling

8:17

long sequences

8:21

um so just many of you might be aware of

8:23

the Transformer architecture already

8:24

this is just so that we're on the same

8:26

page in terms of of notation so

8:28

Transformer is a machine learning

8:30

architecture where you stack a bunch of

8:32

these blocks or encoder or decoder the

8:35

two types and each block is each block

8:39

consists of two layers the attention

8:41

layer and the MLP layer and we'll focus

8:43

on the attention layer because that's

8:45

usually the bottleneck for uh for long

8:48

sequences

8:49

and attention it looks like this many of

8:52

you my might have seen this so this is

8:54

just so that we're on the same page in

8:55

terms of notation

8:57

um the inputs you would have the query q

9:00

the key K and the value V

9:03

and the steps are that you would take q

9:07

and K and you multiply them together

9:09

optionally you apply some masking uh

9:12

then you do a softmax to normalize

9:14

things and you'd apply a drop out

9:16

optionally and then you multiply that

9:17

with v to get an output o

9:19

and the the the thing that prevents

9:22

attention from scaling to Long sequences

9:24

is that naively attention has compute

9:26

and memory quadratic in sequence length

9:28

uh here's a good thing I didn't know

9:30

with with n

9:32

um and the reason is simple is because

9:34

you have to deal with these big n by n

9:36

matrices

9:38

um so uh you need to you know n Square

9:42

amount of memory to store it you need n

9:44

Square amount of compute to process it

9:46

so that's

9:47

um that's a simple answer and you can

9:50

think of M as let's say a thousand or

9:52

two thousand

9:53

um and then D the Hat Dimension is

9:56

usually around 64 or 128. so and and by

9:59

n really uh

10:01

uh as it was whether quadratic blow up

10:05

happens

10:06

so we want to we want to address the

10:08

address this so we're going to

10:09

understand what are the bottlenecks and

10:11

the bottleneck turns out to be not be uh

10:14

computation the battery is in memory

10:16

reads and writes

10:19

um so what do I mean by that is uh there

10:22

are a bunch of intermediate steps where

10:23

you have to deal with this n by n deal

10:26

with matrices of size n by n

10:29

and so

10:31

um that means a lot of times it's going

10:33

to be spend on just reading and writing

10:37

into memory

10:39

um and that's really slows you down as

10:40

I'll show you with some some profile

10:43

and so naively it requires a lot of

10:45

reads and rights from slow GPU memory

10:47

hbm you know it's just the technology

10:49

what you think of as sgpu memory

10:52

uh and there's a paper from mlc's last

10:55

year that we really like uh the called

10:58

Data movement is all you need where they

10:59

study Transformers and they they found

11:01

out that yeah the the bottleneck is is

11:05

data movement which is just reading and

11:08

writing data is actually not computation

11:10

and that's quite wasteful

11:13

um so we're very much inspired by that

11:15

paper and just as a preview for the

11:17

result so here I'm comparing the against

11:20

the the pi torch attention

11:22

implementation and our flash retention

11:24

implementation and they compute the same

11:26

thing you know we're not changing the

11:28

output and we're not changing the math

11:30

they compute the same thing but it turns

11:32

out Pi torch uh when you compute

11:34

attention most of the time is spend on

11:36

these memory intensive operations

11:38

masking soft mix and Dropout whereas the

11:40

matrix multiplication uh

11:42

counter-intuitively they don't take that

11:44

much time even though matrix

11:45

multiplication takes the most uh

11:48

floating Point operations

11:50

um is the memory reads and writes that

11:52

really approach you and so flash

11:54

attention is a way to compute the same

11:56

thing but with significantly reduced

11:59

memory reads and rights and we can go in

12:02

this case maybe six times faster

12:06

so to to understand how we do that we

12:09

need to understand a little bit about

12:10

how gpus works so this is a very brief

12:13

background on on GPU and here I'm

12:16

showing some diagrams let's say your

12:17

machine has a bunch of gpus

12:20

um and each GPU you can think of as

12:23

having a pool of memory would you know

12:26

if you type Nvidia SMI it shows you 32

12:30

gigs that's the hbm that's the amount of

12:32

GPU memory you have but when you do

12:34

computation uh what happens is that the

12:38

data is loaded the input is loaded from

12:40

the hbm it gets loaded to the compute

12:43

unit and the SRAM which is you can think

12:45

of as analogous to CPU cache it gets

12:48

loaded from hbm to the compute unit

12:51

all the computation happens there and

12:53

then the output gets written back to hbm

12:55

so it turns out this loading and writing

12:58

of

13:00

um of data to HPM actually is the is the

13:04

bottleneck and I'm I'm looking here at

13:06

the bottom uh a blog post from Horus

13:10

this is a part switch developer is an

13:12

excellent blog post on understanding

13:14

what are the bottlenecks for a lot of

13:16

these deep learning operations so it's a

13:18

great buff of um I highly recommend it

13:21

so okay so how do we so we see that

13:23

there is an asymmetry between uh the hbm

13:28

and the SRAM so hbm is usually quite

13:31

fast and it's quite large but SRAM is

13:34

even an order of magnitude faster but

13:36

it's let's say three orders of Mac into

13:38

smaller this is what I'm showing on the

13:40

on the right and these numbers are for

13:41

the a100 gpus but the gpus are similar

13:44

so the questions can we exploit this

13:46

this asymmetry in the the memory

13:48

hierarchy so that we can get speed up

13:51

and the answer is yes with with i o

13:53

awareness that means we want to account

13:55

for different accountable reads and

13:57

rights to different levels of memory so

13:59

we're going to try and reduce the reads

14:01

and rights to GPU hbm

14:04

so that's the main

14:06

objective we're going to reduce reads

14:08

and write

14:09

so that's uh that's it for the

14:10

background I'll pause here for for

14:12

questions

14:13

um either about attention or about you

14:15

know GPU architecture

14:29

so sorry I just have one quick question

14:30

since nobody has it like um

14:33

so when you do the metric multiplication

14:36

operation are you saving it as a

14:37

different variables or you are saving

14:40

just overwriting the same variables

14:42

it seems that you are just overriding

14:43

right

14:45

um yes uh so here we're dealing with the

14:49

an end by n Matrix so yeah you can I

14:52

guess stay from memory if you just write

14:53

to the same location yes okay

14:56

okay

14:58

um in practice uh when you train models

15:00

people don't do that because generally

15:02

for when you do training you need the

15:05

the the to store the intermediate value

15:08

so that you can compute gradient in the

15:10

backward pass so in practice most of the

15:12

attempts people don't write to the same

15:15

location but yeah it's a great idea you

15:17

can write to the same location maybe say

15:19

I don't know 4X memory or something yeah

15:22

right because I would imagine like in

15:24

the pie torch exactly um the pie touch

15:26

operation that you are showing for the

15:27

attention I think they are also not

15:29

really not only like time efficient but

15:31

they are not also like memory efficient

15:33

I don't know if you have any solutions

15:35

since you are just doing the metric of

15:36

multiplication this kind of like um

15:39

inverse functions I don't know if you

15:41

thought about like optimizing also the

15:43

memory at the same time of optimizing

15:46

the time

15:47

yeah so that's that's a great point so

15:49

as we show that you can be clever and

15:51

not you know not materialize these n by

15:54

n Matrix matrices at all

15:57

um so in the end you get something like

15:58

10x memory savings overall

16:01

um so one

16:04

um one thing about reading and writing

16:06

to the same location is that you still

16:07

need to you know in terms of memory yeah

16:09

you do save the total amount of memory

16:11

but in terms of time you still need to

16:13

do the same number of reads and writes

16:16

right so you might not get speed up but

16:18

you might do my save some some memory

16:20

yeah I agree

16:22

okay yeah

16:24

um could you could you explain why the

16:27

Dropout and softness actually takes uh

16:29

much more time than the yeah yeah so

16:32

this is a little bit surprising

16:35

um so turns out the

16:38

um

16:39

there most of the time is actually spent

16:41

on reading and writing into memory so

16:43

every time let's say you start with with

16:44

a uh a you you takes Q times K you get

16:49

the Matrix a and then you apply some

16:50

masking and what really happens is

16:52

actually you're not doing any any math

16:54

what you need what you do is you load

16:56

the value from hbm to the compute unit

16:58

the compute units set some of the values

17:01

to to zero or negative infinity and then

17:04

it writes them back down the the output

17:06

so it's uh uh it has very minimal

17:11

floating Point operations but the amount

17:13

of memory reads and wise is much larger

17:15

than Matrix or at least you know double

17:17

the major amount of memory written right

17:19

when you do matrix multiplication so

17:24

um this is a little bit surprising

17:25

because attention

17:27

um yeah most of the time you think

17:28

things are bottom act by computes if you

17:30

reduce the amount of computation you

17:31

would go faster but for attention it's a

17:33

little bit counterintuitive is that the

17:35

The Tam is taken by memory reads and

17:37

writes just the you know the matrix

17:39

multiplication turns out to be faster

17:41

than these uh masking or Dropout or

17:44

softmax operations

17:46

I see thanks

17:49

I had a good question but for Dropout if

17:53

you're doing random dropper you won't

17:55

need to like load anything from HPM

17:58

right

17:59

uh so you still do need to load things

18:01

from hbm and in the sense that because

18:03

for each GPU uh operations it's called

18:07

kernel the input starts out in HPM you

18:09

need to load it into hbm you set some of

18:12

those to be zero and you write it down

18:14

to hbm

18:15

um

18:17

yeah the architecture works I see yeah

18:21

so if we prevent that from going down to

18:23

hbm before doing the Dropout then that

18:26

can serious but I guess we still need to

18:28

do it for the gradients so yes yeah so

18:31

that's an excellent uh idea actually you

18:35

know people have tried it attention so

18:37

well studied that people try all all

18:39

ways to get all the ways you could think

18:41

of to optimize these things and yeah

18:43

they can combine like hey let's just

18:45

load you know let's say combine stop Max

18:46

and rubber you can load things in do

18:48

softmax and then just apply Dropout

18:50

before you write the the output out uh

18:53

so it is an excellent idea and people

18:56

have done that and they have seen some

18:58

speed up

18:59

um but again generally you need to

19:02

materialize at least one of these n by

19:04

end matrices and um that's still quite a

19:06

bit slower yeah correct but thanks yeah

19:10

so there are more optimized

19:11

implementation here we're comparing

19:12

against pythosh but uh you know the the

19:15

excellent people at Nvidia they they

19:17

have a bunch of different

19:18

implementations

19:20

um and we do compare against them

19:22

um to you know combine some of these

19:24

steps to reduce IELTS so uh from a

19:27

hardware perspective yeah it's very much

19:28

known that yeah you want to do we do

19:30

reads and writes and you can find some

19:32

of these operations

19:33

um but so far people haven't been able

19:35

to combine all of them

19:37

um and and as we show there there's a

19:39

little bit subtlety but but you you can

19:41

do it and and you can get the whole

19:43

thing much faster

19:45

yeah

19:47

yeah great questions

19:52

all right so so I'll go into details of

19:54

how we actually do this and the the one

19:58

trick is actually really simple

20:00

um so before that so uh as I explained

20:03

we want to reduce the memory reads and

20:05

rights to hbm and the answer usually is

20:09

that you want to compute things by

20:11

blocks so that it's small enough that it

20:13

fits into SRAM that you can load from

20:15

hbm to SRAM which is new local

20:17

computation on SRAM

20:20

um and so you you don't need to to

20:23

repeatedly read and write to to hbm but

20:26

there is there are two challenges one is

20:29

that the softmax as defined it needs the

20:33

whole row before you can do softmax by

20:35

by definition uh that's challenge number

20:38

one so if you want to do things by Block

20:40

it's not clear how you would do softmax

20:43

if you want access to one block instead

20:45

of the whole the whole row and Challenge

20:48

number two is that

20:50

um when you train models you usually

20:52

need these intermediate values so that

20:55

you can in the backward pass you can

20:56

compute gradients

20:59

um so if we want to save memory or we

21:02

want to do reads and write if we don't

21:03

save these large attention matrices how

21:06

do we compute the backward pass

21:09

um and we use two well-established

21:10

techniques to address these one is

21:12

called tiling which just means Thailand

21:15

just means doing things by block but you

21:17

need to restructure the the attention

21:19

algorithm a little bit uh to get the the

21:22

uh to get things to load from hbm to

21:24

SRAM and the compute attention kind of

21:25

locally uh it needs a little bit of a

21:28

trick to get the the right answer

21:31

um and the second Technique we use is

21:32

called recomputation so the answer is

21:35

um we don't store the attention Matrix

21:37

from the forward pass we recomputed in

21:39

the backward pass and turns out

21:40

breathing computation is actually faster

21:42

than than storing it and reading it

21:44

again so that's a little bit surprising

21:47

um and I'll go into details of some of

21:50

these techniques

21:51

um so the way we implement it is that we

21:53

have a few scooter kernel for so that we

21:55

have fine grain control of the memory

21:58

reads and rights so we can control where

22:00

to place

22:01

um what and we provide a pi torch

22:05

interface that that people can use

22:07

so let's go into detail of some of these

22:09

techniques

22:10

um the first one is tiling and this is

22:12

you just need a little bit of math this

22:14

is the only math in in the talk

22:17

um the trick is that you have a soft Max

22:20

over let's say a large attention Matrix

22:23

um

22:24

but you can break it up let's say you

22:26

can break it up into two pieces so if

22:28

you want to compute top Max of A1

22:29

concatenate with A2 you can actually

22:31

compute topics of A1 first then computer

22:34

soft Max of A2 and then you can rescale

22:36

them using these scalar Alpha and beta

22:39

and you you can you can work out exactly

22:41

what Alpha and beta are uh they're

22:44

they're the details in the paper but

22:47

they're they're pretty easy to to work

22:49

out so uh softmax you can actually break

22:53

it into smaller softmaxes so that you

22:56

can exploit locality and when you apply

22:59

to attention you're going to have

23:00

softmax with A1 A1 and A2 times the V

23:03

the value Matrix V1 and V2 and you can

23:07

decompose it exactly the same way so

23:09

you're gonna do you get soft Max of A1

23:11

times V1

23:13

so that's a local computation another

23:15

local conversation is you take stock Max

23:17

of 82 times V2 and is rescale the

23:21

ingredients and adding things together

23:22

you get the right answers so

23:24

mathematically is the same answer you're

23:26

just doing the computational let's say a

23:28

different order

23:30

and the nice thing is that you can

23:32

exploit locality because here we don't

23:35

need to load the whole of A1 A2 in we

23:37

just need to load A1 and V1 and then we

23:39

compute whatever local things we we need

23:41

so this is the the only trick that you

23:45

need to know

23:47

um and the algorithm is is relatively

23:49

simple

23:50

um so we're going to load inputs in

23:52

block by block from HB into SRAM so on

23:55

the diagram on the right you can hbm is

23:58

in green and SRAM is in Orange so you

24:01

can think of conceptually we're

24:03

Computing attention block by block and

24:04

for each block we're going to load

24:06

whatever inputs we need into from hbm to

24:09

SRAM we do whatever local computation

24:11

and then we're going to write down the

24:13

output to httpm so we never materialize

24:15

this big n by n Matrix

24:18

um

24:19

um and and with that you you get memory

24:23

saving and you get speed up as as I'll

24:24

show you in some of the benchmarks

24:27

um so Tyler that's the that's the idea

24:29

uh you analytically rewrite softmax a

24:33

little bit

24:34

um so that you can do local computation

24:37

block by block so that's the that's the

24:40

main idea

24:41

uh another quick question so in this

24:44

case the alphas and betas are not

24:48

constants right they depend on A1 and

24:50

attitude yes yes

24:53

um because yes that's right so when you

24:55

load in A1 and A2 you also need to

24:58

compute let's say as one scalar per row

25:02

um so it's depends it's dependent on on

25:05

the data you're right here so then you

25:08

can't do it by the blog because it also

25:10

depends on A1 E2 and it also depends on

25:12

everyone I need to provide

25:15

so Alpha only depends on A1 oh sorry

25:18

sorry yeah yeah no no you're right so

25:20

Alpha depends on both A1 and A2 but it

25:22

did depend on just one scalar they're

25:24

just normalizing uh scalars from uh from

25:28

each block so you can think of uh think

25:31

of it as you know you have a summary

25:33

statistics each block and then based on

25:36

the summary statistics which are really

25:37

small you can combine them yes

25:40

I see so you first read okay so you

25:43

compute some researchistics and okay

25:45

yeah that that makes sense that can be

25:47

easily done when you are Computing the

25:49

A1 and A2 right so yeah

25:53

yeah great great question

25:57

foreign

26:00

has been known in a while uh uh you know

26:04

tiling in general computing by block is

26:06

kind of the Cornerstone of of databases

26:08

Graphics

26:10

um numerical linear algebra and this

26:12

trick of decomposing softmax uh the

26:15

first time we saw it was in 2018 so

26:18

um so these are well established

26:19

techniques that were that we're using

26:21

but surprisingly these simple techniques

26:23

will allow us to go faster and more

26:25

memory efficient

26:27

okay so that's the first technique

26:28

tiling the second technique is

26:30

recomputation it conceptually is also

26:32

very simple so you don't store the the

26:35

uh attention Matrix from the

26:37

um from the forward pass in the back

26:39

request you recompute it and if you

26:40

already have these normalizing

26:41

normalization factors Alpha and beta you

26:44

can actually recompute it pretty quickly

26:46

since the inputs you need are going to

26:49

be loaded from SRAM anyway so in some

26:51

sense uh computation is is almost free

26:54

because you're already doing the reads

26:57

and write to load inputs in

27:00

um and again we don't store this big n

27:02

by a matrix everything is loaded in and

27:04

then compute on SRAM

27:06

um

27:06

so the the memory requirement is much

27:08

smaller we're much fewer reads but we do

27:11

encourage more flaps and here I'm

27:13

comparing uh the standard implementation

27:15

with flash attention

27:17

uh and this table and first of all in

27:20

terms of flops we actually incur more

27:21

flops it's about 12 more flops because

27:24

we recompute things in the backward pass

27:26

but in terms of hbm reads and writes so

27:29

so memory reads and writes we're about

27:32

nine times smaller and in terms of

27:34

runtime we're about in this case like

27:36

six times smaller so even though we

27:40

recompute things even though we in term

27:41

of lab surprisingly it actually speeds

27:44

uh things up We Begin Again the the

27:47

observation is that is attention is not

27:50

bottlenecked by uh computation is

27:53

bottlenecked by reads and rights so you

27:55

want to reduce it right even if you have

27:57

to incur more computation

28:02

so that's the the second technique

28:05

um and so those are you know the two

28:08

techniques that we use these are well

28:09

established

28:11

um but surprisingly

28:13

um attention's been out for five years

28:14

is one of the most studied uh problem in

28:18

machine learning is uh but surprisingly

28:20

no one has uh has done this

28:23

um so it's pretty cool you can put it

28:24

put together these these simple

28:26

techniques with a little bit of a Twist

28:28

and then you can you can make attention

28:30

go faster and more memory efficient and

28:31

even compared to the best uh the the

28:33

strongest Baseline we we compare against

28:36

which I'll go into yeah so I'll pass

28:38

here

28:39

um for for questions

28:43

another question I had was how do you

28:45

make sure that this Affinity From the

28:47

Block to the SRAM is maintained in like

28:50

the python implementation for your

28:52

flashlight yeah yeah so that's the

28:55

difficult part so we had to implement

28:57

this in Cuda so that we can decide we

29:00

can control when to load what things

29:03

um so that you know that actually took

29:06

significant obviously two students

29:09

writing Cuda for three months or

29:11

something but uh yeah but we're excited

29:14

about a couple of things one is that uh

29:17

we're collaborating with with compiler

29:19

researchers so that we can automate a

29:21

bunch of these things so the compilers

29:22

can do it too

29:24

um number two uh some people you know

29:27

some some other researchers really like

29:28

this so they they independently

29:30

re-implement it in in Jax or in Triton

29:33

Triton is a python-like high-level

29:36

language so it's not as painful to to

29:38

write

29:39

um

29:40

um so we're we're excited about that

29:42

hopefully this will be accessible to

29:44

researchers if you just want to use it

29:46

then we have a pi switch interface you

29:48

just call it and it's going to run

29:50

um if you want to Tinker with it then

29:52

some of these higher level uh

29:54

implementation in highly level languages

29:57

uh could be pretty useful

29:59

awesome yeah that's great thanks

30:04

okay so those are the techniques we use

30:07

to speed up attention again the goal is

30:09

so that we can make a lab Transformer to

30:12

model long sequences and and now we're

30:15

gonna uh yeah I'll show you some some

30:17

empirical validations on how this this

30:21

helps us with that goal

30:23

so we're going to Benchmark the

30:25

attention module we're going to show

30:26

that it is faster uh

30:29

even when you put in an end-to-end model

30:31

so you get faster end-to-end model

30:34

training or Transformers and again we're

30:35

not changing the model at all we're

30:36

competing the same thing which is faster

30:38

and the second thing is that we're going

30:41

to save a lot of memory so it allows to

30:44

model longer sequences and that leads to

30:46

higher quality Transformers

30:50

so first we when we brand Benchmark just

30:53

the attention module we see that in

30:56

terms of speed is about 2 to 4X speed up

30:58

compared to the standard implementation

31:01

from python hockey phase or whatever you

31:03

people commonly use

31:06

um so this is we have a chart on a100

31:08

where we get 2 to 4X speed up depending

31:10

on whether you have a masking or drop

31:13

out

31:14

um and in the paper we have more

31:16

comparisons with approximate attention

31:18

and sparse attention and so on we also

31:21

Benchmark and uh Benchmark on a bunch of

31:23

different gpus I'm not showing here all

31:25

the charts are on our GitHub repo yeah

31:28

generally you can we observe about two

31:31

to four x speed up uh of attention and

31:33

again attention is a very well studied

31:35

problem and surprisingly you can you can

31:38

get substantial speed up

31:40

um

31:41

uh even though tons of engineering

31:44

efforts has been poured into improving

31:46

uh attention implementation

31:48

so that says uh just a tension module

31:50

you get two to four x speed up and when

31:52

you uh when you measure memory by usage

31:56

which is the total memory you need uh

31:58

the nice thing about Flash attention is

32:00

memory is linear in sequence length it's

32:02

not quadratic

32:04

um so depending on sequence length

32:07

um where you can see either 10x or 20x

32:10

memory savings uh for the attention

32:12

layer loan so this allows to scale up to

32:15

64 sequence thing of 64k on on one a100

32:19

GPU and just for contacts uh some of the

32:22

largest Transformer models have context

32:24

line of like 2K just because of this

32:27

quadratic block so if you have memory

32:29

linear in sequence Lanes you can scale

32:30

to much longer sequences

32:35

um all right so now we're going to plug

32:37

flash attention into a model and we're

32:40

going to train end to end and we'll see

32:41

that it leads to faster end-to-end

32:43

training

32:44

um

32:45

so the first one uh we want to compare

32:49

is is bird large it's extremely commonly

32:52

used model

32:54

um and The Benchmark or the yeah The

32:56

Benchmark we're going to use is ammo

32:58

perf which is a highly optimized

33:00

standard Benchmark for training speed

33:03

um so uh this is a comparison where we

33:08

all the models starts from the same

33:10

checkpoint they train on the exact same

33:12

data same validation data and you just

33:15

measure how long it takes to reach a

33:16

certain accuracy

33:18

um so everything else the same the model

33:20

is the same just the different

33:21

implementations different algorithms

33:24

um how fast do they run and uh if you

33:27

use a commonly used hugging phase bird

33:30

um

33:31

then this takes like 55 minutes

33:34

um if you use the ml perf uh

33:37

implementation from Nvidia which is

33:39

which was the the world record

33:42

um

33:42

when we submitted the paper

33:45

um you know this is extremely optimized

33:47

teams of Engineers have worked on this

33:49

so they they're like two 2.5 x faster

33:53

than than hugging face uh but

33:57

surprisingly flash attention can be even

33:59

faster than than

34:01

um the previous ml perf record by 15 so

34:05

it was really excited uh exciting for us

34:07

so you know we're also 2x 2.2x faster

34:11

than hockey Facebook so if you're

34:12

training board and you're using hacking

34:14

face implementation you're leaving

34:16

around 2x performance on on the table

34:22

um

34:22

so the second model we can compare to is

34:26

gpd2 and we're training gpd2 small and

34:30

medium from scratch on this commonly

34:32

used data set open web text and again

34:35

again we compare against hugging phase

34:38

which is very commonly used and we

34:40

compare against the strongest Baseline

34:41

we could find which is Megatron LM which

34:43

again an implementation from Nvidia

34:46

that's been used to train some of the

34:49

largest language models

34:51

um and we see that we get the same

34:54

quality we in terms of training time in

34:58

terms of training time we're about three

35:00

or three point five x faster than Huggy

35:02

face

35:03

um so again if you're using hanging face

35:06

you're leaving about two 3x on on the

35:08

table I know the models are the same

35:10

we're not changing the model we're only

35:11

changing the the algorithm and the

35:13

implementation

35:16

so the upshot is you get faster training

35:19

for a bunch of these Transformer based

35:20

models

35:23

um

35:24

policy for for questions

35:30

so do you start analyzing also the

35:34

attention visualization for this model

35:36

just to see like if there is any

35:37

difference in the attention

35:40

output or I know that it is more or less

35:43

like mathematically it's a exactly the

35:45

same but I don't know if you have

35:48

um analyzed like how that engine output

35:50

looks like in the normal model which is

35:52

your flash attention

35:54

yeah so we haven't visualized things we

35:57

did some spot check where the outputs

35:58

are the same or up to numerical

36:00

Precision like yeah one e minus four or

36:04

something so the upwards are the same

36:06

yeah so I don't expect the attention

36:09

patterns to change

36:11

um if you want to change the attention

36:13

pattern then there's some other methods

36:15

where you say you know I want to add

36:17

this to the attention so that it biases

36:19

towards like local features or Global

36:21

features or something so we haven't

36:23

explored that yeah but that's uh that

36:25

could be interesting

36:26

okay so you didn't have any kind of like

36:29

linear oppression performance on the

36:30

attention and just to see like if there

36:32

is any change in attention so you just

36:33

analyze it's more like a training speed

36:36

wise right

36:37

yeah training speed wise

36:39

um the the training curve look identical

36:42

so they look like they lie on top of

36:44

each other again it's because we're not

36:45

changing the model right

36:54

all right so here we can change the

36:56

model and we can we can train uh

36:59

Transformers on longer sequences and

37:01

that generally leads to higher quality

37:03

so here's an example again gpt2 but we

37:06

increase the context length and usually

37:09

when you increase the context length it

37:11

goes slower

37:13

but generally it gets it gives you a

37:15

better model and this is measured by

37:18

perplexity where lower is is better and

37:20

surprisingly when we increase the

37:22

context length by 4K uh we get we're

37:25

still faster than Megatron and LM at 1K

37:28

and we achieve lower complexity

37:32

so this is this is really exciting and

37:35

in fact a bunch of you know a few

37:36

companies are looking to use Flash

37:38

attention to train uh language models

37:40

with longer context up to 8K

37:44

um you know so this stuff actually works

37:47

yeah so one longer context it leads to

37:51

better language models the second one

37:54

let's say is a classification model this

37:56

is a classification task where you have

37:58

a bunch of long documents these are

38:00

either physician notes uh that's mimic

38:03

and then the second one is a legal

38:05

reasoning task and they could the

38:08

sequences could be pretty long up to 10

38:10

10K and when we take a pre-trained model

38:13

and we we extend this context and and we

38:16

fine-tune on this classification task we

38:18

see that we immediately get some

38:20

improvement uh inequality by just simply

38:24

using longer longer contexts so yeah

38:28

this this makes sense

38:31

and so one one synthetic task that's

38:34

that's pretty cool is called path X this

38:37

is more of a benchmark test to see if

38:38

Transformers you know how how much you

38:40

can push transformer into the sequence

38:42

length and the task is looks like this

38:44

image where there are two dots and a

38:46

bunch of lines and you're supposed to

38:47

say whether the two lines are the two

38:49

dots are connected and in this case it

38:51

is not connected it's a classification

38:53

task uh so it's more of a benchmark

38:56

synthetic tasks you're not supposed to

38:59

use patches so you actually need to

39:01

process one pixel at a time your

39:04

sequence length is either 16k or 64k and

39:07

in the past people have tried and

39:10

um basically no Transformer can get

39:13

better than chance of a better Enchanted

39:15

performance either because they run out

39:18

of memory they too slow to train or

39:21

um they do some approximation and they

39:23

don't they you know it can't pick up

39:26

these signals at all and so what's

39:27

really exciting is Flash intention use

39:29

the first Transformer that could solve

39:31

better these tasks better than Jazz so

39:33

it should go up to sequence length 64k

39:35

or

39:36

um yeah 16k or 64k

39:39

so that's something we're pretty excited

39:40

about

39:42

um yeah policy for for questions before

39:44

going to uh future directions

39:51

what's the difference between flash

39:53

attention and blog Sports flash

39:55

attention here

39:57

yeah so I I didn't mention those um

39:59

since I have flash attention you can

40:00

also impose sparsity which is you know

40:03

so far I've talked about only doing

40:04

exact attention we're not approximately

40:06

I'm approximating anything for

40:10

um there's been a long line of work on

40:12

on attention approximation in one such

40:14

way to do it is impose some sparsity so

40:17

you if you similarly impose some

40:18

sparsity flash attention is again

40:21

compatible with that and that allows you

40:23

to uh reduce the computation

40:27

by exploiting sparsity at the cost of

40:30

some approximation

40:32

um so for the for the largest task path

40:34

256 where the sequence thing is 64k uh

40:37

faster tension would run but it would

40:39

take like a month to to train but block

40:41

sparse if you impose some sparsity it

40:44

finishes training in like three days or

40:46

something

40:47

um

40:48

so it's one way you can push the the

40:51

sequence thing even even longer by using

40:54

some approximation but for the most part

40:55

flash attention

40:57

um that it just does exact attention is

40:59

not approximating anything yeah I see

41:06

all right so let's go into some future

41:07

directions that we're pretty excited

41:09

about the first is just getting it into

41:12

more people's hand

41:14

um so the pi support developers are

41:16

integrating this into pytor

41:19

torch.nn.transformer uh the hugging face

41:21

folks are integrating into their models

41:24

as well and independently

41:26

um other researchers they were excited

41:29

about these ideas so they re-implemented

41:31

it in in Jax uh the openai folks

41:34

re-implemented in in Triton which is

41:36

this python-like language that's a

41:39

little bit higher level than 10 Cuda so

41:40

that's a little bit more friendly for

41:42

for tinkering

41:44

and second one there's some technical

41:46

work

41:47

um you see that the ideas are actually

41:48

simple so maybe compilers can support me

41:52

automatically do these things and so far

41:55

right now they are not able to because

41:57

they need a little bit of math rewrite

42:00

um but uh we're working we're

42:02

collaborating with with compiler

42:04

researchers so that some of these

42:06

techniques could be done automatically

42:08

and maybe in the future yeah your your

42:11

model will just get faster more memory

42:13

efficient automatically

42:15

uh and the third one is a high level uh

42:18

point which is we want to bring Hardware

42:20

we're thinking into other deep learning

42:23

operations Beyond just just attention

42:27

and we believe this is a being a big and

42:30

important idea and in fact it came from

42:32

things like databases where for decades

42:34

people have been trying to reduce the i

42:37

o and spend hugely successful

42:41

so the more AI question is what can we

42:44

do with longer sequences

42:46

um you know we're we're really happy

42:48

that several companies are looking in to

42:50

use Flash attention to train language

42:52

models with 8K context Lane which is

42:54

about four times longer than the the the

42:57

the largest models right now gp3 gp3 can

43:00

model of context have context thing

43:02

about 2k so people are excited about

43:04

pushing this further to get better

43:07

models another thing as I mentioned in

43:09

the beginning is we want to model highs

43:11

resolution images be it from MRI images

43:14

or pathology images maybe High high-res

43:18

images will give us a better insights or

43:20

better models

43:21

um so those those are the things we're

43:23

excited about

43:25

um so in summary flash attention it is

43:28

fast it is memory efficient and it is

43:31

exact so it doesn't do any approximation

43:33

to attention and the two key ideas are

43:37

tiling and recomputation and the upshot

43:40

is you get faster some model training or

43:41

you get better models with longer

43:43

sequences and with that uh General I'd

43:47

like to conclude so thanks so much for

43:50

for your attention and our code is is

43:52

open source um if you will flash

43:54

attention or you go to GitHub we have

43:56

links to everything

43:58

um and yeah we're

44:00

um we're excited to see

44:01

how modeling last sequences with flash

44:05

attention what what kind of new

44:06

capabilities is going to give us yeah

44:09

thanks for your attention

44:12

all right thanks Chi for the amazing

44:15

talk let's all give him a round of

44:17

virtual Applause

44:19

and is there any questions from the

44:22

audience

44:23

yeah have to to take questions

44:26

um as well yeah

44:28

when you mentioned uh high-res images

44:31

what sort of Matrix are you thinking or

44:33

like bit depth to

44:36

um so one application more collaborating

44:39

with real term is these pathology images

44:41

uh where they're either megapixel or

44:44

gigapixel so the that's like I think the

44:48

largest one they have is 50K by 50k with

44:51

a hundred channels and that is generally

44:53

just way too big to do anything with and

44:55

right now people just cut them into

44:57

patches and

44:58

and process one patch at a time so

45:02

um you know maybe we can do something

45:04

there

45:05

um

45:06

let's see if I can find these

45:10

um yeah

45:12

um for MRI I think the the resolutions

45:14

are like fifth uh 5 12 to 512 something

45:18

like that yeah that's about right even

45:21

256 occurs sometimes

45:23

yeah

45:24

um so again that's still larger than

45:26

what people you know current models can

45:28

support

45:30

foreign

45:38

what's the longest sequence lens you

45:41

have experimented with

45:43

um using flash attention

45:45

yeah so the longest we've tried is uh

45:48

either 16k or 64k and um

45:53

I think that corresponds to images of

45:56

like

45:57

two 256 if you do it by pixel if you do

46:00

patching like vit then

46:03

um you know that allows you that could

46:04

correspond to high resolution images

46:07

yeah so similar thing is about 16k to

46:10

64k which again is quite a bit longer

46:13

than the most common Transformers

46:16

yeah

46:18

um yeah I guess for the histopathology

46:21

images it can be a thousand hundred

46:24

sorry tens of thousands patches or yeah

46:27

even more than that so yeah yes I can

46:29

see that flash attention could be a very

46:31

good um

46:33

could be very useful here

46:36

yeah I guess like if you can do 64k then

46:40

yeah you can probably like you just like

46:43

take a square root of 64k and those many

46:47

patches it's still a huge context lens

46:49

for uh pathology which is for sure and

46:52

probably can work on whole MRI which is

46:54

completely that right

46:56

yeah hopefully hopefully we'll get there

46:58

this is in active research and if you're

47:01

interested please please talk to us uh

47:03

yeah we we this is an area we're really

47:06

excited about so

47:08

um yeah we'd love to talk to you

47:09

[Music]

47:15

is there any other questions

47:23

okay thank you so much I think yeah

47:26

let's think three again for the talk and

47:29

thank you everyone for joining us today

47:30

we will upload the recording of this

47:33

talk to our YouTube channel later today

47:35

and if you have follow questions feel

47:37

free to reach out to us on Twitter

47:39

directly

47:41

thank you thank you Trey thanks for

47:43

giving doctor bye

Interactive Summary

This presentation introduces 'FlashAttention', a novel approach to make the attention mechanism in Transformers faster and more memory-efficient while maintaining exact results. The speaker explains that the primary bottleneck in standard Transformer models is not computation, but the frequent data movement (reads and writes) between the slow GPU High Bandwidth Memory (HBM) and the fast on-chip SRAM. By utilizing two key techniques—IO-aware tiling and recomputation during the backward pass—FlashAttention minimizes these memory bottlenecks. This allows for training models with significantly longer sequence lengths (up to 64k), which enables applications like high-resolution medical imaging and complex long-document analysis, while also providing substantial speed-ups for standard training tasks.

Suggested questions

4 ready-made prompts