Tensara Logo

tensara

Sum Over Dimension

EASY

Perform sum reduction over a specified dimension of an input tensor:

output[i1,,id1,1,id+1,,in]=id=0Sd1input[i1,,id,,in]\text{output}[i_1,\ldots,i_{d-1},1,i_{d+1},\ldots,i_n] = \sum_{i_d=0}^{S_d-1} \text{input}[i_1,\ldots,i_d,\ldots,i_n]

where dd is the dimension to reduce over, nn is the number of dimensions, and SdS_d is the size of dimension dd.

Input:

  • Tensor input of arbitrary shape S1×S2××SnS_1 \times S_2 \times \cdots \times S_n
  • dim (dd): Dimension to reduce over (0-based indexing)
  • shape: Array containing the dimensions of the input tensor
  • ndim (nn): Number of dimensions in the input tensor

Output:

  • Tensor output with shape S1××Sd1×1×Sd+1××SnS_1 \times \cdots \times S_{d-1} \times 1 \times S_{d+1} \times \cdots \times S_n
    • The reduced dimension is kept with size 1 (keepdim=True)

Notes:

  • The input tensor is stored in row-major order
  • The reduction should maintain numerical stability by using appropriate accumulation techniques
  • The output tensor preserves the dimensionality of the input tensor with the reduced dimension having size 1
  • This problem is adapted from KernelBench

GPU Type

Language

Data Type

Loading...

Loading editor...

CUDA C++ environment

Sample Run Results

Hit "Run" to test your code with sample inputs

Desktop Required for Code Submission

For the best coding experience, please switch to a desktop device to write and submit your solution.