From 512b3111377c72e5815fbf9742154f5e656be094 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:00:29 -0700 Subject: [PATCH] Improve the llama-cpp-python exception messages --- modules/llama_cpp_python_hijack.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/llama_cpp_python_hijack.py b/modules/llama_cpp_python_hijack.py index c9813708..6ec15626 100644 --- a/modules/llama_cpp_python_hijack.py +++ b/modules/llama_cpp_python_hijack.py @@ -13,11 +13,21 @@ imported_module = None def llama_cpp_lib(): global imported_module + def module_to_purpose(module_name): + if module_name == 'llama_cpp': + return 'CPU' + elif module_name == 'llama_cpp_cuda_tensorcores': + return 'tensorcores' + elif module_name == 'llama_cpp_cuda': + return 'default' + + return 'unknown' + return_lib = None if shared.args.cpu: if imported_module and imported_module != 'llama_cpp': - raise Exception(f"Cannot import 'llama_cpp' because '{imported_module}' is already imported. See issue #1575 in llama-cpp-python. Please restart the server before attempting to use a different version of llama-cpp-python.") + raise Exception(f"The {module_to_purpose(imported_module)} version of llama-cpp-python is already loaded. Switching to the CPU version currently requires a server restart.") try: return_lib = importlib.import_module('llama_cpp') imported_module = 'llama_cpp' @@ -26,7 +36,7 @@ def llama_cpp_lib(): if shared.args.tensorcores and return_lib is None: if imported_module and imported_module != 'llama_cpp_cuda_tensorcores': - raise Exception(f"Cannot import 'llama_cpp_cuda_tensorcores' because '{imported_module}' is already imported. See issue #1575 in llama-cpp-python. Please restart the server before attempting to use a different version of llama-cpp-python.") + raise Exception(f"The {module_to_purpose(imported_module)} version of llama-cpp-python is already loaded. Switching to the tensorcores version currently requires a server restart.") try: return_lib = importlib.import_module('llama_cpp_cuda_tensorcores') imported_module = 'llama_cpp_cuda_tensorcores' @@ -35,7 +45,7 @@ def llama_cpp_lib(): if return_lib is None: if imported_module and imported_module != 'llama_cpp_cuda': - raise Exception(f"Cannot import 'llama_cpp_cuda' because '{imported_module}' is already imported. See issue #1575 in llama-cpp-python. Please restart the server before attempting to use a different version of llama-cpp-python.") + raise Exception(f"The {module_to_purpose(imported_module)} version of llama-cpp-python is already loaded. Switching to the default version currently requires a server restart.") try: return_lib = importlib.import_module('llama_cpp_cuda') imported_module = 'llama_cpp_cuda' @@ -44,7 +54,7 @@ def llama_cpp_lib(): if return_lib is None and not shared.args.cpu: if imported_module and imported_module != 'llama_cpp': - raise Exception(f"Cannot import 'llama_cpp' because '{imported_module}' is already imported. See issue #1575 in llama-cpp-python. Please restart the server before attempting to use a different version of llama-cpp-python.") + raise Exception(f"The {module_to_purpose(imported_module)} version of llama-cpp-python is already loaded. Switching to the CPU version currently requires a server restart.") try: return_lib = importlib.import_module('llama_cpp') imported_module = 'llama_cpp'