TensorTonic
Study Plans
Study Plan
Blockwise Online Softmax
Activation FunctionsGPU Programming
Hard

Implement stable row-wise softmax in CUDA using online summaries. A summary contains the maximum mmm and the shifted exponential sum ℓ\ellℓ of its values:

ℓ=∑jexj−m.\ell=\sum_j e^{x_j-m}.ℓ=j∑​exj​−m.

Here, jjj indexes values and xjx_jxj​ is one value. For another block with summary mb,ℓbm_b,\ell_bmb​,ℓb​, the combined summary is:

mnew=max⁡(m,mb).m_{\mathrm{new}}=\max(m,m_b).mnew​=max(m,mb​). ℓnew=em−mnewℓ+emb−mnewℓb.\ell_{\mathrm{new}}=e^{m-m_{\mathrm{new}}}\ell+e^{m_b-m_{\mathrm{new}}}\ell_b.ℓnew​=em−mnew​ℓ+emb​−mnew​ℓb​.

Here, subscript bbb denotes the other block and subscript new denotes their union. Once mmm and ℓ\ellℓ summarize the complete row, its normalized output at column jjj is:

yj=exj−mℓ.y_j=\frac{e^{x_j-m}}{\ell}.yj​=ℓexj​−m​.

Complete the empty CUDA kernel; the supplied solve launcher is already implemented. Input and output are preallocated matrices with the supplied dimensions and element strides. The kernel returns no value and writes the probabilities into output, preserving the input shape and storage dtype.

Examples

Input: input = [[1, 2, 3], [-1, 0, 1]], dtype = float32, block_size = 64

Output: [[0.090031, 0.244728, 0.665241], [0.090031, 0.244728, 0.665241]]

Explanation: The rows differ by a constant shift, so they have the same probabilities.

Input: input = [[1000, 1001, 1002, 999]], dtype = float32, block_size = 128

Output: [[0.087144, 0.236883, 0.643914, 0.032059]]

Input: input = [[-3, 2, 0.5, 4, -1]], dtype = float16, block_size = 32

Output: [[0.000777, 0.115356, 0.025742, 0.852539, 0.005745]]

Hint 1

expf evaluates the exponential in float32.

Hint 2

__syncthreads coordinates access to shared-memory summaries.

Hint 3

__half2float and __bfloat162float convert low-precision input values.

Requirements

  • Use CUDA with float32 arithmetic for the online summaries and normalization.
  • Preserve float32, float16, or bfloat16 storage in the output.
  • Support the supplied input and output strides without modifying the input.

Constraints

  • Input values are finite and input and output pointers reference separate CUDA buffers.
  • rows and cols are nonnegative integers; an empty dimension requires no writes.
  • Row and column strides are measured in elements.
  • dtype_code is 0 for float32, 1 for float16, and 2 for bfloat16.
  • block_size is a power of two from 32 through 1024; the supplied launcher handles invalid launch metadata.
Try Similar Problems
Fused Tiled Matmul and ReLUHardTriton Row-Wise SoftmaxMediumMasked Triton GELU KernelEasySoftmax as a CUDA KernelMediumOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumFused Matmul + Bias + ReLU in TritonMediumImplement 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 KernelEasy

Accepts: array

Accepts: string

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Blockwise Online Softmax

Activation FunctionsGPU Programming
Hard

Implement stable row-wise softmax in CUDA using online summaries. A summary contains the maximum mmm and the shifted exponential sum ℓ\ellℓ of its values:

ℓ=∑jexj−m.\ell=\sum_j e^{x_j-m}.ℓ=j∑​exj​−m.

Here, jjj indexes values and xjx_jxj​ is one value. For another block with summary mb,ℓbm_b,\ell_bmb​,ℓb​, the combined summary is:

mnew=max⁡(m,mb).m_{\mathrm{new}}=\max(m,m_b).mnew​=max(m,mb​). ℓnew=em−mnewℓ+emb−mnewℓb.\ell_{\mathrm{new}}=e^{m-m_{\mathrm{new}}}\ell+e^{m_b-m_{\mathrm{new}}}\ell_b.ℓnew​=em−mnew​ℓ+emb​−mnew​ℓb​.

Here, subscript bbb denotes the other block and subscript new denotes their union. Once mmm and ℓ\ellℓ summarize the complete row, its normalized output at column jjj is:

yj=exj−mℓ.y_j=\frac{e^{x_j-m}}{\ell}.yj​=ℓexj​−m​.

Complete the empty CUDA kernel; the supplied solve launcher is already implemented. Input and output are preallocated matrices with the supplied dimensions and element strides. The kernel returns no value and writes the probabilities into output, preserving the input shape and storage dtype.

Examples

Input: input = [[1, 2, 3], [-1, 0, 1]], dtype = float32, block_size = 64

Output: [[0.090031, 0.244728, 0.665241], [0.090031, 0.244728, 0.665241]]

Explanation: The rows differ by a constant shift, so they have the same probabilities.

Input: input = [[1000, 1001, 1002, 999]], dtype = float32, block_size = 128

Output: [[0.087144, 0.236883, 0.643914, 0.032059]]

Input: input = [[-3, 2, 0.5, 4, -1]], dtype = float16, block_size = 32

Output: [[0.000777, 0.115356, 0.025742, 0.852539, 0.005745]]

Hint 1

expf evaluates the exponential in float32.

Hint 2

__syncthreads coordinates access to shared-memory summaries.

Hint 3

__half2float and __bfloat162float convert low-precision input values.

Requirements

  • Use CUDA with float32 arithmetic for the online summaries and normalization.
  • Preserve float32, float16, or bfloat16 storage in the output.
  • Support the supplied input and output strides without modifying the input.

Constraints

  • Input values are finite and input and output pointers reference separate CUDA buffers.
  • rows and cols are nonnegative integers; an empty dimension requires no writes.
  • Row and column strides are measured in elements.
  • dtype_code is 0 for float32, 1 for float16, and 2 for bfloat16.
  • block_size is a power of two from 32 through 1024; the supplied launcher handles invalid launch metadata.
Try Similar Problems
Fused Tiled Matmul and ReLUHardTriton Row-Wise SoftmaxMediumMasked Triton GELU KernelEasySoftmax as a CUDA KernelMediumOnline Softmax Kernel in TritonMediumFused Row-Wise Softmax in TritonMediumFused Matmul + Bias + ReLU in TritonMediumImplement 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 KernelEasy

Accepts: array

Accepts: string

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.