interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-sycl / gla.cpp

107 lines · 4399 bytes Raw Blame History
1#include <sycl/sycl.hpp>
2
3#include "common.hpp"
4#include "gla.hpp"
5
6template <u_int HEAD_SIZE>
7static void gated_linear_attn_f32_kernel(const dpct::queue_ptr stream, u_int B, u_int T, u_int C, u_int H, float scale,
8 const float * k, const float * v, const float * r, const float * td,
9 const float * s, float * dst) {
10 const u_int head_size = HEAD_SIZE;
11 const u_int state_size = C * head_size;
12 const u_int n_seq_tokens = T / B;
13 sycl::range<1> block_dims((C / H));
14 sycl::range<1> grid_dims((B * H));
15 stream->submit([&](sycl::handler & cgh) {
16 /* local memory accessors*/
17 auto _k = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
18 auto _r = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
19 auto _td = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
20
21 cgh.parallel_for(sycl::nd_range<1>(grid_dims * block_dims, block_dims), [=](sycl::nd_item<1> item) {
22 u_int tid = item.get_local_id(0);
23 u_int bid = item.get_group(0);
24
25 u_int batch_i = bid / H;
26 u_int head_i = bid % H;
27
28 float state[head_size];
29
30#pragma unroll
31 for (u_int i = 0; i < head_size; i++) {
32 state[i] = s[batch_i * state_size + head_i * head_size * head_size + i * head_size + tid];
33 }
34
35 for (u_int t = batch_i * n_seq_tokens * C + head_i * head_size + tid;
36 t < (batch_i + 1) * n_seq_tokens * C + head_i * head_size + tid; t += C) {
37
38 item.barrier(sycl::access::fence_space::local_space); //sync threads
39 _k[tid] = k[t];
40 _r[tid] = r[t];
41 _td[tid] = td[t];
42 item.barrier(sycl::access::fence_space::local_space); //sync threads
43
44 const float _v = v[t];
45 float y = 0;
46
47 for (u_int j = 0; j < head_size; j += 4) {
48 const sycl::float4 & k = (sycl::float4 &) (_k[j]);
49 const sycl::float4 & r = (sycl::float4 &) (_r[j]);
50 const sycl::float4 & td = (sycl::float4 &) (_td[j]);
51 sycl::float4 & s = (sycl::float4 &) (state[j]);
52 sycl::float4 kv;
53
54 kv.x() = k.x() * _v;
55 kv.y() = k.y() * _v;
56 kv.z() = k.z() * _v;
57 kv.w() = k.w() * _v;
58
59 s.x() = s.x() * td.x() + kv.x();
60 s.y() = s.y() * td.y() + kv.y();
61 s.z() = s.z() * td.z() + kv.z();
62 s.w() = s.w() * td.w() + kv.w();
63
64 y += r.x() * s.x();
65 y += r.y() * s.y();
66 y += r.z() * s.z();
67 y += r.w() * s.w();
68 }
69 dst[t] = y * scale;
70 }
71#pragma unroll
72 for (u_int i = 0; i < head_size; i++) {
73 dst[T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid] = state[i];
74 }
75 });
76 });
77}
78
79void ggml_sycl_op_gated_linear_attn(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
80 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/5);
81 const float * k_d = static_cast<const float *>(dst->src[0]->data);
82 const float * v_d = static_cast<const float *>(dst->src[1]->data);
83 const float * r_d = static_cast<const float *>(dst->src[2]->data);
84 const float * td_d = static_cast<const float *>(dst->src[3]->data);
85 const float * s_d = static_cast<const float *>(dst->src[4]->data);
86
87 const int64_t B = dst->src[4]->ne[1];
88 const int64_t T = dst->src[0]->ne[2];
89 const int64_t C = dst->ne[0];
90 const int64_t H = dst->src[0]->ne[1];
91
92 dpct::queue_ptr stream = ctx.stream();
93 GGML_ASSERT(dst->src[4]->type == GGML_TYPE_F32);
94 GGML_ASSERT(C % H == 0);
95 GGML_ASSERT(C / H == 64 || C / H == 128);
96
97 float scale;
98 memcpy(&scale, dst->op_params, sizeof(float));
99
100 float * dst_d = (float *) dst->data;
101
102 if (C / H == 64) {
103 gated_linear_attn_f32_kernel<64>(stream, B, T, C, H, scale, k_d, v_d, r_d, td_d, s_d, dst_d);
104 } else {
105 gated_linear_attn_f32_kernel<128>(stream, B, T, C, H, scale, k_d, v_d, r_d, td_d, s_d, dst_d);
106 }
107}