Skip to content

core: typed config field readers and named EOS policies; Qwen 3.5 CUDA, HIP and qwen27 Metal read through them (#2460 item 4) - #3716

Merged
ashhart merged 4 commits into
ashhart:mainfrom
jschmied:cfg-fields
Nov 19, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
jschmied:cfg-fields

Conversation

@jschmied

@jschmied jschmied commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Draft for #2460 item 4, steps 2 and 3 of the survey (issuecomment-6938315721), kept as small as the survey's proposal so you can judge the shape before more families move. Head 5c2f24af78, on main 038e4a2787. Nothing a user sees changes except one log line on a refused config (below).

What it adds (commit 1, bf343980b8)

  • core/config_fields.zig: one value read one way. requiredInt, requiredFloat, optionalInt, optionalFloat, optionalFlag on a Reader (object, source name, *Problem), and the value-level int, float, flag they use. Ints are integer-tagged only and range-checked before any narrowing, floats take an int or a float, bools are JSON bools. Optional keys say what a present null means (.refused or .absent). Every refusal is error.BadConfigField plus a Problem text naming source, field and reason, e.g. config.json text_config: num_key_value_heads is a string, not an integer. No defaults, pins or geometry: those stay in the families.
  • core/tokenizer/eos.zig: collect(out, policy, root, text, generation, vocab) with a Policy (order, nested arrays, most ids, none refused) and three named policies:
    • served: today's resolve (root; text_config only when the root gave none; then generation; flat arrays; no limit; none allowed). resolve and merge now run on it with the same refusal names (BadEos).
    • qwen35_hip: the Qwen 3.5 HIP loader's rule (config_checks.zig:92 on main): root, text tower and generation all; nested arrays read; at most 8; none refused (MissingEos).
    • qwen27_metal: qwen27 Metal's rule (qwen27/config.zig:340 on main): the root's key whenever it exists (a root null or [] hides the tower), else the tower; then generation; nested arrays read; at most 8; none refused.
      BadEosValue (not an id, an id array or null) is separate from BadEos (an id past the vocabulary), so each family keeps its own name for it: HIP says BadEos, qwen27 says BadConfig, as on main.
  • Both files are std-only shared modules (zig/build/qwen35_layers.zig table, beside qwen35_rules and rope_config), because qwen3_5/config.zig and qwen27/config.zig also build in modules without core. tokenizer.zig imports eos through the shared module so the file lives in one module per compilation.

What moves (commit 2, 9500529074)

  • CUDA qwen3_5/cuda_geometry.zig: integer and number read through config_fields; the refusal stays BadQwenConfig. Geometry.read logs the Problem on a refusal.
  • qwen27 Metal qwen27/config.zig: required, optional, integer, norm_topk_prob through config_fields (refusal stays BadConfig); EOS through qwen27_metal. model.zig:95 logs the Problem on a refusal.
  • HIP qwen3_5/config_checks.zig: eos calls qwen35_hip; its signature and weights/config.zig are unchanged (so no overlap with hip: ModelOpt NVFP4 and FP8 checkpoints keep their codes and decode in flight #2490, the only open PR touching these files).
  • Each backend keeps its rule by name: EOS order as above; top_k > 31 (CUDA) and > 15 (qwen27) refusals; tied heads through qwen35_rules (TiedEmbeddingHead on qwen27, admitted on CUDA); rope_scaling/YaRN handling through rope_config/cuda_rope; head-dim derivation, pins and products. None of those lines changed.
  • The served stop set on every backend still comes from server/hf_text.zig:220 → resolve (now the served policy). CUDA qwen3_5 reads no EOS itself.

Evidence (all on 84cd8edb10; 9500529074 differs from it only by config_fields.zig's header comment, shortened to one line for lean. 5c2f24af78 fixes a macOS compile error mac-agent found in qwen27/model.zig: the new var why: cfg.Problem redeclared a later local why; it is now problem. That file builds only on macOS, so the Linux runs never compiled it; zig ast-check on every changed file is clean now)

  • Refusal and struct snapshot, main vs head: every config entry point (CUDA cuda_geometry.parse, HIP weights/config.Config.parse, qwen27 Metal config.parse, Metal 2B config.parse) over 24 real Qwen 3.5/3.6/3.8 configs (20 HF repo configs incl. 0.8B to 397B, 2.4T, FP8 and GPTQ, and 4 local checkpoints: Qwen3.8-27B MLX 4-bit, our Qwen3.8-27B NVFP4, Qwen3.6-35B-A3B MLX and NVIDIA NVFP4), each with its generation_config.json and six generation variants, plus 16,066 mutations (24 keys at root and in text_config: absent, null, "5", 5.0, 128.9, -1, 0, 1, 2^32, 1e20, true, [], nested and null-holding arrays, {}). 16,932 variants: the 54 MB snapshot is byte-identical (sha256 550e535d…), both every parsed struct ({any}) and every refusal name. One gap: 0 is left out for the four head counts, because it panics on main and here alike (HIP config_checks.products divides by kv_heads/key_heads; qwen27 divides hidden / heads when head_dim is absent). That is a follow-up, not part of a move.
  • Host tests: test-config-fields (fixture admitted values; 19 named refusals by exact Problem text; truncation at the buffer end), test-checkpoint-eos (19 fixtures × 3 policies, including every row of the survey's EOS matrix; resolve against the served column; a non-object source). qwen27 config_test.zig 21/21 and qwen3_5 config.zig/config_checks.zig 14/14 run by hand on Linux (they are macOS-only steps in the build). test-cuda-host, test-checkpoint-io-host, compile-config-server, test-qwen35-rules-host, -layers-host, -metal-scope-host, -formats-host: 436/436 steps green on the GB10.
  • Planted controls, each red before green (core tests: 14 of 14 red, in config_fields_test.zig and eos_policy_test.zig): int range check dropped; int truncating a float; float taking a string; a null always absent; Problem without the field; flag taking 0/1; root_key_else_text falling through on a null root; every_source skipping the tower; nested arrays refused; most-ids not enforced; none allowed under required; dedup dropped; served naming a malformed value something other than BadEos; served reading the tower after the root gave ids. Family controls against the snapshot: qwen27 on served (155 lines differ), HIP on qwen27_metal (272), CUDA counts admitting 0 (78), qwen27 refusing a null rms_norm_eps (51).
  • Served on CUDA (GB10, Qwen3.8-27B NVFP4, no learned drafter, the default copy drafts on), main then head then head then main: greedy and seeded (t 0.8, seed 7) replies over 5 prompts at 1 stream and 4 prompts at 4 streams: token hash, completion length and finish reason equal to main in all four runs (every reply ends stop on EOS, 2 to 75 tokens, so the served stop set from resolve is exercised). Binaries: main fb78306…, head 0572cf1… (md5, built from 038e4a2787 and 84cd8edb10).
  • Resume, drafting and 8 streams are not run: the config is parsed once at load and nothing in the request path changes; the replies above are compared against main, not against an earlier run of this head.
  • Speed: config parse only, nothing on the decode path changes. Load time (launch to /v1/models, page cache dropped before each start) and one c=1 decode row: load main 12.7 / 12.2 s, head 12.7 / 11.7 s (the server's own loaded in line: 12.3 / 11.8 vs 12.4 / 11.7 s); c=1 decode (256 tokens, 3 runs each) main 11.58 / 11.43 tok/s, head 11.59 / 11.57 tok/s, TTFT 0.126-0.132 s on all four. No served cell slower than main beyond run-to-run noise.
  • Full zig build -Dfixed-cpu test on the GB10: green. x86 (8 cores, 84cd8edb10): test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon 55/55 steps and 230/230 tests, check-release-paths, x86 check-native-cuda-host compile-cuda-tests, zig fmt --check green; test 10146/10225 with 1 failure and test-host with 2: cli_config_test "serve --config: expand reads the file…" (it fails the same way on this x86 box's main-only runs, head == base cf4555373 and 4ddc28cfc) and test-dsv41-device-host (in this box's runs of 9 other branches too); neither touches these files. pr_check.py 0 errors on 9500529074, lean 221 vs 221 on main, 0 new.

Metal (mac-agent, M3 Pro 36 GB, head 5c2f24af78 vs main 038e4a2787; no sudo there, so no page-cache purge, A-B-B-A order instead). All four pass:

  1. zig build test-qwen-dense-geometry-host test-qwen35-metal-scope-host: head 10/10 steps, 112/112 tests; main 10/10, 115/115. (9500529074 failed to compile here, see the evidence note.)
  2. Qwen3.8-27B MLX 4-bit, chat, 4 prompts, 256 max tokens:
    • 1 stream (DFlash2 drafter, --parallel 1), greedy and seeded (t 0.8, seed 7), plain and drafted: 16/16 equal to main on token sha256, length and finish reason; all stop; drafted == plain.
    • 4 streams: 36 GB can't hold four streams beside the drafter, so --no-drafts, ctx 2048, --parallel 4: greedy and seeded 8/8 equal to main, and 4 streams == 1 stream plain.
    • DFlash run() without a stop override: every drafted reply stops at the same token count and hash as main and as plain (lengths 61/43/18/11 greedy, 57/43/18/11 seeded).
  3. Load and c=1 decode (plain greedy, 256 forced), A-B-B-A: main 14.1 s / 8.4 tok/s, head 39.4 s / 8.5, head 9.6 s / 8.1, main 9.8 s / 8.3; same reply hash. The 39.4 s is the new binary's first, cold load; the warm loads are 9.6 vs 9.8 s. Decode is equal within noise.
  4. text_config.num_key_value_heads: "4": both refuse with BadConfig; head logs config.json text_config: num_key_value_heads is a string, not an integer, main logs no reason.

What this does not do (kept for separate, reviewable one-line policy diffs or issues): changing any family's EOS rule (the qwen27-vs-served set, HIP's union), qwen27 tied heads, qwen27 YaRN, the top_k caps, the Metal 2B reader, the HIP loader's Python-int() field reads, and the zero-divisor panics above. Gemma 4 and Spark 2.5 are the next migrations if this shape is right.

Proposed CHANGELOG line (yours to place): core: config_fields.zig (typed field reads naming source and field) and named EOS policies in eos.zig; Qwen 3.5 CUDA, HIP and qwen27 Metal read through them with unchanged refusals (#2460).

@SergeBlondin

Copy link
Copy Markdown

tested on a 3090, looks good

@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. We read this head (5c2f24af) against current main, where it applies cleanly. It does what it says and no more:

  • config_fields: one typed read per value, with a Problem naming the source and field.
  • Named EOS policies: each keeps today's rule. served is what resolve did, qwen35_hip is the HIP loader's union with at most 8 ids, and qwen27_metal is the root key, else the tower.
  • Unchanged refusals: each family keeps its own refusal names (BadQwenConfig, BadEos, BadConfig).
  • Your receipts back it: the snapshot over 24 real configs and 16,066 mutations is byte-identical to main, and replies are equal to main on CUDA and Metal, greedy and seeded, drafted and plain.
  • Sizes: every file stays under 600 lines (tokenizer.zig 597, cuda_geometry.zig 555), and the comments are single lines.

One note for landing: #3777 edits the Error line in zig/src/families/qwen3_5/config_checks.zig, right below the checkpoint_eos import this PR adds there. A three-way merge takes both, as your merge-tree showed, so whichever lands second needs only that.

Whether this shape is the one to extend to Gemma 4 and Spark 2.5 is our call on #2460. We'll answer it there.

  • Synthena

jschmied and others added 4 commits November 19, 2026 15:39
config_fields.zig reads one config.json value one way: required or optional, int, float or bool,
and every refusal names its source and field in a Problem. eos.zig takes a Policy (order, nested
arrays, most ids, none refused); resolve is the served policy unchanged, and qwen35_hip and
qwen27_metal name the two Qwen family rules. Both are std-only shared modules so std-only family
files can import them.
CUDA (cuda_geometry.zig) and qwen27 Metal read their counts, numbers and flags through
config_fields; the refusals keep their names (BadQwenConfig, BadConfig) and the field now
reaches the log. The HIP loader's and qwen27's EOS ids come from eos.zig's qwen35_hip and
qwen27_metal policies. Geometry, pins, top_k caps, tied heads and RoPE stay in the families.
@ashhart
ashhart merged commit 6858510 into ashhart:main Nov 19, 2026
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants