keepcalculator

06 Numbers and Powers

Self-Hosting DeepSeek: Fix Memory and Latency Problems

Card No. 06·29210 · Updated · Standard formula, shown below

Slow responses, out-of-memory errors, and models that refuse to load often come from a mismatch between DeepSeek’s model size, its runtime settings, and the memory available to your server. Work through the checks below to identify whether the bottleneck is RAM, GPU memory, model format, or request load, then adjust one factor at a time.

What You’ll Need

  • Access to the machine running the model and its logs or monitoring tools.
  • The model name, parameter size, quantization, runtime, and launch settings.
  • A way to check system RAM, GPU memory, CPU and GPU use, and disk space.
  • A small repeatable test prompt, plus a record of the current settings so you can undo changes.

Step-by-Step

  1. Define the symptom and establish a baseline. Separate slow model startup from slow first-token time and slow generation. Restart the server, wait for it to become ready, then submit the same short prompt a few times with no other users. Note time to first token, tokens per second if reported, peak memory, and whether the delay occurs on every request or only the first. A first request can be slower because the runtime loads weights or builds caches.
  2. Confirm which DeepSeek model you are loading. DeepSeek refers to multiple models and variants, including dense and mixture-of-experts models, with different memory requirements. Check the exact model card or distribution documentation for the architecture, parameter count, supported context length, and required runtime. Do not assume that a model’s advertised total parameters equal the number of active parameters per token, or that active parameters determine the full weight memory needed: the weights and runtime still have to fit in memory.
  3. Check available memory while the model is running. On Linux, use tools such as free -h for system memory and nvidia-smi for NVIDIA GPU memory and utilization. For other accelerators, use their vendor monitoring tools. Check disk space too: some runtimes load or cache model files locally. Look for out-of-memory messages, swapping, GPU memory exhaustion, or another process using the device. Leave headroom for the operating system, runtime, and request cache; a model that barely loads may fail when requests arrive.
  4. Compare the model’s memory demands with the hardware. Weight files are only part of the total. Runtime overhead, key-value (KV) cache for the context, batch size, and concurrent requests use additional memory. A longer context or more simultaneous sequences can substantially increase cache use. If memory is insufficient, choose a smaller model or a supported, more highly quantized model. Quantization reduces weight memory but may affect output quality and speed; verify that your serving runtime supports that exact model format and quantization.
  5. Verify the model format and runtime compatibility. Follow the model publisher’s loading instructions for your chosen inference engine. Confirm that the engine supports the architecture, tokenizer, quantization, and any remote model code required by the distribution. Update or change the runtime only after checking its documentation and recording the working configuration. A model file that downloads successfully may still be incompatible with an older engine or an unsupported file format.
  6. Reduce memory pressure with conservative settings. Temporarily lower the maximum context length, batch size, and number of concurrent requests. These are different controls: context affects the amount of text a request can retain, while batching and concurrency affect how many sequences are processed or cached together. Use the setting names documented by your runtime; they are not identical across tools. Restart the server after changing settings and repeat the same test. Increase limits gradually only while memory remains stable.
  7. Find the actual latency bottleneck. If GPU memory is full, reduce model or cache requirements before raising concurrency. If GPU utilization stays low while CPU use is high, check CPU offload, tokenizer load, and whether the runtime is actually using the accelerator. If both CPU and GPU remain lightly used, inspect logs for waiting on disk, model loading, or request queuing. Slow storage can delay startup, while swapping can make inference dramatically slower. Avoid changing several settings at once, because that obscures the cause.
  8. Check the request path and concurrency. Test directly against the model server, then compare with requests through your API gateway or application. A queue, proxy timeout, network hop, or oversized prompt can look like slow inference. Check whether multiple clients are submitting work and whether the server has a documented queue or concurrency limit. Keep a representative prompt and avoid sending conversation history that the model does not need.
  9. Apply changes incrementally and keep a rollback path. Save the known-good model identifier, runtime version, launch command, and settings. Change one variable, restart cleanly, and compare the same test results. Once stable, increase context or concurrency in small steps while watching peak memory and latency. If a change causes errors or worse results, restore the saved configuration rather than stacking more unverified tweaks.

Common Mistakes

  • Using model size alone as a memory estimate. Weights, cache, runtime overhead, and concurrent requests all matter.
  • Setting the context limit to the model’s maximum immediately. A large limit can consume memory even when typical prompts are shorter, depending on runtime allocation behavior.
  • Assuming more CPU or GPU offload is always faster. Moving data between system RAM and GPU memory can add latency; results depend on hardware and runtime.
  • Mixing model files and loaders. A quantized file or checkpoint may not work with every engine. Match the format to documented support.
  • Changing multiple variables at once. This makes it difficult to tell whether a memory or latency improvement came from the model, cache, or concurrency setting.

Tips

  • Measure time to first token separately from tokens per second; they point to different bottlenecks.
  • Test with both a short prompt and a prompt near your normal workload, while keeping the test repeatable.
  • Track peak memory during real concurrent use, not just while the model is idle.
  • For a single-user service, a smaller context and one active request may be a better starting point than maximizing throughput.
  • Keep model files on a local drive with adequate free space, and avoid deleting caches while the server is running.

Troubleshooting

The model will not load: Check the first error in the server log, available disk space, and free system and accelerator memory. Confirm the model path, file integrity, architecture support, and quantization compatibility. If memory is the cause, try a smaller supported model or quantization before adjusting unrelated settings.

The server loads but crashes on a prompt: Reduce context length, batch size, or concurrency and retry with a short prompt. This often reveals whether the KV cache or peak request memory is exceeding available capacity.

It is slow despite plenty of memory: Check accelerator utilization, CPU load, swapping, and disk activity. Confirm that inference is running on the intended device and that CPU offload is not creating a transfer bottleneck. Compare a direct server request with one sent through your application.

Latency rises with multiple users: Check queue depth and per-request memory. Reduce simultaneous sequences or configure a supported queue limit, then increase concurrency gradually. Do not simply raise batch size if memory is already near its limit.

Updates break a previously working setup: Restore the recorded runtime and launch settings, then verify model compatibility before updating again. Change one component at a time and retain the last working model files until the replacement has passed your tests.

Frequently Asked Questions

Does a quantized DeepSeek model always run faster?
No. Quantization usually reduces weight memory, but speed depends on the quantization format, hardware, and runtime kernels. Benchmark the exact setup.

Why does a model load but run out of memory later?
Requests need additional memory for context and KV cache. Longer prompts, larger batches, and more concurrent requests can push usage beyond the load-time requirement.

Will CPU offload fix a GPU memory error?
It can let some runtimes use system RAM for part of a model, but it may slow inference. It is not supported or equally effective for every model and runtime.

How much context should I set?
Use the smallest limit that covers your real prompts and conversation history. Follow the model and runtime documentation, and increase it only after checking memory use.

Should I upgrade the runtime to fix compatibility?
Only if its documentation or release notes indicate support for your model format or architecture. Back up the working configuration and test the update before using it for live requests.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →