unity4ar commited on
Commit
031ce2d
·
verified ·
1 Parent(s): 47c194f

Use canonical .to('cuda') pattern + progress logs so container log shows what loader is doing

Browse files
Files changed (1) hide show
  1. llm/zerogpu_backend.py +23 -8
llm/zerogpu_backend.py CHANGED
@@ -42,28 +42,43 @@ _tokenizer = None
42
  _load_done = threading.Event()
43
 
44
 
 
 
 
 
 
45
  def _load_model() -> None:
46
- """Heavy CPU work (download ~16 GB safetensors + load to cuda emulation).
47
- Runs in a daemon thread so that import returns immediately and the HTTP
48
- server can serve /api/setup/status without blocking 15+ minutes."""
 
 
 
 
 
49
  global _model, _tokenizer, _LOAD_ERROR
50
  try:
 
51
  _tokenizer = AutoTokenizer.from_pretrained(_MODEL_ID, trust_remote_code=True)
52
- # On ZeroGPU, `device_map="cuda"` lands on the emulated device at import
53
- # and becomes the real GPU inside @spaces.GPU. Off-platform (no `spaces`
54
- # package installed), fall back to CPU so the module is still importable.
55
- device_map = "cuda" if _ON_ZEROGPU else "cpu"
56
  model = AutoModelForCausalLM.from_pretrained(
57
  _MODEL_ID,
58
  torch_dtype=torch.bfloat16,
59
  trust_remote_code=True,
60
- device_map=device_map,
61
  low_cpu_mem_usage=True,
62
  )
 
 
 
 
 
 
63
  model.eval()
64
  _model = model
 
65
  except Exception as exc: # pragma: no cover
66
  _LOAD_ERROR = f"{exc.__class__.__name__}: {exc}"
 
67
  finally:
68
  _load_done.set()
69
 
 
42
  _load_done = threading.Event()
43
 
44
 
45
+ def _log(msg: str) -> None:
46
+ """stdout flush so progress shows up in the HF container log immediately."""
47
+ print(f"[zerogpu_backend] {msg}", flush=True)
48
+
49
+
50
  def _load_model() -> None:
51
+ """Heavy CPU work (download ~16 GB safetensors + place on cuda). Runs in a
52
+ daemon thread so import returns immediately and the HTTP server can serve
53
+ /api/setup/status without blocking. Mirrors the canonical pattern from
54
+ https://huggingface.co/docs/hub/en/spaces-zerogpu :
55
+
56
+ pipe = X.from_pretrained(...)
57
+ pipe.to('cuda')
58
+ """
59
  global _model, _tokenizer, _LOAD_ERROR
60
  try:
61
+ _log(f"loading tokenizer for {_MODEL_ID}")
62
  _tokenizer = AutoTokenizer.from_pretrained(_MODEL_ID, trust_remote_code=True)
63
+ _log("tokenizer ready; loading model weights (this downloads ~16 GB on first run)")
 
 
 
64
  model = AutoModelForCausalLM.from_pretrained(
65
  _MODEL_ID,
66
  torch_dtype=torch.bfloat16,
67
  trust_remote_code=True,
 
68
  low_cpu_mem_usage=True,
69
  )
70
+ _log("weights loaded to CPU; placing on cuda")
71
+ if _ON_ZEROGPU:
72
+ model = model.to("cuda")
73
+ _log("model on cuda (emulated until @spaces.GPU acquires real GPU)")
74
+ else:
75
+ _log("not on ZeroGPU; keeping model on CPU for local dev")
76
  model.eval()
77
  _model = model
78
+ _log("READY")
79
  except Exception as exc: # pragma: no cover
80
  _LOAD_ERROR = f"{exc.__class__.__name__}: {exc}"
81
+ _log(f"LOAD FAILED: {_LOAD_ERROR}")
82
  finally:
83
  _load_done.set()
84