In words: B sequences of T tokens, C wide. The two projections cost 8BTC2. The two attention matmuls, the scores and the weights times the values, cost 4BT2C.
Each attention matmul is 2BHT2d, and Hd=C, so H drops out again.
At C=768, T=1024 and B=1, that's 4.83 GFLOPs in the projections plus 3.22 in attention, 8.05 GFLOPs in all. PyTorch's FLOP counter (torch.utils.flop_counter.FlopCounterMode) agrees on the reference solution to the last FLOP: 8,053,063,680.
The backward pass costs twice the forward pass. So forward plus backward is three times the forward pass alone.
In the editor, write attn_flops(B, T, C) from the formula. The first check is the 8,053,063,680 above.
Pick an answer above to see the rest
Instructions
1
Write attn_flops(B, T, C): the forward FLOPs of one layer, with a multiply-add counted as 2.
2
Add the two projections, qkv and proj, and the two attention matmuls, the scores and the weights times v.