Skip to content

Commit 3df9a49

Browse files
unamedkrclaude
andauthored
fix(wasm): drop pthreads — fixes UI hang during prefill and generation (#32)
pthreads + ASYNCIFY conflict: emscripten_sleep() on the main thread doesn't yield properly when pthreads are active (Atomics.wait-based scheduling interferes with ASYNCIFY's stack unwinding). Result: UI hangs during both prefill and generation despite yield calls. Fix: remove -pthread and PTHREAD_POOL_SIZE. Keep ASYNCIFY + SIMD128 + relaxed-simd + O3 + LTO. The emscripten_sleep(0) calls in both prefill (every 2 tokens) and generation (every 4 tokens) now work correctly, keeping the browser responsive. quant.h's matmul still uses pthread_create internally, but without -pthread they become no-ops in Emscripten — single-threaded matmul with SIMD vectorization. Binary: 384K → 320K (-17%, no pthread runtime). Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3fdbaf6 commit 3df9a49

3 files changed

Lines changed: 6 additions & 9 deletions

File tree

wasm/build.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/bin/bash
22
# Build quant.cpp WASM demo
3-
# pthreads for parallel matmul + SIMD + ASYNCIFY for UI streaming
3+
# SIMD + ASYNCIFY for streaming (no pthreads — conflicts with ASYNCIFY sleep)
44
set -e
55

66
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
77
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
88

9-
echo "=== Building quant.cpp WASM (pthreads + SIMD) ==="
9+
echo "=== Building quant.cpp WASM (SIMD + ASYNCIFY) ==="
1010

1111
if ! command -v emcc &>/dev/null; then
1212
echo "Error: emcc not found. Install Emscripten SDK."
@@ -22,24 +22,21 @@ emcc "$SCRIPT_DIR/quant_wasm.c" \
2222
-msimd128 \
2323
-mrelaxed-simd \
2424
-flto \
25-
-pthread \
2625
-s WASM=1 \
27-
-s INITIAL_MEMORY=256MB \
28-
-s MAXIMUM_MEMORY=4GB \
2926
-s ALLOW_MEMORY_GROWTH=1 \
27+
-s MAXIMUM_MEMORY=4GB \
28+
-s INITIAL_MEMORY=256MB \
3029
-s EXPORTED_FUNCTIONS='["_main","_wasm_load_model","_wasm_generate","_wasm_generate_async","_wasm_model_info","_wasm_is_ready","_malloc","_free"]' \
3130
-s EXPORTED_RUNTIME_METHODS='["UTF8ToString","allocateUTF8","FS"]' \
3231
-s FORCE_FILESYSTEM=1 \
3332
-s MODULARIZE=0 \
34-
-s ENVIRONMENT='web,worker' \
33+
-s ENVIRONMENT=web \
3534
-s NO_EXIT_RUNTIME=1 \
3635
-s ASSERTIONS=0 \
3736
-s STACK_SIZE=1MB \
3837
-s ASYNCIFY \
3938
-s 'ASYNCIFY_IMPORTS=["emscripten_sleep"]' \
4039
-s ASYNCIFY_STACK_SIZE=65536 \
41-
-s PTHREAD_POOL_SIZE=4 \
42-
-s PTHREAD_POOL_SIZE_STRICT=0 \
4340
-lm \
4441
-DNDEBUG \
4542
-D__EMSCRIPTEN__ \

wasm/quant.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm/quant.wasm

-42.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)