Implementation:Tensorflow Tfjs MathBackendCPU
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, CPU_Backend |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
MathBackendCPU is the pure-JavaScript CPU backend for TensorFlow.js. It extends KernelBackend and provides a reference implementation that runs tensor operations entirely on the CPU using typed arrays. This backend is the fallback when no accelerated backend (WebGL, WebGPU, WASM, or Node) is available. It manages tensor data storage, reference counting, memory disposal, and supports complex number tensors.
Code Reference
Source Location
tfjs-backend-cpu/src/backend_cpu.ts (GitHub)
Key Imports
import {backend_util, BackendTimingInfo, buffer, DataStorage, DataType, engine, env,
kernel_impls, KernelBackend, Rank, ShapeMap, Tensor, Tensor2D,
TensorBuffer, TensorInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {assertNotComplex} from './cpu_util';
Class Signature
export class MathBackendCPU extends KernelBackend {
public blockSize = 48;
data: DataStorage<TensorData<DataType>>;
constructor();
}
Exported Interface: TensorData
export interface TensorData<D extends DataType> {
values?: backend_util.BackendValues;
dtype: D;
complexTensorInfos?: {real: TensorInfo, imag: TensorInfo};
refCount: number;
}
Key Methods
write
Writes values into a new data bucket and returns a DataId. On first use in a Node.js environment, emits a warning recommending the native Node backend.
override write(values: backend_util.BackendValues, shape: number[], dtype: DataType): DataId
makeTensorInfo
Creates a TensorInfo object, handling string encoding when the dtype is 'string'.
makeTensorInfo(shape: number[], dtype: DataType,
values?: backend_util.BackendValues|string[]): TensorInfo
read / readSync
Reads tensor data asynchronously or synchronously. For complex64 tensors, merges the real and imaginary float arrays.
override async read(dataId: DataId): Promise<backend_util.BackendValues>
override readSync(dataId: DataId): backend_util.BackendValues
disposeData
Decrements reference count and frees memory when it reaches zero. Recursively disposes complex tensor components.
override disposeData(dataId: DataId, force = false): boolean
memory
Returns memory info, noting that reported values are unreliable upper bounds due to JavaScript garbage collection.
floatPrecision
Returns 32, indicating 32-bit float precision for this backend.
I/O Contract
| Method | Input | Output |
|---|---|---|
write |
BackendValues, shape array, dtype |
DataId handle
|
makeTensorInfo |
shape, dtype, optional values | TensorInfo object
|
read / readSync |
DataId |
BackendValues (typed array or Uint8Array[] for strings)
|
disposeData |
DataId, optional force flag |
boolean (true if memory released)
|
where |
condition Tensor |
Tensor2D of indices
|
floatPrecision |
(none) | 32
|
Usage Example
import {MathBackendCPU} from '@tensorflow/tfjs-backend-cpu';
import * as tf from '@tensorflow/tfjs-core';
// Register the CPU backend
tf.registerBackend('cpu', () => new MathBackendCPU());
await tf.setBackend('cpu');
const t = tf.tensor([1, 2, 3, 4]);
console.log(t.dataSync()); // Float32Array [1, 2, 3, 4]
Related Pages
- Tensorflow_Tfjs_Layer_Utils - Utilities for printing model summaries
- Tensorflow_Tfjs_Training_Utils - Training weight utilities used during model fitting