█

AI/TLDR

मुझे पिलाएं कॉफ़ी
मुझे एक कॉफ़ी पिलाएँ

Encoder vs Decoder Models: Transformer Variants की तुलना

Transformer के तीनों बॉडी प्लान समझें, और जानें कि chat models, embedding models और translators अलग तरीके से क्यों बने होते हैं।

मध्यम12 मिनट पढ़ेंअपडेट 12 जून 2026

सरल भाषा में

Transformer आर्किटेक्चर के तीन अलग-अलग बॉडी प्लान होते हैं — encoder-only, decoder-only और encoder-decoder। किस मॉडल में कौन सा बॉडी प्लान है, इसी से तय होता है कि वह किस काम में अच्छा है। BERT encoder-only है; GPT, Claude और Llama decoder-only हैं; T5 और BART encoder-decoder हैं। अगर आपने कभी सोचा है कि टेक्स्ट को classify करने, generate करने या translate करने के लिए अलग-अलग मॉडल क्यों चुने जाते हैं, तो इसका जवाब यही है।

Encoder vs Decoder Models — डायग्राम
Encoder vs Decoder Models — linkedin.com

एक आसान उदाहरण लीजिए: मान लीजिए आपको किसी सवाल का जवाब देने के लिए एक दस्तावेज़ पढ़ना है। एक encoder पूरे दस्तावेज़ को एक साथ पढ़ता है — वह आगे और पीछे दोनों तरफ़ देख सकता है, और हर शब्द को उसके संदर्भ में समझ लेता है। एक decoder दस्तावेज़ पढ़कर आगे लिखता जाता है — वह केवल पहले आए शब्द देखता है और एक-एक करके नए शब्द generate करता है। एक encoder-decoder दोनों काम करता है: encoder स्रोत टेक्स्ट को गहराई से पढ़ता है, फिर वह समझ एक decoder को सौंपता है, जो शुरू से एक नया sequence generate करता है।

दोनों हिस्से एक ही बुनियादी building block से बने होते हैं: एक पर एक रखे गए transformer blocks (attention + MLP)। फ़र्क एक अहम बात में है — attention layer को क्या देखने की इजाज़त है — और इसी एक फ़र्क से मॉडल के तीन बिल्कुल अलग परिवार बनते हैं, जिनमें से हर एक अपनी जगह पर सबसे आगे है।

यह क्यों मायने रखता है

किसी काम के लिए गलत architecture परिवार चुनना, model चुनते या fine-tune करते समय, सबसे आम गलतियों में से एक है। semantic search के लिए text embedding बनवाने को कहा गया decoder-only LLM औसत नतीजे देगा; और paragraph generate करने को कहा गया encoder-only मॉडल यह काम कर ही नहीं सकता। तीनों परिवारों को समझने से आप सही tool चुन सकते हैं, और यह भी समझ सकते हैं कि vendors अलग-अलग प्रकार के मॉडलों के लिए अलग दाम क्यों लेते हैं।

एक practical builder के तौर पर यह तीन ठोस तरीकों से आपको प्रभावित करता है। पहला, embeddings और retrieval: vector databases, semantic search और RAG pipelines लगभग हमेशा encoder-based मॉडल इस्तेमाल करते हैं, क्योंकि bidirectional context ज़्यादा समृद्ध vector representations बनाता है। दूसरा, generation: आप जिस भी chat API को call करते हैं — GPT, Claude, Gemini, Llama, Mistral — वह decoder-only मॉडल है, और यह जानने से समझ आता है कि वे token-by-token generate क्यों करते हैं और लंबे जवाब ज़्यादा महंगे क्यों पड़ते हैं। तीसरा, structured transformation tasks (translation, summarization, code conversion): fine-tune किए जाने पर encoder-decoder मॉडल इन कामों में कहीं बड़े decoder-only मॉडलों को भी पीछे छोड़ सकते हैं, क्योंकि इनका architecture स्वाभाविक रूप से input को समझने और output generate करने को अलग करता है।

Frontier AI में रुझान लगभग हर काम के लिए decoder-only मॉडलों की ओर रहा है, जिसकी वजह scaling और instruction tuning की सफलता है। लेकिन encoder-only और encoder-decoder मॉडल कहीं से भी खत्म नहीं हुए हैं — search infrastructure, multilingual NLP pipelines, और वे सभी स्थितियाँ जहाँ एक छोटा, fine-tuned मॉडल लागत के मामले में एक बड़े prompted मॉडल को हराना चाहता है, इन्हीं पर अब भी इनका दबदबा है।

यह कैसे काम करता है

समझने वाला मुख्य mechanism है attention mask — एक matrix जो बताता है कि हर token किन दूसरे tokens पर attend कर सकता है। Mask बदल दीजिए, तो परिवार बदल जाता है।

Encoder-only: पूरी दृश्यता, कोई generation नहीं

Encoder-only मॉडल में attention mask पूरी तरह खुला होता है: हर token दोनों दिशाओं में हर दूसरे token पर attend कर सकता है। वाक्य "the bank by the river" में bank शब्द प्रोसेस करते समय मॉडल एक साथ the (उसके बाईं ओर) और river (उसके दाईं ओर) दोनों देखता है। यह bidirectional नज़रिया हर token का सबसे समृद्ध contextual representation देता है। इस मॉडल को masked language modeling (MLM) से pre-train किया जाता है — लगभग 15% tokens को बेतरतीब छिपाकर मॉडल से उन्हें भरवाया जाता है — जिससे वह हर दिशा के संदर्भ से भाषा सीखता है।

BERT (Google, 2018) canonical encoder-only मॉडल है। इनपुट की शुरुआत में [CLS] token लगाकर forward pass चलाया जाता है, और उस [CLS] position पर आया अंतिम vector पूरे इनपुट का सार माना जाता है — classification के लिए आदर्श। Embeddings के लिए, sentence-transformers (SBERT) BERT को siamese network तरीके से ढालता है, जो cosine similarity तुलना के लिए optimize किए गए dense vectors बनाता है। इसकी कीमत यह है कि bidirectional मॉडल नया टेक्स्ट generate नहीं कर सकते — अगला token बनाने की कोई व्यवस्था नहीं है, क्योंकि mask भविष्य देख लेता है।

Decoder-only: causal masking से generation संभव होता है

Decoder-only मॉडल एक causal mask (जिसे auto-regressive mask भी कहते हैं) लगाता है: position i केवल position 0 से i तक के tokens पर attend कर सकती है। भविष्य के tokens झाँकने की अनुमति नहीं है। हर step पर मॉडल वर्तमान position के बाईं ओर का सब कुछ देखता है और अगला token predict करता है। इस मॉडल को next-token prediction से pre-train किया जाता है: training corpus के हर prefix को देखकर अगला token predict करना। यह training objective बहुत अच्छी तरह scale होता है — एक ही forward pass sequence की हर position पर training signal देता है।

GPT-2 (OpenAI, 2019) ने दिखाया कि यह architecture अप्रत्याशित रूप से अच्छी तरह scale करता है। GPT, Claude, Llama, Gemini, Mistral, Qwen और DeepSeek परिवार — आज आप जितने भी बड़े generative मॉडल इस्तेमाल करते हैं, वे सब decoder-only transformer हैं — कभी-कभी grouped-query attention, SwiGLU activations या rotary positional embeddings (RoPE) जैसे बदलावों के साथ, लेकिन causal-masked architecture वही रहता है। Decoder-only design scaling की दौड़ जीता, क्योंकि next-token prediction अब तक खोजा गया सबसे data-efficient और hardware-friendly training objective है।

Encoder-decoder: मूल seq2seq डिज़ाइन

2017 का मूल transformer paper ("Attention Is All You Need") एक encoder-decoder मॉडल का वर्णन करता था, जो machine translation के लिए बनाया गया था। Encoder पूरे source sequence को bidirectional attention के साथ प्रोसेस करता है और इनपुट का समृद्ध contextual representation बनाता है। Decoder फिर output sequence को auto-regressively generate करता है — एक समय में एक token, अपने पिछले tokens पर causally masked — लेकिन हर decoder block में एक अतिरिक्त cross-attention layer होती है, जो उसे सीधे encoder के output पर attend करने देती है।

Cross-attention ही वह तरीका है जिससे decoder source से जुड़ा रहता है: हर generation step पर वह पूछता है, "मैं अभी जो लिख रहा हूँ, उसके लिए इनपुट का कौन सा हिस्सा सबसे ज़्यादा प्रासंगिक है?" और उसी हिसाब से उन्हें weight देता है। T5 (Google, 2020) ने हर NLP task को text-to-text के रूप में देखकर इस डिज़ाइन को हर काम पर लगाया। BART (Meta, 2019) ने इसी architecture पर denoising pre-training objective इस्तेमाल किया और abstractive summarization benchmarks पर रिकॉर्ड बनाए।

मॉडल परिवारों की तुलना

आर्किटेक्चरAttention प्रकारPre-training कार्यखूबियाँप्रसिद्ध मॉडल
Encoder-onlyBidirectionalMasked language modelingवर्गीकरण, embeddings, NLUBERT, RoBERTa, DeBERTa
Decoder-onlyCausal (बाएँ से दाएँ)अगला token predictionटेक्स्ट generation, reasoning, chatGPT, Claude, Llama, Mistral, Gemini
Encoder-decoderBidirectional (enc) + causal + cross (dec)Seq2seq denoising / text-to-textअनुवाद, summarization, structured outputT5, BART, mT5

कुछ बारीकियाँ जानने लायक हैं। पहली, decoder-only मॉडल classification भी कर सकते हैं — आप उन्हें classification head के साथ fine-tune करते हैं या prompt करते हैं। बस शुद्ध classification कामों पर encoder-only मॉडल की accuracy तक पहुँचने के लिए उन्हें आमतौर पर ज़्यादा parameters चाहिए। दूसरी, encoder-only मॉडल उन कामों के लिए छोटे और inference में तेज़ होते हैं जिनके लिए वे बने हैं: 110M parameters वाला BERT-base classifier अक्सर binary text classification के काम में 7B decoder-only मॉडल को उसकी लागत के एक हिस्से पर हरा देता है। तीसरी, encoder-decoder मॉडल data-efficient होते हैं: summarization dataset पर fine-tune किया गया मध्यम आकार का T5 अक्सर कहीं बड़े prompted LLM से बेहतर प्रदर्शन करता है, क्योंकि architecture encoder और decoder को अलग-अलग specialize कर सकता है।

Frontier AI में decoder-only मॉडल क्यों हावी हैं

जब encoder-decoder मॉडल ज़्यादा सिद्धांतगत लगते हैं — समझने के लिए अलग encoder, लिखने के लिए अलग decoder — तो पूरा frontier decoder-only पर क्यों आ गया? तीन कारण सबसे अहम हैं।

  1. ट्रेनिंग की दक्षता। N लंबाई के sequence पर decoder-only मॉडल एक forward pass में N training signals देता है — हर prefix अगला token predict करता है। Encoder-only मॉडल केवल masked positions (हर pass में लगभग 15% tokens) पर signal देता है। Decoder का training objective हर compute dollar से ज़्यादा signal निकालता है।
  2. सरलता बेहतर तरीके से scale होती है। Encoder-decoder मॉडल में संतुलित करने के लिए दो weight सेट होते हैं और tune करने के लिए एक cross-attention mechanism। Decoder-only मॉडल में एक समरूप stack होता है। जब आप अरबों डॉलर compute पर खर्च कर रहे हों, तो आप चाहते हैं कि टूटने के लिए कम engineering surfaces हों।
  3. Scale के साथ उभरती क्षमताएँ। अनुभवजन्य रूप से decoder-only मॉडल in-context learning, chain-of-thought reasoning और instruction-following क्षमताएँ दिखाते हैं, जो scale बढ़ने पर उभरती हैं — ऐसी क्षमताएँ जिनकी उम्मीद नहीं थी और जो encoder-decoder scaling में उतनी साफ़ तरह दोबारा नहीं दिखीं। Kaplan et al. के scaling laws decoder-only मॉडलों पर तैयार हुए थे, और ChatGPT बनाने वाली पूरी RLHF + instruction-tuning pipeline decoder-only architectures पर ही विकसित की गई थी।

इस पर शोध जारी है कि क्या bidirectional encoders समान रूप से scale होने पर मुकाबला कर सकते हैं। 2025 के एक paper (NV-Embed, NVIDIA से) ने दिखाया कि embedding के समय bidirectional attention चालू किए गए decoder-only LLM retrieval में state-of-the-art प्रदर्शन दे सकते हैं — पारंपरिक सीमा धुंधली करते हुए। लेकिन अभी के लिए ecosystem के tooling, hardware optimization (खासकर inference के लिए KV-cache) और जमा हुए training recipes, सब decoder-only मानकर चलते हैं। ऐसा मॉडल जो इस धारणा को तोड़ता है, उसे भारी switching costs का सामना करना पड़ता है।

और गहराई से

Decoder मॉडलों का bidirectional fine-tuning। Encoder और decoder के बीच की सख़्त सीमा अब नरम हो रही है। NV-Embed जैसे शोध दिखाते हैं कि अगर inference के समय causal mask हटा दिया जाए (ताकि decoder सभी positions को bidirectionally देखे), तो वही decoder-only मॉडल retrieval कामों के लिए BERT से बेहतर embedding मॉडल बन जाता है। कई labs के production embedding endpoints में यही इस्तेमाल हो रहा है। मुख्य समझ यह है कि causal mask एक training विकल्प है, architectural बाध्यता नहीं — असली attention mechanism तीनों परिवारों में एक जैसा है।

Prefix LM — बीच का रास्ता। एक चौथा, कम आम variant भी है: prefix language model। यह इनपुट ("prefix") पर bidirectional attention और generated output पर causal attention इस्तेमाल करता है। यह PaLM और कुछ T5 variants जैसे मॉडलों में इस्तेमाल होता है। इसमें prompt की encoder जैसी समझ और decoder जैसी generation मिलती है, बस training थोड़ी ज़्यादा जटिल हो जाती है।

KV cache और decoder-only inference। Causal mask का एक ठोस नतीजा यह है कि decoder-only inference को KV cache से भारी फ़ायदा होता है — पिछले tokens के लिए निकाले गए key और value tensors को संग्रहीत करना, ताकि हर नए token के लिए उन्हें दोबारा गणना न करनी पड़े। इसीलिए लंबा prompt generation की गति घटा देता है: cache अनुपात में बढ़ता है। Encoder-only मॉडलों में यह "incremental generation" वाली समस्या नहीं होती, और वे आमतौर पर बिना cache के एक ही forward pass चलाते हैं।

Decoder-only पर Mixture-of-Experts (MoE)। Decoder-only architecture MoE विस्तार के लिए ख़ास तौर पर अनुकूल साबित हुआ है — हर dense MLP layer की जगह कई "expert" sub-networks और एक सीखने वाला router लगाया जाता है, जो हर token के लिए केवल कुछ experts सक्रिय करता है। Llama 4 (2025) ने Mixtral और उससे पहले के मॉडलों के बाद MoE अपनाया। MoE कुल parameter count (और इस तरह model capacity) बढ़ाता है, जबकि हर token पर compute लगभग स्थिर रहता है। यह इसीलिए संभव है क्योंकि decoder-only stack blocks की एक साफ़, एकसमान श्रृंखला है — जिससे हर block को शर्तीय बनाना आसान होता है।

कब decoder-only LLM को prompt करने के बजाय encoder-decoder को fine-tune करें। सीमित sequence-to-sequence कामों पर (जैसे structured JSON को summary वाक्य में बदलना, दो संकीर्ण domains के बीच translate करना, natural language से SQL generate करना), fine-tuned T5-base (250M parameters) अक्सर frontier LLM prompting को उसकी inference लागत के बीसवें हिस्से पर और बेहतर consistency के साथ हरा देता है। Architecture का मेल — encoder पूरा इनपुट समेट लेता है, decoder ठीक वही generate करता है जो आप specify करते हैं — एक सामान्य-उद्देश्य मॉडल को prompt करने से कहीं ज़्यादा कसा हुआ inductive bias देता है।

अक्सर पूछे जाने वाले प्रश्न

BERT और GPT में architecture के स्तर पर क्या फ़र्क है?

BERT encoder-only है: यह bidirectional attention इस्तेमाल करता है और इनपुट के हर token को एक साथ देख सकता है, इसलिए टेक्स्ट को समझने और classify करने में यह उत्कृष्ट है। GPT decoder-only है: यह causal mask इस्तेमाल करता है ताकि हर token केवल पिछले tokens पर attend करे, इसलिए टेक्स्ट generate करने में यह उत्कृष्ट है। कोई भी architecture वस्तुनिष्ठ रूप से बेहतर नहीं है — वे अलग-अलग कामों के लिए optimize किए गए हैं।

ज़्यादातर LLM decoder-only क्यों होते हैं?

Decoder-only मॉडल एक forward pass में हर token position पर training signal देते हैं (next-token prediction), जिससे बड़े पैमाने पर training data और compute के लिहाज़ से वे बेहद कुशल होते हैं। इन्हें एक सरल, एकसमान architecture का फ़ायदा भी मिलता है, जो साफ़ तरीके से scale होता है और inference के समय KV cache के साथ अच्छी तरह मेल खाता है। अनुभवजन्य रूप से, instruction-following और chain-of-thought reasoning जैसी क्षमताएँ scaled decoder-only मॉडलों से सबसे साफ़ तौर पर उभरीं।

क्या decoder-only मॉडल text embeddings बना सकता है?

हाँ, लेकिन इसके लिए एक चाल चलनी पड़ती है। सामान्य causal-masked generation कमज़ोर embeddings देता है, क्योंकि हर token का representation केवल बाईं ओर के context को देखता है। Production embedding APIs (जैसे OpenAI का text-embedding-3 या NVIDIA का NV-Embed) या तो ख़ास pooling strategies इस्तेमाल करते हैं, या embedding pass के लिए causal mask अस्थायी रूप से बंद कर देते हैं, ताकि मॉडल bidirectionally attend करे — यानी embedding pass में decoder को प्रभावी रूप से encoder की तरह चलाया जाता है।

'seq2seq' का क्या मतलब है और यह encoder-decoder मॉडलों से कैसे जुड़ा है?

Seq2seq (sequence-to-sequence) किसी भी ऐसे काम को कहता है जिसमें मॉडल एक sequence इनपुट लेता है और उसकी जगह एक अलग sequence output देता है — translation, summarization और question answering इसके क्लासिक उदाहरण हैं। Encoder-decoder (जिसे seq2seq भी कहा जाता है) transformer मॉडल ठीक इसी के लिए बने हैं: encoder पूरे इनपुट को bidirectionally पढ़ता है, और decoder output को auto-regressively generate करता है, encoder के output पर cross-attention के साथ।

क्या summarization के लिए T5, GPT से बेहतर है?

समान मॉडल आकार पर, fine-tuned T5 अक्सर structured summarization benchmarks पर prompted GPT-class मॉडल से बेहतर प्रदर्शन करता है। BART ने अपने seq2seq denoising objective से abstractive summarization पर 6 ROUGE point का सुधार हासिल किया। फिर भी, instruction tuning वाले बहुत बड़े decoder-only मॉडल (GPT और Claude परिवार) zero-shot सेटिंग में ज़्यादा गुणवत्ता वाले summaries बना सकते हैं — इसमें समझौता लागत और latency बनाम fine-tuning की मेहनत के बीच है।

Encoder-decoder मॉडल में cross-attention क्या है?

Cross-attention हर decoder block के अंदर एक अतिरिक्त attention layer है। decoder केवल अपने पिछले tokens पर attend करने के बजाय encoder के output representations पर attend करता है। हर generation step पर decoder cross-attention से पूछता है: encoded इनपुट का कौन सा हिस्सा मैं अभी जो लिख रहा हूँ उससे सबसे ज़्यादा प्रासंगिक है? यही वह तरीका है जिससे translation मॉडल target लिखते समय source वाक्य के प्रति वफ़ादार रहते हैं।

आगे पढ़ें