Tensara Logo

tensara

Softmax

MEDIUM

Compute the softmax function over a specified dimension of an input tensor:

softmax(xi)=exp(xi)j=1Sdexp(xj)\text{softmax}(x_i) = \frac{\exp(x_i)}{\sum_{j=1}^{S_d} \exp(x_j)}

where xix_i represents elements along the specified dimension dd, 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 compute softmax 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 the same shape as input, containing the softmax probabilities

Notes:

  • The input tensor is stored in row-major order
  • The output values should be in the range (0, 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.