code wiki / (root) / nx_f32_gqa_attention.nx

nx_f32_gqa_attention.nx

buildroot/runtime/nx_f32_gqa_attention.nx

4560 B103 linesdepth 7pulls 9 transitivereach 3 importersview sourcekind tooltopic f32
docsdependenciesstructsconstsfunctions

about

nx_f32_gqa_attention.nx -- software-f32 Grouped-Query multi-head attention (Ainslie 2023 GQA). sd-server -> Nishi migration: Qwen3-4B (the Z-Image text encoder + companion chat model) uses GQA with 32 query heads sharing 8 kv heads (4:1, confirmed from the real GGUF: q_out 4096, kv_out 1024). This wraps the gated single-head `nx_f32_attention`: for each query head h it gathers Q_h and the SHARED K/V of kv head (h / group), runs scaled-dot-product attention, and scatters the head output back. This is the attention core of a real Qwen (and DiT) transformer layer. Q,out: flat *i64 f32 bits [n_tokens, n_q_heads*head_dim]. K,V: [n_tokens, n_kv_heads*head_dim]. license_tier: ORIGINAL

dependencies 5 imports · 2 importers

nx_syscalls.nx nx_f32.nx nx_f32_div.nx nx_f32_cvt.nx nx_f32_attention.nx nx_f32_gqa_attention.nx nx_f32_qwen_layer.nx nx_qwen_hybrid_attn.nx

imports: nx_syscalls.nxnx_f32.nxnx_f32_div.nxnx_f32_cvt.nxnx_f32_attention.nx

imported by: nx_f32_qwen_layer.nxnx_qwen_hybrid_attn.nx

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

main sys_mmap nx_i32_to_f32 nx_f32_div nx_f32_classify nx_f32_exp_field nx_f32_mant_field nx_f32_sign nx_f32_mant_field ↻ nx_f32_exp_field ↻ nx_f32_gqa_attention sys_mmap ↻ nx_f32_attention nx_f32_add nx_f32_classify ↻ nx_f32_sign ↻ nx_f32_mant_field ↻ nx_f32_exp_field ↻ nx_f32_mul nx_f32_classify ↻ nx_f32_sign ↻ nx_f32_mant_field ↻ nx_f32_exp_field ↻ nx_f32_softmax _f32_gt nx_f32_sub nx_f32_add ↻ nx_f32_neg nx_f32_classify ↻ nx_f32_sign ↻ nx_f32_sub ↻ nx_f32_exp nx_f32_classify ↻ nx_f32_sign ↻ _f32_to_i32_rne nx_f32_classify ↻ nx_f32_sign ↻ nx_f32_exp_field ↻ nx_f32_mant_field ↻ nx_i32_to_f32 ↻

structs

none

consts

17const NX_F32GQA_OK: i64 = 0
18const NX_F32GQA_ERR: i64 = 1

functions

20func nx_f32_gqa_attention(Q: *i64, K: *i64, V: *i64, n_tokens: i64,
66func main() -> i64