From 466bca187e000e08d9328f6b20deda8544951a46 Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Wed, 3 Jul 2024 02:02:39 -0400 Subject: [PATCH] feat: Update ggml lib install dir --- CMakeLists.txt | 6 +++--- ggml/ggml.py | 14 ++++++++++---- pyproject.toml | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ceecc61..1b53968 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,9 @@ message(SKBUILD_STATE="${SKBUILD_STATE}") if(SKBUILD_STATE STREQUAL "editable") # Temporary fix for https://github.com/scikit-build/scikit-build-core/issues/374 - set(GGML_PYTHON_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ggml) + set(GGML_PYTHON_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ggml/lib) else() - set(GGML_PYTHON_INSTALL_DIR ${SKBUILD_PLATLIB_DIR}/ggml) + set(GGML_PYTHON_INSTALL_DIR ${SKBUILD_PLATLIB_DIR}/ggml/lib) endif() set(BUILD_SHARED_LIBS "On") @@ -38,4 +38,4 @@ install( install( FILES $ DESTINATION ${GGML_PYTHON_INSTALL_DIR} -) \ No newline at end of file +) diff --git a/ggml/ggml.py b/ggml/ggml.py index 0fc2efe..071b498 100644 --- a/ggml/ggml.py +++ b/ggml/ggml.py @@ -59,7 +59,11 @@ import ctypes import pathlib import functools -import importlib.resources +try: + # Python < 3.9 + import importlib_resources +except ImportError: + import importlib.resources as importlib_resources from typing import ( cast, Any, @@ -79,7 +83,7 @@ # Load the library def load_shared_library(module_name: str, lib_base_name: str): # Construct the paths to the possible shared library names - base_path = pathlib.Path(__file__).parent.resolve() + base_path = pathlib.Path(__file__).parent.resolve() / "lib" # Searching for the library in the current directory under the name "libggml" (default name # for ggml) and "ggml" (default name for this repo) lib_names: List[str] = [ @@ -92,7 +96,8 @@ def load_shared_library(module_name: str, lib_base_name: str): for lib_name in lib_names: try: - with importlib.resources.path(module_name, lib_name) as p: + with importlib_resources.as_file(importlib_resources.files(module_name).joinpath("lib", lib_name)) as p: # type: ignore + p = cast(pathlib.Path, p) if os.path.exists(p): path = p break @@ -107,6 +112,7 @@ def load_shared_library(module_name: str, lib_base_name: str): cdll_args = dict() # type: ignore # Add the library directory to the DLL search path on Windows (if needed) if sys.platform == "win32" and sys.version_info >= (3, 8): + os.environ["PATH"] = str(base_path) + os.pathsep + os.environ["PATH"] os.add_dll_directory(str(base_path)) cdll_args["winmode"] = 0 @@ -7752,7 +7758,7 @@ class gguf_init_params(ctypes.Structure): if TYPE_CHECKING: no_alloc: bool - ctx: ggml_context_p + ctx: CtypesPointer[ggml_context_p] _fields_ = [ ("no_alloc", ctypes.c_bool), diff --git a/pyproject.toml b/pyproject.toml index 942f1a5..c188d6e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,8 @@ authors = [ requires-python = ">=3.7" dependencies = [ "numpy>=1.20.0", - "typing_extensions>=4.6.3" + "typing_extensions>=4.6.3", + "importlib_resources>=6.4.0; python_version < '3.9'", ] classifiers = [ "Programming Language :: Python :: 3",