Skip to content

Commit 5602a88

Browse files
committed
feat: Add llama.cpp web assembly support
Primarily for calculating embeddings on the client Signed-off-by: Gordon Smith <[email protected]>
1 parent 7c8edd5 commit 5602a88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5115
-848
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.nyc_output/
2-
/.vscode/c_cpp_properties.json
3-
/.vscode/ipch
2+
.vscode/c_cpp_properties.json
3+
.vscode/ipch
4+
.vscode/settings.json
45
.nx/
56
bin/
67
build/
@@ -18,6 +19,7 @@ dist-test/
1819
/emsdk*
1920
/lib-*
2021
node_modules/
22+
/packages/llama/docs
2123
/rust
2224
/third-party
2325
/src-ts/*.wasm.ts

.vscode/launch.json

+19-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "test-node",
9+
"type": "node",
10+
"request": "launch",
11+
"cwd": "${workspaceFolder}",
12+
"runtimeArgs": [
13+
"run-script",
14+
"test-node"
15+
],
16+
"runtimeExecutable": "npm",
17+
"skipFiles": [
18+
"<node_internals>/**"
19+
],
20+
"outFiles": [
21+
"${workspaceFolder}/packages/**/*.js",
22+
"${workspaceFolder}/packages/**/*.c",
23+
"!**/node_modules/**"
24+
],
25+
},
726
{
827
"name": "index-browser",
928
"type": "msedge",
@@ -75,24 +94,6 @@
7594
"webpack:///*": "/*"
7695
},
7796
},
78-
{
79-
"name": "test-node",
80-
"type": "node",
81-
"request": "launch",
82-
"runtimeArgs": [
83-
"run-script",
84-
"test-node"
85-
],
86-
"runtimeExecutable": "npm",
87-
"skipFiles": [
88-
"<node_internals>/**"
89-
],
90-
"outFiles": [
91-
"${workspaceFolder}/**/*.js",
92-
"${workspaceFolder}/**/*.c",
93-
"!**/node_modules/**"
94-
],
95-
},
9697
{
9798
"name": "esbuild",
9899
"type": "node",

.vscode/settings.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"cmake.cmakePath": "${workspaceFolder}/scripts/cmake.sh",
3-
"ecl.launchConfiguration": "not found",
4-
"ecl.targetCluster": {}
2+
"cmake.cmakePath": "${workspaceFolder}/scripts/cmake.sh"
53
}

.vscode/tasks.json

+48-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
2-
// See https://go.microsoft.com/fwlink/?LinkId=733558
3-
// for the documentation about the tasks.json format
42
"version": "2.0.0",
53
"tasks": [
64
{
75
"type": "npm",
8-
"label": "Compile Types Watch",
9-
"script": "compile-types-watch",
6+
"label": "build-types-watch",
7+
"script": "build-types-watch",
108
"problemMatcher": [
119
"$tsc-watch"
1210
],
@@ -16,9 +14,48 @@
1614
},
1715
{
1816
"type": "npm",
19-
"label": "Compile TypeScript Watch",
20-
"script": "compile-ts-watch",
21-
"problemMatcher": [],
17+
"label": "build-ts-watch",
18+
"script": "build-ts-watch",
19+
"problemMatcher": [
20+
"$tsc-watch"
21+
],
22+
"presentation": {
23+
"group": "group-build"
24+
}
25+
},
26+
{
27+
"type": "npm",
28+
"label": "build-cpp",
29+
"script": "build-cpp",
30+
"problemMatcher": [
31+
"$gcc"
32+
],
33+
"presentation": {
34+
"group": "group-build"
35+
}
36+
},
37+
{
38+
"type": "npm",
39+
"label": "build-cpp-watch",
40+
"script": "build-cpp-watch",
41+
"dependsOn": [
42+
"build-cpp"
43+
],
44+
"problemMatcher": {
45+
"owner": "cpp",
46+
"fileLocation": [
47+
"relative",
48+
"${workspaceFolder}"
49+
],
50+
"pattern": {
51+
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
52+
"file": 1,
53+
"line": 2,
54+
"column": 3,
55+
"severity": 4,
56+
"message": 5
57+
}
58+
},
2259
"presentation": {
2360
"group": "group-build"
2461
}
@@ -27,23 +64,21 @@
2764
"type": "npm",
2865
"label": "Web Server",
2966
"script": "serve",
30-
"problemMatcher": [],
3167
"presentation": {
3268
"group": "group-build"
3369
}
3470
},
3571
{
3672
"label": "build",
3773
"dependsOn": [
38-
"Compile Types Watch",
39-
"Compile TypeScript Watch",
40-
"Web Server"
74+
"build-types-watch",
75+
"build-ts-watch",
76+
"build-cpp-watch"
4177
],
4278
"group": {
4379
"kind": "build",
4480
"isDefault": true
45-
},
46-
"problemMatcher": []
81+
}
4782
}
4883
]
4984
}

CMakeLists.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ set(CMAKE_INSTALL_PREFIX "..")
88
set(VCPKG_INCLUDE_DIR ${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include)
99

1010
# See: https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
11-
set(EM_CPP_FLAGS "")
11+
set(EM_CPP_FLAGS
12+
"-flto"
13+
)
1214

1315
set(EM_LINK_FLAGS
1416
"-sASSERTIONS=0"
@@ -35,11 +37,12 @@ set(EM_LINK_FLAGS
3537
"-sUSE_GLFW=0"
3638
"-sALLOW_UNIMPLEMENTED_SYSCALLS=1"
3739
"-sINCOMING_MODULE_JS_API=\"['wasmBinary']\""
40+
"--no-entry"
3841
"--pre-js ${CMAKE_CURRENT_SOURCE_DIR}/src-cpp/src/pre.js"
3942
"--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src-cpp/src/post.js"
4043
)
4144

42-
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
45+
if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
4346
set(EM_LINK_FLAGS
4447
${EM_LINK_FLAGS}
4548
"-sUSE_ES6_IMPORT_META=1"
@@ -79,8 +82,8 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
7982
set(PACK_MODE "-d")
8083
endif ()
8184

82-
add_subdirectory(src-cpp)
8385
add_subdirectory(packages/base91/src-cpp)
8486
add_subdirectory(packages/expat/src-cpp)
8587
add_subdirectory(packages/graphviz/src-cpp)
88+
add_subdirectory(packages/llama/src-cpp)
8689
add_subdirectory(packages/zstd/src-cpp)

CMakePresets.json

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
{
1313
"name": "vcpkg-emscripten",
14+
"hidden": true,
1415
"inherits": [
1516
"vcpkg"
1617
],
@@ -31,6 +32,16 @@
3132
"CMAKE_BUILD_TYPE": "Debug"
3233
}
3334
},
35+
{
36+
"name": "vcpkg-emscripten-RelWithDebInfo",
37+
"inherits": [
38+
"vcpkg-emscripten"
39+
],
40+
"binaryDir": "${sourceDir}/build",
41+
"cacheVariables": {
42+
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
43+
}
44+
},
3445
{
3546
"name": "vcpkg-emscripten-MinSizeRel",
3647
"inherits": [

docs/.vitepress/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default {
3838
{ text: 'DuckDB', link: '/duckdb/src/duckdb/classes/DuckDB' },
3939
{ text: 'Expat', link: '/expat/src/expat/classes/Expat' },
4040
{ text: 'Graphviz', link: '/graphviz/src/graphviz/classes/Graphviz' },
41+
{ text: 'Llama', link: '/llama/src/llama/classes/Llama' },
4142
{ text: 'Zstd', link: '/zstd/src/zstd/classes/Zstd' },
4243
]
4344
}

docs/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ features:
3535
- title: GraphViz
3636
details: The Graphviz layout algorithms take descriptions of graphs in a simple text language, and make diagrams in useful formats, such as images and SVG for web pages or display in an interactive graph browser.
3737
link: /graphviz/src/graphviz/classes/Graphviz
38+
- title: Llama
39+
details: Inference of Meta's LLaMA model (and others) in pure C/C++.
40+
link: /llama/src/llama/classes/Llama
3841
- title: Zstd
3942
details: Zstandard is a fast compression algorithm, providing high compression ratios and is backed by an extremely fast decoder.
4043
link: /zstd/src/zstd/classes/Zstd

0 commit comments

Comments
 (0)