AfkaraLP commited on
Commit
4a0f59e
·
1 Parent(s): 1f38746

Preload nvidia CUDA libs (RTLD_GLOBAL) so libllama.so finds libcudart on ZeroGPU; eager model download; duration=120

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -76,26 +76,37 @@ impl Point {
76
  ]
77
 
78
  _llm = None
79
- _model_path = None
80
 
 
 
 
 
 
81
 
82
- def model_path():
83
- global _model_path
84
- if _model_path is None:
85
- _model_path = hf_hub_download(
86
- repo_id=MODEL_REPO,
87
- filename=MODEL_FILE,
88
- token=os.environ.get("HF_TOKEN"),
89
- )
90
- return _model_path
 
 
 
 
 
 
91
 
92
 
93
  def get_llm():
94
  global _llm
95
  if _llm is None:
 
96
  from llama_cpp import Llama
97
  _llm = Llama(
98
- model_path=model_path(),
99
  n_gpu_layers=N_GPU_LAYERS,
100
  n_ctx=CTX,
101
  n_batch=512,
@@ -115,7 +126,7 @@ def clean(text: str) -> str:
115
  return text.rstrip()
116
 
117
 
118
- @gpu(duration=60)
119
  def complete(code, temperature, max_tokens, top_p):
120
  if code.strip() == "":
121
  return "", code, "*Empty input.*"
 
76
  ]
77
 
78
  _llm = None
 
79
 
80
+ _model_path = hf_hub_download(
81
+ repo_id=MODEL_REPO,
82
+ filename=MODEL_FILE,
83
+ token=os.environ.get("HF_TOKEN"),
84
+ )
85
 
86
+
87
+ def _preload_cuda():
88
+ import ctypes
89
+ import glob
90
+ import site
91
+
92
+ dirs = []
93
+ for sp in site.getsitepackages():
94
+ dirs += glob.glob(sp + "/nvidia/*/lib")
95
+ for d in sorted(dirs):
96
+ for so in sorted(glob.glob(d + "/*.so*")):
97
+ try:
98
+ ctypes.CDLL(so, mode=ctypes.RTLD_GLOBAL)
99
+ except OSError:
100
+ pass
101
 
102
 
103
  def get_llm():
104
  global _llm
105
  if _llm is None:
106
+ _preload_cuda()
107
  from llama_cpp import Llama
108
  _llm = Llama(
109
+ model_path=_model_path,
110
  n_gpu_layers=N_GPU_LAYERS,
111
  n_ctx=CTX,
112
  n_batch=512,
 
126
  return text.rstrip()
127
 
128
 
129
+ @gpu(duration=120)
130
  def complete(code, temperature, max_tokens, top_p):
131
  if code.strip() == "":
132
  return "", code, "*Empty input.*"