Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Core concepts

Encoder and Decoder

About 3890 characters Updated Nov 30, 2026 23 views AI wiki

The encoder (Encoder) and the decoder (Decoder) are a pair of basic components in sequence-to-sequence modeling: the encoder compresses the raw input into an internal representation, and the decoder then generates the output step by step from that representation. Together they form the backbone of mainstream architectures from machine translation to dialogue generation, and they are also the key coordinate for understanding the different variants of the Transformer family.

Core Principles

The encoder is responsible for "reading". It takes the input sequence, passes it through several layers of transformation (self-attention and feed-forward networks) and maps the variable-length input into a set of context vectors that are either of fixed length or as long as the input. In early recurrent network designs the encoder compressed the whole sentence into a final hidden state, creating a severe information bottleneck; after the attention mechanism was introduced, the decoder could access all of the encoder's outputs directly at every step, greatly relieving the problem.

The decoder is responsible for "writing". It usually works autoregressively: at each step it predicts the next token from the prefix generated so far and appends that token to the input, moving forward in a loop. To keep training and inference consistent, the decoder's self-attention must use a mask that blocks future positions, preventing the model from peeking at the answer when predicting the current position. In the encoder-decoder structure the decoder layer also contains a cross-attention sublayer that uses the decoder-side state as the query to read the encoder-side output.

In terms of information flow, the encoder is bidirectional and can process a whole input in parallel at once, while the decoder is unidirectional and must generate in order. This asymmetry determines the difference in engineering characteristics between the two.

Key Points

The first point is architectural variants. The encoder-only representative is BERT, which excels at understanding tasks such as classification, extraction and retrieval; the decoder-only representatives are GPT and the LLaMA family, which excel at generation; the full encoder-decoder structure is represented by T5 and BART, suited to tasks such as translation and summarization where both input and output are long. Which structure to choose essentially depends on whether the task leans more toward understanding or more toward generation.

The second point is the training objective. Encoder-style models commonly use masked language modeling, randomly hiding some tokens for the model to restore; decoder-style models use next-token prediction; encoder-decoder models commonly use denoising or span reconstruction, hiding spans and requiring sequence generation ability.

The third point is the difference in inference efficiency. Autoregressive generation in the decoder cannot be parallelized and requires a full forward pass for every generated token, which is why optimizations such as speculative decoding and key-value caching matter especially for decoders. The encoder is naturally parallel and computes more fully.

The fourth point is the correct use of attention masks. The encoder uses a bidirectional fully visible mask, the decoder's self-attention uses a causal mask, and cross-attention applies no causal restriction. Getting the mask wrong causes a mismatch between training and inference behavior and is one of the most common implementation errors.

Common Misconceptions

One misconception equates "decoding" with "translating into natural language", when the decoder is only the component that maps an internal representation to output tokens and whether the output is readable has nothing to do with humans. A second is to believe the encoder-decoder structure is necessarily better than a single decoder; in fact after large-scale pretraining the pure decoder architecture has become mainstream for its structural simplicity and scalability, while the encoder-decoder still has advantages on specific tasks but is not universally better. A third is to ignore the information bottleneck: whatever the structure, compressing a long input into a limited representation loses detail.

Application Scenarios

Machine translation, speech recognition and speech synthesis, text summarization and rewriting, image caption generation, reranking in retrieval-augmented generation, code completion, and cross-modal conversion such as the conditional encoding used in text-to-image generation.