Local Deployment of Large Models on a 12GB GPU: Model Selection, Parameters, and Benchmarking

    A comprehensive guide to locally deploying large language models on 12GB consumer GPUs like the RTX 3060 using llama.cpp. It covers model and quantization selection, VRAM budgeting for KV Cache, server setup, benchmarking prefill and generation speeds, and troubleshooting common deployment issues.

    1. Define Goals and Hardware Boundaries

    This article is intended for consumer-grade graphics cards with 12GB of VRAM, such as the RTX 3060 12GB. Although these cards are rated for 12GB, the system and desktop environment will occupy a portion of the VRAM, leaving approximately 11GB available for the model. You can verify this by checking the “Total” and “Used” values using nvidia-smi on your machine.

    The goal is specific: to use llama.cpp on your local machine to launch an OpenAI-compatible API server capable of handling coding, summarization, and Q&A tasks, and to measure the generation speed using a consistent method. This article does not provide fixed tokens/second metrics, as speed depends on the GPU model, VRAM bandwidth, quantization format, context length, and CPU memory. Readers should rely on measurements taken on their own hardware.

    2. Model and Quantization Selection

    Whether a model fits into VRAM depends primarily on the size of the weights file. The quantization format determines the file size, with common options including Q4_K_M, Q5_K_M, and Q8_0. Q4_K_M has the smallest footprint; while it shows noticeable precision loss, it remains usable for most tasks. Q8_0 is closer to the original precision but is roughly 1.7 times the size of Q4_K_M.

    The file sizes in the table below are approximate; refer to the download page for exact values.

    Model Quantization File Size (Approx.) Fits Entirely in 12GB VRAM
    Qwen2.5-14B-Instruct Q4_K_M ~9GB Yes, remaining space for KV Cache
    Qwen2.5-14B-Instruct Q5_K_M ~10.5GB Barely, context must be limited to ~4096
    Qwen2.5-14B-Instruct Q8_0 ~15.7GB No, some layers must be offloaded to system RAM
    Qwen2.5-32B-Instruct Q4_K_M ~20GB No, partial offloading required

    The core principle is to prioritize fitting the entire model into VRAM. When a model cannot fit entirely, the remaining layers are kept in system memory for CPU computation, which significantly decreases generation speed.

    Qwen provides GGUF format models on Hugging Face under the repository name Qwen/Qwen2.5-14B-Instruct-GGUF. Before downloading, please read the model license to ensure it permits your intended use case.

    3. Installation and Startup

    Step 1: Install a CUDA-enabled version of llama.cpp. You can either compile it yourself or use the pre-compiled binaries from the releases page. After installation, run nvidia-smi to confirm that your GPU model and driver version are visible.

    Step 2: Download the model file. The exact filename depends on the repository; this guide uses qwen2.5-14b-instruct-q4_k_m.gguf as an example.

    Step 3: Launch the server:

    1. llama-server \
    2. -m ./qwen2.5-14b-instruct-q4_k_m.gguf \
    3. -ngl 99 \
    4. -c 4096 \
    5. -fa on \
    6. -np 1 \
    7. --host 127.0.0.1 \
    8. --port 8080

    The parameters function as follows:

    • -m: Path to the model file.
    • -ngl: Number of layers to offload to the GPU. Setting this to 99 attempts to place all layers into VRAM. If the value exceeds the model’s total layers, it effectively loads all of them.
    • -c: Context length, which determines the maximum number of tokens the KV Cache can hold.
    • -fa on: Enables Flash Attention, reducing redundant VRAM read/writes during attention calculation. The exact parameter syntax may vary by version, so run llama-server --help first to verify.
    • -np: Number of parallel slots. The context length set by -c is divided equally among the slots. Setting this to 1 is the most VRAM-efficient for personal use.
    • --host and --port: Listening address and port. This example explicitly uses 127.0.0.1 to prevent the service from accidentally binding to a LAN address.

    If the model cannot fit entirely into VRAM (e.g., the Q4_K_M 32B model), you can start with a lower layer count:

    1. llama-server \
    2. -m ./qwen2.5-32b-instruct-q4_k_m.gguf \
    3. -ngl 24 \
    4. -c 4096 \
    5. -fa on \
    6. -np 1 \
    7. --host 127.0.0.1 \
    8. --port 8080

    -ngl 24 is just a starting point. After launching, monitor nvidia-smi. If there is more than 1GB of VRAM to spare, increase -ngl by 4 to 8 layers at a time until you approach the VRAM limit, then step back by one increment.

    4. How to Calculate VRAM Budget

    The size of the KV Cache is directly proportional to the context length, calculated using the following formula:

    Bytes per token = 2 × Number of Layers × KV Head Count × Head Dimension × Bytes per Element

    The number of layers, KV head count, and head dimension can be found in the config.json file in the model directory. For Qwen2.5-14B, there are 48 layers, 8 KV heads, and a head dimension of 128. The KV Cache defaults to f16, which means each element takes 2 bytes. Plugging these into the formula:

    2 × 48 × 8 × 128 × 2 = 196,608 bytes (approx. 192 KiB)

    Based on this value, the relationship between context length and VRAM usage is as follows:

    • Context 4096: ~768 MiB
    • Context 8192: ~1.5 GiB
    • Context 16384: ~3 GiB

    Adding the Q4_K_M weights (~9GB) and the computation buffer (several hundred MB, refer to startup logs), the total usage for a 4096 context is about 10GB, which leaves headroom within the ~11GB of available VRAM. A context of 8192 will approach the limit; you must verify with nvidia-smi after launching.

    5. Understand Startup Logs

    When the server starts, the logs will print GPU information similar to the following (exact text may vary by version):

    1. ggml_cuda_init: found 1 CUDA devices:
    2. Device 0: NVIDIA GeForce RTX 3060, compute capability 8.6, VMM: yes
    • A compute capability of 8.6 corresponds to Ampere architecture consumer GPUs, which includes multiple models in the RTX 30 series.
    • The VMM field indicates whether the driver supports CUDA Virtual Memory Management. It does not dictate whether a model can be partially offloaded; determining VRAM sufficiency should be based on the -ngl setting and nvidia-smi readings.

    Once the model is loaded, the log will output a line similar to offloaded 25/49 layers to GPU. This line indicates the actual number of layers placed on the GPU, serving as direct proof of whether offloading is effective.

    6. Benchmarking: Measure Prefill and Generation Separately

    Evaluating local inference typically involves two metrics. Prefill speed (prompt processing) measures how fast the model processes the input prompt; the longer the input, the more noticeable the impact. Generation speed (token generation) measures how many tokens are produced per second, directly affecting the responsiveness of the replies. Both should be recorded separately.

    It is recommended to use llama-bench for benchmarking:

    1. llama-bench -m ./qwen2.5-14b-instruct-q4_k_m.gguf -ngl 99 -p 512 -n 128 -r 3

    In the output table, the pp512 row represents the Prefill speed, and the tg128 row represents the Generation speed, both measured in tokens/second. The -r 3 flag means the test repeats three times, providing the mean and standard deviation to mitigate occasional fluctuations.

    To measure the performance of the actual server, you can monitor the server logs. After each request, the logs will output prompt eval time and eval time lines, which include the corresponding tokens/second values.

    When recording benchmark results, save at least the following information: model filename and SHA256, quantization format, values for -c, -ngl, and -np, driver version, CPU model, RAM size, and nvidia-smi readings for VRAM and GPU utilization during the test. Numbers lacking this context cannot be reproduced or compared effectively.

    7. Common Issues and Troubleshooting

    “CUDA out of memory” on startup. Perform these steps sequentially: lower -ngl to leave remaining layers to the CPU; lower -c (e.g., from 8192 to 4096); switch from Q5_K_M to Q4_K_M. Change only one parameter at a time to determine which fix was effective.

    Sudden drop in generation speed. This is usually caused by layers overflowing to the CPU, or another application consuming VRAM. Use nvidia-smi to check VRAM usage. If GPU utilization remains persistently low while CPU usage is high, it generally means a significant amount of computation has fallen onto the CPU.

    High latency for the first token with long inputs. Prefill speed decreases as input length increases. Shortening the prompt or reducing the number of retained historical conversation turns can significantly improve the first token latency.

    Unknown parameter error. Parameter names may change across different versions. Run llama-server --help to view the syntax for your current version, then modify your startup command accordingly.

    Garbled output or noticeable quality degradation. First, check if the file is intact. The download page usually provides the SHA256 hash for each file. Calculate your local file’s hash and compare it. If they do not match, you need to re-download the file.

    8. Risks and Boundaries

    • When VRAM is near full capacity, the desktop environment and browsers may stutter. It is recommended to close other VRAM-consuming applications before running the model.
    • Quantization inherently loses precision. For tasks such as mathematical computation and code generation, validate output quality using your own test cases; do not rely solely on speed.
    • The example in this article binds the service to 127.0.0.1, allowing local access only. If LAN access is required, you must configure access controls and avoid exposing the service directly to the public internet.
    • Prolonged full-load operation will increase GPU temperature and power consumption. Pay attention to chassis cooling and ensure your power supply unit has sufficient headroom.