Repository navigation
Improve Shared Library Loading Mechanism - #10
Conversation
Make shared library code more robust with some platform specific functionality and more descriptive errors when failures occur
|
Thank you for the contribution, I'll test this out shortly on a couple platforms and merge it in if everything works fine. |
| _base_path.glob("*.so"), _base_path.glob("*.dylib"), _base_path.glob("*.dll") | ||
| ) | ||
| _lib = ctypes.CDLL(str(_lib_path)) | ||
| def load_shared_library(lib_base_name): |
There was a problem hiding this comment.
Rename to _load_shared_library to avoid it appearing in any public APIs
There was a problem hiding this comment.
Sorry, I saw you marked this as resolved, is there a reason to not have this be _load_shared_library?
There was a problem hiding this comment.
Oops! Sorry haha. It wasn't an issue for me so I clicked so quick. I thought I was accepting the suggestion xD
There was a problem hiding this comment.
@MillionthOdin16 is the intention to be able to patch the library with a custom dll at runtime correct? I could be fine with that but _lib would need to be updateable as well for that to work.
There was a problem hiding this comment.
I haven't narrowed down the library issue yet. I'm thinking of that as a separate issue to address. I need to figure out why it's happening first.
I think I got everything that you intended now. Is there anything else you want tweaked?
| raise FileNotFoundError(f"Shared library with base name '{lib_base_name}' not found") | ||
|
|
||
| # Specify the base name of the shared library to load | ||
| lib_base_name = "llama" |
There was a problem hiding this comment.
Same here, should be _lib_base_name
|
Tested and works! Once those changes are in I'll merge this and push a new release to PyPI |
lib_base_name and load_shared_library to _lib_base_name and _load_shared_library
iter abetlen#9 Linux CUDA cells failed at the final executable link of llama-mtmd-cli with: undefined reference to cudaFree@libcudart.so.12 CMake found CUDAToolkit 12.8.61 correctly; all .cu files compiled fine; libggml-cuda.so built. The link error is on llama-mtmd-cli (a multi-modal CLI tool) — its CMake target_link_libraries lists ggml-cuda + driver stub but doesn't propagate cudart from ggml-cuda. This is an upstream-CMake bug in llama.cpp's tools/mtmd subdir. The Python wheel only needs libllama.so (the Python bindings link it directly via _llama_cpp). The mtmd CLI tool is not invoked from Python. Skip the tools build entirely with -DLLAMA_BUILD_TOOLS=OFF. Also resolves the iter abetlen#9 Windows cp312 cpu transient (HTTP 502 on pip download) — re-running will likely flake-recover even without the tools change.
…link with linker flags iter abetlen#10 set LLAMA_BUILD_TOOLS=OFF to skip the failing llama-mtmd-cli link, but that's a feature regression: libmtmd.so is the unified multimodal runtime that the Python wheel ships for Llava/MiniCPM-V/Gemma 3 vision/ Qwen-VL/audio support. Python handlers (MtmdChatHandler, Llava15ChatHandler, MoondreamChatHandler) load these .so files via ctypes in-process — they're not optional. Proper fix: keep all targets building, force cudart onto every executable and shared-library link line via: CMAKE_EXE_LINKER_FLAGS=-Wl,--no-as-needed,-lcudart CMAKE_SHARED_LINKER_FLAGS=-Wl,--no-as-needed,-lcudart plus the existing LIBRARY_PATH / LD_LIBRARY_PATH / LDFLAGS / CUDAToolkit_ROOT. Why CMAKE_EXE_LINKER_FLAGS works where LDFLAGS didn't: CMake constructs linker invocations from target_link_libraries; LDFLAGS env is advisory and CMake's auto-pickup is inconsistent. CMAKE_EXE_LINKER_FLAGS is mandatory — added to every link command. --no-as-needed prevents ld from dropping the direct -lcudart since cudart symbols are referenced from libggml-cuda.so transitively, not the executable's own .o files. Macos x86_64 stays dropped (issue #4 covers re-enable for 0.7.1).
This pull request refines the shared library loading code by introducing the
load_shared_libraryfunction, which accepts the library's base name (lib_base_name) as input. Key improvements include:Automatic determination of the library's file extension based on the operating system (
.sofor Linux,.dylibfor macOS,.dllfor Windows). This also solves an issue that occurs when more than one lib exists within the same dir.Flexible library paths: Constructs two paths based on lib_base_name, accommodating slight differences in naming conventions used by
llamacppandllama-cpp-python.On Windows (Python 3.8+), addition of the library directory to the DLL search path.
Attempted loading of the shared library via
ctypes.CDLLand appropriate error handling, including raisingRuntimeErrorfor load failures andFileNotFoundErrorfor missing libraries.