Per-Layer Embeddings: A Practical Approach to Memory Optimization for On-Device LLMs
Per-layer embeddings offer a practical way to run larger language models on memory-constrained devices. This article explains why embedding and output projection parameters dominate memory usage in transformer models, how per-layer placement keeps those parameters in host memory while reserving accelerator memory for the transformer core, what benefits and costs this trade-off involves, and which engineering practices—memory profiling, quantization, real-device measurement, and graceful degradation—matter most when deploying models on hardware with under 2GB of available memory.
1. The Memory Bottleneck on End Devices
Running large language models or multimodal models locally on phones, tablets, and wearables has become one of the main directions for AI adoption. Local inference brings lower response latency, less dependence on the cloud, and better privacy protection. But what usually holds large models back on-device is not compute—it is memory.
Physical memory on end devices is often very tight. A mainstream mobile device may have only a few hundred megabytes to one or two gigabytes of memory available, and the operating system plus background processes consume a sizeable share of that. A model in the billions-of-parameters range, by contrast, needs several gigabytes just to load its weights into memory—before counting the activations, KV cache, and other overhead generated during inference. The result: the model does not fit, or it barely fits and then repeatedly triggers paging and memory reclamation, so inference speed collapses or the model fails to start at all.
So the core problem of on-device deployment reduces to one sentence: fit the largest and most capable model possible into a limited memory budget. Per-layer embeddings is a typical engineering approach to that problem.
2. What Actually Fills Up Memory
Before optimizing memory, you need to see where it goes. The parameter count of a typical transformer-based model comes from three parts:
- Embedding layer: maps discrete tokens to dense vectors. When the vocabulary is large (hundreds of thousands of entries or more), the embedding matrix grows sharply and can account for a very significant share of parameters.
- Transformer body: the attention layers and feed-forward networks. This is the core of the model’s “thinking” and also the most compute-intensive part.
- Output projection layer (LM Head): maps the final hidden vectors back into vocabulary space. Its shape is closely tied to the embedding layer and is equally affected by vocabulary size.
The conventional approach is to push every parameter into the high-speed memory of an accelerator (GPU, NPU, and so on). The problem is that accelerator memory is not only small in capacity but also expensive and scarce. When large-but-not-compute-heavy parameters such as the embedding and output layers occupy that precious high-speed memory over the long term, the transformer layers that genuinely need fast compute get squeezed out.
This leads to a key insight: not all parameters have the same need to reside in high-speed memory.
3. The Core Idea Behind Per-Layer Embeddings
Per-layer embeddings is essentially a tiered parameter placement and loading mechanism. Most of the model’s parameters are hosted and scheduled efficiently by host memory on the general-purpose processor (CPU), while only the compute-intensive, frequently accessed core weights of the transformer stay in accelerator memory.
In one sentence, the design philosophy is: let scarce high-speed memory serve only the computation that needs it most, and let larger, cheaper memory handle storage and lookups.
In the inference flow, this mechanism typically works as follows:
- Embedding parameters remain resident in host memory, through memory mapping and similar techniques, rather than being copied into the accelerator as a whole.
- The embedding lookup for input tokens is performed on the processor side, producing the corresponding vector representations.
- Those vectors are then passed into the transformer layers held in accelerator memory for the main computation.
- At the output stage, part of the projection work can likewise be handled in a tiered fashion to reduce peak usage of high-speed memory.
Through this “divide and conquer” layout, the peak memory footprint during both model startup and inference is significantly reduced. As commonly observed in the industry, after this kind of tiered parameter management, a model with a nominally large parameter count can have an actual memory footprint approaching or even matching that of a model with a noticeably smaller parameter count—that is, delivering the capability of a “5B-class” model while consuming a memory footprint close to a “2B-class” model. The one-to-one binding between parameter count and memory usage is no longer rigid, which makes it possible to run larger models smoothly on devices with less than 2GB of memory.
It is worth stressing that such numerical relationships are empirical results for specific implementations and specific models, not universal laws. Gains differ noticeably across models, vocabulary sizes, and quantization strategies.
4. Why Focus on the Embedding Layer
Once you understand this design, it becomes clear why the optimization centers on embedding-related parameters:
- Large share: with a large vocabulary, the embedding and output projection are naturally the “parameter heavyweights,” so reducing their resident memory yields the most direct benefit.
- Friendly access pattern: an embedding lookup is essentially a sparse access that fetches a row by index. It suits on-demand loading via memory mapping and does not require repeated large-scale matrix operations on-chip.
- Relatively manageable compute intensity: compared with the dense matrix multiplications in the transformer body, lookups involve little computation and depend less on high bandwidth, so completing them on the processor side has a relatively limited impact on overall latency.
This is the elegance of the layered design: it places parameters according to their individual characteristics rather than applying a single rule to everything.
5. A Typical Implementation Sketch
The following illustrative pseudocode (for explaining the mechanism only, not a specific product interface) shows how tiered placement works in practice:
# Pseudocode: placing embedding parameters and transformer weights in different tiers# 1. Mount the embedding table into host memory via memory mapping so it does not occupy accelerator memory as a wholeembedding_table = mmap_load("embeddings.bin") # stays resident in CPU-side memory# 2. Load the transformer core weights into accelerator high-speed memorytransformer = load_to_accelerator("transformer_weights")def forward(input_ids):# 3. Perform the lookup on the CPU side to obtain the token vectorstoken_vectors = embedding_lookup(embedding_table, input_ids)# 4. Send only the activation vectors to the accelerator and run the transformer bodyhidden = transformer(token_vectors)# 5. Stage the output projection to control peak high-speed memory usagelogits = output_projection(hidden, embedding_table)return logits
In this logic, the only thing that truly stays resident in accelerator memory is the transformer body, while the bulky embedding_table remains on the processor side and is accessed on demand. This contrasts sharply with the traditional path of “stuffing everything into GPU memory.”
6. Trade-offs: Benefits and Costs
Every engineering optimization involves trade-offs, and per-layer embeddings is no exception. It is worth looking at it rationally.
Benefits:
- Substantially lower memory usage: moving large blocks of parameters out of scarce memory directly addresses the on-device fit problem.
- Larger runnable models: the same memory budget accommodates models with more parameters and stronger capability.
- Improved cost structure: less reliance on expensive high-speed memory makes deployment on low-cost devices easier.
Costs and caveats:
- Data movement overhead: parameters are spread across two memory tiers, adding a cross-memory transfer that may introduce extra latency. Mitigations include sensible memory mapping, batched lookups, and pipeline overlap.
- Higher implementation complexity: finer-grained memory management and scheduling logic is required, which demands more engineering capability.
- Gains are strongly model-dependent: vocabulary size, quantization scheme, and operator implementations all affect real-world results, so measure on your target model rather than copying conclusions wholesale.
7. Where It Fits and Engineering Recommendations
Per-layer embeddings is best suited to memory-constrained on-device scenarios where stronger model capability is desired, such as offline voice assistants, local document question answering, and real-time image understanding. For implementation, the following practices are recommended:
- Profile memory first: use tooling to quantify the actual footprint of the embedding layer, the transformer layers, and the output layer, identify the real memory heavyweights, and then decide where to focus optimization.
- Prioritize quantization: before layering parameters, evaluate a suitable quantization precision (for example, lower-bit representations). Quantization and layering often stack their gains.
- Measure, don’t assume: benchmark startup time, time-to-first-token, and sustained throughput on the target device to verify whether the data movement overhead is acceptable.
- Degrade gracefully: design fallback strategies for when memory is tight, such as automatically reducing batch size or switching to a smaller model branch, so that a minimum level of usability is preserved.
- Watch the surrounding ecosystem: use general engineering capabilities such as container platforms, monitoring and alerting, and logging services to continuously observe and optimize the lifecycle and performance of on-device applications.
8. Conclusion
Per-layer embeddings represents a pragmatic engineering mindset for on-device AI. Instead of always trying to cram a bigger model into a smaller device, it reallocates memory resources through fine-grained tiered parameter management so that every kind of parameter sits where it belongs. Its value lies not just in a specific numerical improvement, but in offering a transferable methodology: when facing resource constraints, first understand the nature of the bottleneck, then apply targeted layering and scheduling. As on-device hardware continues to evolve and quantization, sparsification, and related techniques mature, this kind of memory optimization approach will become a fundamental skill for deploying large models on-device.
