Self-Hosting Open-Weight LLMs: A Practical Guide to Deployment and Fine-Tuning

    Open-weight models give teams full control over deployment and fine-tuning, but self-hosting isn't always the right choice. This guide walks through license review, VRAM estimation, inference service setup, and LoRA/QLoRA fine-tuning, helping you decide when to self-host and how to do it properly.

    First: Should You Self-Host at All?

    Self-hosting usually makes sense in three situations: your data cannot be sent to external services, your traffic volume is large and consistently stable, or your business requires fine-tuning on proprietary data or analyzing model internals. Three situations suggest against it: low call volume, a team lacking GPU operations experience, or rapidly changing requirements that demand frequent model swaps.

    A recommended order of work: review the license first, then estimate resources, and finally run a technical validation. If the license doesn’t permit a use case, you cannot go live with it regardless of technical feasibility. Each section below covers one of these stages.

    1. Start with a License Review

    Open weights do not mean fully open source. The license determines which use cases are allowed, and this review should be completed before downloading the model. Check the following items one by one:

    • Commercial use: Does the license allow use in for-profit products? Are there user-scale or revenue thresholds?
    • Derivative models: Can fine-tuned models be redistributed or used commercially? Must you retain the original model name or cite its source?
    • Generated output: Can outputs be used to train other models? Does the license list prohibited uses?
    • Redistribution: If you package the weights for customers or into images, which license texts and notices must accompany them?
    • Training data: Does the license of your fine-tuning dataset permit commercial use? Does it contain restricted content?
    • Patents and trademarks: Does the license grant a patent license? Can you use the model’s name for your own product?
    • Compliance records: Keep the license text, version number, download date, and file hashes for future audits.

    Always rely on the official pages of the model hosting platform and publisher for these terms. Before production launch, have legal counsel confirm the interpretation of the license terms.

    2. VRAM Estimation and Resource Planning

    VRAM consumption consists of three parts: model weights, the KV cache (key-value vectors cached during attention computation), and runtime overhead.

    Part one: weight memory. The formula is:

    Weight memory ≈ parameter count × bytes per parameter

    Bytes per parameter depend on precision. FP16 and BF16 use 2 bytes per parameter, INT8 uses 1 byte, and INT4 uses 0.5 bytes. For a 7B-parameter model, FP16 weights require roughly 7×10^9 × 2 bytes, or about 14 GB. With INT4, that drops to about 3.5 GB. Note this covers weights only.

    Part two: KV cache. The formula is:

    KV cache memory ≈ 2 × layers × KV heads × head dimension × context length × concurrent requests × bytes per element

    Assume a model with 32 layers, 8 KV heads, head dimension 128, context length 4096, concurrency of 1, and FP16 precision. Substituting gives 2×32×8×128×4096×1×2 bytes, roughly 0.5 GB. At concurrency 8, that becomes about 4 GB. If the model uses grouped-query attention (GQA), the KV head count is smaller than the attention head count, which significantly reduces KV cache usage.

    Part three: runtime overhead, including activations, framework caches, and memory fragmentation. As a rule of thumb, reserve 10% to 20% of total capacity. This is an empirical range, not a precise value.

    Putting it together, total requirement ≈ (weight memory + KV cache memory) × (1 + reserve ratio).

    Estimation error is usually significant; actual results depend on the inference framework, operator implementations, and batching strategy. Before launch, measure peak VRAM with your target framework and load-test with real request length distributions. For capacity planning, first determine your maximum context length and peak concurrency, then choose VRAM capacity and leave headroom for traffic spikes.

    3. Building the Inference Service

    Deploying an inference service involves seven steps.

    Step 1: choose an inference framework. For high-throughput online services, consider frameworks supporting continuous batching such as vLLM or TGI. For single-machine, local, or edge-device scenarios, lightweight options like llama.cpp or Ollama work well. Base your choice on concurrency requirements, hardware type, and team familiarity.

    Step 2: choose a quantization method. INT8 and INT4 (common formats include AWQ, GPTQ, and GGUF) reduce VRAM usage but may affect output quality. Compare accuracy and output stability before and after quantization using your own evaluation set before deciding.

    Step 3: configure concurrency and batching parameters, including max batch size, max sequence length, and queue timeout. Requests exceeding the queue timeout should fail fast to avoid piling up in the queue.

    Step 4: set up monitoring. Continuously collect VRAM usage, GPU utilization, queue length, time-to-first-token, tokens per second, and error rate. Trigger alerts when VRAM approaches its limit.

    Step 5: load test. Run stress tests with anonymized real request distributions to find the maximum throughput before latency exceeds what your business can tolerate, and keep headroom for traffic fluctuations.

    Step 6: isolate permissions. Place the inference endpoint behind a gateway, enable authentication, and enforce rate limits per user or application. Grant service accounts only minimal permissions, and mount model files as read-only.

    Step 7: sanitize logs. By default, do not log full prompts or model outputs. When logging is genuinely necessary, mask fields such as phone numbers, ID numbers, and bank card numbers first, and set a log retention period.

    Before launch, also prepare a rollback plan by keeping the previous version’s model files and configuration. If the new version has problems, you can quickly switch back.

    4. Key Points for LoRA and QLoRA Fine-Tuning

    LoRA freezes the original model weights and trains only an adapter composed of low-rank matrices. The adapter’s parameter count is typically a tiny fraction of the base model. QLoRA first quantizes the base model to 4 bits, then trains a LoRA adapter on top of it, allowing fine-tuning with much less VRAM.

    Three things matter during data preparation:

    • Consistent format: Use a conversation or instruction format—e.g., one JSON record per line with instruction, input, and output fields—or the chat template required by the target model.
    • Data cleaning: Remove duplicate and malformed samples, unify annotation rules, and check label consistency.
    • Data splitting: Keep training, validation, and test sets separate. The test set must not participate in any tuning.

    The following parameters serve as a starting point and should be adjusted according to your task.