Skip to content

fix(metal): subtract delta tap distance before reducing the ring - #1098

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:bot-795-delta-ring-2
Nov 1, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:bot-795-delta-ring-2

Conversation

@juliankang4

@juliankang4 juliankang4 commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Metal delta 커널과 공유 oracle의 ring slot 계산에서 u32 덧셈 넘침을 없애는 버그 수정입니다.

Fixes #795.

What and why

The Metal delta tap computed its ring slot as (pos0 + ring_rows - back) % ring_rows. Once pos0 is within ring_rows of u32::MAX, the addition wraps and the kernel reads the wrong slot (and the shared oracle panics with an overflow in debug builds). The pos0 < back guard already runs first, so the subtraction cannot go negative and the addition is not needed. The fix is (pos0 - back) % ring_rows in crates/metal/kernels/delta.metal (tap_x) and in crates/metal/src/bin/shared/oracle_delta.rs (tap). Launches, memory accesses and float operation order are unchanged.

The delta_ring gate clause now also checks the last safe position, the first overflowing position and u32::MAX against their ring-equivalent small positions, comparing g, partials, state and ring bits. A new mutant, delta_ring_overflow, restores the old expression and must be rejected by that clause.

Release notes: Fixed: Metal delta layers read the correct ring slot at positions near the u32 limit.

Code shape

The review reads these first. A box left unticked needs one line of why.

  • No second copy. Logic two models (or two call sites) share has one common owner; I lifted it there instead of
    copying it (AGENTS.md "Common code first", docs/rust-quality.md R14). Where I found an existing copy, I
    named it below.
  • Model code holds only architecture facts. A model's own files carry only what its architecture forces
    (tensor shapes, the attention kind, the quant). A common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. Nothing I made unused is left behind (functions, flags, gate clauses, docs).
  • No silent failure. Undefined input is a named error or panic, never a defined-looking output.
  • Comments state what is true now. No dates, issue numbers or how a bug was found (tools/check-comments.sh).
  • No relaxed gate. No band, tolerance or lint moved to satisfy a check; a re-pin carries PIN(YYYY-MM-DD):
    and its reason.
  • 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. The shader and the oracle are separate-language owners of the same slot rule and both changed.

What I ran

Base e9f238b0d53e12f8a5e75d89450b59fd0c6a2b11 (upstream/main), PR head 93be74347a3a0f6812ee5cb1749129677dac7d36. The gate needs no model file: the delta shape uses drawn parameters.

Before the fix, the new boundary check makes the oracle overflow:

$ CARGO_BUILD_JOBS=4 cargo build -p bloomery-metal --bin gate_metal_qwen35moe_kernels
$ bash tools/mac-gate.sh target/debug/gate_metal_qwen35moe_kernels --instance qwen35moe_gdn48 --only delta_ring
thread 'main' panicked at crates/metal/src/bin/gate_metal_qwen35moe_kernels/../shared/oracle_delta.rs:310:17:
attempt to add with overflow

On the PR head:

$ cargo clippy -p bloomery-metal --bin gate_metal_qwen35moe_kernels
$ cargo build -p bloomery-metal --bin gate_metal_qwen35moe_kernels
$ bash tools/mac-gate.sh target/debug/gate_metal_qwen35moe_kernels --instance qwen35moe_gdn48 --only delta_ring
clause delta_ring: PASS (position 0 reads no ring slot, positions 1 and 2 read the live slot, one slot is written, and positions through u32::MAX match their ring-equivalent positions)
gate_metal_qwen35moe_kernels [qwen35moe_gdn48] (only delta_ring): PASS
$ bash tools/mac-gate.sh target/debug/gate_metal_qwen35moe_kernels --instance qwen35moe_gdn48 --only delta_rows
clause delta_rows: PASS (rows@first position 1@4, 9@0, 9@20, 12@7, 12@3, 64@5, 64@9 are the stepped launches' bits)
gate_metal_qwen35moe_kernels [qwen35moe_gdn48] (only delta_rows): PASS
$ bash tools/mac-gate.sh target/debug/gate_metal_qwen35moe_kernels --instance qwen35moe_gdn48 --mutant delta_ring_overflow
mutant: delta_ring_overflow (adding the ring length before subtracting the tap distance wraps at large positions)
clause delta_ring: FAIL (g[0]: got -3.926735e-4, want -3.825856693008463e-4, error 1.008783873044744e-5 over the band 2.3556013816811785e-8)
gate_metal_qwen35moe_kernels [qwen35moe_gdn48] (mutant delta_ring_overflow): FAIL (delta_ring)
PASS: delta_ring_overflow rejected with rc=1

Lint ratchet on the workspace (cargo clippy --workspace --all-targets with the repository's feature set):

chain-clippy: lint rc 0 (^warning: 0, the ratchet 0 in tools/lint-ratchet.txt)

Hardware (if the change is for a machine)

Apple silicon Mac (Mac17,2), macOS 27.0.1, Metal shaders compiled from source at run time (gemm: architecture=applegpu_g17g metal4=true matrix-units=true engine-path=tensor).

AI help

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

Avoid overflowing the position word when looking up ring predecessors near u32::MAX. Keep the shared oracle on the same guarded subtraction and pin the original expression with a boundary mutant.

Fixes midagedev#795

@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: Guarded subtraction avoids ring-slot overflow, and the extended gate checks positions near the u32 limit.

Checked

Duplicates: no new functions, types or constants; the added mutant has a unique name. Dryer reported no pairs.
Complexity: diagnostics report delta_ring rising from 17 to 24 through a boundary-case loop and comparisons of four outputs.
Silent failure: the shader and oracle zero-return guards supply convolution padding for predecessors before the sequence starts.
Tests: the added loop compares g, partials, state and ring bits at three upper-boundary positions against ring-equivalent small positions. The new mutant restores the addition at delta.metal:168; the PR reports rejection on g[0].
Gates: no thresholds, tolerances, pins or cases removed or relaxed. The PR supplies an oracle overflow before the fix and mutant-failure output afterward.
Performance: the ring read removes one unsigned addition. Launches, copies and floating-point order are unchanged in the diff.
Boundary checks: sequence-start subtraction is guarded; geometry requires 11 ring rows; chunk-position overflow is refused.
Escape catalog: no C1–C4 patterns matched. No ledger lessons were in force.

Notes

Metal gates were not run locally. The PR supplies Apple Silicon delta_ring and delta_rows passes and rejection of delta_ring_overflow.

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

@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 4129d11 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

metal: convolution ring lookup overflows at valid high positions

1 participant