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/ssm_scan.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 / ssm_scan.cl` 346 lines ยท 14474 bytes Raw Blame History 1 // Mamba2 fused SSM scan kernel. One workgroup per (head, dim, seq); WG size = 2 // 64 threads. Each thread owns c_factor = d_state/64 state elements in 3 // private registers; the state stays resident across the n_tokens t-loop 4 // 5 // References: 6 // ggml/src/ggml-cuda/ssm-scan.cu:117 ssm_scan_f32_group 7 // ggml/src/ggml-cpu/ops.cpp:9368 ggml_compute_forward_ssm_scan_f32 8 9 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 10 11 #ifdef cl_khr_subgroups 12 #pragma OPENCL EXTENSION cl_khr_subgroups : enable 13 #endif 14 15 #if defined(cl_qcom_reqd_sub_group_size) 16 #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable 17 #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) 18 #else 19 #define REQD_SUBGROUP_SIZE_64 20 #endif 21 22 inline float softplus_f32(float x) { 23 return (x <= 20.0f) ? log(1.0f + exp(x)) : x; 24 } 25 26 // d_state = 128 (most Mamba-2 models, e.g. mamba2-2.7B, Codestral-Mamba). 27 // WG = 64 threads, each holds 2 state elements (tid and tid+64). 28 REQD_SUBGROUP_SIZE_64 29 kernel void kernel_ssm_scan_f32_mamba2_d128( 30 global const char * src0_base, ulong src0_off, 31 global const char * src1_base, ulong src1_off, 32 global const char * src2_base, ulong src2_off, 33 global const char * src3_base, ulong src3_off, 34 global const char * src4_base, ulong src4_off, 35 global const char * src5_base, ulong src5_off, 36 global const char * src6_base, ulong src6_off, 37 global char * dst_base, ulong dst_off, 38 ulong s0_nb2, ulong s0_nb3, 39 ulong x_nb2, ulong x_nb3, 40 ulong dt_nb1, ulong dt_nb2, 41 ulong A_nb1, 42 ulong B_nb2, ulong B_nb3, 43 ulong C_nb2, ulong C_nb3, 44 ulong s_off_bytes, 45 int head_dim, int n_head, int n_group, int n_tokens 46 ) { 47 const int d_state = 128; 48 49 const int tid = (int) get_local_id(0); 50 const int wg_x = (int) get_group_id(0); 51 const int seq_id = (int) get_group_id(1); 52 53 const int head_id = wg_x / head_dim; 54 const int dim_id = wg_x - head_id * head_dim; 55 const int g = head_id / (n_head / n_group); 56 57 src0_base += src0_off; 58 src1_base += src1_off; 59 src2_base += src2_off; 60 src3_base += src3_off; 61 src4_base += src4_off; 62 src5_base += src5_off; 63 src6_base += src6_off; 64 dst_base += dst_off; 65 66 const int seq_slot = ((global const int *) src6_base)[seq_id]; 67 68 const ulong state_base_off = (ulong)seq_slot * s0_nb3 + (ulong)head_id * s0_nb2 69 + (ulong)dim_id * d_state * sizeof(float); 70 global const float * s0_warp = (global const float *)(src0_base + state_base_off); 71 const ulong state_out_off = (ulong)seq_id * s0_nb3 + (ulong)head_id * s0_nb2 72 + (ulong)dim_id * d_state * sizeof(float); 73 global float * s_warp = (global float *)(dst_base + s_off_bytes + state_out_off); 74 75 global const char * x_seq = src1_base + (ulong)seq_id * x_nb3; 76 global const char * dt_seq = src2_base + (ulong)seq_id * dt_nb2; 77 global const char * B_seq = src4_base + (ulong)seq_id * B_nb3 + (ulong)g * d_state * sizeof(float); 78 global const char * C_seq = src5_base + (ulong)seq_id * C_nb3 + (ulong)g * d_state * sizeof(float); 79 80 const ulong y_dim_total = (ulong)n_head * head_dim; 81 global float * y_seq = (global float *)dst_base 82 + (ulong)seq_id * (ulong)n_tokens * y_dim_total; 83 84 const float A_val = ((global const float *)src3_base)[(ulong)head_id * A_nb1 / sizeof(float)]; 85 86 // c_factor = 2: each thread owns 2 state elements (tid and tid+64). 87 float state0 = s0_warp[tid]; 88 float state1 = s0_warp[tid + 64]; 89 90 for (int t = 0; t < n_tokens; ++t) { 91 const float dt_h = ((global const float *)(dt_seq + (ulong)t * dt_nb1))[head_id]; 92 const float dt_softplus = softplus_f32(dt_h); 93 const float dA = exp(dt_softplus * A_val); 94 const float x_val = ((global const float *)(x_seq + (ulong)t * x_nb2))[(ulong)head_id * head_dim + dim_id]; 95 const float x_dt = x_val * dt_softplus; 96 97 const float B0 = ((global const float *)(B_seq + (ulong)t * B_nb2))[tid]; 98 const float B1 = ((global const float *)(B_seq + (ulong)t * B_nb2))[tid + 64]; 99 const float C0 = ((global const float *)(C_seq + (ulong)t * C_nb2))[tid]; 100 const float C1 = ((global const float *)(C_seq + (ulong)t * C_nb2))[tid + 64]; 101 102 state0 = state0 * dA + B0 * x_dt; 103 state1 = state1 * dA + B1 * x_dt; 104 const float partial = state0 * C0 + state1 * C1; 105 106 const float sum = sub_group_reduce_add(partial); 107 if (tid == 0) { 108 y_seq[(ulong)t * y_dim_total + (ulong)head_id * head_dim + dim_id] = sum; 109 } 110 } 111 112 s_warp[tid] = state0; 113 s_warp[tid + 64] = state1; 114 } 115 116 // d_state = 256 (Falcon-H1). WG = 64 threads, each holds 4 state elements. 117 REQD_SUBGROUP_SIZE_64 118 kernel void kernel_ssm_scan_f32_mamba2_d256( 119 global const char * src0_base, ulong src0_off, 120 global const char * src1_base, ulong src1_off, 121 global const char * src2_base, ulong src2_off, 122 global const char * src3_base, ulong src3_off, 123 global const char * src4_base, ulong src4_off, 124 global const char * src5_base, ulong src5_off, 125 global const char * src6_base, ulong src6_off, 126 global char * dst_base, ulong dst_off, 127 ulong s0_nb2, ulong s0_nb3, 128 ulong x_nb2, ulong x_nb3, 129 ulong dt_nb1, ulong dt_nb2, 130 ulong A_nb1, 131 ulong B_nb2, ulong B_nb3, 132 ulong C_nb2, ulong C_nb3, 133 ulong s_off_bytes, 134 int head_dim, int n_head, int n_group, int n_tokens 135 ) { 136 const int d_state = 256; 137 138 const int tid = (int) get_local_id(0); 139 const int wg_x = (int) get_group_id(0); 140 const int seq_id = (int) get_group_id(1); 141 142 const int head_id = wg_x / head_dim; 143 const int dim_id = wg_x - head_id * head_dim; 144 const int g = head_id / (n_head / n_group); 145 146 src0_base += src0_off; 147 src1_base += src1_off; 148 src2_base += src2_off; 149 src3_base += src3_off; 150 src4_base += src4_off; 151 src5_base += src5_off; 152 src6_base += src6_off; 153 dst_base += dst_off; 154 155 const int seq_slot = ((global const int *) src6_base)[seq_id]; 156 157 const ulong state_base_off = (ulong)seq_slot * s0_nb3 + (ulong)head_id * s0_nb2 158 + (ulong)dim_id * d_state * sizeof(float); 159 global const float * s0_warp = (global const float *)(src0_base + state_base_off); 160 const ulong state_out_off = (ulong)seq_id * s0_nb3 + (ulong)head_id * s0_nb2 161 + (ulong)dim_id * d_state * sizeof(float); 162 global float * s_warp = (global float *)(dst_base + s_off_bytes + state_out_off); 163 164 global const char * x_seq = src1_base + (ulong)seq_id * x_nb3; 165 global const char * dt_seq = src2_base + (ulong)seq_id * dt_nb2; 166 global const char * B_seq = src4_base + (ulong)seq_id * B_nb3 + (ulong)g * d_state * sizeof(float); 167 global const char * C_seq = src5_base + (ulong)seq_id * C_nb3 + (ulong)g * d_state * sizeof(float); 168 169 const ulong y_dim_total = (ulong)n_head * head_dim; 170 global float * y_seq = (global float *)dst_base 171 + (ulong)seq_id * (ulong)n_tokens * y_dim_total; 172 173 const float A_val = ((global const float *)src3_base)[(ulong)head_id * A_nb1 / sizeof(float)]; 174 175 // c_factor = 4: each thread owns 4 state elements. 176 float state0 = s0_warp[tid]; 177 float state1 = s0_warp[tid + 64]; 178 float state2 = s0_warp[tid + 128]; 179 float state3 = s0_warp[tid + 192]; 180 181 for (int t = 0; t < n_tokens; ++t) { 182 const float dt_h = ((global const float *)(dt_seq + (ulong)t * dt_nb1))[head_id]; 183 const float dt_softplus = softplus_f32(dt_h); 184 const float dA = exp(dt_softplus * A_val); 185 const float x_val = ((global const float *)(x_seq + (ulong)t * x_nb2))[(ulong)head_id * head_dim + dim_id]; 186 const float x_dt = x_val * dt_softplus; 187 188 global const float * B_t = (global const float *)(B_seq + (ulong)t * B_nb2); 189 global const float * C_t = (global const float *)(C_seq + (ulong)t * C_nb2); 190 191 const float B0 = B_t[tid]; 192 const float B1 = B_t[tid + 64]; 193 const float B2 = B_t[tid + 128]; 194 const float B3 = B_t[tid + 192]; 195 const float C0 = C_t[tid]; 196 const float C1 = C_t[tid + 64]; 197 const float C2 = C_t[tid + 128]; 198 const float C3 = C_t[tid + 192]; 199 200 state0 = state0 * dA + B0 * x_dt; 201 state1 = state1 * dA + B1 * x_dt; 202 state2 = state2 * dA + B2 * x_dt; 203 state3 = state3 * dA + B3 * x_dt; 204 const float partial = state0 * C0 + state1 * C1 + state2 * C2 + state3 * C3; 205 206 const float sum = sub_group_reduce_add(partial); 207 if (tid == 0) { 208 y_seq[(ulong)t * y_dim_total + (ulong)head_id * head_dim + dim_id] = sum; 209 } 210 } 211 212 s_warp[tid] = state0; 213 s_warp[tid + 64] = state1; 214 s_warp[tid + 128] = state2; 215 s_warp[tid + 192] = state3; 216 } 217 218 kernel void kernel_ssm_scan_f32( 219 global const char * s_buf, 220 ulong s_off, 221 global const char * x_buf, 222 ulong x_off, 223 global const char * dt_buf, 224 ulong dt_off, 225 global const char * A_buf, 226 ulong A_off, 227 global const char * B_buf, 228 ulong B_off, 229 global const char * C_buf, 230 ulong C_off, 231 global const char * ids_buf, 232 ulong ids_off, 233 global char * dst_buf, 234 ulong dst_off, 235 ulong s_nb2, 236 ulong s_nb3, 237 ulong x_nb2, 238 ulong x_nb3, 239 ulong dt_nb1, 240 ulong dt_nb2, 241 ulong A_nb1, 242 ulong B_nb2, 243 ulong B_nb3, 244 ulong C_nb2, 245 ulong C_nb3, 246 ulong state_off, 247 int head_dim, 248 int n_head, 249 int n_group, 250 int n_tokens, 251 ulong s_nb1, 252 ulong x_nb1, 253 ulong B_nb1, 254 ulong C_nb1, 255 uint A_ne0, 256 uint d_state, 257 uint n_seqs, 258 uint K, 259 local float * reduce 260 ) { 261 global const char * s_data = s_buf + s_off; 262 global const char * x_data = x_buf + x_off; 263 global const char * dt_data = dt_buf + dt_off; 264 global const char * A_data = A_buf + A_off; 265 global const char * B_data = B_buf + B_off; 266 global const char * C_data = C_buf + C_off; 267 global const int * ids_data = (global const int *) (ids_buf + ids_off); 268 global float * dst = (global float *) (dst_buf + dst_off); 269 const uint y_elems = state_off / sizeof(float); 270 271 const uint tid = get_local_id(0); 272 const uint inner_idx = get_group_id(0); 273 const uint seq_idx = get_group_id(1); 274 const uint head_idx = inner_idx / head_dim; 275 const uint dim_idx = inner_idx - head_idx * head_dim; 276 const uint group_idx = head_idx / (n_head / n_group); 277 const uint state_slot = (uint) ids_data[seq_idx]; 278 279 const ulong s_idx = (ulong) state_slot * s_nb3 + 280 (ulong) head_idx * s_nb2 + 281 (ulong) dim_idx * s_nb1 + 282 (ulong) tid * sizeof(float); 283 float state = *((global const float *) (s_data + s_idx)); 284 285 const ulong A_idx = (ulong) head_idx * A_nb1 + 286 (ulong) (tid % A_ne0) * sizeof(float); 287 const float A_value = *((global const float *) (A_data + A_idx)); 288 289 for (int token_idx = 0; token_idx < n_tokens; ++token_idx) { 290 const ulong x_idx = (ulong) head_idx * x_nb1 + 291 (ulong) token_idx * x_nb2 + 292 (ulong) seq_idx * x_nb3 + 293 (ulong) dim_idx * sizeof(float); 294 const ulong dt_idx = (ulong) token_idx * dt_nb1 + 295 (ulong) seq_idx * dt_nb2 + 296 (ulong) head_idx * sizeof(float); 297 const ulong B_idx = (ulong) group_idx * B_nb1 + 298 (ulong) token_idx * B_nb2 + 299 (ulong) seq_idx * B_nb3 + 300 (ulong) tid * sizeof(float); 301 const ulong C_idx = (ulong) group_idx * C_nb1 + 302 (ulong) token_idx * C_nb2 + 303 (ulong) seq_idx * C_nb3 + 304 (ulong) tid * sizeof(float); 305 306 const float x_value = *((global const float *) (x_data + x_idx)); 307 const float dt_value = *((global const float *) (dt_data + dt_idx)); 308 const float B_value = *((global const float *) (B_data + B_idx)); 309 const float C_value = *((global const float *) (C_data + C_idx)); 310 const float dt_soft_plus = dt_value > 20.0f ? dt_value : log(1.0f + exp(dt_value)); 311 const float dA = exp(dt_soft_plus * A_value); 312 const float x_dt = x_value * dt_soft_plus; 313 314 state = mad(state, dA, B_value * x_dt); 315 reduce[tid] = state * C_value; 316 barrier(CLK_LOCAL_MEM_FENCE); 317 318 for (uint stride = d_state / 2; stride > 0; stride >>= 1) { 319 if (tid < stride) { 320 reduce[tid] += reduce[tid + stride]; 321 } 322 barrier(CLK_LOCAL_MEM_FENCE); 323 } 324 325 if (tid == 0) { 326 const uint y_idx = dim_idx + head_idx * head_dim + 327 token_idx * n_head * head_dim + 328 seq_idx * n_tokens * n_head * head_dim; 329 dst[y_idx] = reduce[0]; 330 } 331 332 const uint snapshot_slot = n_tokens - 1 - token_idx; 333 if (snapshot_slot > 0 && snapshot_slot < K) { 334 const uint snapshot_idx = y_elems + tid + dim_idx * d_state + 335 head_idx * d_state * head_dim + 336 (snapshot_slot * n_seqs + seq_idx) * d_state * head_dim * n_head; 337 dst[snapshot_idx] = state; 338 } 339 barrier(CLK_LOCAL_MEM_FENCE); 340 } 341 342 const uint state_idx = y_elems + tid + dim_idx * d_state + 343 head_idx * d_state * head_dim + 344 seq_idx * d_state * head_dim * n_head; 345 dst[state_idx] = state; 346 }