چرا Self-Attention: پیچیدگی، موازیسازی و طول مسیر
Attention Is All You Need
- Question first: for a sentence of n = 50 tokens and d = 512, which is cheaper per layer, self-attention or a recurrent layer? What about n = 10,000?
- The three desiderata of Section 4: total computational complexity per layer, minimum number of sequential operations, maximum path length between long-range dependencies
- Table 1: Self-Attention O(n^2 · d), O(1), O(1); Recurrent O(n · d^2), O(n), O(n); Convolutional O(k · n · d^2), O(1), O(log_k(n)); Self-Attention (restricted) O(r · n · d), O(1), O(n/r)
- Where n^2 · d comes from: the n x n score matrix, each entry a d-dimensional dot product
- Self-attention is faster than recurrence when n < d, which the paper notes is most often the case for word-piece and byte-pair sentence representations
- The quadratic trade-off: cost grows with the square of sequence length; the paper's proposed remedy is restricted attention over a neighborhood of size r, raising maximum path length to O(n/r)
- Convolutions: a single layer with k < n does not connect all positions; separable convolutions cost O(k · n · d + n · d^2), equal to self-attention plus FFN even when k = n
- Side benefit: interpretability; the appendix figures show different heads learning different tasks, such as resolving which noun a pronoun refers to (Figure 4) and sentence-structure patterns (Figure 5)
معیارهای سهگانه ارزیابی لایهها در مقاله
**معمای مقایسه: لایه خودتوجهی یا بازگشتی؟**
برای جمله کوتاه با \(n = 50\) و \(d = 512\) کدام لایه ارزانتر است؟ با افزایش طول متن به \(n = 10{,}000\) چطور؟
**۱. کل پیچیدگی محاسباتی در هر لایه**
مجموع کل عملیات محاسباتی مورد نیاز در هر لایه (Complexity per Layer).
**۲. قابلیت موازیسازی (حداقل عملیات متوالی)**
تعداد مراحلی که باید حتماً پشت سر هم اجرا شوند و قابل موازیسازی نیستند (Sequential Operations).
**۳. حداکثر طول مسیر (Maximum Path Length)**
بیشترین مسافتی که سیگنال بین هر دو توکن طی میکند؛ کلید یادگیری وابستگیهای بلندمدت.
مقایسه تحلیلی خودتوجهی و لایه بازگشتی
| نوع لایه | پیچیدگی محاسباتی | توالی کوتاه (\(n=50, d=512\)) | توالی بلند (\(n=10000, d=512\)) | عملیات ترتیبی | بیشینه طول مسیر |
| --- | --- | --- | --- | --- | --- |
| Self-Attention | \(O(n^2 \cdot d)\) | \(1.28 \times 10^6\) (۱۰ برابر سبکتر) | \(51.2 \times 10^9\) | \(O(1)\) | \(O(1)\) |
| Recurrent | \(O(n \cdot d^2)\) | \(13.1 \times 10^6\) | \(2.62 \times 10^9\) (۲۰ برابر سبکتر) | \(O(n)\) | \(O(n)\) |
کانولوشن و خودتوجهی محدودشده در جدول ۱
| نوع لایه | پیچیدگی هر لایه | عملیات ترتیبی | حداکثر طول مسیر |
| --- | --- | --- | --- |
| Convolutional | \(O(k \cdot n \cdot d^2)\) | \(O(1)\) | \(O(\log_k n)\) |
| Separable Conv | \(O(k \cdot n \cdot d + n \cdot d^2)\) | \(O(1)\) | \(O(\log_k n)\) |
| Restricted Self-Attention | \(O(r \cdot n \cdot d)\) | \(O(1)\) | \(O(n/r)\) |
جمعبندی: مزایا، محدودیتها و تفسیرپذیری
**کارایی بالا در رژیم عملیاتی**
در ترجمه ماشینی \(n \ll d\) برقرار است (با \(d = 512\)). گامهای ترتیبی \(\mathcal{O}(1)\) امکان پردازش کاملاً موازی ماتریسی روی GPU را فراهم میکنند.
**چالش مقیاسپذیری متنهای طولانی**
پیچیدگی محاسباتی \(\mathcal{O}(n^2 \cdot d)\) برای متنهای بسیار طولانی گلوگاه است؛ ایده Restricted Attention افقی باز برای کارهای آینده بود.
**ارزش افزوده: تفسیرپذیری (Interpretability)**
سرهای توجه تخصصهای زبانی پیدا میکنند؛ از جمله ردیابی ساختار نحوی (Syntax) و اتصال ضمایر به مراجع (Coreference).
بازگشت به دوره