Skip to content

metal: build-time instances, the model geometry a body is compiled for, picked at open - #836

Merged
midagedev merged 4 commits into
mainfrom
mac-instances
Oct 26, 2026
Merged

midagedev merged 4 commits into
mainfrom
mac-instances

Conversation

@midagedev

Copy link
Copy Markdown
Owner

The Metal kernels are compiled for one model geometry: head size, KV heads, expert width, router size. This change makes that geometry a build-time instance. build.rs compiles the MSL once per instance row. The open picks the row from the file's facts, inside the one check owner (check.rs). A file whose shape no instance is built for is refused by name, and --plan prints that refusal as an item.

Today there is one row, Qwen3.6-35B-A3B's. So this is a move: no kernel or pipeline changes.

Evidence:

  • just metal-compare against the base: 181 pipelines same, added 0, changed 0. Decode trace equal at depth ~0 and 600, ids differ 0.
  • On an M1 Pro (source path): clippy -D warnings for both feature sets, 46 lib tests, just lab-metal PASS (155 mutants, each red on its clause).
  • A new clause, check::tests::a_shape_no_instance_is_built_for_is_the_opens_first_item, went red on a mutant that falls back to the first row.
  • Not run: the Metal e2e gate (barred on the maintainers' Macs, as in metal: Q8_0 and Q5_K owners; the census's free bytes are the host's room #834). The box fixture-tier batch runs in the next union batch, because Cargo.lock changes: bloomery-metal gains a dependency. The result follows in a comment.

AI use: written and checked with Claude Code.

🤖 Generated with Claude Code

…r, picked at open by the file's facts in the one check

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MUST_FIX: the move is sound, but one lookup is copied out of its owner and one new refusal clause can never decide anything.

Must fix

1. gqa_row is a second copy of models::shape::gqa_row

crates/metal/src/instance.rs:69 re-implements the lookup of shape::gqa_row_v (head == value, first row that takes(group)) in const form. The doc comment says so. A new test (the_registered_rows_are_the_shape_tables, first assert) exists only to keep the two copies equal. The shape table already owns the const form for routers (router_row, shape.rs:501), so the const lookup belongs in models.
Cost: a change to the lookup rule (a new GroupRule, a tie-break) must be made twice, and one copy can drift without a build error.
Fix: make the owner const, delete the copy and the first assert of that test.

pub const fn gqa_row_v(head: u32, value: u32, group: u32) -> Option<GqaInst> {
    let mut i = 0;
    while i < GQA.len() {
        let r = GQA[i];
        if r.head == head && r.value == value && r.takes(group) {
            return Some(r);
        }
        i += 1;
    }
    None
}

gqa_row in shape.rs:869 becomes pub const fn too (it only calls gqa_row_v). In instance.rs the row is then row: match shape::gqa_row(256, 16 / 2) { Some(r) => r, None => panic!("the shape table has no GQA row for this head at this group") },.

2. The new hidden-width clause in head_checks never decides a refusal

crates/metal/src/arch/qwen3moe/check.rs:765 refuses a hidden that is not a multiple of the head type's block. The check a few lines above already refuses every hidden != router::K, and router::K (2048) is a multiple of every block size (32, 256). So the clause fires only when an earlier item has already refused, and adds a second item for the same cause. No test covers it: deleting the clause turns nothing red.
Cost: dead code with a message that implies a case that does not exist. Add it with the first instance whose hidden width is not a block multiple.
Fix: delete the else if !hidden.is_multiple_of(...) { ... return false; } branch.

Non-blocking

⚠ Key equality includes the shape rows' at text: crates/metal/src/instance.rs:18 stores a whole RouterInst/GqaInst, so Key == also compares the at: &'static str provenance. A reworded at in models makes every file "no instance" with the same axes. Compare the axes, not the row, or say in the doc that the row is identity.

⚠ shared_slot ignores the gate: crates/metal/src/kernels/experts_geom.rs:19 returns top_k while slots adds the shared slot only when rule.gated. For an ungated instance the header would name a slot past the end. Not reachable with one row; add the row's own check when the second row comes.

Checked

  • Duplicates: 24 new items checked; one hit (finding 1). dryer pair library.rs:349-356 ~ 363-370: two thin wrappers that differ in the language version; not one fact.
  • Complexity: check::read 16→17, open_as 15→16, generate 28→29, with_geometry 38→39. Each is one added branch or call, even growth; no split asked.
  • Silent failure: 7 sites followed (select map_err, instance_of, sources, METALLIBS lookup, HOST_BUILT, agree, the instance compare in open_as). Each ends in a named refusal or a build panic.
  • Tests: a_shape_no_instance_is_built_for_is_the_opens_first_item protects the select call in read; the mutant "fall back to REGISTERED[0]" turns of_key's test red. The new head_checks clause has no test (finding 2).
  • Gates: no pin moved. One new const assert (SCALE² · HEAD_DIM == 1) tightens, does not relax.
  • Hot path: none touched (build, open and load time only). Escape catalog: no pattern matched.

Notes

  • The Metal e2e gate was not run (the PR body says so); the box fixture batch result is promised in a later comment. Evidence is an M1 Pro, not an nvidia-smi card.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

… a module-level include

orphan-tests cannot read the items a module-level include! brings in;
BUILT and METALLIBS are now ordinary consts whose values are included.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MUST_FIX: the new commit is correct, but both must-fix items of the last review are still open.

Since the last review

  • Must fix 1 (gqa_row copy): still open. instance.rs:69 is unchanged, and shape::gqa_row_v at crates/models/src/shape.rs:860 is still not const.
  • Must fix 2 (dead hidden-width clause): still open. check.rs:765 is unchanged.
  • ⚠ Key equality includes at: still open (instance.rs:18).
  • ⚠ shared_slot ignores the gate: still open (experts_geom.rs:19).

Must fix

1. gqa_row is a second copy of models::shape::gqa_row

instance.rs:69 re-implements the lookup of shape::gqa_row_v (head == value, first row that takes(group)) in const form. A test exists only to keep the two copies equal.
Cost: a change to the lookup rule must be made twice, and one copy can drift without a build error.
Fix: make the owner const, delete the copy and the first assert of the_registered_rows_are_the_shape_tables.

pub const fn gqa_row_v(head: u32, value: u32, group: u32) -> Option<GqaInst> {
    let mut i = 0;
    while i < GQA.len() {
        let r = GQA[i];
        if r.head == head && r.value == value && r.takes(group) {
            return Some(r);
        }
        i += 1;
    }
    None
}

gqa_row (shape.rs:869) becomes pub const fn too. In instance.rs the row is then row: match shape::gqa_row(256, 16 / 2) { Some(r) => r, None => panic!("the shape table has no GQA row for this head at this group") },.

2. The hidden-width clause in head_checks never decides a refusal

check.rs:765 refuses a hidden that is not a multiple of the head block. An earlier check already refuses every hidden != router::K, and router::K (2048) is a multiple of 32 and 256. The clause fires only after another item has refused. No test covers it.
Cost: dead code with a message for a case that does not exist. Add it with the first instance whose hidden width is not a block multiple.
Fix: delete the else if !hidden.is_multiple_of(...) { ... return false; } branch.

Non-blocking

⚠ shared_slot ignores the gate: see the item above; add the row's own check with the second row.

⚠ Key equality compares at text: compare the axes, not the row.

Checked

  • Duplicates: the new commit adds 2 items (built.rs, metallibs.rs tables), none duplicated. dryer pair library.rs:354-361 ~ 368-375: two wrappers that differ in the language version; not one fact.
  • Complexity: check::read, open_as, generate, with_geometry each rose by 1 in the full PR (even growth). The new commit changes none.
  • Silent failure: 2 sites followed in the new commit. A missing metallibs.rs cannot happen, because METALLIBS and the file share the toolchain condition. HOST_BUILT still ends in a build panic.
  • Tests: none added or changed in the new commit.
  • Gates: no pin moved. Escape catalog: no pattern matched. Hot path: none (build time).

Notes

  • The new commit splits the instances.rs include into two typed const expressions. The cfg(metal_lib = "metallib") guard on METALLIBS matches the build script. I found no defect in it.
  • The Metal e2e gate was not run (the PR body says so). The evidence is an M1 Pro, not an nvidia-smi card.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@midagedev

Copy link
Copy Markdown
Owner Author

Rebased on main after #837 (branch mac-instances-r, 7aea0c7; the PR branch rebases onto main cleanly). A second commit turns the instance tables into const expressions (built.rs, metallibs.rs): orphan-tests cannot read the items a module-level include! brings in, and it now passes (4109 tests).

On 7aea0c7, an M1 Pro: clippy -D warnings for both feature sets rc 0, lib 46 passed, just lab-metal PASS. just metal-compare origin/main: 181 same, added 0, changed 0; decode trace equal at depth ~0 and 600, ids differ 0. The box fixture-tier batch runs in the next union batch (Cargo.lock changes).

…tecture registers its own

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo label Oct 26, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MUST_FIX: the new commit is correct, but both must-fix items of the last review are still open.

Since the last review

  • Must fix 1 (gqa_row copy): still open. instance.rs:69 is unchanged, and shape::gqa_row_v at crates/models/src/shape.rs:860 is still not const.
  • Must fix 2 (dead hidden-width clause): still open. check.rs:765 is unchanged.
  • ⚠ Key equality includes at: still open (instance.rs:18); compare the axes, not the row.
  • ⚠ shared_slot ignores the gate: still open (experts_geom.rs:19); add the row's own check with the second row.

The new commit only adds one row to tools/model-tax.tsv.

Must fix

1. gqa_row is a second copy of models::shape::gqa_row

instance.rs:69 re-implements the lookup of shape::gqa_row_v (head == value, first row that takes(group)) in const form. A test exists only to keep the two copies equal.
Cost: a change to the lookup rule must be made twice, and one copy can drift without a build error.
Fix: make the owner const, delete the copy and the first assert of the_registered_rows_are_the_shape_tables.

pub const fn gqa_row_v(head: u32, value: u32, group: u32) -> Option<GqaInst> {
    let mut i = 0;
    while i < GQA.len() {
        let r = GQA[i];
        if r.head == head && r.value == value && r.takes(group) {
            return Some(r);
        }
        i += 1;
    }
    None
}

gqa_row (shape.rs:869) becomes pub const fn too. In instance.rs the row is then row: match shape::gqa_row(256, 16 / 2) { Some(r) => r, None => panic!("the shape table has no GQA row for this head at this group") },.

2. The hidden-width clause in head_checks never decides a refusal

check.rs:765 refuses a hidden that is not a multiple of the head block. An earlier check already refuses every hidden != router::K, and router::K (2048) is a multiple of 32 and 256. The clause fires only after another item has refused. No test covers it.
Cost: dead code with a message for a case that does not exist. Add it with the first instance whose hidden width is not a block multiple.
Fix: delete the else if !hidden.is_multiple_of(...) { ... return false; } branch.

Non-blocking

⚠ PIN date is in the future: tools/model-tax.tsv:137 says PIN(2026-10-27). The review ran on 2026-10-26, and the other new rows use the day they were written. Set the date of the commit.

Checked

  • Duplicates: the new commit adds 1 item (the tax row), none duplicated. Earlier dryer pair library.rs:354-361 ~ 368-375: two wrappers that differ in the language version; not one fact.
  • Complexity: check::read, open_as, generate, with_geometry each rose by 1 in the full PR (even growth). The new commit changes none.
  • Silent failure: no new site in the new commit.
  • Tests: none added or changed in the new commit.
  • Gates: no pin moved except the new tax row above. Escape catalog: no pattern matched. Hot path: none (build time).

Notes

  • The Metal e2e gate was not run (the PR body says so). The evidence is an M1 Pro, not an nvidia-smi card.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

… not a copy; the head check drops a branch no file reaches

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja label Oct 26, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: the new commit fixes both must-fix items of the last review and adds nothing that breaks.

Since the last review

  • Must fix 1 (gqa_row copy): fixed. gqa_row_v and gqa_row in shape.rs:860 are const fn, and the copy in instance.rs is gone. GQA is a const slice, GqaInst is Copy and takes is const fn, so the const loop compiles.
  • Must fix 2 (dead hidden-width clause): fixed. The branch is gone from check.rs:765; the hidden != router::K check at line 731 still refuses every other width.
  • ⚠ PIN date in the future: still open, tools/model-tax.tsv:137 says PIN(2026-10-27). Set the date of the commit.
  • ⚠ Key equality includes at: still open (instance.rs:18).
  • ⚠ shared_slot ignores the gate: still open (experts_geom.rs:19).

Checked

  • Duplicates: the new commit adds 0 items and removes one copy. The earlier dryer pair library.rs:354-361 ~ 368-375 is two wrappers that differ in the language version; not one fact.
  • Complexity: check::read, open_as, generate, with_geometry each rose by 1 in the full PR (even growth). The new commit changes none.
  • Silent failure: no new site. The None arm of gqa_row in instance.rs:77 is a const panic!, so it fails the build.
  • Tests: the removed assert in the_registered_rows_are_the_shape_tables only checked the deleted copy against the owner. No test lost a contract.
  • Gates: no pin moved. Escape catalog: no pattern matched. Hot path: none (build time and open).

Notes

  • The Metal e2e gate was not run (the PR body says so). The evidence is an M1 Pro, not an nvidia-smi card.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@midagedev
midagedev merged commit 511262a into main Oct 26, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant