ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / fattn-sparse.cpp
| 1 | #include "fattn.hpp" |
| 2 | #include "fattn-sparse.hpp" |
| 3 | |
| 4 | #include <cstdint> |
| 5 | #include <cstdio> |
| 6 | #include <cstring> |
| 7 | |
| 8 | static constexpr int64_t SPARSE_FA_PAD = 256; |
| 9 | static constexpr int64_t SPARSE_FA_MIN_RATIO = 2; |
| 10 | |
| 11 | extern int g_ggml_sycl_enable_sparse_fa; |
| 12 | extern int g_ggml_sycl_debug_sparse_fa; |
| 13 | extern int g_ggml_sycl_sparse_fa_margin; |
| 14 | |
| 15 | static int sparse_fa_enabled(void) { |
| 16 | return g_ggml_sycl_enable_sparse_fa; |
| 17 | } |
| 18 | |
| 19 | static int sparse_fa_debug(void) { |
| 20 | return g_ggml_sycl_debug_sparse_fa; |
| 21 | } |
| 22 | |
| 23 | // slack above n_kv_max; callers may exceed the hint by a few always-attended positions |
| 24 | static int sparse_fa_margin(void) { |
| 25 | return g_ggml_sycl_sparse_fa_margin; |
| 26 | } |
| 27 | |
| 28 | // Unordered output is fine: softmax over the selected set is permutation invariant. |
| 29 | static void sparse_fa_compact_mask(sycl::queue * stream, |
| 30 | const sycl::half * __restrict__ mask, |
| 31 | int32_t * __restrict__ indices, |
| 32 | int32_t * __restrict__ count, |
| 33 | const int64_t n_kv, |
| 34 | const int64_t n_kv_g) { |
| 35 | constexpr size_t WG = 256; |
| 36 | const size_t global = (size_t) GGML_PAD(n_kv, (int64_t) WG); |
| 37 | |
| 38 | stream->parallel_for( |
| 39 | sycl::nd_range<1>(sycl::range<1>(global), sycl::range<1>(WG)), |
| 40 | [=](sycl::nd_item<1> item) { |
| 41 | const int64_t i = (int64_t) item.get_global_id(0); |
| 42 | if (i >= n_kv || !sycl::isfinite((float) mask[i])) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | sycl::atomic_ref<int32_t, |
| 47 | sycl::memory_order::relaxed, |
| 48 | sycl::memory_scope::device, |
| 49 | sycl::access::address_space::global_space> ctr(*count); |
| 50 | |
| 51 | const int32_t pos = ctr.fetch_add(1); |
| 52 | if (pos < (int32_t) n_kv_g) { |
| 53 | indices[pos] = (int32_t) i; |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | // Rows along ne[0] are contiguous for every type used as a KV cache, so this is |
| 59 | // a plain byte copy and needs no per-type code. Padding slots are zeroed. |
| 60 | static void sparse_fa_gather_rows(sycl::queue * stream, |
| 61 | const uint8_t * __restrict__ src, |
| 62 | uint8_t * __restrict__ dst, |
| 63 | const int32_t * __restrict__ indices, |
| 64 | const int32_t * __restrict__ count, |
| 65 | const size_t row_size, |
| 66 | const size_t src_nb1, |
| 67 | const size_t src_nb2, |
| 68 | const int64_t n_kv_g, |
| 69 | const int64_t n_head) { |
| 70 | GGML_ASSERT(row_size % sizeof(uint32_t) == 0); |
| 71 | const size_t words = row_size / sizeof(uint32_t); |
| 72 | |
| 73 | stream->parallel_for( |
| 74 | sycl::range<3>((size_t) n_head, (size_t) n_kv_g, words), |
| 75 | [=](sycl::id<3> id) { |
| 76 | const int64_t h = (int64_t) id[0]; |
| 77 | const int64_t slot = (int64_t) id[1]; |
| 78 | const size_t w = id[2]; |
| 79 | |
| 80 | uint32_t * dst_row = |
| 81 | (uint32_t *) (dst + ((size_t) (h * n_kv_g + slot)) * row_size); |
| 82 | |
| 83 | if (slot >= (int64_t) *count) { |
| 84 | dst_row[w] = 0; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | const uint32_t * src_row = |
| 89 | (const uint32_t *) (src + (size_t) indices[slot] * src_nb1 + |
| 90 | (size_t) h * src_nb2); |
| 91 | dst_row[w] = src_row[w]; |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | static void sparse_fa_gather_mask(sycl::queue * stream, |
| 96 | const sycl::half * __restrict__ mask, |
| 97 | sycl::half * __restrict__ mask_g, |
| 98 | const int32_t * __restrict__ indices, |
| 99 | const int32_t * __restrict__ count, |
| 100 | const int64_t n_kv_g, |
| 101 | const int64_t n_rows, |
| 102 | const size_t mask_s1) { |
| 103 | stream->parallel_for( |
| 104 | sycl::range<2>((size_t) n_rows, (size_t) n_kv_g), |
| 105 | [=](sycl::id<2> id) { |
| 106 | const int64_t r = (int64_t) id[0]; |
| 107 | const int64_t slot = (int64_t) id[1]; |
| 108 | |
| 109 | sycl::half v = sycl::half(-INFINITY); |
| 110 | if (slot < (int64_t) *count) { |
| 111 | v = mask[(size_t) r * mask_s1 + (size_t) indices[slot]]; |
| 112 | } |
| 113 | mask_g[(size_t) r * n_kv_g + slot] = v; |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | static bool sparse_fa_applicable(const ggml_tensor * dst, int64_t & n_kv_g_out) { |
| 118 | const ggml_tensor * Q = dst->src[0]; |
| 119 | const ggml_tensor * K = dst->src[1]; |
| 120 | const ggml_tensor * V = dst->src[2]; |
| 121 | const ggml_tensor * mask = dst->src[3]; |
| 122 | |
| 123 | if (!Q || !K || !V || !mask) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | const int32_t n_kv_max = ggml_get_op_params_i32(dst, 4); |
| 128 | if (n_kv_max <= 0) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | float max_bias = 0.0f; |
| 133 | float logit_softcap = 0.0f; |
| 134 | memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); |
| 135 | memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float)); |
| 136 | if (max_bias != 0.0f || logit_softcap != 0.0f) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // single-token decode only; prefill amortises the scan already |
| 141 | if (Q->ne[1] != 1) { |
| 142 | return false; |
| 143 | } |
| 144 | if (K->ne[3] != 1 || V->ne[3] != 1 || mask->ne[2] != 1 || mask->ne[3] != 1) { |
| 145 | return false; |
| 146 | } |
| 147 | if (mask->type != GGML_TYPE_F16 || mask->ne[0] < K->ne[1]) { |
| 148 | return false; |
| 149 | } |
| 150 | if (K->ne[2] != V->ne[2]) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | // nb[1] may stride over heads (interleaved cache); only ne[0] must be contiguous |
| 155 | if (K->nb[0] != ggml_type_size(K->type) || V->nb[0] != ggml_type_size(V->type)) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | const size_t k_row = ggml_row_size(K->type, K->ne[0]); |
| 160 | const size_t v_row = ggml_row_size(V->type, V->ne[0]); |
| 161 | if (k_row % sizeof(uint32_t) || v_row % sizeof(uint32_t)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | const int64_t n_kv_g = GGML_PAD((int64_t) n_kv_max + sparse_fa_margin(), SPARSE_FA_PAD); |
| 166 | if (n_kv_g * SPARSE_FA_MIN_RATIO > K->ne[1]) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | n_kv_g_out = n_kv_g; |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool ggml_sycl_flash_attn_ext_sparse(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 175 | int64_t n_kv_g = 0; |
| 176 | if (!sparse_fa_enabled() || !sparse_fa_applicable(dst, n_kv_g)) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | ggml_tensor * K = dst->src[1]; |
| 181 | ggml_tensor * V = dst->src[2]; |
| 182 | ggml_tensor * mask = dst->src[3]; |
| 183 | |
| 184 | const int64_t n_kv = K->ne[1]; |
| 185 | const int64_t n_head_k = K->ne[2]; |
| 186 | const int64_t n_rows_m = mask->ne[1]; |
| 187 | |
| 188 | const size_t k_row = ggml_row_size(K->type, K->ne[0]); |
| 189 | const size_t v_row = ggml_row_size(V->type, V->ne[0]); |
| 190 | |
| 191 | dpct::queue_ptr stream = ctx.stream(); |
| 192 | |
| 193 | ggml_sycl_pool_alloc<int32_t> idx_alloc(ctx.pool(), (size_t) n_kv_g); |
| 194 | ggml_sycl_pool_alloc<int32_t> cnt_alloc(ctx.pool(), 1); |
| 195 | ggml_sycl_pool_alloc<uint8_t> k_alloc(ctx.pool(), (size_t) n_head_k * n_kv_g * k_row); |
| 196 | ggml_sycl_pool_alloc<uint8_t> v_alloc(ctx.pool(), (size_t) n_head_k * n_kv_g * v_row); |
| 197 | ggml_sycl_pool_alloc<sycl::half> m_alloc(ctx.pool(), (size_t) n_rows_m * n_kv_g); |
| 198 | |
| 199 | int32_t * d_idx = idx_alloc.get(); |
| 200 | int32_t * d_cnt = cnt_alloc.get(); |
| 201 | uint8_t * d_K = k_alloc.get(); |
| 202 | uint8_t * d_V = v_alloc.get(); |
| 203 | sycl::half * d_mask = m_alloc.get(); |
| 204 | |
| 205 | // reset with a kernel: on Level Zero a queue memset is not always ordered before the next kernel |
| 206 | stream->single_task([=]() { *d_cnt = 0; }); |
| 207 | |
| 208 | sparse_fa_compact_mask(stream, (const sycl::half *) mask->data, |
| 209 | d_idx, d_cnt, n_kv, n_kv_g); |
| 210 | |
| 211 | sparse_fa_gather_rows(stream, (const uint8_t *) K->data, d_K, d_idx, d_cnt, |
| 212 | k_row, K->nb[1], K->nb[2], n_kv_g, n_head_k); |
| 213 | |
| 214 | sparse_fa_gather_rows(stream, (const uint8_t *) V->data, d_V, d_idx, d_cnt, |
| 215 | v_row, V->nb[1], V->nb[2], n_kv_g, n_head_k); |
| 216 | |
| 217 | sparse_fa_gather_mask(stream, (const sycl::half *) mask->data, d_mask, |
| 218 | d_idx, d_cnt, n_kv_g, n_rows_m, |
| 219 | mask->nb[1] / sizeof(sycl::half)); |
| 220 | |
| 221 | if (sparse_fa_debug() && stream->ext_oneapi_get_state() != sycl::ext::oneapi::experimental::queue_state::recording) { |
| 222 | int32_t h_cnt = 0; |
| 223 | SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(&h_cnt, d_cnt, sizeof(int32_t)))); |
| 224 | SYCL_CHECK(CHECK_TRY_ERROR(stream->wait())); |
| 225 | fprintf(stderr, "[FA-SPARSE] n_kv=%lld n_kv_max=%d n_kv_g=%lld finite=%d%s\n", |
| 226 | (long long) n_kv, ggml_get_op_params_i32(dst, 4), |
| 227 | (long long) n_kv_g, (int) h_cnt, |
| 228 | h_cnt > (int32_t) n_kv_g ? " OVERFLOW" : ""); |
| 229 | } |
| 230 | |
| 231 | // shallow copies retargeted at the gathered buffers; kernels are unchanged |
| 232 | ggml_tensor K_g = *K; |
| 233 | K_g.data = d_K; |
| 234 | K_g.ne[1] = n_kv_g; |
| 235 | K_g.nb[1] = k_row; |
| 236 | K_g.nb[2] = (size_t) n_kv_g * k_row; |
| 237 | K_g.nb[3] = (size_t) n_head_k * n_kv_g * k_row; |
| 238 | K_g.view_src = nullptr; |
| 239 | K_g.view_offs = 0; |
| 240 | |
| 241 | ggml_tensor V_g = *V; |
| 242 | V_g.data = d_V; |
| 243 | V_g.ne[1] = n_kv_g; |
| 244 | V_g.nb[1] = v_row; |
| 245 | V_g.nb[2] = (size_t) n_kv_g * v_row; |
| 246 | V_g.nb[3] = (size_t) V->ne[2] * n_kv_g * v_row; |
| 247 | V_g.view_src = nullptr; |
| 248 | V_g.view_offs = 0; |
| 249 | |
| 250 | ggml_tensor M_g = *mask; |
| 251 | M_g.data = d_mask; |
| 252 | M_g.ne[0] = n_kv_g; |
| 253 | M_g.nb[1] = (size_t) n_kv_g * sizeof(sycl::half); |
| 254 | M_g.nb[2] = M_g.nb[1] * mask->ne[1]; |
| 255 | M_g.nb[3] = M_g.nb[2]; |
| 256 | M_g.view_src = nullptr; |
| 257 | M_g.view_offs = 0; |
| 258 | |
| 259 | ggml_tensor dst_g = *dst; |
| 260 | dst_g.src[1] = &K_g; |
| 261 | dst_g.src[2] = &V_g; |
| 262 | dst_g.src[3] = &M_g; |
| 263 | dst_g.op_params[4] = 0; // avoid re-entering this path |
| 264 | |
| 265 | ggml_sycl_flash_attn_ext(ctx, &dst_g); |
| 266 | |
| 267 | return true; |
| 268 | } |