From f219bd9d05535ce6a18ff408e17690beb5579cb9 Mon Sep 17 00:00:00 2001 From: Luca Beurer-Kellner Date: Tue, 15 Aug 2023 17:54:50 +0200 Subject: [PATCH] llama.cpp: import more defensively --- src/lmql/models/lmtp/backends/llama_cpp_model.py | 3 ++- src/lmql/models/lmtp/backends/lmtp_model.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lmql/models/lmtp/backends/llama_cpp_model.py b/src/lmql/models/lmtp/backends/llama_cpp_model.py index 956a5465..32fe5b31 100644 --- a/src/lmql/models/lmtp/backends/llama_cpp_model.py +++ b/src/lmql/models/lmtp/backends/llama_cpp_model.py @@ -2,7 +2,6 @@ import sys import numpy as np -from llama_cpp import Llama, LlamaTokenizer import lmql.utils.nputil as nputil from lmql.models.lmtp.backends.lmtp_model import (LMTPModel, LMTPModelResult, @@ -10,6 +9,8 @@ class LlamaCppModel(LMTPModel): def __init__(self, model_identifier, **kwargs): + from llama_cpp import Llama + self.model_identifier = model_identifier self.kwargs = kwargs diff --git a/src/lmql/models/lmtp/backends/lmtp_model.py b/src/lmql/models/lmtp/backends/lmtp_model.py index ec2931fd..f1f17f57 100644 --- a/src/lmql/models/lmtp/backends/lmtp_model.py +++ b/src/lmql/models/lmtp/backends/lmtp_model.py @@ -107,9 +107,9 @@ def wrapper(loader): import importlib if module_dependencies is not None: for module in module_dependencies: - try: - importlib.import_module(module) - except ImportError: + # check without importing + spec = importlib.util.find_spec(module) + if spec is None: def error_func(*args, **kwargs): assert False, "To use the {} backend, please install the '{}' package.".format(name, module) LMTPModel.registry[name] = error_func