-
Notifications
You must be signed in to change notification settings - Fork 22
/
tests.py
171 lines (130 loc) · 4.84 KB
/
tests.py
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
import asyncio
import os
import json
import shutil
import inspect
import requests
import subprocess
import platform
import importlib.util
import sys
config = None
def check_nvidia_gpu():
try:
# Utilizza torch per verificare la presenza di una GPU NVIDIA
return torch.cuda.is_available() and 'NVIDIA' in torch.cuda.get_device_name(0)
except Exception as e:
print(f"Error while checking for NVIDIA GPU: {e}")
return False
def get_cuda_version():
try:
if torch.cuda.is_available():
cuda_version = torch.version.cuda.replace(".","").strip()
return "cu"+cuda_version
else:
return "No NVIDIA GPU available"
except Exception as e:
print(f"Error while checking CUDA version: {e}")
return "Unable to determine CUDA version"
def check_avx2_support():
import cpuinfo
try:
info = cpuinfo.get_cpu_info()
return 'avx2' in info['flags']
except Exception as e:
print(f"Error while checking AVX2 support: {e}")
return False
def get_python_version():
if "3.9" in platform.python_version():
return "39"
elif "3.10" in platform.python_version():
return "310"
elif "3.11" in platform.python_version():
return "311"
else:
return None
def get_os():
return platform.system()
def get_os_bit():
return platform.architecture()[0].replace("bit","")
import requests
def get_platform_tag(_os):
#return the first tag in the list of tags
try:
import packaging.tags
response = requests.get("https://api.github.com/repos/abetlen/llama-cpp-python/releases/latest")
jresponse= response.json()
return extract_platform_tag(jresponse,list(packaging.tags.sys_tags())[0],_os)
except:
return None
def install_package(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", package])
def install_llama():
try:
gpu = check_nvidia_gpu()
avx2 = check_avx2_support()
lcpVersion = get_last_llcpppy_version()
python_version = get_python_version()
_os = get_os()
os_bit = get_os_bit()
platform_tag = get_platform_tag(_os)
print(f"Python version: {python_version}")
print(f"OS: {_os}")
print(f"OS bit: {os_bit}")
print(f"Platform tag: {platform_tag}")
if python_version == None:
print("Unsupported Python version. Please use Python 3.9, 3.10 or 3.11.")
return
#python -m pip install llama-cpp-python --force-reinstall --no-deps --index-url=https://jllllll.github.io/llama-cpp-python-cuBLAS-wheels/AVX2/cu117
if avx2:
avx="AVX2"
else:
avx="AVX"
if gpu:
cuda = get_cuda_version()
print(f"--index-url=https://jllllll.github.io/llama-cpp-python-cuBLAS-wheels/{avx}/{cuda}")
else:
print(f"https://github.com/abetlen/llama-cpp-python/releases/download/v{lcpVersion}/llama_cpp_python-{lcpVersion}-{platform_tag}.whl")
except Exception as e:
print(f"Error while installing LLAMA: {e}")
# llama wheels https://github.com/jllllll/llama-cpp-python-cuBLAS-wheels
def check_module(package):
import importlib
try:
print("Detected: ", package)
importlib.import_module(package)
return True
except ImportError:
return False
import zipfile
import re
def extract_platform_tag(jresponse,tag,_os):
if _os.lower()== "linux" or _os.lower()=="macosx":
print(jresponse)
for res in jresponse["assets"]:
url = res["browser_download_url"]
print(url)
pattern = r'.*-((cp\d+-cp\d+)-(manylinux|macosx)_(\d+_\d+)_(x86_64|i686)\.whl)$'
match = re.match(pattern, url)
pattern_tag = r'((cp\d+-cp\d+)-(manylinux|macosx)_\d+_\d+_(x86_64|i686))$'
match_tag = re.match(pattern_tag, tag)
if match_tag:
rl_platform_tag = f"{match_tag.group(2)}-{match_tag.group(3)}_**_{match_tag.group(4)}"
#print(rl_platform_tag)
if match:
# Estrai il platform tag dal match
url_platform_tag = f"{match.group(2)}-{match.group(3)}_**_{match.group(5)}"
final_platform_tag =f"{match.group(2)}-{match.group(3)}_{match.group(4)}_{match.group(5)}"
if rl_platform_tag == url_platform_tag:
return final_platform_tag
return None
else:
return tag
def get_last_llcpppy_version():
try:
import requests
response = requests.get("https://api.github.com/repos/abetlen/llama-cpp-python/releases/latest")
return response.json()["tag_name"].replace("v","")
except:
return "0.2.20"
install_llama()