Inferensys

Glossary

Positional Encoding

A technique used in non-recurrent sequence models like Transformers to inject information about the absolute or relative position of tokens in a sequence, preserving the temporal order of transactions.
MLOps engineer reviewing model serving infrastructure on laptop, container orchestration visible, technical workspace.
SEQUENCE ORDER PRESERVATION

What is Positional Encoding?

A mechanism for injecting information about the position of tokens in a sequence into non-recurrent architectures, preserving the temporal order of transactions.

Positional encoding is a technique that injects information about the absolute or relative position of tokens into the input embeddings of a sequence model, such as a Transformer. Because the self-attention mechanism is permutation-invariant and processes all elements in parallel, it has no inherent sense of order. Positional encoding resolves this by adding a unique signal to each token's representation, allowing the model to distinguish 'transaction A before transaction B' from the reverse, thereby preserving the critical temporal structure of a financial event stream.

The most common implementation uses sinusoidal functions of varying frequencies, generating a deterministic vector for each position that the model can learn to interpret. This allows the architecture to capture relative distances between any two transactions in a sequence. In fraud detection, this is essential for modeling the sequential nature of user behavior, enabling the model to recognize that a login followed by a password change and a high-value wire transfer is a fundamentally different pattern than the same events occurring in a different order.

TEMPORAL ORDER PRESERVATION

Key Features of Positional Encoding

Positional encoding is the mechanism that injects sequence order information into non-recurrent architectures. Without it, a Transformer perceives a sequence of transactions as a bag of events, losing the critical temporal structure needed to distinguish a legitimate spending pattern from a fraudulent one.

01

Sinusoidal Fixed Encoding

The original method from Attention Is All You Need uses sine and cosine functions of varying frequencies to encode position. Each dimension of the positional vector corresponds to a sinusoid.

  • Formula: PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
  • Key Property: Allows the model to easily learn to attend to relative positions, as PE(pos+k) can be represented as a linear function of PE(pos).
  • Benefit: No additional parameters to learn and can theoretically extrapolate to sequence lengths longer than those seen during training.
02

Learned Positional Embedding

An alternative approach where position representations are treated as learnable parameters, initialized randomly and updated via backpropagation. Common in BERT and GPT architectures.

  • Mechanism: A standard embedding layer maps integer indices [0, 1, ..., N] to dense vectors.
  • Constraint: The maximum sequence length is fixed at training time; the model cannot natively handle longer sequences without interpolation.
  • Use Case: Often preferred when the training data distribution of sequence lengths is well-defined and bounded, such as processing a fixed window of 100 prior transactions.
03

Relative Positional Encoding

Instead of encoding absolute position, this method modifies the self-attention mechanism to depend on the pairwise distance between tokens. This directly models the intuition that the relationship between transaction t and t-1 is more important than the absolute index of t.

  • Implementation: Often achieved by adding a learned bias to the attention score based on the offset between query and key positions.
  • Advantage: Naturally generalizes to sequence lengths unseen during training, making it robust for variable-length transaction histories.
  • Example: Transformer-XL and T5 relative position biases.
04

Rotary Position Embedding (RoPE)

A widely adopted method that encodes position by rotating the query and key vectors in the attention mechanism. The dot-product attention score then depends only on the relative distance between tokens.

  • Mechanism: Multiplies vectors by a rotation matrix where the rotation angle is a function of the position index.
  • Key Benefit: Seamlessly integrates absolute position information while naturally decaying the attention weight with increasing relative distance, providing a strong inductive bias for temporal locality.
  • Relevance: Used in Llama, Mistral, and other state-of-the-art models for processing long sequences.
05

AliBi (Attention with Linear Biases)

A simple yet effective method that adds a static, non-learned bias to the attention scores. The bias is a linear penalty proportional to the distance between tokens.

  • Formula: A constant slope m is subtracted from the attention score for each step of separation.
  • Advantage: Extremely computationally efficient with no learned parameters and has demonstrated strong extrapolation to sequences 2-3x longer than training length.
  • Application: Ideal for high-throughput fraud detection pipelines where inference speed on long transaction histories is critical.
06

Functional Time Encoding

An approach that maps continuous time stamps directly to vectors, moving beyond discrete sequence positions to model the actual inter-arrival times between transactions.

  • Method: Uses functions like cos(ω * t) or learnable neural networks to encode the real-valued time t of a transaction.
  • Significance: Captures the temporal density of events—a burst of 10 transactions in 1 minute is fundamentally different from 10 transactions over 10 hours, a distinction lost by discrete positional indexing.
  • Integration: Often concatenated with or added to the standard positional encoding to provide both sequential order and temporal rhythm information.
SEQUENCE ORDERING MECHANISMS

Absolute vs. Relative Positional Encoding

Comparison of the two primary approaches for injecting positional information into non-recurrent sequence models like Transformers, critical for preserving temporal order in transaction streams.

FeatureAbsolute Positional EncodingRelative Positional EncodingRotary Position Embedding (RoPE)

Core Mechanism

Adds a fixed or learned vector to token embeddings based on absolute index position (e.g., sine/cosine functions).

Modifies the attention computation to encode the relative distance between any two tokens, independent of absolute position.

Encodes relative position by rotating query and key vectors in embedding space, blending absolute and relative approaches.

Position Representation

Absolute index (0, 1, 2, ... n).

Relative offset (k tokens apart).

Relative offset via multiplicative rotation matrix.

Extrapolation to Longer Sequences

Translation Invariance

Computational Overhead

Negligible (element-wise addition).

Moderate (recomputes pairwise biases).

Low (applied directly to Q/K vectors).

Typical Use Case

Original Transformer, fixed-length sequences where absolute order is paramount.

Tasks requiring generalization to unseen sequence lengths, such as long document processing.

Modern large language models requiring efficient long-context scaling and stability.

Integration Point

Input embedding layer (pre-attention).

Attention score computation (within attention).

Query/Key projection (within attention).

Memory Footprint

O(n) for storing position vectors.

O(n^2) for pairwise bias matrix.

O(1) additional memory.

POSITIONAL ENCODING

Frequently Asked Questions

Clear, technically precise answers to the most common questions about how Transformers and other non-recurrent models preserve the sequential order of financial transactions.

Positional encoding is a technique for injecting information about the absolute or relative position of tokens into a non-recurrent sequence model. Unlike recurrent neural networks (RNNs) such as LSTMs or GRUs, which process data sequentially and inherently capture order, the Transformer architecture processes all tokens in parallel via the self-attention mechanism. This parallelism makes the model permutation-invariant—it sees a sequence as a set, not an ordered list. For financial fraud detection, where the temporal order of transactions (e.g., a small test transaction followed by a large transfer) is a critical signal, this is catastrophic. Positional encoding solves this by adding a unique positional signal to each token's embedding before it enters the attention layers, allowing the model to distinguish 'Transaction A then Transaction B' from 'Transaction B then Transaction A' and learn temporal patterns indicative of fraud.

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.