Single-GPU Deployment and Inference Optimization for Medium-Scale Dense Large Models
This article explores practical strategies for deploying and optimizing medium-scale dense large language models on a single GPU. It covers memory budgeting, KV cache management for long contexts, low-bit compression trade-offs, multimodal input handling, and service-level memory validation to help developers efficiently run models locally.
1. Why Focus on Medium-Scale Dense Models?
Dense models with billions to tens of billions of parameters strike a balance between capability and cost. They outperform smaller models in multi-step reasoning, complex code generation, and long-document understanding, yet do not require multi-node, multi-GPU clusters. For small and medium-sized teams or independent developers, the ability to run a model on a single GPU is often the first deployment threshold.
Every parameter in a dense model participates in every forward pass, meaning inference compute and memory usage scale proportionally with the parameter count. Mixture of Experts (MoE) architectures only activate a subset of parameters per pass, reducing single-pass compute but introducing challenges like expert load imbalance and complex scheduling. When selecting a model, you should compare the costs of both architectures in the same table rather than focusing solely on total parameter count.
2. Memory Budget: Can It Fit?
Before deployment, estimate the memory footprint. Memory usage consists of four main parts: model weights, KV cache, activations and temporary buffers, and fixed framework runtime overhead. Weights are the easiest to calculate using the following formula:
Weight Memory (GB) ≈ Parameters (Billions) × Bits per Parameter ÷ 8
For a 31B parameter model:
- bfloat16 (16 bits per parameter): ~62 GB
- FP8 (8 bits per parameter): ~31 GB
- INT4 (4 bits per parameter): ~15.5 GB
When loaded in bfloat16, an 80GB GPU theoretically leaves about 18GB. Actual available memory must also deduct system and driver usage, leaving the remaining space to accommodate the KV cache, activations, and framework overhead. Therefore, long-context scenarios typically require compressing both weights and the cache.
3. KV Cache Determines Long Context Limits
The KV cache grows linearly with sequence length. Each token must save a set of Key and Value vectors in every layer. The calculation formula is:
KV Cache (GB) ≈ 2 × Layers × KV Heads × Head Dimension × Sequence Length × Batch Size × Bytes per Element ÷ 1e9
Here is a demonstration using hypothetical architectural parameters: 60 layers, 8 KV heads, 128 head dimension, sequence length 262144 (256K), and a batch size of 1. These numbers are for demonstration only; actual models should use their official configurations.
- 2 bytes per element (bfloat16): ~64.4 GB
- 1 byte per element (8-bit): ~32.2 GB
- 0.5 bytes per element (4-bit): ~16.1 GB
Under these assumptions, the cache for a 256K context in bfloat16 exceeds single-GPU capacity. If weights use FP8 (~31 GB) and the cache uses 8-bit (~32 GB), they total ~63 GB, leaving about 16 GB for activations and framework overhead. This illustrates a direct trade-off between context length and cache precision.
Grouped-Query Attention (GQA) structurally reduces cache size by having multiple query heads share fewer KV heads. During model selection, this parameter often determines long-context feasibility more directly than total parameter count.
4. Low-Bit Compression: Approaches and Trade-offs
Common methods to reduce memory usage fall into three categories:
- Weight Quantization: Maps weights from high precision to low-bit formats, reducing load size and memory bandwidth pressure.
- KV Cache Quantization: Stores key-value vectors in low-bit formats, usually calculating scale factors per channel or per group, and dequantizing upon read.
- Structural Optimization: Includes Grouped-Query Attention, sliding window attention, and cache eviction strategies, reducing saved states at the design level.
Each method has trade-offs. Low-bit representation introduces rounding errors, and tasks like long-range retrieval and multi-step mathematical reasoning are typically more sensitive than simple Q&A. Dequantization also adds computational overhead, which might offset some gains in high-concurrency scenarios. Therefore, you should not rely solely on general benchmark conclusions but compare output quality before and after compression using your own business samples.
5. Engineering Multimodal Inputs
If the model supports image inputs, images are encoded into a certain number of visual tokens. Higher resolutions yield more visual tokens, increasing memory usage and time-to-first-token latency. In practice, consider the following approaches:
- Set a maximum input resolution based on the task. For document images, crop the valid area first before sending it to the model.
- OCR and chart analysis are sensitive to resolution. It is recommended to extract a batch of samples and compare recognition accuracy at different resolutions to find the balance between precision and cost.
- Video inputs typically extract frames at a fixed frame rate and limit the total number of frames to prevent a single request from filling the context.
- The service layer should set an upper limit on the total visual tokens per request. If exceeded, return a clear error message rather than silently truncating.
6. Memory Checking in Service Deployment
Before going live, you can use a simple script for a rough capacity check. The code below demonstrates the calculation logic. The 6 GB overhead is an empirical reserved value that should be measured and adjusted based on the actual framework and batch size.
```python
def weight_gb(params_b: float, bits: float) -> float:
“””Weight memory (GB): Parameters (Billions) × bits ÷ 8”””
return params_b * bits / 8
def kv_cache_gb(layers: int, kv
