Skip to content

Commit 3656c6d

Browse files
committed
feat: bootstrap llama web bridge source
0 parents  commit 3656c6d

7 files changed

Lines changed: 2254 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.build/
2+
dist/

CMakeLists.txt

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(llamadart_webgpu_bridge C CXX)
3+
4+
if (NOT EMSCRIPTEN)
5+
message(FATAL_ERROR "This project must be configured with emcmake/emcc")
6+
endif()
7+
8+
if (NOT DEFINED LLAMA_CPP_DIR)
9+
set(LLAMA_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/llama_cpp")
10+
endif()
11+
12+
if (NOT EXISTS "${LLAMA_CPP_DIR}/CMakeLists.txt")
13+
message(FATAL_ERROR "LLAMA_CPP_DIR is invalid: ${LLAMA_CPP_DIR}")
14+
endif()
15+
16+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/artifacts")
17+
18+
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
19+
20+
set(LLAMA_BUILD_COMMON OFF CACHE BOOL "" FORCE)
21+
set(LLAMA_BUILD_TESTS OFF CACHE BOOL "" FORCE)
22+
set(LLAMA_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
23+
set(LLAMA_BUILD_SERVER OFF CACHE BOOL "" FORCE)
24+
set(LLAMA_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
25+
set(LLAMA_HTTPLIB OFF CACHE BOOL "" FORCE)
26+
set(LLAMA_OPENSSL OFF CACHE BOOL "" FORCE)
27+
set(LLAMA_BUILD_HTML OFF CACHE BOOL "" FORCE)
28+
set(LLAMA_WASM_SINGLE_FILE OFF CACHE BOOL "" FORCE)
29+
30+
set(GGML_NATIVE OFF CACHE BOOL "" FORCE)
31+
set(GGML_OPENMP OFF CACHE BOOL "" FORCE)
32+
set(GGML_WEBGPU ON CACHE BOOL "" FORCE)
33+
set(GGML_VULKAN OFF CACHE BOOL "" FORCE)
34+
set(GGML_METAL OFF CACHE BOOL "" FORCE)
35+
set(GGML_BLAS OFF CACHE BOOL "" FORCE)
36+
37+
add_subdirectory("${LLAMA_CPP_DIR}" "${CMAKE_BINARY_DIR}/llama_cpp")
38+
39+
find_package(Threads REQUIRED)
40+
41+
set(MTMD_AUDIO_SRC "${LLAMA_CPP_DIR}/tools/mtmd/mtmd-audio.cpp")
42+
set(MTMD_AUDIO_PATCHED "${CMAKE_BINARY_DIR}/generated/mtmd-audio-single-thread.cpp")
43+
44+
file(READ "${MTMD_AUDIO_SRC}" MTMD_AUDIO_CONTENT)
45+
string(FIND "${MTMD_AUDIO_CONTENT}" "4, // n_threads" MTMD_AUDIO_THREAD_MARKER_INDEX)
46+
if (MTMD_AUDIO_THREAD_MARKER_INDEX EQUAL -1)
47+
message(FATAL_ERROR "mtmd-audio.cpp thread marker not found; update single-thread wasm patch")
48+
endif()
49+
string(REPLACE
50+
"4, // n_threads"
51+
"1, // n_threads (patched for single-threaded wasm)"
52+
MTMD_AUDIO_CONTENT
53+
"${MTMD_AUDIO_CONTENT}")
54+
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/generated")
55+
file(WRITE "${MTMD_AUDIO_PATCHED}" "${MTMD_AUDIO_CONTENT}")
56+
57+
add_library(llamadart_mtmd STATIC
58+
"${LLAMA_CPP_DIR}/tools/mtmd/mtmd.cpp"
59+
"${MTMD_AUDIO_PATCHED}"
60+
"${LLAMA_CPP_DIR}/tools/mtmd/mtmd-helper.cpp"
61+
"${LLAMA_CPP_DIR}/tools/mtmd/clip.cpp"
62+
"${LLAMA_CPP_DIR}/tools/mtmd/models/cogvlm.cpp"
63+
"${LLAMA_CPP_DIR}/tools/mtmd/models/conformer.cpp"
64+
"${LLAMA_CPP_DIR}/tools/mtmd/models/glm4v.cpp"
65+
"${LLAMA_CPP_DIR}/tools/mtmd/models/internvl.cpp"
66+
"${LLAMA_CPP_DIR}/tools/mtmd/models/kimivl.cpp"
67+
"${LLAMA_CPP_DIR}/tools/mtmd/models/kimik25.cpp"
68+
"${LLAMA_CPP_DIR}/tools/mtmd/models/llama4.cpp"
69+
"${LLAMA_CPP_DIR}/tools/mtmd/models/llava.cpp"
70+
"${LLAMA_CPP_DIR}/tools/mtmd/models/minicpmv.cpp"
71+
"${LLAMA_CPP_DIR}/tools/mtmd/models/pixtral.cpp"
72+
"${LLAMA_CPP_DIR}/tools/mtmd/models/qwen2vl.cpp"
73+
"${LLAMA_CPP_DIR}/tools/mtmd/models/qwen3vl.cpp"
74+
"${LLAMA_CPP_DIR}/tools/mtmd/models/siglip.cpp"
75+
"${LLAMA_CPP_DIR}/tools/mtmd/models/whisper-enc.cpp"
76+
"${LLAMA_CPP_DIR}/tools/mtmd/models/mobilenetv5.cpp"
77+
"${LLAMA_CPP_DIR}/tools/mtmd/models/youtuvl.cpp"
78+
)
79+
80+
target_compile_features(llamadart_mtmd PRIVATE cxx_std_17)
81+
target_compile_options(llamadart_mtmd PRIVATE
82+
"-sMEMORY64=1"
83+
)
84+
target_include_directories(llamadart_mtmd PRIVATE
85+
"${LLAMA_CPP_DIR}/tools/mtmd"
86+
"${LLAMA_CPP_DIR}"
87+
"${LLAMA_CPP_DIR}/vendor"
88+
)
89+
target_link_libraries(llamadart_mtmd PRIVATE ggml llama Threads::Threads)
90+
91+
if (NOT MSVC)
92+
target_compile_options(llamadart_mtmd PRIVATE -Wno-cast-qual)
93+
endif()
94+
95+
add_executable(llama_webgpu_core src/llama_webgpu_core.cpp)
96+
97+
target_compile_features(llama_webgpu_core PRIVATE cxx_std_17)
98+
99+
target_compile_options(llama_webgpu_core PRIVATE
100+
"-sMEMORY64=1"
101+
)
102+
103+
target_include_directories(llama_webgpu_core PRIVATE
104+
"${LLAMA_CPP_DIR}/include"
105+
"${LLAMA_CPP_DIR}/ggml/include"
106+
"${LLAMA_CPP_DIR}/tools/mtmd"
107+
)
108+
109+
target_link_libraries(llama_webgpu_core PRIVATE llama llamadart_mtmd)
110+
111+
target_link_options(llama_webgpu_core PRIVATE
112+
"-sMEMORY64=1"
113+
"-sALLOW_MEMORY_GROWTH=1"
114+
"-sASSERTIONS=1"
115+
"-sJSPI=1"
116+
"-sJSPI_EXPORTS=['llamadart_webgpu_probe','llamadart_webgpu_load_model','llamadart_webgpu_mmproj_load','llamadart_webgpu_tokenize_to_json','llamadart_webgpu_detokenize_from_json','llamadart_webgpu_generate','llamadart_webgpu_begin_generation','llamadart_webgpu_next_token','llamadart_webgpu_shutdown']"
117+
"-sMODULARIZE=1"
118+
"-sEXPORT_ES6=1"
119+
"-sEXPORT_NAME=createLlamaWebGpuCoreModule"
120+
"-sENVIRONMENT=web,worker"
121+
"-sEXPORTED_RUNTIME_METHODS=['FS','ccall','UTF8ToString']"
122+
"-sEXPORTED_FUNCTIONS=['_main','_llamadart_webgpu_probe','_llamadart_webgpu_backends_json','_llamadart_webgpu_last_error','_llamadart_webgpu_load_model','_llamadart_webgpu_mmproj_load','_llamadart_webgpu_mmproj_free','_llamadart_webgpu_mmproj_supports_vision','_llamadart_webgpu_mmproj_supports_audio','_llamadart_webgpu_media_clear_pending','_llamadart_webgpu_media_add_file','_llamadart_webgpu_media_add_encoded','_llamadart_webgpu_media_add_rgb','_llamadart_webgpu_media_add_audio_f32','_llamadart_webgpu_tokenize_to_json','_llamadart_webgpu_last_tokens_json','_llamadart_webgpu_detokenize_from_json','_llamadart_webgpu_last_detokenized','_llamadart_webgpu_generate','_llamadart_webgpu_begin_generation','_llamadart_webgpu_next_token','_llamadart_webgpu_last_piece','_llamadart_webgpu_end_generation','_llamadart_webgpu_request_cancel','_llamadart_webgpu_last_output','_llamadart_webgpu_get_context_size','_llamadart_webgpu_model_meta_json','_llamadart_webgpu_shutdown']"
123+
)
124+
125+
set_target_properties(llama_webgpu_core PROPERTIES
126+
OUTPUT_NAME "llama_webgpu_core"
127+
SUFFIX ".js"
128+
)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Jhin Lee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# llama-web-bridge
2+
3+
Reusable llama.cpp web bridge runtime (JS + WASM).
4+
5+
This repository provides:
6+
7+
- `src/llama_webgpu_core.cpp` (native bridge core)
8+
- `js/llama_webgpu_bridge.js` (JS runtime wrapper)
9+
- `CMakeLists.txt` for Emscripten builds
10+
11+
## Build
12+
13+
Requirements:
14+
15+
- Emscripten SDK (`emcmake`, `emcc`) in `PATH`
16+
- llama.cpp source checkout
17+
18+
Build command:
19+
20+
```bash
21+
./scripts/build_bridge.sh
22+
```
23+
24+
Useful environment variables:
25+
26+
- `LLAMA_CPP_DIR` (path to llama.cpp source)
27+
- `BUILD_DIR` (cmake build dir)
28+
- `OUT_DIR` (output directory; defaults to `dist/`)
29+
30+
Build outputs:
31+
32+
- `dist/llama_webgpu_bridge.js`
33+
- `dist/llama_webgpu_core.js`
34+
- `dist/llama_webgpu_core.wasm`
35+
36+
## Publishing
37+
38+
Published, versioned artifacts are consumed from:
39+
40+
- `leehack/llama-web-bridge-assets`

0 commit comments

Comments
 (0)