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-vulkan / vulkan-shaders / dsv4_hc_pre.comp

75 lines · 1983 bytes Raw Blame History
1#version 450
2
3#extension GL_EXT_control_flow_attributes : require
4
5// Collapse the hc residual streams of a token into one, weighted per stream:
6//
7// dst[i0, it] = scale * sum_ih x[i0, ih, it] * weights[ih, it]
8//
9// GATED: weights is a per-element gate [n_embd, hc, n_tokens], applied as sigmoid:
10//
11// dst[i0, it] = scale * sum_ih x[i0, ih, it] * sigmoid(gate[i0, ih, it])
12
13layout(constant_id = 0) const uint BLOCK_SIZE = 256;
14layout(constant_id = 1) const uint GATED = 0;
15
16layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
17
18layout(push_constant) uniform parameter
19{
20 uint n_embd;
21 uint n_tokens;
22
23 uint nbx0; uint nbx1; uint nbx2; // x
24 uint nbw0; uint nbw1; uint nbw2; // weights / gate
25 uint nbd0; uint nbd1; // dst
26
27 uint x_offset;
28 uint w_offset;
29 uint d_offset;
30
31 float scale;
32};
33
34layout(binding = 0, std430) readonly buffer X { float data_x[]; };
35layout(binding = 1, std430) readonly buffer W { float data_w[]; };
36layout(binding = 2, std430) writeonly buffer D { float data_d[]; };
37
38const uint hc = 4;
39
40shared float w[hc];
41
42void main() {
43 const uint tid = gl_LocalInvocationID.x;
44 const uint it = gl_WorkGroupID.y;
45
46 if (GATED == 0) {
47 if (tid < hc) {
48 w[tid] = data_w[w_offset + tid * nbw0 + it * nbw1];
49 }
50 barrier();
51 }
52
53 // After the barrier, so every invocation reaches it.
54 const uint i0 = gl_WorkGroupID.x * BLOCK_SIZE + tid;
55 if (i0 >= n_embd) {
56 return;
57 }
58
59 const uint xb = x_offset + i0 * nbx0 + it * nbx2;
60 const uint wb = w_offset + i0 * nbw0 + it * nbw2;
61
62 float result = 0.0f;
63 [[unroll]]
64 for (uint ih = 0; ih < hc; ++ih) {
65 float wv;
66 if (GATED == 1) {
67 wv = 1.0f / (1.0f + exp(-data_w[wb + ih * nbw1]));
68 } else {
69 wv = w[ih];
70 }
71 result = fma(data_x[xb + ih * nbx1], wv, result);
72 }
73
74 data_d[d_offset + i0 * nbd0 + it * nbd1] = scale * result;
75}