sush0401 commited on
Commit
ce5cab3
·
verified ·
1 Parent(s): 576c6b1

Pre-warm LLM into CPU RAM at startup (avoids first-call GPU timeout)

Browse files
Files changed (2) hide show
  1. app.py +4 -1
  2. inference.py +11 -0
app.py CHANGED
@@ -22,7 +22,10 @@ MOCK = os.environ.get("TINYWORLD_MOCK", "0") == "1"
22
  if not MOCK and os.environ.get("TINYWORLD_INFER", "modal").lower() == "local":
23
  try:
24
  import inference # noqa: F401 (registers ZeroGPU GPU tasks)
25
- print("[app] ZeroGPU inference module loaded")
 
 
 
26
  except Exception as e:
27
  print(f"[app] inference preimport failed: {e}")
28
 
 
22
  if not MOCK and os.environ.get("TINYWORLD_INFER", "modal").lower() == "local":
23
  try:
24
  import inference # noqa: F401 (registers ZeroGPU GPU tasks)
25
+ import threading
26
+ # warm the model into CPU RAM in the background so startup isn't blocked
27
+ threading.Thread(target=inference.warmup, daemon=True).start()
28
+ print("[app] ZeroGPU inference module loaded; warming model in background")
29
  except Exception as e:
30
  print(f"[app] inference preimport failed: {e}")
31
 
inference.py CHANGED
@@ -40,6 +40,17 @@ def _load_llm():
40
  return _llm, _tok
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
43
  def _strip_think(text):
44
  text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
45
  if "<think>" in text:
 
40
  return _llm, _tok
41
 
42
 
43
+ def warmup():
44
+ """Download + load the LLM into CPU RAM ahead of time (no GPU needed) so the
45
+ first @spaces.GPU call only moves to CUDA and generates — avoids the cold 8GB
46
+ load racing the ZeroGPU duration limit. Safe to call from a background thread."""
47
+ try:
48
+ _load_llm()
49
+ print("[inference] LLM warmed (CPU RAM)")
50
+ except Exception as e:
51
+ print(f"[inference] warmup failed: {e}")
52
+
53
+
54
  def _strip_think(text):
55
  text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
56
  if "<think>" in text: