Inferensys

Glossary

Transformer Architecture

A neural network architecture that relies entirely on a self-attention mechanism to process sequential data in parallel, replacing recurrence with positional encodings.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
FOUNDATIONAL SEQUENCE MODELING

What is Transformer Architecture?

A neural network design that processes entire sequences in parallel using self-attention, replacing recurrence with positional encodings to capture long-range dependencies.

The Transformer architecture is a neural network design that relies entirely on a self-attention mechanism to process sequential data in parallel, eliminating the sequential computation bottleneck of recurrent models. It computes a weighted contextual representation of every element in an input sequence simultaneously, enabling the capture of long-range dependencies without the vanishing gradient constraints of architectures like Long Short-Term Memory (LSTM) networks.

To preserve sequence order, the architecture injects positional encodings into input embeddings before the attention layers. The core multi-head self-attention block allows the model to jointly attend to information from different representation subspaces at different positions. This parallelizable design, introduced in the 2017 paper "Attention Is All You Need," forms the backbone of modern large language models and sequence-based recommenders like the Behavior Sequence Transformer (BST).

ARCHITECTURAL COMPONENTS

Key Features of the Transformer Architecture

The Transformer architecture revolutionized sequence modeling by discarding recurrence entirely in favor of a pure self-attention mechanism, enabling massive parallelization and capturing long-range dependencies with unprecedented efficiency.

01

Self-Attention Mechanism

The core computational unit that allows each position in a sequence to attend to all other positions simultaneously. For each token, the model computes Query (Q), Key (K), and Value (V) vectors through learned linear projections. The attention weight between token i and token j is derived from the scaled dot-product of Q_i and K_j, determining how much of V_j contributes to the output representation of token i.

  • Scaled Dot-Product Attention: Attention(Q,K,V) = softmax(QK^T / √d_k)V
  • The scaling factor √d_k prevents the softmax from entering regions of extremely small gradients
  • Enables direct modeling of dependencies regardless of their distance in the sequence
  • Contrasts sharply with RNNs, where information must flow sequentially through hidden states
02

Multi-Head Attention

Rather than performing a single attention function, the Transformer projects Q, K, and V h times with different learned linear projections, performing attention in parallel across multiple representation subspaces. Each head can learn to attend to different types of relationships—syntactic structure, semantic similarity, or positional proximity—simultaneously.

  • Typical configurations use 8 to 16 heads
  • Outputs of all heads are concatenated and projected back to the model dimension
  • Formula: MultiHead(Q,K,V) = Concat(head_1, ..., head_h)W^O
  • Each head operates on a reduced dimension d_k = d_model / h to keep total computational cost constant
03

Positional Encoding

Since the Transformer contains no recurrence or convolution, it has no inherent notion of token order. Positional encodings inject sequence order information by adding a unique vector to each input embedding. The original paper used sinusoidal functions of varying frequencies:

  • PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
  • PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
  • This allows the model to extrapolate to sequence lengths unseen during training
  • Learned positional embeddings are a common alternative, treating position as a trainable lookup table
  • Relative positional encodings (e.g., Rotary Position Embedding (RoPE)) encode the relative distance between tokens, improving length generalization
04

Feed-Forward Networks (FFN)

Each encoder and decoder layer contains a fully connected position-wise feed-forward network applied identically and independently to every token position. This component introduces non-linearity and increases the model's capacity to learn complex transformations.

  • Consists of two linear transformations with a ReLU or GELU activation in between: FFN(x) = W_2 * activation(W_1 * x + b_1) + b_2
  • The inner dimension is typically 4x the model dimension (e.g., 2048 for a 512-dim model)
  • Can be viewed as performing a per-token key-value memory lookup
  • SwiGLU and other gated variants have become standard in modern architectures like LLaMA and PaLM for improved training stability
05

Residual Connections & Layer Normalization

Each sub-layer (attention and FFN) is wrapped with a residual connection followed by layer normalization. The residual connection adds the sub-layer's input directly to its output, creating a gradient highway that mitigates the vanishing gradient problem in deep networks.

  • Post-LN: Normalization applied after the residual addition (original paper design)
  • Pre-LN: Normalization applied to the input before the sub-layer, then added to the residual. Pre-LN has become dominant as it improves training stability and convergence speed
  • The residual structure enables training Transformers with hundreds of layers
  • Formula: Output = LayerNorm(x + Sublayer(x)) for Post-LN; Output = x + Sublayer(LayerNorm(x)) for Pre-LN
06

Encoder-Decoder Architecture

The original Transformer uses an encoder-decoder structure for sequence-to-sequence tasks like machine translation. The encoder stack processes the entire source sequence bidirectionally, while the decoder generates the target sequence autoregressively.

  • Encoder: N identical layers, each with multi-head self-attention and FFN. All positions attend to all positions (bidirectional)
  • Decoder: N identical layers with three sub-layers: masked self-attention (prevents attending to future positions), cross-attention (attends to encoder output), and FFN
  • Cross-attention uses Q from the decoder and K,V from the encoder, allowing the decoder to focus on relevant source tokens at each generation step
  • Modern decoder-only models (GPT, LLaMA) discard the encoder entirely, relying solely on causal self-attention for autoregressive generation
TRANSFORMER ARCHITECTURE

Frequently Asked Questions

Clear, technical answers to the most common questions about the Transformer architecture, the foundational neural network behind modern large language models and sequential user behavior modeling.

The Transformer architecture is a neural network design that relies entirely on a self-attention mechanism to process sequential data in parallel, replacing recurrence with positional encodings. Introduced in the 2017 paper 'Attention Is All You Need,' it works by computing weighted representations of every element in an input sequence relative to all other elements simultaneously. The architecture consists of an encoder stack and a decoder stack, each composed of alternating layers of multi-head self-attention and position-wise feed-forward networks, wrapped with residual connections and layer normalization. Unlike recurrent neural networks that process tokens one step at a time, the Transformer ingests the entire sequence at once, enabling massive parallelization during training and capturing long-range dependencies without the vanishing gradient problem that plagues LSTMs.

SEQUENCE MODELING ARCHITECTURES

Transformer vs. LSTM vs. RNN: A Comparison

A technical comparison of the three primary neural architectures for processing sequential user behavior data, highlighting their mechanisms, computational properties, and suitability for session-based personalization tasks.

FeatureTransformerLSTMRNN

Core Mechanism

Self-attention with positional encoding

Gated cells (input, forget, output gates)

Recurrent hidden state with tanh activation

Parallel Sequence Processing

Handles Long-Range Dependencies

Mitigates Vanishing Gradient

Training Speed (Relative)

Fast (parallelizable)

Moderate

Slow (strictly sequential)

Memory Footprint per Layer

O(n²) attention complexity

O(n) with constant cell state

O(n) with single hidden vector

Positional Awareness Method

Sinusoidal or learned positional encodings

Implicit via sequential recurrence

Implicit via sequential recurrence

Interpretability of Attention

High (explicit attention weights)

Low (gating signals opaque)

Low (hidden state opaque)

SEQUENTIAL USER BEHAVIOR MODELING

Transformer Architecture in Retail Personalization

The Transformer architecture has revolutionized retail personalization by replacing recurrent neural networks with a parallelizable self-attention mechanism. This enables models to capture complex, long-range dependencies in user clickstreams and purchase histories, powering next-generation session-based recommenders and intent scoring engines.

01

Self-Attention: The Core Mechanism

Self-attention computes a weighted representation of every item in a user's behavioral sequence by dynamically assessing the relevance of each past interaction to every other interaction. Unlike recurrent models that process actions step-by-step, self-attention allows the model to directly connect a purchase made 50 clicks ago to the current browsing context in a single operation.

  • Query, Key, Value (QKV): Each item in the sequence is projected into three vectors. The attention weight between item i and item j is the scaled dot-product of the query for i and the key for j.
  • Scaled Dot-Product Attention: The raw attention scores are divided by the square root of the dimension (d_k) to prevent vanishing gradients from large dot products in high-dimensional spaces.
  • Parallelization: Because attention scores for all positions are computed simultaneously, Transformers train significantly faster on GPUs than LSTMs processing sequences token-by-token.
O(n²)
Time Complexity per Layer
02

Positional Encoding: Preserving Temporal Order

Since the Transformer has no inherent notion of sequence order, positional encodings are added to the input embeddings to inject information about the absolute or relative position of each user action. This is critical for retail sequences where the order of clicks—search, product view, add-to-cart, purchase—encodes the user's decision-making stage.

  • Sinusoidal Encoding: The original Transformer uses fixed sine and cosine functions of varying frequencies, allowing the model to extrapolate to sequence lengths unseen during training.
  • Learned Positional Embeddings: Many retail implementations, such as the Behavior Sequence Transformer (BST), learn a dedicated embedding vector for each position, which is trained jointly with the model.
  • Relative Positional Encoding: Variants like Transformer-XL encode the distance between pairs of items, which is more natural for modeling the relative recency of a click versus an absolute timestamp.
03

Multi-Head Attention: Capturing Diverse Behavioral Patterns

Multi-head attention runs multiple self-attention operations in parallel, each with its own learned linear projections. This allows the model to simultaneously attend to different aspects of a user's behavioral sequence.

  • Head 1 might learn to focus on short-term session intent, attending heavily to the last 3-5 clicks.
  • Head 2 might capture long-term brand affinity, linking the current session to a purchase made weeks ago.
  • Head 3 might identify price sensitivity patterns, correlating views of discounted items with cart abandonment.

The outputs of all heads are concatenated and projected, giving the subsequent layer a rich, multi-faceted representation of the user's sequential behavior.

05

Feed-Forward Networks: Per-Position Non-Linearity

After the multi-head attention sub-layer, each position's representation is passed through an identical position-wise feed-forward network (FFN). This consists of two linear transformations with a ReLU activation in between: FFN(x) = max(0, xW₁ + b₁)W₂ + b₂.

  • Role: While attention mixes information across positions, the FFN applies a non-linear transformation independently to each position, increasing the model's capacity to learn complex feature interactions.
  • Dimensionality: The inner hidden layer is typically 4x larger than the model dimension (d_ff = 2048 for d_model = 512), creating a bottleneck that forces the model to learn compressed, generalizable representations.
  • Retail Relevance: For a sequence of product views, the FFN can learn non-linear interactions between a product's category embedding and its price embedding at each time step.
06

Residual Connections & Layer Normalization

Each sub-layer (multi-head attention and feed-forward) is wrapped with a residual connection followed by layer normalization. This architectural choice is critical for training deep Transformers on long user sequences without degradation.

  • Residual Connection: The input to a sub-layer is added to its output: LayerNorm(x + Sublayer(x)). This provides a direct gradient path backward, mitigating the vanishing gradient problem in deep stacks.
  • Layer Normalization: Normalizes the activations across the feature dimension for each individual sequence position, stabilizing training and accelerating convergence.
  • Pre-LN vs. Post-LN: Modern implementations often apply layer normalization before the sub-layer (Pre-LN), which has been shown to improve training stability for very deep models used in large-scale retail catalogs.
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.