Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/src/mojollama/kernels/gqa_attention.c # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / gqa_attention.c` 135 lines · 5526 bytes Raw Blame History 1 /* gqa_attention.c — GQA attention with cblas_sgemm + single-threaded OpenBLAS. 2 * 3 * Uses cblas_sgemm for the attention score computation and V aggregation. 4 * OpenBLAS is set to single-thread (openblas_set_num_threads(1)) to avoid 5 * nested OMP parallelism. The outer OMP loop handles head parallelism instead. 6 * 7 * For each KV head: 8 * scores[gqa_rep, seq_len] = Q[gqa_rep, HD] @ K[kv_h, seq_len, HD]^T * scale 9 * softmax each head's scores 10 * out[gqa_rep, HD] = scores * V[kv_h, seq_len, HD] 11 * 12 * Compile: 13 * gcc -O3 -mavx2 -mfma -fopenmp -shared -fPIC -o gqa_attention.so \ 14 * gqa_attention.c -lopenblas -lm 15 */ 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 23 static inline float hsum_ps(__m256 v) { 24 __m128 lo = _mm256_castps256_ps128(v); 25 __m128 hi = _mm256_extractf128_ps(v, 1); 26 lo = _mm_add_ps(lo, hi); 27 lo = _mm_hadd_ps(lo, lo); 28 lo = _mm_hadd_ps(lo, lo); 29 return _mm_cvtss_f32(lo); 30 } 31 32 static inline __m256 exp_ps_simd(__m256 x) { 33 const __m256 log2e = _mm256_set1_ps(1.`4426950408889634f`); 34 __m256 t = _mm256_mul_ps(x, log2e); 35 __m256i n = _mm256_cvtps_epi32(_mm256_add_ps(t, _mm256_set1_ps(0.5f))); 36 __m256 f = _mm256_sub_ps(t, _mm256_cvtepi32_ps(n)); 37 __m256 p = _mm256_set1_ps(1.0f); 38 p = _mm256_fmadd_ps(p, f, _mm256_set1_ps(0.`6931471805599453f`)); 39 p = _mm256_fmadd_ps(p, f, _mm256_set1_ps(0.`2402265069591007f`)); 40 p = _mm256_fmadd_ps(p, f, _mm256_set1_ps(0.`0555041086648216f`)); 41 p = _mm256_fmadd_ps(p, f, _mm256_set1_ps(0.`0096181291076285f`)); 42 p = _mm256_fmadd_ps(p, f, _mm256_set1_ps(0.`0013333558146428f`)); 43 __m256i biased = _mm256_add_epi32(n, _mm256_set1_epi32(127)); 44 biased = _mm256_max_epi32(biased, _mm256_setzero_si256()); 45 biased = _mm256_min_epi32(biased, _mm256_set1_epi32(255)); 46 __m256 two_n = _mm256_castsi256_ps(_mm256_slli_epi32(biased, 23)); 47 return _mm256_mul_ps(p, two_n); 48 } 49 50 static inline void softmax_simd(float *scores, int seq_len) { 51 int s; 52 __m256 vmax = _mm256_set1_ps(-1e30f); 53 for (s = 0; s <= seq_len - 8; s += 8) 54 vmax = _mm256_max_ps(vmax, _mm256_loadu_ps(scores + s)); 55 float max_val; 56 { 57 __m128 v = _mm256_castps256_ps128(vmax); 58 __m128 v2 = _mm256_extractf128_ps(vmax, 1); 59 v = _mm_max_ps(v, v2); 60 v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0x4e)); 61 v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0xb1)); 62 max_val = _mm_cvtss_f32(v); 63 } 64 for (; s < seq_len; s++) 65 if (scores[s] > max_val) max_val = scores[s]; 66 67 __m256 vsum = _mm256_setzero_ps(); 68 __m256 vmaxv = _mm256_set1_ps(max_val); 69 for (s = 0; s <= seq_len - 8; s += 8) { 70 __m256 v = _mm256_sub_ps(_mm256_loadu_ps(scores + s), vmaxv); 71 v = _mm256_min_ps(_mm256_max_ps(v, _mm256_set1_ps(-80.0f)), _mm256_set1_ps(80.0f)); 72 __m256 e = exp_ps_simd(v); 73 _mm256_storeu_ps(scores + s, e); 74 vsum = _mm256_add_ps(vsum, e); 75 } 76 float sum_exp = hsum_ps(vsum); 77 for (; s < seq_len; s++) { 78 float e = expf(scores[s] - max_val); 79 if (e > 1e38f) e = 1e38f; 80 scores[s] = e; sum_exp += e; 81 } 82 float inv_sum = 1.0f / (sum_exp + 1e-10f); 83 __m256 invv = _mm256_set1_ps(inv_sum); 84 for (s = 0; s <= seq_len - 8; s += 8) 85 _mm256_storeu_ps(scores + s, _mm256_mul_ps(_mm256_loadu_ps(scores + s), invv)); 86 for (; s < seq_len; s++) scores[s] *= inv_sum; 87 } 88 89 /* GQA attention decode with cblas_sgemm. 90 * Sets OpenBLAS to single-thread to avoid nested OMP conflicts. 91 * Uses OMP parallelism over kv_heads instead. 92 */ 93 void gqa_attention_decode(const float *q, const float *k_cache, const float *v_cache, 94 float *out, int seq_len, int n_head, int n_kv_head, int head_dim, 95 float *workspace) { 96 int gqa_rep = n_head / n_kv_head; 97 float scale = 1.0f / sqrtf((float)head_dim); 98 int kv_stride = n_kv_head * head_dim; 99 100 /* Set OpenBLAS to single-thread (avoid nested OMP) */ 101 openblas_set_num_threads(1); 102 103 float *scores_all = workspace; 104 float *tmp_scores = workspace + (size_t)n_head * seq_len; 105 106 #pragma omp parallel for schedule(static) 107 for (int kv_h = 0; kv_h < n_kv_head; kv_h++) { 108 const float *qh = q + (size_t)kv_h * gqa_rep * head_dim; 109 const float *kh = k_cache + (size_t)kv_h * head_dim; 110 const float *vh = v_cache + (size_t)kv_h * head_dim; 111 112 /* scores[gqa_rep, seq_len] = Q[gqa_rep, HD] * K[kv_h, seq_len, HD]^T * scale */ 113 cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, 114 gqa_rep, seq_len, head_dim, 115 scale, qh, head_dim, kh, kv_stride, 116 0.0f, tmp_scores, seq_len); 117 118 /* Softmax each of the gqa_rep heads */ 119 for (int qi = 0; qi < gqa_rep; qi++) { 120 float *sh = scores_all + (size_t)(kv_h * gqa_rep + qi) * seq_len; 121 memcpy(sh, tmp_scores + (size_t)qi * seq_len, seq_len * sizeof(float)); 122 softmax_simd(sh, seq_len); 123 } 124 125 /* out[gqa_rep, HD] = scores * V[kv_h, seq_len, HD] */ 126 for (int qi = 0; qi < gqa_rep; qi++) { 127 float *sh = scores_all + (size_t)(kv_h * gqa_rep + qi) * seq_len; 128 float *oh = out + (size_t)(kv_h * gqa_rep + qi) * head_dim; 129 cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 130 1, head_dim, seq_len, 131 1.0f, sh, seq_len, vh, kv_stride, 132 0.0f, oh, head_dim); 133 } 134 } 135 }