code wiki / (root) / nx_bpe.nx

nx_bpe.nx

buildroot/runtime/nx_bpe.nx

18152 B495 linesdepth 5pulls 7 transitivereach 66 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_bpe.nx -- Byte-Pair Encoding tokenizer. Closes the TEXT-INTERFACE gap. With this brick the substrate can convert input text -> token IDs (encode) and token IDs -> text (decode), completing the input/output bookends around the transformer compute pipeline. Combined with nx_gguf (model loader, 7b4fafc6), nx_embedding (token lookup, 84725593), nx_transformer_block (forward, 8efc7edb), and nx_token_sample (logits sampling, 68e63138), the substrate now has the COMPLETE end-to-end inference path -- pure NishiLang, zero PyTorch, zero CUDA, zero llama.cpp dependency. ===== Algorithm (Sennrich/Haddow/Birch 2016) ===================== BPE starts with a base vocabulary of bytes (256 entries 0..255). Training collects an ordered list of merge rules: pairs of token IDs that recur frequently get merged into a new token ID. Encoding (Sennrich 2016, Algorithm 1): 1. Split input text into single bytes (each is an initial token). 2. Find the adjacent token-pair with the LOWEST rank (= highest priority) in the merge table. 3. Apply that merge: replace the pair with the merged token ID. 4. Repeat until no pair has a rank. Decoding: concatenate the byte sequences of each token in order. ===== Substrate composition ===================================== nx_intern.nx -- canonical byte-string <-> ID table (the vocab itself) nx_loop -- bounded loops; budget = n_input * n_merges hard ceiling per JPL Rule 2 v1 perf: linear scan over the merge table per merge-decision step. For 50k merges + 100-token output that's ~5M comparisons total. O(1) pair lookup via nx_map (key = a*MAX_VOCAB + b) is the queued v2 perf upgrade.

dependencies 4 imports · 49 importers

nx_syscalls.nx nx_tier.nx nx_loop.nx nx_intern.nx nx_bpe.nx nx_actor_role_llm_v2.nx nx_actor_role_llm_v2_autoreg_test. nx_actor_role_llm_v2_real_test.nx nx_actor_role_llm_v2_test.nx nx_batched_gate.nx nx_batched_tput.nx nx_chat_warmcache.nx nx_coder_gen_f32.nx nx_companion_chat.nx nx_companion_voice.nx

diagram shows first 10 each side; +0 more imports, +39 more importers in the complete lists below.

imports: nx_syscalls.nxnx_tier.nxnx_loop.nxnx_intern.nx

imported by: nx_actor_role_llm_v2.nxnx_actor_role_llm_v2_autoreg_test.nxnx_actor_role_llm_v2_real_test.nxnx_actor_role_llm_v2_test.nxnx_batched_gate.nxnx_batched_tput.nxnx_chat_warmcache.nxnx_coder_gen_f32.nxnx_companion_chat.nxnx_companion_voice.nxnx_embed_bench.nxnx_f32_bpe_load.nxnx_f32_bpe_load_test.nxnx_f32_embed_gate.nxnx_f32_llm_bench_test.nxnx_f32_llm_chat_test.nxnx_f32_llm_cohere_test.nxnx_f32_llm_e2e_test.nxnx_f32_llm_live_gen_test.nxnx_f32_llm_run.nxnx_f32_llm_run_test.nxnx_f32_llm_run_v2.nxnx_f32_llm_run_v2_test.nxnx_f32_llm_run_v3.nxnx_f32_llm_run_v3_test.nxnx_f32_llm_serve.nxnx_f32_qwen_capital_gate.nxnx_gguf_fixture_tiny.nxnx_llm_argmax_probe.nxnx_llm_batch_serve.nxnx_llm_france_probe.nxnx_llm_layerlens_probe.nxnx_llm_run.nxnx_llm_run_v2.nxnx_llm_run_v2_test.nxnx_llm_sched.nxnx_llm_sched_gate.nxnx_mt_r3_gate.nxnx_paged_fwd_gate.nxnx_qwen_native_gate.nxnx_qwen_wsl_timing_gate.nxnx_reason_paged_probe.nxnx_reason_probe.nxnx_reasoning.nxnx_reasoning_gate.nxnx_reasoning_paged.nxnx_specdec.nxnx_specdec_gate.nxnx_tok_probe.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main nx_bpe_vocab_new sys_mmap nx_intern_new nx_intern_next_pow2 nx_hash_new sys_mmap ↻ nx_bpe_add_token nx_intern_get nx_hash_fnv1a_bytes nx_hash_get nx_hash_probe nx_hash_fnv1a_i64 nx_intern_bytes_eq nx_hash_put nx_hash_probe ↻ nx_bpe_add_merge nx_bpe_encode sys_mmap ↻ nx_intern_get ↻ _bpe_find_merge_rank nx_bpe_decode sys_mmap ↻ nx_intern_at nx_bpe_verdict_is_valid

structs

77struct NxBpeMerge
85struct NxBpeVocab

consts

58const NX_BPE_OK: nx_int = 0
59const NX_BPE_ERR_OOB: nx_int = 1
60const NX_BPE_ERR_BAD_VOCAB: nx_int = 2
61const NX_BPE_ERR_BUDGET: nx_int = 3
62const NX_BPE_ERR_BAD_TOKEN: nx_int = 4
63const NX_BPE_N_VERDICTS: nx_int = 5
83const NX_BPE_MERGE_BYTES: nx_int = 24 // 3 fields * 8
92const NX_BPE_VOCAB_BYTES: nx_int = 32 // 4 fields * 8
155const NX_BPE_MERGE_BUDGET_FACTOR: nx_int = 4 // max merges per input byte

functions

65func nx_bpe_verdict_is_valid(v: nx_int) -> nx_int
called by 1: main
94func nx_bpe_vocab_new(intern_bytes_cap: i64, intern_ids_cap: i64,
106func nx_bpe_add_token(v: *NxBpeVocab, bytes: *u8, len: i64) -> i64
111func nx_bpe_add_merge(v: *NxBpeVocab, a: i64, b: i64, merged: i64) -> nx_int
125func _bpe_find_merge_rank(v: *NxBpeVocab, a: i64, b: i64) -> nx_int
157func nx_bpe_encode(v: *NxBpeVocab, text: *u8, n: nx_int, out_tokens: *i64) -> nx_int
240func _bpe_byte_to_utf8(b: i64, out: *u8) -> nx_int
262func nx_bpe_encode_bytelevel(v: *NxBpeVocab, text: *u8, n: nx_int, out_tokens: *i64) -> nx_int
325func _bpe_unmap_cp(cp: i64) -> i64
335func nx_bpe_decode_bytelevel(v: *NxBpeVocab, tokens: *i64, n: nx_int, out_text: *u8) -> nx_int
362func nx_bpe_decode(v: *NxBpeVocab, tokens: *i64, n: nx_int, out_text: *u8) -> nx_int
426func main() -> i64