code wiki / (root) / nx_moe.nx

nx_moe.nx

buildroot/runtime/nx_moe.nx

7429 B202 linesdepth 8pulls 19 transitivereach 1 importersview sourcekind library
docsdependenciesstructsconstsfunctions

about

nx_moe.nx -- sovereign MIXTURE-OF-EXPERTS sparse FFN (the last census OPPORTUNITY axis "Mixture of experts (sparse)", momentum 3; Mixtral / Switch-class, Shazeer 2017 / Fedus 2021 / Jiang 2024). Per token: router logits = x @ W_router^T -> softmax -> TOP-K experts -> renormalize the k probs -> out = sum_k p_i * SwiGLU_FFN_expert_i(x). Only k of n_experts FFNs run per token (the sparsity that lets total parameters scale past per-token compute). Experts accumulate in SELECTION ORDER (prob-descending; ties -> lowest index) -- deterministic, documented, and mirrored by the gate's oracle. Composes the EXISTING primitives: nx_f32_matmul_t (weights transposed, the LLM convention), nx_f32_softmax, nx_f32_silu. Weights here are plain F32 buffers; binding real MoE GGUF expert tensors (and the lazy/ quantized paths) is the documented follow-on once an MoE model is on disk. Touch counters per expert make the sparsity MEASURABLE (gated), not asserted. genealogy_id: shazeer_2017_moe + fedus_2021_switch + jiang_2024_mixtral lineage_id: substrate_moe_v1

dependencies 7 imports · 1 importers

nx_syscalls.nx nx_tier.nx nx_f32.nx nx_f32_div.nx nx_f32_softmax.nx nx_f32_matmul_t.nx nx_f32_activations.nx nx_moe.nx nx_moe_gate.nx

imports: nx_syscalls.nxnx_tier.nxnx_f32.nxnx_f32_div.nxnx_f32_softmax.nxnx_f32_matmul_t.nxnx_f32_activations.nx

imported by: nx_moe_gate.nx

structs

39struct NxMoeLayer

consts

30const NX_MOE_OK: nx_int = 0
31const NX_MOE_ERR_NULL: nx_int = 1
32const NX_MOE_ERR_BAD_DIM: nx_int = 2
51const NX_MOE_LAYER_BYTES: nx_int = 72

functions

53func nx_moe_layer_alloc(n_experts: nx_int, top_k: nx_int,
called by 1: main calls 1: sys_mmap
87func nx_moe_route(L: *NxMoeLayer, x_row: *i64,
135func nx_moe_expert_ffn(L: *NxMoeLayer, e: nx_int, x_row: *i64,
164func nx_moe_forward(L: *NxMoeLayer, X: *i64, m: nx_int,