LeetKernels
ProblemsStart solving
Problems

11. 3D Convolution medium

Implement a program that performs a 3D convolution operation. Given a 3D input volume and a 3D kernel (filter), compute the convolved output. The convolution should use a "valid" boundary condition (no padding).

For a 3D convolution, the output at position (i,j,k) is given by:

output(i,j,k)=d=0∑Kd​−1​r=0∑Kr​−1​c=0∑Kc​−1​input(i+d,j+r,k+c)⋅kernel(d,r,c)

The input consists of:

  • input: A 3D volume of 32-bit floats, as a 1D array (row-major, then depth).
  • kernel: A 3D kernel of 32-bit floats, as a 1D array (row-major, then depth).
  • input_depth, input_rows, input_cols: Dimensions of the input.
  • kernel_depth, kernel_rows, kernel_cols: Dimensions of the kernel.
Output:
  • output: A 1D array (row-major, then depth) storing the result.
Output dimensions:
  • output_depth = input_depth - kernel_depth + 1
  • output_rows = input_rows - kernel_rows + 1
  • output_cols = input_cols - kernel_cols + 1

Implementation Requirements

  • Use only native features (external libraries are not permitted)
  • The solve function signature must remain unchanged
  • The final result must be stored in output

Examples

Example 1:

Input volume V∈R3×3×3: Vd=0​Vd=1​Vd=2​​=​147​258​369​​=​101316​111417​121518​​=​192225​202326​212427​​​ Kernel K∈R2×3×3: Kd=0​Kd=1​​=​110​010​010​​=​110​110​001​​​ Output O∈R2×1×1: [82,163]

Example 2:

Input volume V∈R2×2×2: Vd=0​Vd=1​​=[13​24​]=[57​68​]​ Kernel K∈R2×2×2: Kd=0​Kd=1​​=[11​11​]=[11​11​]​ Output O∈R1×1×1: [36]

Constraints

  • 1 ≤ input_depth, input_rows, input_cols ≤ 256
  • 1 ≤ kernel_depth, kernel_rows, kernel_cols ≤ 5
  • kernel_depth ≤ input_depth
  • kernel_rows ≤ input_rows
  • kernel_cols ≤ input_cols
  • Performance is measured with input_cols = 128, input_rows = 128, kernel_cols = 5, kernel_rows = 5

Starter code (CUDA)

#include <cuda_runtime.h>

// input, kernel, output are device pointers
extern "C" void solve(const float* input, const float* kernel, float* output, int input_depth,
                      int input_rows, int input_cols, int kernel_depth, int kernel_rows,
                      int kernel_cols) {}

Solve it on your GPU

Previous: 2D ConvolutionNext: Multi-Head Attention

From LeetGPU, CC BY-NC-ND 4.0.