nx_genpipe.nx source
↩ module page · 229 lines · 10644 B
1// nx_genpipe.nx -- a DECLARED generation pipeline: parse it, then prove every claim against the
2// files themselves.
3//
4// Operator 2026-08-07: *"z image turbo is the mix of all the parts, we dont need to break those
5// parts apart just make sure that they are hot swappable with other models"* and *"when i use krea
6// or something else it needs to have comfyui capabilities without its messy interface"*.
7//
8// ⇒ THE UNIT OF SWAP IS THE WHOLE PIPELINE, NOT THE DiT. Swapping only the DiT and keeping "the"
9// text encoder assumes every family shares one -- they do not, and a mismatched encoder produces a
10// picture rather than an error. So a pipeline names ALL its parts together and is swapped whole.
11//
12// ★★★★★ THE DESCRIPTOR IS A CLAIM; THE FILE IS THE EVIDENCE. Every declared role is verified by
13// probing the container's tensor names (nx_genrole), and a mismatch is a REFUSAL. This is the same
14// law that cost the estate elsewhere: a self-declared status is a comment, not a measurement.
15//
16// Format -- key TAB value, '#' comments, order irrelevant:
17// pipeline zimage_turbo
18// family z-image
19// text_encoder /path/qwen2.5-vl-7b-instruct-q8_0.gguf
20// dit /path/zimage_turbo_nsfw_2602-Q8_0.gguf
21// vae_decoder /path/taesd.safetensors
22// adapter /path/loras/z_real_v1.safetensors 1000 # multiplier in milli
23// steps 8
24// cfg_milli 1000
25// cap negative_prompt
26// license_tier: ORIGINAL
27
28import "nx_syscalls.nx"
29import "nx_le.nx"
30import "nx_f32.nx"
31import "nx_f32_div.nx"
32import "nx_f32_cvt.nx"
33import "nx_f16.nx"
34import "nx_strconv.nx"
35import "nx_genweights.nx"
36import "nx_genarch.nx"
37import "nx_genrole.nx"
38const PIPE_MAGIC_262144: i64 = 262144
39
40const PIPE_MAX_ADAPTERS: i64 = 16
41const PIPE_MAX_CAPS: i64 = 16
42
43// descriptor slots
44const PP_NAME: i64 = 0
45const PP_FAMILY: i64 = 1 // FAM_* code
46const PP_TEXTENC: i64 = 2 // *u8 path or 0
47const PP_DIT: i64 = 3
48const PP_VAEDEC: i64 = 4
49const PP_STEPS: i64 = 5
50const PP_CFG_MIL: i64 = 6
51const PP_N_ADAPT: i64 = 7
52const PP_N_CAPS: i64 = 8
53const PP_VAE_EXT: i64 = 9 // vae the EXTERNAL engine reads (see below)
54const PP_SAMPLER: i64 = 10
55const PP_NEGFILE: i64 = 11
56const PP_ADAPT0: i64 = 16 // 2 slots each: path, multiplier_milli
57const PP_CAP0: i64 = 48 // 1 slot each: *u8
58const PP_SLOTS: i64 = 72
59
60func _pp_len(s: *u8) -> i64 {
61 var n: i64 = 0
62 while s[n] != (0 as u8) { n = n + 1 }
63 return n
64}
65func _pp_eq(a: *u8, b: *u8) -> i64 {
66 var i: i64 = 0
67 while a[i] != (0 as u8) {
68 if a[i] != b[i] { return 0 }
69 i = i + 1
70 }
71 if b[i] != (0 as u8) { return 0 }
72 return 1
73}
74func _pp_family_code(s: *u8) -> i64 {
75 if _pp_eq(s, "z-image" as *u8) == 1 { return FAM_ZIMAGE }
76 if _pp_eq(s, "flux" as *u8) == 1 { return FAM_FLUX }
77 if _pp_eq(s, "taesd" as *u8) == 1 { return FAM_TAESD }
78 if _pp_eq(s, "sd-vae" as *u8) == 1 { return FAM_SDVAE }
79 if _pp_eq(s, "llm-gguf" as *u8) == 1 { return FAM_LLMGGUF }
80 return FAM_UNKNOWN
81}
82
83// Parse a descriptor file into pp[PP_SLOTS]. Returns 0, or a negative line number on a bad line.
84// An UNRECOGNISED KEY IS AN ERROR, not a shrug: a typo'd `vae-decoder` would otherwise leave the
85// pipeline silently missing a part it believes it declared.
86func nx_pipe_parse(path: *u8, pp: *i64) -> i64 {
87 var i: i64 = 0
88 while i < PP_SLOTS { pp[i] = 0; i = i + 1 }
89 pp[PP_STEPS] = 8
90 pp[PP_CFG_MIL] = 1000
91
92 let fd: i64 = sys_openat_rd(path)
93 if fd < 0 { return 0 - 1 }
94 let cap: i64 = PIPE_MAGIC_262144
95 let buf: *u8 = sys_mmap(cap + 64)
96 var n: i64 = 0
97 var go: i64 = 1
98 while go == 1 {
99 let r: i64 = sys_read(fd, ((buf as i64) + n) as *u8, cap - n)
100 if r <= 0 { go = 0 } else { n = n + r }
101 if n >= cap { go = 0 }
102 }
103 sys_close(fd)
104 buf[n] = 0
105
106 let fld: *i64 = sys_mmap(64) as *i64
107 let ep: *i64 = sys_mmap(64) as *i64
108 var pos: i64 = 0
109 var line: i64 = 0
110 var rc: i64 = 0
111 while pos < n {
112 if rc != 0 { return rc }
113 line = line + 1
114 let start: i64 = pos
115 var e: i64 = pos
116 var eol: i64 = n
117 var scanning: i64 = 1
118 while scanning == 1 {
119 if e >= n { eol = n; scanning = 0 }
120 else {
121 if buf[e] == (0x0A as u8) { eol = e; scanning = 0 } else { e = e + 1 }
122 }
123 }
124 buf[eol] = 0
125 if eol > start { if buf[eol - 1] == (0x0D as u8) { buf[eol - 1] = 0 } }
126 pos = eol + 1
127
128 // tokenise in place: fields are runs of non-tab, so aligned descriptors work
129 fld[0] = 0
130 fld[1] = 0
131 fld[2] = 0
132 var nf: i64 = 0
133 var intok: i64 = 0
134 var p: i64 = start
135 while p <= eol {
136 if buf[p] == (0x09 as u8) { buf[p] = 0; intok = 0 }
137 else {
138 if buf[p] == (0 as u8) { intok = 0 }
139 else {
140 if intok == 0 {
141 if nf < 3 { fld[nf] = (buf as i64) + p; nf = nf + 1 }
142 intok = 1
143 }
144 }
145 }
146 p = p + 1
147 }
148
149 if nf > 0 {
150 let k: *u8 = fld[0] as *u8
151 if k[0] != (0x23 as u8) { // '#' comment
152 let v1: *u8 = fld[1] as *u8
153 let v2: *u8 = fld[2] as *u8
154 var hit: i64 = 0
155 if _pp_eq(k, "pipeline" as *u8) == 1 { hit = 1; pp[PP_NAME] = fld[1] }
156 if _pp_eq(k, "family" as *u8) == 1 {
157 hit = 1
158 if nf > 1 {
159 pp[PP_FAMILY] = _pp_family_code(v1)
160 if pp[PP_FAMILY] == FAM_UNKNOWN { rc = 0 - line }
161 }
162 }
163 if _pp_eq(k, "text_encoder" as *u8) == 1 { hit = 1; pp[PP_TEXTENC] = fld[1] }
164 if _pp_eq(k, "dit" as *u8) == 1 { hit = 1; pp[PP_DIT] = fld[1] }
165 if _pp_eq(k, "vae_decoder" as *u8) == 1 { hit = 1; pp[PP_VAEDEC] = fld[1] }
166 // ⚠THE VAE SLOT IS ENGINE-SPECIFIC, AND THIS COST A FAILED RUN TO LEARN. Our
167 // decoder reads TAESD (`decoder.layers.N.conv`); sd.cpp demands a full SD VAE
168 // (`first_stage_model.decoder.up.N...`) and aborts listing every missing tensor.
169 // Same pipeline, same checkpoint, two different vae files -- so a descriptor with
170 // ONE vae key silently belongs to whichever engine wrote it.
171 // ★ A SLOT THAT MEANS DIFFERENT FILES TO DIFFERENT CONSUMERS NEEDS TWO SLOTS.
172 if _pp_eq(k, "vae_decoder_extern" as *u8) == 1 { hit = 1; pp[PP_VAE_EXT] = fld[1] }
173 // ⚠MEASURED, NOT COSMETIC. sd.cpp picks `euler` only for models it RECOGNISES as
174 // flow-matching (Flux/SD3/Wan) and `euler_a` for everything else. Z-Image IS a flow
175 // model but is not on that list, so it silently got an ANCESTRAL sampler that
176 // re-injects noise every step. Symptom: the same seed rendered THREE copies of the
177 // subject side by side; forcing `euler` produced one.
178 // ★★★★★ A DEFAULT CHOSEN BY A FAMILY WHITELIST IS WRONG FOR EVERY FAMILY NOBODY
179 // ADDED TO THE LIST -- AND IT FAILS AS BAD OUTPUT, NEVER AS AN ERROR.
180 if _pp_eq(k, "sampler" as *u8) == 1 { hit = 1; pp[PP_SAMPLER] = fld[1] }
181 // ⚠THE NEGATIVE PROMPT IS A PRODUCT ARTEFACT, NOT A NICETY. The estate composes it
182 // at render time from `prompt_negative_clause` in content.db (8 clauses: hand /
183 // breast / face anatomy, body topology, rendering artifacts, + per-slot). I rendered
184 // without one for a whole session and chased its absence as five separate bugs:
185 // ghosted faces (face_anatomy), duplicated subjects (body_topology), and softness
186 // (rendering_artifacts literally lists "blurry, low quality, worst quality").
187 // ★★★★★★ A DEFECT YOU CAN NAME IN THE PRODUCT'S OWN NEGATIVE-PROMPT TABLE IS NOT A
188 // NEW BUG -- IT IS THE SUPPRESSION YOU FORGOT TO APPLY.
189 // ⚠AND IT NEEDS cfg > 1: at cfg 1.0 the negative branch is never evaluated, so the
190 // clause list is present, parsed, passed, and has ZERO effect. Measured: cfg 1.0
191 // with negatives == cfg 1.0 without; cfg 3.0 fixed the render.
192 if _pp_eq(k, "negative_file" as *u8) == 1 { hit = 1; pp[PP_NEGFILE] = fld[1] }
193 if _pp_eq(k, "steps" as *u8) == 1 { hit = 1; if nf > 1 { pp[PP_STEPS] = nx_strconv_parse_i64(v1, ep) } }
194 if _pp_eq(k, "cfg_milli" as *u8) == 1 { hit = 1; if nf > 1 { pp[PP_CFG_MIL] = nx_strconv_parse_i64(v1, ep) } }
195 if _pp_eq(k, "cap" as *u8) == 1 {
196 hit = 1
197 if pp[PP_N_CAPS] >= PIPE_MAX_CAPS { rc = 0 - line }
198 else { pp[PP_CAP0 + pp[PP_N_CAPS]] = fld[1]; pp[PP_N_CAPS] = pp[PP_N_CAPS] + 1 }
199 }
200 if _pp_eq(k, "adapter" as *u8) == 1 {
201 hit = 1
202 if pp[PP_N_ADAPT] >= PIPE_MAX_ADAPTERS { rc = 0 - line }
203 else {
204 let s: i64 = PP_ADAPT0 + pp[PP_N_ADAPT] * 2
205 pp[s] = fld[1]
206 // A missing multiplier defaults to 1.0, never 0 -- an adapter declared with
207 // no strength means "apply it", not "apply nothing".
208 if nf > 2 { pp[s + 1] = nx_strconv_parse_i64(v2, ep) } else { pp[s + 1] = 1000 }
209 pp[PP_N_ADAPT] = pp[PP_N_ADAPT] + 1
210 }
211 }
212 // ⚠ AN UNRECOGNISED KEY IS AN ERROR, NOT A SHRUG: a typo'd `vae-decoder` would
213 // otherwise leave the pipeline silently missing a part it believes it declared.
214 if hit == 0 { rc = 0 - line }
215 if nf < 2 { if hit == 1 { rc = 0 - line } }
216 }
217 }
218 }
219 return rc
220}
221
222func nx_pipe_has_cap(pp: *i64, want: *u8) -> i64 {
223 var i: i64 = 0
224 while i < pp[PP_N_CAPS] {
225 if _pp_eq(pp[PP_CAP0 + i] as *u8, want) == 1 { return 1 }
226 i = i + 1
227 }
228 return 0
229}