मूल शोधपत्र: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser और Illia Polosukhin, "Attention Is All You Need" (2017).
arXiv: https://arxiv.org/abs/1706.03762\ NeurIPS paper: https://papers.neurips.cc/paper/7181-attention-isall-you-need
इस explanation के बाद आप समझ पाएँगे:
यह वाक्य पढ़िए:
"रीना ने मीना को किताब दी क्योंकि वह परीक्षा की तैयारी कर रही थी।"
यहाँ "वह" किसके लिए है---रीना या मीना? सही उत्तर आसपास के शब्दों और पूरे वाक्य के अर्थ पर निर्भर करता है। भाषा समझने के लिए केवल एक शब्द को अलग से देखना पर्याप्त नहीं; हमें यह देखना होता है कि दूसरे शब्द उससे कैसे जुड़े हैं।
अब एक और वाक्य:
"बैंक के पास नाव खड़ी थी।"
यहाँ "बैंक" का अर्थ नदी का किनारा है, न कि पैसे रखने वाली संस्था। आसपास के शब्द अर्थ तय करने में मदद करते हैं।
मानव पाठक यह काम ध्यान देकर करता है। Transformer में attention mechanism प्रत्येक token को sequence के अन्य tokens से जानकारी लेने का एक सीखा हुआ तरीका देता है। वह हर बार केवल पिछले शब्द से आगे नहीं बढ़ता; attention के माध्यम से वह कई positions के बीच संबंध बना सकता है।
यह मनुष्य जैसी चेतना या समझ का प्रमाण नहीं है। यह एक गणितीय प्रक्रिया है, जो training के दौरान उपयोगी संबंधों के लिए parameters सीखती है।
भाषा, अनुवाद और speech जैसे कार्यों में data अक्सर sequence के रूप में आता है। उदाहरण के लिए:
I | am | learning | mathematics
Sequence में दो बातें महत्वपूर्ण हैं:
Transformer से पहले sequence-to-sequence कार्यों में Recurrent Neural Networks (RNNs), LSTMs/GRUs और Convolutional Neural Networks (CNNs) का व्यापक उपयोग होता था। Encoder-decoder models में attention को पहले से सहायक mechanism के रूप में भी जोड़ा जाता था।
RNN sequence को क्रम से process करता है। हर step पर वह वर्तमान token और पिछले step की hidden state का उपयोग करके नई hidden state बनाता है।
सरल रूप में:
ht=f(xt,ht−1)
यह तरीका क्रम को स्वाभाविक रूप से संभालता है, लेकिन सामान्य RNN computation में अगले step को पिछले step के परिणाम की प्रतीक्षा करनी पड़ती है। इसलिए एक ही sequence के positions को training के दौरान पूरी तरह parallel process करना कठिन होता है।
CNN स्थानीय आसपास के हिस्सों पर filters लगाकर features निकालता है। Sequence में दूर-दूर के tokens के बीच संबंध पकड़ने के लिए कई layers या विशेष व्यवस्था की जरूरत पड़ सकती है।
Vaswani और सहलेखकों ने पूछा: क्या sequence modeling के लिए recurrence और convolution को हटाकर, मुख्यतः attention पर आधारित architecture बनाया जा सकता है?
उनका उत्तर Transformer था: encoder-decoder architecture, जिसमें attention और position-wise feed-forward layers को stack किया जाता है। मूल paper ने machine translation पर इसका परीक्षण किया और training parallelization के लाभों को रेखांकित किया।
Computer शब्दों को सीधे मानव की तरह नहीं पढ़ता। पहले text को tokens में बाँटा जाता है। Token पूरा शब्द, शब्द का हिस्सा, punctuation या कोई अन्य token unit हो सकता है।
उदाहरण के लिए:
Transformers are useful
किसी tokenizer में यह tokens बन सकता है:
["Transform", "ers", "are", "useful"]
यह केवल illustrative example है; वास्तविक tokenization vocabulary और tokenizer पर निर्भर करती है।
फिर प्रत्येक token को एक numeric vector यानी embedding में बदला जाता है:
token→embedding vectorVector को संख्याओं की सूची समझें, जैसे:
0.12,;−0.31,;0.84,;…
ये संख्याएँ सीधे शब्दकोश की परिभाषाएँ नहीं हैं। Training के दौरान embeddings ऐसी representations सीखती हैं जो model के कार्य में उपयोगी हों।
मूल Transformer में model dimension (d_{model}=512) था। यानी हर token की embedding 512-dimensional vector थी।
Attention एक तरीका है जिसमें किसी position की representation, sequence की अन्य positions से मिली जानकारी का weighted combination बनती है।
एक classroom उदाहरण लें। कक्षा में शिक्षक एक प्रश्न पूछते हैं: "वह किसकी किताब थी?" जवाब देने के लिए आप वाक्य में उन शब्दों पर अधिक ध्यान देंगे जो "वह" और "किताब" के संबंध को स्पष्ट करते हैं। आप हर शब्द को समान महत्व नहीं देंगे।
Transformer में attention weights यह तय करते हैं कि किसी token की नई representation बनाते समय किन अन्य tokens की Value कितनी मात्रा में शामिल होगी।
महत्वपूर्ण: Attention weight को "मॉडल ने मनुष्य की तरह सचमुच ध्यान दिया" का सीधा प्रमाण नहीं मानना चाहिए। यह गणना में इस्तेमाल होने वाला learned weighting mechanism है।
Attention को समझाने के लिए तीन नाम उपयोग किए जाते हैं:
ये नाम समझने के लिए analogy हैं। तकनीकी रूप से Q, K और V input representations पर learned linear projections लगाने से बनते हैं।
मान लें sequence में token representations की matrix (X) है:
Q=XWQ,K=XWK,V=XWVˆ
यहाँ (W^Q), (W^K), (W^V) learned weight matrices हैं। अलग-अलग projections एक ही input को matching और information-transfer के लिए अलग representation spaces में बदलती हैं।
मूल paper का मुख्य attention operation है:
Attention(Q,K,V)=softmax(dkQKT)Vइसे चरणों में समझें।
QKTˆ
यह dot products के माध्यम से हर query और keys के बीच compatibility scores निकालता है। Score अधिक होने का अर्थ है कि उस query-key pair को इस learned matching space में अधिक compatibility मिली।
dkQKT
(d_k) key vector की dimension है। Dimensions बड़ी होने पर unscaled dot products का magnitude बढ़ सकता है। इससे softmax बहुत तीखे probabilities दे सकता है और gradients छोटे हो सकते हैं। (dk) से divide करना scores को अधिक उपयोगी scale में रखने का तरीका है।
Softmax scores को ऐसे weights में बदलता है जिनका योग प्रत्येक query के लिए 1 होता है:
αi=∑jesjesi
इससे हर key-position को एक normalized weight मिलता है।
∑iαiVi
अब Values को उनके attention weights के अनुसार मिलाया जाता है। अधिक weight वाली Value output में अधिक योगदान देती है।
मान लें किसी query को तीन positions से weights मिले:
Position Attention weight
(V_1) 0.70 (V_2) 0.20 (V_3) 0.10 कुल 1.00
Output होगा:
0.70V1+0.20V2+0.10V3
इसका अर्थ है कि इस query की output representation में (V_1) का योगदान सबसे अधिक है। यह केवल समझाने के लिए चुने गए weights हैं; किसी वास्तविक model के weights input और learned parameters से निकलते हैं।
जब Q, K और V एक ही sequence की representations से बनते हैं, तो इसे self-attention कहते हैं। इससे sequence का हर token उसी sequence के अन्य tokens से जानकारी ले सकता है---उपलब्ध mask के अधीन।
उदाहरण: "The animal didn't cross the street because it was tired."
Self-attention model को "it" और "animal" के बीच संबंध सीखने का अवसर देता है। यह उदाहरण संभावित संबंध दिखाता है; model का वास्तविक निर्णय context और training पर निर्भर करेगा।
Cross-attention में Query एक sequence से आती है और Keys/Values दूसरी sequence से।
मूल encoder-decoder Transformer में decoder की Query, encoder के output से बनी Keys और Values को देखती है। Translation में decoder इस तरह source sentence की जानकारी का उपयोग कर सकता है, जब वह target sentence के अगले token की prediction करता है।
यदि एक attention operation पूरे representation space में संबंध निकाले, तो वह कुछ प्रकार की जानकारी को एक ही मिश्रण में समेट सकता है। Multi-Head Attention कई अलग learned projections पर attention parallel चलाता है।
headi=Attention(QWiQ,KWiK,VWiVˆ)फिर heads के outputs concatenate करके एक और learned projection लगाई जाती है:
MultiHead(Q,K,V)=Concat(head1,…,headh)WOˆमूल paper के base Transformer में:
क्योंकि (8 ×64=512), heads के outputs concatenate होकर model dimension के अनुरूप बनते हैं।
इसे कई अलग "देखने के तरीकों" की analogy से समझ सकते हैं। लेकिन यह निश्चित नहीं कि हर head एक साफ़-सुथरा मानवीय grammatical role ही सीखता है। Heads के व्यवहार को interpret करना अलग research question है।
RNN में sequence order computation में स्वाभाविक रूप से शामिल होता है। लेकिन self-attention अपने आप token order नहीं बताता। यदि tokens की representations में position की जानकारी न दी जाए, तो attention अकेले sequence के permutation को पहचानने में सक्षम नहीं होगा।
मूल Transformer ने token embeddings में positional encodings जोड़े:
Input representation=Token Embedding+Positional EncodingPaper ने अलग-अलग frequencies वाले sine और cosine functions इस्तेमाल किए:
PE(pos,2i)=sin(100002i/dmodelpos) PE(pos,2i+1)=cos(100002i/dmodelpos)जहाँ:
सरल analogy: token embedding बताती है "कौन-सा token है", और positional encoding model को "यह token sequence में कहाँ है" की अतिरिक्त सूचना देती है।
Paper ने learned positional embeddings का भी परीक्षण किया और अपने प्रयोगों में sinusoidal तथा learned versions के परिणाम लगभग समान पाए। Sinusoidal encoding चुनने का एक कारण यह hypothesis था कि यह training में देखी गई लंबाई से आगे sequence lengths पर generalization में मदद कर सकती है। इसे हर आधुनिक model के लिए guaranteed property न समझें।
2017 के मूल Transformer में दो मुख्य हिस्से थे:
मूल base configuration में encoder की 6 layers और decoder की 6 layers थीं।
Encoder layer का सरल
flow`Input representations` → `Multi-Head Self-Attention` → `Add & Norm` → `Feed-Forward Network` → `Add & Norm`
Decoder layer का सरल
flow`Previous output tokens` → `Masked Self-Attention` → `Add & Norm` → `Encoder–Decoder Cross-Attention` → `Add & Norm` → `Feed-Forward Network` → `Add & Norm`
हर encoder layer में दो sub-layers होते हैं:
हर sub-layer के आसपास residual connection और layer normalization रखे गए थे।
हर decoder layer में तीन sub-layers होते हैं:
इससे decoder पहले target sequence के उपलब्ध हिस्से को देखता है और फिर source sequence की encoded जानकारी से संबंध बनाता है।
Autoregressive generation में model को अगला token predict करना होता है। Training के दौरान decoder को यह अनुमति नहीं होनी चाहिए कि position (i), उससे आगे के सही target tokens को देख ले।
इसलिए decoder self-attention में causal mask लगाया जाता है। Future positions के attention scores को softmax से पहले निषिद्ध कर दिया जाता है।
तीन target positions का illustrative mask:
Query position Position 1 Position 2 Position 3
1 देख सकता है छिपा छिपा 2 देख सकता है देख सकता है छिपा 3 देख सकता है देख सकता है देख सकता है
यह mask सुनिश्चित करता है कि position 1 भविष्य के token 2 और 3 का उपयोग न करे, position 2 token 3 को न देखे, इत्यादि।
Encoder self-attention में यही causal restriction आवश्यक नहीं होती, क्योंकि encoder input sequence के सभी tokens को एक साथ देख सकता है। Decoder की cross-attention encoder के पूरे source representation को देख सकती है।
Attention दूसरे tokens से जानकारी मिलाने का तरीका देता है। उसके बाद प्रत्येक position पर एक Feed-Forward Network (FFN) लागू होता है:
FFN(x)=max(0,xW1+b1)W2+b2
मूल paper में यह दो linear transformations और बीच में ReLU activation से बना था। यह हर position पर अलग-अलग लागू होता है, लेकिन एक ही layer के सभी positions के लिए parameters साझा होते हैं।
Base Transformer में:
आसान भाषा में: Attention sequence में दूसरी positions से relevant information मिलाता है; FFN उस position की representation को आगे transform करता है।
गहरी neural networks को train करना कठिन हो सकता है। Transformer में sub-layer के output को उसके input के साथ जोड़ने के लिए residual connection इस्तेमाल किया गया:
LayerNorm(x+Sublayer(x))Residual connection information और gradient के लिए एक अतिरिक्त रास्ता देता है। Layer normalization activations को व्यवस्थित करने में मदद करती है। इन्हें अकेले training की सफलता की गारंटी नहीं मानना चाहिए; ये पूरे architecture और optimization recipe के हिस्से हैं।
मूल paper ने machine translation के लिए encoder-decoder Transformer को train किया। Model target sequence के अगले token की probability predict करता था। Training के दौरान predicted distribution और सही target token के बीच loss निकाला जाता है, और backpropagation/optimizer के माध्यम से parameters update किए जाते हैं।
Paper की training recipe में Adam optimizer, learning-rate schedule, dropout और label smoothing जैसी तकनीकें शामिल थीं। Base model के लिए authors ने (d_{model}=512), (d_{ff}=2048), 8 heads और 6 encoder/decoder layers का उपयोग किया।
यहाँ "attention" का अर्थ यह नहीं कि model बिना training के संबंध जानता है। Q/K/V projections, embeddings और FFN के parameters data से optimize किए जाते हैं।
RNN में सामान्यतः sequence की position (t) की hidden state, position (t-1) की state पर निर्भर करती है। इससे positions के बीच sequential dependency बनती है।
Transformer का self-attention एक layer में सभी positions के pairwise scores matrix operations के माध्यम से निकाल सकता है। इसलिए training के दौरान sequence positions का बड़ा हिस्सा parallel process किया जा सकता है, जो accelerator hardware पर उपयोगी है।
लेकिन इसका अर्थ यह नहीं कि Transformer की हर computation सस्ती है। Standard self-attention में sequence length (n) के साथ attention-score matrix का आकार (n ×n) होता है। इसलिए लंबे sequences पर memory और computation की लागत बढ़ सकती है।
मूल paper ने मुख्य रूप से WMT 2014 English-to-German और English-to-French machine translation benchmarks पर Transformer का मूल्यांकन किया।
Paper के abstract में authors ने रिपोर्ट किया:
BLEU machine translation के outputs की reference translations से तुलना करने वाला एक automatic evaluation metric है। यह उपयोगी है, लेकिन अकेले translation quality, factual correctness या भाषा की सभी विशेषताओं को नहीं मापता।
इन परिणामों को paper के समय और उन्हीं benchmarks/configurations के संदर्भ में पढ़ना चाहिए। इन्हें आज के हर model या हर language task पर लागू नहीं किया जा सकता।
शीर्षक का आशय यह था कि sequence transduction के लिए प्रस्तावित architecture में recurrence और convolution की जगह attention को केंद्रीय computation बनाया जा सकता है।
इसका अर्थ यह नहीं है कि Transformer में attention के अलावा कोई operation नहीं होता। मूल architecture में embeddings, positional encoding, feed-forward networks, residual connections, normalization और output projection/softmax भी शामिल हैं।
अधिक सटीक रूप में कहें तो: Transformer ने sequence modeling की मुख्य inter-token interaction mechanism के रूप में attention का उपयोग किया और recurrent/convolutional sequence processing को हटाया।
आज के कई बड़े भाषा मॉडल Transformer से प्रेरित हैं, लेकिन वे मूल 2017 encoder-decoder architecture की हूबहू प्रतियाँ नहीं हैं।
विषय मूल Transformer (2017) कई आधुनिक decoder-only LLMs
मुख्य उद्देश्य Machine translation अगला token predict करके सहित text generation और अन्य sequence-to-sequence tasks tasks
Architecture Encoder + Decoder अक्सर decoder-only
Attention Encoder self-attention, आम तौर पर causal masked decoder self-attention self-attention, cross-attention
Position information Sinusoidal positional अलग-अलग learned या अन्य encoding positional methods संभव
Scale Base model में 6+6 कई आकारों और layers, configurations में (d_{model}=512)
Training Paper की बड़े और विविध datasets, translation-focused अलग objectives/recipes recipe
यह table broad architectural distinction दिखाती है; हर आधुनिक LLM एक ही design का उपयोग नहीं करता।
Transformer ने sequence modeling में महत्वपूर्ण बदलाव किया, लेकिन इसकी सीमाएँ भी हैं।
"मान लो computer को एक वाक्य का अनुवाद करना है। पुराने recurrent model में वह अक्सर शब्दों को क्रम से process करता था। Transformer ने एक अलग तरीका दिया: हर token को दूसरे tokens से सीधे जानकारी लेने का अवसर मिलता है। इसे attention कहते हैं। Attention में Query और Key से matching score बनता है, Softmax उन scores को weights में बदलता है, और Values का weighted sum नया representation बनाता है। कई attention heads अलग learned projections पर यह काम करते हैं। क्योंकि attention अपने आप word order नहीं जानता, positional encoding जोड़ी जाती है। मूल Transformer में Encoder input को represent करता है और Decoder output sequence बनाता है। Decoder mask के कारण भविष्य के target tokens नहीं देख सकता। इस architecture ने translation training को अधिक parallelizable बनाया, लेकिन इसका मतलब यह नहीं कि model मानव की तरह सोचता है या attention ही उसके अंदर की हर प्रक्रिया है।"
Attention Is All You Need ने 2017 में Transformer architecture प्रस्तुत किया, जिसने sequence modeling के लिए attention को केंद्रीय भूमिका दी और recurrent/convolutional sequence processing के बिना encoder-decoder model बनाया। इसकी प्रमुख तकनीकी अवधारणाएँ scaled dot-product attention, multi-head attention, positional encoding, masking, feed-forward layers और residual connections हैं।
इस paper की मुख्य सीख यह है कि sequence के हर token की representation बनाते समय दूसरे tokens से संबंधों को सीधे और parallel तरीके से model किया जा सकता है। साथ ही, Transformer केवल attention का एक सूत्र नहीं है---यह कई components, training choices और architectural assumptions का संयोजन है।
एक पंक्ति में: Transformer ऐसा neural network architecture है जो sequence के tokens के बीच learned attention संबंधों का उपयोग करके contextual representations बनाता है और मूल paper में इन्हीं सिद्धांतों से machine translation किया गया।
लेखन-संबंधी नोट: Q/K/V की "प्रश्न--मिलान--जानकारी" वाली व्याख्या और classroom examples intuition के लिए हैं। Equations तथा architecture मूल paper पर आधारित हैं; आधुनिक LLMs के बारे में कथन व्यापक प्रवृत्तियों का सार हैं, हर model पर लागू होने वाले नियम नहीं।