nx_model_spec.nx
buildroot/runtime/nx_model_spec.nx
about
nx_model_spec.nx -- typed model configuration envelope.
L4 foundational data brick. Every model-running primitive composes
against this: nx_gguf loader populates an NxModelSpec from
metadata; the inference scaffold reads it to size scratch buffers
and dispatch the right norm + activation + position-encoding
kernels.
Per the bits-up cardinal: model-specific constants (n_heads,
hidden_dim, etc.) belong in a TYPED struct, not scattered as
magic numbers across call sites. Per the data-driven cardinal:
the same substrate runs Llama / Mistral / Qwen / GPT-2 / BERT
just by swapping NxModelSpec values.
===== Sealed enums ==============================================
Norm kind:
NX_NORM_RMS -- Zhang/Sennrich 2019 (Llama / Mistral / Qwen)
NX_NORM_LAYER -- Ba/Kiros/Hinton 2016 (GPT-2 / BERT / T5 / ViT)
NX_NORM_NONE -- bypass (debug / experimentation)
Activation kind (FFN gate function):
NX_ACT_SILU -- Ramachandran 2017 (Llama / Mistral SwiGLU)
NX_ACT_GELU -- Hendrycks/Gimpel 2016 (GPT-2 / BERT GeGLU)
NX_ACT_RELU -- Fukushima 1969 / Glorot 2011 (older models)
Position encoding kind:
NX_POS_ROPE -- Su 2021 (Llama / Mistral / Qwen)
NX_POS_SINUSOIDAL -- Vaswani 2017 (original transformer)
NX_POS_LEARNED -- BERT / GPT-2 absolute-learned table
NX_POS_NONE -- bypass
FFN gating kind:
NX_FFN_SWIGLU -- SiLU(gate) * up (Llama / Mistral)
NX_FFN_GEGLU -- GELU(gate) * up (newer encoder models)
NX_FFN_PLAIN -- act(up) (original transformer)
genealogy_id: yaml_huggingface_config_2022 + onnx_model_metadata +
ggml_gguf_metadata_2024
lineage_id: substrate_model_spec_v1
dependencies 3 imports · 9 importers
imports: nx_syscalls.nxnx_tier.nxnx_loop.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_gguf_fixture_tiny.nxnx_gguf_fixture_tiny_test.nxnx_llm_run.nxnx_llm_run_v2.nxnx_llm_run_v2_test.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 123 | struct NxModelSpec |
consts
| 54 | const NX_NORM_RMS: nx_int = 0 |
| 55 | const NX_NORM_LAYER: nx_int = 1 |
| 56 | const NX_NORM_NONE: nx_int = 2 |
| 57 | const NX_NORM_N: nx_int = 3 |
| 67 | const NX_ACT_SILU: nx_int = 0 |
| 68 | const NX_ACT_GELU: nx_int = 1 |
| 69 | const NX_ACT_RELU: nx_int = 2 |
| 70 | const NX_ACT_NONE: nx_int = 3 |
| 71 | const NX_ACT_N: nx_int = 4 |
| 81 | const NX_POS_ROPE: nx_int = 0 |
| 82 | const NX_POS_SINUSOIDAL: nx_int = 1 |
| 83 | const NX_POS_LEARNED: nx_int = 2 |
| 84 | const NX_POS_NONE: nx_int = 3 |
| 85 | const NX_POS_N: nx_int = 4 |
| 95 | const NX_FFN_SWIGLU: nx_int = 0 |
| 96 | const NX_FFN_GEGLU: nx_int = 1 |
| 97 | const NX_FFN_PLAIN: nx_int = 2 |
| 98 | const NX_FFN_N: nx_int = 3 |
| 108 | const NX_MS_OK: nx_int = 0 |
| 109 | const NX_MS_ERR_BAD_DIM: nx_int = 1 |
| 110 | const NX_MS_ERR_BAD_HEAD_GEOM: nx_int = 2 |
| 111 | const NX_MS_ERR_BAD_KIND: nx_int = 3 |
| 112 | const NX_MS_ERR_BAD_VOCAB: nx_int = 4 |
| 113 | const NX_MS_N_VERDICTS: nx_int = 5 |
| 138 | const NX_MS_STRUCT_BYTES: nx_int = 96 // 12 fields * 8 |
functions
| 59 | func nx_norm_is_valid(k: nx_int) -> nx_int called by 1: nx_model_spec_validate |
| 73 | func nx_act_is_valid(k: nx_int) -> nx_int called by 1: nx_model_spec_validate |
| 87 | func nx_pos_is_valid(k: nx_int) -> nx_int called by 1: nx_model_spec_validate |
| 100 | func nx_ffn_is_valid(k: nx_int) -> nx_int called by 1: nx_model_spec_validate |
| 115 | func nx_ms_verdict_is_valid(v: nx_int) -> nx_int called by 1: main |
| 140 | func nx_model_spec_new() -> *NxModelSpec called by 8: mainnx_gft_build_tiny_llamamainmainnx_model_spec_llama_7bnx_model_spec_mistral_7b+2 calls 1: sys_mmap |
| 162 | func nx_model_spec_validate(s: *NxModelSpec) -> nx_int |
| 196 | func nx_model_spec_attn_params_per_layer(s: *NxModelSpec) -> i64 called by 1: nx_model_spec_total_params |
| 203 | func nx_model_spec_ffn_params_per_layer(s: *NxModelSpec) -> i64 called by 1: nx_model_spec_total_params |
| 209 | func nx_model_spec_total_params(s: *NxModelSpec) -> i64 |
| 225 | func nx_model_spec_llama_7b() -> *NxModelSpec |
| 242 | func nx_model_spec_mistral_7b() -> *NxModelSpec |
| 259 | func nx_model_spec_gpt2_small() -> *NxModelSpec |
| 289 | func main() -> i64 |