File size: 9,572 Bytes
1295a89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
from jinja2 import Template
from json import load
from os import listdir
from os.path import getsize
from requests import request, RequestException
from subprocess import Popen, PIPE, run
from threading import Lock
import typing as t
__all__ = [
'LLaMaCPP',
'LLMS',
]
with open('/opt/llms/index.json', 'r') as _f:
LLMS = load(_f)
class LLaMaCPP:
def __init__(self):
self._model_name = None
self._process = None
self._readers = 0
self._read_lock = Lock()
self._write_lock = Lock()
def _add_reader(self):
with self._read_lock:
self._readers += 1
if self._readers == 1:
self._write_lock.acquire()
def _remove_reader(self):
with self._read_lock:
self._readers -= 1
if self._readers == 0:
self._write_lock.release()
def set_model(self, model_name: str) -> None:
if model_name not in self.list_available_models():
raise Exception(f"Model {model_name} not found")
with self._write_lock:
self._model_name = model_name
def load_model(self, print_log: bool = False, seed: int = None, threads: int = None, kv_cache_type: t.Optional[t.Literal['f16', 'bf16', 'q8_0', 'q5_0', 'q4_0']] = None, context: int = None, temperature: float = None, top_p: float = None, top_k: int = None, min_p: float = None) -> None:
if self.process_is_alive():
raise Exception("A model is already loaded. Use stop() before loading a new model.")
if self._model_name is None:
raise Exception("Model not set")
short_name = self.short_model_name(self._model_name)
if short_name is None:
raise Exception(f"Model {self._model_name} not found")
if seed is None:
seed = -1
if threads is None:
threads = 16
if kv_cache_type is None:
kv_cache_type = 'q8_0'
context = min_none(context, LLMS[short_name]['context'])
if temperature is None:
temperature = LLMS[short_name]['sampling']['temperature']
if top_p is None:
top_p = LLMS[short_name]['sampling']['top_p']
if top_k is None:
top_k = LLMS[short_name]['sampling']['top_k']
if min_p is None:
min_p = LLMS[short_name]['sampling']['min_p']
with self._write_lock:
offload_layers = calculate_offload_layers(self._model_name, short_name)
print(f"Loading model {self._model_name} with {offload_layers} layers offloaded")
command = [
'/opt/llama.cpp/bin/llama-server',
'--threads', str(threads),
'--ctx-size', str(context),
'--flash-attn',
'--no-escape',
'--cache-type-k', kv_cache_type,
'--cache-type-v', kv_cache_type,
'--batch-size', '32',
'--ubatch-size', '16',
'--mlock',
'--n-gpu-layers', str(offload_layers),
'--model', f'/opt/llms/{self._model_name}',
'--seed', str(seed),
'--temp', str(temperature),
'--top-k', str(top_k),
'--top-p', str(top_p),
'--min-p', str(min_p),
'--host', '127.0.0.1',
'--port', '8432',
'--alias', short_name,
]
if print_log:
stdout = None
stderr = None
else:
stdout = PIPE
stderr = PIPE
self._process = Popen(command, stdout=stdout, stderr=stderr, text=True)
return None
def apply_chat_template(self, conversation: t.List[t.Dict[str, str]], enable_thinking: bool = False) -> str:
short_name = self.short_model_name(self._model_name)
chat_template: str = LLMS[short_name]['chat_template']
template = Template(chat_template)
options: t.Dict[str, t.Any] = {
'messages': conversation,
'tools': [],
'add_generation_prompt': True,
'enable_thinking': False,
}
if LLMS[short_name]['thinking']:
if LLMS[short_name]['optional_thinking']:
options['enable_thinking'] = enable_thinking
else:
options['enable_thinking'] = True
else:
options['enable_thinking'] = False
return template.render(**options)
def generate(self, prompt: t.Union[str, t.List[t.Dict[str, str]]], enable_thinking: bool = False, temperature: float = None, top_k: int = None, top_p: float = None, min_p: float = None, n_predict: int = None, grammar: str = None, seed: int = None) -> str: # type: ignore
if isinstance(prompt, list):
prompt = self.apply_chat_template(prompt, enable_thinking)
json_data: t.Dict[str, t.Any] = {
'prompt': prompt,
}
if temperature is not None:
json_data['temperature'] = temperature
if top_k is not None:
json_data['top_k'] = top_k
if top_p is not None:
json_data['top_p'] = top_p
if min_p is not None:
json_data['min_p'] = min_p
if n_predict is not None:
json_data['n_predict'] = n_predict
if grammar is not None:
json_data['grammar'] = grammar
if seed is not None:
json_data['seed'] = seed
self._add_reader()
try:
req = request('POST', 'http://127.0.0.1:8432/completion', json=json_data)
if req.status_code != 200:
raise Exception(req.text)
json_return = req.json()
return json_return['content']
finally:
self._remove_reader()
def process_is_alive(self) -> bool: # type: ignore
self._add_reader()
try:
if self._process is None:
return False
return self._process.poll() is None
finally:
self._remove_reader()
def is_loading(self) -> bool: # type: ignore
self._add_reader()
try:
req = request('GET', 'http://127.0.0.1:8432/health')
return req.status_code == 503
except RequestException:
return False
finally:
self._remove_reader()
def is_running(self) -> bool: # type: ignore
self._add_reader()
try:
req = request('GET', 'http://127.0.0.1:8432/health')
return req.status_code == 200
except RequestException:
return False
finally:
self._remove_reader()
def has_error(self) -> bool: # type: ignore
self._add_reader()
try:
req = request('GET', 'http://127.0.0.1:8432/health')
return req.status_code not in [200, 503]
except RequestException:
return True
finally:
self._remove_reader()
def stop(self) -> None:
with self._write_lock:
if self._process is None:
return None
self._process.terminate()
return None
def kill(self):
with self._write_lock:
if self._process is None:
return None
self._process.kill()
return None
def get_system_message(self) -> t.List[t.Dict[str, str]]:
short_name = self.short_model_name(self._model_name)
system_message = LLMS[short_name]['system_message']
if system_message == '':
return []
return [{'role': 'system', 'content': system_message}]
@staticmethod
def list_available_models() -> t.List[str]:
directory_list = listdir('/opt/llms/')
model_list = []
for entry in directory_list:
if entry.endswith('.gguf') and LLaMaCPP.short_model_name(entry) is not None:
model_list.append(entry)
return model_list
@staticmethod
def short_model_name(model_name: str) -> t.Optional[str]:
for model in sorted(LLMS.keys(), key=lambda x: len(x) , reverse=True):
if model_name.startswith(model):
return model
return None
def min_none(a: t.Any, b: t.Any) -> t.Any:
"""
Returns the minimum of two values, or the single value if one of them is None.
:param a: First value
:param b: Second value
:return: The minimum of a and b, or a/b if one of them is None
"""
if a is None:
return b
if b is None:
return a
return min(a, b)
def calculate_offload_layers(model_name: str, short_model_name: str) -> int:
"""
Calculates the number of layers to offload
:param model_name: The name of the model
:param short_model_name: The short name of the model
:return: The number of layers to offload
"""
free_vram = check_free_vram()
llm_size = getsize(f"/opt/llms/{model_name}") / (1024 ** 2)
llm_size = llm_size * 1.1
layers = LLMS[short_model_name]['layers']
vram_per_layer = llm_size / layers
return min(int(free_vram / vram_per_layer), layers)
def check_free_vram() -> int:
"""
Checks the amount of free VRAM on the GPU
:return: The amount of free VRAM in MB
"""
nvidia_smi = run(['nvidia-smi', '--query-gpu=memory.free', '--format=csv,nounits,noheader'], stdout=PIPE, text=True)
if nvidia_smi.returncode != 0:
raise Exception(nvidia_smi.stderr)
return int(nvidia_smi.stdout)
|