code wiki / (root) / nx_f32_llama_block.nx

nx_f32_llama_block.nx

buildroot/runtime/nx_f32_llama_block.nx

9692 B238 linesdepth 8pulls 17 transitivereach 67 importersview sourcekind librarytopic f32
docsdependenciesstructsconstsfunctions

about

nx_f32_llama_block.nx -- bits-up Llama-class transformer block forward pass (v3). The full per-layer integration. Composes: nx_f32_rmsnorm nx_f32_matmul (for W_q, W_k, W_v, W_o, W_gate, W_up, W_down) nx_f32_rope_apply_vector (optional, per query/kv head per token) nx_f32_attn_with_cache (the new KV-cached attention kernel) nx_f32_silu (FFN SwiGLU gate activation) nx_f32_mul (element-wise gate * up) nx_f32_add (residual additions) Layer structure (canonical Llama / Llama-2 / Qwen2 form): x_in = x // input attn_in = RMSNorm(x_in, gamma_attn, eps) Q = matmul(attn_in, W_q) // [n_tokens, q_dim] K_new = matmul(attn_in, W_k) // [n_tokens, kv_dim] V_new = matmul(attn_in, W_v) // [n_tokens, kv_dim] if apply_rope: for each token t, pos = cache.seq_len + t: for each query head h: RoPE(Q[t, h*head_dim:(h+1)*head_dim], pos) for each kv head h: RoPE(K_new[t, h*head_dim:(h+1)*head_dim], pos) attn_concat = attn_with_cache(Q, K_new, V_new, ..., cache, layer_idx) attn_proj = matmul(attn_concat, W_o) // [n_tokens, hidden_dim] x_mid = x_in + attn_proj // residual ffn_in = RMSNorm(x_mid, gamma_ffn, eps) gate_raw = matmul(ffn_in, W_gate) // [n_tokens, ffn_dim] up = matmul(ffn_in, W_up) // [n_tokens, ffn_dim] for each element: hidden = SiLU(gate_raw) * up ffn_proj = matmul(hidden, W_down) // [n_tokens, hidden_dim] out = x_mid + ffn_proj // residual The CALLER advances cache.seq_len ONCE per forward pass after invoking this block for every layer in the stack. Weight layout convention (matches matmul convention): matmul(A [M,K], B [K,N]) -> [M,N]

dependencies 10 imports · 46 importers

nx_syscalls.nx nx_tier.nx nx_f32.nx nx_f32_rmsnorm.nx nx_f32_matmul.nx nx_f32_activations.nx nx_f32_rope.nx nx_f32_attn_multi.nx nx_f32_kv_cache.nx nx_f32_attn_cached.nx nx_f32_llama_block.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 nx_f32_llama_block_test.nx nx_f32_llama_block_v4.nx nx_f32_llama_block_v4_test.nx nx_f32_llama_layer_load.nx

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

imports: nx_syscalls.nxnx_tier.nxnx_f32.nxnx_f32_rmsnorm.nxnx_f32_matmul.nxnx_f32_activations.nxnx_f32_rope.nxnx_f32_attn_multi.nxnx_f32_kv_cache.nxnx_f32_attn_cached.nx

imported by: nx_batched_gate.nxnx_batched_tput.nxnx_chat_warmcache.nxnx_coder_gen_f32.nxnx_companion_chat.nxnx_companion_voice.nxnx_f32_llama_block_test.nxnx_f32_llama_block_v4.nxnx_f32_llama_block_v4_test.nxnx_f32_llama_layer_load.nxnx_f32_llama_layer_load_test.nxnx_f32_llama_stack.nxnx_f32_llama_stack_test.nxnx_f32_llama_v4b.nxnx_f32_llama_v4p.nxnx_f32_llm.nxnx_f32_llm_bench_test.nxnx_f32_llm_chat_test.nxnx_f32_llm_cohere_test.nxnx_f32_llm_e2e_test.nxnx_f32_llm_embed_diag_test.nxnx_f32_llm_live_gen_test.nxnx_f32_llm_live_load_only_test.nxnx_f32_llm_live_load_test.nxnx_f32_llm_load.nxnx_f32_llm_load_test.nxnx_f32_llm_run_test.nxnx_f32_llm_run_v2_test.nxnx_f32_llm_serve.nxnx_f32_llm_test.nxnx_f32_qwen_capital_gate.nxnx_llm_argmax_probe.nxnx_llm_batch_serve.nxnx_llm_diff_probe.nxnx_llm_forward_profile.nxnx_llm_france_probe.nxnx_llm_layerlens_probe.nxnx_llm_sched_gate.nxnx_lowrank_kv_real.nxnx_paged_fwd_gate.nxnx_qwen_native_gate.nxnx_qwen_wsl_timing_gate.nxnx_reason_paged_probe.nxnx_reason_probe.nxnx_reasoning_gate.nxnx_specdec_gate.nx

structs

84struct NxF32LlamaLayer

consts

70const NX_F32_BLK_OK: nx_int = 0
71const NX_F32_BLK_ERR_BAD_DIM: nx_int = 1
72const NX_F32_BLK_ERR_NULL: nx_int = 2
73const NX_F32_BLK_ERR_CACHE: nx_int = 3
74const NX_F32_BLK_N_VERDICTS: nx_int = 4
96const NX_F32_LLAMA_LAYER_BYTES: nx_int = 72 // 9 fields * 8

functions

76func nx_f32_blk_verdict_is_valid(v: nx_int) -> nx_int
called by 1: main
100func nx_f32_llama_layer_alloc() -> *NxF32LlamaLayer
107func nx_f32_llama_block_forward_v3(