Startup wasm worker all at once

This commit is contained in:
msqr1
2024-09-27 17:34:30 -07:00
parent 4f78f596ae
commit e3f19bc6cf
9 changed files with 25 additions and 28 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "Util.h" #include "Util.h"
#include <vosk_api.h> #include "vosk_api.h"
struct CommonModel { struct CommonModel {
bool normalMdl; bool normalMdl;

View File

@@ -1,6 +1,7 @@
#include "Util.h" #include "Util.h"
#include <emscripten/wasm_worker.h>
#include <emscripten/em_js.h> #include "emscripten/wasm_worker.h"
#include "emscripten/em_js.h"
EM_JS(void, mtFireEv, (int index, int typeIdx, int content), { EM_JS(void, mtFireEv, (int index, int typeIdx, int content), {
objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) })); objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) }));
@@ -48,9 +49,9 @@ int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
} }
return Successful; return Successful;
} }
void Worker::startup(int _fn, int _pool) { static void workerStartup(int _pool) {
WorkerPool& pool{*reinterpret_cast<WorkerPool*>(_pool)}; WorkerPool& pool{*reinterpret_cast<WorkerPool*>(_pool)};
std::function<void()>& fn{*reinterpret_cast<std::function<void()>*>(_fn)}; std::function<void()> fn;
while(!pool.done) { while(!pool.done) {
// Wait until unlocked // Wait until unlocked
emscripten_atomic_wait_u32(&pool.qLock, true, -1); emscripten_atomic_wait_u32(&pool.qLock, true, -1);
@@ -70,15 +71,24 @@ void Worker::startup(int _fn, int _pool) {
fn(); fn();
} }
} }
using _startupFn = void(*)(int);
EM_JS(void, startupWorkers, (_startupFn startupFn, WorkerPool* pool), {
Object.values(_wasmWorkers).forEach(worker => {
worker.postMessage({
"_wsc": startupFn,
"x": [ pool ]
});
})
})
static constexpr int workerStack{32768}; static constexpr int workerStack{32768};
static std::array<std::byte, MAX_WORKERS * workerStack> stacks; static std::array<std::byte, MAX_WORKERS * workerStack> stacks;
#undef MAX_WORKERS
WorkerPool::WorkerPool() { WorkerPool::WorkerPool() {
for(unsigned int i = 0; i < workers.size(); i++) { for(int i{}; i < MAX_WORKERS; ++i) {
workers[i].handle = emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack); emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack);
emscripten_wasm_worker_post_function_vii(workers[i].handle, Worker::startup, reinterpret_cast<int>(&workers[i].fn),reinterpret_cast<int>(this));
} }
startupWorkers(workerStartup, this);
} }
#undef MAX_WORKERS
WorkerPool::~WorkerPool() { WorkerPool::~WorkerPool() {
done = true; done = true;
emscripten_atomic_store_u32(&qLock, false); emscripten_atomic_store_u32(&qLock, false);

View File

@@ -1,11 +1,11 @@
#pragma once #pragma once
#include <filesystem> #include <filesystem>
#include <functional>
#include <variant> #include <variant>
#include <fstream> #include <fstream>
#include <functional>
#include <emscripten/atomic.h> #include "emscripten/atomic.h"
#include <emscripten/console.h> #include "emscripten/console.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -30,12 +30,6 @@ enum UntarStatus {
FailedWrite, FailedWrite,
FailedClose FailedClose
}; };
struct WorkerPool;
struct Worker {
int handle;
std::function<void()> fn;
static void startup(int _fn, int _pool);
};
#ifndef MAX_WORKERS #ifndef MAX_WORKERS
#define MAX_WORKERS 1 #define MAX_WORKERS 1
#endif #endif
@@ -43,7 +37,6 @@ struct WorkerPool {
bool qLock{true}; // True is locked, false is unlocked bool qLock{true}; // True is locked, false is unlocked
bool done{}; bool done{};
std::queue<std::function<void()>> taskQ; std::queue<std::function<void()>> taskQ;
std::array<Worker, MAX_WORKERS> workers;
WorkerPool(); WorkerPool();
~WorkerPool(); ~WorkerPool();
void exec(std::function<void()> fn); void exec(std::function<void()> fn);

View File

@@ -71,12 +71,6 @@ if [ ! -d "$VOSK" ]; then
emar -rcs vosk.a ${VOSK_FILES//.cc/.o} emar -rcs vosk.a ${VOSK_FILES//.cc/.o}
fi fi
cd "$VOSK"/src
VOSK_FILES="recognizer.cc language_model.cc model.cc spk_model.cc vosk_api.cc" &&
em++ -O3 -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -Wno-deprecated -I. -I"$KALDI"/src -I"$OPENFST"/include recognizer.cc -c &&
emar -rcs vosk.a ${VOSK_FILES//.cc/.o}
echo "GOod"
cd "$SRC" && cd "$SRC" &&
em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O3 -std=c++23 -fno-rtti -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sINCOMING_MODULE_JS_API=wasmMemory,instantiateWasm,wasm -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -L"$KALDI"/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L"$OPENFST"/lib -l:libfst.a -l:libfstngram.a -L"$CLAPACK_WASM" -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L"$VOSK"/src -l:vosk.a -lembind -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js --closure 1 -o ../Vosklet.js em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O3 -std=c++23 -fno-rtti -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sINCOMING_MODULE_JS_API=wasmMemory,instantiateWasm,wasm -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -L"$KALDI"/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L"$OPENFST"/lib -l:libfst.a -l:libfstngram.a -L"$CLAPACK_WASM" -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L"$VOSK"/src -l:vosk.a -lembind -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js --closure 1 -o ../Vosklet.js
cd .. && cd .. &&

2
test
View File

@@ -36,7 +36,7 @@ OPENFST=$(realpath openfst)
CLAPACK_WASM=$(realpath clapack-wasm) CLAPACK_WASM=$(realpath clapack-wasm)
cd src && cd src &&
MODE=1 && # 0: Debug, 1: Optimized release MODE=0 && # 0: Debug, 1: Optimized release
echo "Mode = $MODE" && echo "Mode = $MODE" &&
if [ "$MODE" = 0 ]; then if [ "$MODE" = 0 ]; then
em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O0 -std=c++23 -g3 -Wall -Wextra -Wpedantic -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -fno-rtti -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sSTACK_OVERFLOW_CHECK=2 -sASSERTIONS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sRUNTIME_DEBUG -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sDISABLE_EXCEPTION_CATCHING=0 -sINCOMING_MODULE_JS_API=wasmMemory,instantiateWasm,wasm -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -L"$KALDI"/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L"$OPENFST"/lib -l:libfst.a -l:libfstngram.a -L"$CLAPACK_WASM" -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L"$VOSK"/src -l:vosk.a -lembind -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js -o ../test.js em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O0 -std=c++23 -g3 -Wall -Wextra -Wpedantic -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -fno-rtti -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sSTACK_OVERFLOW_CHECK=2 -sASSERTIONS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sRUNTIME_DEBUG -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sDISABLE_EXCEPTION_CATCHING=0 -sINCOMING_MODULE_JS_API=wasmMemory,instantiateWasm,wasm -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -L"$KALDI"/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L"$OPENFST"/lib -l:libfst.a -l:libfstngram.a -L"$CLAPACK_WASM" -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L"$VOSK"/src -l:vosk.a -lembind -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js -o ../test.js