6. Softmax Attention medium
Implement a GPU program that computes the softmax attention operation for a given set of matrices. Given the query
matrix Q of size M×d, key matrix K of size N×d, and value matrix
V of size N×d, your program should compute the output matrix using the formula:
where the softmax function is
applied row-wise.
Implementation Requirements
- Use only GPU native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The final result must be stored in the output matrix
output
Example 1:
Input:
Q (2×4):
K (3×4):
V (3×4):
Output:
output (2×4):
Example 2:
Input:
Q (1×2):
K (2×2):
V (2×2):
Output:
output (1×2):
Constraints
- Matrix
Qis of sizeM×dand matricesKandVare of sizeN×d - 1 ≤
M,N≤ 100,000 - 1 ≤
d≤ 128 - Performance is measured with
M= 512,N= 256
Starter code (CUDA)
#include <cuda_runtime.h>
// Q, K, V, output are device pointers
extern "C" void solve(const float* Q, const float* K, const float* V, float* output, int M, int N,
int d) {}
From LeetGPU, CC BY-NC-ND 4.0.