ICS 582Lecture 06المحاضرة 06Part 07الجزء 07
Decoder-only, encoder-only and encoder-decoder modelsنماذج Decoder-only وEncoder-only وEncoder-decoder
The three ways to build a language model from transformer blocks, what each is trained to do and used for, and why transformer LM training runs in parallel across positions.الطرق الثلاث لبناء نموذج لغة من كتل المحوِّل، وما يُدرَّب عليه كل منها وما يُستخدم له، ولماذا يجري تدريب نماذج اللغة بالمحوِّلات بالتوازي عبر المواضع.
- Conceptsالمفاهيم
- 5
- Slidesالشرائح
- 32-37
- Readingمدة القراءة
- 30 min30 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
You now know the transformer block: self-attention, a feedforward layer, residual connections and layer norm. This part answers the natural next question. Given a stack of these blocks, how do you turn it into a language model, and what changes when you want to classify a review, translate a sentence, or chat?
The answer is surprisingly small. The blocks stay the same. What changes is which positions are allowed to attend to which, and what the model is trained to predict. That one wiring choice gives three families: decoder-only models like GPT, encoder-only models like BERT, and encoder-decoder models like the original transformer. Expect a comparison question on the exam: training objective, attention pattern, typical task, and example models for each. Expect a second question on why transformer training parallelizes when RNN training does not. Both are answered here, and the comparison table in the first concept is the page to memorize.
أنت تعرف الآن كتلة الـ transformer: الانتباه الذاتي (self-attention)، وطبقة feedforward، والوصلات المتبقية (residual connections)، وlayer norm. يجيب هذا الجزء عن السؤال الطبيعي التالي: إذا كان لديك مكدس (stack) من هذه الكتل، فكيف تحوّله إلى نموذج لغة، وما الذي يتغير عندما تريد تصنيف مراجعة، أو ترجمة جملة، أو إجراء محادثة؟
الإجابة صغيرة على نحو مفاجئ. الكتل تبقى كما هي. ما يتغير هو أي المواضع يُسمح لها بالانتباه إلى أي مواضع، وما الذي يُدرَّب النموذج على التنبؤ به. هذا الاختيار الواحد في طريقة التوصيل يعطي ثلاث عائلات: نماذج decoder-only مثل GPT، ونماذج encoder-only مثل BERT، ونماذج encoder-decoder مثل الـ transformer الأصلي. توقّع سؤال مقارنة في الامتحان: هدف التدريب، ونمط الانتباه، والمهمة المعتادة، والنماذج الأمثلة لكل عائلة. وتوقّع سؤالًا ثانيًا عن سبب قابلية تدريب الـ transformer للتوازي بينما تدريب الـ RNN ليس كذلك. كلاهما مُجاب عنه هنا، وجدول المقارنة في المفهوم الأول هو الصفحة التي يجب حفظها.
By the end you canفي نهاية هذا الجزء ستستطيع
- Draw the three transformer LM architectures and say which attention links each one has: causal, full, or cross.أن ترسم معماريات نماذج اللغة الثلاث المبنية على الـ transformer وتحدد روابط الانتباه في كل منها: سببي (causal)، أو كامل (full)، أو متقاطع (cross).
- Compare decoder-only, encoder-only and encoder-decoder models by training objective, attention pattern, typical task and example models.أن تقارن بين نماذج decoder-only و encoder-only و encoder-decoder من حيث هدف التدريب، ونمط الانتباه، والمهمة المعتادة، والنماذج الأمثلة.
- Explain what a masked language model is trained to do and why its outputs are representations rather than words.أن تشرح ما الذي يُدرَّب عليه الـ masked language model، ولماذا تكون مخرجاته تمثيلات (representations) لا كلمات.
- Give the three synonyms for decoder-only models and explain what each name says.أن تذكر الأسماء المرادفة الثلاثة لنماذج decoder-only وتشرح ما يعنيه كل اسم.
- Explain why transformer LM training runs in parallel across positions while generation stays sequential, and compute the averaged loss for a short sentence.أن تشرح لماذا يجري تدريب نموذج اللغة المبني على الـ transformer بالتوازي عبر المواضع بينما يبقى التوليد تسلسليًا، وأن تحسب متوسط الخسارة لجملة قصيرة.
Take the phrase So long and thanks for, five tokens, and push it through one layer of a transformer. Each position produces an output vector by attending to other positions. The only design decision left is this: when position 3 (the word and) builds its output, which of the five inputs may it look at?
خذ العبارة So long and thanks for، وهي خمسة رموز (tokens)، ومرّرها عبر طبقة واحدة من transformer. كل موضع ينتج متجه مخرجات بالانتباه إلى مواضع أخرى. القرار التصميمي الوحيد المتبقي هو: عندما يبني الموضع 3 (الكلمة and) مخرجاته، فإلى أي من المدخلات الخمسة يُسمح له أن ينظر؟
One block, three wiringsكتلة واحدة، ثلاث طرق توصيل
There are exactly three useful answers, and each defines a model family that slide 33 draws side by side.
هناك ثلاث إجابات مفيدة بالضبط، وكل منها تحدد عائلة من النماذج ترسمها الشريحة 33 جنبًا إلى جنب.
- Only to the left. Position 3 may read positions 1, 2 and 3, never 4 or 5. This is causal (masked) self-attention, and the model built from it is a decoder-only model. Because it never sees the future, it can be trained to predict the next word, and it can generate text. In the slide figure every arrow points straight up or up and to the right.إلى اليسار فقط. يمكن للموضع 3 أن يقرأ المواضع 1 و2 و3، ولا يقرأ أبدًا 4 أو 5. هذا هو causal (masked) self-attention أي الانتباه الذاتي السببي (المقنّع)، والنموذج المبني عليه هو decoder-only model. ولأنه لا يرى المستقبل أبدًا، يمكن تدريبه على التنبؤ بالكلمة التالية، ويمكنه توليد النص. في شكل الشريحة، كل سهم يتجه إلى الأعلى مباشرة أو إلى الأعلى واليمين.
- Everywhere. Position 3 may read all five positions, left and right. This is ordinary bidirectional self-attention, and the model is an encoder-only model. Its arrows in the figure cross in both directions. Seeing the future makes next-word prediction trivial (the answer is in the input), so it is trained on a different task, and its outputs are drawn as empty ovals: vectors that represent each token in context, not predicted words.في كل مكان. يمكن للموضع 3 أن يقرأ المواضع الخمسة كلها، يسارًا ويمينًا. هذا هو self-attention العادي ثنائي الاتجاه، والنموذج هو encoder-only model. أسهمه في الشكل تتقاطع في الاتجاهين. رؤية المستقبل تجعل التنبؤ بالكلمة التالية أمرًا تافهًا (فالإجابة موجودة في المدخلات)، لذلك يُدرَّب على مهمة مختلفة، وتُرسم مخرجاته كأشكال بيضاوية فارغة: متجهات تمثل كل رمز في سياقه، لا كلمات متنبأ بها.
- Two stacks talking. An encoder reads a whole source sequence bidirectionally. A separate decoder writes the target sequence causally, and every decoder position also reads every final encoder state through cross-attention. This is the encoder-decoder transformer. In the figure, the fan of lines from the left stack to the top of the right stack is cross-attention.مكدسان يتحاوران. يقرأ encoder تسلسل المصدر كاملًا بشكل ثنائي الاتجاه. ويكتب decoder منفصل التسلسل الهدف بشكل سببي، وكل موضع في الـ decoder يقرأ أيضًا كل حالة نهائية للـ encoder عبر cross-attention. هذا هو الـ transformer من نوع encoder-decoder. في الشكل، مروحة الخطوط الممتدة من المكدس الأيسر إلى أعلى المكدس الأيمن هي الـ cross-attention.
The grid makes the difference countable. With tokens, bidirectional attention computes scores. Causal attention keeps only of them, because every score with key position is replaced by before the softmax, which turns it into a weight of exactly zero. For N = 5 that is 15 of 25.
تجعل الشبكة الفرق قابلًا للعدّ. مع رمزًا، يحسب الانتباه ثنائي الاتجاه درجة (score). أما الانتباه السببي فيُبقي منها فقط، لأن كل درجة يكون فيها موضع المفتاح تُستبدل بـ قبل الـ softmax، مما يحوّلها إلى وزن يساوي صفرًا تمامًا. عندما N = 5 يكون ذلك 15 من 25.
- Causal self-attentionانتباه ذاتي سببي
Click an output position at the top to keep only the links it reads from.انقر على موضع مخرج في الأعلى لتُبقي فقط الروابط التي يقرأ منها.
- Training objectiveهدف التدريب
- Predict the next word from the words to its left.التنبؤ بالكلمة التالية من الكلمات التي على يسارها.
- Attention patternنمط الانتباه
- Causal: position i sees positions 1 to i only.سببي: الموضع i يرى المواضع من 1 إلى i فقط.
- Typical tasksالمهام النموذجية
- Generation: chat, completion, story writing.التوليد: المحادثة، والإكمال، وكتابة القصص.
- Examplesأمثلة
- GPT family, GPT-2
The comparison to memorizeالمقارنة التي يجب حفظها
| Architectureالمعمارية | Also calledتُسمى أيضًا | Attention patternنمط الانتباه | Training objectiveهدف التدريب | Output per positionالمخرج في كل موضع | Typical tasksالمهام المعتادة | Examplesأمثلة |
|---|---|---|---|---|---|---|
| Decoder-onlyDecoder-only | Causal, autoregressive or left-to-right LLMنموذج LLM سببي (causal)، أو ذاتي الانحدار (autoregressive)، أو من اليسار إلى اليمين (left-to-right) | Causal self-attention: position i sees 1 to iCausal self-attention: الموضع i يرى المواضع من 1 إلى i | Predict the next wordالتنبؤ بالكلمة التالية | A probability over the vocabulary at every positionتوزيع احتمالي على المفردات في كل موضع | Generation: chat, completion, writingالتوليد: المحادثة، والإكمال، والكتابة | GPT familyعائلة GPT |
| Encoder-onlyEncoder-only | Masked language model (MLM), bidirectional encoderنموذج لغة مقنّع (masked language model أو MLM)، أو encoder ثنائي الاتجاه | Full self-attention: every position sees every positionانتباه ذاتي كامل (full self-attention): كل موضع يرى كل المواضع | Predict masked words from both sidesالتنبؤ بالكلمات المقنّعة من الجانبين | One contextual vector per tokenمتجه سياقي (contextual vector) واحد لكل رمز | Classification and tagging after fine-tuning (sentiment, NER)التصنيف والوسم بعد الـ fine-tuning (تحليل المشاعر sentiment، وNER) | BERT, RoBERTaBERT, RoBERTa |
| Encoder-decoderEncoder-decoder | Seq2seq transformertransformer من نوع seq2seq | Encoder full; decoder causal plus cross-attention to encoder statesالـ encoder كامل، والـ decoder سببي مع cross-attention إلى حالات الـ encoder | Map an input sequence to an output sequenceتحويل تسلسل مدخلات إلى تسلسل مخرجات | Output words, generated left to rightكلمات المخرج، تُولَّد من اليسار إلى اليمين | Machine translation, speech recognition, summarizationالترجمة الآلية، والتعرف على الكلام، والتلخيص | Original transformer, T5, BART, Whisperالـ transformer الأصلي، وT5، وBART، وWhisper |
Recall
For each architecture, which attention links exist: causal, full bidirectional, or cross?في كل معمارية، ما روابط الانتباه الموجودة: سببية، أم ثنائية الاتجاه كاملة، أم متقاطعة (cross)؟
Hide one word: So long [MASK] thanks for all the fish. You can fill the gap with and almost instantly, and you used both sides to do it. So long on the left suggests a farewell, and thanks on the right demands a conjunction. A model that may only look left would have to guess from So long alone.
أخفِ كلمة واحدة: So long [MASK] thanks for all the fish. يمكنك ملء الفراغ بـ and فورًا تقريبًا، وقد استخدمت الجانبين لفعل ذلك. فـ So long على اليسار توحي بالوداع، وthanks على اليمين تتطلب أداة عطف. أما النموذج الذي لا يُسمح له إلا بالنظر إلى اليسار فعليه أن يخمّن من So long وحدها.
What an encoder is trained to doما الذي يُدرَّب عليه الـ encoder
That gap-filling game is the training objective of the most popular encoders, the masked language models (MLMs) of the BERT family. A bidirectional encoder cannot be trained to predict the next word, because full attention would let each position simply read the next word from the input. So the input is corrupted instead. In BERT, 15% of the tokens are chosen for prediction. Of those, 80% are replaced by the special token [MASK], 10% by a random word, and 10% are left unchanged. The model then predicts the original token at each chosen position, using cross-entropy as before. The targets come from the raw text itself, so this is still self-supervised training.
لعبة ملء الفراغات هذه هي هدف التدريب لأشهر الـ encoders، أي نماذج اللغة المقنّعة (masked language models أو MLMs) من عائلة BERT. لا يمكن تدريب encoder ثنائي الاتجاه على التنبؤ بالكلمة التالية، لأن الانتباه الكامل يسمح لكل موضع بأن يقرأ الكلمة التالية ببساطة من المدخلات. لذلك تُفسَد المدخلات بدلًا من ذلك. في BERT يُختار 15% من الرموز للتنبؤ بها. ومن هذه، يُستبدل 80% بالرمز الخاص [MASK]، و10% بكلمة عشوائية، و10% تُترك دون تغيير. ثم يتنبأ النموذج بالرمز الأصلي في كل موضع مختار، مستخدمًا cross-entropy كما سبق. الأهداف تأتي من النص الخام نفسه، لذلك يظل هذا self-supervised training أي تدريبًا ذاتي الإشراف.
Representations, then fine-tuningتمثيلات، ثم fine-tuning
The purpose of an encoder is not to generate text. Its output is one contextual vector per input token, the ovals at the top of the encoder panel on slide 33. The vector for bank in river bank differs from the one in bank loan, because each was built by attending to the whole sentence. To use these vectors, you add a small task head and continue training on labeled data, which is fine-tuning. For sentiment, a classifier reads one vector that summarizes the sentence and outputs positive or negative. For named entity recognition, a classifier reads every token's vector and outputs a tag such as person or location.
الغرض من الـ encoder ليس توليد النص. مخرجاته متجه سياقي واحد لكل رمز من المدخلات، وهي الأشكال البيضاوية في أعلى لوحة الـ encoder في الشريحة 33. متجه bank في river bank (ضفة النهر) يختلف عن متجهها في bank loan (قرض بنكي)، لأن كلًا منهما بُني بالانتباه إلى الجملة كاملة. ولاستخدام هذه المتجهات، تضيف رأسًا صغيرًا للمهمة (task head) وتواصل التدريب على بيانات موسومة، وهذا هو fine-tuning. في تحليل المشاعر (sentiment)، يقرأ مصنِّف متجهًا واحدًا يلخص الجملة ويُخرج إيجابي أو سلبي. وفي التعرف على الكيانات المسماة (named entity recognition)، يقرأ مصنِّف متجه كل رمز ويُخرج وسمًا مثل شخص أو مكان.
Quick checkاختبار سريع
What is the pretraining objective of a BERT-style encoder-only model?ما هدف التدريب المسبق (pretraining) لنموذج encoder-only على طريقة BERT؟
Translate merci pour tout into English. The output, thanks for everything, is a new sequence in a different language, and its length need not match the input. Neither family so far fits cleanly. An encoder alone outputs one vector per French word, not English words. A decoder alone can write English, but it has no special place for a source sentence it must stay faithful to.
ترجم merci pour tout إلى الإنجليزية. المخرج، thanks for everything، تسلسل جديد بلغة مختلفة، ولا يلزم أن يطابق طوله طول المدخل. لا تناسب أي من العائلتين السابقتين هذه المهمة بشكل نظيف. فالـ encoder وحده يُخرج متجهًا واحدًا لكل كلمة فرنسية، لا كلمات إنجليزية. والـ decoder وحده يستطيع كتابة الإنجليزية، لكن ليس لديه مكان خاص لجملة مصدر يجب أن يبقى وفيًا لها.
Read everything, then write left to rightاقرأ كل شيء، ثم اكتب من اليسار إلى اليمين
The encoder-decoder transformer is trained to map one sequence to another. The encoder reads the entire source bidirectionally, exactly like an encoder-only model, and produces one contextual vector per source token. The decoder generates the target one token at a time with causal self-attention over the target words so far. Between the two sits cross-attention: in each decoder block, the queries come from the decoder, while the keys and values come from the final encoder output. So when the decoder is about to write everything, it can put most of its attention weight on tout.
يُدرَّب الـ transformer من نوع encoder-decoder على تحويل تسلسل إلى تسلسل آخر. يقرأ الـ encoder المصدر كاملًا بشكل ثنائي الاتجاه، تمامًا مثل نموذج encoder-only، وينتج متجهًا سياقيًا واحدًا لكل رمز في المصدر. ويولّد الـ decoder الهدف رمزًا بعد رمز مع causal self-attention على الكلمات الهدف حتى الآن. وبين الاثنين يوجد cross-attention: في كل كتلة decoder تأتي الاستعلامات (queries) من الـ decoder، بينما تأتي المفاتيح والقيم (keys وvalues) من المخرج النهائي للـ encoder. لذلك عندما يوشك الـ decoder على كتابة everything، يستطيع أن يضع معظم وزن انتباهه على tout.
This is the same encoder-decoder idea you met with RNNs, with one big repair. An RNN encoder squeezed the whole source into a single final hidden state, a bottleneck for long sentences. A transformer decoder can look at every encoder state directly, at every step, with its own attention weights. It is also the architecture of the original transformer paper, which was built for machine translation.
هذه هي فكرة الـ encoder-decoder نفسها التي قابلتها مع الـ RNNs، مع إصلاح كبير واحد. كان encoder الـ RNN يضغط المصدر كله في حالة مخفية نهائية واحدة (hidden state)، وهذا عنق زجاجة للجمل الطويلة. أما decoder الـ transformer فيستطيع النظر إلى كل حالة من حالات الـ encoder مباشرة، في كل خطوة، بأوزان انتباه خاصة به. وهي أيضًا معمارية ورقة الـ transformer الأصلية، التي بُنيت للترجمة الآلية.
Where it is usedأين يُستخدم
- Machine translation: one language in, another language out. The canonical seq2seq task.الترجمة الآلية: لغة تدخل، ولغة أخرى تخرج. إنها مهمة الـ seq2seq النموذجية.
- Speech recognition: a sequence of acoustic feature frames in, a sequence of words out. Whisper is an encoder-decoder transformer of this kind.التعرف على الكلام: تسلسل من إطارات الخصائص الصوتية (acoustic feature frames) يدخل، وتسلسل من الكلمات يخرج. وWhisper هو transformer من نوع encoder-decoder من هذا الصنف.
- Other sequence-to-sequence text tasks such as summarization, which T5 and BART handle by casting every task as text in, text out.مهام نصية أخرى من نوع تسلسل إلى تسلسل مثل التلخيص، التي يتعامل معها T5 وBART بصياغة كل مهمة على أنها نص يدخل ونص يخرج.
Recall
In an encoder-decoder transformer, where do the queries, keys and values of cross-attention come from, and which parts are causal?في transformer من نوع encoder-decoder، من أين تأتي الـ queries والـ keys والـ values في الـ cross-attention، وأي الأجزاء سببية؟
Type So long and thanks into a chatbot. To continue, it must choose the next word using only what is already written, because nothing to the right exists yet. It picks for, appends it, and repeats. This is the whole job of a decoder-only model, and it is how GPT-style LLMs work.
اكتب So long and thanks في روبوت محادثة. ليكمل، يجب أن يختار الكلمة التالية مستخدمًا فقط ما كُتب بالفعل، لأنه لا شيء موجود على اليمين بعد. فيختار for، ويضيفها، ويكرر. هذه هي وظيفة نموذج decoder-only كلها، وهكذا تعمل نماذج LLMs على طريقة GPT.
Three names, one modelثلاثة أسماء، نموذج واحد
A decoder-only model predicts words left to right. The slides list its three other names, and each one describes a different face of the same rule.
الـ decoder-only model يتنبأ بالكلمات من اليسار إلى اليمين. تذكر الشرائح أسماءه الثلاثة الأخرى، وكل اسم يصف وجهًا مختلفًا للقاعدة نفسها.
- Causal LLM: the output at position i depends only on causes, positions 1 to i, never on effects to its right. This is the causal mask.Causal LLM (سببي): المخرج في الموضع i يعتمد فقط على الأسباب، أي المواضع من 1 إلى i، ولا يعتمد أبدًا على النتائج التي على يمينه. هذا هو causal mask.
- Autoregressive LLM: each generated token is fed back as input for the next one, so the model regresses on its own previous outputs. This is autoregressive generation.Autoregressive LLM (ذاتي الانحدار): كل رمز مولَّد يُعاد إدخاله كمدخل للرمز التالي، فالنموذج ينحدر (regresses) على مخرجاته السابقة نفسها. هذا هو autoregressive generation.
- Left-to-right LLM: the direction in which it reads and writes. For Arabic text the order is really first-to-last token in reading order, which is right to left on the page; the model does not care about screen direction, only sequence order.Left-to-right LLM (من اليسار إلى اليمين): الاتجاه الذي يقرأ ويكتب به. في النص العربي يكون الترتيب في الحقيقة من الرمز الأول إلى الأخير بترتيب القراءة، وهو من اليمين إلى اليسار على الصفحة. النموذج لا يهتم باتجاه الشاشة، بل بترتيب التسلسل فقط.
The training objective is the one from the RNN language model: at every position, predict the next word, and pay . Why is it called a decoder when there is no encoder? It is the decoder half of the original encoder-decoder transformer with the cross-attention removed. The original transformer came first; the decoder-only language model was carved out of it later.
هدف التدريب هو نفسه هدف نموذج اللغة المبني على الـ RNN: في كل موضع، تنبأ بالكلمة التالية، وادفع . لماذا يُسمى decoder مع أنه لا يوجد encoder؟ لأنه نصف الـ decoder من الـ transformer الأصلي من نوع encoder-decoder بعد إزالة الـ cross-attention. الـ transformer الأصلي جاء أولًا، ثم اقتُطع منه نموذج اللغة decoder-only لاحقًا.
Quick checkاختبار سريع
Which name describes a decoder-only transformer language model?أي اسم يصف نموذج لغة transformer من نوع decoder-only؟
Label a product review as positive or negative.تصنيف مراجعة منتج بأنها إيجابية أو سلبية.
Ask two questions: is the whole input known before you answer, and is the output a label or new text?اسأل سؤالين: هل المدخل كله معروف قبل أن تجيب، وهل المخرج تصنيف أم نص جديد؟
Recall
Give the three synonyms for decoder-only models and say what each one emphasizes.اذكر الأسماء المرادفة الثلاثة لنماذج decoder-only وقل ما الذي يبرزه كل منها.
Train an RNN language model on So long and thanks for all. It must compute hidden state 1, then use it for hidden state 2, then 3: five dependent steps, each waiting for the last. A transformer computes all five positions' predictions, and all five losses, in a single forward pass. Slide 37 states the payoff in one line: training can be in parallel.
درّب نموذج لغة مبنيًا على RNN على So long and thanks for all. يجب أن يحسب الحالة المخفية 1، ثم يستخدمها للحالة المخفية 2، ثم 3: خمس خطوات متتابعة يعتمد بعضها على بعض، كل منها تنتظر سابقتها. أما الـ transformer فيحسب تنبؤات المواضع الخمسة كلها، والخسائر الخمس كلها، في تمرير أمامي واحد. تذكر الشريحة 37 الفائدة في سطر واحد: يمكن أن يكون التدريب متوازيًا.
Why the positions do not have to wait for each otherلماذا لا تحتاج المواضع إلى انتظار بعضها
Two facts combine. First, teacher forcing: during training the input at every position is the true previous text, not the model's own guess, so the entire input row So long and thanks for and the entire target row long and thanks for all are known before the pass begins. Second, the causal mask: although all five positions are computed together as one matrix product, position 2 cannot attend to positions 3 to 5, so it never sees its own target and. Each position is therefore an honest next-word prediction, and all of them are computed at once. An input of N tokens, with the target row shifted one place to the right, gives N training examples per forward pass. That is parallel training, and it is what made pretraining on enormous corpora practical.
حقيقتان تجتمعان. الأولى teacher forcing: أثناء التدريب يكون المدخل في كل موضع هو النص السابق الحقيقي، لا تخمين النموذج نفسه، لذلك يكون صف المدخلات كاملًا So long and thanks for وصف الأهداف كاملًا long and thanks for all معروفين قبل بدء التمرير. الثانية causal mask: مع أن المواضع الخمسة تُحسب معًا كضرب مصفوفات واحد، لا يستطيع الموضع 2 الانتباه إلى المواضع من 3 إلى 5، فلا يرى أبدًا هدفه and. لذلك كل موضع هو تنبؤ نزيه بالكلمة التالية، وكلها تُحسب دفعة واحدة. مدخل من N رمزًا، مع إزاحة صف الأهداف موضعًا واحدًا إلى اليمين، يعطي N مثالًا تدريبيًا في كل تمرير أمامي. هذا هو parallel training أي التدريب المتوازي، وهو ما جعل التدريب المسبق على مدوّنات نصية ضخمة أمرًا عمليًا.
The figure on slide 37 shows the pipeline per position: input embedding, the shared transformer block, a linear layer, a softmax over the vocabulary, and a loss box , , and so on. The loss is the cross-entropy from part 4: the negative log of the probability given to the correct next word.
يُظهر الشكل في الشريحة 37 خط المعالجة لكل موضع: تضمين المدخل (input embedding)، ثم transformer block المشتركة، ثم طبقة خطية (linear layer)، ثم softmax على المفردات، ثم صندوق الخسارة ، ، وهكذا. الخسارة هي cross-entropy من الجزء 4: سالب لوغاريتم الاحتمال المعطى للكلمة التالية الصحيحة.
Put numbers on it. Suppose that in one forward pass the softmax at each position gives the correct next word the probabilities below. All five rows come out of the same pass.
لنضع أرقامًا. افترض أنه في تمرير أمامي واحد يعطي الـ softmax في كل موضع الكلمة التالية الصحيحة الاحتمالات أدناه. الصفوف الخمسة كلها تخرج من التمرير نفسه.
| Positionالموضع | Visible contextالسياق المرئي | True next wordالكلمة التالية الصحيحة | ŷ[w_true]ŷ[w_true] | -ln ŷ[w_true]-ln ŷ[w_true] |
|---|---|---|---|---|
| 11 | SoSo | longlong | 0.200.20 | 1.6091.609 |
| 22 | So longSo long | andand | 0.500.50 | 0.6930.693 |
| 33 | So long andSo long and | thanksthanks | 0.400.40 | 0.9160.916 |
| 44 | So long and thanksSo long and thanks | forfor | 0.800.80 | 0.2230.223 |
| 55 | So long and thanks forSo long and thanks for | allall | 0.250.25 | 1.3861.386 |
Worked exampleمثال محلول
Averaging into the training lossحساب المتوسط للحصول على خسارة التدريب
Sum the five lossesاجمع الخسائر الخمس
1.609 + 0.693 + 0.916 + 0.223 + 1.386 = 4.8271.609 + 0.693 + 0.916 + 0.223 + 1.386 = 4.827Average over positionsخذ المتوسط على المواضع
Resultالنتيجة
The training loss for this sequence is about 0.965, obtained from one pass. Backpropagation then updates all weights from all five positions together. Note where the pain is: position 1, with only So as context, gives long just 0.20 and pays the largest loss.خسارة التدريب لهذا التسلسل تساوي تقريبًا 0.965، وقد حصلنا عليها من تمرير واحد. ثم يحدّث الـ backpropagation كل الأوزان من المواضع الخمسة معًا. لاحظ أين يكمن الألم: الموضع 1، الذي سياقه So فقط، يعطي long احتمال 0.20 فقط ويدفع أكبر خسارة.
Generation is still one token at a timeالتوليد ما زال رمزًا واحدًا في كل مرة
At inference time the trick disappears. To write the continuation, the model must sample word t + 1 before it can use it as input for word t + 2. There is no known target row to feed in. Producing N new tokens therefore needs N forward passes in sequence, exactly as in autoregressive generation with an RNN.
عند الاستدلال (inference) تختفي هذه الحيلة. لكتابة التكملة، يجب أن يختار النموذج الكلمة t + 1 قبل أن يستطيع استخدامها كمدخل للكلمة t + 2. لا يوجد صف أهداف معروف لإدخاله. لذلك يحتاج إنتاج N رمزًا جديدًا إلى N تمريرًا أماميًا متتابعًا، تمامًا كما في autoregressive generation مع الـ RNN.
| Phaseالمرحلة | RNN LMنموذج لغة RNN | Transformer LMنموذج لغة transformer |
|---|---|---|
| Trainingالتدريب | Sequential: each hidden state needs the previous one, so N dependent stepsتسلسلي: كل حالة مخفية تحتاج إلى سابقتها، لذلك توجد N خطوة متتابعة يعتمد بعضها على بعض | Parallel: all N positions in one pass (teacher forcing plus causal mask)متوازٍ: المواضع الـ N كلها في تمرير واحد (teacher forcing مع القناع السببي) |
| Generationالتوليد | Sequential: one token per stepتسلسلي: رمز واحد في كل خطوة | Sequential: one token per forward passتسلسلي: رمز واحد في كل تمرير أمامي |
Quick checkاختبار سريع
Why can a transformer language model compute every position's loss in one forward pass during training?لماذا يستطيع نموذج لغة مبني على الـ transformer حساب خسارة كل موضع في تمرير أمامي واحد أثناء التدريب؟
Recall
A transformer LM is trained on a 7-token sentence. How many next-word losses does one forward pass produce, and how many forward passes does it take to generate 6 new tokens?يُدرَّب نموذج لغة مبني على الـ transformer على جملة من 7 رموز. كم خسارة للكلمة التالية ينتج تمرير أمامي واحد، وكم تمريرًا أماميًا يلزم لتوليد 6 رموز جديدة؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- The same transformer block builds three model families. The difference is which positions may attend to which.كتلة الـ transformer نفسها تبني ثلاث عائلات من النماذج. والفرق هو أي المواضع يُسمح لها بالانتباه إلى أي مواضع.
- Decoder-only (causal, autoregressive, left-to-right): causal mask, next-word prediction, used for generation. GPT family.Decoder-only (سببي، ذاتي الانحدار autoregressive، من اليسار إلى اليمين): قناع سببي (causal mask)، والتنبؤ بالكلمة التالية، ويُستخدم للتوليد. عائلة GPT.
- Encoder-only: full bidirectional attention, trained to predict masked words from both sides, outputs contextual vectors, usually fine-tuned for classification or tagging. BERT family.Encoder-only: انتباه ثنائي الاتجاه كامل، يُدرَّب على التنبؤ بالكلمات المقنّعة من الجانبين، ويُخرج متجهات سياقية (contextual vectors)، وعادة يُعاد ضبطه (fine-tuned) للتصنيف أو الوسم (tagging). عائلة BERT.
- Encoder-decoder (seq2seq): bidirectional encoder, causal decoder, plus cross-attention from every decoder position to every encoder state. Translation and speech recognition. Original transformer, T5, BART, Whisper.Encoder-decoder (seq2seq): مُرمِّز (encoder) ثنائي الاتجاه، ومفكِّك (decoder) سببي، إضافة إلى cross-attention من كل موضع في الـ decoder إلى كل حالة من حالات الـ encoder. يُستخدم في الترجمة والتعرف على الكلام. الـ transformer الأصلي، و T5، و BART، و Whisper.
- Two different masks: the causal mask hides future positions inside attention; the [MASK] token hides input words in BERT's training data.قناعان مختلفان: القناع السببي يخفي المواضع المستقبلية داخل الانتباه، ورمز [MASK] يخفي كلمات من المدخلات في بيانات تدريب BERT.
- Training is parallel: with teacher forcing the whole target sentence is known, and the causal mask stops any position from seeing its own answer, so all N losses -log ŷ[w_true] come out of one forward pass.التدريب متوازٍ: مع teacher forcing تكون الجملة الهدف كلها معروفة، ويمنع القناع السببي أي موضع من رؤية إجابته، فتخرج كل الخسائر الـ N وهي -log ŷ[w_true] من تمرير أمامي (forward pass) واحد.
- Generation is sequential: token t+1 does not exist until token t has been sampled, so producing N tokens needs N forward passes.التوليد تسلسلي: الرمز t+1 لا يوجد قبل أن يُختار (sampled) الرمز t، لذلك يحتاج توليد N رمزًا إلى N تمريرًا أماميًا.
Sourcesالمصادر
- Speech and Language Processing (3rd ed. draft), Chapter 7: Transformers and PretrainingBookDaniel Jurafsky and James H. Martin, StanfordCausal, autoregressive and left-to-right LMs; why the model is called decoder-only; parallel training because targets are known and the causal mask hides each position's target; -log of the correct token as the loss.نماذج اللغة السببية (causal) وذاتية الانحدار (autoregressive) ومن اليسار إلى اليمين (left-to-right)، ولماذا يُسمى النموذج decoder-only، والتدريب المتوازي لأن الأهداف معروفة والقناع السببي يخفي هدف كل موضع، و -log لاحتمال الرمز الصحيح كخسارة.(opens in a new tab)(يفتح في تبويب جديد)
- Speech and Language Processing (3rd ed. draft), Chapter 9: Masked Language ModelsBookDaniel Jurafsky and James H. Martin, StanfordBidirectional encoders as contextual representation models, the 15% and 80/10/10 masking scheme, and fine-tuning for classification and named entity tagging.الـ encoders ثنائية الاتجاه كنماذج لتمثيلات سياقية، وخطة التقنيع (masking) 15% و 80/10/10، والـ fine-tuning للتصنيف ووسم الكيانات المسماة (named entity tagging).(opens in a new tab)(يفتح في تبويب جديد)
- BERT: Pre-training of Deep Bidirectional Transformers for Language UnderstandingPaperDevlin, Chang, Lee and Toutanova, NAACL 2019Deep bidirectional representations from unlabeled text, fine-tuned with one added output layer.تمثيلات عميقة ثنائية الاتجاه تُتعلَّم من نص غير موسوم، ثم يُعاد ضبطها (fine-tuned) بإضافة طبقة مخرجات واحدة.(opens in a new tab)(يفتح في تبويب جديد)
- Attention Is All You NeedPaperVaswani et al., NeurIPS 2017The original transformer: an encoder-decoder for machine translation, more parallelizable than recurrent models.الـ transformer الأصلي: نموذج encoder-decoder للترجمة الآلية، أكثر قابلية للتوازي من النماذج التكرارية (recurrent).(opens in a new tab)(يفتح في تبويب جديد)
- BART: Denoising Sequence-to-Sequence Pre-trainingPaperLewis et al., ACL 2020An encoder-decoder that combines a bidirectional encoder like BERT with a left-to-right decoder like GPT.نموذج encoder-decoder يجمع encoder ثنائي الاتجاه مثل BERT مع decoder يعمل من اليسار إلى اليمين مثل GPT.(opens in a new tab)(يفتح في تبويب جديد)
- T5 model documentationDocsHugging Face TransformersT5 as an encoder-decoder transformer that casts every task as text-to-text.T5 بوصفه transformer من نوع encoder-decoder يصوغ كل مهمة على أنها نص يدخل ونص يخرج (text-to-text).(opens in a new tab)(يفتح في تبويب جديد)
- GPT-2 model documentationDocsHugging Face TransformersGPT-2 as a causal transformer LM trained to predict the next word, with attention only to previous tokens.GPT-2 بوصفه نموذج لغة transformer سببيًا يُدرَّب على التنبؤ بالكلمة التالية، مع انتباه إلى الرموز السابقة فقط.(opens in a new tab)(يفتح في تبويب جديد)
- Whisper: Robust Speech Recognition via Large-Scale Weak SupervisionDocsOpenAIWhisper is a Transformer sequence-to-sequence model for speech recognition.Whisper نموذج Transformer من نوع تسلسل إلى تسلسل (sequence-to-sequence) للتعرف على الكلام.(opens in a new tab)(يفتح في تبويب جديد)