ٹیکنیکل گائیڈ
SwiGLU اور گیٹڈ ایکٹیویشنز
SwiGLU is a gated activation used inside the feed-forward layers of transformers: the input is projected twice, one copy passes through the Swish function, and the two copies are multiplied element by element.
اس گائیڈ کو سنیں۔اے آئی نے بیان کیا۔ · ~7 min
اس صفحہ پر8 min read
جائزہ
Noam Shazeer's 2020 paper showed it lowered perplexity at equal compute, and PaLM and LLaMA adopted it, making it a standard part of large language model design.
اہم نکات
- SwiGLU multiplies Swish(xW) by a second projection xV element by element, then projects back down with a third matrix.
- Noam Shazeer introduced SwiGLU and related GLU variants in the February 2020 paper GLU Variants Improve Transformer.
- To keep parameters equal, gated feed-forward layers shrink the hidden width to two thirds of the usual 4d.
- In T5-base tests, SwiGLU lowered held-out log-perplexity from 1.677 to 1.636 compared with ReLU at matched compute.
- PaLM in 2022 and LLaMA in 2023 both use SwiGLU in their feed-forward layers.
- The gated linear unit dates to Dauphin et al.'s 2016 gated convolutional language models from Facebook AI Research.
گہرا غوطہ
An activation function is the nonlinear step inside a neural network that lets it model more than straight lines. In a transformer, the most obvious place it appears is the feed-forward network (FFN), the small two-layer network applied to every token after the attention step. The original 2017 transformer used ReLU there, which keeps positive numbers and zeroes out negatives. A gated activation works differently: it computes two separate linear projections of the same input and multiplies them element by element, so one projection acts as a gate that scales the other up or down for each feature. SwiGLU is the gated variant in which the gate passes through the Swish function. It replaces the first layer and the activation of the FFN, not the attention mechanism.
Step by step, a standard ReLU FFN takes a token vector x of width d, multiplies it by a matrix W1 to get a wider hidden vector, applies ReLU, and multiplies by W2 to return to width d. A SwiGLU FFN uses three matrices. It computes xW and xV, two hidden vectors of the same size. It applies Swish to the first one; Swish(z) equals z times sigmoid(z), a smooth curve that behaves like ReLU for large positive values but lets small negative values through and dips slightly below zero. It multiplies Swish(xW) by xV element by element, and only then projects back with W2. The result is that each hidden feature is the product of a content signal and a learned, input-dependent gate.
The idea has three ancestors. Yann Dauphin and colleagues at Facebook AI Research introduced the gated linear unit (GLU) in December 2016 in "Language Modeling with Gated Convolutional Networks". Their GLU multiplies a linear projection by a sigmoid of a second projection, and they argued that the ungated linear path eases the vanishing-gradient problem in deep networks while keeping nonlinear capacity. Prajit Ramachandran, Barret Zoph and Quoc Le at Google Brain found Swish in October 2017 through an automated search over activation functions, reporting top-1 ImageNet gains of 0.9% for Mobile NASNet-A and 0.6% for Inception-ResNet-v2 when replacing ReLU. Noam Shazeer then combined the two in "GLU Variants Improve Transformer", posted in February 2020, which defined ReGLU, GEGLU and SwiGLU and tested them in the T5 model.
Shazeer's experiment was deliberately fair. Because gated layers have three weight matrices rather than two, he shrank the hidden width by a factor of two thirds, from 3,072 to 2,048 in T5-base, so every variant had the same parameter count and operation count. After 524,288 pre-training steps on the C4 dataset, held-out log-perplexity was 1.677 for ReLU, 1.679 for GELU, 1.633 for GEGLU and 1.636 for SwiGLU. On fine-tuning, the GLUE average rose from 83.80 with ReLU to 84.36 with SwiGLU, and the SuperGLUE average from 72.76 to 74.56. The paper itself offers no theory for why the variants work, and its author attributes their success, tongue in cheek, to divine benevolence.
Adoption followed in the largest models. Google's PaLM paper (April 2022) listed SwiGLU among its architecture choices, citing Shazeer's quality gains over ReLU, GELU and plain Swish. Meta's LLaMA paper (February 2023) adopted SwiGLU, crediting PaLM for the choice, and set the FFN hidden size to two thirds of 4d, where d is the model width. A 2021 study by Sharan Narang and colleagues, "Do Transformer Modifications Transfer Across Implementations and Applications?", is useful context: it found that most proposed transformer changes did not improve results when reimplemented, yet SwiGLU and GeGLU improved pre-training, fine-tuning and supervised training without slowing training.
Compared with the alternatives, the differences are modest but consistent. ReLU is the cheapest and simplest. GELU, which weights each input by the probability that a standard normal variable falls below it, and plain Swish are smooth single-path activations that performed close to ReLU in Shazeer's perplexity table. The gated family, GLU, ReGLU, GEGLU and SwiGLU, all beat the ungated versions in that table, with GEGLU and SwiGLU best. Even the bilinear variant with no nonlinearity at all, just the product of two projections, reached 1.648, better than ReLU, which suggests that the multiplicative gate matters at least as much as the exact curve.
The limitations are practical rather than dramatic. The gains are small in absolute terms, a few hundredths of log-perplexity in the original study, and Shazeer's fine-tuning results are described in the paper as noisy, so single-benchmark comparisons should be read with caution. The extra matrix means three weight reads and three matrix multiplications per FFN, which PaLM acknowledges, and the two intermediate projections both have to be held in memory during training. Choosing the hidden width matters: without the two-thirds reduction a SwiGLU layer has 50% more FFN parameters than its ReLU counterpart, which would make comparisons unfair. Finally, the evidence is empirical; no published theory explains why gating helps.
In practice, SwiGLU costs nothing extra once the width is adjusted. Most deep learning libraries provide Swish under the name SiLU, so a SwiGLU FFN is three linear layers, one SiLU call and one element-wise multiplication. The Swish paper notes the function can be written with a trainable scale beta, but the transformer versions use a fixed beta of 1. Hardware teams sometimes round the two-thirds width to a multiple of a convenient number so the matrices map well onto accelerator tiles. For anyone training a transformer from scratch, the published record supports SwiGLU or GEGLU as a reasonable default.
04کام کی مثال
Tracing one SwiGLU feed-forward step by hand
Take a toy token vector x = [1, -1] with width d = 2 and choose a hidden width of 2 for simplicity.
Let W be the identity matrix, so xW = [1, -1], and let V = [[2, 0], [0, 2]], so xV = [2, -2].
Apply Swish to xW: Swish(1) = 1 * sigmoid(1), about 0.731, and Swish(-1) = -1 * sigmoid(-1), about -0.269.
Multiply element by element with xV: 0.731 * 2 is about 1.462 and -0.269 * -2 is about 0.538, giving [1.462, 0.538].
Project back with W2; with W2 as the identity, the FFN output is [1.462, 0.538].
Compare with a ReLU FFN using W1 = V: ReLU([2, -2]) = [2, 0], so the negative feature is lost entirely instead of being gated.
جو یہ دکھاتا ہے۔
A hand-computed SwiGLU output of about [1.462, 0.538]. Check it by running the same numbers through torch.nn.functional.silu(x @ W) * (x @ V) @ W2 in PyTorch, which should match to three decimal places, and note how the gate kept a scaled version of the negative input.
حقیقی دنیا کا نفاذ
PaLM (Google, 2022) uses SwiGLU in every MLP block, noting that it needs three matrix multiplications instead of two but improved quality in compute-matched experiments.
LLaMA (Meta, 2023) replaces ReLU with SwiGLU and sets the feed-forward hidden size to two thirds of 4d instead of 4d, so parameter count stays close to a standard block.
In Shazeer's T5-base experiments, swapping the ReLU feed-forward layer for SwiGLU cut held-out log-perplexity from 1.677 to 1.636 after 524,288 training steps with matched parameters.
Gated convolutional language models from Facebook AI Research (Dauphin et al., 2016) introduced the gated linear unit and reported state-of-the-art results on WikiText-103 without recurrence.
A 2021 Google study of dozens of transformer modifications found that SwiGLU and GeGLU were among the few that improved pre-training and fine-tuning without slowing training.
SwiGLU اور گیٹڈ ایکٹیویشنز کا مستقبل
The research record on SwiGLU is mostly empirical. Shazeer's 2020 paper offered no explanation for the gains, and the 2021 Narang et al. study warned that many transformer changes fail to transfer across codebases, so independent replications remain the main evidence. Open questions include why multiplicative gating helps so consistently, whether the benefit changes at very large scale, and how gating interacts with other common choices such as RMS normalization, mixture-of-experts layers and low-precision training. On the engineering side, SwiGLU's three matrices and two intermediate activations make it a target for fused GPU kernels and memory-saving recomputation, and accelerator vendors and framework authors continue to publish optimized implementations. Since PaLM in 2022 and LLaMA in 2023, gated feed-forward layers have become a default in openly documented large language model architectures, and new activation proposals are now typically benchmarked against SwiGLU or GEGLU rather than against ReLU.
اسے عملی جامہ پہنانا
یہ کیوں اہمیت رکھتا ہے۔
- لاگت اور بجٹ. فن تعمیر کے فیصلے سالوں تک کارکردگی اور آپریٹنگ لاگت کو آگے بڑھاتے ہیں۔
- واضح فیصلے. تکنیکی تعلیم ٹیموں کو صحیح اسٹیک منتخب کرنے میں مدد کرتی ہے، نہ صرف جدید ترین۔
- کوالٹی کنٹرول. انجینئرنگ کے بہتر انتخاب پیداوار میں قابل اعتماد واقعات کو کم کرتے ہیں۔
پر نگاہ رکھیں
- ایک بینچ مارک کو بہتر بنانا نظام کی وسیع تر کمزوریوں کو چھپا سکتا ہے۔
- بنیادی ڈھانچے اور دیکھ بھال کے اخراجات کو اکثر کم سمجھا جاتا ہے۔
- سیکورٹی اور مشاہداتی فرق بڑھ سکتا ہے کیونکہ نظام زیادہ پیچیدہ ہو جاتا ہے۔
اگلے اقدامات
- نفاذ سے پہلے تاخیر، معیار اور لاگت کے اہداف کی وضاحت کریں۔
- حقیقت پسندانہ بوجھ اور ڈیٹا کی شرائط کے تحت بینچ مارک۔
- غلطیوں، بڑھے ہوئے، اور صارف کے اثرات کے لیے آلے کی نگرانی۔
- اسکیلنگ سے پہلے رول بیک اور واقعہ کے ردعمل کے راستے تیار کریں۔
ذرائع اور مزید پڑھنا
- arXiv (Google)GLU Variants Improve Transformer
- arXiv (ICML 2017)Language Modeling with Gated Convolutional Networks
- arXiv (Google Brain)Searching for Activation Functions
- arXiv (Google)PaLM: Scaling Language Modeling with Pathways
- arXiv (Meta AI)LLaMA: Open and Efficient Foundation Language Models
- arXiv (Google Research)Do Transformer Modifications Transfer Across Implementations and Applications?
دریافت کرتے رہیں
مفت نیوز لیٹر
روزانہ AI بریفنگ حاصل کریں
ہر ہفتے کے دن صبح تین تصدیق شدہ AI کہانیاں، سادہ انگریزی میں لکھی گئیں۔ ہمیشہ کے لیے مفت، کوئی اشتہار نہیں۔
ہر ہفتے کے دن ایک ای میل۔ ایک کلک میں ان سبسکرائب کریں۔ ہم کبھی بھی آپ کا پتہ فروخت یا شیئر نہیں کرتے۔
اپنے آپ کو جانچیں۔
Take the SwiGLU and Gated Activations quiz
ہر جواب پر فوری فیڈبیک، اور کورس پاس کرنے کے بعد قابلِ تصدیق ID کے ساتھ شیئر کرنے کے قابل سرٹیفکیٹ۔
مفت AI تعلیم کی حمایت کریں۔ AI Understanding ایک 501(c)(3) غیر منافع بخش ادارہ ہے — کوئی اشتہار نہیں، کوئی پے وال نہیں، کبھی نہیں۔ عطیہ کریں۔
اکثر پوچھے گئے سوالات
What is SwiGLU and Gated Activations?
SwiGLU is a gated activation used inside the feed-forward layers of transformers: the input is projected twice, one copy passes through the Swish function, and the two copies are multiplied element by element. Noam Shazeer's 2020 paper showed it lowered perplexity at equal compute, and PaLM and LLaMA adopted it, making it a standard part of large language model design.
کیا بنیادی آپریشن گیٹڈ لکیری یونٹ کی وضاحت کرتا ہے؟
ایک GLU ایک ویلیو پروجیکشن کو گیٹ پروجیکشن عنصر کے حساب سے ضرب دیتا ہے، جس سے نیٹ ورک کنٹرول کی معلومات کو فی جہت کے بہاؤ کی اجازت دیتا ہے۔
SwiGLU اپنی گیٹ برانچ کے لیے کون سا ایکٹیویشن فنکشن استعمال کرتا ہے؟
SwiGLU سوئش کا استعمال کرتا ہے، جسے SiLU بھی کہا جاتا ہے، جس کی تعریف x * sigmoid(beta*x) ہے، گیٹنگ برانچ کے لیے۔
ایک SwiGLU فیڈ فارورڈ بلاک کتنے ویٹ میٹرس استعمال کرتا ہے؟
SwiGLU کو تین میٹرکس کی ضرورت ہے: گیٹ پروجیکشن، ویلیو پروجیکشن، اور آؤٹ پٹ پروجیکشن۔
پوشیدہ طول و عرض عام طور پر SwiGLU تہوں میں تقریباً دو تہائی کیوں ہے؟
تیسرا میٹرکس دوسری صورت میں کمپیوٹ کو بڑھا دے گا، اس لیے پوشیدہ سائز کو تقریباً 8/3 ڈی تک سکڑ کر بجٹ کو مماثل رکھتا ہے۔
2020 میں ٹرانسفارمر فیڈ فارورڈ لیئرز کے لیے SwiGLU کو کس نے مقبول بنایا؟
Noam Shazeer کے 2020 نوٹ 'GLU Variants Improve Transformer' نے SwiGLU کو متعارف کرایا اور اس کے پریشان کن فوائد کو ظاہر کیا۔
سیکھتے رہیں
متعلقہ گائیڈز
اس موضوع کے لیے مزید گائیڈز چنے گئے ہیں۔