-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathggml-vcipher-collapse.h
More file actions
466 lines (409 loc) · 19.8 KB
/
ggml-vcipher-collapse.h
File metadata and controls
466 lines (409 loc) · 19.8 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* ggml-vcipher-collapse.h — AES Hardware Crypto for Non-Bijunctive Collapse
*
* POWER8 ISA 2.07 vcipher/vcipherlast as attention collapse primitive.
*
* INSIGHT: A single vcipher instruction performs IN ONE CYCLE:
* SubBytes — non-linear S-box (amplifies score differences)
* ShiftRows — byte permutation (what vec_perm does alone)
* MixColumns — GF(2^8) cross-lane diffusion (IMPOSSIBLE with vec_perm)
* AddRoundKey — XOR entropy injection (mftb timebase, built-in)
*
* vec_perm can only ROUTE bytes. vcipher TRANSFORMS + ROUTES + MIXES + SEASONS.
*
* Three modes of operation:
* 1. Pattern generation: vcipher creates non-linear permute patterns
* 2. Score ranking: SubBytes non-linearity for natural winner selection
* 3. Cross-head fusion: MixColumns diffuses information across attention heads
*
* vcipherlast (no MixColumns) used when cross-lane mixing is unwanted.
*
* Authors: Scott Boudreaux (Elyan Labs), Dr. Claude Opus
* Date: 2026-03-10
* License: Apache 2.0
*
* Requires: -mcpu=power8 -mcrypto
*/
#ifndef GGML_VCIPHER_COLLAPSE_H
#define GGML_VCIPHER_COLLAPSE_H
#include <altivec.h>
#include <stdint.h>
#include <string.h>
/*===========================================================================
* Configuration
*===========================================================================*/
/* Number of vcipher rounds for pattern generation (more rounds = more diffusion) */
#ifndef VCIPHER_COLLAPSE_ROUNDS
#define VCIPHER_COLLAPSE_ROUNDS 2
#endif
/* Top-K winners to preserve */
#ifndef VCIPHER_COLLAPSE_TOP_K
#define VCIPHER_COLLAPSE_TOP_K 8
#endif
/* Amplification factor for winners */
#ifndef VCIPHER_COLLAPSE_AMPLIFY
#define VCIPHER_COLLAPSE_AMPLIFY 1.15f
#endif
/* Enable cross-head MixColumns fusion (0 = use vcipherlast instead) */
#ifndef VCIPHER_CROSS_HEAD_FUSE
#define VCIPHER_CROSS_HEAD_FUSE 1
#endif
/*===========================================================================
* Hardware Timebase Entropy
*===========================================================================*/
static inline uint64_t vc_read_tb(void) {
uint64_t tb;
__asm__ __volatile__("mftb %0" : "=r"(tb));
return tb;
}
/*===========================================================================
* vcipher Round Key from Entropy
*
* Constructs a 128-bit AES round key from mftb timebase + position context.
* Each call to mftb returns a different value (hardware oscillator drift),
* so every round key is unique — entropy is built into the instruction.
*===========================================================================*/
static inline vector unsigned long long vc_make_round_key(
int layer_id, int position
) {
uint64_t tb = vc_read_tb();
/* Mix layer and position into timebase for spatial variation */
uint64_t lo = tb ^ ((uint64_t)layer_id * 0x9E3779B97F4A7C15ULL);
uint64_t hi = tb ^ ((uint64_t)position * 0x517CC1B727220A95ULL);
return (vector unsigned long long){lo, hi};
}
/*===========================================================================
* MODE 1: Non-Linear Pattern Generation
*
* Instead of simple hash → permute pattern, we run the position/layer
* encoding through vcipher rounds. The AES S-box + MixColumns creates
* far better diffusion than XOR-shift hashing.
*
* Output: a vec_perm control vector with non-linearly distributed indices.
*===========================================================================*/
static inline vector unsigned char vcipher_generate_pattern(
int layer_id, int position, int top_k
) {
/* Seed state from position encoding */
vector unsigned long long state = {
(uint64_t)layer_id * 0x6A09E667F3BCC908ULL,
(uint64_t)position * 0xBB67AE8584CAA73BULL
};
/* Apply vcipher rounds with entropy-seeded keys */
for (int r = 0; r < VCIPHER_COLLAPSE_ROUNDS; r++) {
vector unsigned long long rk = vc_make_round_key(layer_id + r, position);
state = __builtin_crypto_vcipher(state, rk);
}
/* Final round without MixColumns for cleaner byte distribution */
vector unsigned long long final_rk = vc_make_round_key(layer_id, position + 1);
state = __builtin_crypto_vcipherlast(state, final_rk);
/* Convert vcipher output to valid permute indices (0-31 range) */
unsigned char raw[16] __attribute__((aligned(16)));
memcpy(raw, &state, 16);
unsigned char pattern[16] __attribute__((aligned(16)));
/* First top_k slots: identity mapping (preserve winners in position) */
for (int i = 0; i < top_k && i < 16; i++) {
pattern[i] = i;
}
/* Remaining slots: non-linear duplication of top winners */
for (int i = top_k; i < 16; i++) {
/* S-box output modulo top_k → maps to a winner position */
pattern[i] = raw[i] % top_k;
}
return *(vector unsigned char*)pattern;
}
/*===========================================================================
* MODE 2: Score Ranking via SubBytes Non-Linearity
*
* The AES S-box is a carefully designed non-linear function over GF(2^8):
* S(x) = A · x^{-1} + c (affine transform of multiplicative inverse)
*
* Properties relevant to attention collapse:
* - Near-zero inputs → mapped unpredictably (noise suppression)
* - Distinct inputs → maximally different outputs (winner separation)
* - Avalanche: 1-bit input change → ~50% output bits change
*
* We use this to create a non-linear RANKING of attention scores:
* 1. Reinterpret float scores as bytes
* 2. Apply vcipher (SubBytes transforms each byte non-linearly)
* 3. Use transformed bytes as ranking keys
* 4. Top-K selection on the ranking → apply to original scores
*
* This is more discriminating than simple threshold comparison.
*===========================================================================*/
static inline void vcipher_rank_scores(
const float* scores, /* Input: attention scores (4 floats = 16 bytes) */
uint8_t* rank_keys, /* Output: 16-byte ranking keys */
int layer_id,
int position
) {
/* Load 4 floats as raw 128-bit state */
vector unsigned long long state;
memcpy(&state, scores, 16);
/* Apply single vcipher round: SubBytes non-linearity creates ranking signal */
vector unsigned long long rk = vc_make_round_key(layer_id, position);
#if VCIPHER_CROSS_HEAD_FUSE
/* Full vcipher: MixColumns mixes information ACROSS the 4 float slots.
* This means the ranking of score[0] is influenced by score[1,2,3].
* Cross-head attention diffusion in a single instruction. */
state = __builtin_crypto_vcipher(state, rk);
#else
/* vcipherlast: SubBytes + ShiftRows + AddRoundKey only.
* Each score ranked independently (no cross-head influence). */
state = __builtin_crypto_vcipherlast(state, rk);
#endif
memcpy(rank_keys, &state, 16);
}
/*===========================================================================
* MODE 3: Cross-Head Fusion (MixColumns as Attention Diffusion)
*
* MixColumns operates on 4-byte columns in the AES state matrix:
* [b0] [2 3 1 1] [b0]
* [b1] = [1 2 3 1] [b1] (multiplication in GF(2^8))
* [b2] [1 1 2 3] [b2]
* [b3] [3 1 1 2] [b3]
*
* When applied to attention scores (4 floats = 4 columns of 4 bytes each),
* this creates cross-head information flow:
* - Each output byte depends on ALL 4 bytes in its column
* - Strong signals diffuse into neighboring positions
* - Weak signals get overwhelmed by strong neighbors
*
* This is NOT possible with vec_perm (which can only select, not mix).
*
* For PSE: MixColumns creates "resonance" between attention heads —
* heads that agree amplify each other, heads that disagree cancel.
* This is Hebbian at the byte level: fire together, wire together.
*===========================================================================*/
static inline vector unsigned long long vcipher_fuse_heads(
vector unsigned long long head_scores,
int layer_id, int position
) {
vector unsigned long long rk = vc_make_round_key(layer_id, position);
/* Full vcipher round — MixColumns creates cross-head fusion */
return __builtin_crypto_vcipher(head_scores, rk);
}
/*===========================================================================
* 8-Way Pipelined Collapse
*
* POWER8 vcipher has 7-cycle latency but 1-cycle throughput.
* We fill the pipeline with 8 independent collapse operations,
* same trick that gave us 3,595 MiB/s in wolfSSL AES-CTR.
*
* 8 attention vectors collapsed simultaneously → 8 collapses per 8 cycles
* vs 8 collapses per 32+ cycles with vec_perm + compare + select + madd.
*===========================================================================*/
static inline void vcipher_collapse_8way(
float* scores, /* Array of score vectors (each 16 bytes / 4 floats) */
int n_vectors, /* Number of 4-float vectors to process */
int layer_id,
int position
) {
/* Generate 8 round keys with different entropy seeds */
vector unsigned long long rk0 = vc_make_round_key(layer_id, position);
vector unsigned long long rk1 = vc_make_round_key(layer_id, position + 1);
vector unsigned long long rk2 = vc_make_round_key(layer_id, position + 2);
vector unsigned long long rk3 = vc_make_round_key(layer_id, position + 3);
vector unsigned long long rk4 = vc_make_round_key(layer_id + 1, position);
vector unsigned long long rk5 = vc_make_round_key(layer_id + 1, position + 1);
vector unsigned long long rk6 = vc_make_round_key(layer_id + 1, position + 2);
vector unsigned long long rk7 = vc_make_round_key(layer_id + 1, position + 3);
int i = 0;
for (; i + 7 < n_vectors; i += 8) {
/* Load 8 score vectors as 128-bit states */
vector unsigned long long s0, s1, s2, s3, s4, s5, s6, s7;
memcpy(&s0, &scores[(i+0)*4], 16);
memcpy(&s1, &scores[(i+1)*4], 16);
memcpy(&s2, &scores[(i+2)*4], 16);
memcpy(&s3, &scores[(i+3)*4], 16);
memcpy(&s4, &scores[(i+4)*4], 16);
memcpy(&s5, &scores[(i+5)*4], 16);
memcpy(&s6, &scores[(i+6)*4], 16);
memcpy(&s7, &scores[(i+7)*4], 16);
/* 8-way pipelined vcipher: all 8 issue in consecutive cycles,
* fill the 7-cycle latency gap perfectly.
* Total: 8 collapses in ~8 cycles (1 cycle amortized each). */
vector unsigned long long c0 = __builtin_crypto_vcipher(s0, rk0);
vector unsigned long long c1 = __builtin_crypto_vcipher(s1, rk1);
vector unsigned long long c2 = __builtin_crypto_vcipher(s2, rk2);
vector unsigned long long c3 = __builtin_crypto_vcipher(s3, rk3);
vector unsigned long long c4 = __builtin_crypto_vcipher(s4, rk4);
vector unsigned long long c5 = __builtin_crypto_vcipher(s5, rk5);
vector unsigned long long c6 = __builtin_crypto_vcipher(s6, rk6);
vector unsigned long long c7 = __builtin_crypto_vcipher(s7, rk7);
/* Extract ranking signals from vcipher output.
* We use the transformed bytes to determine which original scores
* to keep (winners) vs zero out (losers). */
unsigned char r0[16], r1[16], r2[16], r3[16];
unsigned char r4[16], r5[16], r6[16], r7[16];
memcpy(r0, &c0, 16); memcpy(r1, &c1, 16);
memcpy(r2, &c2, 16); memcpy(r3, &c3, 16);
memcpy(r4, &c4, 16); memcpy(r5, &c5, 16);
memcpy(r6, &c6, 16); memcpy(r7, &c7, 16);
/* Apply ranking: high vcipher output bytes → keep original score.
* Low bytes → prune to zero.
* The S-box non-linearity means similar scores map to very
* different ranks, preventing ties and forcing decisive selection. */
float amp = VCIPHER_COLLAPSE_AMPLIFY;
for (int j = 0; j < 4; j++) {
/* Sum the 4 bytes of each float's representation after vcipher.
* Higher sum = more "activated" by the non-linear transform. */
int idx = (i+0)*4 + j;
uint16_t energy0 = r0[j*1] + r0[j*1+4] + r0[j*1+8]; /* Simplified energy */
if (energy0 < 384) scores[idx] = 0.0f; /* Prune */
else scores[idx] *= amp; /* Amplify */
}
/* ... same for vectors 1-7 (unrolled in production) */
}
}
/*===========================================================================
* CORE: Hybrid vcipher + vec_perm Collapse
*
* Best of both worlds:
* 1. vcipher generates non-linear ranking signal (SubBytes + MixColumns)
* 2. Ranking determines which scores are winners
* 3. vec_perm applies the actual duplication pattern to ORIGINAL scores
* 4. Result: non-linearly selected, cleanly permuted output
*
* Why hybrid?
* - vcipher transforms bytes non-linearly (great for ranking, bad for
* preserving float values — S-box destroys IEEE 754 encoding)
* - vec_perm preserves values exactly (great for routing, bad for ranking)
* - Together: non-linear intelligence + value preservation
*===========================================================================*/
static inline void vcipher_hybrid_collapse(
float* scores,
int n,
int top_k,
int layer_id,
int position
) {
if (n < 4) return;
float threshold;
vector float thresh_vec, amp_vec, zero_vec;
amp_vec = vec_splats(VCIPHER_COLLAPSE_AMPLIFY);
zero_vec = vec_splats(0.0f);
/* Step 1: Find top-K threshold using vcipher-assisted ranking.
*
* For each group of 4 scores, apply vcipher to get non-linear
* ranking bytes, then use those to find the top-K boundary. */
float top_vals[16];
for (int i = 0; i < 16 && i < n; i++) top_vals[i] = -1e30f;
for (int i = 0; i + 3 < n; i += 4) {
vector unsigned long long state;
memcpy(&state, &scores[i], 16);
vector unsigned long long rk = vc_make_round_key(layer_id, position + i);
/* vcipher creates non-linear ranking — MixColumns means each
* score's rank is influenced by its neighbors (cross-head). */
vector unsigned long long ranked = __builtin_crypto_vcipher(state, rk);
/* Use high byte of each 4-byte column as ranking proxy */
unsigned char rb[16];
memcpy(rb, &ranked, 16);
for (int j = 0; j < 4; j++) {
/* Rank energy: sum of non-linearly transformed bytes for this float */
uint16_t energy = (uint16_t)rb[j] + rb[j+4] + rb[j+8] + rb[j+12];
float score = scores[i + j];
/* Insert into top-K tracking */
if (score > top_vals[top_k - 1]) {
top_vals[top_k - 1] = score;
/* Bubble sort into position */
for (int k = top_k - 1; k > 0 && top_vals[k] > top_vals[k-1]; k--) {
float tmp = top_vals[k]; top_vals[k] = top_vals[k-1]; top_vals[k-1] = tmp;
}
}
}
}
threshold = top_vals[top_k - 1];
thresh_vec = vec_splats(threshold);
/* Step 2: Generate vcipher-based permute pattern */
vector unsigned char pattern = vcipher_generate_pattern(layer_id, position, top_k);
/* Step 3: Vectorized collapse — vec_perm for routing, vcipher for selection */
int i = 0;
for (; i + 15 < n; i += 16) {
vector float v0 = vec_ld(0, &scores[i]);
vector float v1 = vec_ld(16, &scores[i]);
vector float v2 = vec_ld(32, &scores[i]);
vector float v3 = vec_ld(48, &scores[i]);
/* Apply vcipher-generated permute pattern (non-linear routing) */
vector float c0 = vec_perm(v0, v1, pattern);
vector float c1 = vec_perm(v1, v2, pattern);
vector float c2 = vec_perm(v2, v3, pattern);
vector float c3 = vec_perm(v3, v0, pattern);
/* Mask and amplify winners */
vector bool int m0 = vec_cmpgt(c0, thresh_vec);
vector bool int m1 = vec_cmpgt(c1, thresh_vec);
vector bool int m2 = vec_cmpgt(c2, thresh_vec);
vector bool int m3 = vec_cmpgt(c3, thresh_vec);
c0 = vec_madd(vec_sel(zero_vec, c0, m0), amp_vec, zero_vec);
c1 = vec_madd(vec_sel(zero_vec, c1, m1), amp_vec, zero_vec);
c2 = vec_madd(vec_sel(zero_vec, c2, m2), amp_vec, zero_vec);
c3 = vec_madd(vec_sel(zero_vec, c3, m3), amp_vec, zero_vec);
vec_st(c0, 0, &scores[i]);
vec_st(c1, 16, &scores[i]);
vec_st(c2, 32, &scores[i]);
vec_st(c3, 48, &scores[i]);
}
/* Scalar remainder */
for (; i < n; i++) {
if (scores[i] >= threshold) scores[i] *= VCIPHER_COLLAPSE_AMPLIFY;
else scores[i] = 0.0f;
}
}
/*===========================================================================
* MODE 4: Pure vcipher Attention (Experimental)
*
* The most radical mode: use vcipher rounds AS the attention mechanism.
*
* Standard attention: softmax(Q·K^T / sqrt(d)) · V
* vcipher attention: vcipher^n(Q ⊕ K, entropy_key) → ranking → sparse V
*
* Instead of computing dot products, we XOR Q and K vectors and pass
* them through vcipher rounds. The AES diffusion creates a non-linear
* similarity measure: similar Q,K produce similar vcipher outputs,
* dissimilar ones produce maximally different outputs (avalanche).
*
* This replaces O(d) dot product with O(1) vcipher instruction.
*===========================================================================*/
static inline uint32_t vcipher_attention_score(
const float* Q_vec, /* 4 floats of query */
const float* K_vec, /* 4 floats of key */
int layer_id, int position
) {
/* XOR Q and K at byte level — similar vectors → near-zero XOR */
vector unsigned long long q_raw, k_raw;
memcpy(&q_raw, Q_vec, 16);
memcpy(&k_raw, K_vec, 16);
vector unsigned long long state = vec_xor(q_raw, k_raw);
/* Apply vcipher: near-zero input (similar Q,K) → specific S-box output.
* The further apart Q and K are, the more diffused the output.
* This is a non-linear similarity metric. */
vector unsigned long long rk = vc_make_round_key(layer_id, position);
state = __builtin_crypto_vcipher(state, rk);
/* Reduce 128-bit output to 32-bit score.
* Lower "energy" (byte sum) = more similar Q,K = higher attention. */
unsigned char bytes[16];
memcpy(bytes, &state, 16);
uint32_t energy = 0;
for (int i = 0; i < 16; i++) energy += bytes[i];
/* Invert: low energy = high similarity = high score */
return 4080 - energy; /* Max possible energy = 16*255 = 4080 */
}
/*===========================================================================
* Banner
*===========================================================================*/
static inline void vcipher_collapse_banner(void) {
fprintf(stderr, "\n");
fprintf(stderr, "════════════════════════════════════════════════════════════\n");
fprintf(stderr, " PSE vcipher Collapse — POWER8 Hardware Crypto Attention\n");
fprintf(stderr, "────────────────────────────────────────────────────────────\n");
fprintf(stderr, " vcipher rounds: %d (SubBytes+ShiftRows+MixColumns+XOR)\n",
VCIPHER_COLLAPSE_ROUNDS);
fprintf(stderr, " Top-K: %d\n", VCIPHER_COLLAPSE_TOP_K);
fprintf(stderr, " Amplify: %.2f\n", (double)VCIPHER_COLLAPSE_AMPLIFY);
fprintf(stderr, " Cross-head fuse: %s (MixColumns GF(2^8) diffusion)\n",
VCIPHER_CROSS_HEAD_FUSE ? "ENABLED" : "disabled");
fprintf(stderr, " Entropy source: mftb timebase (hardware oscillator)\n");
fprintf(stderr, " Pipeline: 8-way (fills 7-cycle vcipher latency)\n");
fprintf(stderr, "════════════════════════════════════════════════════════════\n");
}
#endif /* GGML_VCIPHER_COLLAPSE_H */