LocalAI icon indicating copy to clipboard operation
LocalAI copied to clipboard

When the models directory already contains downloaded models, starting the LocalAI service and waiting for the HTTP server on port 8080 to become available takes an excessively long time.

Open yanghanliang-cn opened this issue 6 months ago • 2 comments

**LocalAI version:4.1.3 **

docker image name:localai/localai:latest-gpu-nvidia-cuda-12 Environment, CPU architecture, OS, and Version:

CPU:AMD Ryzen 9 5950X 16-Core Processor X64 Main Memory:64GB GPU:nvidia GeForce GTX 1050TI VRAM:4GB OS:windows 11 X64 25H2 Docker Environment:DockerDesktop 4.71.0

Describe the bug

When the models directory already contains downloaded models, starting the LocalAI service and waiting for the HTTP server on port 8080 to become available takes an excessively long time. DEBUGINFO: May 12 22:42:36 INFO AgentJobService started retention_days=30 caller={caller.file="/build/core/services/agentpool/agent_jobs.go" caller.L=1523 } May 12 22:42:36 DEBUG [inference_defaults] applying defaults for model modelID="Qwen3.5-0.8B" family=map[min_p:0 presence_penalty:1.5 repeat_penalty:1 temperature:0.7 top_k:20 top_p:0.8] caller={caller.file="/build/core/config/inference_defaults.go" caller.L=90 } May 12 22:45:28 DEBUG [gguf] guessDefaultsFromFile: NGPULayers set NGPULayers=0x3ec5b19f0be8 modelName="Qwen3.5-0.8B" caller={caller.file="/build/core/config/gguf.go" caller.L=48 } May 12 22:45:28 DEBUG [gguf] guessDefaultsFromFile: template already set name="Qwen3.5-0.8B" modelName="Qwen3.5-0.8B" caller={caller.file="/build/core/config/gguf.go" caller.L=62 }

To Reproduce

Using the Docker image and OS/software/hardware environment described above, starting the LocalAI service with pre-downloaded models in the modelsdirectory... Expected behavior

Avoid prolonged startup delays for the HTTP service on port 8080. Logs

DEBUGINFO: May 12 22:42:36 INFO AgentJobService started retention_days=30 caller={caller.file="/build/core/services/agentpool/agent_jobs.go" caller.L=1523 } May 12 22:42:36 DEBUG [inference_defaults] applying defaults for model modelID="Qwen3.5-0.8B" family=map[min_p:0 presence_penalty:1.5 repeat_penalty:1 temperature:0.7 top_k:20 top_p:0.8] caller={caller.file="/build/core/config/inference_defaults.go" caller.L=90 } May 12 22:45:28 DEBUG [gguf] guessDefaultsFromFile: NGPULayers set NGPULayers=0x3ec5b19f0be8 modelName="Qwen3.5-0.8B" caller={caller.file="/build/core/config/gguf.go" caller.L=48 } May 12 22:45:28 DEBUG [gguf] guessDefaultsFromFile: template already set name="Qwen3.5-0.8B" modelName="Qwen3.5-0.8B" caller={caller.file="/build/core/config/gguf.go" caller.L=62 }

Additional context

yanghanliang-cn avatar May 12 '26 15:05 yanghanliang-cn

There was a significant delay between May 12 22:42:36​ (inference_defaults.go) and May 12 22:45:28​ (config/gguf.go).

yanghanliang-cn avatar May 12 '26 15:05 yanghanliang-cn

I dug into this. The long gap between log lines at startup (in your case ~3 min for a single model) is the GGUF default-guessing step in llamaCppDefaults: on boot LocalAI parses every model's GGUF header to guess defaults (context size, GPU layers, chat template, …), and that parse was reading the entire tokenizer vocab — tokenizer.ggml.tokens/scores/merges, often 100k–260k entries — element by element, once per model. On slow storage (a models dir on a Docker Desktop volume crosses the VM/filesystem boundary on every read) those hundreds of thousands of tiny read() syscalls dominate startup before the HTTP server comes up.

None of the guessed defaults need the vocab contents — only scalar metadata and array lengths. The fix parses the header with SkipLargeMetadata() (seek past the big arrays) and UseMMap() (fault in a few header pages instead of issuing per-element reads). Measured on a generated 256k-token GGUF, this drops the parse from ~524k read() syscalls to 8, which is the difference that matters on slow filesystems. The same optimization is applied to the VRAM metadata reader, which has the same scalar-only access pattern.

Preparing a PR with the change plus regression coverage.

Dennisadira avatar Jun 07 '26 21:06 Dennisadira