Ox1 Cursor commited on
Commit
632a1a3
·
1 Parent(s): b60933b

perf(model): tune llama.cpp for cpu-basic

Browse files

Set n_threads=2 when SPACE_ID is present to match 2 vCPU on CPU Basic.
Update docstrings to reflect CPU Basic deployment instead of ZeroGPU.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (1) hide show
  1. src/model_loader.py +16 -14
src/model_loader.py CHANGED
@@ -1,9 +1,7 @@
1
  """Singleton model loader for GGUF models.
2
 
3
  Keeps one model loaded at a time. Supports dual-mode deployment:
4
- - HF Spaces (ZeroGPU): CPU-only inference (n_gpu_layers=0) because
5
- ZeroGPU's CUDA emulation is PyTorch-specific and llama.cpp links
6
- against the system CUDA runtime which is not available.
7
  - Local: Full GPU offload via CUDA when libcudart.so.12 is present.
8
  """
9
 
@@ -98,8 +96,7 @@ class _ModelManager:
98
  def _detect_gpu_layers() -> int:
99
  """Detect whether to offload layers to GPU.
100
 
101
- On HF Spaces the CUDA runtime is unavailable to llama.cpp
102
- (ZeroGPU only exposes CUDA through PyTorch emulation).
103
  Locally, probe for libcudart.so.12 to confirm real CUDA.
104
  """
105
  if os.environ.get("SPACE_ID"):
@@ -145,18 +142,23 @@ class _ModelManager:
145
  chat_handler = Qwen25VLChatHandler(clip_model_path=str(mmproj_path))
146
 
147
  n_gpu_layers = self._detect_gpu_layers()
 
148
  logger.info(
149
- "Loading model: %s (handler: %s, gpu_layers: %s)",
150
- config.model_file, config.handler_type, n_gpu_layers,
151
  )
152
 
153
- self._llm = Llama(
154
- model_path=str(model_path),
155
- chat_handler=chat_handler,
156
- n_gpu_layers=n_gpu_layers,
157
- n_ctx=config.n_ctx,
158
- verbose=False,
159
- )
 
 
 
 
160
  self._current_config = config
161
  logger.info("Model loaded successfully")
162
  return self._llm
 
1
  """Singleton model loader for GGUF models.
2
 
3
  Keeps one model loaded at a time. Supports dual-mode deployment:
4
+ - HF Spaces (CPU Basic): CPU-only inference (n_gpu_layers=0, n_threads=2)
 
 
5
  - Local: Full GPU offload via CUDA when libcudart.so.12 is present.
6
  """
7
 
 
96
  def _detect_gpu_layers() -> int:
97
  """Detect whether to offload layers to GPU.
98
 
99
+ On HF Spaces (CPU Basic) there is no CUDA — always CPU.
 
100
  Locally, probe for libcudart.so.12 to confirm real CUDA.
101
  """
102
  if os.environ.get("SPACE_ID"):
 
142
  chat_handler = Qwen25VLChatHandler(clip_model_path=str(mmproj_path))
143
 
144
  n_gpu_layers = self._detect_gpu_layers()
145
+ n_threads = 2 if os.environ.get("SPACE_ID") else None
146
  logger.info(
147
+ "Loading model: %s (handler: %s, gpu_layers: %s, threads: %s)",
148
+ config.model_file, config.handler_type, n_gpu_layers, n_threads or "default",
149
  )
150
 
151
+ llama_kwargs: dict = {
152
+ "model_path": str(model_path),
153
+ "chat_handler": chat_handler,
154
+ "n_gpu_layers": n_gpu_layers,
155
+ "n_ctx": config.n_ctx,
156
+ "verbose": False,
157
+ }
158
+ if n_threads is not None:
159
+ llama_kwargs["n_threads"] = n_threads
160
+
161
+ self._llm = Llama(**llama_kwargs)
162
  self._current_config = config
163
  logger.info("Model loaded successfully")
164
  return self._llm