Architecture · AI & Data Scienceالبنية · الذكاء الاصطناعي وعلم البيانات

Inside the guard:
how a Transformer reads a message.
داخل الحارس:
كيف يقرأ الـ Transformer رسالة.

Follow one sentence through the 13.5 million parameters behind the demo. Every vector, attention map and probability on this page is computed live by the real model, right here in your browser.تتبّع جملة واحدة عبر 13.5 مليون معامل يقوم عليها العرض. كل متجه وخريطة انتباه واحتمال في هذه الصفحة يحسبه النموذج الحقيقي مباشرة، هنا في متصفحك.

…

The pictures below need the model. It is 14 MB, downloads once, then stays in your browser.الرسوم أدناه تحتاج النموذج. حجمه 14 ميغابايت، يُنزَّل مرة واحدة ثم يبقى في متصفحك.

0

The journey at a glanceالرحلة في لمحة

The model is ELECTRA-small, a 12-layer Transformer encoder, fine-tuned to answer one question: is this text trying to hijack an AI? Click any step to jump to its chapter. The shapes update with your sentence (T tokens).النموذج هو ELECTRA-small، وهو مُرمِّز Transformer من 12 طبقة، ضُبط ليجيب عن سؤال واحد: هل يحاول هذا النص السيطرة على الذكاء الاصطناعي؟ انقر أي خطوة لتنتقل إلى فصلها. تتحدّث الأبعاد مع جملتك (T Token).

1

Tokens: turning text into numbersالـ Tokens: تحويل النص إلى أرقام

A neural network can only do arithmetic, so the first step chops the text into tokens and swaps each one for its number in a fixed vocabulary.الشبكة العصبية لا تعرف إلا الحساب، لذا تقطّع الخطوة الأولى النص إلى Tokens وتستبدل كلًّا منها برقمه في مفردات ثابتة.

The tokenizer is WordPiece with a vocabulary of 30,522 pieces. It lower-cases the text, splits it on spaces and punctuation, and looks each word up. Common words are one token. A rare word is broken into pieces it does know: pieces that continue a word start with ##.المُقطِّع هو WordPiece بمفردات من 30,522 قطعة. يحوّل النص إلى أحرف صغيرة، ويقسمه عند المسافات وعلامات الترقيم، ويبحث عن كل كلمة. الكلمات الشائعة Token واحد، أما الكلمة النادرة فتُقسَّم إلى قطع يعرفها، والقطع التي تُكمل كلمة تبدأ بـ ##.

Two special tokens are added. [CLS] ("classification") goes first: by the last layer, its vector will summarise the whole message, and that is what the decision is based on. [SEP] marks the end.يُضاف Tokenان خاصان. [CLS] (اختصار «التصنيف») يأتي أولًا: في الطبقة الأخيرة سيلخّص متجهه الرسالة كلها، وعليه يُبنى القرار. و[SEP] يحدّد النهاية.

The model reads at most 512 tokens; anything longer is cut off. On this page the limit is 64, so the pictures stay readable.يقرأ النموذج 512 Token على الأكثر، وما زاد يُقصّ. وفي هذه الصفحة الحد 64 كي تبقى الرسوم مقروءة.

Show the mathsاعرض الرياضيات

WordPiece uses greedy longest-match-first: for each word, take the longest prefix that is in the vocabulary, then repeat on the rest (as a ## piece) until the word is used up. If no prefix matches, the word becomes [UNK].يعتمد WordPiece على المطابقة الأطول أولًا: لكل كلمة يأخذ أطول بادئة موجودة في المفردات، ثم يكرّر على الباقي (كقطعة ##) حتى تنتهي الكلمة. وإن لم تطابق أي بادئة، تصبح الكلمة [UNK].

The input is therefore just a list of integers.فالمُدخل إذن مجرد قائمة من عددًا صحيحًا.

Your sentence as tokensجملتك على هيئة Tokens

special tokenToken خاص## word piece## قطعة كلمةwhole wordكلمة كاملة
2

Embeddings: a vector for every tokenالـ Embeddings: متجه لكل Token

Each token ID is replaced by a list of learned numbers that places it in a space where similar meanings sit close together.يُستبدل رقم كل Token بقائمة من الأرقام المتعلَّمة تضعه في فضاء تتقارب فيه المعاني المتشابهة.

Three lookup tables are added together for every token:تُجمع ثلاثة جداول بحث لكل Token:

  • Token embedding: what the word is (30,522 rows × 128 numbers).Token embedding: ما هي الكلمة (30,522 صفًا × 128 رقمًا).
  • Position embedding: where it sits (512 rows × 128). Attention has no built-in sense of order, so position must be added.Position embedding: أين تقع (512 صفًا × 128). الانتباه لا يدرك الترتيب بطبيعته، لذا لا بد من إضافة الموضع.
  • Segment embedding: sentence A or B (2 rows). It is always A here.Segment embedding: الجملة A أو B (صفّان). وهي A دائمًا هنا.

The sum is normalised with LayerNorm, then a small linear layer projects it from 128 to 256 numbers, the width of the rest of the model. ELECTRA keeps the big vocabulary table narrow (128) to save parameters: at 256 wide it would need 3.9 million more.يُطبَّع المجموع بـ LayerNorm، ثم تُسقطه طبقة خطية صغيرة من 128 إلى 256 رقمًا، وهو عرض بقية النموذج. ويُبقي ELECTRA جدول المفردات الكبير ضيقًا (128) توفيرًا للمعاملات: فلو كان عرضه 256 لاحتاج 3.9 مليون معامل إضافي.

Did you know? The token table alone holds 28.8% of all the model's parameters.هل تعلم؟ جدول الـ Tokens وحده يضم 28.8% من كل معاملات النموذج.

Show the mathsاعرض الرياضيات

are learned per-dimension scale and shift, and avoids dividing by zero. مقياس وإزاحة متعلَّمان لكل بُعد، و تمنع القسمة على صفر.

The real vectorsالمتجهات الحقيقية

One row per token, one cell per number. negative · positive. Hover a row to see its values.صف لكل Token، وخلية لكل رقم. سالب · موجب. مرّر المؤشر على صف لترى قيمه.

 

3

Self-attention: tokens looking at each otherالانتباه الذاتي: Tokens ينظر بعضها إلى بعض

The heart of the Transformer. In every layer, each token decides which other tokens matter to it and pulls in information from them.قلب الـ Transformer. في كل طبقة، يقرّر كل Token أيّ الـ Tokens الأخرى تهمّه ويسحب منها المعلومات.

Every token's vector is turned into three new vectors:يتحوّل متجه كل Token إلى ثلاثة متجهات جديدة:

  • Query: what am I looking for?Query: عمّ أبحث؟
  • Key: what do I offer?Key: ماذا أقدّم؟
  • Value: what will I pass on if chosen?Value: ماذا سأمرّر إن اختِرت؟

Each query is compared with every key (a dot product). Softmax turns the scores into attention weights that are positive and sum to 1. Each token's new vector is the weighted average of everyone's values.يُقارَن كل Query بكل Key (بالضرب النقطي). ويحوّل Softmax الدرجات إلى أوزان انتباه موجبة مجموعها 1. ومتجه كل Token الجديد هو المتوسط الموزون لقيم الجميع.

This happens in 4 heads at once, each with its own 64-number queries, keys and values, so different heads can track different patterns. Their outputs are joined back to 256 numbers and mixed by one more linear layer.يحدث هذا في 4 رؤوس معًا، لكلٍّ منها Queries وKeys وValues خاصة من 64 رقمًا، فتتتبّع الرؤوس المختلفة أنماطًا مختلفة. ثم تُضمّ مخرجاتها إلى 256 رقمًا وتمزجها طبقة خطية أخرى.

Read with care. Attention shows where information flows, not a full explanation of the decision. Many heads simply park weight on [SEP] or [CLS] when they have nothing useful to do.اقرأ بحذر. الانتباه يبيّن أين تتدفق المعلومات، لا تفسيرًا كاملًا للقرار. فكثير من الرؤوس تركن وزنها على [SEP] أو [CLS] حين لا يكون لديها ما تفعله.

Show the mathsاعرض الرياضيات

For layer input and each head :لمُدخل الطبقة ولكل رأس :

Dividing by keeps the dot products from growing with the vector size, which would push softmax into tiny gradients. is a mask: for padding positions, 0 elsewhere. Row of is exactly what the heatmap shows.القسمة على تمنع نواتج الضرب النقطي من الكبر مع حجم المتجه، وهو ما يدفع Softmax إلى تدرّجات ضئيلة. و قناع: لمواضع الحشو و0 لغيرها. والصف من هو تمامًا ما تعرضه الخريطة الحرارية.

Real attention weightsأوزان الانتباه الحقيقية

Hover a token on the left to see where it looks.مرّر المؤشر على Token في اليسار لترى أين ينظر.

Where [CLS] looks in this layerأين ينظر [CLS] في هذه الطبقة

4

Feed-forward, residuals and LayerNormFeed-forward والوصلات المتبقية وLayerNorm

Attention mixes information between tokens. The feed-forward network then processes each token on its own.الانتباه يمزج المعلومات بين الـ Tokens، ثم تعالج شبكة Feed-forward كل Token وحده.

Each token's 256 numbers are expanded to 1,024, passed through the GELU activation, and squeezed back to 256. The wide middle gives the model room to store patterns; this part holds 46.6% of all parameters.تتوسّع أرقام كل Token الـ 256 إلى 1,024، وتمرّ عبر دالة التنشيط GELU، ثم تُضغط ثانيةً إلى 256. والوسط العريض يمنح النموذج مساحة لتخزين الأنماط، وهذا الجزء يضم 46.6% من كل المعاملات.

Around both sub-layers there is a residual connection: the input is added back to the output (x + f(x)). Each layer only has to learn a correction, and gradients can flow straight through all 12 layers during training.حول كلتا الطبقتين الفرعيتين وصلة متبقية (Residual): يُضاف المُدخل إلى المُخرج (x + f(x)). فلا تحتاج كل طبقة إلا أن تتعلّم تصحيحًا، وتتدفق التدرّجات مباشرة عبر الطبقات الـ 12 أثناء التدريب.

After each addition, LayerNorm rescales every token's vector to a stable range, which keeps training well behaved.بعد كل جمع، يعيد LayerNorm تحجيم متجه كل Token إلى مدى مستقر، فيبقى التدريب منضبطًا.

Show the mathsاعرض الرياضيات

is the standard normal CDF. Unlike ReLU, GELU is smooth and lets small negative values through, weighted by how likely they are. This is the "post-LN" layout used by BERT and ELECTRA. دالة التوزيع التراكمي الطبيعي المعياري. وبخلاف ReLU، فإن GELU سلسة وتمرّر القيم السالبة الصغيرة موزونةً باحتمالها. وهذا ترتيب «post-LN» الذي يستخدمه BERT وELECTRA.

One encoder layerطبقة مُرمِّز واحدة

789,760 parameters · repeated 12×789,760 معاملًا · تتكرر 12 مرة
input H · T × 256المُدخل H · T × 256
Multi-head attentionQ, K, V, output · 263,168
+ residual+ وصلة متبقية
LayerNorm512
Feed-forward 256 → 1024 → 256525,568
+ residual+ وصلة متبقية
LayerNorm512
output · T × 256المُخرج · T × 256

GELU vs ReLUGELU مقابل ReLU

hover the chartمرّر المؤشر على الرسم
5

Twelve layers deepاثنتا عشرة طبقة

The same block runs 12 times, each with its own weights. Early layers tend to capture surface features such as words and positions; later layers build more abstract meaning.تعمل الكتلة نفسها 12 مرة، لكلٍّ منها أوزانها. تميل الطبقات الأولى إلى التقاط السمات السطحية كالكلمات والمواضع، وتبني الطبقات اللاحقة معنى أكثر تجريدًا.

Only one vector reaches the decision: the [CLS] token after layer 12. Through attention it has gathered information from every other token, layer by layer.متجه واحد فقط يصل إلى القرار: Token ‏[CLS] بعد الطبقة 12. وقد جمع عبر الانتباه معلومات من كل Token آخر، طبقةً بعد طبقة.

The heat strip shows that vector after each layer, so you can watch it change as it moves up the stack.يعرض الشريط الحراري ذلك المتجه بعد كل طبقة، فتراه يتغيّر وهو يصعد عبر الطبقات.

The chart below it runs a thought experiment: what would the final classifier say if it read the [CLS] vector of an earlier layer? The answer shows roughly where in the stack the "injection" signal forms for your sentence.والرسم الذي تحته تجربة ذهنية: ماذا سيقول المصنِّف النهائي لو قرأ متجه [CLS] من طبقة أبكر؟ والجواب يبيّن تقريبًا أين تتكوّن إشارة «الحقن» لجملتك.

Illustration only. The classifier was trained on layer 12 alone, so early read-outs are a probe (like a "logit lens"), not how the model actually decides.للتوضيح فقط. دُرّب المصنِّف على الطبقة 12 وحدها، فالقراءات المبكرة مِجسّ (مثل «logit lens»)، لا الطريقة التي يقرّر بها النموذج فعلًا.

[CLS] vector after each layerمتجه [CLS] بعد كل طبقة

13 rows × 256 numbers13 صفًا × 256 رقمًا

Early read-out: P(injection) by layerالقراءة المبكرة: P(injection) حسب الطبقة

7

Where the 13.5 million parameters liveأين تقيم الـ 13.5 مليون معامل

A parameter is one learned number. Here is every one of the model's 13,549,314, grouped by job.المعامل رقم واحد متعلَّم. وهذه معاملات النموذج كلها، 13,549,314، مجمّعة حسب وظيفتها.

Inside one encoder layerداخل طبقة مُرمِّز واحدة

Compared with other modelsمقارنةً بنماذج أخرى

Show the mathsاعرض الرياضيات

A linear layer from to numbers has weights and biases:الطبقة الخطية من إلى رقمًا فيها وزنًا و انحيازًا (Bias):

8

How it learnedكيف تعلّم

Two stages: Google pre-trained ELECTRA on general English, then we fine-tuned it to spot injections.على مرحلتين: درّبت Google نموذج ELECTRA مسبقًا على الإنجليزية العامة، ثم ضبطناه بدقة (Fine-tuning) ليكشف الحقن.

Stage 1: pre-training (ELECTRA)المرحلة 1: التدريب المسبق (ELECTRA)

BERT-style models learn by hiding 15% of the words and guessing them. ELECTRA does something more efficient called replaced token detection. A small generator swaps some words for plausible fakes, and the main model, the discriminator, must say for every token whether it is original or replaced. Learning from every position, not just 15%, is why a model this small can be this capable. Our guard starts from that discriminator.تتعلّم نماذج BERT بإخفاء 15% من الكلمات وتخمينها. أما ELECTRA فيفعل شيئًا أكفأ يُسمّى كشف الـ Token المستبدَل: مولِّد صغير يستبدل بعض الكلمات ببدائل معقولة، والنموذج الرئيسي، المميِّز، عليه أن يقول لـ كل Token هل هو أصلي أم مستبدَل. والتعلّم من كل موضع، لا من 15% فقط، هو سرّ قدرة نموذج بهذا الصغر. وحارسنا يبدأ من ذلك المميِّز.

Stage 2: fine-tuning (this project)المرحلة 2: الضبط الدقيق (هذا المشروع)

We added the classification head and trained the whole model on 20,446 labelled texts, checking the loss on 2,433 held-back validation texts four times per epoch. Training stopped once the validation loss had not improved for three checks in a row, and the best checkpoint (epoch 1.5) was kept. After that point the model starts memorising its training data: training loss keeps falling while validation loss rises.أضفنا رأس التصنيف ودرّبنا النموذج كله على 20,446 نصًا مصنَّفًا، مع فحص الخسارة على 2,433 نصًا محجوزًا للتحقق أربع مرات في كل حقبة (Epoch). وتوقف التدريب حين لم تتحسّن خسارة التحقق ثلاث فحوص متتالية، واحتفظنا بأفضل نسخة (الحقبة 1.5). بعد تلك النقطة يبدأ النموذج بحفظ بيانات تدريبه: تواصل خسارة التدريب الهبوط بينما ترتفع خسارة التحقق.

Show the mathsاعرض الرياضيات

Replaced-token detection, for a corrupted input with discriminator :كشف الـ Token المستبدَل، لمُدخل مُحرَّف ومميِّز :

Fine-tuning minimises the cross-entropy from chapter 6 with AdamW (learning rate , weight decay ):يقلّل الضبط الدقيق الإنتروبيا المتقاطعة من الفصل 6 باستخدام AdamW (معدل التعلّم ، وWeight decay ‏):

rises linearly over the first 6% of steps (warm-up), then decays linearly to 0. يرتفع خطيًا خلال أول 6% من الخطوات (الإحماء)، ثم يتناقص خطيًا حتى 0.

Replaced token detectionكشف الـ Token المستبدَل

illustrationتوضيح

Our fine-tuning runجولة الضبط الدقيق التي أجريناها

9

From a training notebook to your browserمن دفتر التدريب إلى متصفحك

The trained model was exported and compressed so it can run on this page without any server.صُدّر النموذج المدرَّب وضُغط ليعمل في هذه الصفحة دون أي خادم.

ONNX is a portable format that describes the model as a graph of maths operations. Your browser runs that graph with ONNX Runtime Web (compiled to WebAssembly), loaded through transformers.js.ONNX صيغة قابلة للنقل تصف النموذج كمخطط من العمليات الرياضية. ويشغّل متصفحك هذا المخطط بـ ONNX Runtime Web (المترجم إلى WebAssembly)، المحمَّل عبر transformers.js.

Quantization stores each weight matrix as 8-bit integers plus one scale factor, instead of 32-bit decimals. The file shrinks from 54.4 MB to 14.1 MB, and it gives the same verdict as the full-precision model on 3,369 of 3,383 test texts (99.6%).التكميم (Quantization) يخزّن كل مصفوفة أوزان كأعداد صحيحة بـ 8 بتات ومعامل تحجيم واحد، بدلًا من كسور بـ 32 بتًا. فيصغر الملف من 54.4 إلى 14.1 ميغابايت، ويعطي الحكم نفسه الذي يعطيه النموذج كامل الدقة في 3,369 من 3,383 نص اختبار (99.6%).

On the right are ten real weights from the first layer's attention, before and after this rounding.وهنا عشرة أوزان حقيقية من انتباه الطبقة الأولى، قبل هذا التقريب وبعده.

Show the mathsاعرض الرياضيات

Symmetric per-tensor quantization of a weight matrix :التكميم المتماثل لكل Tensor لمصفوفة أوزان :

Activations are quantized on the fly while the model runs ("dynamic" quantization), so only the weights need storing as integers.تُكمَّم التنشيطات أثناء عمل النموذج (التكميم «الديناميكي»)، فلا يلزم تخزين إلا الأوزان كأعداد صحيحة.

Ten real weights, rounded to int8عشرة أوزان حقيقية مقرّبة إلى int8

File sizeحجم الملف