interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / algebra / trit.om

553 lines · 10811 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gpu/algebra/trit.om: pure-Omega mixed algebra core (replaces src/algebra/oma_trit.{h,c}).
3note Balanced trit encoding (H1), 64-trit bitplane blocks, H2 multiply, exact dot products,
4note ternary-weight x int8 dot product (H3), and int64 <-> balanced ternary conversion.
5note Compose with: si/directives/om2.om gpu/algebra/trit.om [z3.om pack.om quant.om] ...
6
7note Explicit return codes (matching src/algebra/oma_trit.h).
8const OMA_OK int 0
9const OMA_E_INVALID_TRIT int -1
10const OMA_E_INVALID_CODE int -2
11const OMA_E_INVALID_PLANES int -3
12const OMA_E_OVERFLOW int -4
13const OMA_E_INVALID_BYTE int -5
14const OMA_E_INVALID_Z3 int -6
15const OMA_E_ARG int -7
16const OMA_E_UNDERFLOW int -8
17
18note 2-bit trit codes: bit0 = pos plane, bit1 = neg plane; 0b11 is invalid.
19const OMA_CODE_ZERO uchar 0
20const OMA_CODE_POS uchar 1
21const OMA_CODE_NEG uchar 2
22const OMA_CODE_INVALID uchar 3
23
24const OMA_BLOCK_TRITS ulong 64
25const OMA_BLOCK_BYTES ulong 16
26const OMA_DENSE_TRITS_PER_BYTE ulong 5
27const OMA_DENSE_BYTE_LIMIT int 243
28const OMA_DOT_I8_MAX_N ulong 16777215
29const OMA_INT64_MAX_DIGITS ulong 41
30
31const OMA_U64_ALL ulong 0xffffffffffffffff
32const OMA_I64_MAX long 9223372036854775807
33
34note All algebra structs declared before first fn (required by omc).
35struct OmaBlock pos ulong negp ulong end
36struct OmaZ3Block one ulong two ulong end
37struct OmaVal rc int val long end
38struct OmaAddRes rc int sum int carry int end
39struct OmaQuantRes rc int scale f32 end
40struct OmaRelL2Res rc int scale f32 err f64 end
41
42note ---- bit helpers ----
43
44fn oma_popcount64 x ulong to int
45v = sub x and shr x 1 0x5555555555555555
46v = add and v 0x3333333333333333 and shr v 2 0x3333333333333333
47v = and add v shr v 4 0x0f0f0f0f0f0f0f0f
48p = mul v 0x0101010101010101
49ret int shr p 56
50end
51
52fn oma_ctz64 x ulong to ulong
53if eq x 0
54ret ulong 64
55end
56n = ulong 0
57m = x
58while eq and m 1 0 bound 64
59m = shr m 1
60n = add n 1
61end
62ret n
63end
64
65fn oma_i64_min to long
66ret sub long -9223372036854775807 long 1
67end
68
69fn oma_add_i64 a long b long res edit OmaVal to int
70i64max = OMA_I64_MAX
71i64min = oma_i64_min
72if gt b 0
73if gt a sub i64max b
74put res rc OMA_E_OVERFLOW
75ret OMA_E_OVERFLOW
76end
77end
78if lt b 0
79if lt a sub i64min b
80put res rc OMA_E_OVERFLOW
81ret OMA_E_OVERFLOW
82end
83end
84put res val add a b
85put res rc OMA_OK
86ret OMA_OK
87end
88
89note ---- single trit and 2-bit code ops ----
90
91fn oma_trit_make v int res edit OmaVal to int
92if or lt v -1 gt v 1
93put res rc OMA_E_INVALID_TRIT
94ret OMA_E_INVALID_TRIT
95end
96put res val long v
97put res rc OMA_OK
98ret OMA_OK
99end
100
101fn oma_trit_to_code t int res edit OmaVal to int
102if eq t 0
103put res val long 0
104put res rc OMA_OK
105ret OMA_OK
106end
107if eq t 1
108put res val long 1
109put res rc OMA_OK
110ret OMA_OK
111end
112if eq t -1
113put res val long 2
114put res rc OMA_OK
115ret OMA_OK
116end
117put res rc OMA_E_INVALID_TRIT
118ret OMA_E_INVALID_TRIT
119end
120
121fn oma_code_to_trit code int res edit OmaVal to int
122if or lt code 0 gt code 2
123put res rc OMA_E_INVALID_CODE
124ret OMA_E_INVALID_CODE
125end
126p = and code 1
127n = and shr code 1 1
128put res val long sub p n
129put res rc OMA_OK
130ret OMA_OK
131end
132
133fn oma_code_neg a int res edit OmaVal to int
134if or lt a 0 gt a 2
135put res rc OMA_E_INVALID_CODE
136ret OMA_E_INVALID_CODE
137end
138r = or shl and a 1 1 and shr a 1 1
139put res val long r
140put res rc OMA_OK
141ret OMA_OK
142end
143
144fn oma_block_validate b view OmaBlock to int
145if ne and get b pos get b negp 0
146ret OMA_E_INVALID_PLANES
147end
148ret OMA_OK
149end
150
151fn oma_block_add a view OmaBlock b view OmaBlock s edit OmaBlock c edit OmaBlock to int
152rc = oma_block_validate a
153if ne rc OMA_OK
154ret rc
155end
156rc = oma_block_validate b
157if ne rc OMA_OK
158ret rc
159end
160ap = get a pos
161an = get a negp
162bp = get b pos
163bn = get b negp
164az = xor or ap an OMA_U64_ALL
165bz = xor or bp bn OMA_U64_ALL
166t = or and ap bz and az bp
167put s pos or t and an bn
168t = or and an bz and az bn
169put s negp or t and ap bp
170put c pos and ap bp
171put c negp and an bn
172ret OMA_OK
173end
174
175fn oma_block_mul a view OmaBlock b view OmaBlock res edit OmaBlock to int
176rc = oma_block_validate a
177if ne rc OMA_OK
178ret rc
179end
180rc = oma_block_validate b
181if ne rc OMA_OK
182ret rc
183end
184ap = get a pos
185an = get a negp
186bp = get b pos
187bn = get b negp
188put res pos or and ap bp and an bn
189put res negp or and ap bn and an bp
190ret OMA_OK
191end
192
193fn oma_blk_to_code b view OmaBlock to int
194p = and get b pos 1
195n = shl and get b negp 1 1
196ret int or p n
197end
198
199fn oma_code_add a int b int res edit OmaAddRes to int
200if or or lt a 0 gt a 2 or lt b 0 gt b 2
201put res rc OMA_E_INVALID_CODE
202ret OMA_E_INVALID_CODE
203end
204ba = new OmaBlock ulong and a 1 ulong and shr a 1 1
205bb = new OmaBlock ulong and b 1 ulong and shr b 1 1
206bs = new OmaBlock 0 0
207bc = new OmaBlock 0 0
208rc = oma_block_add view ba view bb edit bs edit bc
209if ne rc OMA_OK
210put res rc rc
211ret rc
212end
213put res sum oma_blk_to_code view bs
214put res carry oma_blk_to_code view bc
215put res rc OMA_OK
216ret OMA_OK
217end
218
219fn oma_code_mul a int b int res edit OmaVal to int
220if or or lt a 0 gt a 2 or lt b 0 gt b 2
221put res rc OMA_E_INVALID_CODE
222ret OMA_E_INVALID_CODE
223end
224ba = new OmaBlock ulong and a 1 ulong and shr a 1 1
225bb = new OmaBlock ulong and b 1 ulong and shr b 1 1
226bp = new OmaBlock 0 0
227rc = oma_block_mul view ba view bb edit bp
228if ne rc OMA_OK
229put res rc rc
230ret rc
231end
232put res val long oma_blk_to_code view bp
233put res rc OMA_OK
234ret OMA_OK
235end
236
237fn oma_trit_neg a int res edit OmaVal to int
238ca = new OmaVal 0 0
239rc = oma_trit_to_code a edit ca
240if ne rc OMA_OK
241put res rc rc
242ret rc
243end
244cn = new OmaVal 0 0
245rc = oma_code_neg int get ca val edit cn
246if ne rc OMA_OK
247put res rc rc
248ret rc
249end
250ret oma_code_to_trit int get cn val res
251end
252
253fn oma_trit_add a int b int res edit OmaAddRes to int
254ca = new OmaVal 0 0
255cb = new OmaVal 0 0
256rc = oma_trit_to_code a edit ca
257if ne rc OMA_OK
258put res rc rc
259ret rc
260end
261rc = oma_trit_to_code b edit cb
262if ne rc OMA_OK
263put res rc rc
264ret rc
265end
266ar = new OmaAddRes 0 0 0
267rc = oma_code_add int get ca val int get cb val edit ar
268if ne rc OMA_OK
269put res rc rc
270ret rc
271end
272ts = new OmaVal 0 0
273tc = new OmaVal 0 0
274rc = oma_code_to_trit get ar sum edit ts
275if ne rc OMA_OK
276put res rc rc
277ret rc
278end
279rc = oma_code_to_trit get ar carry edit tc
280if ne rc OMA_OK
281put res rc rc
282ret rc
283end
284put res sum int get ts val
285put res carry int get tc val
286put res rc OMA_OK
287ret OMA_OK
288end
289
290fn oma_trit_mul a int b int res edit OmaVal to int
291ca = new OmaVal 0 0
292cb = new OmaVal 0 0
293rc = oma_trit_to_code a edit ca
294if ne rc OMA_OK
295put res rc rc
296ret rc
297end
298rc = oma_trit_to_code b edit cb
299if ne rc OMA_OK
300put res rc rc
301ret rc
302end
303cm = new OmaVal 0 0
304rc = oma_code_mul int get ca val int get cb val edit cm
305if ne rc OMA_OK
306put res rc rc
307ret rc
308end
309ret oma_code_to_trit int get cm val res
310end
311
312note ---- 64-trit block ops ----
313
314fn oma_block_encode inp view byte 64 res edit OmaBlock to int
315p = ulong 0
316n = ulong 0
317for i ulong 64
318t = int get inp i
319if eq t 1
320p = or p shl ulong 1 i
321else
322if eq t -1
323n = or n shl ulong 1 i
324else
325if ne t 0
326ret OMA_E_INVALID_TRIT
327end
328end
329end
330end
331put res pos p
332put res negp n
333ret OMA_OK
334end
335
336fn oma_block_decode b view OmaBlock res edit byte 64 to int
337rc = oma_block_validate b
338if ne rc OMA_OK
339ret rc
340end
341bp = get b pos
342bn = get b negp
343for i ulong 64
344pv = int and shr bp i 1
345nv = int and shr bn i 1
346put res i byte sub pv nv
347end
348ret OMA_OK
349end
350
351fn oma_block_neg a view OmaBlock res edit OmaBlock to int
352rc = oma_block_validate a
353if ne rc OMA_OK
354ret rc
355end
356ap = get a pos
357an = get a negp
358put res pos an
359put res negp ap
360ret OMA_OK
361end
362
363fn oma_block_dot a view OmaBlock b view OmaBlock res edit OmaVal to int
364p = new OmaBlock 0 0
365rc = oma_block_mul a b edit p
366if ne rc OMA_OK
367put res rc rc
368ret rc
369end
370d = sub oma_popcount64 get p pos oma_popcount64 get p negp
371put res val long d
372put res rc OMA_OK
373ret OMA_OK
374end
375
376note ---- ternary weights x int8 activations (H3, add/sub only) ----
377note w_planes holds 2*ceil(n/64) ulongs: [pos_0, neg_0, pos_1, neg_1, ...].
378
379fn oma_dot_tw_i8_blk wp ulong wn ulong x view byte * base ulong to int
380acc = int 0
381m = wp
382while ne m 0 bound 64
383idx = oma_ctz64 m
384acc = add acc int get x add base idx
385m = and m sub m 1
386end
387m = wn
388while ne m 0 bound 64
389idx = oma_ctz64 m
390acc = sub acc int get x add base idx
391m = and m sub m 1
392end
393ret acc
394end
395
396fn oma_check_planes_live wp ulong wn ulong n ulong base ulong to int
397if ne and wp wn 0
398ret OMA_E_INVALID_PLANES
399end
400rem_l = sub n base
401if lt rem_l 64
402live = sub shl ulong 1 rem_l 1
403if ne and or wp wn xor live OMA_U64_ALL 0
404ret OMA_E_ARG
405end
406end
407ret OMA_OK
408end
409
410fn oma_dot_tw_i8 w_planes view ulong * x view byte * n ulong res edit OmaVal to int
411if gt n OMA_DOT_I8_MAX_N
412put res rc OMA_E_OVERFLOW
413ret OMA_E_OVERFLOW
414end
415if gt n len x
416put res rc OMA_E_ARG
417ret OMA_E_ARG
418end
419nb = div add n 63 64
420if lt len w_planes mul nb 2
421put res rc OMA_E_ARG
422ret OMA_E_ARG
423end
424acc = int 0
425k = ulong 0
426while lt k nb bound 262144
427wp = get w_planes mul k 2
428wn = get w_planes add mul k 2 1
429base = mul k 64
430rc = oma_check_planes_live wp wn n base
431if ne rc OMA_OK
432put res rc rc
433ret rc
434end
435acc = add acc oma_dot_tw_i8_blk wp wn x base
436k = add k 1
437end
438put res val long acc
439put res rc OMA_OK
440ret OMA_OK
441end
442
443note ---- int64 <-> balanced ternary (little-endian) ----
444
445fn oma_int_to_bt v long digits edit byte * ndigits edit OmaVal to int
446tmp = new byte 41 0
447n = ulong 0
448u = ulong 0
449if lt v 0
450if eq v oma_i64_min
451u = ulong 0x8000000000000000
452else
453u = ulong neg v
454end
455else
456u = ulong v
457end
458while ne u 0 bound 42
459r = rem u 3
460u = div u 3
461if eq r 2
462put tmp n byte -1
463u = add u 1
464else
465put tmp n byte int r
466end
467n = add n 1
468end
469if gt n len digits
470put ndigits rc OMA_E_OVERFLOW
471ret OMA_E_OVERFLOW
472end
473i = ulong 0
474while lt i n bound 42
475d = int get tmp i
476if lt v 0
477d = neg d
478end
479put digits i byte d
480i = add i 1
481end
482put ndigits val long n
483put ndigits rc OMA_OK
484ret OMA_OK
485end
486
487fn oma_int_to_bt_fixed v long digits edit byte * n ulong to int
488if gt n len digits
489ret OMA_E_ARG
490end
491tmp = new byte 41 0
492used = new OmaVal 0 0
493rc = oma_int_to_bt v edit tmp edit used
494if ne rc OMA_OK
495ret rc
496end
497un = ulong get used val
498if gt un n
499ret OMA_E_OVERFLOW
500end
501i = ulong 0
502while lt i un bound 42
503put digits i get tmp i
504i = add i 1
505end
506while lt i n bound 1024
507put digits i byte 0
508i = add i 1
509end
510ret OMA_OK
511end
512
513fn oma_bt_to_int digits view byte * n ulong res edit OmaVal to int
514if gt n len digits
515put res rc OMA_E_ARG
516ret OMA_E_ARG
517end
518i = ulong 0
519while lt i n bound 1024
520d = int get digits i
521if or lt d -1 gt d 1
522put res rc OMA_E_INVALID_TRIT
523ret OMA_E_INVALID_TRIT
524end
525i = add i 1
526end
527acc = long 0
528t = new OmaVal 0 0
529rem_i = n
530while gt rem_i 0 bound 1024
531rem_i = sub rem_i 1
532dv = long int get digits rem_i
533rc = oma_add_i64 acc dv edit t
534if ne rc OMA_OK
535put res rc OMA_E_OVERFLOW
536ret OMA_E_OVERFLOW
537end
538rc = oma_add_i64 get t val acc edit t
539if ne rc OMA_OK
540put res rc OMA_E_OVERFLOW
541ret OMA_E_OVERFLOW
542end
543rc = oma_add_i64 get t val acc edit t
544if ne rc OMA_OK
545put res rc OMA_E_OVERFLOW
546ret OMA_E_OVERFLOW
547end
548acc = get t val
549end
550put res val acc
551put res rc OMA_OK
552ret OMA_OK
553end