WASM run in node.js: how to use npm modules?

132 views
Skip to first unread message

Jim Lloyd

unread,
Mar 5, 2021, 1:45:26 AM3/5/21
to emscripten-discuss
I have a body of C++ code that I have successfully compiled with em++. Many of the unit tests run. I am unclear now how to achieve my end goal, which is to use this code in a node/express server that imports @tensorflow/tfjs-node and utilizes that module to perform Tensorflow predictions.

I believe the general outline  will require that I inject JavaScript code to load the TensorFlow module, probably using `--pre-js <file>`. If `--pre-js` is correct, what should the contents of `<file>` be? One line should be this:

    const tf = require('@tensorflow/tfjs-node');

My C++ code will at some point do something like this:

    EM_JS(val, loadModel, (string modelUrl), {
        return await tf.loadGraphModel(modelUrl);
    });
    val model = loadModel("http://...")

Later in my code I will pass the model and a tensor into another EM_JS function to do the prediction, but I expect that once I understand the above I will be able figure out the rest.

How do I arrange for the tf module reference to be available within the EM_JS definition of the loadModel function?

Of course, if there is already some demo somewhere that does something similar, a link to it may be all that I need. So far I have not found it after a couple hours of searching.

Thanks.



J Decker

unread,
Mar 5, 2021, 11:41:17 AM3/5/21
to emscripten-discuss
Basically this is  a JSON-like parser that results in various types of values to JS... (not just a specific neural network interface)

 I setup a JS->Native code object, that has the access methods to the native module, and some data tracking stuff.. 

This is 'objects' which is an array of values which are in JS, but are referenced from the C++ code via an index number into the array..


These are examples that create a new array, or object... further down more elemental objects are created and pushed into the value array...

(Set object by Index)  - assign another referenced object into another....

After playing with this for a while, I really gave up on this approach for my own purposes, the overhead of creating JS objects from WASM during the process of parsing JSOX(JSON) was much slower than just doing it in JS natively...

Also you don't really NEED a preload code.. you can just have the require in an init... which can setup the interface area between the native and JS code... 



--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/2a618a23-fd04-462e-bec7-dd37506b3d89n%40googlegroups.com.

Jim Lloyd

unread,
Mar 6, 2021, 3:10:46 PM3/6/21
to emscripte...@googlegroups.com, J Decker
Thanks for your answer J Decker. The first working solution that I came up with doesn't reuse any of your code, but your answer was helpful to get me past some small hurdles.
​I have some working code available at this gist.
Reply all
Reply to author
Forward
0 new messages