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 RaggedTensorToTensor Impl

From Leeroopedia


Knowledge Sources
Domains Deep_Learning, CPU_Backend
Last Updated 2026-02-10 06:00 GMT

Overview

The raggedTensorToTensorImpl function converts a ragged tensor into a dense tensor. It takes ragged tensor components (values, row partition information, shape, and a default value for padding) and produces a regular dense tensor. The implementation is based on the TensorFlow C++ kernel.

The core logic is encapsulated in the internal RaggedTensorToTensorOp class, which handles multiple row partition types (VALUE_ROWIDS, ROW_SPLITS, and FIRST_DIM_SIZE).

Code Reference

Source Location

tfjs-backend-cpu/src/kernels/RaggedTensorToTensor_impl.ts (GitHub)

Signature

export function raggedTensorToTensorImpl(
    shape: TypedArray, shapesShape: number[], values: TypedArray,
    valuesShape: number[], valuesDType: DataType, defaultValue: TypedArray,
    defaultValueShape: number[], rowPartitionValues: TypedArray[],
    rowPartitionValuesShapes: number[][],
    rowPartitionTypes: string[]): [number[], TypedArray]

Imports

import {backend_util, broadcastTo, DataType, reshape, tidy, TypedArray, util} from '@tensorflow/tfjs-core';
import RowPartitionType = backend_util.RowPartitionType;

I/O Contract

Inputs

Name Type Description
shape TypedArray Desired output shape tensor
shapesShape number[] Shape of the shape tensor
values TypedArray Flat values of the ragged tensor
valuesShape number[] Shape of the values tensor
valuesDType DataType Data type of values
defaultValue TypedArray Default value used for padding empty positions
defaultValueShape number[] Shape of the default value tensor
rowPartitionValues TypedArray[] Array of row partition tensors
rowPartitionValuesShapes number[][] Shapes of each row partition tensor
rowPartitionTypes string[] Types of each row partition (e.g., "ROW_SPLITS", "VALUE_ROWIDS", "FIRST_DIM_SIZE")

Output

Returns a tuple [outputShape, outputTensor]:

  • outputShape (number[]): The shape of the dense output tensor.
  • outputTensor (TypedArray): The dense tensor data.

Internal Class: RaggedTensorToTensorOp

The RaggedTensorToTensorOp class performs the conversion through these key methods:

  • compute(): Main entry point. Calculates output size, builds multipliers, computes output indices for each ragged dimension, then calls setOutput.
  • getFirstDimensionSize(): Determines the size of the first dimension from partition metadata.
  • calculateOutputSize(firstDim): Computes the full output shape, resolving any unknown (-1) dimensions using getMaxWidth.
  • calculateFirstParentOutputIndex(): Builds the initial output index array for the first dimension.
  • calculateOutputIndex(): Dispatches to calculateOutputIndexValueRowID or calculateOutputIndexRowSplit depending on partition type.
  • setOutput(): Copies values into the dense output tensor, filling gaps with the default value. Handles broadcasting of the default value when needed.

Static Helper Methods

  • getMaxWidthRowSplit(rowSplit): Finds the maximum row width from row split boundaries.
  • getMaxWidthValueRowID(valueRowIds): Finds the maximum row width from value row ID sequences.

Module-Level Helpers

  • copyArray(dst, src, size): Element-wise copy between typed arrays.
  • makeShape(shape, isPartial): Validates and converts shape values, allowing -1 for partial shapes.

Usage Example

import {raggedTensorToTensorImpl} from './RaggedTensorToTensor_impl';

const shape = new Int32Array([2, 3]);
const values = new Float32Array([1, 2, 3, 4, 5]);
const rowSplits = new Int32Array([0, 3, 5]);

const [outputShape, outputTensor] = raggedTensorToTensorImpl(
    shape, [2], values, [5], 'float32',
    new Float32Array([0]), [],
    [rowSplits], [[3]],
    ['ROW_SPLITS']);
// outputShape: [2, 3]
// outputTensor: [1, 2, 3, 4, 5, 0]

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment