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/ggml/blob/master/src/ggml-opencl/kernels/lightning_indexer.cl # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-opencl / kernels / lightning_indexer.cl` 111 lines ยท 3775 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 #ifdef cl_khr_subgroup_shuffle 3 #pragma OPENCL EXTENSION cl_khr_subgroup_shuffle : enable 4 #endif 5 6 kernel void kernel_lightning_indexer( 7 global const char * q, 8 ulong offset_q, 9 global const char * k, 10 ulong offset_k, 11 global const char * w, 12 ulong offset_w, 13 global const char * mask, 14 ulong offset_m, 15 global float * dst, 16 ulong offset_d, 17 int head_size, 18 int n_heads, 19 int n_tokens, 20 int n_keys, 21 int n_masks, 22 int type_k, 23 ulong nbq1, 24 ulong nbq2, 25 ulong nbq3, 26 ulong nbk2, 27 ulong nbk3, 28 ulong nbw1, 29 ulong nbw3, 30 ulong nbm1, 31 ulong nbm3, 32 int use_shuffle 33 ) { 34 const int tid = get_local_id(0); 35 const int lane = tid & (use_shuffle ? 3 : 15); 36 const int key = use_shuffle ? get_group_id(0)*16 + tid/4 : get_group_id(0)*4 + tid/16; 37 const int token = get_group_id(1); 38 const int stream = get_group_id(2); 39 40 q += offset_q + token*nbq2 + stream*nbq3; 41 k += offset_k + (ulong) key*nbk2 + stream*nbk3; 42 w += offset_w + token*nbw1 + stream*nbw3; 43 mask += offset_m + token*nbm1 + (stream % n_masks)*nbm3; 44 dst = (global float *) ((global char *) dst + offset_d); 45 46 #ifdef cl_khr_subgroup_shuffle 47 if (use_shuffle) { 48 // Each group of four lanes keeps one key for all query heads. 49 float4 cached[8]; 50 #pragma unroll 51 for (int j = 0; j < 8; ++j) { 52 const int i = 4*(lane + j*4); 53 cached[j] = key < n_keys ? (type_k == 0 ? vload4(0, (global const float *) k + i) : convert_float4(vload4(0, (global const half *) k + i))) : (float4) (0.0f); 54 } 55 56 float score = 0.0f; 57 for (int h = 0; h < n_heads; ++h) { 58 global const float * q_head = (global const float *) (q + h*nbq1); 59 float4 sum = (float4) (0.0f); 60 #pragma unroll 61 for (int j = 0; j < 8; ++j) { 62 sum = mad(vload4(0, q_head + 4*(lane + j*4)), cached[j], sum); 63 } 64 float value = sum.s0 + sum.s1 + sum.s2 + sum.s3; 65 value += sub_group_shuffle_xor(value, 2); 66 value += sub_group_shuffle_xor(value, 1); 67 if (lane == 0) { 68 score = mad(fmax(value, 0.0f), ((global const float *) w)[h], score); 69 } 70 } 71 if (lane == 0 && key < n_keys) { 72 dst[((ulong) stream*n_tokens + token)*n_keys + key] = score + (float) ((global const half *) mask)[key]; 73 } 74 return; 75 } 76 #endif 77 78 local float sums[64]; 79 float score = 0.0f; 80 for (int h = 0; h < n_heads; ++h) { 81 global const float * q_head = (global const float *) (q + h*nbq1); 82 float sum = 0.0f; 83 if (key < n_keys) { 84 for (int i = lane; i < head_size; i += 16) { 85 float kv; 86 if (type_k == 0) { 87 kv = ((global const float *) k)[i]; 88 } else { 89 kv = (float) ((global const half *) k)[i]; 90 } 91 sum = mad(q_head[i], kv, sum); 92 } 93 } 94 sums[tid] = sum; 95 barrier(CLK_LOCAL_MEM_FENCE); 96 for (int stride = 8; stride > 0; stride /= 2) { 97 if (lane < stride) { 98 sums[tid] += sums[tid + stride]; 99 } 100 barrier(CLK_LOCAL_MEM_FENCE); 101 } 102 if (lane == 0) { 103 score = mad(fmax(sums[tid], 0.0f), ((global const float *) w)[h], score); 104 } 105 barrier(CLK_LOCAL_MEM_FENCE); 106 } 107 108 if (lane == 0 && key < n_keys) { 109 dst[((ulong) stream*n_tokens + token)*n_keys + key] = score + (float) ((global const half *) mask)[key]; 110 } 111 }