|
import subprocess |
|
import re |
|
import sys |
|
|
|
def install_llama_cpp_python(cuda=None): |
|
try: |
|
|
|
result = subprocess.run(['nvcc', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
|
version_output = result.stdout |
|
version_match = re.search(r"release (\d+\.\d+),", version_output) |
|
if not version_match: |
|
raise RuntimeError("CUDA version not found in nvcc output.") |
|
|
|
cuda_version = version_match.group(1) |
|
version_map = { |
|
"12.1": "cu121", |
|
"12.2": "cu122", |
|
"12.3": "cu123", |
|
"12.4": "cu124", |
|
} |
|
major_minor = '.'.join(cuda_version.split('.')[:2]) |
|
if cuda is None: |
|
cuda_suffix = version_map.get(major_minor) |
|
else: |
|
cuda_suffix = cuda |
|
|
|
if not cuda_suffix: |
|
raise ValueError(f"No suitable wheel found for CUDA version {cuda_version}.") |
|
|
|
extra_index_url = f"https://abetlen.github.io/llama-cpp-python/whl/{cuda_suffix}" |
|
subprocess.run([sys.executable, '-m', 'pip', 'install', 'llama-cpp-python', '--extra-index-url', extra_index_url]) |
|
print(f"Successfully installed llama-cpp-python with CUDA {cuda_suffix}") |
|
|
|
except FileNotFoundError: |
|
print("Error: nvcc (CUDA) is not installed or not in PATH.") |
|
except Exception as e: |
|
print(f"An error occurred: {e}") |
|
|
|
|
|
install_llama_cpp_python('cu124') |