Tensara Logo

tensara

All Problems

2D Max Pooling

MEDIUM

Perform 2D max pooling on an input tensor:

output[i,j]=maxm=0,n=0k1,k1input[Si+DmP,Sj+DnP]\text{output}[i,j] = \max_{m=0,n=0}^{k-1,k-1} \text{input}[S \cdot i + D \cdot m - P, S \cdot j + D \cdot n - P]

The max pooling operation slides a window of size k×kk \times k over the input tensor with stride SS, dilation DD, and padding PP, computing the maximum value within each window position.

Input:

  • Matrix input of size H×W\text{H} \times \text{W} (input tensor)
  • kernel_size (kk): Size of the pooling window
  • stride (SS): Step size between window positions
  • padding (PP): Number of zero-padding elements added on all sides
  • dilation (DD): Spacing between kernel elements

Output:

  • Matrix output of size Hout×Wout\text{H}_{\text{out}} \times \text{W}_{\text{out}} where: Hout=H+2PD(k1)1S+1\text{H}_{\text{out}} = \left\lfloor\frac{\text{H} + 2P - D(k-1) - 1}{S}\right\rfloor + 1 Wout=W+2PD(k1)1S+1\text{W}_{\text{out}} = \left\lfloor\frac{\text{W} + 2P - D(k-1) - 1}{S}\right\rfloor + 1

Notes:

  • All matrices are stored in row-major order
  • Zero padding is applied when specified by the padding parameter
  • For values outside the input boundaries (after padding), use negative infinity
  • Dilation controls the spacing between kernel elements, creating an effective kernel size of D(k1)+1D(k-1) + 1
  • This problem is adapted from KernelBench
Console

Sample Run Results

Hit "Run" to test your code with sample inputs

Loading...

Loading editor...

CUDA C++ environment

Desktop Required for Code Submission

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