Environment:Tensorflow Tfjs Browser Runtime
| Metadata | |
|---|---|
| Source | TensorFlow.js |
| Domains | Infrastructure, Browser_ML |
| Date | 2026-02-10 00:00 GMT |
Overview
Browser runtime environment for TensorFlow.js with WebGL, WebGPU, WASM, and CPU backend support.
Description
Browser-based execution of TensorFlow.js models. Supports four backends:
- WebGL (priority 2, auto-registered when
isBrowser()is true) — GPU-accelerated backend using WebGL 2.0 or 1.0 shaders. This is the default backend in most browsers. - WebGPU (priority 3, requires Chrome M113+ with
navigator.gpu) — Next-generation GPU backend offering improved performance over WebGL. Highest priority when available. - WASM (requires WebAssembly support, optional SIMD and multi-threading) — CPU-based backend compiled from C++ to WebAssembly, significantly faster than pure JS for CPU workloads.
- CPU (pure JavaScript fallback) — Reference backend with no hardware requirements. Slowest, but guaranteed to work in any JS environment.
TensorFlow.js automatically selects the highest-priority available backend at startup. The backend priority determines selection order: WebGPU (3) > WebGL (2) > WASM/CPU (1).
Usage
Use this environment for any browser-based ML inference or training workflow. Required by all implementations that use browser APIs like tf.browser.fromPixels(), model.predict(), or tf.loadGraphModel() with HTTP URLs.
System Requirements
| Requirement | Details |
|---|---|
| Browser | Modern web browser: Chrome 79+, Firefox, Safari, Edge |
| GPU | Required for WebGL and WebGPU backends. Integrated or discrete GPU with up-to-date drivers. |
| WebGPU | Chrome M113+ (default since May 2023) with navigator.gpu available
|
| WASM | Browser must support WebAssembly. SIMD and multi-threading detection is automatic. |
| CPU Backend | No special hardware requirements (pure JavaScript) |
Dependencies
Union Package (Recommended)
npm install @tensorflow/tfjs
The @tensorflow/tfjs union package bundles all browser-relevant packages together.
Individual Packages
| Package | Purpose |
|---|---|
@tensorflow/tfjs-core |
Core tensor operations, engine, and backend interface |
@tensorflow/tfjs-layers |
Keras-compatible layers API for model building |
@tensorflow/tfjs-converter |
Loading and executing converted TensorFlow models |
@tensorflow/tfjs-backend-webgl |
WebGL backend (GPU acceleration) |
@tensorflow/tfjs-backend-webgpu |
WebGPU backend (next-gen GPU acceleration) |
@tensorflow/tfjs-backend-wasm |
WebAssembly backend (optimized CPU) |
@tensorflow/tfjs-backend-cpu |
Pure JavaScript CPU backend (fallback) |
At least one backend package must be installed alongside the core packages.
TypeScript
- TypeScript 5.0.4
Credentials
None required for browser runtime.
Quick Install
Via npm
npm install @tensorflow/tfjs
Via CDN Script Tag
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
Code Evidence
WebGL Backend Auto-Registration (Browser Only)
From tfjs-backend-webgl/src/base.ts:24-26:
if (device_util.isBrowser()) {
registerBackend('webgl', () => new MathBackendWebGL(), 2 /* priority */);
}
The WebGL backend only registers itself when running in a browser context. The priority of 2 means it is selected by default unless a higher-priority backend (WebGPU at priority 3) is available.
From tfjs-backend-webgpu/src/webgpu_util.ts:157-159:
function isWebGPUSupported(): boolean {
return (typeof globalThis !== 'undefined') &&
(globalThis.navigator.gpu != null);
}
WebGPU support is detected by checking for the presence of navigator.gpu on the global object.
isBrowser Detection
From tfjs-core/src/device_util.ts:60-64:
function isBrowser(): boolean {
return (typeof window !== 'undefined' && window.document != null) ||
(typeof WorkerGlobalScope !== 'undefined');
}
The isBrowser() utility returns true when running in a window context or inside a Web Worker.
Common Errors
| Error | Cause | Solution |
|---|---|---|
| WebGPU not supported | Browser does not expose navigator.gpu |
Upgrade to Chrome M113+ or use a WebGPU-enabled browser. Falls back to WebGL automatically. |
| WebGL context lost | GPU memory exhaustion or driver instability | Reduce model size, decrease texture dimensions, or use the WASM backend as an alternative. |
| EmscriptenModule not found (Angular) | Angular build does not include Emscripten types | Add @types/emscripten to the types array in tsconfig.json.
|
Compatibility Notes
- WebGPU became the default GPU backend in Chrome since M113 (May 2023). It is not yet supported in iOS Safari.
- WASM SIMD and threading feature detection is automatic — the backend selects the optimal binary at runtime.
- WebGL backend checks
isBrowser()before auto-registering, so it will not register in Node.js environments. - iOS Safari supports WebGL only (no WebGPU yet). Performance may be lower than desktop browsers.
- Web Workers are supported —
isBrowser()returnstrueinsideWorkerGlobalScope.
Related Pages
Required By (Implementations)
- Implementation:Tensorflow_Tfjs_Tf_Tensor_Creation
- Implementation:Tensorflow_Tfjs_Tf_Model_Functional
- Implementation:Tensorflow_Tfjs_Tf_Sequential
- Implementation:Tensorflow_Tfjs_LayersModel_Compile
- Implementation:Tensorflow_Tfjs_LayersModel_Fit
- Implementation:Tensorflow_Tfjs_LayersModel_Evaluate
- Implementation:Tensorflow_Tfjs_LayersModel_Predict
- Implementation:Tensorflow_Tfjs_LayersModel_Save
- Implementation:Tensorflow_Tfjs_LayersModel_Compile_And_Fit_For_Transfer
- Implementation:Tensorflow_Tfjs_LayersModel_Evaluate_And_Save_For_Transfer
- Implementation:Tensorflow_Tfjs_Tf_LoadLayersModel
- Implementation:Tensorflow_Tfjs_Tf_LoadLayersModel_For_Transfer
- Implementation:Tensorflow_Tfjs_Tf_LoadGraphModel
- Implementation:Tensorflow_Tfjs_GraphModel_Predict
- Implementation:Tensorflow_Tfjs_Container_GetLayer
- Implementation:Tensorflow_Tfjs_Layer_Trainable_Setter
- Implementation:Tensorflow_Tfjs_GPT2Tokenizer_Constructor
- Implementation:Tensorflow_Tfjs_GPT2Preprocessor_Constructor
- Implementation:Tensorflow_Tfjs_GPT2Backbone_Constructor
- Implementation:Tensorflow_Tfjs_GPT2CausalLM_Constructor
- Implementation:Tensorflow_Tfjs_GPT2CausalLM_Generate