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