Tensara Logo

tensara

All Problems

1D Convolution

EASY

Perform 1D convolution between an input signal and a kernel with zero padding and centered kernel.

Let r=K12r = \frac{K-1}{2}. Out-of-bounds accesses to A\text{A} are treated as zero.

C[i]=j=0K1A[i+jr]B[j]\text{C}[i] = \sum_{j=0}^{K-1} \text{A}[\,i + j - r\,] \cdot \text{B}[j]

The convolution operation slides the kernel over the input signal, computing the sum of element-wise multiplications at each position. Zero padding is used at the boundaries.

Input:

  • Vector A\text{A} of size N\text{N} (input signal)
  • Vector B\text{B} of size K\text{K} (convolution kernel)

Output:

  • Vector C\text{C} of size N\text{N} (convolved signal)

Notes:

  • K\text{K} is odd and smaller than N\text{N}
  • Use zero padding at the boundaries where the kernel extends beyond the input signal
  • The kernel is centered at each position, with (K1)/2(K-1)/2 elements on each side
  • Output size is NN (same as input) due to padding
  • This matches PyTorch torch.nn.functional.conv1d(..., padding=K//2) (cross-correlation, kernel is not flipped)
  • 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.