Inferensys

Glossary

Cache Locality

Cache locality is an optimization principle for arranging computations and data access patterns to maximize the reuse of data stored in fast CPU cache memory, thereby reducing slower accesses to main memory (RAM).
Data engineer managing feature store on laptop, feature definitions visible, casual data engineering session.
ON-DEVICE INFERENCE OPTIMIZATION

What is Cache Locality?

Cache locality is a fundamental principle for optimizing data access patterns to maximize the reuse of data in fast CPU cache memory, directly reducing the performance penalty of accessing slower main memory (RAM).

Cache locality, also called data locality, is the principle of organizing computations and data structures to maximize the probability that the data a processor needs is already present in its small, fast cache memory. This is critical because accessing data from main RAM is orders of magnitude slower than from cache. High locality is achieved through temporal locality (reusing the same data repeatedly) and spatial locality (accessing data items that are stored close together in memory).

In on-device inference optimization, cache locality is engineered through techniques like loop tiling (blocking), memory layout transformations (e.g., NHWC vs. NCHW), and operator fusion. These methods minimize cache misses, which are expensive stalls where the CPU must wait for data from RAM. For small language models running on edge hardware, optimizing for cache locality is essential to achieve low-latency, energy-efficient execution within strict memory bandwidth constraints.

ON-DEVICE INFERENCE OPTIMIZATION

Key Characteristics of Cache Locality

Cache locality is a foundational principle for high-performance computing, especially critical for on-device inference where memory bandwidth is a primary bottleneck. It focuses on structuring data access and computation to maximize reuse within the CPU's fast cache hierarchy.

01

Temporal Locality

Temporal locality refers to the principle that data accessed recently is likely to be accessed again in the near future. Optimizing for this involves reusing data while it remains in cache.

  • Example: Reusing the same weight tensor across multiple input vectors in a batched matrix multiplication.
  • Impact: Reduces repeated, costly fetches from main memory (DRAM).
  • Implementation: Loop ordering that keeps frequently accessed data in inner loops.
02

Spatial Locality

Spatial locality refers to the principle that data elements stored close to each other in memory are likely to be accessed together. Optimizing for this ensures that fetching one piece of data brings adjacent, needed data into cache.

  • Example: Accessing consecutive elements of an array or a contiguous block of a feature map in a convolutional neural network.
  • Impact: Maximizes the utility of each cache line fetch (typically 64 bytes).
  • Implementation: Using contiguous memory layouts (e.g., NCHW/NHWC formats) and avoiding strided or random access patterns.
05

Data Layout Transformation

Data layout transformation involves rearranging how multidimensional data (like tensors) is stored in linear memory to create contiguous access patterns aligned with compute loops.

  • Common Formats: Converting from NCHW to NHWC format can provide better spatial locality for convolution operations on certain hardware.
  • Operation: Often performed by inference runtimes (e.g., TensorRT, ONNX Runtime) during graph optimization.
  • Goal: Minimize cache line waste and ensure sequential memory accesses during the innermost computation loops.
06

Impact on Model Architecture

Neural network model architecture choices have a profound impact on inherent cache locality. Efficient architectures are designed for predictable, sequential data flow.

  • Favorable Patterns: Convolutions with small kernels and dense linear layers exhibit strong spatial locality.
  • Challenging Patterns: Attention mechanisms with large, dynamic key-value caches can strain locality.
  • Co-Design: Hardware-aware Neural Architecture Search (NAS) often uses latency estimators that model cache behavior to find optimal architectures for a target device.
CACHE LOCALITY

Frequently Asked Questions

Cache locality is a foundational principle for optimizing computational performance, especially critical for on-device inference where memory bandwidth is a key bottleneck. These questions address its core concepts, implementation, and impact on AI systems.

Cache locality is a design principle that organizes data access and computational patterns to maximize the reuse of information stored in a processor's fast, but limited, cache memory, minimizing slower accesses to main memory (RAM). It is critically important for AI performance because neural network inference is dominated by memory-bound operations like large matrix multiplications (GEMM). Poor locality results in the processor waiting for data, a state known as a cache miss, which can stall computation and drastically increase inference latency. On edge devices with constrained memory bandwidth, optimizing for cache locality is often the difference between a model running in real-time or being unusable.

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.