Home Knowledge Base Browser-Based ML: WebGPU and WebAssembly

Browser-Based ML: WebGPU and WebAssembly

Browser ML Technologies

TechnologyPurposePerformance
WebGPUGPU compute in browserFast
WebGLGraphics + limited computeMedium
WebAssemblyNear-native CPUFast
JavaScriptPure JS executionSlow

WebGPU for ML

// Check WebGPU support
if (!navigator.gpu) {
    console.log("WebGPU not supported");
    return;
}

// Get GPU adapter and device
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();

// Create compute pipeline for matrix multiply
const shaderModule = device.createShaderModule({
    code: `
        @compute @workgroup_size(8, 8)
        fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
            // Matrix multiplication kernel
        }
    `
});

Frameworks

Transformers.js

import { pipeline } from "@xenova/transformers";

// Load model (downloads to browser cache)
const classifier = await pipeline("sentiment-analysis");

// Run inference
const result = await classifier("I love this product!");
console.log(result);  // [{label: "POSITIVE", score: 0.99}]

ONNX Runtime Web

import * as ort from "onnxruntime-web";

// Load model
const session = await ort.InferenceSession.create("model.onnx");

// Prepare input
const tensor = new ort.Tensor("float32", inputData, [1, 3, 224, 224]);

// Run inference
const results = await session.run({ input: tensor });
console.log(results.output.data);

WebLLM

import { CreateMLCEngine } from "@mlc-ai/web-llm";

const engine = await CreateMLCEngine("Llama-2-7b-chat-hf-q4f16_1");

const response = await engine.chat.completions.create({
    messages: [{ role: "user", content: "Hello!" }],
    stream: true
});

Performance Comparison

BackendRelative Speed
Native CUDA100%
WebGPU20-40%
WebGL10-20%
WASM SIMD5-15%
Pure JS1-5%

Model Size Considerations

ModelSizeBrowser Suitable
DistilBERT250MBYes
BERT base440MBYes
Llama 7B Q43.5GBChallenging
GPT-2500MBYes

Best Practices

browserwebgpuwasm

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.