Tensara Logo

tensara

2D Convolution

MEDIUM

Perform 2D convolution between an input image and a kernel:

C[i,j]=k=0Kh1l=0Kw1A[i+k,j+l]B[k,l]\text{C}[i,j] = \sum_{k=0}^{K_h-1}\sum_{l=0}^{K_w-1} \text{A}[i+k,j+l] \cdot \text{B}[k,l]

The convolution operation slides the 2D kernel over the input image, computing the sum of element-wise multiplications at each position. Zero padding is used at the boundaries.

Input:

  • Matrix A\text{A} of size H×W\text{H} \times \text{W} (input image)
  • Matrix B\text{B} of size Kh×KwK_h \times K_w (convolution kernel)
  • Both KhK_h and KwK_w are odd and smaller than HH and WW respectively

Output:

  • Matrix C\text{C} of size H×W\text{H} \times \text{W} (convolved image)

Notes:

  • All matrices A\text{A}, B\text{B}, and C\text{C} are stored in row-major order
  • Use zero padding at the boundaries where the kernel extends beyond the input image
  • The kernel is centered at each position, with (Kh1)/2(K_h-1)/2 rows and (Kw1)/2(K_w-1)/2 columns on each side

GPU Type

Language

Data Type

Loading...

Loading editor...

CUDA C++ environment

Sample Run Results

Hit "Run" to test your code with sample inputs

Desktop Required for Code Submission

For the best coding experience, please switch to a desktop device to write and submit your solution.