Training Data, Batching, Hardware, and the Learning-Rate Schedule
Attention Is All You Need
- Prediction prompt: why might a learning rate that starts at zero, climbs, and then decays help a freshly initialized deep attention model?
- Data (Section 5.1): WMT 2014 English-German, about 4.5 million sentence pairs, byte-pair encoding with a shared source-target vocabulary of about 37000 tokens; WMT 2014 English-French, 36M sentences, 32000 word-piece vocabulary
- Batching by approximate sequence length; each batch has approximately 25000 source tokens and 25000 target tokens
- Hardware and schedule (Section 5.2): one machine with 8 NVIDIA P100 GPUs; base model about 0.4 seconds per step, 100,000 steps or 12 hours; big model 1.0 seconds per step, 300,000 steps (3.5 days)
- Optimizer (Section 5.3): Adam with beta_1 = 0.9, beta_2 = 0.98, epsilon = 10^-9
- Equation (3): lrate = d_model^-0.5 · min(step_num^-0.5, step_num · warmup_steps^-1.5), with warmup_steps = 4000
- Reading the formula: linear increase for the first 4000 steps, then decay proportional to the inverse square root of the step number
- Hand computation of lrate at steps 1000, 4000, and 16000 for d_model = 512
دادههای آموزش و استراتژی بچبندی
**دادههای آموزش و واژگان (Training Data)**
• **WMT 2014 EN-DE:** حدود \(4.5\) میلیون جفتجمله با BPE و واژگان مشترک \(37{,}000\) توکن
• **WMT 2014 EN-FR:** حدود \(36\) میلیون جفتجمله با WordPiece و \(32{,}000\) توکن
**اشتباه رایج: اندازه بچ بر مبنای جمله**
• **فرض اشتباه:** تثبیت تعداد جملات در هر بچ (مانند \(64\) یا \(128\) جمله)
• **چالش:** تفاوت شدید در طول جملات \(\leftarrow\) نوسان شدید حجم محاسبات و سرریز حافظه GPU
**نوآوری مقاله: دستهبندی بر مبنای توکن**
• **مرتبسازی:** دستهبندی جفتجملات بر اساس طول تقریبی
• **ظرفیت ثابت توکن:** حدود \(25{,}000\) توکن مبدا و \(25{,}000\) توکن مقصد در هر بچ
• **تعداد متغیر جملات:** \(500\) جمله کوتاه یا \(200\) جمله بلند \(\leftarrow\) بار حافظه کاملاً یکنواخت
سختافزار، زمان آموزش و بهینهساز Adam
**سختافزار (Hardware)**
یک سرور منفرد مجهز به ۸ کارت گرافیک NVIDIA P100
**مدل پایه (Base Model)**
\(d_{\text{model}} = 512\) | تعداد گام: \(100{,}000\) (سرعت: \(0.4\text{ s/step}\)) | کل زمان: ۱۲ ساعت
**مدل بزرگ (Big Model)**
\(d_{\text{model}} = 1024\) | تعداد گام: \(300{,}000\) (سرعت: \(1.0\text{ s/step}\)) | کل زمان: ۳.۵ روز
**هایپرپارامترهای بهینهساز Adam**
\(\beta_1 = 0.9\) | \(\beta_2 = 0.98\) (برخلاف پیشفرض مرسوم \(0.999\)) | \(\epsilon = 10^{-9}\)
زمانبندی نرخ یادگیری و رفتار تابع کمینه
```svg
<svg viewBox="0 0 800 450" xmlns="http://www.w3.org/2000/svg"><defs><marker id="arrow" viewBox="0 0 10 10" refX="6" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path d="M 0 1 L 10 5 L 0 9 z" class="tm-arrow" /></marker></defs><rect x="40" y="15" width="720" height="80" rx="10" class="tm-surface" /><text x="400" y="42" class="tm-text" font-size="15" font-weight="bold" text-anchor="middle" direction="rtl">رابطه شماره ۳: فرمول زمانبندی نرخ یادگیری در ترنسفورمر</text><text x="400" y="72" class="tm-text2" font-size="14" text-anchor="middle">lrate = d_model^(-0.5) × min( step_num^(-0.5) , step_num × warmup_steps^(-1.5) )</text><line x1="90" y1="370" x2="740" y2="370" class="tm-line" marker-end="url(#arrow)" stroke-width="1.5" /><line x1="90" y1="370" x2="90" y2="120" class="tm-line" marker-end="url(#arrow)" stroke-width="1.5" /><text x="735" y="400" class="tm-text2" font-size="13" text-anchor="end" direction="rtl">شماره گام (step_num)</text><text x="85" y="115" class="tm-text2" font-size="13" text-anchor="start" direction="rtl">نرخ یادگیری (lrate)</text><text x="90" y="390" class="tm-muted" font-size="12" text-anchor="middle">0</text><line x1="310" y1="140" x2="310" y2="370" class="tm-line" stroke-dasharray="4,4" stroke-width="1" /><text x="310" y="392" class="tm-text-accent" font-size="13" font-weight="bold" text-anchor="middle">4000 (warmup_steps)</text><text x="620" y="390" class="tm-muted" font-size="12" text-anchor="middle">16000</text><line x1="90" y1="370" x2="310" y2="170" class="tm-line-accent" stroke-width="3.5" /><circle cx="310" cy="170" r="5" class="tm-accent" /><text x="310" y="150" class="tm-text-accent" font-size="13" font-weight="bold" text-anchor="middle" direction="rtl">نقطه اوج در گام ۴۰۰۰</text><rect x="110" y="180" width="180" height="95" rx="8" class="tm-soft" /><text x="200" y="205" class="tm-text-accent" font-size="13" font-weight="bold" text-anchor="middle" direction="rtl">فاز ۱: وارمآپ خطی</text><text x="200" y="227" class="tm-text2" font-size="12" text-anchor="middle">step_num < 4000</text><text x="200" y="246" class="tm-text2" font-size="11" text-anchor="middle" direction="rtl">شاخه فعال: عبارت ضربی خطی</text><text x="200" y="263" class="tm-text-green" font-size="11" text-anchor="middle" direction="rtl">افزایش آرام با شیب ثابت</text><path d="M 310 170 C 390 220, 500 270, 710 305" class="tm-line-accent" stroke-width="3.5" fill="none" /><rect x="460" y="180" width="215" height="95" rx="8" class="tm-soft" /><text x="567" y="205" class="tm-text-accent" font-size="13" font-weight="bold" text-anchor="middle" direction="rtl">فاز ۲: افت تدریجی (Decay)</text><text x="567" y="227" class="tm-text2" font-size="12" text-anchor="middle">step_num > 4000</text><text x="567" y="246" class="tm-text2" font-size="11" text-anchor="middle" direction="rtl">شاخه فعال: step_num^(-0.5)</text><text x="567" y="263" class="tm-text-green" font-size="11" text-anchor="middle" direction="rtl">کاهش ملایم با جذر معکوس گامها</text></svg>
```
محاسبه گامبهگام نرخ یادگیری در گامهای مختلف
فرمول نرخ یادگیری ترنسفورمر: \( \mathrm{lrate} = d_{\mathrm{model}}^{-0.5} \cdot \min(\mathrm{step}^{-0.5},\, \mathrm{step} \cdot \mathrm{warmup}^{-1.5}) \) با \( d_{\mathrm{model}} = 512 \) و \( \mathrm{warmup} = 4000 \)
**ضرایب پایه ثابت**
\[ d_{\mathrm{model}}^{-0.5} = \frac{1}{\sqrt{512}} \approx 0.0442, \quad \mathrm{warmup}^{-1.5} = \frac{1}{4000 \sqrt{4000}} \approx 3.95 \times 10^{-6} \]
محاسبه ضرایب ثابت مستقل از گام
**گام ۱۰۰۰ (فاز Warmup)**
\[ \min(1000^{-0.5},\, 1000 \times 3.95 \times 10^{-6}) = \min(0.0316,\, 0.0040) = 0.0040 \implies \mathrm{lrate} \approx 0.0442 \times 0.0040 \approx 0.000175 \]
شاخه خطی (وارمآپ) کوچکتر و فعال است
**گام ۴۰۰۰ (نقطه اوج Peak)**
\[ \min(4000^{-0.5},\, 4000 \times 4000^{-1.5}) = 4000^{-0.5} \approx 0.0158 \implies \mathrm{lrate} \approx 0.0442 \times 0.0158 \approx 0.00070 \]
هر دو شاخه برابرند؛ بیشینه نرخ یادگیری
**گام ۱۶۰۰۰ (فاز Decay)**
\[ \min(16000^{-0.5},\, 16000 \times 3.95 \times 10^{-6}) = \min(0.0079,\, 0.0632) = 0.0079 \implies \mathrm{lrate} \approx 0.0442 \times 0.0079 \approx 0.00035 \]
شاخه معکوس جذر فعال است (نصف اوج)
Back to course