Skip to content

Commit b55fc24

Browse files
committed
feat: enable mtmd for emscripten
1 parent 72a01bb commit b55fc24

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

‎.github/workflows/build-and-release.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ jobs:
159159
CIBW_BUILD_VERBOSITY: "1"
160160
CIBW_REPAIR_WHEEL_COMMAND: ""
161161
CIBW_BEFORE_TEST: "curl -L --fail --retry 3 -o /tmp/stories260K.gguf https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories260K.gguf"
162-
CIBW_TEST_COMMAND: "python -c \"from llama_cpp import Llama; llm = Llama(model_path='/tmp/stories260K.gguf', n_ctx=64, n_batch=8, n_threads=1, verbose=False); print('loaded', llm.n_vocab(), llm.n_ctx()); print('generated', llm('Once upon a', max_tokens=1, temperature=0)['choices'][0]['text'])\""
163-
CMAKE_ARGS: "-DLLAVA_BUILD=OFF -DLLAMA_WASM_MEM64=OFF -DEMSCRIPTEN_SYSTEM_PROCESSOR=wasm32 -DGGML_NATIVE=OFF -DGGML_OPENMP=OFF -DGGML_METAL=OFF -DGGML_BLAS=OFF -DGGML_CUDA=OFF -DGGML_HIP=OFF -DGGML_VULKAN=OFF -DGGML_OPENCL=OFF -DGGML_RPC=OFF -DLLAMA_CURL=OFF -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TOOLS=OFF -DLLAMA_BUILD_SERVER=OFF"
162+
CIBW_TEST_COMMAND: "python -c \"import llama_cpp.mtmd_cpp as mtmd; from llama_cpp import Llama; print('mtmd marker', mtmd.mtmd_default_marker().decode()); llm = Llama(model_path='/tmp/stories260K.gguf', n_ctx=64, n_batch=8, n_threads=1, verbose=False); print('loaded', llm.n_vocab(), llm.n_ctx()); print('generated', llm('Once upon a', max_tokens=1, temperature=0)['choices'][0]['text'])\""
163+
CMAKE_ARGS: "-DLLAMA_WASM_MEM64=OFF -DEMSCRIPTEN_SYSTEM_PROCESSOR=wasm32 -DGGML_NATIVE=OFF -DGGML_OPENMP=OFF -DGGML_METAL=OFF -DGGML_BLAS=OFF -DGGML_CUDA=OFF -DGGML_HIP=OFF -DGGML_VULKAN=OFF -DGGML_OPENCL=OFF -DGGML_RPC=OFF -DLLAMA_CURL=OFF -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TOOLS=OFF -DLLAMA_BUILD_SERVER=OFF"
164164
with:
165165
output-dir: wheelhouse
166166

‎CMakeLists.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ if (LLAMA_BUILD)
8080
set(CMAKE_SYSTEM_PROCESSOR wasm32 CACHE STRING "Target processor" FORCE)
8181
endif()
8282

83-
set(LLAVA_BUILD OFF CACHE BOOL "Build llava shared library and install alongside python package" FORCE)
8483
set(LLAMA_WASM_MEM64 OFF CACHE BOOL "llama.cpp: enable wasm64 memory" FORCE)
8584
set(GGML_NATIVE OFF CACHE BOOL "ggml: enable -march=native" FORCE)
8685
set(GGML_OPENMP OFF CACHE BOOL "ggml: use OpenMP" FORCE)

‎llama_cpp/_ctypes_extensions.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,21 @@ def load_shared_library(lib_base_name: str, base_path: pathlib.Path):
7979
else f"{lib_dir}{os.pathsep}{ld_library_path}"
8080
)
8181

82-
if lib_base_name == "llama":
83-
for dependency in ("ggml-base", "ggml-cpu", "ggml"):
84-
dependency_path = (
85-
base_path / f"lib{dependency}{_EMSCRIPTEN_SIDE_MODULE_SUFFIX}"
86-
)
87-
if dependency_path.exists():
88-
try:
89-
ctypes.CDLL(str(dependency_path), **cdll_args) # type: ignore
90-
except Exception as e:
91-
raise RuntimeError(
92-
f"Failed to load shared library '{dependency_path}': {e}"
93-
)
82+
emscripten_dependencies = {
83+
"llama": ("ggml-base", "ggml-cpu", "ggml"),
84+
"mtmd": ("ggml-base", "ggml-cpu", "ggml", "llama"),
85+
}
86+
for dependency in emscripten_dependencies.get(lib_base_name, ()):
87+
dependency_path = (
88+
base_path / f"lib{dependency}{_EMSCRIPTEN_SIDE_MODULE_SUFFIX}"
89+
)
90+
if dependency_path.exists():
91+
try:
92+
ctypes.CDLL(str(dependency_path), **cdll_args) # type: ignore
93+
except Exception as e:
94+
raise RuntimeError(
95+
f"Failed to load shared library '{dependency_path}': {e}"
96+
)
9497

9598
# Try to load the shared library, handling potential errors
9699
for lib_path in lib_paths:

0 commit comments

Comments
 (0)