interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / btwave / trit.om

546 lines · 11873 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note BTWAVE-P0: Non-binary balanced-ternary SWAR wave foundation (native/btwave/trit.om).
3note Pure seeded OM-2 (om2) implementation:
4note 1. Trit32: 32 balanced trits {-1, 0, +1} packed in u64 ((pos32 << 32) | neg32)
5note with constructor postcondition (pos32 & neg32) == 0 (traps 10 -> exit 110).
6note 2. WaveDup: mirrored 64-bit waveguide frame tag with OM-2 holds dup32 (traps 10).
7note 3. CreditJ and GrantJ: distinct abstract types separating earned compute credit
8note from authorized compute extraction (swaps fail omc compilation with error 7).
9note 4. Multi-scale M=6 balanced-ternary toroidal codec on [-364, 364] (729 states,
10note enc(0) = 0 Dark Consensus, 1-op ROR negation symmetry) + M=3 16/16 hypercube.
11note 5. Orthogonal H16 Walsh-Hadamard ternary wave spreading (d_H = 8), correcting
12note all 0..3 carrier corruptions and fail-closed refusing >= 4-carrier ties.
13note 6. Pure OM-2 FIPS 180-4 SHA-256 with fresh message schedule and unrolled rounds.
14om2
15abstract Trit32 ulong ops rotr rotl
16abstract WaveDup ulong
17holds WaveDup dup32
18abstract CreditJ ulong
19abstract GrantJ ulong
20
21const M32 ulong 0xffffffff
22const M16 ulong 0xffff
23const VMAX long 65536
24const WMAX ulong 1000
25
26fn Trit32mk w ulong to Trit32
27ensures eq and shr w 32 and w 0xffffffff 0
28ret seal Trit32 w
29end
30
31fn WaveDupmk w ulong to WaveDup
32ret seal WaveDup w
33end
34
35fn CreditJmk w ulong to CreditJ
36ret seal CreditJ w
37end
38
39fn GrantJmk w ulong to GrantJ
40ret seal GrantJ w
41end
42
43fn wdup_tag tag uint to WaveDup
44u = ulong tag
45ret WaveDupmk or shl u 32 u
46end
47
48fn wdup_chk d WaveDup exp uint to bool
49ret eq and open d 0xffffffff ulong exp
50end
51
52note 1-op balanced-ternary negation across all 32 lanes via single AArch64 ROR #32.
53fn t32neg a Trit32 to Trit32
54ret rotr a 32
55end
56
57note Zero-multiply 32-wide balanced-ternary wave product under constant-time SWAR.
58fn t32mul a Trit32 b Trit32 to Trit32
59wa = open a
60wb = open b
61ws = rotr wb 32
62ab = and wa wb
63as = and wa ws
64p = or shr ab 32 and ab M32
65n = or shr as 32 and as M32
66ret Trit32mk or shl p 32 n
67end
68
69note Branchless SWAR popcount on 32-bit unsigned word (zero hardware multiplies).
70fn pop32 x uint to long
71ensures and ge result 0 le result 32
72v = sub x and shr x 1 0x55555555
73v = add and v 0x33333333 and shr v 2 0x33333333
74v = and add v shr v 4 0x0f0f0f0f
75v = add v shr v 8
76v = add v shr v 16
77ret long and v 0x3f
78end
79
80note Branchless 32-trit inner product in [-32, +32].
81fn t32dot a Trit32 b Trit32 to long
82ensures and ge result -32 le result 32
83m = open t32mul a b
84pp = pop32 uint shr m 32
85nn = pop32 uint and m M32
86ret sub pp nn
87end
88
89note Active non-zero carrier count in [0, 32]; 0 iff Dark Zero (all 32 lanes 0).
90fn t32pow a Trit32 to long
91ensures and ge result 0 le result 32
92wa = open a
93act = or shr wa 32 and wa M32
94ret pop32 uint act
95end
96
97note Flip carrier sign (+1 <-> -1, leaving 0 unchanged) at lane c < 32.
98fn t32flip a Trit32 c ulong to Trit32
99requires lt c 32
100wa = open a
101bit = shl ulong 1 c
102p = shr wa 32
103n = and wa M32
104bp = and p bit
105bn = and n bit
106np = or and p xor M32 bit bn
107nn = or and n xor M32 bit bp
108ret Trit32mk or shl np 32 nn
109end
110
111note Erase carrier to Dark Zero (0) at lane c < 32.
112fn t32eras a Trit32 c ulong to Trit32
113requires lt c 32
114wa = open a
115msk = xor M32 shl ulong 1 c
116p = and shr wa 32 msk
117n = and and wa M32 msk
118ret Trit32mk or shl p 32 n
119end
120
121note M=6 balanced-ternary encoder on [-364, +364] (3^6 = 729 states).
122note Branchless digit split: r in {0,1,2} -> pos = r>>1, neg = (2-r)>>1.
123fn bt6enc u long to Trit32
124requires and ge u -364 le u 364
125z = ulong add u 364
126p = ulong 0
127n = ulong 0
128for k ulong 6 unroll
129r = rem z 3
130z = div z 3
131ip = shr r 1
132ineg = shr sub 2 r 1
133p = or p shl ip k
134n = or n shl ineg k
135end
136ret Trit32mk or shl p 32 n
137end
138
139note M=6 balanced-ternary decoder to [-364, +364].
140fn bt6dec_w wa ulong to long
141requires eq and wa 0xffffffc0ffffffc0 0
142ensures and ge result -364 le result 364
143p = shr wa 32
144n = and wa M32
145acc = long 0
146w = long 1
147for k ulong 6 unroll
148dp = long and shr p k 1
149dn = long and shr n k 1
150acc = add acc mul sub dp dn w
151w = mul w 3
152end
153ret acc
154end
155
156fn bt6dec a Trit32 to long
157ret bt6dec_w open a
158end
159
160note Toroidal group addition on Z/729Z centered on [-364, +364].
161fn bt6tor u long v long to long
162requires and ge u -364 le u 364
163requires and ge v -364 le v 364
164ensures and ge result -364 le result 364
165s = add add u v 1093
166ret sub rem s 729 364
167end
168
169note Constructive 3-trit (3^3 = 27 > 16) hypercube encoder for 4-bit corner m < 16.
170fn hc3enc m ulong to Trit32
171requires lt m 16
172z = add m 6
173p = ulong 0
174n = ulong 0
175for k ulong 3 unroll
176r = rem z 3
177z = div z 3
178p = or p shl shr r 1 k
179n = or n shl shr sub 2 r 1 k
180end
181ret Trit32mk or shl p 32 n
182end
183
184note Constructive 3-trit hypercube decoder; returns 0..15 or 99 if outside [-7, +8].
185fn hc3dec_w wa ulong to ulong
186requires eq and wa 0xfffffff8fffffff8 0
187p = shr wa 32
188n = and wa M32
189u = sub long and p 1 long and n 1
190u = add u mul 3 sub long and shr p 1 1 long and shr n 1 1
191u = add u mul 9 sub long and shr p 2 1 long and shr n 2 1
192if or lt u -7 gt u 8
193ret 99
194end
195ret ulong add u 7
196end
197
198fn hc3dec a Trit32 to ulong
199ret hc3dec_w open a
200end
201
202note Sylvester Walsh-Hadamard H16 biorthogonal wave encoder (32 codewords, d_H = 8).
203fn h16enc sym ulong to Trit32
204requires lt sym 32
205r = and sym 15
206pol = shr sym 4
207p = ulong 0
208n = ulong 0
209for c ulong 16
210pc = ulong and pop32 uint and r c 1
211par = xor pc pol
212ib = shl ulong 1 c
213if eq par 0
214p = or p ib
215else
216n = or n ib
217end
218end
219ret Trit32mk or shl p 32 n
220end
221
222note Branchless-metric H16 decoder: corrects all 0..3 carrier corruptions (flips or erasures)
223note and fail-closed returns -1 on >= 4-carrier corruptions or midpoint ties.
224fn h16dec rx Trit32 to long
225bestd = long 99
226bests = long -1
227ties = long 0
228for s ulong 32
229cw = h16enc s
230pr = open t32mul rx cw
231mt = pop32 uint and shr pr 32 M16
232d = sub 16 mt
233if lt d bestd
234bestd = d
235bests = long s
236ties = 1
237else
238if eq d bestd
239ties = add ties 1
240end
241end
242end
243if and le bestd 3 eq ties 1
244ret bests
245end
246ret -1
247end
248
249note Pack full 32-carrier job wave from decoded H16 symbol sym < 32.
250fn job_wave sym ulong to Trit32
251requires lt sym 32
252lo = open h16enc sym
253hi = open h16enc xor sym 21
254plo = and shr lo 32 M16
255nlo = and lo M16
256phi = shl and shr hi 32 M16 16
257nhi = shl and hi M16 16
258ret Trit32mk or shl or plo phi 32 or nlo nhi
259end
260
261note Deterministic 32-trit operator bank row r in 0..3.
262fn wbank_row r ulong to Trit32
263requires lt r 4
264p = ulong 0
265n = ulong 0
266for i ulong 32
267rm = rem add add mul 3 r i 1 3
268ib = shl ulong 1 i
269if eq rm 2
270p = or p ib
271else
272if eq rm 0
273n = or n ib
274end
275end
276end
277ret Trit32mk or shl p 32 n
278end
279
280note Weighted harmonic inner-product sum across the 4-row balanced-ternary bank.
281fn wave_sum jw Trit32 to long
282ensures and ge result -1280 le result 1280
283d0 = t32dot wbank_row 0 jw
284d1 = t32dot wbank_row 1 jw
285d2 = t32dot wbank_row 2 jw
286d3 = t32dot wbank_row 3 jw
287acc = add d0 mul 3 d1
288acc = add acc mul 9 d2
289acc = add acc mul 27 d3
290ret acc
291end
292
293note Scaled wave evaluation; checked signed mul traps 1 (exit 101) if scale overflows.
294fn wave_eval jw Trit32 sc long to long
295ws = wave_sum jw
296ret mul sc ws
297end
298
299note Two's-complement conversion of signed long to unsigned 64-bit word without trap 5.
300fn u64_tc x long to ulong
301if ge x 0
302ret ulong x
303end
304xp = neg add x 1
305ret sub 0xffffffffffffffff ulong xp
306end
307
308fn i64_tc u ulong to long
309if lt u 0x8000000000000000
310ret long u
311end
312up = sub 0xffffffffffffffff u
313ret sub neg long up 1
314end
315
316fn wr64le b edit uchar * o ulong u ulong to void
317for i ulong 8 unroll
318put b add o i uchar and shr u mul i 8 0xff
319end
320end
321
322fn rd64le b view uchar * o ulong to ulong
323u = ulong 0
324for i ulong 8 unroll
325u = or u shl ulong get b add o i mul i 8
326end
327ret u
328end
329
330fn rd64be b view uchar * o ulong to ulong
331u = ulong 0
332for i ulong 8 unroll
333u = or shl u 8 ulong get b add o i
334end
335ret u
336end
337
338fn wr32le b edit uchar * o ulong u uint to void
339for i ulong 4 unroll
340put b add o i uchar and shr u uint mul i 8 0xff
341end
342end
343
344fn rd32le b view uchar * o ulong to uint
345u = uint 0
346for i ulong 4 unroll
347u = or u shl uint get b add o i uint mul i 8
348end
349ret u
350end
351
352note FIPS 180-4 SHA-256 round constants K[0..63].
353fn wskinit k edit uint 64 to void
354put k 0 0x428a2f98
355put k 1 0x71374491
356put k 2 0xb5c0fbcf
357put k 3 0xe9b5dba5
358put k 4 0x3956c25b
359put k 5 0x59f111f1
360put k 6 0x923f82a4
361put k 7 0xab1c5ed5
362put k 8 0xd807aa98
363put k 9 0x12835b01
364put k 10 0x243185be
365put k 11 0x550c7dc3
366put k 12 0x72be5d74
367put k 13 0x80deb1fe
368put k 14 0x9bdc06a7
369put k 15 0xc19bf174
370put k 16 0xe49b69c1
371put k 17 0xefbe4786
372put k 18 0x0fc19dc6
373put k 19 0x240ca1cc
374put k 20 0x2de92c6f
375put k 21 0x4a7484aa
376put k 22 0x5cb0a9dc
377put k 23 0x76f988da
378put k 24 0x983e5152
379put k 25 0xa831c66d
380put k 26 0xb00327c8
381put k 27 0xbf597fc7
382put k 28 0xc6e00bf3
383put k 29 0xd5a79147
384put k 30 0x06ca6351
385put k 31 0x14292967
386put k 32 0x27b70a85
387put k 33 0x2e1b2138
388put k 34 0x4d2c6dfc
389put k 35 0x53380d13
390put k 36 0x650a7354
391put k 37 0x766a0abb
392put k 38 0x81c2c92e
393put k 39 0x92722c85
394put k 40 0xa2bfe8a1
395put k 41 0xa81a664b
396put k 42 0xc24b8b70
397put k 43 0xc76c51a3
398put k 44 0xd192e819
399put k 45 0xd6990624
400put k 46 0xf40e3585
401put k 47 0x106aa070
402put k 48 0x19a4c116
403put k 49 0x1e376c08
404put k 50 0x2748774c
405put k 51 0x34b0bcb5
406put k 52 0x391c0cb3
407put k 53 0x4ed8aa4a
408put k 54 0x5b9cca4f
409put k 55 0x682e6ff3
410put k 56 0x748f82ee
411put k 57 0x78a5636f
412put k 58 0x84c87814
413put k 59 0x8cc70208
414put k 60 0x90befffa
415put k 61 0xa4506ceb
416put k 62 0xbef9a3f7
417put k 63 0xc67178f2
418end
419
420fn wsiv s edit uint 8 to void
421put s 0 0x6a09e667
422put s 1 0xbb67ae85
423put s 2 0x3c6ef372
424put s 3 0xa54ff53a
425put s 4 0x510e527f
426put s 5 0x9b05688c
427put s 6 0x1f83d9ab
428put s 7 0x5be0cd19
429end
430
431fn wssched w edit uint 64 fresh m view uchar * o ulong to void
432q = ulong 0
433x = uint 0
434y = uint 0
435for i ulong 16 unroll
436q = add o mul i 4
437x = uint get m q
438x = or shl x 8 uint get m add q 1
439x = or shl x 8 uint get m add q 2
440put w i or shl x 8 uint get m add q 3
441end
442for i ulong 48 unroll
443x = get w add i 1
444y = get w add i 14
445x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
446put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
447end
448end
449
450fn wsround s edit uint 8 w view uint 64 kt view uint 64 to void
451a = get s 0
452b = get s 1
453c = get s 2
454d = get s 3
455e = get s 4
456f = get s 5
457g = get s 6
458h = get s 7
459t = uint 0
460for i ulong 64 unroll
461h = add h get kt i
462h = add h get w i
463t = xor rotr e 6 rotr e 11
464t = xor t rotr e 25
465h = add h t
466h = add h xor g and e xor f g
467d = add d h
468t = xor rotr a 2 rotr a 13
469t = xor t rotr a 22
470h = add h t
471t = and c or a b
472h = add h or and a b t
473t = h
474h = g
475g = f
476f = e
477e = d
478d = c
479c = b
480b = a
481a = t
482end
483put s 0 add get s 0 a
484put s 1 add get s 1 b
485put s 2 add get s 2 c
486put s 3 add get s 3 d
487put s 4 add get s 4 e
488put s 5 add get s 5 f
489put s 6 add get s 6 g
490put s 7 add get s 7 h
491end
492
493fn wscomp s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
494wssched w m o
495wsround s w kt
496end
497
498fn wsout dg edit uchar 32 s view uint 8 to void
499for i ulong 8 unroll
500v = get s i
501o = mul i 4
502put dg o uchar and shr v 24 0xff
503put dg add o 1 uchar and shr v 16 0xff
504put dg add o 2 uchar and shr v 8 0xff
505put dg add o 3 uchar and v 0xff
506end
507end
508
509fn wsfin s edit uint 8 w edit uint 64 kt view uint 64 pad edit uchar 128 n ulong to void
510remb = and n 63
511put pad remb 0x80
512eb = ulong 64
513if ge remb 56
514eb = 128
515end
516bits = shl n 3
517for i ulong 8 unroll
518put pad sub eb add i 1 uchar and shr bits mul i 8 0xff
519end
520wscomp s w kt view pad 0
521if eq eb 128
522wscomp s w kt view pad 64
523end
524end
525
526note Hash arbitrary slice m (up to 64 MiB per call) into 32-byte SHA-256 digest dg.
527fn sha256_slice m view uchar * dg edit uchar 32 to void
528n = len m
529s = new uint 8 0
530w = new uint 64 0
531kt = new uint 64 0
532pad = new uchar 128 0
533wsiv s
534wskinit kt
535nb = shr n 6
536for b ulong nb
537wscomp s w kt m shl b 6
538end
539remb = and n 63
540base = shl nb 6
541for i ulong remb
542put pad i get m add base i
543end
544wsfin s w kt pad n
545wsout dg s
546end