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-sycl/ssm_conv.cpp # 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-sycl / ssm_conv.cpp` 361 lines ยท 13963 bytes Raw Blame History 1 #include "ssm_conv.hpp" 2 #include "common.hpp" 3 #include "element_wise.hpp" 4 5 #include 6 7 using namespace sycl; 8 9 // One output element of the conv. DC is d_conv as a compile-time constant (0 keeps the 10 // runtime loop); unfused callers pass literal false/nullptr so the epilogue folds away. 11 template 12 static __dpct_inline__ void ssm_conv_element( 13 size_t idx, 14 const float *src_data, 15 const float *weights, 16 float *dst_data, 17 int d_conv, 18 int d_inner, 19 int n_t, 20 int src_stride_inner, 21 int src_stride_seq, 22 int dst_stride_token, 23 int dst_stride_seq, 24 bool apply_silu, 25 const float *bias 26 ) { 27 // src is token-contiguous per channel, dst is channel-contiguous per token, 28 // so indexing token-fastest coalesces the d_conv loads. 29 const int token = static_cast(idx % n_t); 30 const int channel = static_cast((idx / n_t) % d_inner); 31 const int seq = static_cast(idx / (static_cast(n_t) * static_cast(d_inner))); 32 33 const float *s = src_data 34 + static_cast(seq) * static_cast(src_stride_seq) 35 + static_cast(channel) * static_cast(src_stride_inner) 36 + static_cast(token); 37 38 const float *c = weights + static_cast(channel) * static_cast(d_conv); 39 40 float sumf = 0.0f; 41 if constexpr (DC > 0) { 42 #pragma unroll 43 for (int i0 = 0; i0 < DC; ++i0) { 44 sumf += s[i0] * c[i0]; 45 } 46 } else { 47 for (int i0 = 0; i0 < d_conv; ++i0) { 48 sumf += s[i0] * c[i0]; 49 } 50 } 51 52 // fused bias add: the ADD node broadcasts a 1-D channel bias over tokens 53 if (bias != nullptr) { 54 sumf += bias[channel]; 55 } 56 57 const size_t dst_idx = 58 static_cast(seq) * static_cast(dst_stride_seq) + 59 static_cast(token) * static_cast(dst_stride_token) + 60 static_cast(channel); 61 62 dst_data[dst_idx] = apply_silu ? op_silu(sumf) : sumf; 63 } 64 65 // FUSED=false keeps apply_silu/bias out of the kernel capture list, so the unfused launch 66 // takes the pre-fusion argument list; matters at n_t == 1, where the op is launch-bound. 67 template 68 static void kernel_ssm_conv_impl( 69 queue &q, 70 const float *src_data, 71 const float *weights, 72 float *dst_data, 73 int d_conv, 74 int d_inner, 75 int n_t, 76 int n_s, 77 int ncs __attribute__((unused)), 78 int src_stride_inner, 79 int src_stride_seq, 80 int dst_stride_token, 81 int dst_stride_seq, 82 bool apply_silu, 83 const float *bias 84 ) { 85 const size_t total_work = static_cast(d_inner) * static_cast(n_t) * static_cast(n_s); 86 const size_t work_group_size = 256; 87 const size_t num_work_groups = (total_work + work_group_size - 1) / work_group_size; 88 89 const range<1> global_range(num_work_groups * work_group_size); 90 const range<1> local_range(work_group_size); 91 92 if constexpr (FUSED) { 93 q.submit([&](handler &h) { 94 h.parallel_for( 95 nd_range<1>(global_range, local_range), 96 [=](nd_item<1> item) { 97 const size_t idx = item.get_global_id(0); 98 if (idx >= total_work) { 99 return; 100 } 101 102 ssm_conv_element(idx, src_data, weights, dst_data, d_conv, d_inner, n_t, 103 src_stride_inner, src_stride_seq, dst_stride_token, 104 dst_stride_seq, apply_silu, bias); 105 } 106 ); 107 }); 108 } else { 109 GGML_UNUSED(apply_silu); 110 GGML_UNUSED(bias); 111 112 q.submit([&](handler &h) { 113 h.parallel_for( 114 nd_range<1>(global_range, local_range), 115 [=](nd_item<1> item) { 116 const size_t idx = item.get_global_id(0); 117 if (idx >= total_work) { 118 return; 119 } 120 121 ssm_conv_element(idx, src_data, weights, dst_data, d_conv, d_inner, n_t, 122 src_stride_inner, src_stride_seq, dst_stride_token, 123 dst_stride_seq, false, nullptr); 124 } 125 ); 126 }); 127 } 128 } 129 130 // SLM transpose tile: coalesces both the loads and the stores. The +1 pad makes the row 131 // stride 33, coprime with 32 banks, so both phases are bank-conflict-free. 132 template 133 static __dpct_inline__ void ssm_conv_tile( 134 nd_item<1> it, local_accessor tile, const float *src_data, const float *weights, 135 float *dst_data, int n_t, int nt_tiles, int nc_tiles, int src_stride_inner, 136 int src_stride_seq, int dst_stride_token, int dst_stride_seq, bool apply_silu, 137 const float *bias 138 ) { 139 const int lid = static_cast(it.get_local_id(0)); 140 const size_t g = it.get_group(0); 141 const int tt = static_cast(g % nt_tiles); 142 const int ct = static_cast((g / nt_tiles) % nc_tiles); 143 const int seq = static_cast(g / (static_cast(nt_tiles) * nc_tiles)); 144 const int t0 = tt * TT, c0 = ct * TC; 145 146 const int ti = lid % TT; 147 const int cj = lid / TT; 148 #pragma unroll 149 for (int r = 0; r < TC / (WG / TT); ++r) { 150 const int c = cj + r * (WG / TT); 151 const int tok = t0 + ti; 152 float sumf = 0.0f; 153 if (tok < n_t) { 154 const float *s = src_data + static_cast(seq) * src_stride_seq 155 + static_cast(c0 + c) * src_stride_inner + tok; 156 const float *cw = weights + static_cast(c0 + c) * DC; 157 #pragma unroll 158 for (int i = 0; i < DC; ++i) sumf += s[i] * cw[i]; 159 if (bias != nullptr) sumf += bias[c0 + c]; 160 if (apply_silu) sumf = op_silu(sumf); 161 } 162 tile[c * (TT + 1) + ti] = sumf; 163 } 164 it.barrier(access::fence_space::local_space); 165 166 const int cc = lid % TC; 167 const int tj = lid / TC; 168 #pragma unroll 169 for (int r = 0; r < TT / (WG / TC); ++r) { 170 const int t = tj + r * (WG / TC); 171 const int tok = t0 + t; 172 if (tok < n_t) { 173 dst_data[static_cast(seq) * dst_stride_seq 174 + static_cast(tok) * dst_stride_token + c0 + cc] 175 = tile[cc * (TT + 1) + t]; 176 } 177 } 178 } 179 180 // Same FUSED split as kernel_ssm_conv_impl. The fused instantiation keeps the runtime 181 // apply_silu/bias branches: at n_t >= 32 they are amortized over the whole tile. 182 template 183 static void kernel_ssm_conv_tiled( 184 queue &q, const float *src_data, const float *weights, float *dst_data, 185 int d_inner, int n_t, int n_s, int src_stride_inner, int src_stride_seq, 186 int dst_stride_token, int dst_stride_seq, bool apply_silu, const float *bias 187 ) { 188 constexpr int TT = 32, TC = 32, WG = 256; 189 const int nt_tiles = (n_t + TT - 1) / TT; 190 const int nc_tiles = d_inner / TC; 191 const size_t groups = static_cast(nt_tiles) * nc_tiles * n_s; 192 193 if constexpr (FUSED) { 194 q.submit([&](handler &h) { 195 local_accessor tile(range<1>(TC * (TT + 1)), h); 196 h.parallel_for(nd_range<1>(range<1>(groups * WG), range<1>(WG)), [=](nd_item<1> it) { 197 ssm_conv_tile(it, tile, src_data, weights, dst_data, n_t, nt_tiles, 198 nc_tiles, src_stride_inner, src_stride_seq, 199 dst_stride_token, dst_stride_seq, apply_silu, bias); 200 }); 201 }); 202 } else { 203 GGML_UNUSED(apply_silu); 204 GGML_UNUSED(bias); 205 206 q.submit([&](handler &h) { 207 local_accessor tile(range<1>(TC * (TT + 1)), h); 208 h.parallel_for(nd_range<1>(range<1>(groups * WG), range<1>(WG)), [=](nd_item<1> it) { 209 ssm_conv_tile(it, tile, src_data, weights, dst_data, n_t, nt_tiles, 210 nc_tiles, src_stride_inner, src_stride_seq, 211 dst_stride_token, dst_stride_seq, false, nullptr); 212 }); 213 }); 214 } 215 } 216 217 static void kernel_ssm_conv( 218 queue &q, 219 const float *src_data, 220 const float *weights, 221 float *dst_data, 222 int d_conv, 223 int d_inner, 224 int n_t, 225 int n_s, 226 int ncs, 227 int src_stride_inner, 228 int src_stride_seq, 229 int dst_stride_token, 230 int dst_stride_seq, 231 bool apply_silu, 232 const float *bias 233 ) { 234 // Only the fused instantiations carry apply_silu/bias as kernel arguments; the plain 235 // ssm_conv launch keeps the argument list it had before the fusion landed. 236 const bool fused = apply_silu || bias != nullptr; 237 238 // d_inner must be a multiple of 32 so the channel tiles are exact; the transpose is only 239 // worth it for n_t >= 32. d_conv == 4 is the only window with a DC-specialized kernel. 240 if (d_conv == 4 && n_t >= 32 && (d_inner % 32) == 0) { 241 if (fused) { 242 kernel_ssm_conv_tiled<4, true>(q, src_data, weights, dst_data, d_inner, n_t, n_s, 243 src_stride_inner, src_stride_seq, dst_stride_token, 244 dst_stride_seq, apply_silu, bias); 245 } else { 246 kernel_ssm_conv_tiled<4, false>(q, src_data, weights, dst_data, d_inner, n_t, n_s, 247 src_stride_inner, src_stride_seq, dst_stride_token, 248 dst_stride_seq, apply_silu, bias); 249 } 250 return; 251 } 252 253 if (d_conv == 4) { 254 if (fused) { 255 kernel_ssm_conv_impl<4, true>(q, src_data, weights, dst_data, d_conv, d_inner, n_t, n_s, 256 ncs, src_stride_inner, src_stride_seq, dst_stride_token, 257 dst_stride_seq, apply_silu, bias); 258 } else { 259 kernel_ssm_conv_impl<4, false>(q, src_data, weights, dst_data, d_conv, d_inner, n_t, n_s, 260 ncs, src_stride_inner, src_stride_seq, dst_stride_token, 261 dst_stride_seq, apply_silu, bias); 262 } 263 return; 264 } 265 266 if (fused) { 267 kernel_ssm_conv_impl<0, true>(q, src_data, weights, dst_data, d_conv, d_inner, n_t, n_s, 268 ncs, src_stride_inner, src_stride_seq, dst_stride_token, 269 dst_stride_seq, apply_silu, bias); 270 } else { 271 kernel_ssm_conv_impl<0, false>(q, src_data, weights, dst_data, d_conv, d_inner, n_t, n_s, 272 ncs, src_stride_inner, src_stride_seq, dst_stride_token, 273 dst_stride_seq, apply_silu, bias); 274 } 275 } 276 277 inline void ggml_sycl_op_ssm_conv(ggml_backend_sycl_context & ctx, ggml_tensor * dst, ggml_tensor * silu_dst = nullptr, const float * bias = nullptr) { 278 ggml_tensor * src0 = dst->src[0]; 279 ggml_tensor * src1 = dst->src[1]; 280 281 GGML_ASSERT(src0->type == GGML_TYPE_F32); 282 GGML_ASSERT(src1->type == GGML_TYPE_F32); 283 GGML_ASSERT(dst->type == GGML_TYPE_F32); 284 GGML_ASSERT(bias == nullptr || silu_dst != nullptr); 285 286 const int d_conv = src1->ne[0]; 287 const int ncs = src0->ne[0]; 288 const int d_inner = src0->ne[1]; 289 const int n_t = dst->ne[1]; 290 const int n_s = dst->ne[2]; 291 292 GGML_ASSERT(src0->ne[0] == d_conv - 1 + n_t); 293 GGML_ASSERT(src0->ne[1] == d_inner); 294 GGML_ASSERT(src1->ne[1] == d_inner); 295 296 GGML_ASSERT(dst->ne[0] == d_inner); 297 GGML_ASSERT(dst->ne[1] == n_t); 298 GGML_ASSERT(dst->ne[2] == n_s); 299 300 GGML_ASSERT(src0->nb[0] == sizeof(float)); 301 GGML_ASSERT(src1->nb[0] == sizeof(float)); 302 303 GGML_ASSERT(src0->nb[1] == src0->ne[0] * sizeof(float)); 304 305 const int src_stride_inner = ncs; 306 const int src_stride_seq = ncs * d_inner; 307 const int dst_stride_token = d_inner; 308 const int dst_stride_seq = d_inner * n_t; 309 310 try { 311 queue *q = ctx.stream(); 312 313 const float *src_data = static_cast(src0->data); 314 const float *weights = static_cast(src1->data); 315 const bool apply_silu = silu_dst != nullptr; 316 float *dst_data = static_cast((silu_dst ? silu_dst : dst)->data); 317 318 GGML_ASSERT(src_data && weights && dst_data); 319 320 kernel_ssm_conv( 321 *q, 322 src_data, 323 weights, 324 dst_data, 325 d_conv, 326 d_inner, 327 n_t, 328 n_s, 329 ncs, 330 src_stride_inner, 331 src_stride_seq, 332 dst_stride_token, 333 dst_stride_seq, 334 apply_silu, 335 bias 336 ); 337 338 } catch (const std::exception &e) { 339 std::fprintf(stderr, "[SYCL-SSM_CONV] ERROR: %s\n", e.what()); 340 throw; 341 } 342 } 343 344 void ggml_sycl_ssm_conv(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 345 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); 346 ggml_sycl_op_ssm_conv(ctx, dst); 347 } 348 349 // Fused ssm_conv + ADD + SiLU: write silu(conv(x) + b) straight into silu_dst, eliding the 350 // standalone SiLU launch and its HBM round-trip of the conv output. 351 void ggml_sycl_ssm_conv_fused(ggml_backend_sycl_context & ctx, ggml_tensor * dst, ggml_tensor * add, ggml_tensor * silu_dst) { 352 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); 353 GGML_ASSERT(silu_dst && ggml_are_same_shape(dst, silu_dst) && silu_dst->type == GGML_TYPE_F32); 354 // the fused kernel reads only the ADD's bias operand; the ADD result is never written 355 const float * bias = nullptr; 356 if (add != nullptr) { 357 const ggml_tensor * bias_t = (add->src[0] == dst) ? add->src[1] : add->src[0]; 358 bias = static_cast(bias_t->data); 359 } 360 ggml_sycl_op_ssm_conv(ctx, dst, silu_dst, bias); 361 }