12. Multi-Head Attention hard
Implement a program for multi-head self-attention. Given three input matrices (queries), (keys), and (values) of size , compute: where each head computes: with and being the i-th head's partition of the input matrices.
Implementation Requirements
- Use only native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The final result must be stored in the
outputarray
Example 1:
Input: Output:
Example 2:
Input: Output:
Constraints
1 ≤ N ≤ 100002 ≤ d_model ≤ 10241 ≤ h ≤ d_modeld_model % h == 0-10.0 ≤ values ≤ 10.0- Performance is measured with
N= 1,024,d_model= 1,024, and values sampled from[-1.0, 1.0]
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 N,
int d_model, int h) {}
From LeetGPU, CC BY-NC-ND 4.0.