Implementation:Tensorflow Tfjs CPU SparseFillEmptyRows Impl
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, CPU_Backend |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The sparseFillEmptyRowsImpl function fills empty rows in a sparse tensor with a default value. Given a 2D sparse tensor represented by indices and values, it ensures every row (in the first dimension up to denseShape[0]) has at least one entry. Empty rows are filled with a single entry at column 0 containing the provided default value. It also returns metadata needed for backpropagation.
Code Reference
Source Location
tfjs-backend-cpu/src/kernels/SparseFillEmptyRows_impl.ts (GitHub)
Signature
export function sparseFillEmptyRowsImpl(
indices: TypedArray, indicesShape: number[], indicesDType: DataType,
values: TypedArray, valuesDType: DataType, denseShape: TypedArray,
defaultValue: number):
[TypedArray, number[], TypedArray, boolean[], number[]]
Imports
import {backend_util, DataType, TypedArray, util} from '@tensorflow/tfjs-core';
I/O Contract
Inputs
| Name | Type | Description |
|---|---|---|
indices |
TypedArray |
Flat 2D indices of shape [N, rank] for the sparse tensor
|
indicesShape |
number[] |
Shape of the indices tensor ([N, rank])
|
indicesDType |
DataType |
Data type of the indices |
values |
TypedArray |
Sparse tensor values of length N
|
valuesDType |
DataType |
Data type of the values |
denseShape |
TypedArray |
Dense shape of the sparse tensor |
defaultValue |
number |
Value used to fill empty rows |
Output
Returns a tuple of five elements:
outputIndices(TypedArray): New indices with filled rows included.outputIndicesShape(number[]): Shape of the output indices ([M, rank]).outputValues(TypedArray): New values with default values for filled rows.emptyRowIndicator(boolean[]): Boolean array of lengthdenseRowsindicating which rows were empty.reverseIndexMap(number[]): Maps each original index to its position in the output, needed for gradient computation.
Algorithm
- Edge case: If
denseRows === 0, returns empty arrays (with validation thatindicesCountis also 0). - CSR offset computation: Counts elements per row. Validates row indices are non-negative and within bounds.
- Fast path: If all rows are already filled and rows are ordered, returns the original indices/values unchanged with an identity reverse index map.
- Slow path:
- Computes cumulative CSR offsets to determine output positions.
- Allocates output arrays sized to
fullIndicesCount(sum of max(1, count) per row). - Copies existing entries to their correct positions in the output.
- For each empty row, inserts a single entry with the row index and default value.
Usage Example
import {sparseFillEmptyRowsImpl} from './SparseFillEmptyRows_impl';
const indices = new Int32Array([0, 1, 2, 3]); // shape [2, 2], rows 0 and 2
const values = new Float32Array([10, 20]);
const denseShape = new Int32Array([4, 5]);
const [outIdx, outIdxShape, outVals, emptyRows, reverseMap] =
sparseFillEmptyRowsImpl(
indices, [2, 2], 'int32',
values, 'float32',
denseShape, 0);
// emptyRows: [false, true, false, true]
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment