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

From Leeroopedia


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

Overview

The DepthwiseConv2dNativeBackpropInput kernel computes the gradient of a depthwise 2D convolution with respect to the input tensor on the CPU backend. Unlike standard convolution backpropagation, this kernel accounts for the channel multiplier (chMul = outChannels / inChannels) inherent to depthwise convolutions, where each input channel is convolved independently with its own set of filters.

Code Reference

Source Location

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

Signature

export function depthwiseConv2dNativeBackpropInput(args: {
  inputs: DepthwiseConv2dNativeBackpropInputInputs,
  backend: MathBackendCPU,
  attrs: DepthwiseConv2dNativeBackpropInputAttrs
}): TensorInfo

Imports

import {backend_util, DepthwiseConv2dNativeBackpropInput, DepthwiseConv2dNativeBackpropInputAttrs, DepthwiseConv2dNativeBackpropInputInputs, KernelConfig, KernelFunc, TensorBuffer, TensorInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';

Kernel Registration

export const depthwiseConv2dNativeBackpropInputConfig: KernelConfig = {
  kernelName: DepthwiseConv2dNativeBackpropInput,
  backendName: 'cpu',
  kernelFunc: depthwiseConv2dNativeBackpropInput as unknown as KernelFunc
};

I/O Contract

Parameter Type Description
inputs.dy TensorInfo Upstream gradient tensor from the forward depthwise conv2d output
inputs.filter TensorInfo The depthwise filter of shape [filterHeight, filterWidth, inChannels, channelMultiplier]
attrs.inputShape [number, number, number, number] Shape of the original input tensor
attrs.strides [number, number] Stride of the convolution
attrs.pad 'same' | number Padding mode
attrs.dilations [number, number] Dilation rate
attrs.dimRoundingMode 'round' | 'ceil' Rounding mode for output dimensions
Return TensorInfo The gradient tensor dx with shape matching inputShape

Implementation Details

  • Asserts that inputs are not complex tensors.
  • Calls backend_util.computeConv2DInfo with the depthwise flag set to true.
  • Computes the channel multiplier: chMul = outChannels / inChannels.
  • Uses six nested loops (batch, inChannels, inHeight, inWidth, outHeight, outWidth) and an inner loop over the channel multiplier dm.
  • For each input position, computes the dot product across all matching output gradient positions and corresponding filter weights. The output channel index is computed as d2 = d1 * chMul + dm.
  • Filter indices are reversed (filterHeight - 1 - wR, filterWidth - 1 - wC) to implement the transposed convolution.
  • Padding offsets are calculated as topPad = filterHeight - 1 - padInfo.top and leftPad = filterWidth - 1 - padInfo.left.

Usage Examples

import {depthwiseConv2dNativeBackpropInput} from './DepthwiseConv2dNativeBackpropInput';

const dx = depthwiseConv2dNativeBackpropInput({
  inputs: {dy: dyTensor, filter: filterTensor},
  backend: cpuBackend,
  attrs: {
    inputShape: [1, 32, 32, 3],
    strides: [1, 1],
    pad: 'same',
    dilations: [1, 1],
    dimRoundingMode: 'floor'
  }
});

Related Pages

Page Connections

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