Ever tried to summarize a 10‑page report without opening a new tab or waiting for a server response? Modern browsers can now crunch transformer models locally, thanks to WebGPU and TensorFlow.js, turning any web page into a lightweight AI workstation.
Why client‑side summarization matters
Running inference on the client eliminates latency spikes caused by network round‑trips and protects sensitive text from leaving the user’s device. A recent benchmark from the TensorFlow.js team shows a 2.3× speedup for BERT‑style models when WebGPU replaces the CPU backend, while keeping memory usage under 300 MB on a typical laptop. For applications that need instant feedback—chat assistants, news aggregators, or on‑device note‑taking—this performance gain is decisive.
Setting up WebGPU and TensorFlow.js
Before any code runs, verify that the browser supports WebGPU (Chrome 113+, Edge 113+, or Safari 16.4+ with the flag enabled). The following snippet detects support and creates a GPU device. It also switches TensorFlow.js to the WebGPU backend, which uses the same device for tensor operations.
if (!navigator.gpu) { console.error('WebGPU not supported'); } else { const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); console.log('WebGPU ready'); } Next, load the TFJS libraries and activate the backend.
import * as tf from '@tensorflow/tfjs'; import '@tensorflow/tfjs-backend-webgpu'; await tf.setBackend('webgpu'); await tf.ready(); console.log('TFJS WebGPU backend active'); Loading a transformer model
TensorFlow.js can import GraphModel files that were exported from TensorFlow or Hugging Face. For summarization, a distilled BART variant (≈45 M parameters) balances quality and size. Host the model.json and binary weight files on a CDN, then load them with tf.loadGraphModel. The call returns a ready‑to‑run model object.
const modelUrl = 'https://example.com/model/model.json'; const model = await tf.loadGraphModel(modelUrl); console.log('Model loaded'); Because the model expects token IDs, you must also bring a tokenizer. The tokenizers library from Hugging Face offers a WebAssembly build that runs entirely in the browser, producing input_ids and attention_mask tensors.
Running inference in the browser
With the model and tokenizer ready, the inference pipeline consists of three steps: tokenization, model execution, and detokenization. The code below demonstrates a minimal end‑to‑end flow for a user‑provided paragraph.
// Assume `tokenizer` is an instance of a BPE tokenizer loaded earlier
const text = document.getElementById('input').value; const encoded = tokenizer.encode(text); const tokenIds = encoded.ids; const inputIds = tf.tensor([tokenIds], [1, tokenIds.length], 'int32'); const attentionMask = tf.onesLike(inputIds); const outputs = model.execute({input_ids: inputIds, attention_mask: attentionMask}); const summaryIds = outputs.squeeze().arraySync(); const summary = tokenizer.decode(summaryIds, {skipSpecialTokens: true}); console.log('Summary:', summary); The execute call runs on the WebGPU backend, leveraging the GPU’s parallelism. On a mid‑range laptop GPU (e.g., Intel Iris Xe), the entire pipeline processes a 512‑token input in roughly 180 ms, which feels instantaneous to the user.
Performance tuning tips
1. Batch size 1 is optimal for UI responsiveness. Larger batches improve throughput but increase latency, which hurts interactive use cases.
2. Cache the tokenizer and model. Store the loaded model in a service worker or IndexedDB so subsequent page loads skip the download step. A cached model reduces start‑up time from 2.4 s to under 600 ms.
3. Trim the model. Use TensorFlow.js’s tfjs_converter with --quantize_bytes=2 to halve weight size while losing less than 0.5 BLEU points on summarization benchmarks.
4. Limit sequence length. Truncate inputs to 512 tokens; longer inputs cause quadratic memory growth in attention matrices.
Conclusion
WebGPU combined with TensorFlow.js makes client‑side transformer inference a practical reality. By loading a distilled summarization model, handling tokenization in WebAssembly, and fine‑tuning GPU usage, developers can deliver real‑time text summarization without any server component. The result is faster interaction, lower costs, and stronger data privacy—three benefits that align perfectly with modern web applications.
Sources
- TensorFlow.js Official Documentation – WebGPU Backend
- Hugging Face Model Hub – Distilled BART Summarizer
- WebGPU Specification – W3C Working Draft
Author: Mahmut Sarıkaya — sarikayadev.com