Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ add_dependencies(onebit npu_decode_note_test)
# engine#2106: a shipped kernel set's own consistency -- manifest vs bytes, one form or none, name vs directory. The
# refusal this pre-empts ("differ in size") needed private-repo archaeology to diagnose; the symptom is checkable here.
add_test(NAME kernel_set_check COMMAND bash ${CMAKE_SOURCE_DIR}/tests/kernel_set_check_test.sh)

# engine#2186/#2244: two checks that were only ever run by hand. `tools/ci/dupdef_check.py` is the one
# that found a shadowed `def self_test()` in tools/hrx_f16_narrowing_check.py -- the surviving copy
# passed and the shadowed one's arms could never fail. Both take no build input, so a ctest row is the
# route that needs no ci.yml line (the workflow is not editable from a lane).
add_test(NAME dupdef_check COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/ci/dupdef_check.py --check
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_test(NAME hrx_f16_narrowing_selftest COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/hrx_f16_narrowing_check.py --self-test
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
add_test(NAME bump_hrx_report COMMAND bash ${CMAKE_SOURCE_DIR}/tests/bump_hrx_report_test.sh ${CMAKE_SOURCE_DIR}/scripts/bump-hrx-report.sh)
add_test(NAME metrics_serve COMMAND ${CMAKE_SOURCE_DIR}/tests/metrics_serve.sh $<TARGET_FILE:onebit>)
# serve --print-backend: the llama-server Lemonade starts itself, one hop (docs/serve.md): no GPU.
Expand Down
132 changes: 132 additions & 0 deletions tools/ci/dupdef_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
# Copyright 2026 bong-water-water-bong
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""dupdef_check.py -- a shadowed top-level definition silently disables the one it shadows.

A Python module may define `def self_test()` twice. The second wins, the first is dead, and nothing
warns: the file still imports, the surviving function still passes, and the arms the first one
carried can never fail because they never run. `tools/hrx_f16_narrowing_check.py` shipped exactly
that -- two `self_test()` bodies, the scan arms in the shadowed one -- which is a check that cannot
fail wearing the costume of two checks.

Usage:
dupdef_check.py --check [--root DIR ...]
dupdef_check.py --self-test
"""

import ast
import pathlib
import sys

# Vendored and generated trees are not ours to police; a duplicate there is upstream's business.
SKIP_DIR_NAMES = {'.git', 'build', 'node_modules', '__pycache__', 'third_party', '.venv'}
DEFAULT_ROOTS = ('tools', 'scripts')


def duplicates(path):
"""[(name, [lineno, ...])] for top-level def/class names defined more than once."""
try:
tree = ast.parse(path.read_text(errors='replace'))
except (SyntaxError, OSError):
# A file we cannot parse is not this check's finding; whoever owns syntax reporting says so.
return []
seen = {}
for node in tree.body:
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
seen.setdefault((node.name, type(node).__name__), []).append(node.lineno)
return [(name, lines) for (name, _kind), lines in sorted(seen.items()) if len(lines) > 1]


def files_under(roots):
for root in roots:
p = pathlib.Path(root)
if p.is_file() and p.suffix == '.py':
yield p
continue
for f in sorted(p.rglob('*.py')):
if not any(part in SKIP_DIR_NAMES for part in f.parts):
yield f


def main(argv):
if '--self-test' in argv:
return self_test()
if '--check' not in argv:
print(__doc__.strip().splitlines()[-1].strip())
print('usage: dupdef_check.py --check [--root DIR ...] | --self-test')
return 2
roots = []
for i, a in enumerate(argv):
if a == '--root' and i + 1 < len(argv):
roots.append(argv[i + 1])
roots = roots or list(DEFAULT_ROOTS)

bad = 0
n = 0
for f in files_under(roots):
if f.name == 'dupdef_check.py':
continue # its own fixtures define duplicates on purpose
n += 1
for name, lines in duplicates(f):
bad += 1
print(f"dupdef: {f}: `{name}` is defined {len(lines)} times (lines {', '.join(map(str, lines))}) "
f"-- everything but the last is dead, and its assertions never run")
if bad:
print(f"dupdef_check: {bad} shadowed definition(s) in {n} file(s)")
return 1
print(f"dupdef_check: no shadowed top-level definition in {n} file(s)")
return 0


def self_test():
import tempfile
rc = 0

def chk(want, got, what):
nonlocal rc
if want == got:
print(f"ok {what}")
else:
print(f"FAIL {what}: want {want}, got {got}")
rc = 1

with tempfile.TemporaryDirectory() as d:
p = pathlib.Path(d, 'shadowed.py')
p.write_text("def self_test():\n return 1\n\ndef other():\n return 2\n\ndef self_test():\n return 3\n")
chk([('self_test', [1, 7])], duplicates(p), 'two top-level defs of one name are reported with both lines')

q = pathlib.Path(d, 'indented.py')
q.write_text("class A:\n def f(self):\n return 1\n def f(self):\n return 2\n")
chk([], duplicates(q), 'a method defined twice inside a class is not a top-level shadow')

r = pathlib.Path(d, 'once.py')
r.write_text("def a():\n pass\n\ndef b():\n pass\n")
chk([], duplicates(r), 'distinct names are not a finding')

s = pathlib.Path(d, 'broken.py')
s.write_text("def a(:\n")
chk([], duplicates(s), 'an unparseable file is not this check\'s finding (absence of a result, not a pass)')

# The defect this check was written for, reproduced: the surviving def passes, the shadowed one never runs.
t = pathlib.Path(d, 'hrx_shaped.py')
t.write_text("def self_test():\n check('scan arm')\n\ndef check(x):\n pass\n\ndef self_test():\n pass\n")
chk([('self_test', [1, 7])], duplicates(t), 'the hrx_f16_narrowing_check.py shape (a shadowed self_test) is caught')

print(f"dupdef_check --self-test: {'ok' if rc == 0 else 'FAIL'}")
return rc


if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
51 changes: 28 additions & 23 deletions tools/hrx_f16_narrowing_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#
# usage: hrx_f16_narrowing_check.py <corpus-root> [--self-test]
# Exit: 0 always for a report (this is an inventory, not a gate); --self-test is the gate.
import re, sys, pathlib
import hashlib, re, sys, pathlib

NARROW = re.compile(r'fptrunc\s+(%[A-Za-z0-9_]+)|fptrunc\s+%([A-Za-z0-9_]+)')
DEF = re.compile(r'\s*(%[A-Za-z0-9_]+)\s*=\s*(.*)$')
Expand Down Expand Up @@ -160,28 +160,6 @@ def scan(path, lines):
return out


def self_test():
rc = 0
def chk(want, got, what):
nonlocal rc
if want == got:
print(f"ok {what}")
else:
print(f"FAIL {what}: want {want}, got {got}")
rc = 1
mul_acc = [" %init = vector.fragment<init> %zero shape [%m, %n] : vector<16xf16>",
" %next = vector.mulf %block_acc, %k : vector<16xf16>"]
chk('mul', scan('x', mul_acc)[0][3], 'a mul-updated f16 accumulator is flagged (the #2186 item-1 shape)')
add_acc = [" %init = vector.fragment<init> %zero shape [%m, %n] : vector<16xf16>",
" %next = vector.addf %block_acc, %k : vector<16xf16>"]
chk('add', scan('x', add_acc)[0][3], 'an add-only f16 accumulator is reported separately')
f32 = [" %init = vector.fragment<init> %zero shape [%m, %n] : vector<16xf32>"]
chk(0, len(scan('x', f32)), 'an f32 accumulator is not this defect')
print(f"self-test: {'ok' if rc == 0 else 'FAIL'}")
return rc



def self_test():
rc = 0
def chk(want, got, what):
Expand Down Expand Up @@ -234,17 +212,42 @@ def chk(want, got, what):
far = [" %init = vector.fragment<init> %zero shape [%m, %n] : vector<16xf16>"] + [" // pad"] * 200 + \
[" %next = vector.mulf %block_acc, %k : vector<16xf16>"]
chk('mul', scan('x', far)[0][3], 'class 3: an update 200 lines below the init is still found')
# The corpus is part of the result: same bytes -> same fingerprint, a changed file -> a different one.
import tempfile
with tempfile.TemporaryDirectory() as d:
r = pathlib.Path(d)
(r / 'a.loom').write_text('one\n'); (r / 'sub').mkdir(); (r / 'sub' / 'b.loom').write_text('two\n')
fp1 = fingerprint(r)
chk(True, fp1.endswith('(2 .loom file(s))'), 'the fingerprint counts the .loom files it read')
chk(fp1, fingerprint(r), 'the same corpus fingerprints the same way twice')
(r / 'sub' / 'b.loom').write_text('two changed\n')
chk(False, fp1 == fingerprint(r), 'a one-byte change to one file changes the fingerprint')
(r / 'a.loom').rename(r / 'sub' / 'a.loom')
chk(False, fp1 == fingerprint(r), 'a file moved to another path changes it too (the path is hashed)')
print(f"self-test: {'ok' if rc == 0 else 'FAIL'}")
return rc


def fingerprint(root):
"""sha256 over the corpus: sorted (relative path, file digest), so a moved file changes it too."""
h = hashlib.sha256()
n = 0
for p in sorted(root.rglob('*.loom')):
h.update(str(p.relative_to(root)).encode())
h.update(hashlib.sha256(p.read_bytes()).digest())
n += 1
return f"{h.hexdigest()[:16]} ({n} .loom file(s))"


def main(argv):
if '--self-test' in argv:
return self_test()
if len(argv) < 2:
print("usage: hrx_f16_narrowing_check.py <corpus-root>")
return 2
root = pathlib.Path(argv[1])
# The corpus is part of the result. Two runs that disagree are only comparable if the tree they
# read is named and fingerprinted, and "it found 9" is unattributable without both.
inputs, pubs, accs = [], [], []
for p in sorted(root.rglob('*.loom')):
lines = p.read_text(errors='replace').splitlines()
Expand All @@ -255,6 +258,8 @@ def main(argv):
pubs.append(f"{p.relative_to(root)}:{ln} {operand}")
for ln, acc, width, kind, upd in scan(p, lines):
accs.append((kind, f"{p.relative_to(root)}:{ln} {acc} vector<{width}xf16> [{upd or '-'}]"))
print(f"CORPUS {root.resolve()}")
print(f"FINGERPRINT {fingerprint(root)}")
for r in inputs:
print(f"INPUT {r}")
for r in pubs:
Expand Down
Loading