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

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_CPU_Conv2DBackpropInput_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 Conv2DBackpropInput kernel computes the gradient of a 2D convolution with respect to the input tensor on the CPU backend. This is used during backpropagation to propagate gradients through convolutional layers. The implementation uses a direct nested-loop approach iterating over batches, input channels, and spatial dimensions, accumulating dot products between the upstream gradient (dy) and the transposed filter.

Code Reference

Source Location

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

Signature

export function conv2DBackpropInput(args: {
  inputs: Conv2DBackpropInputInputs,
  backend: MathBackendCPU,
  attrs: Conv2DBackpropInputAttrs
}): TensorInfo

Imports

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

Kernel Registration

export const conv2DBackpropInputConfig: KernelConfig = {
  kernelName: Conv2DBackpropInput,
  backendName: 'cpu',
  kernelFunc: conv2DBackpropInput as unknown as KernelFunc
};

I/O Contract

Parameter Type Description
inputs.dy TensorInfo The upstream gradient tensor (output gradient from the forward conv2d)
inputs.filter TensorInfo The convolution filter of shape [filterHeight, filterWidth, inChannels, outChannels]
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 or explicit pad value
attrs.dataFormat 'NCHW' Data layout format
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.
  • Computes convolution metadata (padding, strides, output dimensions) via backend_util.computeConv2DInfo.
  • Allocates a TensorBuffer of shape convInfo.inShape for the output gradient dx.
  • Supports both channelsLast (NHWC) and channelsFirst (NCHW) data formats by computing stride offsets accordingly.
  • Uses six nested loops (batch, inChannels, inHeight, inWidth, outHeight, outWidth) to compute each element of dx as a dot product over the relevant filter window and output gradient positions.
  • Filter indices are reversed (filterHeight - 1 - wR) to perform the transposed convolution.

Usage Examples

import {conv2DBackpropInput} from './Conv2DBackpropInput';

const dx = conv2DBackpropInput({
  inputs: {dy: dyTensor, filter: filterTensor},
  backend: cpuBackend,
  attrs: {
    inputShape: [1, 28, 28, 1],
    strides: [1, 1],
    pad: 'same',
    dataFormat: 'NHWC',
    dimRoundingMode: 'floor'
  }
});

Related Pages

Page Connections

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