ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / fwht.cpp
| 1 | #include "fwht.hpp" |
| 2 | |
| 3 | #include <cmath> |
| 4 | #define P 1.0f |
| 5 | #define N -1.0f |
| 6 | |
| 7 | // constant Hadamard matrix via Paley I construction |
| 8 | static constexpr float H12[12][12] = { |
| 9 | { P, P, P, P, P, P, P, P, P, P, P, P }, |
| 10 | { P, N, P, N, P, P, P, N, N, N, P, N }, |
| 11 | { P, N, N, P, N, P, P, P, N, N, N, P }, |
| 12 | { P, P, N, N, P, N, P, P, P, N, N, N }, |
| 13 | { P, N, P, N, N, P, N, P, P, P, N, N }, |
| 14 | { P, N, N, P, N, N, P, N, P, P, P, N }, |
| 15 | { P, N, N, N, P, N, N, P, N, P, P, P }, |
| 16 | { P, P, N, N, N, P, N, N, P, N, P, P }, |
| 17 | { P, P, P, N, N, N, P, N, N, P, N, P }, |
| 18 | { P, P, P, P, N, N, N, P, N, N, P, N }, |
| 19 | { P, N, P, P, P, N, N, N, P, N, N, P }, |
| 20 | { P, P, N, P, P, P, N, N, N, P, N, N } |
| 21 | }; |
| 22 | |
| 23 | static constexpr float H20[20][20] = { |
| 24 | { P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P }, |
| 25 | { P, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N }, |
| 26 | { P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P }, |
| 27 | { P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P }, |
| 28 | { P, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N }, |
| 29 | { P, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N }, |
| 30 | { P, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N }, |
| 31 | { P, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N }, |
| 32 | { P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P }, |
| 33 | { P, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N }, |
| 34 | { P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P }, |
| 35 | { P, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N }, |
| 36 | { P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P }, |
| 37 | { P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P }, |
| 38 | { P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P }, |
| 39 | { P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P }, |
| 40 | { P, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N }, |
| 41 | { P, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N }, |
| 42 | { P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P }, |
| 43 | { P, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N } |
| 44 | }; |
| 45 | |
| 46 | #undef P |
| 47 | #undef N |
| 48 | |
| 49 | template <int N, typename T> |
| 50 | static void fwht_kernel(const T * __restrict__ src, float * __restrict__ dst, const int64_t n_rows, |
| 51 | const float scale, const sycl::nd_item<2> & item) { |
| 52 | const sycl::sub_group sg = item.get_sub_group(); |
| 53 | |
| 54 | const int64_t r = item.get_global_id(0); |
| 55 | if (r >= n_rows) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | src += r * N; |
| 60 | dst += r * N; |
| 61 | |
| 62 | constexpr int el_w = N / WARP_SIZE; |
| 63 | static_assert(el_w >= 1 && N % WARP_SIZE == 0, "row must be a whole number of sub-group widths"); |
| 64 | |
| 65 | float reg[el_w]; |
| 66 | const int lane = sg.get_local_linear_id(); |
| 67 | |
| 68 | #pragma unroll |
| 69 | for (int i = 0; i < el_w; ++i) { |
| 70 | reg[i] = static_cast<float>(src[i * WARP_SIZE + lane]) * scale; |
| 71 | } |
| 72 | |
| 73 | // Butterflies inside the sub-group. The partner of a lane with bit h clear is the |
| 74 | // lower index of the pair, so it takes the sum and the upper takes lower - upper. |
| 75 | #pragma unroll |
| 76 | for (int h = 1; h < WARP_SIZE; h *= 2) { |
| 77 | #pragma unroll |
| 78 | for (int j = 0; j < el_w; ++j) { |
| 79 | const float val = reg[j]; |
| 80 | const float val2 = dpct::permute_sub_group_by_xor(sg, val, h, WARP_SIZE); |
| 81 | |
| 82 | reg[j] = (lane & h) == 0 ? val + val2 : val2 - val; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Butterflies across registers: h is a multiple of WARP_SIZE, so the partner of |
| 87 | // element i*WARP_SIZE + lane lives in reg[i + h/WARP_SIZE] on the same lane. |
| 88 | #pragma unroll |
| 89 | for (int h = WARP_SIZE; h < N; h *= 2) { |
| 90 | const int step = h / WARP_SIZE; |
| 91 | #pragma unroll |
| 92 | for (int j = 0; j < el_w; j += 2 * step) { |
| 93 | #pragma unroll |
| 94 | for (int k = 0; k < step; ++k) { |
| 95 | const float x = reg[j + k]; |
| 96 | const float y = reg[j + k + step]; |
| 97 | |
| 98 | reg[j + k] = x + y; |
| 99 | reg[j + k + step] = x - y; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #pragma unroll |
| 105 | for (int i = 0; i < el_w; ++i) { |
| 106 | dst[i * WARP_SIZE + lane] = reg[i]; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | template <int N, typename T> |
| 111 | static void launch_fwht(const T * src, float * dst, const int64_t n_rows, const float scale, |
| 112 | dpct::queue_ptr stream) { |
| 113 | constexpr int rows_per_block = 4; |
| 114 | |
| 115 | const int64_t num_blocks = (n_rows + rows_per_block - 1) / rows_per_block; |
| 116 | |
| 117 | // dim 1 is the fastest-varying, so a sub-group is exactly one row's WARP_SIZE lanes. |
| 118 | const sycl::range<2> global(num_blocks * rows_per_block, WARP_SIZE); |
| 119 | const sycl::range<2> local(rows_per_block, WARP_SIZE); |
| 120 | |
| 121 | stream->parallel_for(sycl::nd_range<2>(global, local), |
| 122 | [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 123 | fwht_kernel<N, T>(src, dst, n_rows, scale, item); |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | // Wide blocks: one row per work-group instead of per sub-group, so each work-item |
| 128 | // keeps N/NT values rather than N/WARP_SIZE. Butterflies below the sub-group width |
| 129 | // still shuffle; those up to NT go through work-group local memory; the rest stay |
| 130 | // in registers. |
| 131 | template <int N, int NT, typename T> |
| 132 | static void fwht_kernel_wide(const T * __restrict__ src, |
| 133 | float * __restrict__ dst, |
| 134 | const int64_t n_rows, |
| 135 | const float scale, |
| 136 | const sycl::nd_item<2> & item, |
| 137 | float * smem) { |
| 138 | const int64_t r = item.get_global_id(0); |
| 139 | if (r >= n_rows) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | src += r * N; |
| 144 | dst += r * N; |
| 145 | |
| 146 | constexpr int el_w = N / NT; |
| 147 | static_assert(el_w >= 1 && N % NT == 0, "row must be a whole number of work-group widths"); |
| 148 | |
| 149 | const int tid = item.get_local_id(1); |
| 150 | |
| 151 | float reg[el_w]; |
| 152 | #pragma unroll |
| 153 | for (int i = 0; i < el_w; ++i) { |
| 154 | reg[i] = static_cast<float>(src[i * NT + tid]) * scale; |
| 155 | } |
| 156 | |
| 157 | const sycl::sub_group sg = item.get_sub_group(); |
| 158 | const int lane = sg.get_local_linear_id(); |
| 159 | |
| 160 | // Butterflies inside the sub-group, same pattern as the narrow kernel. |
| 161 | #pragma unroll |
| 162 | for (int h = 1; h < WARP_SIZE; h *= 2) { |
| 163 | #pragma unroll |
| 164 | for (int j = 0; j < el_w; ++j) { |
| 165 | const float val = reg[j]; |
| 166 | const float val2 = dpct::permute_sub_group_by_xor(sg, val, h, WARP_SIZE); |
| 167 | |
| 168 | reg[j] = (lane & h) == 0 ? val + val2 : val2 - val; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Butterflies from the sub-group width up to NT: the partner lane is outside |
| 173 | // this sub-group, so it goes through work-group local memory instead of a shuffle. |
| 174 | for (int h = WARP_SIZE; h < NT; h *= 2) { |
| 175 | #pragma unroll |
| 176 | for (int j = 0; j < el_w; ++j) { |
| 177 | smem[j * NT + tid] = reg[j]; |
| 178 | } |
| 179 | item.barrier(sycl::access::fence_space::local_space); |
| 180 | #pragma unroll |
| 181 | for (int j = 0; j < el_w; ++j) { |
| 182 | const float val = reg[j]; |
| 183 | const float val2 = smem[j * NT + (tid ^ h)]; |
| 184 | reg[j] = (tid & h) == 0 ? val + val2 : val2 - val; |
| 185 | } |
| 186 | item.barrier(sycl::access::fence_space::local_space); |
| 187 | } |
| 188 | |
| 189 | // Butterflies across registers: h is a multiple of NT, so the partner of element |
| 190 | // i*NT + tid lives in reg[i + h/NT] on the same work-item. |
| 191 | for (int h = NT; h < N; h *= 2) { |
| 192 | const int step = h / NT; |
| 193 | for (int j = 0; j < el_w; j += 2 * step) { |
| 194 | for (int k = 0; k < step; ++k) { |
| 195 | const float x = reg[j + k]; |
| 196 | const float y = reg[j + k + step]; |
| 197 | |
| 198 | reg[j + k] = x + y; |
| 199 | reg[j + k + step] = x - y; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | #pragma unroll |
| 205 | for (int i = 0; i < el_w; ++i) { |
| 206 | dst[i * NT + tid] = reg[i]; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | template <int N, int NT, typename T> |
| 211 | static void launch_fwht_wide(const T * src, |
| 212 | float * dst, |
| 213 | const int64_t n_rows, |
| 214 | const float scale, |
| 215 | dpct::queue_ptr stream) { |
| 216 | const sycl::range<2> global(n_rows, NT); |
| 217 | const sycl::range<2> local(1, NT); |
| 218 | |
| 219 | stream->submit([&](sycl::handler & cgh) { |
| 220 | sycl::local_accessor<float, 1> smem(sycl::range<1>(N), cgh); |
| 221 | cgh.parallel_for(sycl::nd_range<2>(global, local), |
| 222 | [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 223 | fwht_kernel_wide<N, NT, T>(src, dst, n_rows, scale, item, get_pointer(smem)); |
| 224 | }); |
| 225 | }); |
| 226 | } |
| 227 | |
| 228 | template <int N, int m, typename T> |
| 229 | static void kronecker_kernel(const T * __restrict__ src, |
| 230 | float * __restrict__ dst, |
| 231 | const int64_t n_rows, |
| 232 | const float scale, |
| 233 | const sycl::nd_item<2> & item) { |
| 234 | static_assert(m == 12 || m == 20, "block size has to be 12 or 20."); |
| 235 | |
| 236 | const sycl::sub_group sg = item.get_sub_group(); |
| 237 | |
| 238 | const int64_t r = item.get_global_id(0); |
| 239 | if (r >= n_rows) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | src += r * N; |
| 244 | dst += r * N; |
| 245 | |
| 246 | constexpr int blocks_per_group = N / m; |
| 247 | constexpr int el_w = blocks_per_group / WARP_SIZE; |
| 248 | static_assert(el_w >= 1 && blocks_per_group % WARP_SIZE == 0, "blocks_per_group must be a multiple of WARP_SIZE"); |
| 249 | float reg[el_w * m]; |
| 250 | const int lane = sg.get_local_linear_id(); |
| 251 | |
| 252 | #pragma unroll |
| 253 | for (int i = 0; i < el_w; ++i) { |
| 254 | const int b_idx = i * WARP_SIZE + lane; |
| 255 | |
| 256 | #pragma unroll |
| 257 | for (int j = 0; j < m; ++j) { |
| 258 | reg[i * m + j] = static_cast<float>(src[b_idx * m + j]) * scale; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | #pragma unroll |
| 263 | for (int b = 0; b < el_w; ++b) { |
| 264 | float z[m] = { 0.0f }; |
| 265 | |
| 266 | #pragma unroll |
| 267 | for (int i = 0; i < m; ++i) { |
| 268 | #pragma unroll |
| 269 | for (int j = 0; j < m; ++j) { |
| 270 | const float h = (m == 12 ? H12[j][i] : H20[j][i]); |
| 271 | z[i] += reg[b * m + j] * h; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | #pragma unroll |
| 276 | for (int i = 0; i < m; ++i) { |
| 277 | reg[b * m + i] = z[i]; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | #pragma unroll |
| 282 | for (int h = 1; h < WARP_SIZE; h *= 2) { |
| 283 | #pragma unroll |
| 284 | for (int j = 0; j < el_w; ++j) { |
| 285 | #pragma unroll |
| 286 | for (int k = 0; k < m; ++k) { |
| 287 | const float val = reg[j * m + k]; |
| 288 | const float val2 = dpct::permute_sub_group_by_xor(sg, val, h, WARP_SIZE); |
| 289 | |
| 290 | reg[j * m + k] = (lane & h) == 0 ? val + val2 : val2 - val; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | #pragma unroll |
| 296 | for (int h = WARP_SIZE; h < blocks_per_group; h *= 2) { |
| 297 | const int step = h / WARP_SIZE; |
| 298 | #pragma unroll |
| 299 | for (int j = 0; j < el_w; j += 2 * step) { |
| 300 | #pragma unroll |
| 301 | for (int s = 0; s < step; ++s) { |
| 302 | #pragma unroll |
| 303 | for (int k = 0; k < m; ++k) { |
| 304 | const float x = reg[(j + s) * m + k]; |
| 305 | const float y = reg[(j + s + step) * m + k]; |
| 306 | |
| 307 | reg[(j + s) * m + k] = x + y; |
| 308 | reg[(j + s + step) * m + k] = x - y; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | #pragma unroll |
| 315 | for (int i = 0; i < el_w; ++i) { |
| 316 | const int b_idx = i * WARP_SIZE + lane; |
| 317 | #pragma unroll |
| 318 | for (int k = 0; k < m; ++k) { |
| 319 | dst[b_idx * m + k] = reg[i * m + k]; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | template <int N, int m, typename T> |
| 325 | static void launch_kronecker(const T * src, |
| 326 | float * dst, |
| 327 | const int64_t n_rows, |
| 328 | const float scale, |
| 329 | dpct::queue_ptr stream) { |
| 330 | constexpr int rows_per_block = 4; |
| 331 | |
| 332 | const int64_t num_blocks = (n_rows + rows_per_block - 1) / rows_per_block; |
| 333 | |
| 334 | // dim 1 is the fastest-varying, so a sub-group is exactly one row's WARP_SIZE lanes. |
| 335 | const sycl::range<2> global(num_blocks * rows_per_block, WARP_SIZE); |
| 336 | const sycl::range<2> local(rows_per_block, WARP_SIZE); |
| 337 | |
| 338 | stream->parallel_for(sycl::nd_range<2>(global, local), |
| 339 | [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(WARP_SIZE)]] { |
| 340 | kronecker_kernel<N, m, T>(src, dst, n_rows, scale, item); |
| 341 | }); |
| 342 | } |
| 343 | |
| 344 | template <typename T> |
| 345 | static bool ggml_sycl_op_fwht_impl(ggml_backend_sycl_context & ctx, const ggml_tensor * src, ggml_tensor * dst) { |
| 346 | const int n = (int) src->ne[0]; |
| 347 | const int64_t rows = ggml_nrows(src); |
| 348 | |
| 349 | const T * src_d = (const T *) src->data; |
| 350 | float * dst_d = (float *) dst->data; |
| 351 | dpct::queue_ptr stream = ctx.stream(); |
| 352 | |
| 353 | const float scale = 1.0f / std::sqrt((float) n); |
| 354 | |
| 355 | switch (n) { |
| 356 | case 64: |
| 357 | launch_fwht<64>(src_d, dst_d, rows, scale, stream); |
| 358 | return true; |
| 359 | case 128: |
| 360 | launch_fwht<128>(src_d, dst_d, rows, scale, stream); |
| 361 | return true; |
| 362 | case 256: |
| 363 | launch_fwht<256>(src_d, dst_d, rows, scale, stream); |
| 364 | return true; |
| 365 | case 512: |
| 366 | launch_fwht<512>(src_d, dst_d, rows, scale, stream); |
| 367 | return true; |
| 368 | case 384: |
| 369 | launch_kronecker<384, 12>(src_d, dst_d, rows, scale, stream); |
| 370 | return true; |
| 371 | case 768: |
| 372 | launch_kronecker<768, 12>(src_d, dst_d, rows, scale, stream); |
| 373 | return true; |
| 374 | case 640: |
| 375 | launch_kronecker<640, 20>(src_d, dst_d, rows, scale, stream); |
| 376 | return true; |
| 377 | case 1280: |
| 378 | launch_kronecker<1280, 20>(src_d, dst_d, rows, scale, stream); |
| 379 | return true; |
| 380 | case 1024: |
| 381 | launch_fwht_wide<1024, 256>(src_d, dst_d, rows, scale, stream); |
| 382 | return true; |
| 383 | case 2048: |
| 384 | launch_fwht_wide<2048, 256>(src_d, dst_d, rows, scale, stream); |
| 385 | return true; |
| 386 | case 4096: |
| 387 | launch_fwht_wide<4096, 256>(src_d, dst_d, rows, scale, stream); |
| 388 | return true; |
| 389 | case 8192: |
| 390 | launch_fwht_wide<8192, 256>(src_d, dst_d, rows, scale, stream); |
| 391 | return true; |
| 392 | default: |
| 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | bool ggml_sycl_op_fwht(ggml_backend_sycl_context & ctx, const ggml_tensor * src, ggml_tensor * dst) { |
| 398 | if (dst->type != GGML_TYPE_F32) { |
| 399 | return false; |
| 400 | } |
| 401 | if (!ggml_are_same_shape(src, dst)) { |
| 402 | return false; |
| 403 | } |
| 404 | if (!ggml_is_contiguous(src) || !ggml_is_contiguous(dst)) { |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | switch (src->type) { |
| 409 | case GGML_TYPE_F32: |
| 410 | return ggml_sycl_op_fwht_impl<float>(ctx, src, dst); |
| 411 | case GGML_TYPE_F16: |
| 412 | return ggml_sycl_op_fwht_impl<sycl::half>(ctx, src, dst); |
| 413 | default: |
| 414 | return false; |
| 415 | } |
| 416 | } |