Local Deployment Guide for a 27B Dense Model: Quantization and Benchmarking

    A practical guide to running a 27B dense model locally: how to check whether your hardware is sufficient, how to estimate GGUF quantization file sizes and VRAM/RAM requirements (including KV cache math), how to launch an OpenAI-compatible server with llama.cpp, how to troubleshoot common issues, how to benchmark prompt processing and generation speeds correctly, and how to choose between the dense and MoE variants for coding tasks.

    1. First, Check Whether Your Hardware Is Sufficient

    All weights of a 27B dense model must fit into VRAM or RAM, and every generated token uses the full set of parameters. Before deploying, answer three questions:

    1. What is your GPU’s nominal VRAM, and how much remains after the OS and display overhead?
    2. How much RAM do you have, and can it hold the portion of weights that does not fit in VRAM?
    3. How long a context do your daily tasks require?

    Usable VRAM is typically lower than the nominal value, since the desktop environment and browser consume part of it. Leave a 1–2 GB margin when estimating.

    2. Quantization Levels and File Sizes

    Quantization compresses model weights from high precision into low-bit representations. The weight file size can be roughly estimated with:

    File size ≈ parameter count × bits per weight ÷ 8

    For a 27B model, using decimal units (1 GB = 10^9 bytes):

    Quantization level Approx. bits per weight Approx. file size Precision vs. size trade-off
    Q8_0 8.5 29 GB Smallest precision loss, largest size
    Q6_K 6.6 22 GB Well balanced between precision and size
    Q5_K_M 5.7 19 GB Smaller than Q6_K, slightly lower precision
    Q4_K_M 4.9 16.5 GB Common entry point, fits 24 GB VRAM
    Q3_K_M 3.9 13 GB Noticeably smaller, larger precision loss
    Q2_K 3.4 11.5 GB Smallest size, largest precision loss; use caution for coding

    Q4_K_M is not the smallest level. Q3_K_M and Q2_K are smaller but cost more precision. Degradation at low bit-widths is usually more visible in code and math tasks than in casual chat, so test on your own tasks before settling on a level.

    The formula gives an estimate. GGUF files contain metadata, and tensors have alignment padding, so actual files are usually slightly larger than the estimate. Always rely on the real size of the downloaded file.

    3. VRAM and RAM Estimation Checklist

    Runtime memory usage has three components:

    • Weights: the file size from the table above; essentially all of it stays resident once loaded.
    • KV cache: the attention cache for the context; grows with context length and the number of parallel slots.
    • Compute buffers and framework overhead: determined by the inference framework, driver, and batch size; typically 1–2 GB.

    The KV cache is estimated as:

    KV cache bytes ≈ 2 × number of layers × number of KV heads × head dimension × context length × bytes per element

    The following uses a set of assumed parameters for demonstration: 62 layers, 8 KV heads, head dimension 128, and 2 bytes per cached element (FP16). Check the model’s configuration file for actual values.

    KV cache per token: 2 × 62 × 8 × 128 × 2 = 253,952 bytes, about 248 KiB. Therefore:

    • At 8,192 context, the KV cache is about 2.1 GB.
    • At 32,768 context, the KV cache is about 8.3 GB.

    Example with a 24 GB GPU running Q4_K_M:

    Scenario Weights KV cache Buffer overhead Total Fits?
    8K context, FP16 cache 16.5 GB 2.1 GB 1.5 GB ~20 GB Yes, with comfortable headroom
    32K context, FP16 cache 16.5 GB 8.3 GB 1.5 GB ~26 GB Exceeds 24 GB
    32K context, KV cache quantized to q8_0 16.5 GB ~4.2 GB 1.5 GB ~22 GB Yes, with limited headroom

    When you exceed VRAM, there are three options: reduce the context length, quantize the KV cache to q8_0 (roughly halving its size), or reduce the number of layers offloaded to the GPU (-ngl). The third option computes some layers in RAM, which noticeably slows generation.

    4. Launching a Local Server with llama.cpp

    1. Pin the version. After cloning the repository, check out a fixed tag or commit and record the commit hash so you can reproduce benchmark results later.

    1. git clone https://github.com/ggml-org/llama.cpp
    2. cd llama.cpp
    3. git checkout <tag or commit hash>
    4. git rev-parse HEAD

    2. Build. With an NVIDIA GPU, enable the CUDA backend. For other backends, refer to the build documentation in the repository.

    1. cmake -B build -DGGML_CUDA=ON
    2. cmake --build build --config Release -j

    3. Start the server. The following command keeps the model fully on the GPU where possible, with an 8,192-token context.

    1. ./build/bin/llama-server \
    2. -m /models/model-Q4_K_M.gguf \
    3. -ngl 99 \
    4. -c 8192 \
    5. -np 1 \
    6. -fa on \
    7. --host 127.0.0.1 \
    8. --port 8080

    Common parameters:

    Parameter Meaning Recommendation
    -m Path to the GGUF file Use an absolute path
    -ngl Number of layers offloaded to the GPU; 99 means offload as many as possible Lower it gradually if VRAM is insufficient
    -c Total context length (tokens) Set according to task needs; larger values use more VRAM
    -np Number of parallel slots The total context is split across slots; re-estimate before increasing
    -fa on Enable Flash Attention Usually required when quantizing the V cache
    -ctk / -ctv Data types for the K and V caches, e.g. q8_0 Use when VRAM is tight
    -t Number of CPU threads Has little effect when the model is fully on the GPU
    —host / —port Listen address and port Keep 127.0.0.1 unless you have a specific need

    4. Call the API. The server is compatible with the OpenAI chat completions interface.

    1. curl http://127.0.0.1:8080/v1/chat/completions \
    2. -H "Content-Type: application/json" \
    3. -d '{
    4. "messages": [{"role": "user", "content": "Write a Python function that reads a CSV and sums a column"}],
    5. "max_tokens": 512,
    6. "temperature": 0.2
    7. }'

    With the OpenAI Python SDK, set base_url to http://127.0.0.1:8080/v1. If you started the server with —api-key, pass the same key in your client. You can check whether the server is ready via /health.

    5. Troubleshooting Checklist

    Symptom Common cause Fix
    out of memory or CUDA-related errors at startup Weights plus KV cache exceed VRAM Lower -c, lower -ngl, or set -ctk and -ctv to q8_0
    Model fails to load Incomplete download or not a GGUF file Verify the SHA256 against the source page and confirm the file is intact
    Port already in use Another process is using 8080 Change —port, or find the process with lsof -i :8080
    Generation speed far below expectations Some layers still on the CPU, or GPU thermal throttling Check layer allocation in the startup log; check temperature and power with nvidia-smi
    Repetitive or garbled output Quantization level too low, or chat template mismatch Compare against a higher level; check the model’s chat template

    6. How to Measure Speed

    LLM inference has two phases. The prefill phase processes the input prompt, measured in pp tokens/s. The generation phase outputs tokens one by one, measured in tg tokens/s. The two differ significantly and must always be reported separately.

    Benchmark with llama-bench:

    1. ./build/bin/llama-bench -m /models/model-Q4_K_M.gguf -ngl 99 -p 512 -n 256 -r 5

    Here, -p is the prefill prompt length, -n is the number of tokens to generate, and -r is the number of repetitions. When reporting results, also document the conditions in this table:

    Item Requirement
    Hardware GPU and RAM models, driver version
    Operating state Plugged into power, performance mode, note cooling conditions
    Software llama.cpp commit hash, build options
    Model File name, quantization level, SHA256
    Parameters -ngl, context length, batch and ubatch sizes
    Workload Prompt length, number of generated tokens
    Statistics Warm up once before timing, repeat at least 5 times, report the median and range

    Generation has a theoretical ceiling: every generated token requires reading all weights once, so the speed limit is roughly memory bandwidth divided by weight bytes. With 16.5 GB of Q4_K_M weights and 900 GB/s of memory bandwidth, the ceiling is about 55 tokens/s. Real-world results are usually below this.

    The commonly cited “20–30+ tokens/s feels smooth” found online is a rule of thumb, not a universal standard. Perception of waiting time varies, so judge against your own reading habits. Speed typically drops as context grows, so benchmark with the context length you will actually use.

    7. Dense vs. MoE Variants for Coding Tasks

    In the dense variant, every token uses all parameters. The MoE variant has more total parameters, but each token activates only a subset of experts. Comparison:

    Dimension Dense 27B MoE variant
    Parameters active per token All 27B participate in computation Only some experts are activated; refer to the official documentation for the ratio
    Weight footprint Only needs to hold 27B weights More total parameters, usually a larger weight file
    Generation speed Reads all weights per step; mainly limited by memory bandwidth Reads fewer active weights per step, but routing adds overhead; results depend on implementation
    Quantization support Common GGUF quantization levels are widely available Confirm your llama.cpp version supports the architecture and quantization type
    Coding task performance Test on your own tasks; fewer deployment and debugging variables Test yourself; do not judge by parameter count alone
    Long context KV cache grows linearly with context Also grows linearly; estimate separately based on that model’s configuration

    Two principles for choosing:

    • If your VRAM can hold a common quantization level of the dense model and your main use is code completion and review, the dense variant has a simpler deployment path.
    • If you have ample RAM but tight VRAM, and your llama.cpp version supports the MoE model, then test the MoE variant.

    The final conclusion should come from your own testing. Prepare 30–50 of your own coding tasks, fix the prompts and temperature, and record first-pass rate, number of revisions, average generation speed, and peak VRAM usage for both versions.

    8. Risks and Compliance Notes

    • Low-bit quantization may degrade coding and math capabilities. Run A/B tests on your own tasks before deployment, and keep the original weights and version records.
    • Local deployment does not by itself satisfy compliance requirements. Keeping data on your own machine is only part of it; you also need access controls, log redaction, and clear retention policies.
    • The server binds to 127.0.0.1 by default. If you bind to 0.0.0.0, set —api-key first and place the service behind a firewall or internal network.
    • Review the model license to confirm it permits your intended use, including commercial use and redistribution.
    • Sustained high load generates heat; thermal throttling reduces speed, so leave cooling headroom.
    • After downloading a model, record its SHA256 checksum so you can later verify the file has not been altered.

    9. Starting Points by Hardware

    • 24 GB VRAM: Start with Q4_K_M and a context of 8K–16K, then adjust based on test results.
    • 16 GB VRAM: Q4_K_M’s 16.5 GB of weights already exceeds VRAM. Choose Q3_K_M with a shorter context, or run some layers in RAM at lower speed.
    • CPU and RAM only: It works, but generation speed is usually far below GPU. Benchmark with llama-bench first to confirm it is acceptable.

    All figures above are estimated starting points. Whether the setup meets your daily needs ultimately depends on your own model files, context lengths, and benchmark results.