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/flash_attn_pre_f16.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 / flash_attn_pre_f16.cl` 156 lines · 5691 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 3 __kernel void flash_attn_kv_pad_f16( 4 const global void * k_void, ulong k_offset, 5 const global void * v_void, ulong v_offset, 6 global void * k_pad_void, 7 global void * v_pad_void, 8 const int n_kv, 9 const int n_head_kv, 10 const int n_batch, 11 const ulong k_nb1, const ulong k_nb2, const ulong k_nb3, 12 const ulong v_nb1, const ulong v_nb2, const ulong v_nb3 13 ) { 14 const int row_idx = get_global_id(0); 15 const int head_kv_idx = get_global_id(1); 16 const int batch_idx = get_global_id(2); 17 18 if (row_idx >= BLOCK_N || head_kv_idx >= n_head_kv || batch_idx >= n_batch) { 19 return; 20 } 21 22 const int tail_start = n_kv - (n_kv % BLOCK_N); 23 const int src_row_idx = tail_start + row_idx; 24 25 const global char * k_src = (const global char *) k_void + k_offset; 26 const global char * v_src = (const global char *) v_void + v_offset; 27 global char * k_pad = (global char *) k_pad_void; 28 global char * v_pad = (global char *) v_pad_void; 29 30 const ulong k_dst_offset = ((ulong) batch_idx * (ulong) n_head_kv + (ulong) head_kv_idx) * ((ulong) BLOCK_N * k_nb1) + (ulong) row_idx * k_nb1; 31 const ulong v_dst_offset = ((ulong) batch_idx * (ulong) n_head_kv + (ulong) head_kv_idx) * ((ulong) BLOCK_N * v_nb1) + (ulong) row_idx * v_nb1; 32 33 if (src_row_idx < n_kv) { 34 const ulong k_src_offset = (ulong) batch_idx * k_nb3 + (ulong) head_kv_idx * k_nb2 + (ulong) src_row_idx * k_nb1; 35 const ulong v_src_offset = (ulong) batch_idx * v_nb3 + (ulong) head_kv_idx * v_nb2 + (ulong) src_row_idx * v_nb1; 36 37 for (ulong i = 0; i < k_nb1; ++i) { 38 k_pad[k_dst_offset + i] = k_src[k_src_offset + i]; 39 } 40 for (ulong i = 0; i < v_nb1; ++i) { 41 v_pad[v_dst_offset + i] = v_src[v_src_offset + i]; 42 } 43 } else { 44 for (ulong i = 0; i < k_nb1; ++i) { 45 k_pad[k_dst_offset + i] = 0; 46 } 47 for (ulong i = 0; i < v_nb1; ++i) { 48 v_pad[v_dst_offset + i] = 0; 49 } 50 } 51 } 52 53 __kernel void flash_attn_mask_pad_f16( 54 const global void * mask_void, ulong mask_offset, 55 global void * mask_pad_void, 56 const int n_q, 57 const int n_kv, 58 const ulong mask_nb1, 59 const ulong mask_nb2, 60 const ulong mask_nb3, 61 const int mask_ne2, 62 const int mask_ne3 63 ) { 64 const int col_idx = get_global_id(0); 65 const int q_row = get_global_id(1); 66 const int mask_slice = get_global_id(2); 67 68 if (col_idx >= BLOCK_N || q_row >= n_q || mask_slice >= mask_ne2 * mask_ne3) { 69 return; 70 } 71 72 const int tail_start = n_kv - (n_kv % BLOCK_N); 73 const int src_col_idx = tail_start + col_idx; 74 const int mask_head_idx = mask_slice % mask_ne2; 75 const int mask_batch_idx = mask_slice / mask_ne2; 76 77 const global char * mask_src_base = (const global char *) mask_void + mask_offset + 78 (ulong) mask_batch_idx * mask_nb3 + 79 (ulong) mask_head_idx * mask_nb2 + 80 (ulong) q_row * mask_nb1; 81 const global half * mask_src = (const global half *) mask_src_base; 82 83 global half * mask_pad = (global half *) mask_pad_void; 84 const ulong dst_idx = 85 (((ulong) mask_batch_idx * (ulong) mask_ne2 + (ulong) mask_head_idx) * (ulong) n_q + (ulong) q_row) * (ulong) BLOCK_N + 86 (ulong) col_idx; 87 88 mask_pad[dst_idx] = src_col_idx < n_kv ? mask_src[src_col_idx] : (half) (-INFINITY); 89 } 90 91 // Per-KV-tile mask class. 0=all -inf (skip tile), 1=mixed (apply mask), 92 // 2=all zero, no -inf (skip mask lookup). Causal diagonal tiles are class 1. 93 __kernel void flash_attn_blk_f16( 94 const global void * mask_void, ulong mask_offset, 95 global char * blk, 96 const int n_q, 97 const int n_kv, 98 const ulong mask_nb1, 99 const ulong mask_nb2, 100 const ulong mask_nb3, 101 const int mask_ne2, 102 const int mask_ne3 103 ) { 104 const int kv_block_idx = get_global_id(0); 105 const int q_block_idx = get_global_id(1); 106 const int mask_slice = get_global_id(2); 107 108 const int n_q_blocks = (n_q + BLOCK_M - 1) / BLOCK_M; 109 const int n_kv_blocks = (n_kv + BLOCK_N - 1) / BLOCK_N; 110 if (kv_block_idx >= n_kv_blocks || q_block_idx >= n_q_blocks || mask_slice >= mask_ne2 * mask_ne3) { 111 return; 112 } 113 114 const int mask_head_idx = mask_slice % mask_ne2; 115 const int mask_batch_idx = mask_slice / mask_ne2; 116 const int q_start = q_block_idx * BLOCK_M; 117 const int k_start = kv_block_idx * BLOCK_N; 118 const int q_count = min(BLOCK_M, n_q - q_start); 119 const int k_count = min(BLOCK_N, n_kv - k_start); 120 121 const half neg_max_half = (half) (-65504.0f); 122 char has_unmasked = 0; 123 char has_masked = 0; 124 char has_nonzero = 0; 125 126 const global char * mask_base = (const global char *) mask_void + mask_offset + 127 (ulong) mask_batch_idx * mask_nb3 + 128 (ulong) mask_head_idx * mask_nb2; 129 130 for (int qi = 0; qi < q_count; ++qi) { 131 const global half * mask_row = (const global half *) (mask_base + (ulong) (q_start + qi) * mask_nb1) + k_start; 132 for (int ki = 0; ki < k_count; ++ki) { 133 const half v = mask_row[ki]; 134 if (v <= neg_max_half) { 135 has_masked = 1; 136 } else { 137 has_unmasked = 1; 138 if (v != (half) 0.0f) { 139 has_nonzero = 1; 140 } 141 } 142 } 143 if (has_masked && has_unmasked) break; // mixed tile — short-circuit. 144 } 145 146 char res; 147 if (has_unmasked == 0) { 148 res = 0; 149 } else if (has_masked || has_nonzero) { 150 res = 1; 151 } else { 152 res = 2; 153 } 154 155 blk[((ulong) mask_slice * (ulong) n_q_blocks + (ulong) q_block_idx) * (ulong) n_kv_blocks + (ulong) kv_block_idx] = res; 156 }