Skip to content

Improve Shared Library Loading Mechanism - #10

Merged
abetlen merged 2 commits into
abetlen:mainfrom
MillionthOdin16:patch-1
Apr 3, 2023
Merged

abetlen merged 2 commits into
abetlen:mainfrom
MillionthOdin16:patch-1

Conversation

@MillionthOdin16

Copy link
Copy Markdown
Contributor

This pull request refines the shared library loading code by introducing the load_shared_library function, which accepts the library's base name (lib_base_name) as input. Key improvements include:

  1. Automatic determination of the library's file extension based on the operating system (.so for Linux, .dylib for macOS, .dll for Windows). This also solves an issue that occurs when more than one lib exists within the same dir.

  2. Flexible library paths: Constructs two paths based on lib_base_name, accommodating slight differences in naming conventions used by llamacpp and llama-cpp-python.

  3. On Windows (Python 3.8+), addition of the library directory to the DLL search path.

  4. Attempted loading of the shared library via ctypes.CDLL and appropriate error handling, including raising RuntimeError for load failures and FileNotFoundError for missing libraries.

Make shared library code more robust with some platform specific functionality and more descriptive errors when failures occur
@abetlen

abetlen commented Apr 3, 2023

Copy link
Copy Markdown
Owner

Thank you for the contribution, I'll test this out shortly on a couple platforms and merge it in if everything works fine.
We have a few open issues around M1 support which I suspect are due to my hacky solution so hopefully this helps fix that.

Comment thread llama_cpp/llama_cpp.py Outdated
_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):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to _load_shared_library to avoid it appearing in any public APIs

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I saw you marked this as resolved, is there a reason to not have this be _load_shared_library?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Sorry haha. It wasn't an issue for me so I clicked so quick. I thought I was accepting the suggestion xD

@abetlen abetlen Apr 3, 2023 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, LGTM. Thank you for this!

Comment thread llama_cpp/llama_cpp.py Outdated
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"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should be _lib_base_name

@abetlen

abetlen commented Apr 3, 2023

Copy link
Copy Markdown
Owner

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
@abetlen
abetlen merged commit 1d9a988 into abetlen:main Apr 3, 2023
ithllc added a commit to tqcli/llama-cpp-python-turboquant that referenced this pull request Apr 28, 2026
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.
ithllc added a commit to tqcli/llama-cpp-python-turboquant that referenced this pull request Apr 28, 2026
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants