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 Graph Executor

From Leeroopedia


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

Overview

The executor.ts module implements the graph execution engine for TensorFlow.js Layers models. It evaluates SymbolicTensor nodes in a computation graph by recursively resolving their dependencies through topological sorting. The module provides the FeedDict class for mapping symbolic tensors to concrete values, an execute() function that drives inference, and LRU caching of topological sort results for repeated executions.

Key components:

  • FeedDict: Maps SymbolicTensor keys to concrete Tensor values (and optional masks).
  • execute(): The core inference function that topologically sorts the computation graph, iterates over nodes, and evaluates each layer's apply() method.
  • Topological sorting: Computed once per unique fetch/feed combination and cached via LruCache.
  • Memory management: Intermediate tensors are disposed during non-training execution when their recipient count reaches zero.

Code Reference

Source Location

tfjs-layers/src/engine/executor.ts (View on GitHub)

Key Signatures

export class FeedDict {
  constructor(feeds?: Feed[] | FeedDict);
  add(key: SymbolicTensor, value: Tensor, mask?: Tensor): FeedDict;
  addFeed(feed: Feed): void;
  hasKey(key: SymbolicTensor): boolean;
  names(): string[];
  getValue(key: SymbolicTensor | string): Tensor;
  getMask(key: SymbolicTensor | string): Tensor;
  disposeMasks(): void;
}

export function execute(
    fetches: SymbolicTensor | SymbolicTensor[],
    feedDict: FeedDict,
    kwargs?: Kwargs,
    probe?: ExecutionProbe
): Tensor | Tensor[] | [Tensor | Tensor[]];

export function updateCacheMaxEntries(maxEntries: number): void;

export interface ExecutionProbe {
  maxNumTensors?: number;
  minNumTensors?: number;
}

Import

import { execute, FeedDict, Feed } from '../engine/executor';

I/O Contract

FeedDict

  • Input: An optional array of Feed objects ({key: SymbolicTensor, value: Tensor}) or another FeedDict for copy-construction.
  • Output: A dictionary that maps SymbolicTensor IDs/names to concrete Tensor values and optional mask Tensors.
  • Constraints: Duplicate keys throw a ValueError. Feed values are cast to match the key's dtype when possible.

execute()

  • Input:
    • fetches: One or more SymbolicTensor nodes to evaluate.
    • feedDict: A FeedDict containing concrete values for input nodes.
    • kwargs (optional): May include training boolean and mask.
    • probe (optional): An ExecutionProbe object for memory profiling.
  • Output: A single Tensor if a single fetch was provided, or an array of Tensors for multiple fetches.
  • Behavior: Performs topological sort of the graph, iterates nodes, calls srcLayer.apply(inputValues, kwargs), and disposes intermediate tensors during inference.

Usage Examples

import { execute, FeedDict } from './engine/executor';

// Create a feed dictionary with input values
const feedDict = new FeedDict([
  { key: model.input as SymbolicTensor, value: inputTensor }
]);

// Execute the model graph
const output = execute(model.output as SymbolicTensor, feedDict);

// Execute with memory profiling
const probe: ExecutionProbe = {};
const result = execute(fetches, feedDict, {}, probe);
console.log('Max tensors during execution:', probe.maxNumTensors);

Related Pages

Page Connections

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