Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Tfjs CPU SparseFillEmptyRows Impl

From Leeroopedia


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 length denseRows indicating which rows were empty.
  • reverseIndexMap (number[]): Maps each original index to its position in the output, needed for gradient computation.

Algorithm

  1. Edge case: If denseRows === 0, returns empty arrays (with validation that indicesCount is also 0).
  2. CSR offset computation: Counts elements per row. Validates row indices are non-negative and within bounds.
  3. Fast path: If all rows are already filled and rows are ordered, returns the original indices/values unchanged with an identity reverse index map.
  4. Slow path:
    1. Computes cumulative CSR offsets to determine output positions.
    2. Allocates output arrays sized to fullIndicesCount (sum of max(1, count) per row).
    3. Copies existing entries to their correct positions in the output.
    4. 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