Skip to content
This repository was archived by the owner on Sep 28, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ cert.pem
key.pem

# build
dist/
/dist/
build/
node_modules/
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ https://tangledgroup.github.io/llama-cpp-wasm/
```bash
git clone https://github.com/tangledgroup/llama-cpp-wasm.git
cd llama-cpp-wasm
./build-single-thread.sh
./build-multi-thread.sh
./build.sh
```

Once build is complete you can find `llama.cpp` built in `dist/llama-st` and `dist/llama-mt` directory.
Expand Down
6 changes: 3 additions & 3 deletions build-multi-thread.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ if [ -d $LLAMA_CPP_WASM_DIST_LLAMA_DIR ]; then
fi

mkdir -p $LLAMA_CPP_WASM_DIST_LLAMA_DIR
cp -rv src/llama/* $LLAMA_CPP_WASM_DIST_LLAMA_DIR
cp -rv src/llama/* $LLAMA_CPP_WASM_DIST_DIR
cp $LLAMA_CPP_BUILD_DIR/bin/main.* $LLAMA_CPP_WASM_DIST_LLAMA_DIR

rm -rf docs/llama-mt
cp -rv $LLAMA_CPP_WASM_DIST_LLAMA_DIR docs/
rm -rf docs/$LLAMA_CPP_WASM_DIST_DIR
cp -rv $LLAMA_CPP_WASM_DIST_DIR docs/
6 changes: 3 additions & 3 deletions build-single-thread.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ if [ -d $LLAMA_CPP_WASM_DIST_LLAMA_DIR ]; then
fi

mkdir -p $LLAMA_CPP_WASM_DIST_LLAMA_DIR
cp -rv src/llama/* $LLAMA_CPP_WASM_DIST_LLAMA_DIR
cp -rv src/llama/* $LLAMA_CPP_WASM_DIST_DIR
cp $LLAMA_CPP_BUILD_DIR/bin/main.* $LLAMA_CPP_WASM_DIST_LLAMA_DIR

rm -rf docs/llama-st
cp -rv $LLAMA_CPP_WASM_DIST_LLAMA_DIR docs/
rm -rf docs/$LLAMA_CPP_WASM_DIST_DIR
cp -rv $LLAMA_CPP_WASM_DIST_DIR docs/
File renamed without changes.
22 changes: 22 additions & 0 deletions docs/dist/llama-mt/main.js

Large diffs are not rendered by default.

Binary file added docs/dist/llama-mt/main.wasm
Binary file not shown.
16 changes: 16 additions & 0 deletions docs/dist/llama-st/main.js

Large diffs are not rendered by default.

Binary file added docs/dist/llama-st/main.wasm
Binary file not shown.
File renamed without changes.
9 changes: 6 additions & 3 deletions docs/llama-mt/main-worker.js → docs/dist/main-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { action } from "./actions.js";
import { loadBinaryResource } from "./utility.js";
import Module from "./main.js";
import { isMultiThreadSupported, loadBinaryResource } from "./utility.js";

// WASM Module
let module;
Expand Down Expand Up @@ -60,6 +59,10 @@ const initWorker = async (modelPath) => {
}],
};

const { default: Module } = await (isMultiThreadSupported()
? import("./llama-mt/main.js")
: import("./llama-st/main.js"));

module = await Module(emscrModule);

const initCallback = (bytes) => {
Expand Down Expand Up @@ -103,7 +106,7 @@ const run_main = (
"--prompt", prompt.toString(),
];

if (!!globalThis.SharedArrayBuffer) {
if (isMultiThreadSupported()) {
args.push("--threads");
args.push((navigator.hardwareConcurrency).toString());
}
Expand Down
26 changes: 26 additions & 0 deletions docs/llama-st/utility.js → docs/dist/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,29 @@ export async function loadBinaryResource(url, callback) {

req.send(null);
}

export function isMultiThreadSupported() {
// If 'SharedArrayBuffer' is not available, WebAssembly threads will not work.
if (typeof SharedArrayBuffer === "undefined") {
return false;
}

try {
// Test for transferability of SABs (for browsers. needed for Firefox)
// https://groups.google.com/forum/#!msg/mozilla.dev.platform/IHkBZlHETpA/dwsMNchWEQAJ
if (typeof MessageChannel !== "undefined") {
new MessageChannel().port1.postMessage(new SharedArrayBuffer(1));
}

// Test for WebAssembly threads capability (for both browsers and Node.js)
// This typed array is a WebAssembly program containing threaded instructions.
return WebAssembly.validate(
new Uint8Array([
0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 5,
4, 1, 3, 1, 1, 10, 11, 1, 9, 0, 65, 0, 254, 16, 2, 0, 26, 11,
])
);
} catch (e) {
return false;
}
}
79 changes: 0 additions & 79 deletions docs/example-multi-thread.html

This file was deleted.

79 changes: 0 additions & 79 deletions docs/example-single-thread.html

This file was deleted.

68 changes: 0 additions & 68 deletions docs/example-single-thread.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/example-multi-thread.js → docs/example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LlamaCpp } from "./llama-mt/llama.js";
import { LlamaCpp } from "./dist/llama.js";

let app;
const buttonRun = document.querySelector("#run");
Expand Down
Loading