TensorTonic
Problems
Softmax as a CUDA Kernel
Activation FunctionsGPU Programming
Medium

Compute softmax over the entire input vector of N float32 values on the GPU.

yi=exi∑j=0N−1exjy_i = \frac{e^{x_i}}{\sum_{j=0}^{N-1} e^{x_j}}yi​=∑j=0N−1​exj​exi​​

Here, x is the input vector, y is the probability vector, N is its length, and i and j index its elements. The normalization includes every element, not a separate group for each thread block. Results must remain valid for large positive or negative inputs.

Write all N probabilities into the preallocated float32 output device buffer, preserving their order and leaving the input unchanged. The function returns no value.

Loading visualization...

Examples

Input: input = [1, 2, 3]

Output: [0.090031, 0.244728, 0.665241]

Explanation: The largest score receives the largest probability, and the three probabilities sum to one.

Input: input = [1, -2, 3, -4]

Output: [0.118405, 0.005895, 0.874902, 0.000798]

Hint 1

Subtracting a common maximum keeps exponential arguments nonpositive.

Hint 2

A grid-stride loop can cover a vector longer than a thread block.

Hint 3

Block-wide reductions require every participating thread to reach their barriers.

Requirements

  • Normalize across the full vector, with probabilities summing to one within float32 precision.
  • Produce numerically stable results for all valid inputs.
  • Write every output element without modifying the input or accessing beyond the buffers.
  • Use CUDA without external computation libraries. The host launch may be adapted, but the solve signature must stay unchanged.

Constraints

  • N is between 1 and 500,000.
  • Input and output are contiguous device buffers containing N float32 values.
  • Input values are finite.
Try Similar Problems
Implement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasyTanh as a CUDA KernelEasyTriton Row-Wise SoftmaxMediumOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumFused Matmul + Bias + ReLU in TritonMediumVector Addition as a CUDA KernelEasyVector Subtraction as a CUDA KernelEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Softmax as a CUDA Kernel

Softmax as a CUDA Kernel

Activation FunctionsGPU Programming
Medium

Compute softmax over the entire input vector of N float32 values on the GPU.

yi=exi∑j=0N−1exjy_i = \frac{e^{x_i}}{\sum_{j=0}^{N-1} e^{x_j}}yi​=∑j=0N−1​exj​exi​​

Here, x is the input vector, y is the probability vector, N is its length, and i and j index its elements. The normalization includes every element, not a separate group for each thread block. Results must remain valid for large positive or negative inputs.

Write all N probabilities into the preallocated float32 output device buffer, preserving their order and leaving the input unchanged. The function returns no value.

Loading visualization...

Examples

Input: input = [1, 2, 3]

Output: [0.090031, 0.244728, 0.665241]

Explanation: The largest score receives the largest probability, and the three probabilities sum to one.

Input: input = [1, -2, 3, -4]

Output: [0.118405, 0.005895, 0.874902, 0.000798]

Hint 1

Subtracting a common maximum keeps exponential arguments nonpositive.

Hint 2

A grid-stride loop can cover a vector longer than a thread block.

Hint 3

Block-wide reductions require every participating thread to reach their barriers.

Requirements

  • Normalize across the full vector, with probabilities summing to one within float32 precision.
  • Produce numerically stable results for all valid inputs.
  • Write every output element without modifying the input or accessing beyond the buffers.
  • Use CUDA without external computation libraries. The host launch may be adapted, but the solve signature must stay unchanged.

Constraints

  • N is between 1 and 500,000.
  • Input and output are contiguous device buffers containing N float32 values.
  • Input values are finite.
Try Similar Problems
Implement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasyTanh as a CUDA KernelEasyTriton Row-Wise SoftmaxMediumOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumFused Matmul + Bias + ReLU in TritonMediumVector Addition as a CUDA KernelEasyVector Subtraction as a CUDA KernelEasy

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1