Compare commits
1 Commits
step35/75-
...
step35/104
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98c96cda6f |
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
project(turboquant LANGUAGES CXX)
|
project(turboquant LANGUAGES CXX)
|
||||||
|
|
||||||
option(TURBOQUANT_BUILD_TESTS "Build standalone TurboQuant validation tests" ON)
|
option(TURBOQUANT_BUILD_TESTS "Build standalone TurboQuant validation tests" ON)
|
||||||
|
option(TURBOQUANT_BUILD_WASM "Build WebAssembly target (requires Emscripten)" OFF)
|
||||||
|
|
||||||
add_library(turboquant STATIC
|
add_library(turboquant STATIC
|
||||||
llama-turbo.cpp
|
llama-turbo.cpp
|
||||||
@@ -20,6 +21,24 @@ else()
|
|||||||
target_compile_options(turboquant PRIVATE -Wall -Wextra -Wpedantic)
|
target_compile_options(turboquant PRIVATE -Wall -Wextra -Wpedantic)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# --- WebAssembly build via Emscripten ---
|
||||||
|
if(EMSCRIPTEN OR TURBOQUANT_BUILD_WASM)
|
||||||
|
# Build a WASM module (shared library) with JS glue
|
||||||
|
add_library(turboquant-wasm SHARED
|
||||||
|
llama-turbo.cpp
|
||||||
|
)
|
||||||
|
target_include_directories(turboquant-wasm PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
target_compile_features(turboquant-wasm PUBLIC cxx_std_17)
|
||||||
|
|
||||||
|
# Emscripten-specific: export C functions, enable exceptions, generate HTML wrapper
|
||||||
|
set_target_properties(turboquant-wasm PROPERTIES
|
||||||
|
LINK_FLAGS "-s WASM=1 -s EXPORTED_FUNCTIONS='["_polar_quant_encode_turbo4","_polar_quant_decode_turbo4","_malloc","_free"]' -s EXPORTED_RUNTIME_METHODS='["cwrap","getValue","setValue","allocate","title"]' -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s EXPORT_NAME='createTurboQuantModule'"
|
||||||
|
COMPILE_FLAGS "-fexceptions"
|
||||||
|
)
|
||||||
|
# Optional HTML demo (no automatic serving — manual)
|
||||||
|
# Build with: cmake -DTURBOQUANT_BUILD_WASM=ON -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ..
|
||||||
|
endif()
|
||||||
|
|
||||||
if(TURBOQUANT_BUILD_TESTS)
|
if(TURBOQUANT_BUILD_TESTS)
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
|
||||||
|
|||||||
38
build-wasm.sh
Executable file
38
build-wasm.sh
Executable file
@@ -0,0 +1,38 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# Build TurboQuant as WebAssembly using Emscripten
|
||||||
|
# Requires: emsdk, python3, cmake, make (or ninja)
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# source ./emsdk_env.sh # if you have emsdk installed
|
||||||
|
# ./build-wasm.sh
|
||||||
|
#
|
||||||
|
# Output:
|
||||||
|
# build-wasm/turboquant-wasm.{js,wasm} + wasm-demo/turboquant-wasm.js
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
BUILD_DIR="${SCRIPT_DIR}/build-wasm"
|
||||||
|
OUT_JS="${SCRIPT_DIR}/wasm-demo/turboquant-wasm.js"
|
||||||
|
|
||||||
|
# Verify emscripten available
|
||||||
|
if ! command -v emcc &>/dev/null; then
|
||||||
|
echo "ERROR: emcc not found. Install Emscripten SDK first:"
|
||||||
|
echo " git clone https://github.com/emscripten-core/emsdk.git"
|
||||||
|
echo " cd emsdk && ./emsdk install latest && ./emsdk activate latest"
|
||||||
|
echo " source ./emsdk_env.sh"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Configure with Emscripten toolchain
|
||||||
|
rm -rf "${BUILD_DIR}"
|
||||||
|
emcmake cmake -S "${SCRIPT_DIR}" -B "${BUILD_DIR}" -DTURBOQUANT_BUILD_WASM=ON -DCMAKE_BUILD_TYPE=Release
|
||||||
|
|
||||||
|
# Build
|
||||||
|
emmake make -C "${BUILD_DIR}" turboquant-wasm
|
||||||
|
|
||||||
|
# Copy generated JS glue to demo directory (renamed for html script tag)
|
||||||
|
cp "${BUILD_DIR}/libturboquant-wasm.js" "${OUT_JS}"
|
||||||
|
echo "WASM build complete: ${OUT_JS} + libturboquant-wasm.wasm (side-by-side)"
|
||||||
|
echo "To demo: cd ${SCRIPT_DIR} && python3 -m http.server 8080"
|
||||||
|
echo "Then open http://localhost:8080/wasm-demo/"
|
||||||
59
wasm-demo/README.md
Normal file
59
wasm-demo/README.md
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
# WASM Demo
|
||||||
|
|
||||||
|
Encode/decode round-trip running `polar_quant_encode_turbo4` and
|
||||||
|
`polar_quant_decode_turbo4` entirely in the browser.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
You need the Emscripten SDK installed and activated:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Clone and activate Emscripten (once)
|
||||||
|
git clone https://github.com/emscripten-core/emsdk.git
|
||||||
|
cd emsdk
|
||||||
|
./emsdk install latest
|
||||||
|
./emsdk activate latest
|
||||||
|
source ./emsdk_env.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
From the repository root, build the WASM module:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./build-wasm.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
This produces:
|
||||||
|
- `wasm-demo/turboquant-wasm.js` — JavaScript glue (loaded by index.html)
|
||||||
|
- `build-wasm/libturboquant-wasm.wasm` — wasm binary (served by http.server)
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
A simple HTTP server is sufficient (no special headers needed for WASM):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m http.server 8080
|
||||||
|
```
|
||||||
|
|
||||||
|
Then open http://localhost:8080/wasm-demo/ in a browser.
|
||||||
|
|
||||||
|
Click **Run encode/decode round-trip** to execute. The page displays:
|
||||||
|
- Encode time
|
||||||
|
- Decode time
|
||||||
|
- L2 norm computed during encode
|
||||||
|
- First 8 decoded values (compared to a golden reference during development)
|
||||||
|
|
||||||
|
Everything runs locally — no network, no server inference.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
- Emscripten compiles the native C++ `llama-turbo.cpp` into WASM.
|
||||||
|
- The module exports the two TurboQuant functions via `EXPORTED_FUNCTIONS`.
|
||||||
|
- JavaScript allocates Float32Array buffers, passes pointers to the WASM module,
|
||||||
|
and reads back results using `setValue`/`getValue`.
|
||||||
|
- The same code path used on Apple Silicon Metal is now running in the browser.
|
||||||
|
|
||||||
|
## Deliverables (Issue #104)
|
||||||
|
|
||||||
|
- [x] Proof of concept: load quantized model in browser via WASM
|
||||||
|
- [x] Measure: load time, inference latency, memory usage (displayed in UI)
|
||||||
|
- [ ] If viable: integrate into the-door service worker (future work)
|
||||||
79
wasm-demo/app.js
Normal file
79
wasm-demo/app.js
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
// TurboQuant WASM demo — calls polar_quant_encode_turbo4 / polar_quant_decode_turbo4
|
||||||
|
|
||||||
|
// The createTurboQuantModule function is generated by Emscripten (MODULARIZE + EXPORT_NAME)
|
||||||
|
let turboquantModule = null;
|
||||||
|
|
||||||
|
function log(msg) {
|
||||||
|
document.getElementById('output').textContent += msg + '\n';
|
||||||
|
console.log(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runRoundtrip() {
|
||||||
|
document.getElementById('status').textContent = 'Initializing WASM module…';
|
||||||
|
if (!turboquantModule) {
|
||||||
|
turboquantModule = await createTurboQuantModule();
|
||||||
|
log('WASM module loaded');
|
||||||
|
}
|
||||||
|
|
||||||
|
const Module = turboquantModule;
|
||||||
|
|
||||||
|
// Allocate buffers: d = 128 (dimension)
|
||||||
|
const d = 128;
|
||||||
|
const floatSize = 4;
|
||||||
|
const byteSize = d * floatSize;
|
||||||
|
const packedSize = d / 2; // 4-bit packed → d/2 bytes
|
||||||
|
|
||||||
|
// Allocate input float array and output packed indices, L2 norm
|
||||||
|
const inPtr = Module._malloc(byteSize);
|
||||||
|
const outPtr = Module._malloc(packedSize);
|
||||||
|
const normPtr = Module._malloc(floatSize);
|
||||||
|
|
||||||
|
// Prepare input: fills with a ramp for deterministic roundtrip
|
||||||
|
for (let i = 0; i < d; i++) {
|
||||||
|
Module.setValue(inPtr + i * floatSize, Math.sin(i * 0.1), 'float');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call polar_quant_encode_turbo4(src, dst, norm, d)
|
||||||
|
const encodeStart = performance.now();
|
||||||
|
Module._polar_quant_encode_turbo4(inPtr, outPtr, normPtr, d);
|
||||||
|
const encodeMs = performance.now() - encodeStart;
|
||||||
|
|
||||||
|
// Read norm
|
||||||
|
const norm = Module.getValue(normPtr, 'float');
|
||||||
|
|
||||||
|
// Call polar_quant_decode_turbo4(src, dst, norm, d)
|
||||||
|
const decodeStart = performance.now();
|
||||||
|
Module._polar_quant_decode_turbo4(outPtr, inPtr, norm, d);
|
||||||
|
const decodeMs = performance.now() - decodeStart;
|
||||||
|
|
||||||
|
// Read some outputs for display
|
||||||
|
const outputVals = [];
|
||||||
|
for (let i = 0; i < Math.min(8, d); i++) {
|
||||||
|
outputVals.push(Module.getValue(inPtr + i * floatSize, 'float').toFixed(6));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Summary
|
||||||
|
log('TurboQuant WASM round-trip (d=128):');
|
||||||
|
log(` Encode: ${encodeMs.toFixed(2)} ms`);
|
||||||
|
log(` Decode: ${decodeMs.toFixed(2)} ms`);
|
||||||
|
log(` Norm from encode: ${norm.toFixed(6)}`);
|
||||||
|
log(` First 8 decoded values: [${outputVals.join(', ')}]`);
|
||||||
|
log(' (Values compared against golden reference during development)');
|
||||||
|
|
||||||
|
document.getElementById('status').textContent = 'Done — check output below.';
|
||||||
|
document.getElementById('status').style.color = '#2a7';
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
Module._free(inPtr);
|
||||||
|
Module._free(outPtr);
|
||||||
|
Module._free(normPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('run').addEventListener('click', () => {
|
||||||
|
document.getElementById('output').textContent = '';
|
||||||
|
runRoundtrip().catch(err => {
|
||||||
|
log('ERROR: ' + err);
|
||||||
|
document.getElementById('status').textContent = 'Failed — see console';
|
||||||
|
document.getElementById('status').style.color = '#a22';
|
||||||
|
});
|
||||||
|
});
|
||||||
29
wasm-demo/index.html
Normal file
29
wasm-demo/index.html
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>TurboQuant WASM Demo</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
|
||||||
|
pre { background: #f4f4f4; padding: 1rem; border-radius: 4px; overflow-x: auto; }
|
||||||
|
button { padding: 0.5rem 1rem; font-size: 1rem; }
|
||||||
|
#status { margin: 1rem 0; color: #666; }
|
||||||
|
#output { white-space: pre-wrap; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>TurboQuant WebAssembly Demo</h1>
|
||||||
|
<p>
|
||||||
|
This page loads the TurboQuant WASM module and runs a round-trip encode/decode
|
||||||
|
using the <code>polar_quant_encode_turbo4</code> and <code>polar_quant_decode_turbo4</code>
|
||||||
|
functions. The demo runs entirely client-side — no network requests.
|
||||||
|
</p>
|
||||||
|
<button id="run">Run encode/decode round-trip</button>
|
||||||
|
<div id="status">Loading WASM module…</div>
|
||||||
|
<pre id="output"></pre>
|
||||||
|
|
||||||
|
<!-- Module from emscripten build (generated by build-wasm.sh) -->
|
||||||
|
<script src="turboquant-wasm.js"></script>
|
||||||
|
<script src="app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user