Skip to content

fix(metal): reject every malformed GEMM shape component - #1109

Merged
juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-bot-1108
Nov 1, 2026
Merged

juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-bot-1108

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Metal GEMM probe의 --shape가 잘못된 조각이 섞인 값을 정상 입력으로 받던 오류를 고쳤습니다.

What and why

Fixes #1108. metal_gemm_time --shape parsed each : separated piece with filter_map(parse().ok()), so a bad piece was dropped silently. 1:bad:2:3 came out as three valid numbers and ran, and the device path then hit a divide by zero. The parser now collects every piece as a Result and accepts a shape only when all three pieces parse as usize. A failure returns the existing --shape takes N:K:R, not <value> error before the device opens. run takes its arguments as a parameter so a unit test can call it, and the lab-metal recipe now runs that test.

Release notes: Fixed: metal_gemm_time rejects a --shape value with a malformed component instead of dropping it.

Code shape

  • No second copy. The --shape parser in crates/metal/src/bin/metal_gemm_time/mac.rs is the only N:K:R parser; I changed it in place.
  • Model code holds only architecture facts. No model code is touched.
  • Dead code is gone. Nothing is left unused.
  • No silent failure. A malformed piece, a wrong piece count or an overflowing number is the named --shape error.
  • Comments state what is true now. The change adds no comments.
  • No relaxed gate. No band, tolerance or lint moved.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": none.

What I ran

Base 2227bd5077e0d2157fc7c43c66003d70dc4e4a5b (upstream/main), head a7e7ac2e2861f725aae6a43866910a55e9b17636.

Before the fix, with only the regression test added, on an Apple M4 Pro:

CARGO_BUILD_JOBS=4 cargo test -p bloomery-metal --bin metal_gemm_time shape_requires_exactly_three_whole_numbers -- --exact mac::tests::shape_requires_exactly_three_whole_numbers
test mac::tests::shape_requires_exactly_three_whole_numbers ... FAILED
attempt to divide by zero
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

After the fix, on macOS:

cargo fmt -p bloomery-metal -- --check
cargo clippy -p bloomery-metal --bin metal_gemm_time
CARGO_BUILD_JOBS=4 cargo test -p bloomery-metal --bin metal_gemm_time shape_requires_exactly_three_whole_numbers -- --exact mac::tests::shape_requires_exactly_three_whole_numbers
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

The test feeds eleven malformed values (bad characters, empty pieces, too few or too many pieces, a negative number, a space, a usize overflow) after a valid --shape, and expects the named error for each. It also checks that repeated valid shapes, one of them usize::MAX:2:3, reach the --dump-source early exit.

The binary itself:

cargo run -p bloomery-metal --bin metal_gemm_time -- --shape 1:bad:2:3 --dump-source /dev/null
metal_gemm_time: --shape takes N:K:R, not 1:bad:2:3        (rc=1)
cargo run -p bloomery-metal --bin metal_gemm_time -- --shape 1:2:3 --shape 4:5:6 --dump-source /dev/null
                                                           (rc=0)

The clippy ratchet over the workspace (cargo clippy --workspace --all-targets) reports lint rc 0 (^warning: 0, the ratchet 0 in tools/lint-ratchet.txt).

Hardware (if the change is for a machine)

The change touches only the host side argument parsing of a Metal development probe, which opens no device for a rejected shape. The failing run was on an Apple M4 Pro (applegpu_g16s, macOS); the passing runs were on an Apple Silicon Mac running macOS.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added the tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo label Nov 1, 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: Malformed shape components now produce the named error before device setup.

Checked

Duplicates: 1 new function checked; no duplicate owner. Dryer reported 0 pairs.
Complexity: crapper flagged no functions; the parser retains its existing match.
Silent failure: 1 changed error path followed; invalid components and wrong component counts return an error through main.
Tests: mac.rs:475 is protected by the malformed-input assertions. Restoring the old token-dropping parser makes them fail. The valid-input assertion also rejects narrowing from usize.
Gates: no pins or cases moved or disappeared; lab-metal now runs the binary's tests.
Escape catalog: no patterns matched.

Notes

The macOS-only regression test was not run here. The PR body includes FAIL-first and passing macOS output.

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

Filed outside this PR: #1110

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 7364a5f into midagedev:main Nov 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

metal-gemm-time: malformed shape components are silently discarded

1 participant