Spaces:
Sleeping
Sleeping
Commit
·
fdb1fe5
1
Parent(s):
9b2706e
SYCL: Add gated linear attention kernel (llama/11175)
Browse files* SYCL: Add Gated Linear attention kernel
* glahpp: add a space at the end of file
* gla: Put the barrier inside the main logic loop
- ggml/src/ggml-sycl/backend.hpp +1 -0
- ggml/src/ggml-sycl/ggml-sycl.cpp +4 -0
- ggml/src/ggml-sycl/gla.cpp +105 -0
- ggml/src/ggml-sycl/gla.hpp +8 -0
ggml/src/ggml-sycl/backend.hpp
CHANGED
|
@@ -29,5 +29,6 @@
|
|
| 29 |
#include "wkv6.hpp"
|
| 30 |
#include "outprod.hpp"
|
| 31 |
#include "element_wise.hpp"
|
|
|
|
| 32 |
|
| 33 |
#endif // GGML_SYCL_BACKEND_HPP
|
|
|
|
| 29 |
#include "wkv6.hpp"
|
| 30 |
#include "outprod.hpp"
|
| 31 |
#include "element_wise.hpp"
|
| 32 |
+
#include "gla.hpp"
|
| 33 |
|
| 34 |
#endif // GGML_SYCL_BACKEND_HPP
|
ggml/src/ggml-sycl/ggml-sycl.cpp
CHANGED
|
@@ -4040,6 +4040,9 @@ bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tens
|
|
| 4040 |
case GGML_OP_RWKV_WKV6:
|
| 4041 |
ggml_sycl_op_rwkv_wkv6(ctx, dst);
|
| 4042 |
break;
|
|
|
|
|
|
|
|
|
|
| 4043 |
default:
|
| 4044 |
return false;
|
| 4045 |
}
|
|
@@ -4507,6 +4510,7 @@ static bool ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, const g
|
|
| 4507 |
case GGML_OP_LEAKY_RELU:
|
| 4508 |
case GGML_OP_TIMESTEP_EMBEDDING:
|
| 4509 |
case GGML_OP_RWKV_WKV6:
|
|
|
|
| 4510 |
return true;
|
| 4511 |
default:
|
| 4512 |
return false;
|
|
|
|
| 4040 |
case GGML_OP_RWKV_WKV6:
|
| 4041 |
ggml_sycl_op_rwkv_wkv6(ctx, dst);
|
| 4042 |
break;
|
| 4043 |
+
case GGML_OP_GATED_LINEAR_ATTN:
|
| 4044 |
+
ggml_sycl_op_gated_linear_attn(ctx, dst);
|
| 4045 |
+
break;
|
| 4046 |
default:
|
| 4047 |
return false;
|
| 4048 |
}
|
|
|
|
| 4510 |
case GGML_OP_LEAKY_RELU:
|
| 4511 |
case GGML_OP_TIMESTEP_EMBEDDING:
|
| 4512 |
case GGML_OP_RWKV_WKV6:
|
| 4513 |
+
case GGML_OP_GATED_LINEAR_ATTN:
|
| 4514 |
return true;
|
| 4515 |
default:
|
| 4516 |
return false;
|
ggml/src/ggml-sycl/gla.cpp
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <sycl/sycl.hpp>
|
| 2 |
+
|
| 3 |
+
#include "common.hpp"
|
| 4 |
+
|
| 5 |
+
template <u_int HEAD_SIZE>
|
| 6 |
+
static 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,
|
| 7 |
+
const float * k, const float * v, const float * r, const float * td,
|
| 8 |
+
const float * s, float * dst) {
|
| 9 |
+
const u_int head_size = HEAD_SIZE;
|
| 10 |
+
const u_int state_size = C * head_size;
|
| 11 |
+
const u_int n_seq_tokens = T / B;
|
| 12 |
+
sycl::range<1> block_dims((C / H));
|
| 13 |
+
sycl::range<1> grid_dims((B * H));
|
| 14 |
+
stream->submit([&](sycl::handler & cgh) {
|
| 15 |
+
/* local memory accessors*/
|
| 16 |
+
auto _k = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
|
| 17 |
+
auto _r = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
|
| 18 |
+
auto _td = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
|
| 19 |
+
|
| 20 |
+
cgh.parallel_for(sycl::nd_range<1>(grid_dims * block_dims, block_dims), [=](sycl::nd_item<1> item) {
|
| 21 |
+
u_int tid = item.get_local_id(0);
|
| 22 |
+
u_int bid = item.get_group(0);
|
| 23 |
+
|
| 24 |
+
u_int batch_i = bid / H;
|
| 25 |
+
u_int head_i = bid % H;
|
| 26 |
+
|
| 27 |
+
float state[head_size];
|
| 28 |
+
|
| 29 |
+
#pragma unroll
|
| 30 |
+
for (u_int i = 0; i < head_size; i++) {
|
| 31 |
+
state[i] = s[batch_i * state_size + head_i * head_size * head_size + i * head_size + tid];
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
for (u_int t = batch_i * n_seq_tokens * C + head_i * head_size + tid;
|
| 35 |
+
t < (batch_i + 1) * n_seq_tokens * C + head_i * head_size + tid; t += C) {
|
| 36 |
+
|
| 37 |
+
item.barrier(sycl::access::fence_space::local_space); //sync threads
|
| 38 |
+
_k[tid] = k[t];
|
| 39 |
+
_r[tid] = r[t];
|
| 40 |
+
_td[tid] = td[t];
|
| 41 |
+
item.barrier(sycl::access::fence_space::local_space); //sync threads
|
| 42 |
+
|
| 43 |
+
const float _v = v[t];
|
| 44 |
+
float y = 0;
|
| 45 |
+
|
| 46 |
+
for (u_int j = 0; j < head_size; j += 4) {
|
| 47 |
+
const sycl::float4 & k = (sycl::float4 &) (_k[j]);
|
| 48 |
+
const sycl::float4 & r = (sycl::float4 &) (_r[j]);
|
| 49 |
+
const sycl::float4 & td = (sycl::float4 &) (_td[j]);
|
| 50 |
+
sycl::float4 & s = (sycl::float4 &) (state[j]);
|
| 51 |
+
sycl::float4 kv;
|
| 52 |
+
|
| 53 |
+
kv.x() = k.x() * _v;
|
| 54 |
+
kv.y() = k.y() * _v;
|
| 55 |
+
kv.z() = k.z() * _v;
|
| 56 |
+
kv.w() = k.w() * _v;
|
| 57 |
+
|
| 58 |
+
s.x() = s.x() * td.x() + kv.x();
|
| 59 |
+
s.y() = s.y() * td.y() + kv.y();
|
| 60 |
+
s.z() = s.z() * td.z() + kv.z();
|
| 61 |
+
s.w() = s.w() * td.w() + kv.w();
|
| 62 |
+
|
| 63 |
+
y += r.x() * s.x();
|
| 64 |
+
y += r.y() * s.y();
|
| 65 |
+
y += r.z() * s.z();
|
| 66 |
+
y += r.w() * s.w();
|
| 67 |
+
}
|
| 68 |
+
dst[t] = y * scale;
|
| 69 |
+
}
|
| 70 |
+
#pragma unroll
|
| 71 |
+
for (u_int i = 0; i < head_size; i++) {
|
| 72 |
+
dst[T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid] = state[i];
|
| 73 |
+
}
|
| 74 |
+
});
|
| 75 |
+
});
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
void ggml_sycl_op_gated_linear_attn(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
|
| 79 |
+
const float * k_d = static_cast<const float *>(dst->src[0]->data);
|
| 80 |
+
const float * v_d = static_cast<const float *>(dst->src[1]->data);
|
| 81 |
+
const float * r_d = static_cast<const float *>(dst->src[2]->data);
|
| 82 |
+
const float * td_d = static_cast<const float *>(dst->src[3]->data);
|
| 83 |
+
const float * s_d = static_cast<const float *>(dst->src[4]->data);
|
| 84 |
+
|
| 85 |
+
const int64_t B = dst->src[4]->ne[1];
|
| 86 |
+
const int64_t T = dst->src[0]->ne[2];
|
| 87 |
+
const int64_t C = dst->ne[0];
|
| 88 |
+
const int64_t H = dst->src[0]->ne[1];
|
| 89 |
+
|
| 90 |
+
dpct::queue_ptr stream = ctx.stream();
|
| 91 |
+
GGML_ASSERT(dst->src[4]->type == GGML_TYPE_F32);
|
| 92 |
+
GGML_ASSERT(C % H == 0);
|
| 93 |
+
GGML_ASSERT(C / H == 64 || C / H == 128);
|
| 94 |
+
|
| 95 |
+
float scale;
|
| 96 |
+
memcpy(&scale, dst->op_params, sizeof(float));
|
| 97 |
+
|
| 98 |
+
float * dst_d = (float *) dst->data;
|
| 99 |
+
|
| 100 |
+
if (C / H == 64) {
|
| 101 |
+
gated_linear_attn_f32_kernel<64>(stream, B, T, C, H, scale, k_d, v_d, r_d, td_d, s_d, dst_d);
|
| 102 |
+
} else {
|
| 103 |
+
gated_linear_attn_f32_kernel<128>(stream, B, T, C, H, scale, k_d, v_d, r_d, td_d, s_d, dst_d);
|
| 104 |
+
}
|
| 105 |
+
}
|
ggml/src/ggml-sycl/gla.hpp
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#ifndef GGML_SYCL_GLA_HPP
|
| 2 |
+
#define GGML_SYCL_GLA_HPP
|
| 3 |
+
|
| 4 |
+
#include "common.hpp"
|
| 5 |
+
|
| 6 |
+
void ggml_sycl_op_gated_linear_attn(ggml_backend_sycl_context & ctx, ggml_tensor * dst);
|
| 7 |
+
|
| 8 |
+
#endif // GGML_SYCL_GLA_HPP
|