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

From Leeroopedia


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

Overview

The Dilation2DBackpropFilter kernel computes the gradient of a 2D morphological dilation with respect to the filter (structuring element) on the CPU backend. It identifies the argmax position in the forward dilation window for each output element and routes the upstream gradient to the corresponding filter position. This follows the TensorFlow C++ reference implementation where, in case of ties, only the last branch (largest h * filterWidth + w) receives the gradient.

Code Reference

Source Location

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

Signature

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

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

Imports

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

I/O Contract

Parameter Type Description
inputs.x Tensor4D 4D input tensor of shape [batch, inHeight, inWidth, inChannels]
inputs.filter Tensor3D 3D structuring element of shape [filterHeight, filterWidth, depth]
inputs.dy Tensor4D Upstream gradient of shape [batch, outHeight, outWidth, inChannels]
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 The filter gradient tensor with the same shape as filter

Implementation Details

  • Converts input tensors to nested arrays via util.toNestedArray for element-wise indexed access.
  • Asserts that dy rank matches the expected output rank.
  • Initializes a zero-filled gradient array matching the filter shape via util.makeZerosNestedTypedArray.
  • For each output position, recomputes the forward dilation: finds the argmax position (hMax, wMax) where x[b][hIn][wIn][d] + filter[h][w][d] is maximized.
  • Accumulates the upstream gradient into gradients[hMax][wMax][d] (only the argmax branch receives the gradient).
  • Writes the result via cpuBackend.write after flattening with util.toTypedArray.

Usage Examples

// Dilation2DBackpropFilter is registered via dilation2DBackpropFilterConfig
// and invoked internally during backpropagation through tf.dilation2d()

Related Pages

Page Connections

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