On-Device AI Memory Optimization: A Deep Dive into Per-Layer Embeddings (PLE)
This article explains why on-device large models hit a memory wall and how Per-Layer Embeddings (PLE) address it by separating sparsely accessed parameters from densely accessed ones. It covers the structure of Transformer memory usage, memory tiering across accelerator memory, CPU main memory, and storage, and two complementary techniques—KV cache sharing and activation quantization. It also walks through the key engineering trade-offs around bandwidth versus capacity, hit rates across device tiers, quantization accuracy loss, and observability, giving developers a practical methodology for building efficient on-device AI applications.
1. Why On-Device AI Hits the “Memory Wall”
Over the past few years, multimodal large models have been steadily migrating from the cloud to the device: users want voice assistants, image understanding, real-time translation, and other capabilities to respond instantly even offline, while avoiding the privacy and latency problems that come with uploading data. However, a sharp conflict has long existed between model capability and memory footprint.
To understand this conflict, we need to start with the parameter structure of Transformer models. The memory usage of a typical decoder-only model can be roughly broken into three parts:
- Core weights: the parameters of attention layers (Q/K/V/O projections) and the feed-forward network (FFN). This part has a huge number of parameters and is accessed densely.
- Embedding and output layers: the input embedding and output projection (LM Head). Their size scales with vocabulary size and hidden dimension, and vocabularies often reach tens of thousands to over a hundred thousand entries—making this an “invisible heavyweight” in memory.
- Runtime overhead: the KV cache, activations, temporary buffers, and so on.
In the cloud, all of this can be handed to accelerators with large memory. But on-device, available memory is often squeezed down to 1-2GB and must be shared with the operating system and other apps. With a naive “cram every parameter into accelerator memory” strategy, a model with billions of parameters simply cannot start. This is precisely the problem that Per-Layer Embeddings aims to solve.
2. Core Idea: Separating “Dense Access” from “Sparse Access”
The starting point of Per-Layer Embeddings (PLE) is a long-overlooked observation: different parameters inside a model have drastically different access patterns.
Take the word embedding as an example. Although it holds a huge number of parameters, during a single inference only a small fraction of token vectors corresponding to the current input is actually accessed. That is, an embedding matrix of more than ten thousand rows may use only tens to hundreds of rows in a single forward pass. This “sparse, on-demand access” characteristic leaves enormous room for memory optimization.
The core idea of PLE can be summed up in one sentence: place large, infrequently used, sparsely accessed parameters (such as per-layer embeddings) in CPU main memory and load them on demand, while keeping densely accessed core Transformer weights that determine the main inference path resident in accelerator memory. By intelligently scheduling across storage tiers, the model’s effective memory footprint is significantly reduced.
In terms of effect, the goal of such tiering mechanisms is to let a model with N parameters occupy roughly as much accelerator memory as a model with about N/2 parameters—the so-called “run twice the parameters with half the memory.” This isn’t compression out of thin air; it moves the sparsely accessed part to a CPU memory tier that has larger capacity, lower cost, but slower access.
3. Memory Tiering: Scheduling Hot and Cold Data
The engineering landing point of PLE is memory tiering. Its scheduling logic can be summarized with a simple three-tier structure:
- Accelerator memory (Hot): stores parameters that are repeatedly accessed on the main inference path, such as attention weights and FFN weights, to guarantee high throughput.
- CPU main memory (Warm): stores sparsely accessed parameters such as the embedding table—large capacity, low cost.
- Down to storage if necessary (Cold): for extremely large embedding tables, they can even be placed on flash/disk and loaded only on a hit.
The inference workflow is as follows:
# Conceptual illustration: on-demand loading of per-layer embeddingsdef forward_layer(token_ids, layer_state, embedding_tables):# 1. The whole embedding table resides in CPU main memory; only fetch rows on demandrows = fetch_embedding_rows(token_ids, embedding_tables) # load only the rows for the current tokenshidden = gather(rows)# 2. Core Transformer weights stay resident in accelerator memory for dense computefor attn, ffn in layer_state.hot_weights:hidden = attn(hidden) # QKV + Attention + Ohidden = ffn(hidden) # feed-forward networkreturn hidden
The key is the first step: fetch_embedding_rows moves only the vectors actually needed by the current batch to the accelerator, while the remaining thousands of rows always stay on the CPU side. This “fetch on demand, discard after use” strategy reduces the memory footprint of the embedding part from “full” to “active subset,” while the compute-intensive core weights always enjoy high-speed access.
4. Two Key Complementary Techniques
Memory tiering alone isn’t enough to keep on-device inference smooth; it must work with two other kinds of optimization to form a combination punch.
1. KV Cache Sharing
During autoregressive decoding, every generated token requires reading the Key/Value caches of all previous tokens. As the sequence grows, the KV cache expands rapidly and squeezes memory. KV sharing techniques let multiple structurally similar layers reuse the same KV cache, or directly share the representation of the input embedding across multiple paths, thereby further reducing the size of tensors that must be resident in the accelerator at the same time under this tiering scheme, easing memory pressure.
2. Activation Quantization
Even when parameters are placed correctly, activations themselves occupy a considerable amount of memory in FP16/BF16. By quantizing activations to INT8 or even lower bit-widths, runtime memory can be compressed by more than half again. Combined with weight quantization, the bandwidth demand of the entire inference pipeline drops, so the on-device accelerator doesn’t have to frequently shuttle data between slow memory and compute units, and response latency decreases accordingly.
Combined, these three form an overall picture: on-demand tiered parameter loading + cache reuse + low-bit representation, together “stuffing” a large model into a hardware environment that was originally only big enough for a small model.
5. Key Trade-offs in Engineering Practice
In real deployment, several dimensions need careful balancing:
- Bandwidth vs. capacity: placing embeddings in CPU introduces data-movement overhead, so row accesses of the embedding should be batched and prefetched as much as possible to hide latency. Batch size and sequence length both affect the hit rate.
- Hit rate and device differences: high-end mobile SoCs have high CPU-accelerator interconnect bandwidth, so the tiering benefits are clear; low-end devices may be limited by bandwidth and need to shrink the active subset more aggressively.
- Quantization accuracy loss: INT8/INT4 quantization is usually manageable for text generation but may be more sensitive for multimodal tasks (especially fine-grained features related to image resolution), requiring per-layer sensitivity analysis to decide which layers can be quantized and which should keep high precision.
- Observability: on-device environments are hard to profile with fine granularity like in the cloud, so it’s recommended to integrate general logging and monitoring capabilities to collect metrics such as memory peaks, load hit rates, and end-to-end latency, forming a closed-loop tuning process.
In scenarios requiring more compute, on-device and cloud are often not either-or, but collaborative: the device carries high-frequency, low-latency requests in daily use through the tiering mechanism, while complex heavy tasks are sent back to the cloud, which has general capabilities such as container scheduling, elastic scaling, object storage, and message queues, to complete batch processing. The two are connected through a unified inference service layer.
6. Summary
The memory bottleneck of on-device multimodal AI is, in essence, not “too many parameters” but “memory tiers used too coarsely.” The value of the Per-Layer Embeddings mechanism lies in capturing the “sparse on-demand access” characteristic of embedding layers, and through hot-cold parameter separation, converting a large memory demand into a schedulable small memory demand. Then, adding KV cache sharing and activation quantization, it ultimately lets large models run locally, with low latency and privacy protection, on mobile devices.
For developers, mastering this methodology matters more than memorizing a specific model: analyzing parameter access patterns, designing sensible memory tiers, and finding the balance point between bandwidth and capacity is the general capability for building efficient on-device AI applications.
