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_scan.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_scan.cpp` 170 lines ยท 8246 bytes Raw Blame History 1 #include "ssm_scan.hpp" 2 #include "common.hpp" 3 4 template 5 static void ssm_scan_f32_group( 6 const float * __restrict__ src0, const float * __restrict__ src1, const float * __restrict__ src2, 7 const float * __restrict__ src3, const float * __restrict__ src4, const float * __restrict__ src5, 8 const int32_t * __restrict__ src6, float * __restrict__ dst, 9 const int src0_nb2, const int src0_nb3, const int src1_nb2, const int src1_nb3, 10 const int src2_nb1, const int src2_nb2, const int src3_nb1, 11 const int src4_nb2, const int src4_nb3, const int src5_nb2, const int src5_nb3, 12 const int64_t s_off, const int64_t n_head, const int64_t d_head, const int64_t n_group, const int64_t n_tok, 13 const int64_t K, 14 const sycl::nd_item<2> & item) { 15 16 const int lane = item.get_local_id(1) % WARP_SIZE; 17 const int warp = item.get_local_id(1) / WARP_SIZE; 18 const int warp_idx = item.get_group(1) * c_factor + warp; 19 const int seq_idx = item.get_group(0); 20 21 const int head_idx = warp_idx / d_head; 22 const int head_off = (warp_idx % d_head) * sizeof(float); 23 const int group_off = (head_idx / (n_head / n_group)) * d_state * sizeof(float); 24 25 const float * s0_warp = (const float *) ((const char *) src0 + src6[seq_idx] * src0_nb3 + head_idx * src0_nb2 + head_off * d_state); 26 const float * x_warp = (const float *) ((const char *) src1 + (seq_idx * src1_nb3) + (warp_idx * sizeof(float))); 27 const float * dt_warp = (const float *) ((const char *) src2 + (seq_idx * src2_nb2) + head_idx * sizeof(float)); 28 const float * A_warp = (const float *) ((const char *) src3 + head_idx * src3_nb1); 29 const float * B_warp = (const float *) ((const char *) src4 + (seq_idx * src4_nb3) + (group_off)); 30 const float * C_warp = (const float *) ((const char *) src5 + (seq_idx * src5_nb3) + (group_off)); 31 float * y_warp = dst + (seq_idx * n_tok * n_head * d_head) + warp_idx; 32 float * s_warp = (float *) ((char *) dst + s_off + seq_idx * src0_nb3 + head_idx * src0_nb2 + head_off * d_state); 33 34 const int stride_x = src1_nb2 / sizeof(float); 35 const int stride_dt = src2_nb1 / sizeof(float); 36 const int stride_B = src4_nb2 / sizeof(float); 37 const int stride_C = src5_nb2 / sizeof(float); 38 const int stride_y = n_head * d_head; 39 40 float state[c_factor]; 41 float state_sum = 0.0f; 42 43 #pragma unroll 44 for (int j = 0; j < c_factor; j++) { 45 state[j] = s0_warp[WARP_SIZE * j + lane]; 46 } 47 48 for (int64_t i = 0; i < n_tok; i++) { 49 const float dt_val = dt_warp[i * stride_dt]; 50 const float dt_soft_plus = (dt_val <= 20.0f ? sycl::log1p(sycl::exp(dt_val)) : dt_val); 51 52 state_sum = 0.0f; 53 const float dA = sycl::exp(dt_soft_plus * A_warp[0]); 54 const float x_dt = x_warp[i * stride_x] * dt_soft_plus; 55 #pragma unroll 56 for (int j = 0; j < c_factor; j++) { 57 const float B_val = B_warp[i * stride_B + WARP_SIZE * j + lane]; 58 const float C_val = C_warp[i * stride_C + WARP_SIZE * j + lane]; 59 state[j] = (state[j] * dA) + (B_val * x_dt); 60 state_sum += state[j] * C_val; 61 } 62 63 state_sum = warp_reduce_sum(state_sum); 64 65 if (lane == 0) { 66 y_warp[i * stride_y] = state_sum; 67 } 68 69 const int64_t slot = n_tok - 1 - i; 70 if (K > 1 && slot > 0 && slot < K) { 71 float * s_snapshot_warp = (float *) ((char *) dst + s_off + (slot * item.get_group_range(0) + seq_idx) * src0_nb3 + head_idx * src0_nb2 + head_off * d_state); 72 #pragma unroll 73 for (int j = 0; j < c_factor; j++) { 74 s_snapshot_warp[WARP_SIZE * j + lane] = state[j]; 75 } 76 } 77 } 78 79 #pragma unroll 80 for (int j = 0; j < c_factor; j++) { 81 s_warp[WARP_SIZE * j + lane] = state[j]; 82 } 83 } 84 85 static void ssm_scan_f32_sycl( 86 const float * src0, const float * src1, const float * src2, const float * src3, 87 const float * src4, const float * src5, const int32_t * src6, float * dst, 88 const int src0_nb2, const int src0_nb3, const int src1_nb2, const int src1_nb3, const int src2_nb1, 89 const int src2_nb2, const int src3_nb1, const int src4_nb2, const int src4_nb3, const int src5_nb2, 90 const int src5_nb3, const int64_t s_off, const int64_t d_state, const int64_t head_dim, 91 const int64_t n_head, const int64_t n_group, const int64_t n_tok, const int64_t n_seq, 92 const int64_t K, 93 dpct::queue_ptr stream) { 94 95 // NOTE: if you change conditions here, be sure to update the corresponding supports_op condition! 96 GGML_ASSERT(src3_nb1 == sizeof(float)); 97 if (d_state == 128) { 98 constexpr int threads = 128; 99 constexpr int num_warps = threads / WARP_SIZE; 100 const sycl::range<2> grid(n_seq, (n_head * head_dim + num_warps - 1) / num_warps); 101 const sycl::range<2> block(1, threads); 102 stream->parallel_for( 103 sycl::nd_range<2>(grid * block, block), 104 [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { 105 ssm_scan_f32_group<128 / WARP_SIZE, 128>( 106 src0, src1, src2, src3, src4, src5, src6, dst, 107 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2, src3_nb1, 108 src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, head_dim, n_group, n_tok, K, item); 109 }); 110 } else if (d_state == 256) { 111 constexpr int threads = 256; 112 constexpr int num_warps = threads / WARP_SIZE; 113 const sycl::range<2> grid(n_seq, (n_head * head_dim + num_warps - 1) / num_warps); 114 const sycl::range<2> block(1, threads); 115 stream->parallel_for( 116 sycl::nd_range<2>(grid * block, block), 117 [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { 118 ssm_scan_f32_group<256 / WARP_SIZE, 256>( 119 src0, src1, src2, src3, src4, src5, src6, dst, 120 src0_nb2, src0_nb3, src1_nb2, src1_nb3, src2_nb1, src2_nb2, src3_nb1, 121 src4_nb2, src4_nb3, src5_nb2, src5_nb3, s_off, n_head, head_dim, n_group, n_tok, K, item); 122 }); 123 } else { 124 GGML_ABORT("ssm_scan: unsupported d_state (must be 128 or 256)"); 125 } 126 } 127 128 inline void ggml_sycl_op_ssm_scan(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 129 const ggml_tensor * src0 = dst->src[0]; 130 const ggml_tensor * src1 = dst->src[1]; 131 const ggml_tensor * src2 = dst->src[2]; 132 const ggml_tensor * src3 = dst->src[3]; 133 const ggml_tensor * src4 = dst->src[4]; 134 const ggml_tensor * src5 = dst->src[5]; 135 const ggml_tensor * src6 = dst->src[6]; 136 137 GGML_ASSERT(src0->type == GGML_TYPE_F32); 138 GGML_ASSERT(src6->type == GGML_TYPE_I32); 139 GGML_ASSERT(dst->type == GGML_TYPE_F32); 140 141 const int64_t nc = src0->ne[0]; 142 const int64_t nr = src0->ne[1]; 143 const int64_t nh = src1->ne[1]; 144 const int64_t ng = src4->ne[1]; 145 const int64_t n_t = src1->ne[2]; 146 const int64_t n_s = src1->ne[3]; 147 const int64_t K = ggml_get_op_params_i32(dst, 0); 148 const int64_t s_off = ggml_nelements(src1) * sizeof(float); 149 150 GGML_ASSERT(K >= 1); 151 GGML_ASSERT(ggml_nelements(src1) + K * nc * nr * nh * n_s == ggml_nelements(dst)); 152 GGML_ASSERT(src3->ne[0] == 1 || K == 1); 153 154 dpct::queue_ptr stream = ctx.stream(); 155 SYCL_CHECK(ggml_sycl_set_device(ctx.device)); 156 157 ssm_scan_f32_sycl( 158 static_cast(src0->data), static_cast(src1->data), 159 static_cast(src2->data), static_cast(src3->data), 160 static_cast(src4->data), static_cast(src5->data), 161 static_cast(src6->data), static_cast(dst->data), 162 src0->nb[2], src0->nb[3], src1->nb[2], src1->nb[3], src2->nb[1], src2->nb[2], 163 src3->nb[1], src4->nb[2], src4->nb[3], src5->nb[2], src5->nb[3], 164 s_off, nc, nr, nh, ng, n_t, n_s, K, stream); 165 } 166 167 void ggml_sycl_ssm_scan(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 168 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/7); 169 ggml_sycl_op_ssm_scan(ctx, dst); 170 }