Introduction

The encoder in a transformer model processes input sequences (like text) into meaningful representations. While training focuses on updating model weights via backpropagation, inference is the phase in which the model is used to generate outputs or embeddings without updating its weights.

During inference, certain parameters can be set or adjusted to control performance, memory, and output characteristics.

During inference, an encoder:

  1. Takes tokenized input sequences.

  2. Processes embeddings through each encoder layer (multi-head attention + feed-forward layers).

  3. Outputs either:

    • Contextual token embeddings (for downstream tasks)

    • Attention weights (optional, for analysis)

    • Pooled representations (sentence or sequence-level embeddings)

Unlike training, no gradients are computed, so parameters affecting learning are ignored.

Tokenizer / Input-Level Parameters

These parameters control how input text is converted into tokens and fed to the encoder:

Purpose: Ensures sequences are represented correctly for attention computation and downstream outputs.

Attention-Level Parameters

These parameters govern the self-attention mechanism inside each encoder layer:

Purpose: Controls how the encoder focuses on different tokens and optionally exposes attention weights for interpretability or debugging.

Hidden-State / Layer-Level Parameters

These parameters control what is returned from each encoder layer:

Purpose: Provides flexibility in extracting embeddings for downstream tasks, e.g., sentence embeddings, intermediate representations, or analysis of layer-wise features.

Output / Post-Processing Parameters

These parameters control how the encoder's output is structured or used:

Purpose: Allows the user to select what output is needed, reducing memory use if only some components are required.

Example: Hugging Face Encoder

from transformers import BertTokenizer, BertModel
import torch

# ------------------------------------
# Tokenizer / Input-Level Parameters
# ------------------------------------

# Load tokenizer
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

# Encode input text
inputs = tokenizer(
    "Hello world! This is an example of encoder inference parameters.",
    return_tensors="pt",           # Return PyTorch tensors
    padding=True,                 # Pad to max length in batch
    truncation=True,              # Truncate sequences > max length
    max_length=20,                # Maximum token length
    add_special_tokens=True       # Add [CLS] and [SEP] tokens
)

# ----------------------------
# Attention-Level Parameters
# ----------------------------
# Inference attention options
attention_mask = inputs["attention_mask"]
head_mask = [None] * model.config.num_hidden_layers  # No heads masked

# -----------------------------------------
# Hidden-State / Layer-Level Parameters
# -----------------------------------------
# Choose which hidden states to return
output_hidden_states = True    # Return hidden states of all layers
use_cache = False              # Not using cache in pure encoder inference

# ----------------------------
# Output / Post-Processing Parameters
# ----------------------------
output_attentions = True       # Return attention scores
return_dict = True             # Return as dictionary for easier access

# ----------------------------
# Run Encoder Inference
# ----------------------------
outputs = model(
    input_ids=inputs["input_ids"],
    attention_mask=attention_mask,
    token_type_ids=inputs["token_type_ids"],
    position_ids=inputs["position_ids"],
    head_mask=head_mask,
    output_attentions=output_attentions,
    output_hidden_states=output_hidden_states,
    use_cache=use_cache,
    return_dict=return_dict
)

# ----------------------------
# Inspect Outputs
# ----------------------------
print("=== Output Shapes ===")
print(f"Last hidden state: {outputs.last_hidden_state.shape}")  # (batch, seq_len, hidden_size)
print(f"Number of hidden states: {len(outputs.hidden_states)}") # hidden states from all layers
print(f"Number of attention matrices: {len(outputs.attentions)}") # attention weights from all layers

Summary

Encoder inference parameters can be grouped as:

CategoryKey ParametersPurpose
Tokenizer / Input-Levelinput_ids, attention_mask, token_type_ids, position_ids, padding_token_idControls input representation and sequence handling
Attention-Levelnum_attention_heads, head_mask, attention_mask, output_attentionsControls focus of self-attention and exposes attention scores
Hidden-State / Layer-Leveloutput_hidden_states, return_dict, use_cacheDetermines what is returned from layers for analysis or embeddings
Output-Levellast_hidden_state, pooler_output, return_dict_in_generateControls the type and structure of outputs