File size: 4,235 Bytes
d816568 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #version 450
#extension GL_KHR_shader_subgroup_arithmetic : enable
/*
* JINXAI Fused Bitwise Associative GGUF Megakernel (Vulkan Compute)
* =================================================================
* Implements the 4 viable multiplication-free mechanisms in ONE fused pass:
* 1. Single Fused Shader: loads packed static GGUF bitplanes into shared VRAM
* once per workgroup, avoiding intermediate global memory round-trips.
* 2. Bit-Shift / LUT Arithmetic: uses XNOR + bitCount (popcount) and
* Power-of-Two (PoT) exponent bit-shifts instead of FP32 GEMM.
* 3. State-Based Recurrent Update: updates a fixed-size Fast-Weight / SSM
* hidden state (O(d) memory, zero growing KV cache).
* 4. LSH-Constrained Sparse Distributed Memory (SDM) Reflection: probes only
* the K nearest LSH buckets (O(K*d), avoiding O(P^2) all-to-all trap).
*/
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(std430, binding = 0) readonly buffer InBits { uint x_bits[]; }; // packed 1-bit input
layout(std430, binding = 1) readonly buffer WeightBits { uint w_bits[]; }; // packed 1-bit GGUF sign plane
layout(std430, binding = 2) readonly buffer WeightMask { uint w_mask[]; }; // packed 1-bit ternary nonzero mask
layout(std430, binding = 3) readonly buffer PotShifts { int w_pot_shift[]; }; // Power-of-Two shift exponent per row
layout(std430, binding = 4) readonly buffer SdmKeys { uint sdm_keys[]; }; // LSH-indexed SDM associative keys
layout(std430, binding = 5) readonly buffer SdmVals { int sdm_vals[]; }; // fixed-point SDM associative values
layout(std430, binding = 6) buffer StateBuf { int state_q12[]; }; // Q12 fixed-point recurrent state
layout(std430, binding = 7) writeonly buffer OutBuf { int y_q12[]; }; // Q12 output vector
layout(push_constant) uniform Params {
uint d_words; // number of uint32 words per vector (d_model / 32)
uint d_out; // output dimension
uint sdm_k; // LSH active bucket probe count (K << P)
int state_decay; // bit-shift decay (e.g. >> 2 = 0.75 retention)
} pc;
shared uint s_x_bits[64];
void main() {
uint row = gl_WorkGroupID.x;
uint lid = gl_LocalInvocationID.x;
if (row >= pc.d_out) return;
// 1. Cooperative load of input bitfield into shared memory
if (lid < pc.d_words) {
s_x_bits[lid] = x_bits[lid];
}
barrier();
// 2. Multiplication-free ternary XNOR + Popcount across packed GGUF words
int partial_sum = 0;
uint row_base = row * pc.d_words;
for (uint w = lid; w < pc.d_words; w += gl_WorkGroupSize.x) {
uint xb = s_x_bits[w];
uint wb = w_bits[row_base + w];
uint wm = w_mask[row_base + w]; // 1 where ternary weight != 0
uint xnor_match = (~(xb ^ wb)) & wm;
int pos_cnt = bitCount(xnor_match);
int active_cnt = bitCount(wm);
partial_sum += (2 * pos_cnt - active_cnt);
}
// Subgroup reduction (wavefront/warp register-level sum)
int sg_sum = subgroupAdd(partial_sum);
if (lid == 0) {
// Apply Power-of-Two (PoT) bit-shift scaling instead of FP32 multiply
int shift = w_pot_shift[row];
int scaled_q12 = (shift >= 0) ? (sg_sum << min(shift, 10)) : (sg_sum >> min(-shift, 10));
// 4. LSH-Constrained Sparse Distributed Memory associative reflection
int sdm_acc = 0;
for (uint k = 0; k < pc.sdm_k; ++k) {
uint k_base = k * pc.d_words;
int ham_sim = 0;
for (uint w = 0; w < pc.d_words; ++w) {
ham_sim += bitCount(~(s_x_bits[w] ^ sdm_keys[k_base + w]));
}
// Threshold gate via integer shift (no float mul)
int gate = max(0, ham_sim - int(pc.d_words * 16));
sdm_acc += (sdm_vals[k * pc.d_out + row] * gate) >> 6;
}
// 3. Recurrent State-Space / Fast-Weight transition (pure bit-shift decay)
int prev_s = state_q12[row];
int decayed = prev_s - (prev_s >> max(1, pc.state_decay));
int next_s = decayed + (scaled_q12 >> 2) + (sdm_acc >> 3);
state_q12[row] = next_s;
y_q12[row] = next_s;
}
}
|