From 12e9232066d074f65cdb8642a4eac96ee3978ead Mon Sep 17 00:00:00 2001 From: abhi-agg <66322306+abhi-agg@users.noreply.github.com> Date: Wed, 24 Mar 2021 17:10:42 +0100 Subject: [PATCH] Patch WASM artifacts to run optimized (wormhole enabled) inference (#68) * A script to patch the wasm artifacts to use wormhole via APIs that instantiate WASM module * Updated README * Load just production ready models * Shallow clone bergamot-models repo since it has such a large history * Improved wasm test_page - test page can load all 5 language pairs - Use intgemm.alpha* models * Refactor the code that patches wasm artifacts to enable wormhole Co-authored-by: Andre Natal Co-authored-by: Motin --- .github/workflows/macos-custom-marian-wasm.yml | 4 ++++ README.md | 15 ++++++++++----- wasm/patch-artifacts-enable-wormhole.sh | 7 +++++++ wasm/test_page/bergamot.html | 5 ++++- wasm/test_page/start_server.sh | 4 +++- 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 wasm/patch-artifacts-enable-wormhole.sh diff --git a/.github/workflows/macos-custom-marian-wasm.yml b/.github/workflows/macos-custom-marian-wasm.yml index 3e49369..00f5cf7 100644 --- a/.github/workflows/macos-custom-marian-wasm.yml +++ b/.github/workflows/macos-custom-marian-wasm.yml @@ -33,6 +33,10 @@ jobs: working-directory: build-wasm run: emmake make -j2 + - name: Instantiate simd wormhole + working-directory: build-wasm + run: bash ../wasm/patch-artifacts-enable-wormhole.sh + - name: Check artifacts working-directory: build-wasm run: | diff --git a/README.md b/README.md index 2b7f0a6..e70e8cd 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,8 @@ Bergamot translator provides a unified API for ([Marian NMT](https://marian-nmt. If you want to package bergamot project specific models, please follow these instructions: ```bash mkdir models - git clone https://github.com/mozilla-applied-ml/bergamot-models - cp -rf bergamot-models/* models + git clone --depth 1 --branch main --single-branch https://github.com/mozilla-applied-ml/bergamot-models + cp -rf bergamot-models/prod/* models gunzip models/*/* ``` @@ -75,11 +75,16 @@ Bergamot translator provides a unified API for ([Marian NMT](https://marian-nmt. emmake make -j ``` - The artefacts (.js and .wasm files) will be available in `wasm` folder of build directory ("build-wasm" in this case). + The wasm artifacts (.js and .wasm files) will be available in `wasm` folder of build directory ("build-wasm" in this case). + + 3. Enable SIMD Wormhole via Wasm instantiation API in generated artifacts + ```bash + bash ../wasm/patch-artifacts-enable-wormhole.sh + ``` #### Recompiling -As long as you don't update any submodule, just follow steps in `4.ii` to recompile.\ -If you update a submodule, execute following command before executing steps in `4.ii` to recompile. +As long as you don't update any submodule, just follow steps in `4.ii` and `4.iii` to recompile.\ +If you update a submodule, execute following command before executing steps in `4.ii` and `4.iii` to recompile. ```bash git submodule update --init --recursive ``` diff --git a/wasm/patch-artifacts-enable-wormhole.sh b/wasm/patch-artifacts-enable-wormhole.sh new file mode 100644 index 0000000..c16ba66 --- /dev/null +++ b/wasm/patch-artifacts-enable-wormhole.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +echo "Patching wasm artifacts to enable wormhole via APIs that compile and instantiate wasm module" +sed -i.bak 's/var result = WebAssembly.instantiateStreaming(response, info);/var result = WebAssembly.instantiateStreaming(response, info, {simdWormhole:true});/g' wasm/bergamot-translator-worker.js +sed -i.bak 's/return WebAssembly.instantiate(binary, info);/return WebAssembly.instantiate(binary, info, {simdWormhole:true});/g' wasm/bergamot-translator-worker.js +sed -i.bak 's/var module = new WebAssembly.Module(bytes);/var module = new WebAssembly.Module(bytes, {simdWormhole:true});/g' wasm/bergamot-translator-worker.js +echo "Done" diff --git a/wasm/test_page/bergamot.html b/wasm/test_page/bergamot.html index b4d2027..4f1f2a0 100644 --- a/wasm/test_page/bergamot.html +++ b/wasm/test_page/bergamot.html @@ -32,6 +32,9 @@ + + + @@ -70,7 +73,7 @@ En consecuencia, durante el año 2011 se introdujeron 180 proyectos de ley que r // Set the Model Configuration as YAML formatted string. // For available configuration options, please check: https://marian-nmt.github.io/docs/cmd/marian-decoder/ const modelConfig = `models: - - /${languagePair}/model.${languagePair}.npz + - /${languagePair}/model.${languagePair}.intgemm.alphas.bin vocabs: - /${vocabLanguagePair}/vocab.${vocabLanguagePair}.spm - /${vocabLanguagePair}/vocab.${vocabLanguagePair}.spm diff --git a/wasm/test_page/start_server.sh b/wasm/test_page/start_server.sh index b83344b..b6fc2a6 100644 --- a/wasm/test_page/start_server.sh +++ b/wasm/test_page/start_server.sh @@ -1,8 +1,10 @@ #!/bin/bash - +echo "Start: Copying artifacts in local folder------" cp ../../build-wasm/wasm/bergamot-translator-worker.data . cp ../../build-wasm/wasm/bergamot-translator-worker.js . cp ../../build-wasm/wasm/bergamot-translator-worker.wasm . cp ../../build-wasm/wasm/bergamot-translator-worker.worker.js . + npm install +echo "Start httpserver" node bergamot-httpserver.js \ No newline at end of file