🧮 Attention FLOPs Estimator
Estimate the floating-point operations of a single attention layer and of the whole model from sequence length, head count and hidden size.
📐 計算公式 / 原理
單層 FLOPs ≈ 2S²d (QKᵀ) + 2S²d (AV) = 4S²d;全模型 GFLOPs = 單層 × 層數 / 10⁹
自注意力的兩次矩陣乘各為 S×S×d 規模,乘加各算一次故乘 2,合計 4S²d,隨序列長度呈平方增長——這正是長上下文成本暴漲的原因。線性注意力與稀疏注意力正是為打破這一 S² 項。
📐 計算公式與說明
單層注意力 FLOPs ≈ 2·S²·d(QKᵀ)+ 2·S²·d(注意力加權·V)= 4S²·d,與首框同口徑;全模型 GFLOPs = 4S²·d × 層數 ÷ 10⁹。校驗:S、d、層數均須為正整數,空值、負數或小數時清除舊結果。判據:計算量隨 S 的平方增長,S 翻倍單層計算量約增至 4 倍。
注意力和序列長度平方相關,長序列代價高。