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/aienux/crates/allen-reflex/reflex.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 / aienux / crates / allen-reflex / reflex.om` 4287 lines ยท 83003 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 note 4 note aienux/crates/allen-reflex/reflex.om: ALLEN REFLEX v0 core in pure OM-2. 5 note Library unit (no om2 header line, no fn main). Prefix: arx_. 6 note Ports: action, dialog, text, lexicon, segment, policy, features, hv, span, model, lib. 7 8 note ========================================================================= 9 note Section 1: Constants and Global State 10 note ========================================================================= 11 12 const ARX_N_LABELS ulong 15 13 const ARX_ACT_STATUS uchar 0 14 const ARX_ACT_HELP uchar 1 15 const ARX_ACT_EVIDENCE uchar 2 16 const ARX_ACT_RECALL uchar 3 17 const ARX_ACT_FACTS uchar 4 18 const ARX_ACT_REMEMBER uchar 5 19 const ARX_ACT_PREFER uchar 6 20 const ARX_ACT_TASK uchar 7 21 const ARX_ACT_PROMPT uchar 8 22 const ARX_ACT_APPROVE uchar 9 23 const ARX_ACT_DENY uchar 10 24 const ARX_ACT_CANCEL uchar 11 25 const ARX_ACT_NONE uchar 12 26 const ARX_ACT_CLARIFY uchar 13 27 const ARX_ACT_REFUSE uchar 14 28 const ARX_ACT_INVALID uchar 255 29 30 const ARX_MODE_REFLEX uchar 0 31 const ARX_MODE_NEAREST uchar 1 32 const ARX_MODE_PERCEPTRON uchar 2 33 34 const ARX_ROLE_USER uchar 0 35 const ARX_ROLE_ASST uchar 1 36 37 const ARX_PEND_UNKNOWN uchar 0 38 const ARX_PEND_YES uchar 1 39 const ARX_PEND_NO uchar 2 40 41 const ARX_GAP_STOP uchar 1 42 const ARX_GAP_COMMA uchar 2 43 const ARX_GAP_COLON uchar 4 44 const ARX_GAP_SEMI uchar 8 45 const ARX_GAP_DASH uchar 16 46 const ARX_GAP_QUESTION uchar 32 47 48 const ARX_REFUSE_NONE uchar 0 49 const ARX_REFUSE_RULE_BREAK uchar 1 50 const ARX_REFUSE_OVERRIDE uchar 2 51 const ARX_REFUSE_FORBIDDEN uchar 3 52 const ARX_REFUSE_BLANKET uchar 4 53 const ARX_REFUSE_DATA_ORDER uchar 5 54 55 const ARX_WHY_NO_USER_TURN uchar 0 56 const ARX_WHY_INPUT_TOO_LONG uchar 1 57 const ARX_WHY_NO_WORDS uchar 2 58 const ARX_WHY_POLICY uchar 3 59 const ARX_WHY_EXACT_RECALL uchar 4 60 const ARX_WHY_SCORED uchar 5 61 const ARX_WHY_LOW_SIMILARITY uchar 6 62 const ARX_WHY_LOW_MARGIN uchar 7 63 const ARX_WHY_LOW_AGREEMENT uchar 8 64 const ARX_WHY_SPAN uchar 9 65 66 const ARX_SPAN_OK uchar 0 67 const ARX_SPAN_EMPTY uchar 1 68 const ARX_SPAN_TOO_LONG uchar 2 69 const ARX_SPAN_UNREP uchar 3 70 71 const ARX_LEARN_OK uchar 0 72 const ARX_LEARN_OFF uchar 1 73 const ARX_LEARN_FULL uchar 2 74 const ARX_LEARN_NO_USER uchar 3 75 76 const ARX_SYM_WORD uchar 1 77 const ARX_SYM_CHAR uchar 2 78 const ARX_SYM_CONCEPT uchar 3 79 const ARX_SYM_CTX uchar 4 80 const ARX_SYM_SHAPE uchar 5 81 const ARX_SYM_LEAD uchar 6 82 83 const ARX_CH_WORD ulong 0 84 const ARX_CH_BIGRAM ulong 1 85 const ARX_CH_CHARS ulong 2 86 const ARX_CH_CONCEPT ulong 3 87 const ARX_CH_LEAD ulong 4 88 const ARX_CH_CTX ulong 5 89 const ARX_CH_CTXCONJ ulong 6 90 const ARX_CH_SHAPE ulong 7 91 const ARX_N_CHANNELS ulong 8 92 const ARX_N_CONCEPTS ulong 18 93 94 const ARX_SEED ulong 0x414c4c454e524546 95 const ARX_HV_WORDS ulong 128 96 const ARX_HV_BITS ulong 8192 97 const ARX_MAX_EXEMPLARS ulong 256 98 const ARX_MAX_CONFLICTS ulong 32 99 const ARX_NOTE_BYTES ulong 96 100 101 const ARX_MAX_TURNS_CAP ulong 32 102 const ARX_TURN_STRIDE ulong 512 103 const ARX_MAX_MSG_CAP ulong 4096 104 const ARX_MAX_TOKENS ulong 256 105 const ARX_TOK_STRIDE ulong 64 106 const ARX_MAX_CLAUSES ulong 64 107 const ARX_MAX_FEATS ulong 768 108 109 const ARX_MAX_CUES ulong 256 110 const ARX_MAX_CUE_WORDS ulong 8 111 const ARX_MAX_STATS ulong 2048 112 const ARX_MAX_TEXT ulong 48 113 114 const ARX_SPARSE_POOL ulong 65536 115 const ARX_PERC_CAP ulong 8192 116 117 note --- Configuration globals --- 118 global arx_cfg_mode uchar 1 119 global arx_cfg_weights ushort 8 120 global arx_cfg_hv_words ulong 1 121 global arx_cfg_hv_k ulong 1 122 global arx_cfg_hv_alpha f32 1 123 global arx_cfg_hv_zband f32 1 124 global arx_cfg_hv_epochs uint 1 125 global arx_cfg_hv_eta f32 1 126 global arx_cfg_max_ex ulong 1 127 global arx_cfg_t_margin f32 1 128 global arx_cfg_t_abs f32 1 129 global arx_cfg_abs_label uchar 1 130 global arx_cfg_t_agree f32 1 131 global arx_cfg_exact f32 1 132 global arx_cfg_perc_epochs uint 1 133 global arx_cfg_sleep_dup f32 1 134 global arx_cfg_sleep_con f32 1 135 global arx_cfg_max_msg ulong 1 136 global arx_cfg_max_toks ulong 1 137 global arx_cfg_max_turns ulong 1 138 global arx_learning uchar 1 139 140 note --- Turn buffer globals --- 141 global arx_turn_n ulong 1 142 global arx_turn_role uchar 32 143 global arx_turn_buf uchar 16384 144 global arx_turn_len ulong 32 145 global arx_turn_raw_len ulong 32 146 147 note --- Dialog state globals --- 148 global arx_dlg_pending uchar 1 149 global arx_dlg_prev uchar 1 150 global arx_dlg_has_user uchar 1 151 global arx_dlg_last_user ulong 1 152 global arx_dlg_msg uchar 4096 153 global arx_dlg_msg_len ulong 1 154 155 note --- Token globals --- 156 global arx_tok_n ulong 1 157 global arx_tok_start ulong 256 158 global arx_tok_end ulong 256 159 global arx_tok_gap uchar 256 160 global arx_tok_alnum uchar 256 161 global arx_tok_word uchar 16384 162 global arx_tok_wlen ulong 256 163 global arx_tok_stem uchar 16384 164 global arx_tok_slen ulong 256 165 global arx_tok_whash ulong 256 166 global arx_tok_shash ulong 256 167 168 note --- Clause globals --- 169 global arx_cl_n ulong 1 170 global arx_cl_start ulong 64 171 global arx_cl_end ulong 64 172 global arx_pri_start ulong 1 173 global arx_pri_end ulong 1 174 175 note --- Policy scratch and result --- 176 global arx_pol_exempt uchar 256 177 global arx_pol_rule uchar 1 178 global arx_pol_tok ulong 1 179 180 note --- Feature globals --- 181 global arx_ft_n ulong 1 182 global arx_ft_fam uchar 3072 183 global arx_ft_val ulong 3072 184 global arx_ft_len uchar 768 185 global arx_ft_ch uchar 768 186 global arx_ft_w ushort 768 187 188 note --- HV scratch and model globals --- 189 global arx_hv_cnt int 8192 190 global arx_hv_acc ulong 128 191 global arx_hv_tmp ulong 128 192 global arx_hv_q ulong 128 193 194 global arx_hv_n ulong 1 195 global arx_hv_hvs ulong 32768 196 global arx_hv_labels uchar 256 197 global arx_hv_notes uchar 24576 198 global arx_hv_nlen ulong 256 199 global arx_hv_counts int 122880 200 global arx_hv_sign ulong 1920 201 global arx_hv_mask ulong 1920 202 global arx_hv_nnz uint 15 203 global arx_hv_nlabel uint 15 204 global arx_hv_keep uchar 256 205 206 note --- Sleep report globals --- 207 global arx_slp_merged ulong 1 208 global arx_slp_kept ulong 1 209 global arx_slp_passes uint 1 210 global arx_slp_con_n ulong 1 211 global arx_slp_con_a ulong 32 212 global arx_slp_con_b ulong 32 213 global arx_slp_con_sim f32 32 214 215 note --- Sparse & NearestModel globals --- 216 global arx_sp_q_k ulong 768 217 global arx_sp_q_w uint 768 218 global arx_sp_q_n ulong 1 219 220 global arx_nr_n ulong 1 221 global arx_nr_off ulong 256 222 global arx_nr_len ulong 256 223 global arx_nr_labels uchar 256 224 global arx_nr_notes uchar 24576 225 global arx_nr_nlen ulong 256 226 global arx_nr_nlabel uint 15 227 global arx_nr_pool_n ulong 1 228 global arx_nr_pool_k ulong 65536 229 global arx_nr_pool_w uint 65536 230 231 note --- PerceptronModel globals --- 232 global arx_pc_w_used uchar 8192 233 global arx_pc_w_key ulong 8192 234 global arx_pc_w_val f32 122880 235 global arx_pc_w_cnt ulong 1 236 global arx_pc_u_used uchar 8192 237 global arx_pc_u_key ulong 8192 238 global arx_pc_u_val f32 122880 239 global arx_pc_u_cnt ulong 1 240 global arx_pc_tr_n ulong 1 241 global arx_pc_tr_off ulong 256 242 global arx_pc_tr_len ulong 256 243 global arx_pc_tr_lbl uchar 256 244 global arx_pc_nlabel uint 15 245 global arx_pc_pool_n ulong 1 246 global arx_pc_pool_k ulong 65536 247 global arx_pc_pool_w uint 65536 248 global arx_pc_order ulong 256 249 250 note --- Scorer output globals --- 251 global arx_sc_s f32 15 252 global arx_sc_best_sim f32 1 253 global arx_sc_has_near uchar 1 254 global arx_sc_nearest ulong 1 255 global arx_sc_agree f32 1 256 257 note --- CueMemory globals --- 258 global arx_cue_pre_n ulong 1 259 global arx_cue_pre_act uchar 256 260 global arx_cue_pre_len uchar 256 261 global arx_cue_pre_h ulong 2048 262 263 global arx_cue_suf_n ulong 1 264 global arx_cue_suf_act uchar 256 265 global arx_cue_suf_len uchar 256 266 global arx_cue_suf_h ulong 2048 267 268 global arx_cue_st_n ulong 1 269 global arx_cue_st_act uchar 2048 270 global arx_cue_st_h ulong 2048 271 global arx_cue_st_c ushort 2048 272 global arx_cue_st_p ushort 2048 273 274 note --- Decision & Explanation output globals --- 275 global arx_dec_act uchar 1 276 global arx_dec_has_txt uchar 1 277 global arx_dec_txt uchar 64 278 global arx_dec_txt_len ulong 1 279 280 global arx_exp_why uchar 1 281 global arx_exp_refuse uchar 1 282 global arx_exp_span_err uchar 1 283 global arx_exp_top uchar 1 284 global arx_exp_second uchar 1 285 global arx_exp_margin f32 1 286 global arx_exp_best_sim f32 1 287 global arx_exp_agree f32 1 288 global arx_exp_has_near uchar 1 289 global arx_exp_near_act uchar 1 290 global arx_exp_near_idx ulong 1 291 global arx_exp_span_bs ulong 1 292 global arx_exp_span_be ulong 1 293 294 note ========================================================================= 295 note Section 2: Action Helpers 296 note ========================================================================= 297 298 text arx_s_act_0 "status" 299 text arx_s_act_1 "help" 300 text arx_s_act_2 "evidence" 301 text arx_s_act_3 "recall" 302 text arx_s_act_4 "facts" 303 text arx_s_act_5 "remember" 304 text arx_s_act_6 "prefer" 305 text arx_s_act_7 "task" 306 text arx_s_act_8 "prompt" 307 text arx_s_act_9 "approve" 308 text arx_s_act_10 "deny" 309 text arx_s_act_11 "cancel" 310 text arx_s_act_12 "none" 311 text arx_s_act_13 "clarify" 312 text arx_s_act_14 "refuse" 313 314 fn arx_copy_bytes dst edit uchar * pos ulong src view uchar * to ulong 315 n = len src 316 i = ulong 0 317 while lt i n bound 4096 318 put dst add pos i get src i 319 i = add i 1 320 end 321 ret add pos n 322 end 323 324 fn arx_eq_bytes a view uchar * b view uchar * to bool 325 na = len a 326 if ne na len b 327 ret false 328 end 329 i = ulong 0 330 while lt i na bound 4096 331 if ne get a i get b i 332 ret false 333 end 334 i = add i 1 335 end 336 ret true 337 end 338 339 fn arx_act_write_name act uchar dst edit uchar * pos ulong to ulong 340 if eq act ARX_ACT_STATUS 341 ret arx_copy_bytes dst pos arx_s_act_0 342 end 343 if eq act ARX_ACT_HELP 344 ret arx_copy_bytes dst pos arx_s_act_1 345 end 346 if eq act ARX_ACT_EVIDENCE 347 ret arx_copy_bytes dst pos arx_s_act_2 348 end 349 if eq act ARX_ACT_RECALL 350 ret arx_copy_bytes dst pos arx_s_act_3 351 end 352 if eq act ARX_ACT_FACTS 353 ret arx_copy_bytes dst pos arx_s_act_4 354 end 355 if eq act ARX_ACT_REMEMBER 356 ret arx_copy_bytes dst pos arx_s_act_5 357 end 358 if eq act ARX_ACT_PREFER 359 ret arx_copy_bytes dst pos arx_s_act_6 360 end 361 if eq act ARX_ACT_TASK 362 ret arx_copy_bytes dst pos arx_s_act_7 363 end 364 if eq act ARX_ACT_PROMPT 365 ret arx_copy_bytes dst pos arx_s_act_8 366 end 367 if eq act ARX_ACT_APPROVE 368 ret arx_copy_bytes dst pos arx_s_act_9 369 end 370 if eq act ARX_ACT_DENY 371 ret arx_copy_bytes dst pos arx_s_act_10 372 end 373 if eq act ARX_ACT_CANCEL 374 ret arx_copy_bytes dst pos arx_s_act_11 375 end 376 if eq act ARX_ACT_NONE 377 ret arx_copy_bytes dst pos arx_s_act_12 378 end 379 if eq act ARX_ACT_CLARIFY 380 ret arx_copy_bytes dst pos arx_s_act_13 381 end 382 if eq act ARX_ACT_REFUSE 383 ret arx_copy_bytes dst pos arx_s_act_14 384 end 385 ret arx_copy_bytes dst pos arx_s_act_13 386 end 387 388 fn arx_act_from_name s view uchar * to uchar 389 if arx_eq_bytes s arx_s_act_0 390 ret ARX_ACT_STATUS 391 end 392 if arx_eq_bytes s arx_s_act_1 393 ret ARX_ACT_HELP 394 end 395 if arx_eq_bytes s arx_s_act_2 396 ret ARX_ACT_EVIDENCE 397 end 398 if arx_eq_bytes s arx_s_act_3 399 ret ARX_ACT_RECALL 400 end 401 if arx_eq_bytes s arx_s_act_4 402 ret ARX_ACT_FACTS 403 end 404 if arx_eq_bytes s arx_s_act_5 405 ret ARX_ACT_REMEMBER 406 end 407 if arx_eq_bytes s arx_s_act_6 408 ret ARX_ACT_PREFER 409 end 410 if arx_eq_bytes s arx_s_act_7 411 ret ARX_ACT_TASK 412 end 413 if arx_eq_bytes s arx_s_act_8 414 ret ARX_ACT_PROMPT 415 end 416 if arx_eq_bytes s arx_s_act_9 417 ret ARX_ACT_APPROVE 418 end 419 if arx_eq_bytes s arx_s_act_10 420 ret ARX_ACT_DENY 421 end 422 if arx_eq_bytes s arx_s_act_11 423 ret ARX_ACT_CANCEL 424 end 425 if arx_eq_bytes s arx_s_act_12 426 ret ARX_ACT_NONE 427 end 428 if arx_eq_bytes s arx_s_act_13 429 ret ARX_ACT_CLARIFY 430 end 431 if arx_eq_bytes s arx_s_act_14 432 ret ARX_ACT_REFUSE 433 end 434 ret ARX_ACT_INVALID 435 end 436 437 fn arx_act_has_text act uchar to bool 438 if eq act ARX_ACT_REMEMBER 439 ret true 440 end 441 if eq act ARX_ACT_PREFER 442 ret true 443 end 444 if eq act ARX_ACT_TASK 445 ret true 446 end 447 if eq act ARX_ACT_PROMPT 448 ret true 449 end 450 ret false 451 end 452 453 fn arx_act_needs_pending act uchar to bool 454 if eq act ARX_ACT_APPROVE 455 ret true 456 end 457 if eq act ARX_ACT_DENY 458 ret true 459 end 460 if eq act ARX_ACT_CANCEL 461 ret true 462 end 463 ret false 464 end 465 466 fn arx_act_creates_pending act uchar to bool 467 if eq act ARX_ACT_TASK 468 ret true 469 end 470 if eq act ARX_ACT_PROMPT 471 ret true 472 end 473 ret false 474 end 475 476 note ========================================================================= 477 note Section 3: Text Normalization, Tokenization, Stemming, Hashing 478 note ========================================================================= 479 480 fn arx_fnv s view uchar * to ulong 481 h = ulong 0xcbf29ce484222325 482 n = len s 483 i = ulong 0 484 while lt i n bound 4096 485 h = xor h ulong get s i 486 h = mul h ulong 0x00000100000001b3 487 i = add i 1 488 end 489 ret h 490 end 491 492 fn arx_is_space b uchar to bool 493 if eq b 32 494 ret true 495 end 496 if eq b 9 497 ret true 498 end 499 if eq b 10 500 ret true 501 end 502 if eq b 13 503 ret true 504 end 505 if eq b 12 506 ret true 507 end 508 ret false 509 end 510 511 fn arx_is_alpha b uchar to bool 512 if ge b 65 513 if le b 90 514 ret true 515 end 516 end 517 if ge b 97 518 if le b 122 519 ret true 520 end 521 end 522 ret false 523 end 524 525 fn arx_is_digit b uchar to bool 526 if ge b 48 527 if le b 57 528 ret true 529 end 530 end 531 ret false 532 end 533 534 fn arx_is_alnum b uchar to bool 535 if arx_is_alpha b 536 ret true 537 end 538 ret arx_is_digit b 539 end 540 541 fn arx_to_lower b uchar to uchar 542 if ge b 65 543 if le b 90 544 ret add b 32 545 end 546 end 547 ret b 548 end 549 550 fn arx_edge_punct b uchar to bool 551 if eq b 46 552 ret true 553 end 554 if eq b 44 555 ret true 556 end 557 if eq b 59 558 ret true 559 end 560 if eq b 58 561 ret true 562 end 563 if eq b 33 564 ret true 565 end 566 if eq b 63 567 ret true 568 end 569 if eq b 34 570 ret true 571 end 572 if eq b 39 573 ret true 574 end 575 if eq b 40 576 ret true 577 end 578 if eq b 41 579 ret true 580 end 581 if eq b 91 582 ret true 583 end 584 if eq b 93 585 ret true 586 end 587 if eq b 123 588 ret true 589 end 590 if eq b 125 591 ret true 592 end 593 if eq b 45 594 ret true 595 end 596 if eq b 96 597 ret true 598 end 599 if eq b 42 600 ret true 601 end 602 ret false 603 end 604 605 fn arx_gap_flags s view uchar * to uchar 606 f = uchar 0 607 n = len s 608 i = ulong 0 609 while lt i n bound 4096 610 b = get s i 611 if eq b 46 612 f = or f ARX_GAP_STOP 613 else 614 if eq b 33 615 f = or f ARX_GAP_STOP 616 else 617 if eq b 63 618 f = or f or ARX_GAP_STOP ARX_GAP_QUESTION 619 else 620 if eq b 44 621 f = or f ARX_GAP_COMMA 622 else 623 if eq b 58 624 f = or f ARX_GAP_COLON 625 else 626 if eq b 59 627 f = or f ARX_GAP_SEMI 628 else 629 if eq b 45 630 lok = false 631 if eq i 0 632 lok = true 633 else 634 pb = get s sub i 1 635 if or eq pb 32 eq pb 45 636 lok = true 637 end 638 end 639 rok = false 640 if eq add i 1 n 641 rok = true 642 else 643 nb = get s add i 1 644 if or eq nb 32 eq nb 45 645 rok = true 646 end 647 end 648 if and lok rok 649 f = or f ARX_GAP_DASH 650 end 651 end 652 end 653 end 654 end 655 end 656 end 657 end 658 i = add i 1 659 end 660 ret f 661 end 662 663 fn arx_lower_buf src view uchar * dst edit uchar * to ulong 664 n = len src 665 cap = len dst 666 i = ulong 0 667 j = ulong 0 668 while lt i n bound 4096 669 if ge j cap 670 break 671 end 672 b = get src i 673 curly = false 674 if eq b 0xe2 675 if lt add i 2 n 676 if eq get src add i 1 0x80 677 b2 = get src add i 2 678 if or eq b2 0x99 eq b2 0x98 679 curly = true 680 end 681 end 682 end 683 end 684 if curly 685 put dst j uchar 39 686 j = add j 1 687 i = add i 3 688 else 689 put dst j arx_to_lower b 690 j = add j 1 691 i = add i 1 692 end 693 end 694 ret j 695 end 696 697 fn arx_ends_with buf view uchar * n ulong suf view uchar * to bool 698 sn = len suf 699 if lt n sn 700 ret false 701 end 702 off = sub n sn 703 i = ulong 0 704 while lt i sn bound 64 705 if ne get buf add off i get suf i 706 ret false 707 end 708 i = add i 1 709 end 710 ret true 711 end 712 713 text arx_s_ing "ing" 714 text arx_s_ies "ies" 715 text arx_s_ed "ed" 716 text arx_s_eed "eed" 717 text arx_s_ss "ss" 718 text arx_s_us "us" 719 text arx_s_is "is" 720 721 fn arx_stem_word src view uchar * n ulong dst edit uchar * to ulong 722 all_a = true 723 i = ulong 0 724 while lt i n bound 64 725 if not arx_is_alpha get src i 726 all_a = false 727 break 728 end 729 i = add i 1 730 end 731 out_n = n 732 if all_a 733 if gt n 3 734 done = false 735 if gt n 5 736 if arx_ends_with src n arx_s_ing 737 out_n = sub n 3 738 done = true 739 end 740 end 741 if not done 742 if gt n 4 743 if arx_ends_with src n arx_s_ies 744 k = ulong 0 745 lim = sub n 3 746 while lt k lim bound 64 747 put dst k get src k 748 k = add k 1 749 end 750 put dst lim uchar 121 751 ret add lim 1 752 end 753 end 754 end 755 if not done 756 if gt n 4 757 if arx_ends_with src n arx_s_ed 758 if not arx_ends_with src n arx_s_eed 759 out_n = sub n 2 760 done = true 761 end 762 end 763 end 764 end 765 if not done 766 if eq get src sub n 1 115 767 if not arx_ends_with src n arx_s_ss 768 if not arx_ends_with src n arx_s_us 769 if not arx_ends_with src n arx_s_is 770 out_n = sub n 1 771 end 772 end 773 end 774 end 775 end 776 end 777 end 778 k = ulong 0 779 while lt k out_n bound 64 780 put dst k get src k 781 k = add k 1 782 end 783 ret out_n 784 end 785 786 text arx_s_cant "can't" 787 text arx_s_wont "won't" 788 text arx_s_nt "n't" 789 text arx_s_am_s "'m" 790 text arx_s_re_s "'re" 791 text arx_s_ll_s "'ll" 792 text arx_s_ve_s "'ve" 793 text arx_s_d_s "'d" 794 text arx_s_s_s "'s" 795 text arx_s_can "can" 796 text arx_s_will "will" 797 text arx_s_not "not" 798 text arx_s_am "am" 799 text arx_s_are "are" 800 text arx_s_have "have" 801 text arx_s_would "would" 802 803 fn arx_store_tok_words idx ulong w view uchar * to void 804 st = new uchar 64 0 805 wn = len w 806 if gt wn ARX_TOK_STRIDE 807 wn = ARX_TOK_STRIDE 808 end 809 sn = arx_stem_word w wn st 810 base = mul idx ARX_TOK_STRIDE 811 k = ulong 0 812 while lt k wn bound 64 813 put arx_tok_word add base k get w k 814 k = add k 1 815 end 816 k = ulong 0 817 while lt k sn bound 64 818 put arx_tok_stem add base k get st k 819 k = add k 1 820 end 821 put arx_tok_wlen idx wn 822 put arx_tok_slen idx sn 823 put arx_tok_whash idx arx_fnv part w 0 wn 824 put arx_tok_shash idx arx_fnv part st 0 sn 825 end 826 827 fn arx_store_tok s ulong e ulong gapv uchar alnumv uchar w view uchar * to void 828 idx = get arx_tok_n 0 829 if ge idx ARX_MAX_TOKENS 830 ret 831 end 832 put arx_tok_start idx s 833 put arx_tok_end idx e 834 put arx_tok_gap idx gapv 835 put arx_tok_alnum idx alnumv 836 arx_store_tok_words idx w 837 put arx_tok_n 0 add idx 1 838 end 839 840 fn arx_store_raw_tok s ulong e ulong w view uchar * to void 841 idx = get arx_tok_n 0 842 if ge idx ARX_MAX_TOKENS 843 ret 844 end 845 wn = len w 846 if gt wn ARX_TOK_STRIDE 847 wn = ARX_TOK_STRIDE 848 end 849 base = mul idx ARX_TOK_STRIDE 850 k = ulong 0 851 while lt k wn bound 64 852 put arx_tok_word add base k get w k 853 put arx_tok_stem add base k get w k 854 k = add k 1 855 end 856 h = arx_fnv part w 0 wn 857 put arx_tok_start idx s 858 put arx_tok_end idx e 859 put arx_tok_gap idx 0 860 put arx_tok_alnum idx 1 861 put arx_tok_wlen idx wn 862 put arx_tok_slen idx wn 863 put arx_tok_whash idx h 864 put arx_tok_shash idx h 865 put arx_tok_n 0 add idx 1 866 end 867 868 fn arx_emit_expanded s ulong e ulong gapv uchar lw view uchar * to void 869 n = len lw 870 al = uchar 0 871 k = ulong 0 872 while lt k n bound 64 873 if arx_is_alnum get lw k 874 al = 1 875 break 876 end 877 k = add k 1 878 end 879 if eq n 5 880 if arx_eq_bytes lw arx_s_cant 881 arx_store_tok s e gapv al arx_s_can 882 arx_store_raw_tok s e arx_s_not 883 ret 884 end 885 if arx_eq_bytes lw arx_s_wont 886 arx_store_tok s e gapv al arx_s_will 887 arx_store_raw_tok s e arx_s_not 888 ret 889 end 890 end 891 if gt n 3 892 if arx_ends_with lw n arx_s_nt 893 arx_store_tok s e gapv al part lw 0 sub n 3 894 arx_store_raw_tok s e arx_s_not 895 ret 896 end 897 if arx_ends_with lw n arx_s_re_s 898 arx_store_tok s e gapv al part lw 0 sub n 3 899 arx_store_raw_tok s e arx_s_are 900 ret 901 end 902 if arx_ends_with lw n arx_s_ll_s 903 arx_store_tok s e gapv al part lw 0 sub n 3 904 arx_store_raw_tok s e arx_s_will 905 ret 906 end 907 if arx_ends_with lw n arx_s_ve_s 908 arx_store_tok s e gapv al part lw 0 sub n 3 909 arx_store_raw_tok s e arx_s_have 910 ret 911 end 912 end 913 if gt n 2 914 if arx_ends_with lw n arx_s_am_s 915 arx_store_tok s e gapv al part lw 0 sub n 2 916 arx_store_raw_tok s e arx_s_am 917 ret 918 end 919 if arx_ends_with lw n arx_s_d_s 920 arx_store_tok s e gapv al part lw 0 sub n 2 921 arx_store_raw_tok s e arx_s_would 922 ret 923 end 924 if arx_ends_with lw n arx_s_s_s 925 arx_store_tok s e gapv al part lw 0 sub n 2 926 ret 927 end 928 end 929 arx_store_tok s e gapv al lw 930 end 931 932 fn arx_tok_emit_span msg view uchar * s ulong e ulong g uchar to void 933 lw = new uchar 64 0 934 lwn = arx_lower_buf part msg s sub e s lw 935 arx_emit_expanded s e g part lw 0 lwn 936 end 937 938 fn arx_tokenize msg view uchar * max_toks ulong to ulong 939 put arx_tok_n 0 0 940 n = len msg 941 cap = max_toks 942 if gt cap ARX_MAX_TOKENS 943 cap = ARX_MAX_TOKENS 944 end 945 i = ulong 0 946 gstart = ulong 0 947 while lt i n bound 4096 948 if ge get arx_tok_n 0 cap 949 break 950 end 951 if arx_is_space get msg i 952 i = add i 1 953 continue 954 end 955 s = i 956 while lt i n bound 4096 957 if arx_is_space get msg i 958 break 959 end 960 i = add i 1 961 end 962 e = i 963 while lt s e bound 4096 964 if not arx_edge_punct get msg s 965 break 966 end 967 s = add s 1 968 end 969 while gt e s bound 4096 970 if not arx_edge_punct get msg sub e 1 971 break 972 end 973 e = sub e 1 974 end 975 if eq s e 976 continue 977 end 978 g = arx_gap_flags part msg gstart sub s gstart 979 gstart = e 980 arx_tok_emit_span msg s e g 981 end 982 if gt get arx_tok_n 0 cap 983 put arx_tok_n 0 cap 984 end 985 ret get arx_tok_n 0 986 end 987 988 fn arx_find_ci hay view uchar * needle view uchar * to long 989 hn = len hay 990 nn = len needle 991 if eq nn 0 992 ret -1 993 end 994 if gt nn hn 995 ret -1 996 end 997 lim = add sub hn nn 1 998 i = ulong 0 999 while lt i lim bound 4096 1000 ok = true 1001 j = ulong 0 1002 while lt j nn bound 512 1003 a = arx_to_lower get hay add i j 1004 b = arx_to_lower get needle j 1005 if ne a b 1006 ok = false 1007 break 1008 end 1009 j = add j 1 1010 end 1011 if ok 1012 ret long i 1013 end 1014 i = add i 1 1015 end 1016 ret -1 1017 end 1018 1019 note ========================================================================= 1020 note Section 4: Dialog Roles, Turns, and Pending State 1021 note ========================================================================= 1022 1023 text arx_s_act_key "\"action\"" 1024 1025 fn arx_parse_action s view uchar * to uchar 1026 n = len s 1027 kn = len arx_s_act_key 1028 if lt n kn 1029 ret ARX_ACT_INVALID 1030 end 1031 found = false 1032 pos = ulong 0 1033 lim = add sub n kn 1 1034 i = ulong 0 1035 while lt i lim bound 4096 1036 ok = true 1037 j = ulong 0 1038 while lt j kn bound 16 1039 if ne get s add i j get arx_s_act_key j 1040 ok = false 1041 break 1042 end 1043 j = add j 1 1044 end 1045 if ok 1046 found = true 1047 pos = add i kn 1048 break 1049 end 1050 i = add i 1 1051 end 1052 if not found 1053 ret ARX_ACT_INVALID 1054 end 1055 colon = false 1056 while lt pos n bound 4096 1057 if eq get s pos 58 1058 colon = true 1059 pos = add pos 1 1060 break 1061 end 1062 pos = add pos 1 1063 end 1064 if not colon 1065 ret ARX_ACT_INVALID 1066 end 1067 while lt pos n bound 4096 1068 if not arx_is_space get s pos 1069 break 1070 end 1071 pos = add pos 1 1072 end 1073 if ge pos n 1074 ret ARX_ACT_INVALID 1075 end 1076 if ne get s pos 34 1077 ret ARX_ACT_INVALID 1078 end 1079 pos = add pos 1 1080 es = pos 1081 while lt pos n bound 4096 1082 if eq get s pos 34 1083 ret arx_act_from_name part s es sub pos es 1084 end 1085 pos = add pos 1 1086 end 1087 ret ARX_ACT_INVALID 1088 end 1089 1090 fn arx_clear_turns to void 1091 put arx_turn_n 0 0 1092 end 1093 1094 fn arx_push_turn role uchar txt view uchar * to void 1095 idx = get arx_turn_n 0 1096 if ge idx ARX_MAX_TURNS_CAP 1097 ret 1098 end 1099 rn = len txt 1100 n = rn 1101 if gt n ARX_TURN_STRIDE 1102 n = ARX_TURN_STRIDE 1103 end 1104 put arx_turn_role idx role 1105 put arx_turn_len idx n 1106 put arx_turn_raw_len idx rn 1107 base = mul idx ARX_TURN_STRIDE 1108 i = ulong 0 1109 while lt i n bound 512 1110 put arx_turn_buf add base i get txt i 1111 i = add i 1 1112 end 1113 put arx_turn_n 0 add idx 1 1114 end 1115 1116 fn arx_exam_turns s view uchar * to void 1117 arx_clear_turns 1118 n = len s 1119 role = ARX_ROLE_USER 1120 seg = ulong 0 1121 i = ulong 0 1122 while lt i n bound 16384 1123 is_sep = false 1124 if lt add i 3 n 1125 if eq get s i 32 1126 if eq get s add i 1 126 1127 if eq get s add i 2 126 1128 if eq get s add i 3 32 1129 is_sep = true 1130 end 1131 end 1132 end 1133 end 1134 end 1135 if is_sep 1136 arx_push_turn role part s seg sub i seg 1137 if eq role ARX_ROLE_USER 1138 role = ARX_ROLE_ASST 1139 else 1140 role = ARX_ROLE_USER 1141 end 1142 i = add i 4 1143 seg = i 1144 else 1145 i = add i 1 1146 end 1147 end 1148 arx_push_turn role part s seg sub n seg 1149 end 1150 1151 fn arx_load_turn_buf idx ulong dst edit uchar * to ulong 1152 n = get arx_turn_len idx 1153 base = mul idx ARX_TURN_STRIDE 1154 i = ulong 0 1155 while lt i n bound 512 1156 put dst i get arx_turn_buf add base i 1157 i = add i 1 1158 end 1159 ret n 1160 end 1161 1162 fn arx_dialog_state max_turns ulong to void 1163 tmp = new uchar 512 0 1164 tn = get arx_turn_n 0 1165 start = ulong 0 1166 if gt tn max_turns 1167 start = sub tn max_turns 1168 end 1169 has_u = uchar 0 1170 last_u = ulong 0 1171 i = start 1172 while lt i tn bound 64 1173 if eq get arx_turn_role i ARX_ROLE_USER 1174 has_u = 1 1175 last_u = i 1176 end 1177 i = add i 1 1178 end 1179 pend = ARX_PEND_UNKNOWN 1180 prev = ARX_ACT_INVALID 1181 if eq has_u 1 1182 j = start 1183 while lt j last_u bound 64 1184 if eq get arx_turn_role j ARX_ROLE_ASST 1185 ln = arx_load_turn_buf j tmp 1186 a = arx_parse_action part tmp 0 ln 1187 prev = a 1188 if ne a ARX_ACT_INVALID 1189 if arx_act_creates_pending a 1190 pend = ARX_PEND_YES 1191 else 1192 if arx_act_needs_pending a 1193 pend = ARX_PEND_NO 1194 end 1195 end 1196 end 1197 end 1198 j = add j 1 1199 end 1200 end 1201 put arx_dlg_pending 0 pend 1202 put arx_dlg_prev 0 prev 1203 put arx_dlg_has_user 0 has_u 1204 put arx_dlg_last_user 0 last_u 1205 end 1206 1207 note ========================================================================= 1208 note Section 5: Lexicon Tables and Phrase Matching 1209 note ========================================================================= 1210 1211 text arx_lex_c0 "remember\nmemorize\nmemorise\nstore\nsave\nkeep in mind\nnote for memory\ndo not forget\nfact\nmemory" 1212 text arx_lex_c1 "prefer\npreference\nrather\nfrom now on\ni like\nalways\ntone\nstyle" 1213 text arx_lex_c2 "task\ntodo\nto-do\nto do\nremind me\nmy list\nmake a note\nnote down" 1214 text arx_lex_c3 "write\ndraft\ncompose\ngenerate\nsummarize\nsummarise\nexplain\ndescribe\nrewrite\ntranslate\npoem\nstory\nhaiku\nlimerick\nletter\nessay\ntoast\nreply\nmessage" 1215 text arx_lex_c4 "approve\naccept\nyes\nyeah\nyep\nconfirm\ngo ahead\nsounds good\nlooks good\nagreed\nproceed" 1216 text arx_lex_c5 "deny\nreject\nno\nnope\ndecline\nwrong\nnot good\nthrow it away\nthrow away\ndiscard\ndo not like\ndo not accept" 1217 text arx_lex_c6 "cancel\nstop\nabort\nnever mind\nnevermind\nforget it\ncall it off\nhalt\ndrop\nchanged my mind\nhold on" 1218 text arx_lex_c7 "status\nstate\nready\nlocked\npending\nbusy\nidle\ndoing\nrunning" 1219 text arx_lex_c8 "help\noption\noptions\ncommand\ncommands\ncan you do\nmenu\ncapabilities\nactions" 1220 text arx_lex_c9 "evidence\naudit\ncounter\ncounters\nhow many\nso far\nlog\nstatistics\nstats" 1221 text arx_lex_c10 "recall\nread back\nlast\nlatest\nnewest\nmost recent\nprevious\nbring up\nrepeat" 1222 text arx_lex_c11 "facts\nremembered\nknow about me\nstored\nyou remember\ntold you" 1223 text arx_lex_c12 "weather\nnews\nprice\nstock\nbitcoin\nscore\nforecast\ntraffic\nrain\ntemperature\ntime is it\nwho won\nright now" 1224 text arx_lex_c13 "light\nlights\nlamp\ntv\nmusic\nplay\nsong\nthermostat\nheater\nvolume\nspeaker\ndoor\ngarage\noven\nswitch on\nturn on\nturn off" 1225 text arx_lex_c14 "book\norder\nflight\npizza\nticket\nhotel\ntaxi\nbuy me\npurchase\nshop" 1226 text arx_lex_c15 "hi\nhello\nhey\nthanks\nthank you\nbye\ngoodbye\ngood morning\ngood night\nhave a nice" 1227 text arx_lex_c16 "this\nthat\nit\nthing\none\nsomething\nstuff\nother\nbefore\nsame\nusual" 1228 1229 text arx_lex_polite "please\nkindly\nok\nokay\nnow\njust\nso\nwell\nalso\nalright\nhey\nfirst\nthen\ncan you\ncould you\nwould you\nwill you\ni want you to\ni need you to\ni would like you to\ngo ahead and" 1230 text arx_lex_filler "ok okay good great fine nice cool thanks thank you hmm um oh well so alright right perfect" 1231 text arx_lex_filler_comma "ok okay well so alright hmm um oh" 1232 text arx_lex_vacuous "this that it thing things one something anything stuff a an the my your some to for me task note fact preference new down please again too" 1233 text arx_lex_write_verbs "write draft compose generate create make produce" 1234 text arx_lex_forbidden "unlock rotate update upgrade shutdown reboot restart wipe erase delete deploy format uninstall rm destroy" 1235 text arx_lex_device_obj "light lights lamp lamps tv television music radio heater fan oven stove alarm speaker kettle heating ac sprinkler sprinklers" 1236 text arx_lex_determiners "a an the any this that your my some its every" 1237 text arx_lex_negation "not never no" 1238 text arx_lex_rb_verbs "ignore disregard forget disable bypass break override skip drop remove turn" 1239 text arx_lex_rb_objs "rule rules instruction instructions policy policies safety restriction restrictions limit limits guardrail guardrails allowed approval filter filters prompt" 1240 text arx_lex_override "developer mode\ndev mode\nadmin mode\ngod mode\ndebug mode\nsystem override\njailbreak\nno rules\nwithout rules\nunrestricted\npretend you\npretend to be\nact as if\nany command\nwithout asking\nsudo\nroot access\nyour instructions\nsystem prompt" 1241 text arx_lex_blanket "automatically always every without" 1242 text arx_lex_report "says said say tells told instructs wrote written asks" 1243 text arx_lex_obey "do it\ndo that\ndo what\ndo as\nfollow\nobey\ncarry out\nexecute\nrun it\nact on\ngo on" 1244 text arx_lex_text_frames "remember\nmemorize\nmemorise\nstore\nsave\nnote\nkeep in mind\nkeep this\ndo not forget\nmake a note\nremind me\ntask\nnew task\nadd\nput\nto do\ntodo\nprefer\ni prefer\ni like\ni would rather\nmy preference\nwrite\ndraft\ncompose\ngenerate\nsummarize\nexplain\ndescribe\nask the model" 1245 text arx_lex_and_verbs "approve deny cancel recall show list check read tell give reject accept" 1246 text arx_lex_trail_mark "afterwards afterward later next" 1247 text arx_lex_trail_polite "please thanks" 1248 text arx_lex_ambiguous "update format restart upgrade" 1249 text arx_lex_verb_leads "to you and then please now just" 1250 1251 fn arx_tok_eq_slice tidx ulong s view uchar * off ulong slen ulong to bool 1252 wn = get arx_tok_wlen tidx 1253 if ne wn slen 1254 ret false 1255 end 1256 base = mul tidx ARX_TOK_STRIDE 1257 i = ulong 0 1258 while lt i wn bound 64 1259 if ne get arx_tok_word add base i get s add off i 1260 ret false 1261 end 1262 i = add i 1 1263 end 1264 ret true 1265 end 1266 1267 fn arx_tok_eq_text tidx ulong s view uchar * to bool 1268 ret arx_tok_eq_slice tidx s 0 len s 1269 end 1270 1271 fn arx_word_in_list tidx ulong lst view uchar * to bool 1272 n = len lst 1273 i = ulong 0 1274 while lt i n bound 2048 1275 if eq get lst i 32 1276 i = add i 1 1277 continue 1278 end 1279 ws = i 1280 while lt i n bound 2048 1281 if eq get lst i 32 1282 break 1283 end 1284 i = add i 1 1285 end 1286 if arx_tok_eq_slice tidx lst ws sub i ws 1287 ret true 1288 end 1289 end 1290 ret false 1291 end 1292 1293 fn arx_match_phrase_slice pos ulong tok_end ulong lst view uchar * ps ulong pe ulong to ulong 1294 wc = ulong 0 1295 p = ps 1296 while lt p pe bound 256 1297 if eq get lst p 32 1298 p = add p 1 1299 continue 1300 end 1301 ws = p 1302 while lt p pe bound 256 1303 if eq get lst p 32 1304 break 1305 end 1306 p = add p 1 1307 end 1308 if ge add pos wc tok_end 1309 ret 0 1310 end 1311 if not arx_tok_eq_slice add pos wc lst ws sub p ws 1312 ret 0 1313 end 1314 wc = add wc 1 1315 end 1316 ret wc 1317 end 1318 1319 fn arx_longest_phrase pos ulong tok_end ulong lst view uchar * to ulong 1320 n = len lst 1321 best = ulong 0 1322 i = ulong 0 1323 while lt i n bound 2048 1324 ps = i 1325 while lt i n bound 2048 1326 if eq get lst i 10 1327 break 1328 end 1329 i = add i 1 1330 end 1331 pe = i 1332 if lt i n 1333 i = add i 1 1334 end 1335 wc = arx_match_phrase_slice pos tok_end lst ps pe 1336 if gt wc best 1337 best = wc 1338 end 1339 end 1340 ret best 1341 end 1342 1343 fn arx_skip_polite pos ulong tok_end ulong to ulong 1344 cur = pos 1345 while lt cur tok_end bound 256 1346 m = arx_longest_phrase cur tok_end arx_lex_polite 1347 if eq m 0 1348 ret cur 1349 end 1350 if gt add cur m tok_end 1351 ret cur 1352 end 1353 cur = add cur m 1354 end 1355 ret cur 1356 end 1357 1358 note ========================================================================= 1359 note Section 6: Segmentation (Clauses and Primary Request) 1360 note ========================================================================= 1361 1362 text arx_s_then "then" 1363 text arx_s_and "and" 1364 text arx_s_after "after" 1365 text arx_s_that "that" 1366 1367 fn arx_push_clause cs ulong ce ulong to void 1368 cn = get arx_cl_n 0 1369 if ge cn ARX_MAX_CLAUSES 1370 ret 1371 end 1372 put arx_cl_start cn cs 1373 put arx_cl_end cn ce 1374 put arx_cl_n 0 add cn 1 1375 end 1376 1377 fn arx_clauses to ulong 1378 put arx_cl_n 0 0 1379 n = get arx_tok_n 0 1380 s = ulong 0 1381 i = ulong 0 1382 while lt i n bound 256 1383 g = get arx_tok_gap i 1384 if gt i s 1385 if ne and g ARX_GAP_SEMI 0 1386 arx_push_clause s i 1387 s = i 1388 i = add i 1 1389 continue 1390 end 1391 end 1392 if arx_tok_eq_text i arx_s_then 1393 if gt i s 1394 prev_and = false 1395 if gt sub i 1 s 1396 if arx_tok_eq_text sub i 1 arx_s_and 1397 prev_and = true 1398 end 1399 end 1400 has_sep = ne and g or ARX_GAP_COMMA ARX_GAP_STOP 0 1401 if or prev_and has_sep 1402 ce = i 1403 if prev_and 1404 ce = sub i 1 1405 end 1406 arx_push_clause s ce 1407 s = add i 1 1408 i = add i 1 1409 continue 1410 end 1411 end 1412 end 1413 if arx_tok_eq_text i arx_s_after 1414 if lt add i 1 n 1415 if arx_tok_eq_text add i 1 arx_s_that 1416 if gt i s 1417 prev_and = false 1418 if gt sub i 1 s 1419 if arx_tok_eq_text sub i 1 arx_s_and 1420 prev_and = true 1421 end 1422 end 1423 has_sep = ne and g or ARX_GAP_COMMA ARX_GAP_STOP 0 1424 if or prev_and has_sep 1425 ce = i 1426 if prev_and 1427 ce = sub i 1 1428 end 1429 arx_push_clause s ce 1430 s = add i 2 1431 i = add i 2 1432 continue 1433 end 1434 end 1435 end 1436 end 1437 end 1438 if arx_tok_eq_text i arx_s_and 1439 if gt i s 1440 if lt add i 1 n 1441 if arx_word_in_list add i 1 arx_lex_and_verbs 1442 arx_push_clause s i 1443 s = add i 1 1444 i = add i 1 1445 continue 1446 end 1447 end 1448 end 1449 end 1450 i = add i 1 1451 end 1452 if lt s n 1453 arx_push_clause s n 1454 end 1455 note Strip trailing markers and drop empty clauses 1456 cn = get arx_cl_n 0 1457 out_n = ulong 0 1458 k = ulong 0 1459 while lt k cn bound 64 1460 cs = get arx_cl_start k 1461 ce = get arx_cl_end k 1462 while gt ce cs bound 256 1463 if not arx_word_in_list sub ce 1 arx_lex_trail_mark 1464 break 1465 end 1466 ce = sub ce 1 1467 end 1468 if gt ce cs 1469 put arx_cl_start out_n cs 1470 put arx_cl_end out_n ce 1471 out_n = add out_n 1 1472 end 1473 k = add k 1 1474 end 1475 put arx_cl_n 0 out_n 1476 ret out_n 1477 end 1478 1479 fn arx_all_filler cs ulong ce ulong to bool 1480 if le ce cs 1481 ret false 1482 end 1483 i = cs 1484 while lt i ce bound 256 1485 if ne get arx_tok_alnum i 0 1486 if not arx_word_in_list i arx_lex_filler 1487 ret false 1488 end 1489 end 1490 i = add i 1 1491 end 1492 ret true 1493 end 1494 1495 fn arx_primary to bool 1496 cn = get arx_cl_n 0 1497 if eq cn 0 1498 ret false 1499 end 1500 cs = get arx_cl_start 0 1501 ce = get arx_cl_end 0 1502 k = ulong 0 1503 while lt k cn bound 64 1504 ks = get arx_cl_start k 1505 ke = get arx_cl_end k 1506 if not arx_all_filler ks ke 1507 cs = ks 1508 ce = ke 1509 break 1510 end 1511 k = add k 1 1512 end 1513 note Drop leading filler sentences 1514 while lt cs ce bound 64 1515 j = add cs 1 1516 while lt j ce bound 256 1517 if ne and get arx_tok_gap j ARX_GAP_STOP 0 1518 break 1519 end 1520 j = add j 1 1521 end 1522 if lt j ce 1523 if arx_all_filler cs j 1524 cs = j 1525 continue 1526 end 1527 end 1528 break 1529 end 1530 note Drop leading FILLER_COMMA before comma 1531 while gt sub ce cs 1 bound 64 1532 if arx_word_in_list cs arx_lex_filler_comma 1533 if ne and get arx_tok_gap add cs 1 ARX_GAP_COMMA 0 1534 cs = add cs 1 1535 continue 1536 end 1537 end 1538 break 1539 end 1540 put arx_pri_start 0 cs 1541 put arx_pri_end 0 ce 1542 ret true 1543 end 1544 1545 note ========================================================================= 1546 note Section 7: Policy Layer 1547 note ========================================================================= 1548 1549 text arx_s_shut "shut" 1550 text arx_s_down "down" 1551 text arx_s_power "power" 1552 text arx_s_off "off" 1553 text arx_s_turn "turn" 1554 text arx_s_switch "switch" 1555 text arx_s_approve "approve" 1556 text arx_s_accept "accept" 1557 1558 fn arx_negated i ulong lo ulong to bool 1559 from = lo 1560 if ge i add lo 2 1561 from = sub i 2 1562 end 1563 k = from 1564 while lt k i bound 4 1565 if arx_word_in_list k arx_lex_negation 1566 ret true 1567 end 1568 k = add k 1 1569 end 1570 ret false 1571 end 1572 1573 fn arx_check_split_pair i ulong cl_k ulong a view uchar * b view uchar * to bool 1574 cs = get arx_cl_start cl_k 1575 ce = get arx_cl_end cl_k 1576 if not arx_tok_eq_text i a 1577 ret false 1578 end 1579 if arx_negated i cs 1580 ret false 1581 end 1582 hi = add i 5 1583 if gt hi ce 1584 hi = ce 1585 end 1586 found = false 1587 k = add i 1 1588 while lt k hi bound 8 1589 if arx_tok_eq_text k b 1590 found = true 1591 break 1592 end 1593 k = add k 1 1594 end 1595 if not found 1596 ret false 1597 end 1598 j = cs 1599 while lt j ce bound 256 1600 if arx_word_in_list j arx_lex_device_obj 1601 ret false 1602 end 1603 j = add j 1 1604 end 1605 ret true 1606 end 1607 1608 fn arx_policy_check_tok i ulong k ulong lead ulong to uchar 1609 cs = get arx_cl_start k 1610 ce = get arx_cl_end k 1611 n = get arx_tok_n 0 1612 if arx_word_in_list i arx_lex_rb_verbs 1613 if not arx_negated i cs 1614 hi = add i 4 1615 if gt hi ce 1616 hi = ce 1617 end 1618 j = add i 1 1619 while lt j hi bound 8 1620 if arx_word_in_list j arx_lex_rb_objs 1621 ret ARX_REFUSE_RULE_BREAK 1622 end 1623 j = add j 1 1624 end 1625 end 1626 end 1627 if gt arx_longest_phrase i n arx_lex_override 0 1628 ret ARX_REFUSE_OVERRIDE 1629 end 1630 if arx_word_in_list i arx_lex_forbidden 1631 if not arx_negated i cs 1632 noun = false 1633 if gt i cs 1634 if arx_word_in_list sub i 1 arx_lex_determiners 1635 noun = true 1636 end 1637 end 1638 amb = arx_word_in_list i arx_lex_ambiguous 1639 vpos = eq i lead 1640 if gt i cs 1641 if arx_word_in_list sub i 1 arx_lex_verb_leads 1642 vpos = true 1643 end 1644 end 1645 if not noun 1646 if or not amb vpos 1647 ret ARX_REFUSE_FORBIDDEN 1648 end 1649 end 1650 end 1651 end 1652 if arx_check_split_pair i k arx_s_shut arx_s_down 1653 ret ARX_REFUSE_FORBIDDEN 1654 end 1655 if arx_check_split_pair i k arx_s_power arx_s_off 1656 ret ARX_REFUSE_FORBIDDEN 1657 end 1658 if arx_check_split_pair i k arx_s_power arx_s_down 1659 ret ARX_REFUSE_FORBIDDEN 1660 end 1661 if arx_check_split_pair i k arx_s_turn arx_s_off 1662 ret ARX_REFUSE_FORBIDDEN 1663 end 1664 if arx_check_split_pair i k arx_s_switch arx_s_off 1665 ret ARX_REFUSE_FORBIDDEN 1666 end 1667 if or arx_tok_eq_text i arx_s_approve arx_tok_eq_text i arx_s_accept 1668 j = cs 1669 while lt j ce bound 256 1670 if arx_word_in_list j arx_lex_blanket 1671 ret ARX_REFUSE_BLANKET 1672 end 1673 j = add j 1 1674 end 1675 end 1676 ret ARX_REFUSE_NONE 1677 end 1678 1679 fn arx_policy_check to uchar 1680 put arx_pol_rule 0 ARX_REFUSE_NONE 1681 put arx_pol_tok 0 0 1682 n = get arx_tok_n 0 1683 i = ulong 0 1684 while lt i n bound 256 1685 put arx_pol_exempt i 0 1686 i = add i 1 1687 end 1688 cn = get arx_cl_n 0 1689 k = ulong 0 1690 while lt k cn bound 64 1691 cs = get arx_cl_start k 1692 ce = get arx_cl_end k 1693 lead = arx_skip_polite cs ce 1694 f = arx_longest_phrase lead ce arx_lex_text_frames 1695 if gt f 0 1696 j = add lead f 1697 while lt j ce bound 256 1698 put arx_pol_exempt j 1 1699 j = add j 1 1700 end 1701 end 1702 k = add k 1 1703 end 1704 has_rep = false 1705 rep_at = ulong 0 1706 k = ulong 0 1707 while lt k cn bound 64 1708 cs = get arx_cl_start k 1709 ce = get arx_cl_end k 1710 lead = arx_skip_polite cs ce 1711 i = cs 1712 while lt i ce bound 256 1713 if eq get arx_pol_exempt i 0 1714 r = arx_policy_check_tok i k lead 1715 if ne r ARX_REFUSE_NONE 1716 put arx_pol_rule 0 r 1717 put arx_pol_tok 0 i 1718 ret r 1719 end 1720 if not has_rep 1721 if arx_word_in_list i arx_lex_report 1722 has_rep = true 1723 rep_at = i 1724 end 1725 end 1726 end 1727 i = add i 1 1728 end 1729 k = add k 1 1730 end 1731 if has_rep 1732 i = add rep_at 1 1733 while lt i n bound 256 1734 if eq get arx_pol_exempt i 0 1735 if gt arx_longest_phrase i n arx_lex_obey 0 1736 put arx_pol_rule 0 ARX_REFUSE_DATA_ORDER 1737 put arx_pol_tok 0 i 1738 ret ARX_REFUSE_DATA_ORDER 1739 end 1740 end 1741 i = add i 1 1742 end 1743 end 1744 ret ARX_REFUSE_NONE 1745 end 1746 1747 note ========================================================================= 1748 note Section 8: Feature Extraction 1749 note ========================================================================= 1750 1751 fn arx_feat_key idx ulong to ulong 1752 b = new uchar 40 0 1753 put b 0 get arx_ft_ch idx 1754 fl = ulong get arx_ft_len idx 1755 base = mul idx 4 1756 k = ulong 0 1757 while lt k fl bound 4 1758 off = add 1 mul k 9 1759 put b off get arx_ft_fam add base k 1760 v = get arx_ft_val add base k 1761 put b add off 1 uchar and v 255 1762 put b add off 2 uchar and shr v 8 255 1763 put b add off 3 uchar and shr v 16 255 1764 put b add off 4 uchar and shr v 24 255 1765 put b add off 5 uchar and shr v 32 255 1766 put b add off 6 uchar and shr v 40 255 1767 put b add off 7 uchar and shr v 48 255 1768 put b add off 8 uchar and shr v 56 255 1769 k = add k 1 1770 end 1771 ret arx_fnv part b 0 add 1 mul fl 9 1772 end 1773 1774 fn arx_rest_content cs ulong ce ulong to ulong 1775 lead = arx_skip_polite cs ce 1776 f = arx_longest_phrase lead ce arx_lex_text_frames 1777 st = add lead f 1778 if gt st ce 1779 st = ce 1780 end 1781 cnt = ulong 0 1782 i = st 1783 while lt i ce bound 256 1784 if ne get arx_tok_alnum i 0 1785 if not arx_word_in_list i arx_lex_vacuous 1786 if not arx_word_in_list i arx_lex_filler 1787 cnt = add cnt 1 1788 end 1789 end 1790 end 1791 i = add i 1 1792 end 1793 ret cnt 1794 end 1795 1796 fn arx_concepts_at i ulong ce ulong to uint 1797 m = uint 0 1798 if gt arx_longest_phrase i ce arx_lex_c0 0 1799 m = or m shl uint 1 0 1800 end 1801 if gt arx_longest_phrase i ce arx_lex_c1 0 1802 m = or m shl uint 1 1 1803 end 1804 if gt arx_longest_phrase i ce arx_lex_c2 0 1805 m = or m shl uint 1 2 1806 end 1807 if gt arx_longest_phrase i ce arx_lex_c3 0 1808 m = or m shl uint 1 3 1809 end 1810 if gt arx_longest_phrase i ce arx_lex_c4 0 1811 m = or m shl uint 1 4 1812 end 1813 if gt arx_longest_phrase i ce arx_lex_c5 0 1814 m = or m shl uint 1 5 1815 end 1816 if gt arx_longest_phrase i ce arx_lex_c6 0 1817 m = or m shl uint 1 6 1818 end 1819 if gt arx_longest_phrase i ce arx_lex_c7 0 1820 m = or m shl uint 1 7 1821 end 1822 if gt arx_longest_phrase i ce arx_lex_c8 0 1823 m = or m shl uint 1 8 1824 end 1825 if gt arx_longest_phrase i ce arx_lex_c9 0 1826 m = or m shl uint 1 9 1827 end 1828 if gt arx_longest_phrase i ce arx_lex_c10 0 1829 m = or m shl uint 1 10 1830 end 1831 if gt arx_longest_phrase i ce arx_lex_c11 0 1832 m = or m shl uint 1 11 1833 end 1834 if gt arx_longest_phrase i ce arx_lex_c12 0 1835 m = or m shl uint 1 12 1836 end 1837 if gt arx_longest_phrase i ce arx_lex_c13 0 1838 m = or m shl uint 1 13 1839 end 1840 if gt arx_longest_phrase i ce arx_lex_c14 0 1841 m = or m shl uint 1 14 1842 end 1843 if gt arx_longest_phrase i ce arx_lex_c15 0 1844 m = or m shl uint 1 15 1845 end 1846 if gt arx_longest_phrase i ce arx_lex_c16 0 1847 m = or m shl uint 1 16 1848 end 1849 ret m 1850 end 1851 1852 fn arx_concepts cs ulong ce ulong to uint 1853 m = uint 0 1854 i = cs 1855 while lt i ce bound 256 1856 m = or m arx_concepts_at i ce 1857 i = add i 1 1858 end 1859 ret m 1860 end 1861 1862 fn arx_ft_push1 ch ulong f0 uchar v0 ulong to void 1863 wt = get arx_cfg_weights ch 1864 if eq wt 0 1865 ret 1866 end 1867 idx = get arx_ft_n 0 1868 if ge idx ARX_MAX_FEATS 1869 ret 1870 end 1871 b = mul idx 4 1872 put arx_ft_fam b f0 1873 put arx_ft_val b v0 1874 put arx_ft_len idx 1 1875 put arx_ft_ch idx uchar ch 1876 put arx_ft_w idx wt 1877 put arx_ft_n 0 add idx 1 1878 end 1879 1880 fn arx_ft_push2 ch ulong f0 uchar v0 ulong f1 uchar v1 ulong to void 1881 wt = get arx_cfg_weights ch 1882 if eq wt 0 1883 ret 1884 end 1885 idx = get arx_ft_n 0 1886 if ge idx ARX_MAX_FEATS 1887 ret 1888 end 1889 b = mul idx 4 1890 put arx_ft_fam b f0 1891 put arx_ft_val b v0 1892 put arx_ft_fam add b 1 f1 1893 put arx_ft_val add b 1 v1 1894 put arx_ft_len idx 2 1895 put arx_ft_ch idx uchar ch 1896 put arx_ft_w idx wt 1897 put arx_ft_n 0 add idx 1 1898 end 1899 1900 fn arx_ft_push_chars buf view uchar * pos ulong g ulong to void 1901 wt = get arx_cfg_weights ARX_CH_CHARS 1902 if eq wt 0 1903 ret 1904 end 1905 idx = get arx_ft_n 0 1906 if ge idx ARX_MAX_FEATS 1907 ret 1908 end 1909 b = mul idx 4 1910 k = ulong 0 1911 while lt k g bound 4 1912 put arx_ft_fam add b k ARX_SYM_CHAR 1913 put arx_ft_val add b k ulong get buf add pos k 1914 k = add k 1 1915 end 1916 put arx_ft_len idx uchar g 1917 put arx_ft_ch idx uchar ARX_CH_CHARS 1918 put arx_ft_w idx wt 1919 put arx_ft_n 0 add idx 1 1920 end 1921 1922 fn arx_extract_char_ngrams tidx ulong to void 1923 buf = new uchar 34 0 1924 wn = get arx_tok_wlen tidx 1925 if gt wn 32 1926 wn = 32 1927 end 1928 put buf 0 uchar 94 1929 base = mul tidx ARX_TOK_STRIDE 1930 k = ulong 0 1931 while lt k wn bound 32 1932 put buf add k 1 get arx_tok_word add base k 1933 k = add k 1 1934 end 1935 put buf add wn 1 uchar 36 1936 sn = add wn 2 1937 if ge sn 3 1938 lim3 = sub sn 2 1939 i = ulong 0 1940 while lt i lim3 bound 34 1941 arx_ft_push_chars buf i 3 1942 i = add i 1 1943 end 1944 end 1945 if ge sn 4 1946 lim4 = sub sn 3 1947 i = ulong 0 1948 while lt i lim4 bound 34 1949 arx_ft_push_chars buf i 4 1950 i = add i 1 1951 end 1952 end 1953 end 1954 1955 fn arx_ef_concepts_lead cs ulong ce ulong to void 1956 pend = ulong get arx_dlg_pending 0 1957 cm = arx_concepts cs ce 1958 k = ulong 0 1959 while lt k ARX_N_CONCEPTS bound 18 1960 if ne and cm shl uint 1 uint k 0 1961 arx_ft_push1 ARX_CH_CONCEPT ARX_SYM_CONCEPT k 1962 arx_ft_push2 ARX_CH_CTXCONJ ARX_SYM_CTX pend ARX_SYM_CONCEPT k 1963 end 1964 k = add k 1 1965 end 1966 lead = arx_skip_polite cs ce 1967 if lt lead ce 1968 hl = get arx_tok_shash lead 1969 arx_ft_push1 ARX_CH_LEAD ARX_SYM_LEAD hl 1970 arx_ft_push2 ARX_CH_CTXCONJ ARX_SYM_CTX pend ARX_SYM_LEAD hl 1971 if lt add lead 1 ce 1972 h2 = get arx_tok_shash add lead 1 1973 arx_ft_push2 ARX_CH_LEAD ARX_SYM_LEAD hl ARX_SYM_WORD h2 1974 end 1975 end 1976 arx_ft_push1 ARX_CH_CTX ARX_SYM_CTX pend 1977 prev_a = get arx_dlg_prev 0 1978 prev_v = ulong 0 1979 if ne prev_a ARX_ACT_INVALID 1980 prev_v = add ulong prev_a 1 1981 end 1982 arx_ft_push1 ARX_CH_CTX ARX_SYM_CTX add 16 prev_v 1983 end 1984 1985 fn arx_ef_ctx_shape cs ulong ce ulong wn ulong ends_q bool to void 1986 arx_ef_concepts_lead cs ce 1987 lb = ulong 4 1988 if le wn 1 1989 lb = 0 1990 else 1991 if eq wn 2 1992 lb = 1 1993 else 1994 if le wn 4 1995 lb = 2 1996 else 1997 if le wn 8 1998 lb = 3 1999 end 2000 end 2001 end 2002 end 2003 arx_ft_push1 ARX_CH_SHAPE ARX_SYM_SHAPE lb 2004 has_q = ends_q 2005 if not has_q 2006 j = add cs 1 2007 while lt j ce bound 256 2008 if ne and get arx_tok_gap j ARX_GAP_QUESTION 0 2009 has_q = true 2010 break 2011 end 2012 j = add j 1 2013 end 2014 end 2015 if has_q 2016 arx_ft_push1 ARX_CH_SHAPE ARX_SYM_SHAPE 10 2017 end 2018 rc = arx_rest_content cs ce 2019 rv = ulong 22 2020 if eq rc 0 2021 rv = 20 2022 else 2023 if le rc 2 2024 rv = 21 2025 end 2026 end 2027 arx_ft_push1 ARX_CH_SHAPE ARX_SYM_SHAPE rv 2028 end 2029 2030 fn arx_extract_features cs ulong ce ulong ends_q bool to ulong 2031 words = new ulong 256 0 2032 seen = new ulong 256 0 2033 put arx_ft_n 0 0 2034 wn = ulong 0 2035 i = cs 2036 while lt i ce bound 256 2037 if ne get arx_tok_alnum i 0 2038 put words wn i 2039 wn = add wn 1 2040 end 2041 i = add i 1 2042 end 2043 sn = ulong 0 2044 i = ulong 0 2045 while lt i wn bound 256 2046 h = get arx_tok_shash get words i 2047 dup = false 2048 j = ulong 0 2049 while lt j sn bound 256 2050 if eq get seen j h 2051 dup = true 2052 break 2053 end 2054 j = add j 1 2055 end 2056 if not dup 2057 put seen sn h 2058 sn = add sn 1 2059 arx_ft_push1 ARX_CH_WORD ARX_SYM_WORD h 2060 end 2061 i = add i 1 2062 end 2063 if ge wn 2 2064 lim = sub wn 1 2065 i = ulong 0 2066 while lt i lim bound 256 2067 ha = get arx_tok_shash get words i 2068 hb = get arx_tok_shash get words add i 1 2069 arx_ft_push2 ARX_CH_BIGRAM ARX_SYM_WORD ha ARX_SYM_WORD hb 2070 i = add i 1 2071 end 2072 end 2073 if ne get arx_cfg_weights ARX_CH_CHARS 0 2074 i = ulong 0 2075 while lt i wn bound 256 2076 arx_extract_char_ngrams get words i 2077 i = add i 1 2078 end 2079 end 2080 arx_ef_ctx_shape cs ce wn ends_q 2081 ret get arx_ft_n 0 2082 end 2083 2084 note ========================================================================= 2085 note Section 9: Binary Hypervectors 2086 note ========================================================================= 2087 2088 fn arx_splitmix_next s ulong to ulong 2089 z = s 2090 z = mul xor z shr z 30 ulong 0xbf58476d1ce4e5b9 2091 z = mul xor z shr z 27 ulong 0x94d049bb133111eb 2092 ret xor z shr z 31 2093 end 2094 2095 fn arx_popcnt64 x ulong to uint 2096 v = sub x and shr x 1 ulong 0x5555555555555555 2097 v = add and v ulong 0x3333333333333333 and shr v 2 ulong 0x3333333333333333 2098 v = and add v shr v 4 ulong 0x0f0f0f0f0f0f0f0f 2099 ret uint shr mul v ulong 0x0101010101010101 56 2100 end 2101 2102 fn arx_hv_item fam ulong val ulong dst edit ulong * to void 2103 s = xor xor ARX_SEED mul fam ulong 0xd6e8feb86659fd93 val 2104 s = add s ulong 0x9e3779b97f4a7c15 2105 w = len dst 2106 i = ulong 0 2107 while lt i w bound 128 2108 s = add s ulong 0x9e3779b97f4a7c15 2109 put dst i arx_splitmix_next s 2110 i = add i 1 2111 end 2112 end 2113 2114 fn arx_hv_bind_sym fam ulong val ulong rot ulong w ulong to void 2115 tmp = new ulong 128 0 2116 arx_hv_item fam val part tmp 0 w 2117 x = ulong 0 2118 while lt x w bound 128 2119 dst_x = add x rot 2120 if ge dst_x w 2121 dst_x = sub dst_x w 2122 end 2123 xv = xor get arx_hv_acc dst_x get tmp x 2124 put arx_hv_acc dst_x xv 2125 x = add x 1 2126 end 2127 end 2128 2129 fn arx_hv_bind_feat fidx ulong w ulong to void 2130 x = ulong 0 2131 while lt x w bound 128 2132 put arx_hv_acc x 0 2133 x = add x 1 2134 end 2135 fl = ulong get arx_ft_len fidx 2136 base = mul fidx 4 2137 j = ulong 0 2138 while lt j fl bound 4 2139 idx = add base j 2140 fam = ulong get arx_ft_fam idx 2141 val = get arx_ft_val idx 2142 rot = rem sub sub fl 1 j w 2143 arx_hv_bind_sym fam val rot w 2144 j = add j 1 2145 end 2146 end 2147 2148 fn arx_hv_accum_feat f ulong w ulong to void 2149 arx_hv_bind_feat f w 2150 wt = int get arx_ft_w f 2151 x = ulong 0 2152 while lt x w bound 128 2153 v = get arx_hv_acc x 2154 base = mul x 64 2155 b = ulong 0 2156 while lt b 64 bound 64 2157 bit = int and shr v b 1 2158 delta = mul sub mul 2 bit 1 wt 2159 ci = add base b 2160 nv = add get arx_hv_cnt ci delta 2161 put arx_hv_cnt ci nv 2162 b = add b 1 2163 end 2164 x = add x 1 2165 end 2166 end 2167 2168 fn arx_hv_finalize_q w ulong to void 2169 tmp = new ulong 128 0 2170 arx_hv_item 7 0 part tmp 0 w 2171 x = ulong 0 2172 while lt x w bound 128 2173 o = ulong 0 2174 tv = get tmp x 2175 base = mul x 64 2176 b = ulong 0 2177 while lt b 64 bound 64 2178 c = get arx_hv_cnt add base b 2179 bit = ulong 0 2180 if gt c 0 2181 bit = 1 2182 else 2183 if eq c 0 2184 bit = and shr tv b 1 2185 end 2186 end 2187 o = or o shl bit b 2188 b = add b 1 2189 end 2190 put arx_hv_q x o 2191 x = add x 1 2192 end 2193 end 2194 2195 fn arx_hv_encode_to_q w ulong to void 2196 d = mul w 64 2197 i = ulong 0 2198 while lt i d bound 8192 2199 put arx_hv_cnt i 0 2200 i = add i 1 2201 end 2202 fn_cnt = get arx_ft_n 0 2203 f = ulong 0 2204 while lt f fn_cnt bound 768 2205 arx_hv_accum_feat f w 2206 f = add f 1 2207 end 2208 arx_hv_finalize_q w 2209 end 2210 2211 fn arx_hv_sim_q_ex ex_idx ulong w ulong to f32 2212 ham = uint 0 2213 base = mul ex_idx w 2214 x = ulong 0 2215 while lt x w bound 128 2216 va = get arx_hv_q x 2217 vb = get arx_hv_hvs add base x 2218 pc = arx_popcnt64 xor va vb 2219 ham = add ham pc 2220 x = add x 1 2221 end 2222 d = f32 mul w 64 2223 ret sub 1.0f32 div mul 2.0f32 f32 ham d 2224 end 2225 2226 fn arx_hv_sim_ex_ex a ulong b ulong w ulong to f32 2227 ham = uint 0 2228 ba = mul a w 2229 bb = mul b w 2230 x = ulong 0 2231 while lt x w bound 128 2232 va = get arx_hv_hvs add ba x 2233 vb = get arx_hv_hvs add bb x 2234 pc = arx_popcnt64 xor va vb 2235 ham = add ham pc 2236 x = add x 1 2237 end 2238 d = f32 mul w 64 2239 ret sub 1.0f32 div mul 2.0f32 f32 ham d 2240 end 2241 2242 note ========================================================================= 2243 note Section 10: Span Extraction and CueMemory 2244 note ========================================================================= 2245 2246 text arx_s_thank "thank" 2247 text arx_s_you "you" 2248 2249 fn arx_cue_reset to void 2250 put arx_cue_pre_n 0 0 2251 put arx_cue_suf_n 0 0 2252 put arx_cue_st_n 0 0 2253 end 2254 2255 fn arx_cue_pre_same idx ulong s ulong clen ulong to bool 2256 base = mul idx ARX_MAX_CUE_WORDS 2257 k = ulong 0 2258 while lt k clen bound 8 2259 ha = get arx_cue_pre_h add base k 2260 hb = get arx_tok_whash add s k 2261 if ne ha hb 2262 ret false 2263 end 2264 k = add k 1 2265 end 2266 ret true 2267 end 2268 2269 fn arx_cue_add_pre a uchar s ulong e ulong to void 2270 clen = sub e s 2271 if gt clen ARX_MAX_CUE_WORDS 2272 ret 2273 end 2274 if ge add get arx_cue_pre_n 0 get arx_cue_suf_n 0 ARX_MAX_CUES 2275 ret 2276 end 2277 pn = get arx_cue_pre_n 0 2278 i = ulong 0 2279 while lt i pn bound 256 2280 if eq get arx_cue_pre_act i a 2281 if eq ulong get arx_cue_pre_len i clen 2282 if arx_cue_pre_same i s clen 2283 ret 2284 end 2285 end 2286 end 2287 i = add i 1 2288 end 2289 put arx_cue_pre_act pn a 2290 put arx_cue_pre_len pn uchar clen 2291 base = mul pn ARX_MAX_CUE_WORDS 2292 k = ulong 0 2293 while lt k clen bound 8 2294 hv = get arx_tok_whash add s k 2295 put arx_cue_pre_h add base k hv 2296 k = add k 1 2297 end 2298 put arx_cue_pre_n 0 add pn 1 2299 end 2300 2301 fn arx_cue_suf_same idx ulong s ulong clen ulong to bool 2302 base = mul idx ARX_MAX_CUE_WORDS 2303 k = ulong 0 2304 while lt k clen bound 8 2305 ha = get arx_cue_suf_h add base k 2306 hb = get arx_tok_whash add s k 2307 if ne ha hb 2308 ret false 2309 end 2310 k = add k 1 2311 end 2312 ret true 2313 end 2314 2315 fn arx_cue_add_suf a uchar s ulong e ulong to void 2316 clen = sub e s 2317 if gt clen ARX_MAX_CUE_WORDS 2318 ret 2319 end 2320 if ge add get arx_cue_pre_n 0 get arx_cue_suf_n 0 ARX_MAX_CUES 2321 ret 2322 end 2323 sn = get arx_cue_suf_n 0 2324 i = ulong 0 2325 while lt i sn bound 256 2326 if eq get arx_cue_suf_act i a 2327 if eq ulong get arx_cue_suf_len i clen 2328 if arx_cue_suf_same i s clen 2329 ret 2330 end 2331 end 2332 end 2333 i = add i 1 2334 end 2335 put arx_cue_suf_act sn a 2336 put arx_cue_suf_len sn uchar clen 2337 base = mul sn ARX_MAX_CUE_WORDS 2338 k = ulong 0 2339 while lt k clen bound 8 2340 hv = get arx_tok_whash add s k 2341 put arx_cue_suf_h add base k hv 2342 k = add k 1 2343 end 2344 put arx_cue_suf_n 0 add sn 1 2345 end 2346 2347 fn arx_cue_bump a uchar h ulong is_cue bool to void 2348 sn = get arx_cue_st_n 0 2349 i = ulong 0 2350 while lt i sn bound 2048 2351 if eq get arx_cue_st_act i a 2352 if eq get arx_cue_st_h i h 2353 if is_cue 2354 v = get arx_cue_st_c i 2355 if lt v 65535 2356 put arx_cue_st_c i add v 1 2357 end 2358 else 2359 v = get arx_cue_st_p i 2360 if lt v 65535 2361 put arx_cue_st_p i add v 1 2362 end 2363 end 2364 ret 2365 end 2366 end 2367 i = add i 1 2368 end 2369 if ge sn ARX_MAX_STATS 2370 ret 2371 end 2372 put arx_cue_st_act sn a 2373 put arx_cue_st_h sn h 2374 if is_cue 2375 put arx_cue_st_c sn 1 2376 put arx_cue_st_p sn 0 2377 else 2378 put arx_cue_st_c sn 0 2379 put arx_cue_st_p sn 1 2380 end 2381 put arx_cue_st_n 0 add sn 1 2382 end 2383 2384 note Returns 0=unknown, 1=false (content), 2=true (cue) 2385 fn arx_cue_like a uchar h ulong to uchar 2386 sn = get arx_cue_st_n 0 2387 i = ulong 0 2388 while lt i sn bound 2048 2389 if eq get arx_cue_st_act i a 2390 if eq get arx_cue_st_h i h 2391 if gt get arx_cue_st_c i get arx_cue_st_p i 2392 ret 2 2393 end 2394 ret 1 2395 end 2396 end 2397 i = add i 1 2398 end 2399 ret 0 2400 end 2401 2402 fn arx_cue_learn cs ulong ce ulong act uchar bs ulong be ulong to bool 2403 lead = arx_skip_polite cs ce 2404 has_s = false 2405 s0 = ulong 0 2406 i = cs 2407 while lt i ce bound 256 2408 if ge get arx_tok_start i bs 2409 has_s = true 2410 s0 = i 2411 break 2412 end 2413 i = add i 1 2414 end 2415 has_e = false 2416 e0 = ulong 0 2417 if gt ce cs 2418 i = sub ce 1 2419 while ge i cs bound 256 2420 if le get arx_tok_end i be 2421 has_e = true 2422 e0 = i 2423 break 2424 end 2425 if eq i cs 2426 break 2427 end 2428 i = sub i 1 2429 end 2430 end 2431 if not and has_s has_e 2432 ret false 2433 end 2434 if gt s0 e0 2435 ret false 2436 end 2437 if lt s0 lead 2438 ret false 2439 end 2440 e1 = add e0 1 2441 arx_cue_add_pre act lead s0 2442 if lt e1 ce 2443 arx_cue_add_suf act e1 ce 2444 end 2445 i = lead 2446 while lt i s0 bound 256 2447 arx_cue_bump act get arx_tok_whash i true 2448 i = add i 1 2449 end 2450 i = s0 2451 while lt i e1 bound 256 2452 arx_cue_bump act get arx_tok_whash i false 2453 i = add i 1 2454 end 2455 ret true 2456 end 2457 2458 fn arx_cue_match_pre_at ci ulong p ulong j ulong m ulong to bool 2459 base = mul ci ARX_MAX_CUE_WORDS 2460 k = ulong 0 2461 while lt k m bound 8 2462 if ne get arx_tok_whash add p k get arx_cue_pre_h add base add j k 2463 ret false 2464 end 2465 k = add k 1 2466 end 2467 ret true 2468 end 2469 2470 fn arx_cue_best_pre_for ci ulong p ulong lead ulong ce ulong best ulong to ulong 2471 clen = ulong get arx_cue_pre_len ci 2472 bm = best 2473 j = ulong 0 2474 while lt j clen bound 8 2475 m = sub clen j 2476 skip = or le m bm ge add p m ce 2477 if not and eq p lead eq j 0 2478 if or lt m 2 eq p lead 2479 skip = true 2480 end 2481 end 2482 if not skip 2483 if arx_cue_match_pre_at ci p j m 2484 bm = m 2485 end 2486 end 2487 j = add j 1 2488 end 2489 ret bm 2490 end 2491 2492 note Returns 0 if no prefix cue matched, or (sv + 1) if matched. 2493 fn arx_cue_best_pre lead ulong ce ulong act uchar to ulong 2494 res = ulong 0 2495 best = ulong 0 2496 p_hi = add lead 3 2497 if gt p_hi ce 2498 p_hi = ce 2499 end 2500 pn = get arx_cue_pre_n 0 2501 p = lead 2502 while lt p p_hi bound 4 2503 ci = ulong 0 2504 while lt ci pn bound 256 2505 if eq get arx_cue_pre_act ci act 2506 if gt get arx_cue_pre_len ci 0 2507 nm = arx_cue_best_pre_for ci p lead ce best 2508 if gt nm best 2509 best = nm 2510 res = add add p nm 1 2511 end 2512 end 2513 end 2514 ci = add ci 1 2515 end 2516 p = add p 1 2517 end 2518 ret res 2519 end 2520 2521 fn arx_cue_colon_dash lead ulong ce ulong act uchar pr ulong to ulong 2522 from = add lead 1 2523 chk_s = lead 2524 if gt pr 0 2525 from = sub pr 1 2526 chk_s = from 2527 end 2528 hi = add lead 8 2529 if gt hi ce 2530 hi = ce 2531 end 2532 j = from 2533 while lt j hi bound 16 2534 if ne and get arx_tok_gap j or ARX_GAP_COLON ARX_GAP_DASH 0 2535 ok = true 2536 t = chk_s 2537 while lt t j bound 16 2538 if eq arx_cue_like act get arx_tok_whash t 1 2539 ok = false 2540 break 2541 end 2542 t = add t 1 2543 end 2544 if ok 2545 ret add j 1 2546 end 2547 ret pr 2548 end 2549 j = add j 1 2550 end 2551 ret pr 2552 end 2553 2554 fn arx_cue_bounds_start cs ulong ce ulong act uchar to ulong 2555 lead = arx_skip_polite cs ce 2556 pr = arx_cue_best_pre lead ce act 2557 pr = arx_cue_colon_dash lead ce act pr 2558 v = lead 2559 if gt pr 0 2560 v = sub pr 1 2561 else 2562 if ne act ARX_ACT_PROMPT 2563 f = arx_longest_phrase v ce arx_lex_text_frames 2564 if lt add v f ce 2565 v = add v f 2566 end 2567 end 2568 end 2569 while lt add v 1 ce bound 16 2570 if ge v add lead 6 2571 break 2572 end 2573 if ne arx_cue_like act get arx_tok_whash v 2 2574 break 2575 end 2576 v = add v 1 2577 end 2578 if gt v ce 2579 v = ce 2580 end 2581 ret v 2582 end 2583 2584 fn arx_cue_bounds_end s ulong ce ulong act uchar to ulong 2585 e = ce 2586 bests = ulong 0 2587 sn = get arx_cue_suf_n 0 2588 ci = ulong 0 2589 while lt ci sn bound 256 2590 clen = ulong get arx_cue_suf_len ci 2591 if eq get arx_cue_suf_act ci act 2592 if and gt clen bests lt add s clen e 2593 if arx_cue_suf_same ci sub e clen clen 2594 bests = clen 2595 end 2596 end 2597 end 2598 ci = add ci 1 2599 end 2600 e = sub e bests 2601 while gt e add s 1 bound 32 2602 if not arx_word_in_list sub e 1 arx_lex_trail_polite 2603 break 2604 end 2605 e = sub e 1 2606 end 2607 if gt e add s 2 2608 if arx_tok_eq_text sub e 2 arx_s_thank 2609 if arx_tok_eq_text sub e 1 arx_s_you 2610 e = sub e 2 2611 end 2612 end 2613 end 2614 ret e 2615 end 2616 2617 fn arx_cue_copy_span msg view uchar * bs ulong be ulong to uchar 2618 j = ulong 0 2619 k = bs 2620 while lt k be bound 4096 2621 b = get msg k 2622 curly = false 2623 if eq b 0xe2 2624 if lt add k 2 be 2625 if eq get msg add k 1 0x80 2626 b2 = get msg add k 2 2627 if or eq b2 0x99 eq b2 0x98 2628 curly = true 2629 end 2630 end 2631 end 2632 end 2633 ch = b 2634 if curly 2635 ch = 39 2636 k = add k 3 2637 else 2638 k = add k 1 2639 end 2640 if ge j ARX_MAX_TEXT 2641 ret ARX_SPAN_TOO_LONG 2642 end 2643 if or lt ch 0x20 gt ch 0x7e 2644 ret ARX_SPAN_UNREP 2645 end 2646 if or eq ch 34 eq ch 92 2647 ret ARX_SPAN_UNREP 2648 end 2649 put arx_dec_txt j ch 2650 j = add j 1 2651 end 2652 put arx_dec_txt_len 0 j 2653 put arx_exp_span_bs 0 bs 2654 put arx_exp_span_be 0 be 2655 ret ARX_SPAN_OK 2656 end 2657 2658 fn arx_cue_balance_be msg view uchar * bs ulong be ulong to ulong 2659 opens = ulong 0 2660 closes = ulong 0 2661 k = bs 2662 while lt k be bound 4096 2663 b = get msg k 2664 if eq b 40 2665 opens = add opens 1 2666 end 2667 if eq b 41 2668 closes = add closes 1 2669 end 2670 k = add k 1 2671 end 2672 if gt opens closes 2673 if lt be len msg 2674 if eq get msg be 41 2675 ret add be 1 2676 end 2677 end 2678 end 2679 ret be 2680 end 2681 2682 fn arx_cue_extract msg view uchar * cs ulong ce ulong act uchar to uchar 2683 s = arx_cue_bounds_start cs ce act 2684 e = arx_cue_bounds_end s ce act 2685 if ge s e 2686 ret ARX_SPAN_EMPTY 2687 end 2688 has_c = false 2689 i = s 2690 while lt i e bound 256 2691 if ne get arx_tok_alnum i 0 2692 if not arx_word_in_list i arx_lex_vacuous 2693 skip_w = false 2694 if eq act ARX_ACT_PROMPT 2695 if arx_word_in_list i arx_lex_write_verbs 2696 skip_w = true 2697 end 2698 end 2699 if not skip_w 2700 has_c = true 2701 break 2702 end 2703 end 2704 end 2705 i = add i 1 2706 end 2707 if not has_c 2708 ret ARX_SPAN_EMPTY 2709 end 2710 bs = get arx_tok_start s 2711 be = arx_cue_balance_be msg bs get arx_tok_end sub e 1 2712 ret arx_cue_copy_span msg bs be 2713 end 2714 2715 note ========================================================================= 2716 note Section 11: Scorer Models (HvModel, NearestModel, PerceptronModel) 2717 note ========================================================================= 2718 2719 fn arx_argmax s view f32 * to ulong 2720 best = ulong 0 2721 bv = get s 0 2722 l = ulong 1 2723 while lt l ARX_N_LABELS bound 15 2724 v = get s l 2725 if gt v bv 2726 bv = v 2727 best = l 2728 end 2729 l = add l 1 2730 end 2731 ret best 2732 end 2733 2734 fn arx_ins_top top edit f32 * off ulong kk ulong s f32 to void 2735 if eq kk 0 2736 ret 2737 end 2738 last = sub kk 1 2739 if le s get top add off last 2740 ret 2741 end 2742 p = last 2743 put top add off p s 2744 while gt p 0 bound 8 2745 cur = get top add off p 2746 prv = get top add off sub p 1 2747 if le cur prv 2748 break 2749 end 2750 put top add off p prv 2751 put top add off sub p 1 cur 2752 p = sub p 1 2753 end 2754 end 2755 2756 fn arx_ins_glob gs edit f32 8 gi edit ulong 8 ka ulong s f32 idx ulong to void 2757 if eq ka 0 2758 ret 2759 end 2760 last = sub ka 1 2761 if le s get gs last 2762 ret 2763 end 2764 p = last 2765 put gs p s 2766 put gi p idx 2767 while gt p 0 bound 8 2768 cur = get gs p 2769 prv = get gs sub p 1 2770 if le cur prv 2771 break 2772 end 2773 ci = get gi p 2774 pi = get gi sub p 1 2775 put gs p prv 2776 put gi p pi 2777 put gs sub p 1 cur 2778 put gi sub p 1 ci 2779 p = sub p 1 2780 end 2781 end 2782 2783 note --- HvModel --- 2784 2785 fn arx_hv_reset to void 2786 put arx_hv_n 0 0 2787 i = ulong 0 2788 while lt i ARX_N_LABELS bound 15 2789 put arx_hv_nnz i 0 2790 put arx_hv_nlabel i 0 2791 i = add i 1 2792 end 2793 end 2794 2795 fn arx_hv_push_q lbl uchar note_s view uchar * to long 2796 n = get arx_hv_n 0 2797 if ge n get arx_cfg_max_ex 0 2798 ret -1 2799 end 2800 if ge n ARX_MAX_EXEMPLARS 2801 ret -1 2802 end 2803 w = get arx_cfg_hv_words 0 2804 base = mul n w 2805 x = ulong 0 2806 while lt x w bound 128 2807 put arx_hv_hvs add base x get arx_hv_q x 2808 x = add x 1 2809 end 2810 put arx_hv_labels n lbl 2811 nlen = len note_s 2812 if gt nlen ARX_NOTE_BYTES 2813 nlen = ARX_NOTE_BYTES 2814 end 2815 nbase = mul n ARX_NOTE_BYTES 2816 k = ulong 0 2817 while lt k nlen bound 96 2818 put arx_hv_notes add nbase k get note_s k 2819 k = add k 1 2820 end 2821 put arx_hv_nlen n nlen 2822 li = ulong lbl 2823 put arx_hv_nlabel li add get arx_hv_nlabel li 1 2824 put arx_hv_n 0 add n 1 2825 ret long n 2826 end 2827 2828 fn arx_hv_add_word bbase ulong v ulong wt int to void 2829 b = ulong 0 2830 while lt b 64 bound 64 2831 bit = int and shr v b 1 2832 delta = mul sub mul 2 bit 1 wt 2833 ci = add bbase b 2834 nv = add get arx_hv_counts ci delta 2835 put arx_hv_counts ci nv 2836 b = add b 1 2837 end 2838 end 2839 2840 fn arx_hv_add_counts l ulong ex_idx ulong wt int to void 2841 w = get arx_cfg_hv_words 0 2842 cbase = mul l mul w 64 2843 hbase = mul ex_idx w 2844 x = ulong 0 2845 while lt x w bound 128 2846 arx_hv_add_word add cbase mul x 64 get arx_hv_hvs add hbase x wt 2847 x = add x 1 2848 end 2849 end 2850 2851 fn arx_hv_calc_band l ulong to f32 2852 d = mul get arx_cfg_hv_words 0 64 2853 cbase = mul l d 2854 sum = 0.0f32 2855 i = ulong 0 2856 while lt i d bound 8192 2857 sum = add sum f32 get arx_hv_counts add cbase i 2858 i = add i 1 2859 end 2860 mean = div sum f32 d 2861 vsum = 0.0f32 2862 i = ulong 0 2863 while lt i d bound 8192 2864 t = sub f32 get arx_hv_counts add cbase i mean 2865 vsum = add vsum mul t t 2866 i = add i 1 2867 end 2868 ret mul get arx_cfg_hv_zband 0 fsqrt div vsum f32 d 2869 end 2870 2871 fn arx_hv_quantize_word bbase ulong sidx ulong band f32 to uint 2872 sv = ulong 0 2873 mv = ulong 0 2874 b = ulong 0 2875 while lt b 64 bound 64 2876 v = f32 get arx_hv_counts add bbase b 2877 if gt v 0.0f32 2878 sv = or sv shl ulong 1 b 2879 end 2880 if or gt v band gt neg v band 2881 mv = or mv shl ulong 1 b 2882 end 2883 b = add b 1 2884 end 2885 put arx_hv_sign sidx sv 2886 put arx_hv_mask sidx mv 2887 ret arx_popcnt64 mv 2888 end 2889 2890 fn arx_hv_quantize l ulong to void 2891 band = arx_hv_calc_band l 2892 w = get arx_cfg_hv_words 0 2893 cbase = mul l mul w 64 2894 sbase = mul l w 2895 nnz = uint 0 2896 x = ulong 0 2897 while lt x w bound 128 2898 nnz = add nnz arx_hv_quantize_word add cbase mul x 64 add sbase x band 2899 x = add x 1 2900 end 2901 put arx_hv_nnz l nnz 2902 end 2903 2904 fn arx_hv_proto_sim_q l ulong to f32 2905 if eq get arx_hv_nlabel l 0 2906 ret -1.0f32 2907 end 2908 nnz = get arx_hv_nnz l 2909 if eq nnz 0 2910 ret -1.0f32 2911 end 2912 w = get arx_cfg_hv_words 0 2913 sbase = mul l w 2914 agree = long 0 2915 dis = long 0 2916 x = ulong 0 2917 while lt x w bound 128 2918 m = get arx_hv_mask add sbase x 2919 diff = xor get arx_hv_q x get arx_hv_sign add sbase x 2920 dis = add dis long arx_popcnt64 and diff m 2921 agree = add agree long arx_popcnt64 and not diff m 2922 x = add x 1 2923 end 2924 d = f32 mul w 64 2925 ret div f32 sub agree dis fsqrt mul f32 nnz d 2926 end 2927 2928 fn arx_hv_proto_sim_ex ex_idx ulong l ulong to f32 2929 if eq get arx_hv_nlabel l 0 2930 ret -1.0f32 2931 end 2932 nnz = get arx_hv_nnz l 2933 if eq nnz 0 2934 ret -1.0f32 2935 end 2936 w = get arx_cfg_hv_words 0 2937 sbase = mul l w 2938 hbase = mul ex_idx w 2939 net = long 0 2940 x = ulong 0 2941 while lt x w bound 128 2942 m = get arx_hv_mask add sbase x 2943 va = get arx_hv_hvs add hbase x 2944 vb = get arx_hv_sign add sbase x 2945 diff = xor va vb 2946 p_a = long arx_popcnt64 and not diff m 2947 p_d = long arx_popcnt64 and diff m 2948 net = add net sub p_a p_d 2949 x = add x 1 2950 end 2951 ret div f32 net fsqrt mul f32 nnz f32 mul w 64 2952 end 2953 2954 fn arx_hv_rebuild_pass to ulong 2955 sc = new f32 15 -9.0f32 2956 n = get arx_hv_n 0 2957 eta = get arx_cfg_hv_eta 0 2958 errs = ulong 0 2959 i = ulong 0 2960 while lt i n bound 256 2961 y = ulong get arx_hv_labels i 2962 l = ulong 0 2963 while lt l ARX_N_LABELS bound 15 2964 if gt get arx_hv_nlabel l 0 2965 put sc l arx_hv_proto_sim_ex i l 2966 else 2967 put sc l -9.0f32 2968 end 2969 l = add l 1 2970 end 2971 p = arx_argmax sc 2972 if ne p y 2973 errs = add errs 1 2974 wy = int mul mul eta sub 1.0f32 get sc y 64.0f32 2975 wp = int mul mul eta sub 1.0f32 get sc p 64.0f32 2976 if lt wy 1 2977 wy = 1 2978 end 2979 if lt wp 1 2980 wp = 1 2981 end 2982 arx_hv_add_counts y i wy 2983 arx_hv_add_counts p i neg wp 2984 arx_hv_quantize y 2985 arx_hv_quantize p 2986 end 2987 i = add i 1 2988 end 2989 ret errs 2990 end 2991 2992 fn arx_hv_rebuild to uint 2993 w = get arx_cfg_hv_words 0 2994 tot_c = mul ARX_N_LABELS mul w 64 2995 i = ulong 0 2996 while lt i tot_c bound 131072 2997 put arx_hv_counts i 0 2998 i = add i 1 2999 end 3000 n = get arx_hv_n 0 3001 i = ulong 0 3002 while lt i n bound 256 3003 arx_hv_add_counts ulong get arx_hv_labels i i 64 3004 i = add i 1 3005 end 3006 l = ulong 0 3007 while lt l ARX_N_LABELS bound 15 3008 arx_hv_quantize l 3009 l = add l 1 3010 end 3011 passes = uint 0 3012 epochs = get arx_cfg_hv_epochs 0 3013 ep = uint 0 3014 while lt ep epochs bound 100 3015 passes = add passes 1 3016 if eq arx_hv_rebuild_pass 0 3017 break 3018 end 3019 ep = add ep 1 3020 end 3021 ret passes 3022 end 3023 3024 fn arx_hv_online idx ulong wrong uchar to void 3025 y = ulong get arx_hv_labels idx 3026 sy = arx_hv_proto_sim_ex idx y 3027 if lt sy -1.0f32 3028 sy = -1.0f32 3029 end 3030 wy = int mul sub 1.0f32 sy 64.0f32 3031 if lt wy 1 3032 wy = 1 3033 end 3034 arx_hv_add_counts y idx wy 3035 arx_hv_quantize y 3036 if ne wrong ARX_ACT_INVALID 3037 p = ulong wrong 3038 if ne p y 3039 if gt get arx_hv_nlabel p 0 3040 sp = arx_hv_proto_sim_ex idx p 3041 wp = int mul mul add 1.0f32 sp 0.5f32 64.0f32 3042 if lt wp 1 3043 wp = 1 3044 end 3045 arx_hv_add_counts p idx neg wp 3046 arx_hv_quantize p 3047 end 3048 end 3049 end 3050 end 3051 3052 fn arx_scores_empty to void 3053 l = ulong 0 3054 while lt l ARX_N_LABELS bound 15 3055 put arx_sc_s l -9.0f32 3056 l = add l 1 3057 end 3058 put arx_sc_best_sim 0 0.0f32 3059 put arx_sc_has_near 0 0 3060 put arx_sc_nearest 0 0 3061 put arx_sc_agree 0 1.0f32 3062 end 3063 3064 fn arx_hv_score_agree gs view f32 8 gi view ulong 8 ka ulong to f32 3065 sc_tmp = new f32 15 0.0f32 3066 l = ulong 0 3067 while lt l ARX_N_LABELS bound 15 3068 put sc_tmp l get arx_sc_s l 3069 l = add l 1 3070 end 3071 tl = arx_argmax sc_tmp 3072 cnt_v = ulong 0 3073 cnt_m = ulong 0 3074 j = ulong 0 3075 while lt j ka bound 8 3076 if gt get gs j -2.0f32 3077 cnt_v = add cnt_v 1 3078 if eq ulong get arx_hv_labels get gi j tl 3079 cnt_m = add cnt_m 1 3080 end 3081 end 3082 j = add j 1 3083 end 3084 if eq cnt_v 0 3085 cnt_v = 1 3086 end 3087 ret div f32 cnt_m f32 cnt_v 3088 end 3089 3090 fn arx_hv_score_labels top view f32 120 kk ulong to void 3091 l = ulong 0 3092 while lt l ARX_N_LABELS bound 15 3093 nl = ulong get arx_hv_nlabel l 3094 if gt nl 0 3095 m = nl 3096 if gt m kk 3097 m = kk 3098 end 3099 sum = 0.0f32 3100 j = ulong 0 3101 while lt j m bound 8 3102 sum = add sum get top add mul l 8 j 3103 j = add j 1 3104 end 3105 al = get arx_cfg_hv_alpha 0 3106 pr = 0.0f32 3107 if lt al 1.0f32 3108 pr = arx_hv_proto_sim_q l 3109 end 3110 knn = div sum f32 m 3111 p1 = mul al knn 3112 p2 = mul sub 1.0f32 al pr 3113 put arx_sc_s l add p1 p2 3114 end 3115 l = add l 1 3116 end 3117 end 3118 3119 fn arx_hv_score_q to void 3120 top = new f32 120 -2.0f32 3121 gs = new f32 8 -2.0f32 3122 gi = new ulong 8 0 3123 arx_scores_empty 3124 n = get arx_hv_n 0 3125 if eq n 0 3126 ret 3127 end 3128 kk = get arx_cfg_hv_k 0 3129 if lt kk 1 3130 kk = 1 3131 end 3132 if gt kk 8 3133 kk = 8 3134 end 3135 ka = kk 3136 if lt ka 3 3137 ka = 3 3138 end 3139 i = ulong 0 3140 while lt i n bound 256 3141 s = arx_hv_sim_q_ex i get arx_cfg_hv_words 0 3142 l = ulong get arx_hv_labels i 3143 arx_ins_top top mul l 8 kk s 3144 arx_ins_glob gs gi ka s i 3145 i = add i 1 3146 end 3147 put arx_sc_best_sim 0 get gs 0 3148 put arx_sc_has_near 0 1 3149 put arx_sc_nearest 0 get gi 0 3150 arx_hv_score_labels top kk 3151 put arx_sc_agree 0 arx_hv_score_agree gs gi ka 3152 end 3153 3154 fn arx_hv_copy_ex dst ulong src ulong w ulong to void 3155 sb = mul src w 3156 db = mul dst w 3157 x = ulong 0 3158 while lt x w bound 128 3159 v = get arx_hv_hvs add sb x 3160 put arx_hv_hvs add db x v 3161 x = add x 1 3162 end 3163 put arx_hv_labels dst get arx_hv_labels src 3164 nlen = get arx_hv_nlen src 3165 put arx_hv_nlen dst nlen 3166 sb = mul src ARX_NOTE_BYTES 3167 db = mul dst ARX_NOTE_BYTES 3168 x = ulong 0 3169 while lt x nlen bound 96 3170 b = get arx_hv_notes add sb x 3171 put arx_hv_notes add db x b 3172 x = add x 1 3173 end 3174 end 3175 3176 fn arx_hv_sleep_compact n ulong w ulong to void 3177 dst = ulong 0 3178 l = ulong 0 3179 while lt l ARX_N_LABELS bound 15 3180 put arx_hv_nlabel l 0 3181 l = add l 1 3182 end 3183 i = ulong 0 3184 while lt i n bound 256 3185 if ne get arx_hv_keep i 0 3186 if ne dst i 3187 arx_hv_copy_ex dst i w 3188 end 3189 li = ulong get arx_hv_labels dst 3190 put arx_hv_nlabel li add get arx_hv_nlabel li 1 3191 dst = add dst 1 3192 end 3193 i = add i 1 3194 end 3195 put arx_hv_n 0 dst 3196 end 3197 3198 fn arx_hv_sleep_add_conflict a ulong b ulong simv f32 to void 3199 cn = get arx_slp_con_n 0 3200 p = ulong 0 3201 if lt cn ARX_MAX_CONFLICTS 3202 put arx_slp_con_a cn a 3203 put arx_slp_con_b cn b 3204 put arx_slp_con_sim cn simv 3205 p = cn 3206 put arx_slp_con_n 0 add cn 1 3207 else 3208 if le simv get arx_slp_con_sim sub ARX_MAX_CONFLICTS 1 3209 ret 3210 end 3211 p = sub ARX_MAX_CONFLICTS 1 3212 put arx_slp_con_a p a 3213 put arx_slp_con_b p b 3214 put arx_slp_con_sim p simv 3215 end 3216 while gt p 0 bound 32 3217 cs = get arx_slp_con_sim p 3218 ps = get arx_slp_con_sim sub p 1 3219 if le cs ps 3220 break 3221 end 3222 ca = get arx_slp_con_a p 3223 cb = get arx_slp_con_b p 3224 pa = get arx_slp_con_a sub p 1 3225 pb = get arx_slp_con_b sub p 1 3226 put arx_slp_con_a p pa 3227 put arx_slp_con_b p pb 3228 put arx_slp_con_sim p ps 3229 put arx_slp_con_a sub p 1 ca 3230 put arx_slp_con_b sub p 1 cb 3231 put arx_slp_con_sim sub p 1 cs 3232 p = sub p 1 3233 end 3234 end 3235 3236 fn arx_hv_sleep dup f32 conflict f32 to void 3237 w = get arx_cfg_hv_words 0 3238 n = get arx_hv_n 0 3239 i = ulong 0 3240 while lt i n bound 256 3241 put arx_hv_keep i 1 3242 i = add i 1 3243 end 3244 merged = ulong 0 3245 i = ulong 0 3246 while lt i n bound 256 3247 if ne get arx_hv_keep i 0 3248 j = add i 1 3249 while lt j n bound 256 3250 if ne get arx_hv_keep j 0 3251 if eq get arx_hv_labels i get arx_hv_labels j 3252 if ge arx_hv_sim_ex_ex i j w dup 3253 put arx_hv_keep j 0 3254 merged = add merged 1 3255 end 3256 end 3257 end 3258 j = add j 1 3259 end 3260 end 3261 i = add i 1 3262 end 3263 if gt merged 0 3264 arx_hv_sleep_compact n w 3265 end 3266 n2 = get arx_hv_n 0 3267 put arx_slp_con_n 0 0 3268 i = ulong 0 3269 while lt i n2 bound 256 3270 j = add i 1 3271 while lt j n2 bound 256 3272 if ne get arx_hv_labels i get arx_hv_labels j 3273 s = arx_hv_sim_ex_ex i j w 3274 if ge s conflict 3275 arx_hv_sleep_add_conflict i j s 3276 end 3277 end 3278 j = add j 1 3279 end 3280 i = add i 1 3281 end 3282 passes = arx_hv_rebuild 3283 put arx_slp_merged 0 merged 3284 put arx_slp_kept 0 n2 3285 put arx_slp_passes 0 passes 3286 end 3287 3288 note --- Sparse Feature Helpers & NearestModel --- 3289 3290 fn arx_sparse_build_q to ulong 3291 n = get arx_ft_n 0 3292 i = ulong 0 3293 while lt i n bound 768 3294 put arx_sp_q_k i arx_feat_key i 3295 put arx_sp_q_w i uint get arx_ft_w i 3296 i = add i 1 3297 end 3298 note Insertion sort by key (n <= 768) 3299 i = ulong 1 3300 while lt i n bound 768 3301 kk = get arx_sp_q_k i 3302 kw = get arx_sp_q_w i 3303 j = i 3304 while gt j 0 bound 768 3305 pk = get arx_sp_q_k sub j 1 3306 if le pk kk 3307 break 3308 end 3309 put arx_sp_q_k j pk 3310 put arx_sp_q_w j get arx_sp_q_w sub j 1 3311 j = sub j 1 3312 end 3313 put arx_sp_q_k j kk 3314 put arx_sp_q_w j kw 3315 i = add i 1 3316 end 3317 note Merge duplicates 3318 if eq n 0 3319 put arx_sp_q_n 0 0 3320 ret 0 3321 end 3322 m = ulong 1 3323 i = ulong 1 3324 while lt i n bound 768 3325 kk = get arx_sp_q_k i 3326 kw = get arx_sp_q_w i 3327 if eq kk get arx_sp_q_k sub m 1 3328 put arx_sp_q_w sub m 1 add get arx_sp_q_w sub m 1 kw 3329 else 3330 put arx_sp_q_k m kk 3331 put arx_sp_q_w m kw 3332 m = add m 1 3333 end 3334 i = add i 1 3335 end 3336 put arx_sp_q_n 0 m 3337 ret m 3338 end 3339 3340 fn arx_dice_tot bo ulong nb ulong to ulong 3341 na = get arx_sp_q_n 0 3342 tot = ulong 0 3343 i = ulong 0 3344 while lt i na bound 768 3345 tot = add tot ulong get arx_sp_q_w i 3346 i = add i 1 3347 end 3348 j = ulong 0 3349 while lt j nb bound 768 3350 tot = add tot ulong get arx_nr_pool_w add bo j 3351 j = add j 1 3352 end 3353 ret tot 3354 end 3355 3356 fn arx_dice_q_nr ex_idx ulong to f32 3357 na = get arx_sp_q_n 0 3358 bo = get arx_nr_off ex_idx 3359 nb = get arx_nr_len ex_idx 3360 i = ulong 0 3361 j = ulong 0 3362 inter = ulong 0 3363 while lt i na bound 1600 3364 if ge j nb 3365 break 3366 end 3367 ka = get arx_sp_q_k i 3368 kb = get arx_nr_pool_k add bo j 3369 if eq ka kb 3370 mn = get arx_sp_q_w i 3371 if lt get arx_nr_pool_w add bo j mn 3372 mn = get arx_nr_pool_w add bo j 3373 end 3374 inter = add inter ulong mn 3375 i = add i 1 3376 j = add j 1 3377 else 3378 if lt ka kb 3379 i = add i 1 3380 else 3381 j = add j 1 3382 end 3383 end 3384 end 3385 tot = arx_dice_tot bo nb 3386 if eq tot 0 3387 ret 0.0f32 3388 end 3389 ret div mul 2.0f32 f32 inter f32 tot 3390 end 3391 3392 fn arx_nr_reset to void 3393 put arx_nr_n 0 0 3394 put arx_nr_pool_n 0 0 3395 l = ulong 0 3396 while lt l ARX_N_LABELS bound 15 3397 put arx_nr_nlabel l 0 3398 l = add l 1 3399 end 3400 end 3401 3402 fn arx_nr_push_q lbl uchar note_s view uchar * to long 3403 n = get arx_nr_n 0 3404 if ge n get arx_cfg_max_ex 0 3405 ret -1 3406 end 3407 if ge n ARX_MAX_EXEMPLARS 3408 ret -1 3409 end 3410 qn = get arx_sp_q_n 0 3411 po = get arx_nr_pool_n 0 3412 if gt add po qn ARX_SPARSE_POOL 3413 ret -1 3414 end 3415 i = ulong 0 3416 while lt i qn bound 768 3417 put arx_nr_pool_k add po i get arx_sp_q_k i 3418 put arx_nr_pool_w add po i get arx_sp_q_w i 3419 i = add i 1 3420 end 3421 put arx_nr_off n po 3422 put arx_nr_len n qn 3423 put arx_nr_pool_n 0 add po qn 3424 put arx_nr_labels n lbl 3425 nlen = len note_s 3426 if gt nlen ARX_NOTE_BYTES 3427 nlen = ARX_NOTE_BYTES 3428 end 3429 nbase = mul n ARX_NOTE_BYTES 3430 k = ulong 0 3431 while lt k nlen bound 96 3432 put arx_nr_notes add nbase k get note_s k 3433 k = add k 1 3434 end 3435 put arx_nr_nlen n nlen 3436 li = ulong lbl 3437 put arx_nr_nlabel li add get arx_nr_nlabel li 1 3438 put arx_nr_n 0 add n 1 3439 ret long n 3440 end 3441 3442 fn arx_nr_score_agree gs view f32 8 gi view ulong 8 ka ulong to f32 3443 sc_tmp = new f32 15 0.0f32 3444 l = ulong 0 3445 while lt l ARX_N_LABELS bound 15 3446 put sc_tmp l get arx_sc_s l 3447 l = add l 1 3448 end 3449 tl = arx_argmax sc_tmp 3450 cnt_v = ulong 0 3451 cnt_m = ulong 0 3452 j = ulong 0 3453 while lt j ka bound 8 3454 if gt get gs j -2.0f32 3455 cnt_v = add cnt_v 1 3456 if eq ulong get arx_nr_labels get gi j tl 3457 cnt_m = add cnt_m 1 3458 end 3459 end 3460 j = add j 1 3461 end 3462 if eq cnt_v 0 3463 cnt_v = 1 3464 end 3465 ret div f32 cnt_m f32 cnt_v 3466 end 3467 3468 fn arx_nr_score_labels top view f32 120 kk ulong to void 3469 l = ulong 0 3470 while lt l ARX_N_LABELS bound 15 3471 nl = ulong get arx_nr_nlabel l 3472 if gt nl 0 3473 m = nl 3474 if gt m kk 3475 m = kk 3476 end 3477 sum = 0.0f32 3478 j = ulong 0 3479 while lt j m bound 8 3480 sum = add sum get top add mul l 8 j 3481 j = add j 1 3482 end 3483 put arx_sc_s l div sum f32 m 3484 end 3485 l = add l 1 3486 end 3487 end 3488 3489 fn arx_nr_score_q to void 3490 top = new f32 120 -2.0f32 3491 gs = new f32 8 -2.0f32 3492 gi = new ulong 8 0 3493 arx_scores_empty 3494 n = get arx_nr_n 0 3495 if eq n 0 3496 ret 3497 end 3498 kk = get arx_cfg_hv_k 0 3499 if lt kk 1 3500 kk = 1 3501 end 3502 if gt kk 8 3503 kk = 8 3504 end 3505 ka = kk 3506 if lt ka 3 3507 ka = 3 3508 end 3509 i = ulong 0 3510 while lt i n bound 256 3511 d = arx_dice_q_nr i 3512 l = ulong get arx_nr_labels i 3513 arx_ins_top top mul l 8 kk d 3514 arx_ins_glob gs gi ka d i 3515 i = add i 1 3516 end 3517 put arx_sc_best_sim 0 get gs 0 3518 put arx_sc_has_near 0 1 3519 put arx_sc_nearest 0 get gi 0 3520 arx_nr_score_labels top kk 3521 put arx_sc_agree 0 arx_nr_score_agree gs gi ka 3522 end 3523 3524 note --- PerceptronModel --- 3525 3526 fn arx_pc_clear_weights to void 3527 i = ulong 0 3528 while lt i ARX_PERC_CAP bound 8192 3529 put arx_pc_w_used i 0 3530 put arx_pc_u_used i 0 3531 i = add i 1 3532 end 3533 put arx_pc_w_cnt 0 0 3534 put arx_pc_u_cnt 0 0 3535 end 3536 3537 fn arx_pc_reset to void 3538 arx_pc_clear_weights 3539 put arx_pc_tr_n 0 0 3540 put arx_pc_pool_n 0 0 3541 l = ulong 0 3542 while lt l ARX_N_LABELS bound 15 3543 put arx_pc_nlabel l 0 3544 l = add l 1 3545 end 3546 end 3547 3548 fn arx_pc_w_find key ulong create bool to long 3549 slot = and key sub ARX_PERC_CAP 1 3550 step = ulong 0 3551 while lt step ARX_PERC_CAP bound 8192 3552 if eq get arx_pc_w_used slot 0 3553 if not create 3554 ret -1 3555 end 3556 if ge get arx_pc_w_cnt 0 sub ARX_PERC_CAP 64 3557 ret -1 3558 end 3559 put arx_pc_w_used slot 1 3560 put arx_pc_w_key slot key 3561 base = mul slot ARX_N_LABELS 3562 l = ulong 0 3563 while lt l ARX_N_LABELS bound 15 3564 put arx_pc_w_val add base l 0.0f32 3565 l = add l 1 3566 end 3567 put arx_pc_w_cnt 0 add get arx_pc_w_cnt 0 1 3568 ret long slot 3569 end 3570 if eq get arx_pc_w_key slot key 3571 ret long slot 3572 end 3573 slot = and add slot 1 sub ARX_PERC_CAP 1 3574 step = add step 1 3575 end 3576 ret -1 3577 end 3578 3579 fn arx_pc_u_find key ulong create bool to long 3580 slot = and key sub ARX_PERC_CAP 1 3581 step = ulong 0 3582 while lt step ARX_PERC_CAP bound 8192 3583 if eq get arx_pc_u_used slot 0 3584 if not create 3585 ret -1 3586 end 3587 if ge get arx_pc_u_cnt 0 sub ARX_PERC_CAP 64 3588 ret -1 3589 end 3590 put arx_pc_u_used slot 1 3591 put arx_pc_u_key slot key 3592 base = mul slot ARX_N_LABELS 3593 l = ulong 0 3594 while lt l ARX_N_LABELS bound 15 3595 put arx_pc_u_val add base l 0.0f32 3596 l = add l 1 3597 end 3598 put arx_pc_u_cnt 0 add get arx_pc_u_cnt 0 1 3599 ret long slot 3600 end 3601 if eq get arx_pc_u_key slot key 3602 ret long slot 3603 end 3604 slot = and add slot 1 sub ARX_PERC_CAP 1 3605 step = add step 1 3606 end 3607 ret -1 3608 end 3609 3610 fn arx_pc_push_q lbl uchar to void 3611 n = get arx_pc_tr_n 0 3612 if ge n ARX_MAX_EXEMPLARS 3613 ret 3614 end 3615 qn = get arx_sp_q_n 0 3616 po = get arx_pc_pool_n 0 3617 if gt add po qn ARX_SPARSE_POOL 3618 ret 3619 end 3620 i = ulong 0 3621 while lt i qn bound 768 3622 put arx_pc_pool_k add po i get arx_sp_q_k i 3623 put arx_pc_pool_w add po i get arx_sp_q_w i 3624 i = add i 1 3625 end 3626 put arx_pc_tr_off n po 3627 put arx_pc_tr_len n qn 3628 put arx_pc_pool_n 0 add po qn 3629 put arx_pc_tr_lbl n lbl 3630 li = ulong lbl 3631 put arx_pc_nlabel li add get arx_pc_nlabel li 1 3632 put arx_pc_tr_n 0 add n 1 3633 end 3634 3635 fn arx_pc_raw_tr tr_idx ulong sc edit f32 * to void 3636 l = ulong 0 3637 while lt l ARX_N_LABELS bound 15 3638 put sc l 0.0f32 3639 l = add l 1 3640 end 3641 off = get arx_pc_tr_off tr_idx 3642 xn = get arx_pc_tr_len tr_idx 3643 i = ulong 0 3644 while lt i xn bound 768 3645 sl = arx_pc_w_find get arx_pc_pool_k add off i false 3646 if ge sl 0 3647 vf = f32 get arx_pc_pool_w add off i 3648 base = mul ulong sl ARX_N_LABELS 3649 l = ulong 0 3650 while lt l ARX_N_LABELS bound 15 3651 wv = get arx_pc_w_val add base l 3652 nv = add get sc l mul wv vf 3653 put sc l nv 3654 l = add l 1 3655 end 3656 end 3657 i = add i 1 3658 end 3659 end 3660 3661 fn arx_pc_add_u fk ulong cv f32 y ulong p ulong to void 3662 us = arx_pc_u_find fk true 3663 if ge us 0 3664 ub = mul ulong us ARX_N_LABELS 3665 uy = add ub y 3666 up = add ub p 3667 put arx_pc_u_val uy add get arx_pc_u_val uy cv 3668 put arx_pc_u_val up sub get arx_pc_u_val up cv 3669 end 3670 end 3671 3672 fn arx_pc_fit_update_one fk ulong vf f32 y ulong p ulong c f32 to void 3673 ws = arx_pc_w_find fk true 3674 if ge ws 0 3675 wb = mul ulong ws ARX_N_LABELS 3676 iy = add wb y 3677 ip = add wb p 3678 put arx_pc_w_val iy add get arx_pc_w_val iy vf 3679 put arx_pc_w_val ip sub get arx_pc_w_val ip vf 3680 arx_pc_add_u fk mul c vf y p 3681 end 3682 end 3683 3684 fn arx_pc_fit_update idx ulong y ulong p ulong c f32 to void 3685 off = get arx_pc_tr_off idx 3686 xn = get arx_pc_tr_len idx 3687 k = ulong 0 3688 while lt k xn bound 768 3689 pi = add off k 3690 fk = get arx_pc_pool_k pi 3691 vf = f32 get arx_pc_pool_w pi 3692 arx_pc_fit_update_one fk vf y p c 3693 k = add k 1 3694 end 3695 end 3696 3697 fn arx_pc_fit_avg c f32 to void 3698 s = ulong 0 3699 while lt s ARX_PERC_CAP bound 8192 3700 if ne get arx_pc_w_used s 0 3701 us = arx_pc_u_find get arx_pc_w_key s false 3702 if ge us 0 3703 wb = mul s ARX_N_LABELS 3704 ub = mul ulong us ARX_N_LABELS 3705 l = ulong 0 3706 while lt l ARX_N_LABELS bound 15 3707 wi = add wb l 3708 ui = add ub l 3709 uv = div get arx_pc_u_val ui c 3710 wv = get arx_pc_w_val wi 3711 put arx_pc_w_val wi sub wv uv 3712 l = add l 1 3713 end 3714 end 3715 end 3716 s = add s 1 3717 end 3718 end 3719 3720 fn arx_pc_fit to void 3721 sc = new f32 15 0.0f32 3722 arx_pc_clear_weights 3723 n = get arx_pc_tr_n 0 3724 if eq n 0 3725 ret 3726 end 3727 i = ulong 0 3728 while lt i n bound 256 3729 put arx_pc_order i i 3730 i = add i 1 3731 end 3732 c = 1.0f32 3733 rng = ARX_SEED 3734 ep = uint 0 3735 while lt ep get arx_cfg_perc_epochs 0 bound 100 3736 if gt n 1 3737 i = sub n 1 3738 while gt i 0 bound 256 3739 rng = add rng ulong 0x9e3779b97f4a7c15 3740 j = rem arx_splitmix_next rng add i 1 3741 ti = get arx_pc_order i 3742 put arx_pc_order i get arx_pc_order j 3743 put arx_pc_order j ti 3744 i = sub i 1 3745 end 3746 end 3747 oi = ulong 0 3748 while lt oi n bound 256 3749 idx = get arx_pc_order oi 3750 y = ulong get arx_pc_tr_lbl idx 3751 arx_pc_raw_tr idx sc 3752 l = ulong 0 3753 while lt l ARX_N_LABELS bound 15 3754 if eq get arx_pc_nlabel l 0 3755 put sc l -1.`0e38f32` 3756 end 3757 l = add l 1 3758 end 3759 p = arx_argmax sc 3760 if ne p y 3761 arx_pc_fit_update idx y p c 3762 end 3763 c = add c 1.0f32 3764 oi = add oi 1 3765 end 3766 ep = add ep 1 3767 end 3768 arx_pc_fit_avg c 3769 end 3770 3771 fn arx_pc_score_q to void 3772 raw = new f32 15 0.0f32 3773 arx_scores_empty 3774 qn = get arx_sp_q_n 0 3775 tot = 0.0f32 3776 i = ulong 0 3777 while lt i qn bound 768 3778 vf = f32 get arx_sp_q_w i 3779 tot = add tot vf 3780 sl = arx_pc_w_find get arx_sp_q_k i false 3781 if ge sl 0 3782 base = mul ulong sl ARX_N_LABELS 3783 l = ulong 0 3784 while lt l ARX_N_LABELS bound 15 3785 wv = get arx_pc_w_val add base l 3786 nv = add get raw l mul wv vf 3787 put raw l nv 3788 l = add l 1 3789 end 3790 end 3791 i = add i 1 3792 end 3793 if lt tot 1.0f32 3794 tot = 1.0f32 3795 end 3796 l = ulong 0 3797 while lt l ARX_N_LABELS bound 15 3798 if gt get arx_pc_nlabel l 0 3799 put arx_sc_s l div get raw l tot 3800 end 3801 l = add l 1 3802 end 3803 put arx_sc_best_sim 0 1.0f32 3804 put arx_sc_agree 0 1.0f32 3805 end 3806 3807 fn arx_pc_online_step y ulong p ulong to void 3808 qn = get arx_sp_q_n 0 3809 i = ulong 0 3810 while lt i qn bound 768 3811 ws = arx_pc_w_find get arx_sp_q_k i true 3812 if ge ws 0 3813 vf = f32 get arx_sp_q_w i 3814 wb = mul ulong ws ARX_N_LABELS 3815 iy = add wb y 3816 ny = add get arx_pc_w_val iy vf 3817 put arx_pc_w_val iy ny 3818 ip = add wb p 3819 np = sub get arx_pc_w_val ip vf 3820 put arx_pc_w_val ip np 3821 end 3822 i = add i 1 3823 end 3824 end 3825 3826 fn arx_pc_online_q lbl uchar to void 3827 sc_tmp = new f32 15 0.0f32 3828 y = ulong lbl 3829 arx_pc_push_q lbl 3830 step = ulong 0 3831 while lt step 8 bound 8 3832 arx_pc_score_q 3833 l = ulong 0 3834 while lt l ARX_N_LABELS bound 15 3835 put sc_tmp l get arx_sc_s l 3836 l = add l 1 3837 end 3838 p = arx_argmax sc_tmp 3839 if eq p y 3840 break 3841 end 3842 arx_pc_online_step y p 3843 step = add step 1 3844 end 3845 end 3846 3847 note ========================================================================= 3848 note Section 12: Reflex Core API 3849 note ========================================================================= 3850 3851 fn arx_cfg_frozen mode uchar to void 3852 put arx_cfg_mode 0 mode 3853 put arx_cfg_hv_words 0 ARX_HV_WORDS 3854 put arx_cfg_hv_k 0 3 3855 put arx_cfg_hv_alpha 0 0.5f32 3856 put arx_cfg_hv_zband 0 0.42f32 3857 put arx_cfg_hv_epochs 0 40 3858 put arx_cfg_hv_eta 0 0.5f32 3859 put arx_cfg_max_ex 0 ARX_MAX_EXEMPLARS 3860 put arx_cfg_t_margin 0 0.0f32 3861 put arx_cfg_t_abs 0 -1.0f32 3862 put arx_cfg_abs_label 0 ARX_ACT_NONE 3863 put arx_cfg_t_agree 0 0.0f32 3864 put arx_cfg_exact 0 0.97f32 3865 put arx_cfg_perc_epochs 0 20 3866 put arx_cfg_sleep_dup 0 0.95f32 3867 put arx_cfg_sleep_con 0 0.80f32 3868 put arx_cfg_max_msg 0 2048 3869 put arx_cfg_max_toks 0 256 3870 put arx_cfg_max_turns 0 16 3871 if eq mode ARX_MODE_REFLEX 3872 put arx_cfg_weights 0 3 3873 put arx_cfg_weights 1 0 3874 put arx_cfg_weights 2 1 3875 put arx_cfg_weights 3 6 3876 put arx_cfg_weights 4 4 3877 put arx_cfg_weights 5 3 3878 put arx_cfg_weights 6 3 3879 put arx_cfg_weights 7 2 3880 else 3881 if eq mode ARX_MODE_NEAREST 3882 put arx_cfg_weights 0 0 3883 put arx_cfg_weights 1 2 3884 put arx_cfg_weights 2 0 3885 put arx_cfg_weights 3 6 3886 put arx_cfg_weights 4 4 3887 put arx_cfg_weights 5 0 3888 put arx_cfg_weights 6 3 3889 put arx_cfg_weights 7 2 3890 put arx_cfg_hv_k 0 5 3891 else 3892 put arx_cfg_weights 0 0 3893 put arx_cfg_weights 1 2 3894 put arx_cfg_weights 2 1 3895 put arx_cfg_weights 3 4 3896 put arx_cfg_weights 4 4 3897 put arx_cfg_weights 5 3 3898 put arx_cfg_weights 6 3 3899 put arx_cfg_weights 7 2 3900 put arx_cfg_perc_epochs 0 10 3901 end 3902 end 3903 end 3904 3905 fn arx_init mode uchar to void 3906 arx_cfg_frozen mode 3907 arx_hv_reset 3908 arx_nr_reset 3909 arx_pc_reset 3910 arx_cue_reset 3911 put arx_learning 0 1 3912 end 3913 3914 fn arx_set_learning on bool to void 3915 if on 3916 put arx_learning 0 1 3917 else 3918 put arx_learning 0 0 3919 end 3920 end 3921 3922 fn arx_exemplars to ulong 3923 m = get arx_cfg_mode 0 3924 if eq m ARX_MODE_REFLEX 3925 ret get arx_hv_n 0 3926 end 3927 if eq m ARX_MODE_NEAREST 3928 ret get arx_nr_n 0 3929 end 3930 ret 0 3931 end 3932 3933 fn arx_exemplar_label idx ulong to uchar 3934 m = get arx_cfg_mode 0 3935 if eq m ARX_MODE_REFLEX 3936 if lt idx get arx_hv_n 0 3937 ret get arx_hv_labels idx 3938 end 3939 end 3940 if eq m ARX_MODE_NEAREST 3941 if lt idx get arx_nr_n 0 3942 ret get arx_nr_labels idx 3943 end 3944 end 3945 ret ARX_ACT_INVALID 3946 end 3947 3948 fn arx_analyse to uchar 3949 msg = new uchar 4096 0 3950 arx_dialog_state get arx_cfg_max_turns 0 3951 if eq get arx_dlg_has_user 0 0 3952 ret ARX_WHY_NO_USER_TURN 3953 end 3954 lu = get arx_dlg_last_user 0 3955 if gt get arx_turn_raw_len lu get arx_cfg_max_msg 0 3956 ret ARX_WHY_INPUT_TOO_LONG 3957 end 3958 mlen = arx_load_turn_buf lu msg 3959 i = ulong 0 3960 while lt i mlen bound 4096 3961 put arx_dlg_msg i get msg i 3962 i = add i 1 3963 end 3964 put arx_dlg_msg_len 0 mlen 3965 tn = arx_tokenize part msg 0 mlen get arx_cfg_max_toks 0 3966 has_al = false 3967 i = ulong 0 3968 while lt i tn bound 256 3969 if ne get arx_tok_alnum i 0 3970 has_al = true 3971 break 3972 end 3973 i = add i 1 3974 end 3975 if not has_al 3976 ret ARX_WHY_NO_WORDS 3977 end 3978 cn_ign = arx_clauses 3979 if not arx_primary 3980 ret ARX_WHY_NO_WORDS 3981 end 3982 ret ARX_WHY_SCORED 3983 end 3984 3985 fn arx_build_feats to ulong 3986 cs = get arx_pri_start 0 3987 ce = get arx_pri_end 0 3988 from = get arx_tok_end sub ce 1 3989 to_b = get arx_dlg_msg_len 0 3990 if lt ce get arx_tok_n 0 3991 to_b = get arx_tok_start ce 3992 end 3993 ends_q = false 3994 i = from 3995 while lt i to_b bound 4096 3996 if eq get arx_dlg_msg i 63 3997 ends_q = true 3998 break 3999 end 4000 i = add i 1 4001 end 4002 ret arx_extract_features cs ce ends_q 4003 end 4004 4005 fn arx_score_current to void 4006 m = get arx_cfg_mode 0 4007 if eq m ARX_MODE_REFLEX 4008 arx_hv_encode_to_q get arx_cfg_hv_words 0 4009 arx_hv_score_q 4010 else 4011 sp_ign = arx_sparse_build_q 4012 if eq m ARX_MODE_NEAREST 4013 arx_nr_score_q 4014 else 4015 arx_pc_score_q 4016 end 4017 end 4018 end 4019 4020 fn arx_exp_reset why uchar to void 4021 put arx_exp_why 0 why 4022 put arx_exp_refuse 0 ARX_REFUSE_NONE 4023 put arx_exp_span_err 0 ARX_SPAN_OK 4024 put arx_exp_top 0 ARX_ACT_CLARIFY 4025 put arx_exp_second 0 ARX_ACT_CLARIFY 4026 put arx_exp_margin 0 0.0f32 4027 put arx_exp_best_sim 0 0.0f32 4028 put arx_exp_agree 0 0.0f32 4029 put arx_exp_has_near 0 0 4030 put arx_exp_near_act 0 ARX_ACT_INVALID 4031 put arx_exp_near_idx 0 0 4032 put arx_exp_span_bs 0 0 4033 put arx_exp_span_be 0 0 4034 put arx_dec_has_txt 0 0 4035 put arx_dec_txt_len 0 0 4036 end 4037 4038 fn arx_decide_select top ulong sec ulong margin f32 to uchar 4039 arx_exp_reset ARX_WHY_SCORED 4040 put arx_exp_top 0 uchar top 4041 put arx_exp_second 0 uchar sec 4042 put arx_exp_margin 0 margin 4043 put arx_exp_best_sim 0 get arx_sc_best_sim 0 4044 put arx_exp_agree 0 get arx_sc_agree 0 4045 nr_lbl = ARX_ACT_INVALID 4046 if ne get arx_sc_has_near 0 0 4047 ni = get arx_sc_nearest 0 4048 nr_lbl = arx_exemplar_label ni 4049 put arx_exp_has_near 0 1 4050 put arx_exp_near_idx 0 ni 4051 put arx_exp_near_act 0 nr_lbl 4052 end 4053 act = uchar top 4054 exact_ok = false 4055 if ge get arx_sc_best_sim 0 get arx_cfg_exact 0 4056 if ne nr_lbl ARX_ACT_INVALID 4057 blocked = false 4058 if eq get arx_dlg_pending 0 ARX_PEND_NO 4059 if arx_act_needs_pending nr_lbl 4060 blocked = true 4061 end 4062 end 4063 if not blocked 4064 exact_ok = true 4065 end 4066 end 4067 end 4068 if exact_ok 4069 act = nr_lbl 4070 put arx_exp_why 0 ARX_WHY_EXACT_RECALL 4071 else 4072 if lt get arx_sc_best_sim 0 get arx_cfg_t_abs 0 4073 act = get arx_cfg_abs_label 0 4074 put arx_exp_why 0 ARX_WHY_LOW_SIMILARITY 4075 else 4076 if and lt margin get arx_cfg_t_margin 0 ne act ARX_ACT_CLARIFY 4077 act = ARX_ACT_CLARIFY 4078 put arx_exp_why 0 ARX_WHY_LOW_MARGIN 4079 else 4080 if and lt get arx_sc_agree 0 get arx_cfg_t_agree 0 ne act ARX_ACT_CLARIFY 4081 act = ARX_ACT_CLARIFY 4082 put arx_exp_why 0 ARX_WHY_LOW_AGREEMENT 4083 end 4084 end 4085 end 4086 end 4087 ret act 4088 end 4089 4090 fn arx_decide_extract_span act uchar to uchar 4091 msg = new uchar 4096 0 4092 mlen = get arx_dlg_msg_len 0 4093 i = ulong 0 4094 while lt i mlen bound 4096 4095 put msg i get arx_dlg_msg i 4096 i = add i 1 4097 end 4098 se = arx_cue_extract part msg 0 mlen get arx_pri_start 0 get arx_pri_end 0 act 4099 if eq se ARX_SPAN_OK 4100 put arx_dec_act 0 act 4101 put arx_dec_has_txt 0 1 4102 ret act 4103 end 4104 put arx_exp_why 0 ARX_WHY_SPAN 4105 put arx_exp_span_err 0 se 4106 put arx_dec_act 0 ARX_ACT_CLARIFY 4107 ret ARX_ACT_CLARIFY 4108 end 4109 4110 fn arx_decide to uchar 4111 sc_arr = new f32 15 0.0f32 4112 st = arx_analyse 4113 if ne st ARX_WHY_SCORED 4114 arx_exp_reset st 4115 put arx_dec_act 0 ARX_ACT_CLARIFY 4116 ret ARX_ACT_CLARIFY 4117 end 4118 rr = arx_policy_check 4119 if ne rr ARX_REFUSE_NONE 4120 arx_exp_reset ARX_WHY_POLICY 4121 put arx_exp_refuse 0 rr 4122 put arx_dec_act 0 ARX_ACT_REFUSE 4123 ret ARX_ACT_REFUSE 4124 end 4125 fn_ign = arx_build_feats 4126 arx_score_current 4127 if eq get arx_dlg_pending 0 ARX_PEND_NO 4128 put arx_sc_s ulong ARX_ACT_APPROVE -9.0f32 4129 put arx_sc_s ulong ARX_ACT_DENY -9.0f32 4130 put arx_sc_s ulong ARX_ACT_CANCEL -9.0f32 4131 end 4132 l = ulong 0 4133 while lt l ARX_N_LABELS bound 15 4134 put sc_arr l get arx_sc_s l 4135 l = add l 1 4136 end 4137 top = arx_argmax sc_arr 4138 top_s = get sc_arr top 4139 put sc_arr top -1.`0e38f32` 4140 sec = arx_argmax sc_arr 4141 act = arx_decide_select top sec sub top_s get sc_arr sec 4142 if arx_act_has_text act 4143 ret arx_decide_extract_span act 4144 end 4145 put arx_dec_act 0 act 4146 ret act 4147 end 4148 4149 text arx_s_json_p1 "{\"action\":\"" 4150 text arx_s_json_p2 "\",\"args\":{" 4151 text arx_s_json_p3 "\"text\":\"" 4152 text arx_s_json_p4 "\"}}" 4153 text arx_s_json_p5 "}}" 4154 4155 fn arx_decision_json dst edit uchar * to ulong 4156 p = ulong 0 4157 p = arx_copy_bytes dst p arx_s_json_p1 4158 p = arx_act_write_name get arx_dec_act 0 dst p 4159 p = arx_copy_bytes dst p arx_s_json_p2 4160 if ne get arx_dec_has_txt 0 0 4161 p = arx_copy_bytes dst p arx_s_json_p3 4162 tn = get arx_dec_txt_len 0 4163 i = ulong 0 4164 while lt i tn bound 64 4165 put dst add p i get arx_dec_txt i 4166 i = add i 1 4167 end 4168 p = add p tn 4169 p = arx_copy_bytes dst p arx_s_json_p4 4170 else 4171 p = arx_copy_bytes dst p arx_s_json_p5 4172 end 4173 ret p 4174 end 4175 4176 fn arx_store_model lbl uchar wrong uchar online bool msg view uchar * to uchar 4177 m = get arx_cfg_mode 0 4178 if eq m ARX_MODE_REFLEX 4179 arx_hv_encode_to_q get arx_cfg_hv_words 0 4180 idx = arx_hv_push_q lbl msg 4181 if lt idx 0 4182 ret ARX_LEARN_FULL 4183 end 4184 if online 4185 arx_hv_online ulong idx wrong 4186 end 4187 ret ARX_LEARN_OK 4188 end 4189 sp_ign = arx_sparse_build_q 4190 if eq m ARX_MODE_NEAREST 4191 idx = arx_nr_push_q lbl msg 4192 if lt idx 0 4193 ret ARX_LEARN_FULL 4194 end 4195 ret ARX_LEARN_OK 4196 end 4197 if online 4198 arx_pc_online_q lbl 4199 else 4200 arx_pc_push_q lbl 4201 end 4202 ret ARX_LEARN_OK 4203 end 4204 4205 fn arx_store_cue lbl uchar msg view uchar * txt view uchar * to void 4206 bs = arx_find_ci msg txt 4207 if ge bs 0 4208 cs = get arx_pri_start 0 4209 ce = get arx_pri_end 0 4210 ub = ulong bs 4211 ue = add ub len txt 4212 cl_ign = arx_cue_learn cs ce lbl ub ue 4213 end 4214 end 4215 4216 fn arx_store lbl uchar has_txt bool txt view uchar * wrong uchar online bool to uchar 4217 msg = new uchar 4096 0 4218 if ne arx_analyse ARX_WHY_SCORED 4219 ret ARX_LEARN_NO_USER 4220 end 4221 fn_ign = arx_build_feats 4222 mlen = get arx_dlg_msg_len 0 4223 i = ulong 0 4224 while lt i mlen bound 4096 4225 put msg i get arx_dlg_msg i 4226 i = add i 1 4227 end 4228 res = arx_store_model lbl wrong online part msg 0 mlen 4229 if ne res ARX_LEARN_OK 4230 ret res 4231 end 4232 if has_txt 4233 if arx_act_has_text lbl 4234 arx_store_cue lbl part msg 0 mlen txt 4235 end 4236 end 4237 ret ARX_LEARN_OK 4238 end 4239 4240 text arx_s_dummy "x" 4241 4242 fn arx_train lbl uchar has_txt bool txt view uchar * to uchar 4243 ret arx_store lbl has_txt txt ARX_ACT_INVALID false 4244 end 4245 4246 fn arx_finish_training to void 4247 m = get arx_cfg_mode 0 4248 if eq m ARX_MODE_REFLEX 4249 rb_ign = arx_hv_rebuild 4250 else 4251 if eq m ARX_MODE_PERCEPTRON 4252 arx_pc_fit 4253 end 4254 end 4255 end 4256 4257 fn arx_learn_correct lbl uchar has_txt bool txt view uchar * to uchar 4258 if eq get arx_learning 0 0 4259 ret ARX_LEARN_OFF 4260 end 4261 pred = arx_decide 4262 wrong = ARX_ACT_INVALID 4263 if ne pred lbl 4264 wrong = pred 4265 end 4266 ret arx_store lbl has_txt txt wrong true 4267 end 4268 4269 fn arx_sleep to void 4270 m = get arx_cfg_mode 0 4271 if eq m ARX_MODE_REFLEX 4272 arx_hv_sleep get arx_cfg_sleep_dup 0 get arx_cfg_sleep_con 0 4273 else 4274 if eq m ARX_MODE_NEAREST 4275 put arx_slp_merged 0 0 4276 put arx_slp_kept 0 get arx_nr_n 0 4277 put arx_slp_passes 0 0 4278 put arx_slp_con_n 0 0 4279 else 4280 arx_pc_fit 4281 put arx_slp_merged 0 0 4282 put arx_slp_kept 0 0 4283 put arx_slp_passes 0 0 4284 put arx_slp_con_n 0 0 4285 end 4286 end 4287 end