Preliminary
-
A∈Rs2s1 where s2 and s1 are the index set, for example in a 5D tensor A∈Rijklm a possible index set could be s2={i,j} and s1={k,l,m}.
-
Bs2s1=flashs1(Qs2s1,Ks3s1,Vs3s1) is a flash attention operation over s1 index set of tensor set <Q,K,V>, and the resulting index set is the index set of Q s.t. B∈Rs2s1
Attention Operation
For this exercise we will simplify the target index sets to match the most common setup, i.e. Qs2s1∈R[M×d], Ks3s1∈R[N×d], Vs3s1∈R[N×d]
Note: In this doc, whenever we have to denote the exact dimensions instead of index set it will be denoted as .[...×...×…] where × symbol separates across different index sets.
Thus our operation becomes:
Bs2s1=attentions1(Qs2s1,Ks3s1,Vs3s1)B[M×d]=attentiond(Q[M×d],K[N×d],V[N×d])
S[M×N]=Q@KT=Q∗(M×d,N×d,M×N)K∈R[M×N]SrmaxM=NmaxS[M×N]∈RMSrm[m,n]=S[m,n]−Srmaxm∀[m,n]∈[M×N]P[M×N]=softmaxN(Srm[M×N])O[M×d]=P@V=P∗(M×N,N×d,M×d)V∈R[M×d]
Note: Notation abuse -> O⟺B
For detailed Forward pass derivation please refer to my previous blog: FlashAttention Kernel: Forward Pass (MATH)
Backward (mode autodiff) Pass:
Bs2s1⟺B[M×d]=attentiond(Q[M×d],K[N×d],V[N×d])
For a given loss value Os3=ϕ and known dBϕs2s1=dOs3=ϕdBs2s1 We need to find out dQϕs2s1, dKϕs3s1, and dVϕs3s1.
Here we will directly differentiate the core attention operation without adjusting for numerical stability of exponent (we did so in forward pass to just make computation stable). Here we will first derive the core backward operations and then change it to computation, followed by mitigating any source of numerical instability.
dVϕs3s1:
Consider following op: Bs2s1=∑s3Ps2s3⋅Vs3s1=P[M×N]@V[N×d]∈Rs2s1[M×d]
dVϕs3′s1′=∂Vs3′s1′∂Oϕ=s2s1∑∂Bs2s1∂Oϕ∂Vs3′s1′∂Bs2s1=s2s1∑dBs2s1∂Vs3′s1′∂Bs2s1∂Vs3′s1′∂Bs2s1=s3∑Ps2s31(s3s1)=(s3′s1′)=Ps2s3′1s1=s1′s2s1∑dBs2s1∂Vs3′s1′∂Bs2s1=s2s1∑dBs2s1Ps2s3′1s1=s1′=s2∑dBs2s1′Ps2s3′dVϕs3′s1′=s2s1∑dBs2s1∂Vs3′s1′∂Bs2s1=s2∑dBs2s1′Ps2s3′=dBT⋅P
Note: This also provides a crucial propetry of tensor differentiation i.e. for tensor product operation Cs2s3=∑s1As2s1Bs1s3=A⋅B then for a given dCsos2s3 the derivative dAsos2′s1′=∑s3dCsos2′s3Bs1′s3 for a simple matmul i.e. so=ϕ,s3∈R,s2∈R,s1∈R this operation shrinks to simply dA=dC⋅BT. Similarly for dBsos1′s3′=∑s2dCsos2s3′As2s1′ for a simple matrix multiplication this would reduce to dB=dCT⋅A
dPϕs2s3:
From the formula derived previously dPϕs2′s3′=∑s1dBs2′s1Vs3′s1=dB⋅VT
dQKϕs2s3T
Here we have encountered softmax operation as Ps2s3=softmaxs3(Ss2s3=QKs2s3T) from the softmax blog we can
Direct Operation:
Bs2s1=softmaxs1(As2s1)
Here Os3 is the final loss value for which we need to extract the derivatives.
∂As2′s1′∂Os3=Bs2′s1′[dBs3s2′s1′−s1∑dBs3s2′s1Bs2′s1]
∂QKs2′s3′T∂Oϕ=Ps2′s3′[dPϕs2′s3′−s3∑dPϕs2′s3Ps2′s3]
for a simple matmul:
∂Si′j′∂Oϕ=Pi′j′[dPi′j′−dPi′:T∘Pi′:]=P×[dP−BMM(dPi′1j,Pi′j1)]
dQϕs2s1 & dKϕs2s1:
S=QKT and we know dS thus we can directly write the derivatives of both Q and K.
dQ=dS⋅KdK=dST⋅Q
Final Backward Pass Equations:
dB∈R[M×D],{Q,dQ}∈R[M×D],{K,dK}∈R[N×D],{V,dV}∈R[N×D]
dV=dBT⋅PdP=dB⋅VTdSi′j′=Pi′j′[dPi′j′−dPi′:T∘Pi′:]dQ=dS⋅KdK=dST⋅Q
Expansion in dimensions:
Sij=d∑qidkjddVjd=dBT⋅P=i∑dBidPij=i∑dBidLiexp(Sij)dPij=dB⋅VT=d∑dBidVjddSij=Pij[dPij−j∑dPijPij]dQid=dS⋅K=j∑dSijKjddKjd=dST⋅Q=i∑dSijQid
Abstract away D dimension:
In a future blog we will see that this is natural to do computation along D (embedding) dimension as all of the computations are independent of each other in this dimension.
Sij=qi∘kjdVj=i∑dBidLiexp(qi∘kj)dPij=dBi∘VjdSij=Pij[dPij−j∑dPijPij]dQi=dSi∘KjdKj=dSjT∘Qi
j∑dPijPij=j∑(d∑dBidVjd)Pij=j∑d∑dBidVjdPij=d∑j∑dBidVjdPij=d∑dBidj∑VjdPij=d∑dBidBid
Sij=qi∘kjdVj=i∑dBidLiexp(qi∘kj)dPij=dBi∘VjdSij=Pij[dPij−dBi∘Bi]dQi=dSi∘KjdKj=dSjT∘Qi