DL0063 Transformer Variable Length Sequences

How does the Transformer handle variable-length sequences?

Answer

A Transformer can process different sequence lengths because self-attention is defined over whatever number of tokens is supplied, up to the model’s context limit. For efficient batching, implementations usually pad sequences to a common length or group examples of similar lengths, then apply a padding mask so valid queries cannot attend to padded keys and values. Decoder-style models also add a causal mask that blocks future positions, while positional encodings identify token order within each sequence. Padded query outputs and losses must be ignored, and computation still scales with the padded batch length rather than only the number of valid tokens.

(1) Batch Construction: Sequences are dynamically padded to the longest item in a batch, bucketed by length, or packed by specialized kernels to reduce wasted work.
(2) Attention Masking: A key-padding mask removes padded keys and values from attention; causal models combine it with a triangular future mask.
(3) Length Boundary: Variable length does not mean unlimited length: positional support, memory, and the configured context window impose a maximum.

Variable-length token sequences padded into one batch with key-padding and causal attention masks.

Figure 1: Unequal token sequences become a rectangular batch; masking separates valid context, padding, and future positions.

Mathematical Formulation:
\mathrm{Attention}(Q,K,V)=\mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}+M_{\mathrm{pad}}+M_{\mathrm{causal}}\right)V
M_{ij}\in\{0,-\infty\}

Where:

  • Q, K, and V are query, key, and value matrices, and d_k is the key width used for score scaling.
  • M_{\mathrm{pad}} assigns -\infty to padded key positions so their softmax probabilities become zero.
  • M_{\mathrm{causal}} assigns -\infty when key index j is later than query index i; encoder-only models normally omit this mask.
  • M_{ij} denotes one combined mask entry and is either 0 for an allowed connection or -\infty for a blocked connection.
  • B is batch size and L_{max} is padded sequence length, giving a dense token tensor of shape B\times L_{max}.
Transformer variable-length processing flowchart from tokenization through masks and output cleanup.

Figure 2: A practical variable-length pipeline: tokenize, bucket or pad, build masks, run attention, then ignore padded outputs or continue autoregressive generation.


Login to view more content

Did you solve the problem?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *