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 Dilation2D Kernel

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_CPU_Dilation2D_Kernel.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

The Dilation2D kernel implements 2D morphological dilation on the CPU backend. For each spatial output position and channel, it computes the maximum of input + filter over the filter window. This operation is analogous to max-pooling but adds the structuring element (filter) values before taking the maximum. The implementation follows the TensorFlow C++ reference kernel.

Code Reference

Source Location

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

Signature

The kernel function is defined inline as an anonymous function within the KernelConfig object:

export const dilation2DConfig: KernelConfig = {
  kernelName: Dilation2D,
  backendName: 'cpu',
  kernelFunc: ({inputs, backend, attrs}) => { ... }
};

Imports

import {backend_util, Dilation2D, Dilation2DAttrs, Dilation2DInputs, KernelConfig, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';

I/O Contract

Parameter Type Description
inputs.x TensorInfo 4D input tensor of shape [batch, inHeight, inWidth, inChannels] (NHWC format)
inputs.filter TensorInfo 3D structuring element of shape [filterHeight, filterWidth, depth]
attrs.strides [number, number] Stride for each spatial dimension
attrs.pad 'same' Padding mode
attrs.dilations [number, number] Dilation rate for the filter
Return TensorInfo Output tensor of shape [batch, outHeight, outWidth, inChannels]

Implementation Details

  • Computes dilation metadata via backend_util.computeDilation2DInfo with NHWC data format.
  • Allocates an output array of the computed output size using util.getArrayFromDType.
  • Iterates over batches, output spatial positions, and channels.
  • For each output element, initializes curVal to Number.MIN_SAFE_INTEGER and scans the filter window.
  • At each filter position, computes the input coordinate factoring in dilation (hIn = hBeg + h * dilationHeight).
  • Skips out-of-bounds positions. For valid positions, computes val = xVals[xIndex] + filterVals[filterIndex] and keeps the maximum.
  • Uses util.locToIndex with precomputed strides for flat array indexing.
  • Writes the final output via cpuBackend.write.

Usage Examples

// Dilation2D is registered via dilation2DConfig and invoked through tf.dilation2d()
import * as tf from '@tensorflow/tfjs-core';

const result = tf.dilation2d(inputTensor, filterTensor, strides, pad, dilations);

Related Pages

Page Connections

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