Skip to content

fix(strategy): reject a degenerate n in runner before building a Backend#276

Closed
statxc wants to merge 1 commit into
zeokin:mainfrom
statxc:fix/strategy-runner-n-boundary
Closed

fix(strategy): reject a degenerate n in runner before building a Backend#276
statxc wants to merge 1 commit into
zeokin:mainfrom
statxc:fix/strategy-runner-n-boundary

Conversation

@statxc

@statxc statxc commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

PR kind

  • fix
  • feat

Summary

matmul.runner.run rejects a degenerate n before touching the GPU, but strategy.runner.run / .compare do not — they construct a Backend (demanding a real GPU) and only then fail deep inside storage:

  • n=0ZeroDivisionError from the // (n * 8) block-size math
  • n<0 → numpy negative dimensions are not allowed

Reproduced on CPU (no GPU needed — the asymmetry is visible in which error you get):

matmul.runner.run(0)       -> ValueError: n must be a positive integer   # correct
strategy.runner.run(0)     -> RuntimeError: requires PyTorch             # Backend built first
strategy.runner.compare(0) -> RuntimeError: requires PyTorch             # Backend built first

Why it regressed (full transparency)

This check landed once in #268 and was lost when that PR was reverted in de2e149 to untangle a conflict with #232. #232's content was re-landed as 4c44b45 (expose validated spectral alpha), but #268 was never re-landed — and its strategy-side test went with it, so nothing caught the drop. Credit for the original fix belongs to #268; this restores the boundary and adds the coverage that would have caught the revert.

Changes

  • strategy/runner.py: reject a non-positive / non-integer n in run() and compare() before Backend(...), mirroring matmul.runner.run's existing check (bool is excluded explicitly, since bool subclasses int).
  • tests/test_runner_n_parity.py (new): a parity table driving matmul.run, strategy.run and strategy.compare through the same boundary cases, so the two standalone packages can't drift apart here again — plus an n=1 off-by-one guard. A stub Backend that raises on construction proves validation runs before any device work, so it runs on CPU.

Touches only strategy/ and tests/ — no protected paths.

Validation

uv run --extra test python -m pytest tests/test_runner_n_parity.py -q        # 15 passed
uv run --extra test python -m pytest tests/ strategy/tests/ eval/tests/ -q   # 401 passed, 27 skipped

After the fix, strategy.runner.run(0) / .compare(0) raise ValueError: n must be a positive integer, got 0 with no GPU required.

Checklist

  • CPU-safe validation passed.
  • My commits do not include Co-authored-by footers for coding agents such as Cursor, Codex, Claude, Copilot, or similar tools.
  • Not a feat PR — no scorecard required.

matmul.runner.run rejects a non-positive / non-integer n up front, but
strategy.runner.run and .compare did not: they constructed a Backend (demanding
a real GPU) and only then failed inside storage -- ZeroDivisionError from the
`// (n * 8)` block-size math for n=0, or numpy's negative-dimensions error for
n<0. Mirror matmul's boundary check in both entry points.

This check landed once in zeokin#268 and was lost when that PR was reverted
(de2e149) to untangle a conflict with zeokin#232; zeokin#232 was re-landed as 4c44b45 but
zeokin#268 never was, and no test covered the strategy side.

Add tests/test_runner_n_parity.py: a parity table driving matmul.run,
strategy.run and strategy.compare through the same boundary cases, so the two
standalone packages cannot drift apart here again. A stub Backend that raises
on construction proves validation happens before any device work, so it runs on
CPU with no GPU.
@github-actions github-actions Bot added area:strategy Smart strategies / transforms (strategy/) area:tests Correctness gates and test suites (tests/) status:needs-review Awaiting maintainer review type:bug Something is incorrect or broken status:ready-non-gpu Fix/docs PR cleared non-GPU triage; maintainer review only labels Jul 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR currently has merge conflicts with the base branch. Please resolve them within 12 hours or the bot will close the PR automatically.

@github-actions github-actions Bot removed the status:ready-non-gpu Fix/docs PR cleared non-GPU triage; maintainer review only label Jul 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because the reported merge conflict was not resolved within 12 hours of the bot reminder.

@github-actions github-actions Bot closed this Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:strategy Smart strategies / transforms (strategy/) area:tests Correctness gates and test suites (tests/) status:needs-review Awaiting maintainer review type:bug Something is incorrect or broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant