bergamot-translator/wasm
abhi-agg 12e9232066
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 <anatal@gmail.com>
Co-authored-by: Motin <motin@motin.eu>
2021-03-24 17:10:42 +01:00
..
bindings Added JS binding files and cmake infrastructure to build them 2021-02-11 23:36:29 +01:00
test_page Patch WASM artifacts to run optimized (wormhole enabled) inference (#68) 2021-03-24 17:10:42 +01:00
CMakeLists.txt Allow using relative paths for packaging files 2021-02-17 20:06:04 +01:00
patch-artifacts-enable-wormhole.sh Patch WASM artifacts to run optimized (wormhole enabled) inference (#68) 2021-03-24 17:10:42 +01:00
README.md Improved wasm/README 2021-02-18 12:48:45 +01:00

Using Bergamot Translator in JavaScript

The example file bergamot.html in the folder test_page demonstrates how to use the bergamot translator in JavaScript via a <script> tag.

Please note that everything below assumes that the bergamot project specific model files were packaged in wasm binary (using the compile instructions given in the top level README).

Using JS APIs

// The model configuration as YAML formatted string. For available configuration options, please check: https://marian-nmt.github.io/docs/cmd/marian-decoder/
// This example captures the most relevant options: model file, vocabulary files and shortlist file
const modelConfig = "{\"models\":[\"/esen/model.esen.npz\"],\"vocabs\":[\"/esen/vocab.esen.spm\",\"/esen/vocab.esen.spm\"],\"shortlist\":[\"/esen/lex.esen.s2t\"],\"beam-size\":1}";

// Instantiate the TranslationModel
const model = new Module.TranslationModel(modelConfig);

// Instantiate the arguments of translate() API i.e. TranslationRequest and input (vector<string>)
const request = new Module.TranslationRequest();
const input = new Module.VectorString;

// Initialize the input
input.push_back("Hola"); input.push_back("Mundo");

// translate the input; the result is a vector<TranslationResult>
const result = model.translate(input, request);

// Print original and translated text from each entry of vector<TranslationResult>
for (let i = 0; i < result.size(); i++) {
    console.log(' original=' + result.get(i).getOriginalText() + ', translation=' + result.get(i).getTranslatedText());
}

// Don't forget to clean up the instances
model.delete();
request.delete();
input.delete();

Demo (see everything in action)

  • Start the test webserver (ensure you have the latest nodejs installed)

    cd test_page
    bash start_server.sh
    
  • Open any of the browsers below

    • Firefox Nightly +87: make sure the following prefs are on (about:config)

      dom.postMessage.sharedArrayBuffer.bypassCOOP_COEP.insecure.enabled = true
      javascript.options.wasm_simd = true
      javascript.options.wasm_simd_wormhole = true
      
    • Chrome Canary +90: start with the following argument

      --js-flags="--experimental-wasm-simd"
      
  • Browse to the following page:

    http://localhost:8000/bergamot.html
    
  • Run some translations:

    • Choose a model and press Load Model
    • Type a sentence to be translated in the From textbox and press Translate
    • See the results in the To and Log textboxes