/web-to-ai-ml-integrations

Run ML Model in WebAssembly

Step-by-step guide to run ML models in WebAssembly. Achieve seamless AI integration in your web apps.

Book a free  consultation
4.9
Clutch rating 🌟
600+
Happy partners
17+
Countries served
190+
Team members
Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Run ML Model in WebAssembly

 

Converting the ML Model to WebAssembly with Emscripten

 
  • Description: Convert your machine learning model’s inference code (written in C/C++ or using libraries that support C/C++) into a WebAssembly module using Emscripten. Emscripten is a toolchain that compiles C/C++ code to WebAssembly (WASM) so that it can run efficiently in web browsers.
  • Key Concept: WebAssembly is a low-level binary format that allows code, originally written in languages like C or C++, to run on the web almost as fast as native code.

// Example: A simple C++ function for inference (e.g., a linear regression)
// Compile this with Emscripten to generate a .wasm file.

#include 

// Suppose this function computes a simple weighted sum
extern "C" {

// Use EMSCRIPTEN\_KEEPALIVE to ensure the function is not optimized-out
EMSCRIPTEN\_KEEPALIVE
float predict(float input, float weight, float bias) {
    return (input \* weight) + bias; // Linear regression: y = wx + b
}

}

 

Exporting Model Inference Functions

 
  • Description: The functions that perform inference must be exported so that JavaScript can call them once the WebAssembly module is instantiated. By using the EMSCRIPTEN\_KEEPALIVE macro, we prevent these functions from being removed during the compilation optimization process.
  • Clarification: In our example, the function predict is our model inference function. If your ML model is more complex (for example, a neural network), you may have several functions to load weights, process inputs, and produce outputs.

 

Loading and Running the WASM Module in the Browser

 
  • Description: After compiling your C/C++ code to a .wasm file, the next step is to load and instantiate it in your JavaScript environment. The browser’s WebAssembly.instantiateStreaming API allows you to instantiate the module directly from the compiled binary.
  • Key Concept: Instantiation creates an instance of your WASM module, giving you access to its exported functions through a JavaScript object.

// JavaScript code to fetch and instantiate the WebAssembly module

// Fetch and instantiate the WASM file
fetch('model.wasm')
  .then(response => response.arrayBuffer())
  .then(buffer => WebAssembly.instantiate(buffer, {}))
  .then(wasmModule => {
    // The exported functions are available under wasmModule.instance.exports
    const { predict } = wasmModule.instance.exports;

    // Prepare inputs for the prediction function
    const input = 2.0;
    const weight = 3.5;
    const bias = 1.0;

    // Call the ML inference function from WebAssembly
    const result = predict(input, weight, bias);
    console.log('Prediction result:', result);
  })
  .catch(error => {
    console.error('Error loading or running WASM module:', error);
  });

 

Managing Memory and Data Transfer Between JavaScript and WASM

 
  • Description: WebAssembly modules have their own linear memory, and transferring data between JavaScript and WASM often requires careful management to ensure compatibility and performance.
  • Key Concept: Use typed arrays like Uint8Array or Float32Array for data exchange. When your inference function requires larger input data (e.g., image pixels), copy the data into the WASM module’s memory buffer before processing.

// Assume we have a function that expects an array of floats representing image data

// Allocate memory in the WASM module's memory
const numElements = 1024;
const bufferSize = numElements \* Float32Array.BYTES_PER_ELEMENT;
const ptr = wasmModule.instance.exports.malloc(bufferSize); // Custom exported malloc or equivalent

// Create a typed array view to the WASM memory
const wasmMemory = new Float32Array(wasmModule.instance.exports.memory.buffer, ptr, numElements);

// Copy data from JavaScript array to the WASM memory
const inputData = new Float32Array(numElements); // This would be your image pixel values or preprocessed data
// Populate inputData as required...

wasmMemory.set(inputData);

// Call the inference function that takes a pointer to the data
const inferenceResult = wasmModule.instance.exports.run\_inference(ptr, numElements);

// Free the allocated memory if necessary
wasmModule.instance.exports.free(ptr);

 

Optimization Considerations for Running ML Models in WASM

 
  • Description: WebAssembly executes at near-native speeds but optimizing performance is still crucial for resource-intensive ML tasks.
  • Performance Tips:
    • Model Quantization: Reduce model precision to decrease memory footprint and improve inference speed.
    • Parallelism: Explore WebAssembly threads if your target browser environment supports them.
    • Memory Management: Avoid frequent memory allocation and deallocation by reusing buffers.
    • Preprocessing: Perform heavy preprocessing in WebAssembly if possible, to benefit from faster processing speeds.
  • Clarification: Use optimization flags provided by Emscripten (e.g., -O3 for high-level optimizations) when compiling your code.

 

Conclusion: Seamless Integration of ML Models in WebAssembly

 
  • Summary: With Emscripten, you can convert your ML model’s inference code into WebAssembly, exposing the necessary functions to JavaScript. This integration allows you to perform fast, efficient machine learning inference directly in the browser, benefiting from near-native computational speeds while keeping the client-side application lightweight.
  • Key Takeaway: Ensuring proper data exchange, memory management, and function exporting are essential to build a robust integration between your ML model and the web environment.


Recognized by the best

Trusted by 600+ businesses globally

From startups to enterprises and everything in between, see for yourself our incredible impact.

RapidDev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with.

They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

Arkady
CPO, Praction
Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost.

He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Donald Muir
Co-Founder, Arc
RapidDev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space.

They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Mat Westergreen-Thorne
Co-CEO, Grantify
RapidDev is an excellent developer for custom-code solutions.

We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Emmanuel Brown
Co-Founder, Church Real Estate Marketplace
Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 

This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Samantha Fekete
Production Manager, Media Production Company
The pSEO strategy executed by RapidDev is clearly driving meaningful results.

Working with RapidDev has delivered measurable, year-over-year growth. Comparing the same period, clicks increased by 129%, impressions grew by 196%, and average position improved by 14.6%. Most importantly, qualified contact form submissions rose 350%, excluding spam.

Appreciation as well to Matt Graham for championing the collaboration!

Michael W. Hammond
Principal Owner, OCD Tech

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â