42. 2D Max Pooling medium
Implement a 2D max pooling operation for image/feature map downsampling. The program should take an input tensor and produce an output tensor by applying max pooling with specified kernel size, stride, and padding.
Input (4x4) 1 3 2 4 5 8 6 7 9 2 4 3 1 6 5 8 max Output (2x2) 8 7 9 8 kernel: 2x2 stride: 2 padding: 0 dashed borders = pooling windowsImplementation Requirements
- External libraries are not permitted
- The
solvefunction signature must remain unchanged - The final result must be stored in tensor
output
Max Pooling Operation
For each output position (n, c, h_out, w_out), compute the maximum value over the corresponding input window:
output[n, c, h_out, w_out] = max(input[n, c, h:h+kernel_size, w:w+kernel_size])
where h = h_out * stride and w = w_out * stride
Example 1:
Input: input = [[[[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]]]]
kernel_size = 2
stride = 1
padding = 0
Output: output = [[[[5.0, 6.0],
[8.0, 9.0]]]]
Example 2:
Input: input = [[[[1.0, 2.0, 3.0, 4.0, 5.0],
[6.0, 7.0, 8.0, 9.0, 10.0],
[11.0, 12.0, 13.0, 14.0, 15.0],
[16.0, 17.0, 18.0, 19.0, 20.0],
[21.0, 22.0, 23.0, 24.0, 25.0]]]]
kernel_size = 3
stride = 1
padding = 1
Output: output = [[[[7.0, 8.0, 9.0, 10.0, 10.0],
[12.0, 13.0, 14.0, 15.0, 15.0],
[17.0, 18.0, 19.0, 20.0, 20.0],
[22.0, 23.0, 24.0, 25.0, 25.0],
[22.0, 23.0, 24.0, 25.0, 25.0]]]]
Constraints
- 1 ≤ N ≤ 100 (batch size)
- 1 ≤ C ≤ 512 (channels)
- 1 ≤ H, W ≤ 1024 (height, width)
- 1 ≤ kernel_size ≤ 16
- 1 ≤ stride ≤ 16
- 0 ≤ padding ≤ 16
- Input and output tensors use float32 precision
- Performance is measured with
N= 4,kernel_size= 3,stride= 2
Starter code (CUDA)
#include <cuda_runtime.h>
// input, output are device pointers (i.e. pointers to memory on the GPU)
extern "C" void solve(const float* input, float* output, int N, int C, int H, int W,
int kernel_size, int stride, int padding) {}
From LeetGPU, CC BY-NC-ND 4.0.