TensorTonic
Problems
Max Pool 2D as a CUDA Kernel
Computer VisionGPU Programming
Medium

Implement a CUDA kernel that performs 2D max pooling on a single-channel input III of shape (H,W)(H, W)(H,W) with pool window (kH,kW)(kH, kW)(kH,kW) and stride (sH,sW)(sH, sW)(sH,sW). There is no padding.

For each output position (i,j)(i, j)(i,j):

output[i,j]=max⁡a∈[0,kH), b∈[0,kW)input[i⋅sH+a, j⋅sW+b]\text{output}[i, j] = \max_{a \in [0, kH),\ b \in [0, kW)} \text{input}[i \cdot sH + a,\ j \cdot sW + b]output[i,j]=a∈[0,kH), b∈[0,kW)max​input[i⋅sH+a, j⋅sW+b]

The output has shape ((H−kH)/sH+1,(W−kW)/sW+1)((H - kH) / sH + 1, (W - kW) / sW + 1)((H−kH)/sH+1,(W−kW)/sW+1). Both buffers are stored as row-major flat arrays, so element (i,j)(i, j)(i,j) of an R×CR \times CR×C matrix lives at linear index i⋅C+ji \cdot C + ji⋅C+j.

Loading visualization...

Examples

Input: H = 4, W = 4, kH = 2, kW = 2, sH = 2, sW = 2, input = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

Output: [[6.0, 8.0], [14.0, 16.0]]

A standard non-overlapping 2×22 \times 22×2 pool keeps the max of each disjoint window.

Input: H = 3, W = 3, kH = 2, kW = 2, sH = 1, sW = 1, input = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Output: [[5.0, 6.0], [8.0, 9.0]]

With stride 111 the windows overlap, producing a 2×22 \times 22×2 output of local maxima.

Hint 1

Each output cell owns an independent window of input reads. Initialise the reduction with -FLT_MAX (from <float.h>) so the first comparison always succeeds, then update with fmaxf.

Hint 2

Stride (sH,sW)(sH, sW)(sH,sW) controls how much the windows overlap or skip. The base index of the window owned by output (i,j)(i, j)(i,j) is (i*sH, j*sW); iterate a∈[0,kH)a \in [0, kH)a∈[0,kH), b∈[0,kW)b \in [0, kW)b∈[0,kW) over it.

Hint 3

Map blockIdx.y, threadIdx.y to the output row and blockIdx.x, threadIdx.x to the output column, and remember the output buffer has stride outW = (W - kW) / sW + 1.

Requirements

  • Launch one thread per output element using a 2D grid of 16×1616 \times 1616×16 blocks
  • Reduce each kH×kWkH \times kWkH×kW window with fmaxf\texttt{fmaxf}fmaxf into a register, then write once
  • Respect strides sH,sWsH, sWsH,sW when indexing into input\texttt{input}input and row-major output stride outW\texttt{outW}outW
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤H,W≤20481 \le H, W \le 20481≤H,W≤2048
  • 1≤kH≤H1 \le kH \le H1≤kH≤H and 1≤kW≤W1 \le kW \le W1≤kW≤W
  • 1≤sH,sW1 \le sH, sW1≤sH,sW
  • All buffers are device pointers
  • Use 16×1616 \times 1616×16 thread blocks, values are 32-bit float
Try Similar Problems
2D Convolution as a CUDA KernelMedium3D Convolution as a CUDA KernelHard1D Convolution as a CUDA KernelEasyArgmax as a CUDA KernelMediumRMS Normalization as a CUDA KernelMediumMean and Variance as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumSoftmax as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMedium

Accepts: array

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Max Pool 2D as a CUDA Kernel

Max Pool 2D as a CUDA Kernel

Computer VisionGPU Programming
Medium

Implement a CUDA kernel that performs 2D max pooling on a single-channel input III of shape (H,W)(H, W)(H,W) with pool window (kH,kW)(kH, kW)(kH,kW) and stride (sH,sW)(sH, sW)(sH,sW). There is no padding.

For each output position (i,j)(i, j)(i,j):

output[i,j]=max⁡a∈[0,kH), b∈[0,kW)input[i⋅sH+a, j⋅sW+b]\text{output}[i, j] = \max_{a \in [0, kH),\ b \in [0, kW)} \text{input}[i \cdot sH + a,\ j \cdot sW + b]output[i,j]=a∈[0,kH), b∈[0,kW)max​input[i⋅sH+a, j⋅sW+b]

The output has shape ((H−kH)/sH+1,(W−kW)/sW+1)((H - kH) / sH + 1, (W - kW) / sW + 1)((H−kH)/sH+1,(W−kW)/sW+1). Both buffers are stored as row-major flat arrays, so element (i,j)(i, j)(i,j) of an R×CR \times CR×C matrix lives at linear index i⋅C+ji \cdot C + ji⋅C+j.

Loading visualization...

Examples

Input: H = 4, W = 4, kH = 2, kW = 2, sH = 2, sW = 2, input = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

Output: [[6.0, 8.0], [14.0, 16.0]]

A standard non-overlapping 2×22 \times 22×2 pool keeps the max of each disjoint window.

Input: H = 3, W = 3, kH = 2, kW = 2, sH = 1, sW = 1, input = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Output: [[5.0, 6.0], [8.0, 9.0]]

With stride 111 the windows overlap, producing a 2×22 \times 22×2 output of local maxima.

Hint 1

Each output cell owns an independent window of input reads. Initialise the reduction with -FLT_MAX (from <float.h>) so the first comparison always succeeds, then update with fmaxf.

Hint 2

Stride (sH,sW)(sH, sW)(sH,sW) controls how much the windows overlap or skip. The base index of the window owned by output (i,j)(i, j)(i,j) is (i*sH, j*sW); iterate a∈[0,kH)a \in [0, kH)a∈[0,kH), b∈[0,kW)b \in [0, kW)b∈[0,kW) over it.

Hint 3

Map blockIdx.y, threadIdx.y to the output row and blockIdx.x, threadIdx.x to the output column, and remember the output buffer has stride outW = (W - kW) / sW + 1.

Requirements

  • Launch one thread per output element using a 2D grid of 16×1616 \times 1616×16 blocks
  • Reduce each kH×kWkH \times kWkH×kW window with fmaxf\texttt{fmaxf}fmaxf into a register, then write once
  • Respect strides sH,sWsH, sWsH,sW when indexing into input\texttt{input}input and row-major output stride outW\texttt{outW}outW
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤H,W≤20481 \le H, W \le 20481≤H,W≤2048
  • 1≤kH≤H1 \le kH \le H1≤kH≤H and 1≤kW≤W1 \le kW \le W1≤kW≤W
  • 1≤sH,sW1 \le sH, sW1≤sH,sW
  • All buffers are device pointers
  • Use 16×1616 \times 1616×16 thread blocks, values are 32-bit float
Try Similar Problems
2D Convolution as a CUDA KernelMedium3D Convolution as a CUDA KernelHard1D Convolution as a CUDA KernelEasyArgmax as a CUDA KernelMediumRMS Normalization as a CUDA KernelMediumMean and Variance as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumSoftmax as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMedium

Accepts: array

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1