Perform 2D max pooling on an input tensor:
output[i,j]=m=0,n=0maxk−1,k−1input[S⋅i+D⋅m−P,S⋅j+D⋅n−P]
The max pooling operation slides a window of size k×k over the input tensor with stride S, dilation D, and padding P, computing the maximum value within each window position.
Input:
- Matrix
input of size H×W (input tensor)
kernel_size (k): Size of the pooling window
stride (S): Step size between window positions
padding (P): Number of zero-padding elements added on all sides
dilation (D): Spacing between kernel elements
Output:
- Matrix
output of size Hout×Wout where:
Hout=⌊SH+2P−D(k−1)−1⌋+1
Wout=⌊SW+2P−D(k−1)−1⌋+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(k−1)+1
- This problem is adapted from KernelBench