Spaces:
Sleeping
Sleeping
Enable llama.cpp KV prompt cache (512 MB): ~21% off study-plan time
Browse files20 unmodified lines21222324252623 unmodified lines50515253545525 unmodified lines81828384858620 unmodified linesfrom pydantic import BaseModel, Fieldfrom huggingface_hub import hf_hub_downloadfrom llama_cpp import Llamafrom llama_cpp import llama_chat_formatlogging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")23 unmodified linesN_THREADS = int(os.getenv("N_THREADS", str(_USABLE)))N_GPU_LAYERS = int(os.getenv("N_GPU_LAYERS", "0"))DEFAULT_MAX_TOKENS = int(os.getenv("DEFAULT_MAX_TOKENS", "512"))# Current model state (mutable at runtime via /admin/load)._llm: Optional[Llama] = None25 unmodified linesif cfg["chat_format"]:kwargs["chat_format"] = cfg["chat_format"]new_llm = Llama(**kwargs)with _llm_lock: # swap atomically; old model is freed_llm = new_llm_cfg = dict(cfg)20 unmodified lines2122232425262723 unmodified lines5152535455565758596025 unmodified lines86878889909192939420 unmodified linesfrom pydantic import BaseModel, Fieldfrom huggingface_hub import hf_hub_downloadfrom llama_cpp import Llamafrom llama_cpp.llama_cache import LlamaRAMCachefrom llama_cpp import llama_chat_formatlogging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")23 unmodified linesN_THREADS = int(os.getenv("N_THREADS", str(_USABLE)))N_GPU_LAYERS = int(os.getenv("N_GPU_LAYERS", "0"))DEFAULT_MAX_TOKENS = int(os.getenv("DEFAULT_MAX_TOKENS", "512"))# Prompt (KV) cache. Without it every call re-prefills the whole system prompt,# which is NOT cheap on 2 vCPU: measured 20.8s -> 8.5s per call with the study# plan's rotating daily/weekly/monthly prompts, ~21% off a full plan.PROMPT_CACHE_MB = int(os.getenv("PROMPT_CACHE_MB", "512")) # 0 disables# Current model state (mutable at runtime via /admin/load)._llm: Optional[Llama] = None25 unmodified linesif cfg["chat_format"]:kwargs["chat_format"] = cfg["chat_format"]new_llm = Llama(**kwargs)if PROMPT_CACHE_MB > 0:new_llm.set_cache(LlamaRAMCache(capacity_bytes=PROMPT_CACHE_MB * 1024 * 1024))log.info("Prompt cache enabled (%d MB).", PROMPT_CACHE_MB)with _llm_lock: # swap atomically; old model is freed_llm = new_llm_cfg = dict(cfg)
main.py+8