Skip to content

Refuse a head count of 0 by name before any division: HIP Qwen3.5, nemotron, gemma4 Metal DFlash, DeepSeek V4.1 Metal vision - #3777

Merged
ashhart merged 5 commits into
ashhart:mainfrom
jschmied:zero-heads
Nov 19, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
jschmied:zero-heads

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Head b2cf452260 (on main 8cbef9777b). Found while building #3716: a config.json with a head count of 0 panicked on main (integer division by zero) instead of being refused. A correction to #3716's body: it named qwen27's hidden / heads as one of the panics, but Metal 27B already refuses 0; the hidden / heads panic is in the HIP Qwen3.5 path below.

What panicked, and the fix

Entry point On main Now
HIP weights/config.Config.parse → qwen3_5/config.geometry num_attention_heads: 0 without head_dim: hidden / heads panics (config.zig:342). num_key_value_heads / linear_num_key_heads: 0: checks.products % panics (config_checks.zig:79). linear_num_value_heads: 0 was admitted. error.ZeroHeadCount (new), from config_checks.heads. geometry calls it before hidden / heads, and products calls it on all four counts. The old attn_heads == 0 → InvalidConfig line is gone because the new check covers it earlier.
nemotron config.parse num_attention_heads: 0 without head_dim: hidden / heads panics (config.zig:407). With head_dim, heads: 0 and kv_heads: 0 were admitted. error.BadConfig (the family's word), plus a why: "config.json declares 0 attention and 2 key/value heads; both must be positive".
gemma4 Metal dflash_weights.Config.parse num_key_value_heads: 0 panics in heads % kv_heads. heads: 0 was admitted. error.BadDraftConfig, as the CUDA twin (cuda_dflash.zig's size) already refuses a count below 1.
deepseek_v41 Metal vision.Config.parse → check vision_config.num_attention_heads: 0 panics in headDim() (dim / heads). error.UnsupportedVision. The zero-count row now runs before headDim().

These were already safe, so nothing changed there: Metal 27B qwen27/config.parse (required refuses < 1 as BadConfig), Metal 2B qwen3_5/config.parse and CUDA cuda_geometry.parse (both refuse < 1 as BadQwenConfig). The new host test pins all four Qwen3.5 entry points.

Code sharing: main has no core refusal for head counts. Each family reads its counts with its own reader, and most readers already refuse 0. The fix therefore lives where the division is: in config_checks for the Qwen3.5 HIP path, which every caller of products shares, and inline in the three family parsers.

Grep across families and backends. I checked every / and % by a head count in config parsing against the reader that feeds it. These read with a reader that refuses 0, or check for 0 first: qwen27 (Metal, dflash), qwen3 (+Metal), qwen4 (+cuda_attention), step5, llama, minimax_m3 (config, dflash2, dspark), flint, flashnext, kolibri1, dory, lighton_ocr, dots_mocr, lfm2, radio, ling, gemma4 CUDA (dflash, assistants), deepseek_v41 config, qwen3_5 drafters (CUDA, HIP), core dspine/joint_schema/qwen_vl, cluster/model. These accept 0 but never divide by it: glm, glm_dsa, kimi_k3.

Evidence

A refusal test per entry point. Every test also passes on head 0 for each head count:

  • zig/qwen35_rules_host.zig "a head count of 0 is refused by name at every entry point, before any division". It covers every real Qwen3.5 config in the tree (layers_host.reals), with head_dim as declared and removed, times the 4 head counts, times every entry point that admits the base. That is 136 cases: CUDA 28, Metal 27B 32, Metal 2B 4, HIP 72. 13 of the bases have no head_dim, which is the hidden / heads path. The counts are pinned.
  • config_checks.zig "sizes: … zero head counts are refused": each of the four counts at 0 → ZeroHeadCount.
  • nemotron/config_test.zig "a head count of 0 is refused by name before hidden / heads": heads 0 and kv_heads 0, each with and without head_dim. Each gives BadConfig, and the why names the counts.
  • gemma4/dflash_weights.zig and deepseek_v41/metal/vision.zig: the zero rows are in the existing config tests.

Nothing is loaded or queued before a refusal. Each refusal fires inside the config parse, which reads only config.json (and generation_config.json for HIP) bytes, before any weights file is opened. The tests call parse on bytes with std.testing.allocator, whose leak check shows the HIP arena and the nemotron/gemma4 JSON trees are freed on the refusal path.

Red on main. I ran the same test files against main's sources:

  • HIP: thread … panic: division by zero at qwen3_5/config_checks.zig:79 (s.heads % s.kv_heads), via geometry ← weights/config.parse. With the counts reordered: panic: division by zero at qwen3_5/config.zig:342 (model_width / attn_heads). Run as written, the test first fails by name (expected error.ZeroHeadCount, found error.InvalidConfig for heads 0 with head_dim).
  • nemotron: panic: division by zero at nemotron/config.zig:407 without head_dim. With head_dim, main returns a Config with .heads = 0 (expected error.BadConfig).

Snapshot (#3716's harness). The harness runs every real Qwen3.5 config, and 16,066 mutations of it (24 keys × 15 values, root and text_config), through all four entry points. Its output is byte-identical between main and this head: sha256 550e535d…32b5, the same digest #3716 recorded. Then I removed the harness's "skip 0 for the four head counts" line and ran it on head only. It adds 940 lines and no panic. Every zero on the text tower is refused: HIP ZeroHeadCount 76, CUDA BadQwenConfig 72, Metal 27B BadConfig 64, Metal 2B BadQwenConfig 8, and the rest by an earlier row such as quantization or a tied head. A zero at the root of a config that has text_config is ignored, as every other root key is. Main cannot run those rows; it panics on the first one.

Gates:

  • GB10: test-qwen35-rules-host test-qwen35-layers-host test-qwen35-formats-host test-qwen35-metal-scope-host test-nemotron-mtp-policy-host test-nemotron-draw-host: 20/20 steps, 53/53 tests.
  • By hand on Linux (macOS-only steps in the build): qwen3_5 config.zig 14/14, config_checks.zig 6/6, nemotron config.zig 13 passed and 1 skipped (as on main).
  • pr_check.py: 0 errors. Lean: 221 vs 221 on main, 0 new. zig fmt --check passes.
  • x86 (-Dfixed-cpu test, test-host, golden, kernel-contract, security, fuzz, release paths, x86 cuda-host): ci, release-paths, x86-cuda-host, fmt, pr_check and lean pass. test: 10273/10352 tests with 1 failure; test-host: 2 failures. All three are cli_config_test "serve --config: expand reads the file…", which fails the same way in this x86 box's main-only runs (core: typed config field readers and named EOS policies; Qwen 3.5 CUDA, HIP and qwen27 Metal read through them (#2460 item 4) #3716 noted it on cf4555373 and 4ddc28cfc). It doesn't touch these files.
  • Metal (M3 Pro, b2cf452260, no local edits): zig build native passes, and zig build test passes with no filters: 14886 of 14999 tests passed, 113 skipped, 0 failed. Both changed config tests pass on head.
    • Main with only the new test lines grafted in fails both. The vision zero case panics division by zero in headDim() (vision.zig:219). The gemma4 zero case gets heads = 0 admitted instead of BadDraftConfig.
    • One build step fails, and it fails the same way on main 8cbef9777b: the libSystem memcpy link check at zig/build/metal_targets.zig:87. It passed on 038e4a278, so something between the two pulled compiler-rt's memcpy into tensorfold-native. A bisect is running and I'll post the commit.
  • deepseek_v41/metal/vision.zig was reached by no test root. Its tests, the new zero case included, never ran in CI or in zig build test. zig/src/tensorfold.zig's test block now imports it, beside deepseek_v41/metal/glue.zig, which is there for the same reason. Both existing vision tests (the defaults parse and the 2D RoPE table) pass.

Served path. None of this reaches served code for a valid config. The snapshot shows every admitted config parses to the same Spec/Config bytes as on main, and no kernel or engine file is touched. I made no served A/B because there are no served-token or speed rows to show. Seeded, resume and concurrency rows don't apply either: the change only refuses configs that main panicked on (or that main admitted with a zero head count).

Overlap with #3716. Both PRs touch zig/src/families/qwen3_5/config_checks.zig, in different hunks. git merge-tree of the two heads is clean, and test-qwen35-rules-host plus -layers-host pass on the merge (7/7 steps, 12/12 tests). Whichever lands second rebases trivially. This PR does not need #3716's field layer.

Proposed CHANGELOG line (Fixed): "A config.json head count of 0 is refused by name (HIP Qwen3.5 ZeroHeadCount, nemotron, gemma4 Metal DFlash, DeepSeek V4.1 Metal vision) instead of panicking on a division by zero."

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart The bisect for the Metal memcpy link check that fails on main (body, "Gates") is done. It's not from this PR.

  • Range: 038e4a278 good, 8cbef9777b bad, 104 commits, bisected on an M3 Pro (zig build native plus nm -m).
  • First bad commit: 20132e7c1 "cli: tensorfold device says what this machine runs, saved in ~/.tensorfold/device.json".
  • Cause: zig/src/server/device/profile.zig:40 parses device.json with std.json.parseFromSliceLeaky into a Doc whose fields are all integers (u32/u64).
    • A typed integer parse goes through std/json/static.zig sliceToInt. For a token not written as an integer, it falls back to std.fmt.parseFloat(f128, …) and @intFromFloat(i128).
    • That pulls in f128 soft-float and 128-bit conversions. compiler-rt symbols go from 5 at the parent to 52 (__addtf3, __multf3, __divtf3, __fixtfti, __floatuntitf, __divti3, …).
    • It also pulls in compiler-rt's memcpyFast as _memcpy. That is what metal_targets.zig:87 reports ("tensorfold-native links its own memcpy").
  • Check: with only that call stubbed out, _memcpy is again "(undefined) external (from libSystem)" and compiler-rt is back to 8 symbols.
  • Fix: I can send it as a small separate PR if you want. Read device.json as std.json.Value and take each count from .integer, refusing anything else by name. core: typed config field readers and named EOS policies; Qwen 3.5 CUDA, HIP and qwen27 Metal read through them (#2460 item 4) #3716's core/config_fields.zig already has those typed integer readers, so once core: typed config field readers and named EOS policies; Qwen 3.5 CUDA, HIP and qwen27 Metal read through them (#2460 item 4) #3716 lands this would be a few lines there.

@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Refusing a zero head count by name where the division happens is the right place for it:

  • HIP Qwen3.5: config_checks.heads runs before hidden / heads, and on all four counts in products.
  • Nemotron: BadConfig, with a why that names both counts.
  • The gemma4 Metal DFlash config: BadDraftConfig, as its CUDA twin refuses it.
  • The DeepSeek V4.1 Metal vision config: the zero checks now run before headDim().

Each has a test that panics or admits on main and refuses here. Importing vision.zig in tensorfold.zig's test block brings its tests into CI for the first time. Valid configs parse to the same bytes as on main, by your #3716 snapshot.

It applies to current main as it is. Its config_checks.zig hunk sits next to #3716's, and a three-way merge takes both. We'll place your proposed CHANGELOG line when it lands. Thanks also for bisecting the Metal memcpy link failure to 20132e7c1. That one is ours to follow up.

  • Synthena

ashhart and others added 5 commits November 19, 2026 22:11
…ision

HIP's config path divided by num_attention_heads (hidden / heads without head_dim)
and by num_key_value_heads / linear_num_key_heads (checks.products) before any check,
so a 0 panicked; a linear_num_value_heads of 0 was admitted. Metal 27B, Metal 2B and
CUDA already refuse a count below 1 in their readers; the host test pins all four.
…ad count of 0 before dividing

Each reader took 0: nemotron divided hidden / heads without head_dim (and admitted
heads 0 with it), gemma4's Metal drafter config took heads % kv_heads, DeepSeek V4.1's
Metal vision tower took dim / heads. Each now refuses with its family's word.
@ashhart
ashhart merged commit b0b17e8 into ashhart:main Nov 19, 2026
2 of 11 checks passed
@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.

2 participants