code wiki / (root) / nx_wordpiece_lib.nx

nx_wordpiece_lib.nx

buildroot/runtime/nx_wordpiece_lib.nx

10016 B244 linesdepth 2pulls 2 transitivereach 2 importersview sourcekind library
docsdependenciesstructsconstsfunctions

about

nx_wordpiece_lib.nx -- SOVEREIGN WordPiece tokenizer over a BERT uncased vocab.txt, a library so the cross-encoder arm of nx_beir_eval and its gate drive it in-process (search rung R0, 2026-09-14). ABSENT-PROVEN before writing (nx_absent wordpiece buildroot/runtime nx: matches=0 coverage_complete=1). The vocab is read whole through sys_read_file (no cap), every token is hashed into an open-addressing table sized from the token count, and every hit is verified by bytes, so a hash collision can never return the wrong id. Basic tokenisation follows the HF BertTokenizer with do_lower_case: ASCII lowercase, every byte below 33 splits, every ASCII punctuation byte is its own token, a word longer than WP_MAX_WORD_CHARS is [UNK] whole, then greedy longest-match wordpiece with the two-byte continuation prefix. DEVIATION, stated: bytes above 0x7f are kept as word characters (no NFD accent stripping, no CJK isolation), so an accented word normally becomes [UNK] pieces. license_tier: ORIGINAL

dependencies 1 imports · 2 importers

nx_syscalls.nx nx_wordpiece_lib.nx nx_beir_eval.nx nx_bert_ce_gate.nx

imports: nx_syscalls.nx

imported by: nx_beir_eval.nxnx_bert_ce_gate.nx

structs

none

consts

13const WP_G_VBUF: i64 = 0 // vocab.txt bytes
14const WP_G_VLEN: i64 = 1
15const WP_G_NTOK: i64 = 2
16const WP_G_TOFF: i64 = 3 // *i64 token start offset per id
17const WP_G_TLEN: i64 = 4 // *i64 token byte length per id
18const WP_G_HT: i64 = 5 // *i64 open-addressing table, 2 slots per bucket: hash, id+1 (0 = empty)
19const WP_G_HCAP: i64 = 6 // bucket count, a power of two derived from the token count
20const WP_G_UNK: i64 = 7
21const WP_G_CLS: i64 = 8
22const WP_G_SEP: i64 = 9
23const WP_G_PAD: i64 = 10
24const WP_G_LOADED: i64 = 11
25const WP_G_WSCR: i64 = 12 // *u8 lowercased-word scratch, WP_MAX_WORD_CHARS bytes
26const WP_G_PSCR: i64 = 13 // *i64 piece scratch, WP_MAX_WORD_CHARS slots
27const WP_G_SLOTS: i64 = 14
28const WP_G_BYTES: i64 = 112 // WP_G_SLOTS * 8
31const WP_MAX_WORD_CHARS: i64 = 100
33const WP_HASH_BYTE: i64 = 35
35const WP_DJB2_SEED: i64 = 5381
36const WP_DJB2_MUL: i64 = 33
37const WP_LF: i64 = 10
38const WP_CR: i64 = 13
39const WP_SPACE: i64 = 32
40const WP_UPPER_A: i64 = 65
41const WP_UPPER_Z: i64 = 90
42const WP_CASE_DELTA: i64 = 32
43const WP_DEL: i64 = 127

functions

45func wp_hash_run(h0: i64, buf: *u8, off: i64, len: i64) -> i64
53func wp_tok_eq(g: *i64, id: i64, pre: i64, buf: *u8, off: i64, len: i64) -> i64
72func wp_lookup(g: *i64, pre: i64, buf: *u8, off: i64, len: i64) -> i64
91func wp_load(g: *i64, path: *u8) -> i64
153func wp_is_punct(c: i64) -> i64
163func wp_word(g: *i64, buf: *u8, off: i64, len: i64, out: *i64, n: i64) -> i64
192func wp_tokenize(g: *i64, text: *u8, off: i64, len: i64, out: *i64) -> i64
227func wp_pair(g: *i64, qtok: *i64, nq0: i64, dtok: *i64, nd0: i64, maxlen: i64, ids: *i64, types: *i64) -> i64