code wiki / (root) / nx_gen_qknorm_verify.nx

nx_gen_qknorm_verify.nx

buildroot/runtime/nx_gen_qknorm_verify.nx

6040 B148 linesdepth 6pulls 12 transitivereach 0 importersview sourcekind tooltopic gen
docsdependenciesstructsconstsfunctions

about

nx_gen_qknorm_verify.nx -- SOVEREIGN per-head QK-RMSNorm over a PACKED qkv, verified vs oracle. out[t][h][d] = qkv[t][(head_base+h)*head_dim + d] / sqrt(mean_d(.^2) + eps) * w[d] QK-norm normalizes each attention head's query (or key) vector independently over head_dim, reading from the fused qkv projection where q, k and v are packed head-major in one row. Z-Image, Qwen-Image and most recent DiTs do this; it is the first stage of the attention core. Usage: nx_gen_qknorm_verify <model> <qkv> <norm_w> <out> <head_dim> <n_heads> <head_base> [rows] [eps_recip] head_base 0 for q; n_heads for k (q, k, v are packed in that order) WHY THIS IS A SEPARATE ORGAN AND NOT THE RMSNORM ONE: the two tensors have DIFFERENT row strides. The packed qkv row is (n_q + n_k + n_v) * head_dim wide, while the output row is n_heads * head_dim. A generic normalizer that assumed one stride would read the wrong head and still produce finite, plausible numbers -- the failure mode this lane keeps meeting. Hardware __f32_* intrinsics in the hot loop, never the nx_f32_* software twins. license_tier: ORIGINAL

dependencies 8 imports · 0 importers

nx_syscalls.nx nx_le.nx nx_f32.nx nx_f32_div.nx nx_f32_cvt.nx nx_strconv.nx nx_genfix.nx nx_genver.nx nx_gen_qknorm_verify.nx

imports: nx_syscalls.nxnx_le.nxnx_f32.nxnx_f32_div.nxnx_f32_cvt.nxnx_strconv.nxnx_genfix.nxnx_genver.nx

imported by: nobody (leaf or entry point)

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

main nx_genver_emit sys_mmap nxa_die sys_write sys_exit nxa_lock_take nxa_lock_addr sys_write ↻ nxa_lock_give nxa_lock_addr ↻ nxa_report_overrun sys_write ↻ nxa_dump_printable sys_write ↻ nxa_dump_sizes sys_write ↻ nx_strconv_format_i64 sys_write ↻ sys_mmap ↻ nx_strconv_parse_i64 nx_ascii_is_digit zq_strlen nx_genfix_dims sys_mmap ↻ _gf_read_manifest sys_mmap ↻ _gf_cpyz nx_genfix_root sys_openat_rd sys_read sys_close nx_genfix_load _gf_path sys_mmap ↻ _gf_cpyz ↻ nx_genfix_root ↻ _gf_cpy sys_openat_rd ↻ sys_mmap ↻

structs

none

consts

29const K_MAGIC_1000000: i64 = 1000000

functions

31func zq_strlen(s: *u8) -> i64
called by 1: main
37func main(argc: i64, argv: *i64) -> i64