Implement a numerically stable, fused row-wise softmax as a single CUDA kernel.
You are given a matrix input of shape (rows, cols) stored in row-major order. For each row independently, compute:
output[i][j] = exp(input[i][j] - max_j input[i][j]) / sum_j exp(input[i][j] - max_j input[i][j])
Subtracting the per-row maximum before exp is required for numerical stability (large logits must not overflow to inf).
Fusion requirement: perform the max reduction, the exp, the sum reduction, and the final normalize inside one __global__ kernel. Do not launch separate kernels for each stage. Use shared memory and __syncthreads() for the per-row reductions.
Suggested launch configuration: one thread block per row. You may assume 1 <= cols <= 1024 so a single block can cover an entire row (use a power-of-two block size >= cols, with out-of-range threads contributing the identity for each reduction: -FLT_MAX for max, 0 for sum).
Complete:
__global__ kernel that computes fused row-wise softmax.void solve(const float* input, float* output, int rows, int cols) where input and output are host pointers with rows * cols elements. Allocate device memory, copy in, launch, copy out, and free.Do not write a main() in your solution.