Implement stable softmax independently across each row of a two-dimensional CUDA tensor using Triton. For input value xr,c in row r and column c, let C be the number of columns and j index columns. The row maximum mr and normalized output yr,c are:
mr=0≤j<Cmaxxr,j. yr,c=∑j=0C−1exr,j−mrexr,c−mr.Complete only the empty kernel body; the supplied solve launcher is already implemented. The pointers identify the input and output buffers, row_stride is the input row stride in elements, n_cols is the row width, and BLOCK_SIZE is the compile-time padded width. Return no value; write normalized rows into the contiguous output tensor with the same shape and storage dtype as the input.
Input: x = [[1, 2, 3], [3, 2, 1]], dtype = float32
Output: [[0.090031, 0.244728, 0.665241], [0.665241, 0.244728, 0.090031]]
Explanation: Reversing a row reverses its probability order.
Input: x = [[-2, 0, 1, 4, -1]], dtype = float16
Output: [[0.0023, 0.016998, 0.046204, 0.928223, 0.006256]]
Input: x = [[8], [-3]], dtype = bfloat16
Output: [[1], [1]]
tl.max and tl.sum reduce values along a block axis.
tl.load supports a masked fallback value for padded columns.
tl.exp evaluates elementwise exponentials.
Accepts: array
Accepts: string
Submit a correct solution to see your kernel performance.
Implement stable softmax independently across each row of a two-dimensional CUDA tensor using Triton. For input value xr,c in row r and column c, let C be the number of columns and j index columns. The row maximum mr and normalized output yr,c are:
mr=0≤j<Cmaxxr,j. yr,c=∑j=0C−1exr,j−mrexr,c−mr.Complete only the empty kernel body; the supplied solve launcher is already implemented. The pointers identify the input and output buffers, row_stride is the input row stride in elements, n_cols is the row width, and BLOCK_SIZE is the compile-time padded width. Return no value; write normalized rows into the contiguous output tensor with the same shape and storage dtype as the input.
Input: x = [[1, 2, 3], [3, 2, 1]], dtype = float32
Output: [[0.090031, 0.244728, 0.665241], [0.665241, 0.244728, 0.090031]]
Explanation: Reversing a row reverses its probability order.
Input: x = [[-2, 0, 1, 4, -1]], dtype = float16
Output: [[0.0023, 0.016998, 0.046204, 0.928223, 0.006256]]
Input: x = [[8], [-3]], dtype = bfloat16
Output: [[1], [1]]
tl.max and tl.sum reduce values along a block axis.
tl.load supports a masked fallback value for padded columns.
tl.exp evaluates elementwise exponentials.
Accepts: array
Accepts: string
Submit a correct solution to see your kernel performance.