From ca52fdfea23ccac65262666901d03e9c0b3ed2f3 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Thu, 22 Oct 2026 13:31:51 -0600 Subject: [PATCH 01/13] feat(hipdnn): report kernel ingestor runtime impact in deep review A change to the kernel ingestor can build cleanly and still break at run time. The ingestor C++ compiles only with the ingestor flag ON, and the contracts between a rocKE kernel and the C++ that launches it are kept by hand: the kernel's argument list is restated in native packs, test pins and descriptor signatures, and launch geometry, applicability rules and the shipped variant set have Python and C++ halves. A copy that falls out of step is caught on a device, in a flags-ON lane, or not at all, and two pull requests that each pass alone can break the target branch once both merge. The hipdnn-deep-review skill gains an IMPACT lane for these contracts. impact.py reads git objects only and reports, per contract, what changed on which side between the merge base and the head, and also in the head merged into the target branch's tip, so a copy that landed on the target after the merge base is compared too. It judges content rather than paths: comment, whitespace and type-preserving data edits are inert, removed or re-signed Python symbols are traced to their readers through imports and base classes, and a surface file it cannot understand is reported as changed, never as safe. impact_emit.py builds every shipped rocKE variant at both revisions on the CPU, regenerating DVC-held catalogs with the generator's registry enumerator, and compares each variant's kernel arguments and emitted LLVM IR, which separates a kernel edit that changes shipped code from one that does not. Each run ends in one line: BREAKS, NEEDS-DEVICE with the architectures, NONE or N/A. The lane brief maps these facts to findings, names the device runs that would settle what static analysis cannot, and hands CI coverage, build wiring and public API questions to the lanes that own them. The partition script scopes the lane's packet to the ingestor paths, the leak sweep accepts its finding IDs, and the skill's documentation describes the lane and the standalone command. ALMIOPEN-2834 --- AGENTS.md | 1 + projects/hipdnn/docs/ai-rules.md | 1 + .../ai/skills/hipdnn-deep-review/RUNBOOK.md | 10 +- .../ai/skills/hipdnn-deep-review/SKILL.md | 9 +- .../skills/hipdnn-deep-review/lanes/leaks.md | 2 +- .../skills/hipdnn-deep-review/lanes/orient.md | 4 +- .../lanes/runtime-impact.md | 86 + .../hipdnn-deep-review/scripts/impact.py | 2903 +++++++++++++++++ .../hipdnn-deep-review/scripts/impact_emit.py | 685 ++++ .../hipdnn-deep-review/scripts/partition.py | 18 +- .../tools/ai/tests/test_deep_review_impact.py | 550 ++++ .../ai/tests/test_deep_review_impact_emit.py | 441 +++ .../ai/tests/test_deep_review_partition.py | 16 + 13 files changed, 4714 insertions(+), 12 deletions(-) create mode 100644 projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md create mode 100644 projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py create mode 100644 projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py create mode 100644 projects/hipdnn/tools/ai/tests/test_deep_review_impact.py create mode 100644 projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py diff --git a/AGENTS.md b/AGENTS.md index 4bc54c284853..34b56c5f6695 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -78,6 +78,7 @@ All under [`projects/hipdnn/tools/ai/skills/`](projects/hipdnn/tools/ai/skills/) | `hipdnn-tidy` | Running clang-tidy on hipDNN, scoped to the files you changed. | | `hipdnn-ingestor-engine` | Creating or extending a generic-kernel-ingestor integration. | | `hipdnn-uhd` | Taking a hipDNN engine through the UHD heuristic lifecycle on the local GPU: generating, training, judging, validating, installing, and triaging heuristics and their training data. | +| `hipdnn-deep-review` | Reviewing a hipDNN or provider change in depth with parallel agents until its findings close, or checking whether a kernel ingestor change breaks kernel ABI or dispatch at run time (its `IMPACT` lane and `impact.py`). | | `hipdnn-codegen` (at [`projects/hipdnn/tools/DescriptorGenerator/.claude/skills/`](projects/hipdnn/tools/DescriptorGenerator/.claude/skills/hipdnn-codegen/SKILL.md)) | Generating operation boilerplate from a YAML config. | | `rfc-review` | Reviewing a technical RFC across the structured perspectives. Follow-ups: `rfc-review-compatibility`, `rfc-review-ops`, `rfc-review-security`. | | `rfc-backlog` | Turning an RFC into a backlog of Jira stories and tasks. | diff --git a/projects/hipdnn/docs/ai-rules.md b/projects/hipdnn/docs/ai-rules.md index 40a828a8abfc..34a6168b9d1d 100644 --- a/projects/hipdnn/docs/ai-rules.md +++ b/projects/hipdnn/docs/ai-rules.md @@ -32,6 +32,7 @@ When a user asks for a workflow covered by a project skill, tell them the projec - `tools/ai/skills/hipdnn-deep-review/SKILL.md` - Runbook for a multi-agent deep review of a hipDNN or provider PR, branch or commit range: scouts map each affected area, focused reviewer lanes run in parallel, every finding is verified by source proof, reproduction or mutation, and a ledger carries findings across rounds until no blocking or important one stays open. Owns its review policy; the review is advisory and never posts without approval. - Suggest this skill when the user asks for a deep, thorough or multi-agent review, or wants to iterate a review until all findings close. Use `hipdnn-pr-quality` for drafting PR text or a single-pass review. Requires subagents and its sibling skills `hipdnn-superbuild` and `hipdnn-superbuild-test`. + - Its `IMPACT` lane reports kernel ingestor changes that break at run time while the build stays green: kernel argument lists out of step with their C++ and test copies (also against the target branch's tip, which catches two PRs that each pass alone), dispatch predicates, descriptors and generic targets, and which shipped variants' emitted code changed. Suggest `scripts/impact.py report` from that skill for a quick answer on a hip-kernel-provider, rocKE or ingestor change without a full review. - `tools/ai/skills/hipdnn-superbuild/SKILL.md` - Builds hipDNN together with one or more providers via the repository-root superbuild presets (`hipdnn-providers`, `miopen-provider`, `hipblaslt-provider`, `hip-kernel-provider`, `hipdnn-samples`, etc.), in a single CMake invocation. On Windows it auto-runs the wheel-based ROCm setup when no SDK path is supplied. - Suggest this skill when the user asks to build hipDNN with providers, run a superbuild preset, rebuild after a rebase or merge, or set up a fresh build from the repo root. Prefer it over the standalone build whenever providers are involved. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md index 4588a31ecc06..010f196b1ff7 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md @@ -8,7 +8,7 @@ needs subagents; without them, stop and tell the user to run `hipdnn-pr-quality` `` is the PR number, `` the round. `/` comes from the PR URL, else `origin` of `$CLONE`, else `ROCm/rocm-libraries`; pass `-R /` to every `gh pr` and -`gh issue` call. `pin.py`, `partition.py`, `ledger.py` and `comments.py` are in `$SKILL/scripts/`; +`gh issue` call. `pin.py`, `partition.py`, `ledger.py`, `comments.py`, `impact.py` and `impact_emit.py` are in `$SKILL/scripts/`; a script name below stands for `python3 "$SKILL/scripts/"` (`python` or `py -3` on Windows, as in SKILL.md *Platforms*). Each prints terse results and exits non-zero with a one-line reason. After pinning, commands run from `$EVIDENCE/r/` (sourcing `env.sh` changes to it); GNU coreutils, grep and sed are assumed. @@ -157,6 +157,7 @@ its trigger holds, else mark it `N/A`; when unsure, run it. | `DOCS` | [docs](lanes/docs.md) | Accuracy and concision of docs and comments, including edited public headers | A documentation or source file changes | | `DRIFT` | [drift](lanes/drift.md) | The change merged onto the target's current tip: conflicts, broken target-side contracts, tests on the merge | `drift.txt` says `Trigger: yes` | | `RUN` | [build-validate](lanes/build-validate.md) | Building the head and running the relevant tests | Always | +| `IMPACT` | [runtime-impact](lanes/runtime-impact.md) | Kernel ABI and dispatch contracts of the kernel ingestor (kernarg lists, launch surfaces, matchers, descriptors, generic targets) at base, head and the merged tree; which shipped variants change and which device runs would settle it | A file under `dnn-providers/hip-kernel-provider/`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, `projects/hipdnn/tools/IngestorGenerator/` or `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` changes | `ORCH` (process findings you file), `SCOUT`, `SYNTH` ([orient](lanes/orient.md)) and `VER` ([verify](lanes/verify.md)) are not partition choices. Merge two lanes into one agent when the @@ -173,7 +174,7 @@ ledger.py seed "$EVIDENCE" --lane 'DEPS=N/A (no manifest or source changes)' `packets` writes `packets/.md` ([lanes/common.md](lanes/common.md) *Context packet*) for each lane of `owners.tsv` and each concern lane in `--lanes` (changed files in scope with their -`numstat` rows, areas and Orient paths; BUILD and DEPS scope to their trigger files and print +`numstat` rows, areas and Orient paths; BUILD, DEPS and IMPACT scope to their trigger files and print `: no changed file in scope, N/A` when none; `DRIFT` adds `drift.txt` and prints `DRIFT: N/A ()` when its trigger is `no` (give that reason to `seed` with `--lane`); every other concern lane takes all changed files, so name only lanes whose trigger holds; `--parent` is the stack parent). In round 2 on, @@ -214,7 +215,8 @@ Mutation: | writer as its own fresh agent with shell and write access; fill this template as its first message. Agents see only their dispatch: each begins by reading `dispatch-context.md`. -**Queue.** Run at most `Agents:` agents at once, in this priority order: `RUN` first, then `DRIFT`, the +**Queue.** Run at most `Agents:` agents at once, in this priority order: `RUN` first, then `DRIFT`, then +`IMPACT` (its emitter builds every shipped variant twice), then the longest lanes (largest `CORR`, then `TESTQ`), `DUP` and `DOCS` last. `RUN`, `DRIFT`, an approved `ROCK` build, `ROCK`'s configure diff (`Build host: yes`), or any other lane that builds or mutates, run concurrently in their own `$SCRATCH` trees and split `Cores:` between them while they overlap, `RUN` taking the larger share. @@ -230,7 +232,7 @@ Inputs: under $EVIDENCE/r/: PIN.md; pr/body.md or intent.md; CONTEXT.md an Scout: area, slug, changed paths, prior area file or none. SYNTH: orient/*.md. Verifier: finding IDs and report paths. WRITER: LEDGER.md, verify/, PIN.md, pr/pr.json (title), CONTEXT.md, lanes/*.md (M-rule lines, RUN execution counts, - ROCK not-run items). `RUN`, `ROCK`, `AUTH`: the **Options and guards** and + ROCK not-run items). `RUN`, `ROCK`, `AUTH`, `IMPACT`: the **Options and guards** and **Consumers** sections of `CONTEXT.md` and the area files. Follow-up: the lane report path plus the approved request, decision or Handoff. Output: ; scratch $SCRATCH//; first provisional ID p diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md index 80701dd76e67..afa2b12a1b90 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md @@ -1,6 +1,6 @@ --- name: hipdnn-deep-review -description: "Multi-agent deep review of a hipDNN or dnn-providers PR, branch or commit range. Scouts map each affected area, focused reviewer lanes run in parallel, every finding is verified by source proof, reproduction or mutation, and a ledger carries findings across rounds until no blocking or important one stays open. For a PR body or a quick single-pass review, use hipdnn-pr-quality." +description: "Multi-agent deep review of a hipDNN or dnn-providers PR, branch or commit range. Scouts map each affected area, focused reviewer lanes run in parallel, every finding is verified by source proof, reproduction or mutation, and a ledger carries findings across rounds until no blocking or important one stays open. Its IMPACT lane reports kernel ingestor changes that break at run time while the build stays green (kernel ABI, dispatch, descriptors), and its script answers that question standalone. For a PR body or a quick single-pass review, use hipdnn-pr-quality." argument-hint: "[ | branch: | ..] evidence: scratch: [clone:] [intent:] [terms:] [knowledge:] [round:]" allowed-tools: Bash, Read, Write, Edit, Grep, Glob, Task --- @@ -16,7 +16,8 @@ what each agent reads, and `lanes/` holds the briefs. `RUN` builds with [hipdnn-superbuild-test](../hipdnn-superbuild-test/SKILL.md); install those skills alongside this one. Prerequisites: network access, an authenticated `gh` with read access, `git` 2.38 or later (`pin.py` uses `git merge-tree --write-tree`), and Python 3 (it runs the scripts under -`$SKILL/scripts/`); `pre-commit` and `pytest` for the lanes that run validators. `RUN` and verifiers also need +`$SKILL/scripts/`); `pre-commit` and `pytest` for the lanes that run validators. The `IMPACT` lane needs Python 3.10 or newer, +PyYAML, and Jinja2 for its emitter. `RUN` and verifiers also need a ROCm install (Linux: `/opt/rocm` or `ROCM_PATH`; Windows: the wheel SDK that [hipdnn-superbuild](../hipdnn-superbuild/SKILL.md) provisions), CMake, Ninja and about 25 GB free in `$SCRATCH`; without them `defect` and `test-gap` findings stay `UNVERIFIED` and the review closes only through human `ACCEPTED-RISK` decisions, recorded with `ledger.py set` (`PIN.md` records `Build host`). @@ -25,6 +26,10 @@ Platforms: Linux, macOS and Windows. On Windows run the skill from Git Bash (Git `bash`, `awk`, `sed`, `grep` and `comm`. Blocks write the interpreter as `python3`; on Windows that name is the Microsoft Store stub, so use `python` or `py -3` instead. Scripts read and write UTF-8 and LF whatever the host's locale. +For a quick answer without a review, `impact.py report --repo --base --head ` (add `--target-tip +` for an open PR whose target moved) prints the kernel ABI and dispatch contracts a change touches, as the `IMPACT` +lane ([lanes/runtime-impact.md](lanes/runtime-impact.md)) collects them, ending in a `Runtime impact:` line. + ## Entry contract Ask for what is missing; record it in `PIN.md`: diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/leaks.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/leaks.md index 75d248dd07d7..19d85596fa61 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/leaks.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/leaks.md @@ -183,7 +183,7 @@ load_output() { load_output || { echo "STOP: sweep input missing, not clean" >&2; exit 1; } sed -i.bak 's#users/[A-Za-z0-9._-]*/#users/#g' "$L/pub.content" && rm "$L/pub.content.bak" # the expected branch prefix cat > "$L/sweep-allow.txt" <<'EOF2' -^(CORR|TESTQ|TESTB|AUTH|BUILD|DEPS|API|ROCK|LEAK|DUP|DOCS|RUN|DRIFT|ORCH|VER|SCOUT|SYNTH)[0-9]*-p?[0-9]+$ +^(CORR|TESTQ|TESTB|AUTH|BUILD|DEPS|API|ROCK|LEAK|DUP|DOCS|RUN|DRIFT|IMPACT|ORCH|VER|SCOUT|SYNTH)[0-9]*-p?[0-9]+$ ^(handoffs?|worktrees?)$ ^https?://(github\.com/ROCm/[A-Za-z0-9._-]+|raw\.githubusercontent\.com/ROCm/[A-Za-z0-9._-]+|rocm\.docs\.amd\.com|docs\.amd\.com|www\.amd\.com|example\.com)([/:?#].*)?$ EOF2 diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/orient.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/orient.md index 6ebffd990755..40440d1bc1aa 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/orient.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/orient.md @@ -29,10 +29,10 @@ where, what it covers, how to select it); **Options and guards**; **Consumers**. **Options and guards.** For the area's changed files: each `option()` and cache variable with its default, each `#ifdef`, `#if` and CMake `if(...)` guard, and for each change the flag it depends on and that flag's default, so a change -that is dead code with the flag OFF shows. `RUN`, `ROCK` and `AUTH` read this section. +that is dead code with the flag OFF shows. `RUN`, `ROCK`, `AUTH` and `IMPACT` read this section. **Consumers.** The in-repo includers of the area's changed public headers (`git -C "$CLONE" grep -l
"$BASE" --- `) and which project or provider tests exercise them. `RUN` and `AUTH` read this section. +-- `) and which project or provider tests exercise them. `RUN`, `AUTH` and `IMPACT` read this section. Sources, in order: the area's `README.md` and `docs/`; `projects/hipdnn/docs/`; RFCs under `projects/hipdnn/docs/rfcs/`; then the code and tests. Check a document against the code diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md new file mode 100644 index 000000000000..f7f11600b107 --- /dev/null +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -0,0 +1,86 @@ +# Lane: runtime integration (`IMPACT`) + +Find changes that break the kernel ingestor at run time while the build stays green: a kernel's +argument list (its ABI) that no longer matches a copy, a dispatch predicate that drifts from the +kernel it selects, a descriptor or generic-target edit that changes what ships. The ingestor C++ +compiles only with `HIPDNN_ENABLE_KERNEL_INGESTOR` ON, so judge the change as built with the +ingestor, rocKE and SDPA flags ON, whatever their defaults. + +You judge; two scripts collect facts. `impact.py` reads git objects only. `impact_emit.py` +imports and runs rocKE code from the reviewed trees (a `git archive` extract, in a child process +with a timeout and a scrubbed environment): the same exposure as `RUN` building the head. + +## Contracts + +| Contract | Producer | Copies the change must keep in step | +|---|---|---| +| Kernel arguments (ABI) | rocKE builder `b.param(...)` sequence under `dnn-providers/hip-kernel-provider/rocke/library/kernels/` | static `KernelArgument` lists in `src/engines/kernel_ingestor_engine/packs/*.cpp`, literal pins in tests, descriptor `signature` arrays | +| Launch surface | Python grid, block, LDS and tile tables | the `cpp_mirror` files a `launch_surface:` entry names in `projects/hipdnn/tools/IngestorGenerator/configs/*.engine.yaml` | +| Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | +| Shipped variants | `*.kdp.json` (DVC-held for gfx950; regenerated by the generator's registry enumerator) | loader, native packs | +| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` | packer and loader | +| Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | + +Test code and CMake are not yours: they are `outside` for status, but tests are still searched +as copies and readers, so a stale pin or a stale import is yours. + +## Method + +1. Collect the facts. The report covers base to head and, when the target branch moved past the + merge base, the head merged into `$TARGET_TIP`, where contract breaks between two PRs that each + pass alone show up. `DRIFT` builds and tests that merge; you compare its contracts statically: + + ```bash + . ./env.sh; mkdir -p "$SCRATCH/IMPACT" lanes/IMPACT-evidence + python3 "$SKILL/scripts/impact.py" report --round-dir . > lanes/IMPACT-evidence/report.md + ``` + + Sourcing `env.sh` moves to the round directory. Exit 1 means the script could not run: return + `STOP:` with its message. +2. Run the emitter when any rocKE or `hkp_pack` Python file changed in code, including rows the + report calls `inert`: the static reachability it uses (a module counts as shipped when a shipped + builder imports it) misses a call through a dynamic lookup. The emitter builds every shipped + variant at both sides (a few minutes, CPU only) and records each variant's argument list and IR + digest: + + ```bash + python3 "$SKILL/scripts/impact_emit.py" run --repo "$CLONE" --base "$BASE" --head "$HEAD" --out "$SCRATCH/IMPACT/emit.json" + python3 "$SKILL/scripts/impact.py" report --round-dir . --emit "$SCRATCH/IMPACT/emit.json" > lanes/IMPACT-evidence/report-emit.md + ``` + + Record the flavor it used. A family listed under `failures` or a KDP under `not_inspected` + leaves its rows `changed`: never read a failure as equal. +3. Judge every row whose status is not `inert` or `outside`. Read both sides at `$HEAD` (and the + merged tree when the row says `merged-only`) before filing. +4. End **Checked, no finding** with the rollup line the script printed, corrected by your + judgment and saying why when it differs: + `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`. + +## From row to finding + +| Status | Action | +|---|---| +| `mismatch` | A `source-claim` at `BLOCKING` ([../policy.md](../policy.md): an ABI break without a migration path). **Where**: the producer line and every disagreeing copy. **Proof**: the command and both quoted lists. A `merged-only` mismatch breaks the target once merged: **Fix** is to rebase and update the named copy. If the same mismatch exists at `$BASE`, it is pre-existing: `FUTURE WORK` at most | +| `changed`, dispatch | Read the C++ function and its Python counterpart. A divergence (a constant, a range, a tile set, a mask or layout rule that the other side does not share) is a `source-claim` at `IMPORTANT` or higher, quoting both sides. Consistent: no finding | +| `changed`, emitted code or arguments | Consistent with every copy: no finding. The variants changed on an architecture need a device run there | +| `changed`, descriptor or generic target | Check the loader's rules for the changed fields (`projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/DescriptorLoader.hpp`); a value the loader rejects or ignores is a finding. A membership change names the architectures whose archives change | +| `inert`, `outside` | Count them under **Checked, no finding**; list each `inert` reason once | + +For every `changed` row without a finding, name the device run that would settle it: the ctest +entry (find it from the pack or kernel family name in the test sources and `ctest -N -R ` +in `RUN`'s build when one exists), the architecture, and the flags. Put each one under **Approval +requests** and add `AUTH: requires with the ingestor flags ON` under **Handoffs**; +`AUTH` decides whether a run executed. + +## Handoffs + +- `API`: a changed declaration in a plugin_sdk header, or a `.fbs` change. +- `BUILD`: flag matrix, option defaults, test registration, install rules. +- `ROCK`, `AUTH`: whether CI builds and tests the change; you supply the required pairs only. +- `DRIFT`: a `merged-only` row, so the merged build and tests cover the copy you name. +- `TESTB`: a contract with no test that would fail on the break you describe. + +Known gaps: HIP-kind kernels (`ConvNative`, `PointwiseNative`) have their C++ argument lists +checked but not their HIP producers; a dynamic C++ signature (`Gfx1151WmmaAttentionNative`) +cannot be compared statically; one LLVM flavor is emitted per run, so an edit that changes only +another flavor's lowering reads `inert`. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py new file mode 100644 index 000000000000..da998163b842 --- /dev/null +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py @@ -0,0 +1,2903 @@ +#!/usr/bin/env python3 +# Copyright (c) Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT +"""Runtime-integration impact of a git diff for hipDNN's kernel ingestor (kernel ABI, dispatch). + +Usage: impact.py --repo DIR --base REV --head REV + [--target-tip REV] [--json] + impact.py --round-dir DIR [--json] + impact.py report ... --emit FILE.json + +Emits facts, never severity beyond the row statuses and the rollup line. Reads git objects only +(cat-file, diff, ls-tree, grep, merge-tree); the repository and its worktrees are never written +(`merge-tree --write-tree` adds one tree object, no ref, no worktree). Exit 0 when the analysis +ran, whatever it found; exit 1 with one line `impact: ` on stderr when it could not. + +Views. Each fact is computed at base -> head and, when a target tip differs from base, also at the +merged tree `git merge-tree --write-tree ` (before = the tip, after = the merge). A +conflict skips the merged view and says why. A row that holds only in the merged view is marked +`merged-only`: that is the race class (a parallel PR landed a copy the diff cannot see). +`--round-dir` reads PIN.md (CLONE, BASE, HEAD, TARGET_TIP); a tip equal to BASE gives no merged +view. + +Row status: mismatch (producer and a copy disagree, a removed symbol still has a reader), changed +(a contract side changed, or static analysis cannot settle it; a surface file not understood is +always changed), inert (touched, provably without runtime effect), outside (not on the surface; +counted, never dropped). Test files and CMake are outside for status but are still searched as +kernarg copies and as symbol readers. + +Rollup (last line of `report`): + Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file) + +`report --emit FILE.json` folds in impact_emit.py output: changed params vs a static C++ list is a +mismatch; changed IR is `changed` for that arch; when every family is equal (nothing changed, +nothing failed, nothing not inspected) the rocKE rows that were `changed` only because a shipped +module changed become `inert`. Static kernarg mismatch rows are never downgraded. +""" + +import argparse +import ast +import fnmatch +import hashlib +import json +import re +import subprocess +import sys +from dataclasses import dataclass, field +from pathlib import Path, PurePosixPath + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import reviewlib as rl # noqa: E402 + +# -- Tables ------------------------------------------------------------------- + +HKP = "dnn-providers/hip-kernel-provider/" +ROCKE_LIB = HKP + "rocke/library/" +#: Where an import of a rocKE module may resolve. +IMPORT_ROOTS = (ROCKE_LIB, HKP + "rocke/platform/python/") +ENGINE = HKP + "src/engines/kernel_ingestor_engine/" +DESC_DIR = ENGINE + "descriptors/" +PACKER = HKP + "descriptor-packaging/" +GEN = "projects/hipdnn/tools/IngestorGenerator/" +SDK = "projects/hipdnn/plugin_sdk/" +SDK_INGESTOR = SDK + "include/hipdnn_plugin_sdk/ingestor/" +GENERIC_TARGETS_JSON = SDK + "data/gpu_generic_targets.json" +#: Pathspec of every listing and grep: where producers, copies and readers can live. +SEARCH = (HKP, GEN, SDK, "dnn-providers/integration-tests/") +#: Python files here that import rocKE modules are roots of the consumed closure. +IMPORTER_DIRS = (PACKER, ENGINE, GEN) +#: A shipped build's compile path starts here: the production descriptors and the packer. +PRODUCTION_DIRS = (DESC_DIR, PACKER + "python/", PACKER + "tools/") +DESCRIPTOR_SUFFIXES = tuple( + f".{kind}.json" for kind in ("kdp", "ukd", "kmd", "ued", "umd", "uhd", "udd") +) +ALL_ARCHS = "all-ingestor-archs" + +_CARVE = ( + ("asm_sdpa_engine", "ASM_SDPA_ENGINE"), + ("hip_mlops_engine", "HIP_MLOPS_ENGINE"), + ("hip_flash2_engine", "FLASH2"), +) +#: Path -> surface tag, first match wins. A tag in OUTSIDE_TAGS is outside the surface for +#: status; every other tag is a surface. Unmatched paths are `other` (outside). The ASM SDPA, +#: HIP MLOps and flash2 engines, their tests, configs and test-category lists are carved out; +#: test_sdk, frontend, data_sdk and other providers fall through to `other`. +SURFACE_TABLE = ( + *[("carve-out", HKP + f"src/*/{d}/*") for d, _ in _CARVE], + *[("carve-out", HKP + f"src/*/*/{d}/*") for d, _ in _CARVE], + *[("carve-out", HKP + f"config/*{k}*") for _, k in _CARVE], + *[("carve-out", HKP + f"*{k}*_test_categories*.yaml") for _, k in _CARVE], + ("test", SDK + "tests/*"), + ("test", HKP + "src/tests/*"), + ("test", HKP + "src/integration_tests/*"), + ("test", PACKER + "tests/*"), + ("test", PACKER + "examples/*"), + ("test", HKP + "rocke/*/tests/*"), + ("test", ENGINE + "test_descriptors/*"), + ("test", GEN + "tests/*"), + ("test", "dnn-providers/integration-tests/*"), + ("sdk-ingestor", SDK_INGESTOR + "*"), + ("generic-targets", GENERIC_TARGETS_JSON), + ("generic-targets", SDK + "include/hipdnn_plugin_sdk/GpuGenericTargets*"), + ("descriptor", DESC_DIR + "*"), + ("native-pack", ENGINE + "*"), + ("native-pack", HKP + "src/core/Container.cpp"), + ("kpack-runtime", HKP + "src/compilation/Kpack*"), + ("packer", PACKER + "*"), + ("rocke", HKP + "rocke/*"), + ("generator", GEN + "*"), + ("provider", HKP + "*"), +) +OUTSIDE_TAGS = {"carve-out", "test", "cmake", "other"} +#: Tags whose files are read as copies or readers although they are outside for status. +SEARCHED_TAGS = {"test"} + +GENERATED = ( + ( + "*/rocke/*/golden/*_sha256.json", + (HKP + "rocke/library/kernels/", HKP + "rocke/platform/"), + ), + ("*_generated.h", (GEN, "projects/hipdnn/flatbuffers_sdk/")), + ("*/hkp-packed.marker", (PACKER + "python/hkp_pack/",)), + ("*.provenance.json.gz", (PACKER + "python/hkp_pack/",)), + ("*.kpack", (PACKER + "python/hkp_pack/",)), +) +DOC_SUFFIXES = (".md", ".rst", ".txt") +C_SUFFIXES = ( + ".c", + ".cc", + ".cpp", + ".cxx", + ".h", + ".hh", + ".hpp", + ".hxx", + ".inl", + ".hip", + ".cu", + ".cuh", +) +COPY_SUFFIXES = (".py", ".json") + C_SUFFIXES +SEVERITY = {"outside": 0, "inert": 1, "changed": 2, "mismatch": 3} +#: Classes of change that a counterpart can follow (a comment or whitespace edit does not move it). +MOVING = {"code", "generated", "dvc-pointer", "binary"} +CONDITIONAL_NODES = ( + ast.If, + ast.IfExp, + ast.For, + ast.AsyncFor, + ast.While, + ast.Try, + ast.With, + ast.AsyncWith, + ast.Match, +) + ((ast.TryStar,) if hasattr(ast, "TryStar") else ()) +ZERO_SHA = "0" * 40 +GIT_CONFIG = ( + "-c", "core.quotepath=off", + "-c", "core.autocrlf=false", + "-c", "diff.noprefix=false", + "-c", "diff.mnemonicPrefix=false", + "-c", "grep.column=false", +) # fmt: skip + + +# -- git access ---------------------------------------------------------------- + + +def git(repo, *args, stdin=None, ok=(0,)) -> bytes: + """Run git on `repo` immune to user config; raise Fail on a status not in `ok`.""" + done = subprocess.run( + ["git", "-C", str(repo), *GIT_CONFIG, *args], + input=stdin, + capture_output=True, + check=False, + ) + if done.returncode not in ok: + err = done.stderr.decode("utf-8", errors="replace").strip() + raise rl.Fail(f"git {args[0]}: {err or 'exit ' + str(done.returncode)}") + return done.stdout + + +def resolve_revs(repo, revs) -> list: + """[(commit id, tree id)] of each rev, in one rev-parse.""" + args = [x for r in revs for x in (f"{r}^{{commit}}", f"{r}^{{tree}}")] + done = subprocess.run( + ["git", "-C", str(repo), "rev-parse", *args], capture_output=True, check=False + ) + lines = done.stdout.decode("utf-8", errors="replace").split() + if done.returncode != 0 or len(lines) != len(args): + raise rl.Fail(f"cannot resolve {', '.join(revs)} to a commit") + return list(zip(lines[0::2], lines[1::2])) + + +def decode(data): + if data is None or b"\x00" in data: + return None + try: + return data.decode("utf-8") + except UnicodeDecodeError: + return None + + +class Tree: + """One side of a view: a tree object. Blobs, listings, parses and greps are cached.""" + + def __init__(self, repo, oid): + self.repo, self.oid = repo, oid + self.blobs, self.asts, self.lists, self._under = {}, {}, {}, {} + self._paths = None + + @property + def paths(self) -> set: + if self._paths is None: + out = git( + self.repo, "ls-tree", "-r", "-z", "--name-only", self.oid, + "--", *SEARCH, GENERIC_TARGETS_JSON, + ) # fmt: skip + self._paths = { + p for p in out.decode("utf-8", errors="replace").split("\0") if p + } + return self._paths + + def under(self, prefix: str) -> list: + """Paths below `prefix`; a narrow ls-tree unless the full listing is already cached.""" + if self._paths is not None: + return [p for p in self._paths if p.startswith(prefix)] + if prefix not in self._under: + out = git( + self.repo, "ls-tree", "-r", "-z", "--name-only", self.oid, "--", prefix + ) + self._under[prefix] = [ + p for p in out.decode("utf-8", errors="replace").split("\0") if p + ] + return self._under[prefix] + + def read(self, paths) -> dict: + """path -> bytes, None when absent or not a blob; one cat-file for the unseen paths.""" + fetch([(self, p) for p in paths]) + return {p: self.blobs[p] for p in paths} + + def text(self, path): + return decode(self.read([path])[path]) + + def ast(self, path): + if path not in self.asts: + text = self.text(path) + try: + self.asts[path] = None if text is None else ast.parse(text) + except (SyntaxError, ValueError): + self.asts[path] = None + return self.asts[path] + + def grep(self, patterns, options=()) -> list: + return grep_trees([self], patterns, options)[self.oid] + + +def fetch(wanted) -> None: + """Read the unseen (tree, path) blobs into each tree's cache with one cat-file.""" + todo = [(t, p) for t, p in dict.fromkeys(wanted) if p not in t.blobs] + if not todo: + return + specs = [f"{t.oid}:{p}" for t, p in todo] + out = git( + todo[0][0].repo, "cat-file", "--batch", stdin=("\n".join(specs) + "\n").encode() + ) + pos = 0 + for tree, path in todo: + end = out.index(b"\n", pos) + header = out[pos:end].decode("utf-8", errors="replace") + pos = end + 1 + parts = header.rsplit(" ", 2) + if len(parts) != 3 or not parts[2].isdigit(): + tree.blobs[path] = None + continue + size = int(parts[2]) + tree.blobs[path] = out[pos : pos + size] if parts[1] == "blob" else None + pos += size + 1 + + +def grep_trees(trees, patterns, options=()) -> dict: + """Batched `git grep` over SEARCH in all `trees` at once: {oid: [(path, line, text)]}, or + [(path, 0, "")] entries with -l.""" + hits = {t.oid: [] for t in trees} + listing = "-l" in options + for i in range(0, len(patterns), 100): + args = ["grep", "-I", "--no-color", "--no-column", *options] + if not listing: + args.append("-n") + for pattern in patterns[i : i + 100]: + args += ["-e", pattern] + out = git(trees[0].repo, *args, *hits, "--", *SEARCH, ok=(0, 1)) + for raw in out.decode("utf-8", errors="replace").splitlines(): + oid, rest = raw.split(":", 1) + if listing: + hits[oid].append((rest, 0, "")) + else: + path, line, text = rest.split(":", 2) + hits[oid].append((path, int(line), text)) + return hits + + +@dataclass +class Change: + path: str + blob_base: str | None + blob_head: str | None + submodule: bool = False + #: (base_start, base_len, head_start, head_len), git -U0 semantics. + hunks: list = field(default_factory=list) + added: set = field(default_factory=set) + base: bytes | None = None + head: bytes | None = None + tag: str = "other" + cls: str = "" + funcs: list = field(default_factory=list) + + +_HUNK_RE = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@") + + +def parse_raw(out: str) -> list: + """Changes from the `--raw` lines (`:modeA modeB shaA shaB Spath`) of a diff.""" + changes = [] + for line in out.splitlines(): + if not line.startswith(":") or "\t" not in line: + continue + meta, path = line[1:].split("\t", 1) + mode_a, mode_b, sha_a, sha_b, _status = meta.split(" ") + changes.append( + Change( + path=path, + blob_base=None if sha_a == ZERO_SHA else sha_a, + blob_head=None if sha_b == ZERO_SHA else sha_b, + submodule="160000" in (mode_a, mode_b), + ) + ) + return changes + + +def parse_hunks(out: bytes) -> dict: + hunks, path, in_hunk = {}, None, False + for line in out.decode("utf-8", errors="replace").splitlines(): + if line.startswith("diff --git "): + path, in_hunk = None, False + elif not in_hunk and line.startswith(("--- a/", "+++ b/")): + path = line[6:].rstrip("\t") + elif line.startswith("@@") and path is not None: + match = _HUNK_RE.match(line) + if match: + a, b, c, d = match.groups() + hunks.setdefault(path, []).append( + ( + int(a), + 1 if b is None else int(b), + int(c), + 1 if d is None else int(d), + ) + ) + in_hunk = True + return hunks + + +def collect_changes(before: Tree, after: Tree) -> list: + """Changes before -> after with their hunks: one `git diff` and one `cat-file`.""" + out = git( + before.repo, "diff", "-U0", "--raw", "--patch", "--no-abbrev", "--no-renames", + "--src-prefix=a/", "--dst-prefix=b/", "--no-color", "--no-ext-diff", "--no-textconv", + before.oid, after.oid, + ) # fmt: skip + text = out.decode("utf-8", errors="replace") + changes = parse_raw(text) + hunks = parse_hunks(out) + for ch in changes: + ch.tag = tag_of(ch.path) + readable = [c.path for c in changes if not c.submodule and c.tag != "other"] + fetch([(t, p) for p in readable for t in (before, after)]) + for ch in changes: + ch.base, ch.head = before.blobs.get(ch.path), after.blobs.get(ch.path) + ch.hunks = hunks.get(ch.path, []) + ch.added = {h[2] + i for h in ch.hunks for i in range(h[3])} + ch.cls = "unread" if ch.submodule or ch.tag == "other" else classify(ch) + if ch.cls == "code" and ch.tag not in ("other", "carve-out"): + ch.funcs = touched_functions(ch, before, after) + return sorted(changes, key=lambda c: c.path) + + +def merged_tree(repo, tip: str, head: str): + """(tree oid, None) of the clean merge of tip and head, or (None, reason).""" + done = subprocess.run( + ["git", "-C", str(repo), *GIT_CONFIG, "merge-tree", "--write-tree", "--name-only", tip, head], + capture_output=True, + check=False, + ) # fmt: skip + lines = done.stdout.decode("utf-8", errors="replace").splitlines() + if done.returncode == 0 and lines: + return lines[0].strip(), None + if done.returncode == 1 and lines: + files = [] + for line in lines[1:]: + if not line.strip(): + break + files.append(line.strip()) + return None, "merge conflict in " + ( + ", ".join(files) if files else "unknown files" + ) + err = done.stderr.decode("utf-8", errors="replace").strip() + return None, f"git merge-tree failed: {err or 'exit ' + str(done.returncode)}" + + +# -- Surface tags and content classes ------------------------------------------------ + + +def is_cmake(path: str) -> bool: + return PurePosixPath(path).name == "CMakeLists.txt" or path.endswith(".cmake") + + +def is_doc(path: str) -> bool: + name = PurePosixPath(path).name + return (path.endswith(DOC_SUFFIXES) and not is_cmake(path)) or name == ".gitignore" + + +def tag_of(path: str) -> str: + if is_cmake(path) and ( + path.startswith(SEARCH) or path.startswith("projects/hipdnn/") + ): + return "cmake" + for tag, pattern in SURFACE_TABLE: + if fnmatch.fnmatchcase(path, pattern): + return tag + return "other" + + +# gfx950, gfx90a, gfx1151, gfx11-generic; in `Gfx950AttentionDense` the `A` starts a word. +_ARCH = re.compile(r"gfx(?:\d+-generic|\d+(?:[a-f](?![a-z]))?)", re.IGNORECASE) + + +def arch_of(*texts): + for text in texts: + match = _ARCH.search(text or "") + if match: + return match.group(0).lower() + return None + + +def generated_rule(path: str): + for pattern, sources in GENERATED: + if fnmatch.fnmatchcase(path, pattern): + return sources + return None + + +def mask_cpp(text: str, keep_strings: bool = False) -> str: + """Comments become spaces and literal contents `x`; length and newlines are preserved.""" + out, i, n = [], 0, len(text) + raw = re.compile(r'R"([^()\\ ]{0,16})\(') + while i < n: + ch = text[i] + match = ( + raw.match(text, i) + if ch == "R" and (i == 0 or not text[i - 1].isalnum()) + else None + ) + if match: + close = text.find(")" + match.group(1) + '"', match.end()) + end = n if close < 0 else close + len(match.group(1)) + 2 + seg = text[i:end] + out.append(seg if keep_strings else re.sub(r"[^\n]", "x", seg)) + i = end + elif ch in "\"'": + j = i + 1 + while j < n and text[j] != ch and text[j] != "\n": + j += 2 if text[j] == "\\" else 1 + seg = text[i : min(j + 1, n)] + if keep_strings or len(seg) < 2: + out.append(seg) + else: + out.append(seg[0] + re.sub(r"[^\n]", "x", seg[1:-1]) + seg[-1]) + i += len(seg) + elif text.startswith("//", i): + j = text.find("\n", i) + j = n if j < 0 else j + out.append(" " * (j - i)) + i = j + elif text.startswith("/*", i): + j = text.find("*/", i + 2) + j = n if j < 0 else j + 2 + out.append(re.sub(r"[^\n]", " ", text[i:j])) + i = j + else: + out.append(ch) + i += 1 + return "".join(out) + + +def strip_c_comments(text: str) -> str: + return mask_cpp(text, keep_strings=True) + + +def strip_hash_comments(text: str) -> str: + """Drop `#` comments outside double quotes, keeping the line structure.""" + lines = [] + for line in text.split("\n"): + quoted = False + for i, ch in enumerate(line): + if ch == '"' and (i == 0 or line[i - 1] != "\\"): + quoted = not quoted + elif ch == "#" and not quoted: + line = line[:i] + break + lines.append(line) + return "\n".join(lines) + + +def collapse_space(text: str, quotes: str) -> str: + """Whitespace runs outside string literals become one space, or one newline when the run + holds one. Literals stay verbatim and tokens never join (`"attn fwd"` != `"attnfwd"`). + """ + out, i, n = [], 0, len(text) + while i < n: + ch = text[i] + if ch in quotes: + j = i + 1 + while j < n and text[j] != ch and text[j] != "\n": + j += 2 if text[j] == "\\" else 1 + out.append(text[i : j + 1]) + i = j + 1 + elif ch.isspace(): + j = i + while j < n and text[j].isspace(): + j += 1 + out.append("\n" if "\n" in text[i:j] else " ") + i = j + else: + out.append(ch) + i += 1 + return "".join(out).strip() + + +def without_docstrings(tree): + for node in ast.walk(tree): + if isinstance( + node, (ast.Module, ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef) + ): + body = node.body + if body and is_docstring(body[0]): + node.body = body[1:] or [ast.Pass()] + return tree + + +def is_docstring(node) -> bool: + return ( + isinstance(node, ast.Expr) + and isinstance(node.value, ast.Constant) + and isinstance(node.value.value, str) + ) + + +def python_shape(text: str): + try: + return ast.dump(without_docstrings(ast.parse(text))) + except (SyntaxError, ValueError): + return None + + +def load_data(path: str, text: str): + """Canonical JSON text of the parsed JSON/YAML. `json.dumps` keeps `3` and `3.0` apart, + and `1` and `true`; None when the text does not parse.""" + try: + if path.endswith(".json"): + data = json.loads(text) + else: + data = require_yaml().safe_load(text) + return json.dumps(data, sort_keys=True, default=repr) + except rl.Fail: + raise + except Exception: # noqa: BLE001 - any parse failure means "not data-equal" + return None + + +def require_yaml(): + try: + import yaml # noqa: PLC0415 - optional until a YAML file needs parsing + except ImportError: + raise rl.Fail( + "PyYAML is required to read *.yaml (python -m pip install pyyaml)" + ) from None + return yaml + + +def parse_dvc_pointer(text: str) -> dict: + """Output path -> md5 of a `.dvc` pointer; the format is small enough to read directly.""" + outs, entry, in_outs = [], None, False + for line in text.splitlines(): + if not line.strip() or line.lstrip().startswith("#"): + continue + if not line[0].isspace() and not line.startswith("-"): + in_outs, entry = line.split(":", 1)[0].strip() == "outs", None + continue + if not in_outs: + continue + body = line.strip() + if body.startswith("- "): + entry = {} + outs.append(entry) + body = body[2:] + if entry is not None and ":" in body: + key, value = body.split(":", 1) + entry[key.strip()] = value.strip().strip("'\"") + return {o["path"]: o["md5"] for o in outs if o.get("md5") and o.get("path")} + + +def classify(ch: Change) -> str: + """Content class: code, comment-only, whitespace-only, data-equal, docs, generated, + dvc-pointer or binary. Decided by content, never by path (except docs and generated). + """ + path = ch.path + if generated_rule(path): + return "generated" + base = "" if ch.base is None else decode(ch.base) + head = "" if ch.head is None else decode(ch.head) + if base is None or head is None: + return "binary" + if path.endswith(".dvc"): + return ( + "data-equal" + if parse_dvc_pointer(base) == parse_dvc_pointer(head) + else "dvc-pointer" + ) + if is_doc(path): + return "docs" + if base.replace("\r\n", "\n") == head.replace("\r\n", "\n"): + return "whitespace-only" + if path.endswith(C_SUFFIXES): + if collapse_space(base, "\"'") == collapse_space(head, "\"'"): + return "whitespace-only" + if collapse_space(strip_c_comments(base), "\"'") == collapse_space( + strip_c_comments(head), "\"'" + ): + return "comment-only" + elif is_cmake(path): + if collapse_space(strip_hash_comments(base), '"') == collapse_space( + strip_hash_comments(head), '"' + ): + return "comment-only" + elif path.endswith(".py"): + shape = python_shape(base) + if shape is not None and shape == python_shape(head): + lines = lambda t: [ + ln.strip() for ln in t.splitlines() if ln.strip() + ] # noqa: E731 + return "whitespace-only" if lines(base) == lines(head) else "comment-only" + if ( + path.endswith((".json", ".yaml", ".yml")) + and ch.base is not None + and ch.head is not None + ): + data = load_data(path, base) + if data is not None and data == load_data(path, head): + return "data-equal" + return "code" + + +# -- Functions enclosing a hunk ---------------------------------------------------------- + + +def py_scopes(tree) -> list: + """[(qualname, start, end)] of every def and class.""" + found = [] + + def visit(node, qual): + for child in ast.iter_child_nodes(node): + if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + name = f"{qual}.{child.name}" if qual else child.name + found.append((name, child.lineno, child.end_lineno)) + visit(child, name) + else: + visit(child, qual) + + if tree is not None: + visit(tree, "") + return found + + +_CTRL = {"if", "for", "while", "switch", "catch", "else", "do", "try", "return"} + + +def cpp_functions(text: str) -> list: + """[(qualname, start, end)] of the function definitions of a C++ file, by brace scan: enough + to name `kernelMatches`, `scoreKernel` or `attentionDenseKernelSignature`. Bodies are skipped, + namespaces and classes qualify, initializer lists and lambdas at file scope are not functions. + """ + code = mask_cpp(text) + out, stack = [], [] # stack entries: (kind, name) + depth, stmt, line, stmt_line = 0, 0, 1, 1 + start_line = 1 + for i, ch in enumerate(code): + if ch == "\n": + line += 1 + if ch == "{": + if depth: + depth += 1 + continue + raw = code[stmt:i].split("\n") + first = next( + ( + k + for k, ln in enumerate(raw) + if ln.strip() and not ln.lstrip().startswith("#") + ), + 0, + ) + header = "\n".join( + ln for ln in raw if not ln.lstrip().startswith("#") + ).strip() + paren = header.find("(") + scope = re.search( + r"\b(namespace|class|struct|union|enum)\b\s*(?:class\b\s*)?(\w*)", + header, + ) + eq = header.find("=") + m = re.search(r"([~\w][\w:~]*)\s*\(", header) + if scope and (paren < 0 or scope.start() < paren): + stack.append(("scope", scope.group(2))) + elif m and not (0 <= eq < m.start()) and m.group(1) not in _CTRL: + name = m.group(1) + if re.fullmatch(r"[A-Z_][A-Z0-9_]*", name): + close = header.find(")", m.end()) + name = " ".join(header[m.start() : close + 1].split()) + scopes = [n for k, n in stack if k == "scope" and n] + depth = 1 + start_line = stmt_line + first + stack.append(("func", "::".join([*scopes, name]))) + else: + stack.append(("other", "")) + stmt, stmt_line = i + 1, line + elif ch == "}": + if depth > 1: + depth -= 1 + continue + if depth == 1: + depth = 0 + _, name = stack.pop() + out.append((name, start_line, line)) + elif stack: + stack.pop() + stmt, stmt_line = i + 1, line + elif ch == ";" and not depth: + stmt, stmt_line = i + 1, line + return out + + +def touched_functions(ch: Change, before: Tree, after: Tree) -> list: + """Names of the functions a change's hunks fall in, head side and base side.""" + if ch.path.endswith(".py"): + scopes = { + "base": py_scopes(before.ast(ch.path)), + "head": py_scopes(after.ast(ch.path)), + } + elif ch.path.endswith(C_SUFFIXES): + scopes = { + "base": cpp_functions(decode(ch.base) or ""), + "head": cpp_functions(decode(ch.head) or ""), + } + else: + return [] + names = set() + outside = False + for b_start, b_len, h_start, h_len in ch.hunks: + for side, (start, length) in ( + ("base", (b_start, b_len)), + ("head", (h_start, h_len)), + ): + lo, hi = start, start + max(length, 1) - 1 + if length == 0 and side == "head": + continue + inside = [(q, s, e) for q, s, e in scopes[side] if s <= hi and lo <= e] + if inside: + # innermost scopes only: the one with the latest start that contains each line + names.update( + q + for q, s, e in inside + if not any(q2 != q and q2.startswith(q + ".") for q2, *_ in inside) + ) + elif length: + outside = True + out = sorted(names) + if outside and not out: + out = [""] + return out + + +# -- Kernel ABI: parameter lists ----------------------------------------------------------- + + +@dataclass +class ParamList: + lineno: int + names: list = field(default_factory=list) + unconditional: list = field(default_factory=list) + conditional: list = field(default_factory=list) + undetermined: bool = False + #: Source text of everything after the name (type, `noalias=`, ...), parallel to `names`. + decls: list = field(default_factory=list) + + def marked(self) -> list: + return [n + "?" if c else n for n, c in zip(self.names, self.conditional)] + + +def param_lists(tree) -> dict: + """Function qualname -> its `b.param` names in source order, conditional ones marked.""" + result, calls = {}, {} + + def visit(node, qual, function, conditional): + for child in ast.iter_child_nodes(node): + if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)): + name = f"{qual}.{child.name}" if qual else child.name + result.setdefault(name, ParamList(child.lineno)) + visit(child, name, name, False) + elif isinstance(child, ast.ClassDef): + visit( + child, f"{qual}.{child.name}" if qual else child.name, None, False + ) + else: + if ( + function + and isinstance(child, ast.Call) + and isinstance(child.func, ast.Attribute) + and child.func.attr == "param" + ): + calls.setdefault(function, []).append((child, conditional)) + visit( + child, + qual, + function, + conditional or isinstance(child, CONDITIONAL_NODES), + ) + + visit(tree, "", None, False) + for function, found in calls.items(): + info = result[function] + for call, conditional in sorted( + found, key=lambda c: (c[0].lineno, c[0].col_offset) + ): + first = call.args[0] if call.args else None + if not (isinstance(first, ast.Constant) and isinstance(first.value, str)): + info.undetermined = True + continue + info.names.append(first.value) + info.conditional.append(conditional) + info.decls.append( + ", ".join( + [ast.unparse(a) for a in call.args[1:]] + + [ + ( + f"{k.arg}={ast.unparse(k.value)}" + if k.arg + else f"**{ast.unparse(k.value)}" + ) + for k in call.keywords + ] + ) + ) + if not conditional: + info.unconditional.append(first.value) + return {n: i for n, i in result.items() if i.names or i.undetermined} + + +def function_nodes(tree) -> dict: + """Qualname -> FunctionDef for every function, nested ones included.""" + found = {} + + def visit(node, qual): + for child in ast.iter_child_nodes(node): + if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + name = f"{qual}.{child.name}" if qual else child.name + if not isinstance(child, ast.ClassDef): + found[name] = child + visit(child, name) + + if tree is not None: + visit(tree, "") + return found + + +def string_lists(path: str, text: str, tree=None) -> list: + """Ordered string-literal lists of 2+ items: [(names, start, end, function)]. Python by AST; + other text by quoted runs inside `{...}` or `[...]`.""" + found = [] + if path.endswith(".py"): + functions = sorted( + ((f.lineno, f.end_lineno, q) for q, f in function_nodes(tree).items()), + reverse=True, + ) + for node in ast.walk(tree) if tree is not None else []: + if ( + isinstance(node, (ast.List, ast.Tuple)) + and len(node.elts) > 1 + and all( + isinstance(e, ast.Constant) and isinstance(e.value, str) + for e in node.elts + ) + ): + owner = next( + (q for s, e, q in functions if s <= node.lineno <= e), None + ) + found.append( + ([e.value for e in node.elts], node.lineno, node.end_lineno, owner) + ) + return found + for match in re.finditer( + r'[\[{]\s*("[^"\n]*"(?:\s*,\s*"[^"\n]*")+)\s*,?\s*[\]}]', strip_c_comments(text) + ): + start = text.count("\n", 0, match.start()) + 1 + names = re.findall(r'"([^"\n]*)"', match.group(1)) + found.append((names, start, text.count("\n", 0, match.end()) + 1, None)) + return found + + +# -- Kernel ABI: static C++ `KernelArgument` lists ---------------------------------------------- + +_KA_FUNC = re.compile( + r"vector\s*<\s*(?:[\w:]*::)?KernelArgument\s*>\s*(?:const\s*)?&?\s*(\w+)\s*" + r"\((?:[^()]|\([^()]*\))*\)\s*(?:const\s*)?(?:noexcept\s*)?\{" +) +_KA_LIT = re.compile(r"KernelArgument\s*\{") +_KA_DYNAMIC = re.compile(r"\b(?:emplace_back|push_back|for|while)\b|\[[&=]\]") +_STR = re.compile(r'"((?:[^"\\]|\\.)*)"') + + +def split_args(text: str) -> list: + out, cur, depth, quote = [], [], 0, None + for ch in text: + if quote: + cur.append(ch) + if ch == quote: + quote = None + elif ch in "\"'": + quote = ch + cur.append(ch) + elif ch in "({<[": + depth += 1 + cur.append(ch) + elif ch in ")}>]": + depth -= 1 + cur.append(ch) + elif ch == "," and depth == 0: + out.append("".join(cur).strip()) + cur = [] + else: + cur.append(ch) + if "".join(cur).strip(): + out.append("".join(cur).strip()) + return out + + +def match_brace(code: str, open_at: int) -> int: + depth = 0 + for i in range(open_at, len(code)): + if code[i] == "{": + depth += 1 + elif code[i] == "}": + depth -= 1 + if depth == 0: + return i + return len(code) - 1 + + +_SIZES = {"PTR": 8, "I32": 4, "U32": 4, "F32": 4, "I64": 8, "F64": 8} +_SIZEOF = {"void*": 8, "int32_t": 4, "uint32_t": 4, "float": 4, "int64_t": 8, + "uint64_t": 8, "double": 8, "int": 4} # fmt: skip + + +def cpp_arg_size(arg: str): + arg = arg.strip() + if arg in _SIZES: + return _SIZES[arg] + if arg.isdigit(): + return int(arg) + m = re.search(r"sizeof\s*\(\s*([\w:]+\s*\*?)\s*\)", arg) + return _SIZEOF.get(m.group(1).replace(" ", "")) if m else None + + +def cpp_kernel_lists(text: str) -> list: + """Every `std::vector`-returning function: one entry per static list (a + multi-launch pack holds several, each its own consumer), or one dynamic entry. + [{func, line, names, sizes, dynamic, unnamed}]; `names` is None unless every entry is a + `KernelArgument{kind, size, offset, "name"}` literal with a non-empty name.""" + code = mask_cpp(text) + out = [] + for fn in _KA_FUNC.finditer(code): + open_at = fn.end() - 1 + close = match_brace(code, open_at) + body = code[open_at:close] + line = text.count("\n", 0, fn.start()) + 1 + literals = {m.start(): m for m in _KA_LIT.finditer(code, open_at, close)} + if _KA_DYNAMIC.search(body) or not literals: + out.append(dict(func=fn.group(1), line=line, names=None, sizes=None, + dynamic=True, unnamed=False)) # fmt: skip + continue + groups, stack = {}, [] + for i in range(open_at, close): + if i in literals: + end = match_brace(code, literals[i].end() - 1) + args = split_args(text[literals[i].end() : end]) + groups.setdefault(stack[-1] if stack else open_at, []).append( + (args, text.count("\n", 0, i) + 1) + ) + if code[i] == "{": + stack.append(i) + elif code[i] == "}" and stack: + stack.pop() + for _, entries in sorted(groups.items()): + names, sizes = [], [] + for args, _ in entries: + name = _STR.fullmatch(args[3]) if len(args) >= 4 else None + names.append(name.group(1) if name else "") + kind = _STR.fullmatch(args[0]) if args else None + sizes.append( + "ptr" if kind and kind.group(1) == "global_buffer" + else cpp_arg_size(args[1]) if len(args) > 1 else None + ) # fmt: skip + unnamed = any(not n for n in names) + out.append(dict(func=fn.group(1), line=entries[0][1], names=None if unnamed else names, + sizes=sizes, dynamic=False, unnamed=unnamed)) # fmt: skip + return out + + +# -- Copies of a kernarg list --------------------------------------------------------------- + + +def named_lists(tree: Tree, path: str) -> list: + """Ordered name lists of a file: [(names, start, end, owner)].""" + if path in tree.lists: + return tree.lists[path] + text, found = tree.text(path), [] + if text is None: + pass + elif path.endswith(".py"): + found = string_lists(path, text, tree.ast(path)) + elif path.endswith(".json"): + try: + doc = json.loads(text) + except ValueError: + doc = None + for node in walk_json(doc): + sig = node.get("signature") + if isinstance(sig, list) and len(sig) > 1: + names = [a.get("name") if isinstance(a, dict) else a for a in sig] + if all(isinstance(n, str) for n in names): + line = text.count("\n", 0, max(text.find('"signature"'), 0)) + 1 + found.append((names, line, line, None)) + elif path.endswith(C_SUFFIXES): + found = [ + (e["names"], e["line"], e["line"], None) + for e in cpp_kernel_lists(text) + if e["names"] + ] + found += string_lists(path, text) + tree.lists[path] = found + return found + + +def walk_json(value): + if isinstance(value, dict): + yield value + for child in value.values(): + yield from walk_json(child) + elif isinstance(value, list): + for child in value: + yield from walk_json(child) + + +def compatible(cpp_names: list, pl: ParamList) -> bool: + """A static list matches a builder when it is an ordered subsequence of the builder's + params (conditional groups may be absent) holding every unconditional one.""" + it = iter(pl.names) + return all(n in it for n in cpp_names) and all( + n in cpp_names for n in pl.unconditional + ) + + +def related(cpp_names: list, names: list) -> bool: + shared = len(set(cpp_names) & set(names)) + return shared >= 2 and shared * 2 >= len(cpp_names) + + +def list_diff(old: list, new: list) -> str: + added = [n for n in new if n not in old] + removed = [n for n in old if n not in new] + parts = [] + if added: + parts.append("added " + ", ".join(added)) + if removed: + parts.append("removed " + ", ".join(removed)) + if not parts: + parts.append("reordered") + elif [n for n in old if n in new] != [n for n in new if n in old]: + parts.append("reordered") + return "; ".join(parts) + + +# -- Declared pairs (engine manifests) ---------------------------------------------------------- + + +@dataclass +class Surface: + manifest: str + name: str + module: str + function: str + cpp_mirror: str + test: str + arch: str | None + + +def load_surfaces(tree: Tree) -> list: + """`launch_surface` entries of every `*.engine.yaml` under the generator's configs.""" + paths = sorted( + p for p in tree.under(GEN + "configs/") if p.endswith(".engine.yaml") + ) + if not paths: + return [] + yaml = require_yaml() + out = [] + for path, data in tree.read(paths).items(): + text = decode(data) + try: + manifest = yaml.safe_load(text) if text else None + except Exception: # noqa: BLE001 + continue + if not isinstance(manifest, dict) or not isinstance( + manifest.get("launch_surface"), list + ): + continue + root = str(manifest.get("provider_root") or HKP).rstrip("/") + "/" + for entry in manifest["launch_surface"]: + source = entry.get("python_source") if isinstance(entry, dict) else None + if not isinstance(source, str) or ":" not in source: + continue + module, function = source.rsplit(":", 1) + out.append( + Surface( + path, + str(entry.get("name")), + root + "rocke/library/" + module.strip(), + function.strip(), + str(entry.get("cpp_mirror") or "").split(":", 1)[0].strip(), + str(entry.get("test") or "").split(":", 1)[0].strip(), + str(manifest.get("arch") or "") or None, + ) + ) + return out + + +# -- Consumed closure ---------------------------------------------------------------------------- + + +def module_name(path: str): + for root in IMPORT_ROOTS: + if path.startswith(root) and path.endswith(".py"): + parts = path[len(root) : -3].split("/") + if parts[-1] == "__init__": + parts = parts[:-1] + return ".".join(parts) + return None + + +def resolve_module(parts, known): + if not parts: + return None + for root in IMPORT_ROOTS: + stem = root + "/".join(parts) + for candidate in (stem + ".py", stem + "/__init__.py"): + if candidate in known: + return candidate + return None + + +def resolve_path_literal(value: str, known): + value = value.replace("\\", "/") + for root in IMPORT_ROOTS: + marker = root[len(HKP) :] + if marker in value: + candidate = root + value.split(marker, 1)[1] + if candidate in known: + return candidate + if root + value in known: + return root + value + return None + + +def eager_nodes(tree): + """Every node that runs when the module is imported: all but the bodies of functions.""" + stack = [tree] + while stack: + node = stack.pop() + yield node + if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)): + stack.extend(ast.iter_child_nodes(node)) + + +def import_targets(path: str, tree, known, eager: bool = False) -> list: + """[(target path, line)] for every rocKE module a file imports; `eager` keeps only the + imports that run at import time.""" + targets = [] + if tree is None: + return targets + own = module_name(path) + package = own.split(".") if own else [] + if own and not path.endswith("__init__.py"): + package = package[:-1] + for node in eager_nodes(tree) if eager else ast.walk(tree): + if isinstance(node, ast.Import): + for alias in node.names: + targets.append( + (resolve_module(alias.name.split("."), known), node.lineno) + ) + elif isinstance(node, ast.ImportFrom): + if node.level: + if not own: + continue + base = ( + package[: len(package) - (node.level - 1)] + if node.level > 1 + else list(package) + ) + base += node.module.split(".") if node.module else [] + else: + base = node.module.split(".") if node.module else [] + for alias in node.names: + target = ( + None + if alias.name == "*" + else resolve_module(base + [alias.name], known) + ) + targets.append((target or resolve_module(base, known), node.lineno)) + elif isinstance(node, ast.Call): + func = node.func + name = ( + func.attr + if isinstance(func, ast.Attribute) + else getattr(func, "id", "") + ) + if name in ("spec_from_file_location", "SourceFileLoader", "load_source"): + for arg in node.args: + if ( + isinstance(arg, ast.Constant) + and isinstance(arg.value, str) + and arg.value.endswith(".py") + ): + targets.append( + (resolve_path_literal(arg.value, known), node.lineno) + ) + return [(t, line) for t, line in targets if t] + + +def ancestor_inits(path: str, known) -> list: + """The `__init__.py` of every package above `path`: importing the module runs each.""" + for root in IMPORT_ROOTS: + if path.startswith(root): + parts = path[len(root) :].split("/")[:-1] + inits = [ + root + "/".join(parts[:d]) + "/__init__.py" + for d in range(1, len(parts) + 1) + ] + return [i for i in inits if i in known] + return [] + + +@dataclass +class Closure: + #: rocKE module -> the root that reaches it (a descriptor, packer or tool import, or an + #: eager `__init__` import chain; every ancestor `__init__.py` of a consumed module is in). + consumed: dict = field(default_factory=dict) + #: module -> archs of the production roots whose ordinary imports reach its body. + shipped: dict = field(default_factory=dict) + #: archs whose descriptors sit behind DVC: their kernel modules are roots by directory. + opaque: set = field(default_factory=set) + + +def build_closure(tree: Tree) -> Closure: + known = {p for p in tree.paths if p.endswith(".py") and p.startswith(IMPORT_ROOTS)} + out = Closure() + roots = [] # (label, target, arch, production) + descriptors = sorted( + p for p in tree.paths if p.startswith(DESC_DIR) and p.endswith(".json") + ) + for path, data in tree.read(descriptors).items(): + text = decode(data) + if not text or "kernel_source" not in text: + continue + try: + doc = json.loads(text) + except ValueError: + continue + seen = set() + for node in walk_json(doc): + source = node.get("kernel_source") + if isinstance(source, dict) and isinstance(source.get("source"), str): + target = resolve_path_literal(source["source"], known) + if target and source["source"] not in seen: + seen.add(source["source"]) + match = re.search( + r'"source"\s*:\s*"' + re.escape(source["source"]) + '"', text + ) + line = text.count("\n", 0, match.start()) + 1 if match else 0 + roots.append( + (f"{path}:{line}", target, arch_of(path, target), True) + ) + for path in sorted( + p for p in tree.paths if p.startswith(DESC_DIR) and p.endswith(".dvc") + ): + arch = arch_of(path) + if arch: + out.opaque.add(arch) + for mod in sorted(known): + if ( + mod.startswith(ROCKE_LIB + "kernels/") + and arch in mod.split("/") + and not mod.endswith("__init__.py") + ): + roots.append((path, mod, arch, True)) + packages = sorted( + { + p[len(r) :].split("/")[0].removesuffix(".py") + for r in IMPORT_ROOTS + for p in known + if p.startswith(r) + } + ) + mention = re.compile( + r"\b(?:" + + "|".join(map(re.escape, packages + ["spec_from_file_location"])) + + r")\b" + ) + importers = sorted( + p for p in tree.paths + if p.endswith(".py") and p.startswith(IMPORTER_DIRS) and p not in known and tag_of(p) != "test" + ) # fmt: skip + for path, data in tree.read(importers).items(): + text = decode(data) + if text and mention.search(text): + for target, line in import_targets(path, tree.ast(path), known): + roots.append( + (f"{path}:{line}", target, None, path.startswith(PRODUCTION_DIRS)) + ) + graph, body = {}, {} + frontier = sorted({t for _, t, _, _ in roots}) + while frontier: + tree.read(frontier) + for path in frontier: + is_init = path.endswith("__init__.py") + imports = import_targets(path, tree.ast(path), known, eager=is_init) + body[path] = [] if is_init else sorted({t for t, _ in imports}) + graph[path] = [(t, line, is_init) for t, line in imports] + [ + (a, 0, False) for a in ancestor_inits(path, known) + ] + frontier = sorted({t for p in frontier for t, _, _ in graph[p]} - graph.keys()) + for label, target, _arch, _prod in roots: + stack = [(target, label)] + while stack: + current, why = stack.pop() + if current in out.consumed: + continue + out.consumed[current] = why + for nxt, at, eager in graph.get(current, ()): + stack.append( + ( + nxt, + ( + f"{why}, through the eager import at {current}:{at}" + if eager + else why + ), + ) + ) + for _label, target, arch, production in roots: + if not production: + continue + seen, stack = set(), [target] + while stack: + current = stack.pop() + if current in seen: + continue + seen.add(current) + archs = out.shipped.setdefault(current, set()) + if arch: + archs.add(arch) + stack += body.get(current, []) + return out + + +# -- Rows ------------------------------------------------------------------------------------------ + + +def make_row( + kind, path, line, name, status, reason, counterparts=(), arch=None, foldable=False +): + return { + "kind": kind, + "path": path, + "line": line, + "name": name, + "status": status, + "reason": reason, + "counterparts": sorted( + counterparts, key=lambda c: (c["path"], c["line"], c["moved"]) + ), + "arch": arch or ALL_ARCHS, + "foldable": foldable, + "view": "", + "merged_only": False, + } + + +def cp(path, line, moved): + return {"path": path, "line": line, "moved": bool(moved)} + + +class Ctx: + """One view: before -> after, its changes and lazily built analyses.""" + + def __init__(self, before: Tree, after: Tree, label: str): + self.before, self.after, self.label = before, after, label + self.changes = {c.path: c for c in collect_changes(before, after)} + self._closures, self._surfaces = {}, {} + + def closure(self, side) -> Closure: + if side not in self._closures: + self._closures[side] = build_closure( + self.before if side == "before" else self.after + ) + return self._closures[side] + + def surfaces(self, side) -> list: + if side not in self._surfaces: + self._surfaces[side] = load_surfaces( + self.before if side == "before" else self.after + ) + return self._surfaces[side] + + def tree_of(self, side) -> Tree: + return self.before if side == "before" else self.after + + def ast(self, ch: Change, side): + return self.tree_of(side).ast(ch.path) + + def moved(self, path: str) -> bool: + ch = self.changes.get(path) + return ch is not None and ch.cls in MOVING + + +def func_note(funcs: list) -> str: + if not funcs: + return "" + shown = ", ".join(funcs[:6]) + ( + f", +{len(funcs) - 6} more" if len(funcs) > 6 else "" + ) + return f" in {shown}" + + +# -- files ------------------------------------------------------------------------------------------- + + +def surface_changes(base_tree, head_tree) -> list: + """Defs removed or re-signed and module-level statements changed; body-only edits are [].""" + + def surface(tree): + defs, statements = {}, set() + functions = (ast.FunctionDef, ast.AsyncFunctionDef) + sig = lambda n: repr((ast.dump(n.args), [ast.dump(d) for d in n.decorator_list], # noqa: E731 + ast.dump(n.returns) if n.returns else None)) # fmt: skip + for node in tree.body: + if isinstance(node, functions): + defs[f"def {node.name}"] = sig(node) + elif isinstance(node, ast.ClassDef): + members = [ + n + for n in node.body + if not isinstance(n, functions) and not is_docstring(n) + ] + defs[f"class {node.name}"] = repr(([ast.dump(b) for b in node.bases], + [ast.dump(d) for d in node.decorator_list], [ast.dump(m) for m in members])) # fmt: skip + for item in node.body: + if isinstance(item, functions): + defs[f"def {node.name}.{item.name}"] = sig(item) + elif not is_docstring(node): + statements.add(ast.dump(node)) + return defs, statements + + old_defs, old_stmts = surface(base_tree) + new_defs, new_stmts = surface(head_tree) + out = [f"{k} removed" if k not in new_defs else f"{k} changed" + for k, v in old_defs.items() if new_defs.get(k) != v] # fmt: skip + if old_stmts != new_stmts: + out.append("a module-level statement changed") + return out + + +def rocke_status(ctx: Ctx, ch: Change): + """(status, reason, arch, foldable) of a rocKE file whose code changed.""" + path = ch.path + if not path.endswith(".py") or not path.startswith(IMPORT_ROOTS): + return ( + "changed", + "rocKE file that is not an importable Python module: not understood", + arch_of(path), + False, + ) + side = ctx.closure("before" if ch.head is None else "after") + before = after = side + archs = set(side.shipped.get(path, ())) + if path in side.shipped: + arch = ", ".join(sorted(archs)) if archs else None + return ( + "changed", + "module body is reachable from a shipped variant; emitted code or params may differ" + + (f" ({', '.join(sorted(archs))})" if archs else ""), + arch or arch_of(path), + True, + ) + why = after.consumed.get(path) or before.consumed.get(path) + if why: + bt, at = ctx.ast(ch, "before"), ctx.ast(ch, "after") + if bt is None or at is None: + return ( + "changed", + f"imported module added, removed or unparsable ({why})", + arch_of(path), + True, + ) + diff = surface_changes(bt, at) + if diff: + return ( + "changed", + f"imported by {why}; module surface changed: {'; '.join(diff[:3])}", + arch_of(path), + True, + ) + return ( + "inert", + f"body-only edit; imported by {why} but no shipped body reaches it", + None, + False, + ) + return "inert", "reached by no shipped variant", None, False + + +def file_row(ctx: Ctx, ch: Change): + path, tag, cls = ch.path, ch.tag, ch.cls + kind = "file" + if tag in OUTSIDE_TAGS: + return make_row( + kind, + path, + 0, + path, + "outside", + f"{tag}: not on the runtime-integration surface", + ) + if cls == "docs": + return make_row(kind, path, 0, path, "outside", "documentation") + if cls in ("comment-only", "whitespace-only", "data-equal"): + return make_row(kind, path, 0, path, "inert", cls) + arch = arch_of(path) + if tag in ("descriptor", "generic-targets") or cls == "dvc-pointer": + return None # data rows carry these files + if cls == "generated": + sources = generated_rule(path) + other = next( + ( + p + for p in ctx.changes + if p != path and p.startswith(sources) and ctx.changes[p].cls in MOVING + ), + None, + ) + if other: + return make_row( + kind, + path, + 0, + path, + "inert", + f"generated artifact regenerated with its source ({other})", + ) + return make_row( + kind, + path, + 0, + path, + "changed", + "generated artifact changed without its source", + arch=arch, + ) + if cls == "binary": + return make_row( + kind, + path, + 0, + path, + "changed", + "binary or non-UTF-8 surface file: not understood", + arch=arch, + ) + line = ch.hunks[0][2] if ch.hunks else 0 + if tag == "rocke": + status, reason, arch, fold = rocke_status(ctx, ch) + return make_row( + kind, + path, + line, + path, + status, + reason + func_note(ch.funcs) if status != "inert" else reason, + arch=arch, + foldable=fold, + ) + arch = arch_of(path, " ".join(ch.funcs)) + reason = { + "native-pack": "native pack code changed", + "sdk-ingestor": "ingestor SDK header changed", + "kpack-runtime": "kpack runtime changed", + "packer": "packer code changed", + "generator": "ingestor generator changed", + }.get(tag, "provider code changed") + return make_row( + kind, path, line, path, "changed", reason + func_note(ch.funcs), arch=arch + ) + + +# -- kernargs --------------------------------------------------------------------------------------- + + +@dataclass +class Work: + change: Change + qual: str + old: ParamList + new: ParamList | None + line: int + + +def find_copies(ctx: Ctx, work: list) -> dict: + """(path, qual) -> counterparts holding the old kernarg list, searched in both trees by the + ordered-name sequence. A copy still holding the old list after is unmoved; one that held it + before and no longer does moved with the producer.""" + targets = {} + for w in work: + for names in (w.old.names, w.old.unconditional): + if len(names) > 1: + targets.setdefault(tuple(names), set()).add((w.change.path, w.qual)) + copies = {(w.change.path, w.qual): [] for w in work} + if not targets: + return copies + patterns = [f'"{n}"' for n in sorted({names[0] for names in targets})] + hits = grep_trees([ctx.before, ctx.after], patterns, ["-F", "-l"]) + candidates = { + p for found in hits.values() for p, _, _ in found if p.endswith(COPY_SUFFIXES) + } + fetch([(t, p) for p in sorted(candidates) for t in (ctx.before, ctx.after)]) + for path in sorted(candidates): + held = {} + for side, tree in (("before", ctx.before), ("after", ctx.after)): + for names, start, _end, owner in named_lists(tree, path): + held.setdefault(tuple(names), {"before": [], "after": []})[side].append( + (start, owner) + ) + for names, sides in held.items(): + for target in targets.get(names, ()): + after = [s for s, o in sides["after"] if (path, o) != target] + before = [s for s, o in sides["before"] if (path, o) != target] + entry = None + if after: + entry = cp(path, after[0], False) + elif before: + entry = cp(path, before[0], True) + if entry and entry not in copies[target]: + copies[target].append(entry) + return copies + + +def kernarg_rows(ctx: Ctx) -> list: + rows, work, seen_lists = [], [], set() + for ch in ctx.changes.values(): + if ch.tag != "rocke" or not ch.path.endswith(".py"): + continue + bt, at = ctx.ast(ch, "before"), ctx.ast(ch, "after") + if bt is None: + continue + old_lists = param_lists(bt) + new_lists = param_lists(at) if at is not None else {} + new_funcs = function_nodes(at) if at is not None else {} + old_funcs = function_nodes(bt) + arch = arch_of(ch.path) + for qual in sorted(old_lists): + old, new = old_lists[qual], new_lists.get(qual) + if new is None: + work.append(Work(ch, qual, old, None, old_funcs[qual].lineno)) + elif old.undetermined or new.undetermined: + if ast.dump(old_funcs[qual]) != ast.dump(new_funcs[qual]): + rows.append(make_row("kernargs", ch.path, new.lineno, qual, "changed", + f"{qual} passes a non-literal name to b.param; its kernarg list cannot be compared", + arch=arch, foldable=True)) # fmt: skip + elif old.names != new.names: + work.append(Work(ch, qual, old, new, new.lineno)) + elif old.decls != new.decls: + diff = [ + f"{n}({b or '-'} -> {a or '-'})" + for n, b, a in zip(new.names, old.decls, new.decls) + if b != a + ] + rows.append(make_row("kernargs", ch.path, new.lineno, qual, "changed", + f"b.param types of {qual} changed with the same names and order: {'; '.join(diff[:4])}" + "; a type or attribute change moves the kernarg layout", + arch=arch, foldable=True)) # fmt: skip + if work: + copies = find_copies(ctx, work) + for w in work: + found = copies[(w.change.path, w.qual)] + unmoved = [c for c in found if not c["moved"]] + if w.new is None: + change = "absent after (deleted or renamed)" + else: + change = f"{list_diff(w.old.names, w.new.names)}: {w.old.marked()} -> {w.new.marked()}" + seen_lists |= {(c["path"], c["line"]) for c in unmoved} + where = ( + f"{len(unmoved)} of {len(found)} copies unmoved" + if found + else "no copy found" + ) + rows.append(make_row("kernargs", w.change.path, w.line, w.qual, + "mismatch" if unmoved else "changed", + f"b.param list of {w.qual} changed ({change}); {where}", + found, arch=arch_of(w.change.path), foldable=not unmoved)) # fmt: skip + rows += declared_kernarg_rows(ctx, seen_lists) + return rows + + +def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: + """Compare each declared `kernargs` pair's static C++ lists with its builder, after.""" + rows = [] + for surface in ctx.surfaces("after"): + if surface.name != "kernargs" or not surface.cpp_mirror: + continue + producer, mirror = ctx.changes.get(surface.module), ctx.changes.get( + surface.cpp_mirror + ) + if producer is None and mirror is None: + continue + tree = ctx.after.ast(surface.module) + lists = param_lists(tree) if tree is not None else {} + pl = lists.get(surface.function) + text = ctx.after.text(surface.cpp_mirror) + if pl is None or text is None: + what = "builder" if pl is None else "C++ mirror" + rows.append(make_row("kernargs", surface.cpp_mirror, 0, f"declared:{surface.function}", + "mismatch", f"declared kernargs pair is broken at the after side: {what} of " + f"{surface.module}:{surface.function} is missing", arch=surface.arch)) # fmt: skip + continue + counter = [ + cp( + surface.module, + pl.lineno, + producer is not None and producer.cls in MOVING, + ) + ] + for entry in cpp_kernel_lists(text): + key = (surface.cpp_mirror, entry["line"]) + if key in reported: + continue + arch = surface.arch or arch_of(surface.cpp_mirror) + if entry["dynamic"] or entry["unnamed"]: + if mirror is not None and mirror.cls in MOVING: + kind = "dynamic" if entry["dynamic"] else "static but unnamed" + rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", + "changed", f"{entry['func']}() is a {kind} kernarg signature: it cannot be " + f"compared with {surface.function} statically", counter, arch=arch)) # fmt: skip + continue + if not related(entry["names"], pl.names) and not compatible( + entry["names"], pl + ): + continue + if compatible(entry["names"], pl): + if (producer is not None and producer.cls in MOVING) or ( + mirror is not None and mirror.cls in MOVING + ): + rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", + "changed", f"{entry['func']}() agrees with {surface.function}", counter, arch=arch)) # fmt: skip + else: + rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", + "mismatch", f"static list {entry['func']}() disagrees with builder {surface.function} " + f"({list_diff(entry['names'], pl.names)}): {entry['names']} vs {pl.marked()}", + counter, arch=arch)) # fmt: skip + return rows + + +# -- symbols ---------------------------------------------------------------------------------------- + +SYMBOL_TAGS = {"rocke", "packer"} +DEFINITION_FUNCS = (ast.FunctionDef, ast.AsyncFunctionDef) + + +def symbol_table(tree) -> dict: + """Qualname -> (kind, line, end, is_method, contract): top-level defs and classes, their + methods and class fields (annotated or assigned names).""" + table = {} + sig = lambda n: repr((ast.dump(n.args), [ast.dump(d) for d in n.decorator_list], # noqa: E731 + ast.dump(n.returns) if n.returns else None)) # fmt: skip + for node in getattr(tree, "body", []): + if isinstance(node, DEFINITION_FUNCS): + table[node.name] = ("def", node.lineno, node.end_lineno, False, sig(node)) + elif isinstance(node, ast.ClassDef): + table[node.name] = ("class", node.lineno, node.end_lineno, False, + repr(([ast.dump(b) for b in node.bases], [ast.dump(d) for d in node.decorator_list]))) # fmt: skip + for item in node.body: + if isinstance(item, DEFINITION_FUNCS): + table[f"{node.name}.{item.name}"] = ( + "def", + item.lineno, + item.end_lineno, + True, + sig(item), + ) + elif isinstance(item, ast.AnnAssign) and isinstance( + item.target, ast.Name + ): + table[f"{node.name}.{item.target.id}"] = ( + "field", + item.lineno, + item.end_lineno, + True, + ast.dump(item.annotation), + ) + elif isinstance(item, ast.Assign): + for t in item.targets: + if isinstance(t, ast.Name): + table[f"{node.name}.{t.id}"] = ( + "field", + item.lineno, + item.end_lineno, + True, + "assign", + ) + return { + q: e + for q, e in table.items() + if not (q.split(".")[-1].startswith("__") and q.endswith("__")) + } + + +def module_bindings(tree) -> set: + """Names a module still binds at top level: defs, classes, imports and assignments.""" + bound = set() + + def visit(statements): + for node in statements: + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + bound.add(node.name) + elif isinstance(node, (ast.Import, ast.ImportFrom)): + for alias in node.names: + bound.add(alias.asname or alias.name.split(".")[0]) + elif isinstance(node, (ast.Assign, ast.AnnAssign, ast.AugAssign)): + targets = ( + node.targets if isinstance(node, ast.Assign) else [node.target] + ) + for target in targets: + bound.update( + n.id for n in ast.walk(target) if isinstance(n, ast.Name) + ) + if not isinstance( + node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef) + ): + for attr in ("body", "orelse", "finalbody"): + visit(getattr(node, attr, [])) + for handler in getattr(node, "handlers", []): + visit(handler.body) + + visit(getattr(tree, "body", [])) + return bound + + +def iter_imports(path: str, tree): + """(bound name, module path stem, imported name or None for a module, line).""" + here = PurePosixPath(path).parent + for node in ast.walk(tree): + if isinstance(node, ast.ImportFrom): + if node.level: + base = here + for _ in range(node.level - 1): + base = base.parent + stem = "/" + str( + base / node.module.replace(".", "/") if node.module else base + ) + else: + stem = (node.module or "").replace(".", "/") + for alias in node.names: + yield alias.asname or alias.name, stem, alias.name, alias.lineno + elif isinstance(node, ast.Import): + for alias in node.names: + name = alias.asname or alias.name.split(".")[0] + yield name, (alias.name if alias.asname else name).replace( + ".", "/" + ), None, alias.lineno + + +def in_module(path: str, stem: str) -> bool: + files = (stem + ".py", stem + "/__init__.py") + if stem.startswith("/"): + return path in (f[1:] for f in files) + return any(path == f or path.endswith("/" + f) for f in files) + + +def symbol_references(tree, path: str, defining: str, name: str, is_method: bool): + """[(line, node)] of the AST reads of `name`, a symbol of `defining`. A method or field is + an attribute load; a function or class is an import of the name from the defining module, + each read of what that import binds, `m.name` for an imported module alias and, in the + defining file, a read of a name the file never binds. Keyword arguments, strings, docstrings + and same-named bindings from elsewhere are not reads. (node None: an import line.) + """ + if is_method: + return [ + (n.lineno, n) + for n in ast.walk(tree) + if isinstance(n, ast.Attribute) + and n.attr == name + and isinstance(n.ctx, ast.Load) + ] + imports = list(iter_imports(path, tree)) + from_defining = [ + (b, line) + for b, stem, imp, line in imports + if imp == name and in_module(defining, stem) + ] + aliases = {b for b, _ in from_defining} + modules = { + b + for b, stem, imp, _ in imports + if in_module(defining, stem if imp is None else f"{stem}/{imp}") + } + binds_name = any(b == name for b, *_ in imports) or any( + (isinstance(n, ast.Name) and n.id == name and not isinstance(n.ctx, ast.Load)) + or ( + isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)) + and n.name == name + ) + or (isinstance(n, ast.arg) and n.arg == name) + for n in ast.walk(tree) + ) + reads_own = path == defining and not binds_name + refs = [(line, None) for _, line in from_defining] + for node in ast.walk(tree): + if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load): + if node.id in aliases or (reads_own and node.id == name): + refs.append((node.lineno, node)) + elif ( + isinstance(node, ast.Attribute) + and isinstance(node.ctx, ast.Load) + and node.attr == name + and isinstance(node.value, ast.Name) + and node.value.id in modules + ): + refs.append((node.lineno, node)) + return refs + + +def required_missing(definition, call, skip_first=False) -> list: + """Parameters without a default that `call` does not supply.""" + args = definition.args + positional = [a.arg for a in args.posonlyargs + args.args] + required = positional[: len(positional) - len(args.defaults)] + if skip_first and positional: + positional, required = positional[1:], required[1:] + keywords = {k.arg for k in call.keywords} + supplied = set(positional[: len(call.args)]) | keywords + missing = [p for p in required if p not in supplied] + missing += [ + a.arg + for a, d in zip(args.kwonlyargs, args.kw_defaults) + if d is None and a.arg not in keywords + ] + return missing + + +def call_problem(definition, call, skip_first=False): + """Why `call` does not fit `definition`, or None. Starred arguments are not judged.""" + if any(isinstance(a, ast.Starred) for a in call.args) or any( + k.arg is None for k in call.keywords + ): + return None + args = definition.args + positional = [a.arg for a in args.posonlyargs + args.args] + if skip_first and positional: + positional = positional[1:] + if len(call.args) > len(positional) and not args.vararg: + return f"{len(call.args)} positional arguments for {len(positional)} parameters" + allowed = set(positional) | {a.arg for a in args.kwonlyargs} + unknown = [k.arg for k in call.keywords if k.arg not in allowed] + if unknown and not args.kwarg: + return f"unknown keyword(s) {', '.join(unknown)}" + missing = required_missing(definition, call, skip_first) + return f"omits required parameter(s) {', '.join(missing)}" if missing else None + + +_EXTERNAL_BASES = {"object", "ABC", "Generic", "Protocol", "NamedTuple", "Enum", "IntEnum", + "Exception", "TypedDict", "str", "int", "dict", "list"} # fmt: skip + + +def class_members(node) -> set: + """Names a class body defines: methods, properties, class attributes, annotated fields.""" + out = set() + for item in node.body: + if isinstance(item, DEFINITION_FUNCS): + out.add(item.name) + elif isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name): + out.add(item.target.id) + elif isinstance(item, ast.Assign): + out.update(t.id for t in item.targets if isinstance(t, ast.Name)) + return out + + +def resolve_class(tree: Tree, known: list, path: str, name: str, depth: int = 0): + """(module path, ClassDef) that `name` denotes in module `path`, following top-level aliases + (`A = B`) and imports into repo modules; None when it cannot be resolved.""" + module = tree.ast(path) + if module is None or depth > 8: + return None + for node in module.body: + if isinstance(node, ast.ClassDef) and node.name == name: + return path, node + if isinstance(node, ast.Assign) and any( + isinstance(t, ast.Name) and t.id == name for t in node.targets + ): + value = node.value + if isinstance(value, ast.Name): + return resolve_class(tree, known, path, value.id, depth + 1) + return None + for bound, stem, imported, _ in iter_imports(path, module): + if bound != name or imported is None: + continue + target = next((p for p in known if in_module(p, stem)), None) + return ( + resolve_class(tree, known, target, imported, depth + 1) if target else None + ) + return None + + +def class_provides(tree: Tree, known: list, path: str, cls: str, member: str): + """Whether a base of `cls` supplies `member` in `tree`: True, False, or the name of the + first base that cannot be resolved.""" + found = resolve_class(tree, known, path, cls) + if found is None: + return cls + unresolved = None + for base in found[1].bases: + while isinstance(base, ast.Subscript): + base = base.value + if isinstance(base, ast.Attribute): + name = base.attr + module = next( + ( + p + for p in known + if isinstance(base.value, ast.Name) and in_module(p, base.value.id) + ), + None, + ) + where = module or found[0] + elif isinstance(base, ast.Name): + name, where = base.id, found[0] + else: + unresolved = unresolved or ast.unparse(base) + continue + parent = resolve_class(tree, known, where, name) + if parent is None: + if name not in _EXTERNAL_BASES: + unresolved = unresolved or name + continue + if member in class_members(parent[1]): + return True + deeper = class_provides(tree, known, parent[0], parent[1].name, member) + if deeper is True: + return True + unresolved = unresolved or (deeper or None) + return unresolved or False + + +def symbol_rows(ctx: Ctx) -> list: + cands, direct = [], [] # cands: (change, qual, entry, kind, after entry) + for ch in ctx.changes.values(): + if not ch.path.endswith(".py") or ch.tag not in SYMBOL_TAGS: + continue + bt, at = ctx.ast(ch, "before"), ctx.ast(ch, "after") + if bt is None: + continue + old = symbol_table(bt) + new = symbol_table(at) if at is not None else {} + bound = module_bindings(at) if at is not None else set() + for qual, entry in sorted(old.items()): + name = qual.split(".")[-1] + if qual not in new: + if not entry[3] and name in bound: + continue # still bound by its module: re-exported, not removed + cls = qual.split(".")[0] + if entry[3] and cls in old and cls in new: + # a method or field dropped from a class that still inherits the name is not removed + known = [p for p in ctx.after.under(HKP) if p.endswith(".py")] + provided = class_provides(ctx.after, known, ch.path, cls, name) + if provided is True: + continue + if provided: + direct.append(make_row("symbols", ch.path, entry[1], qual, "changed", + f"override removed; base {provided} not resolved", arch=arch_of(ch.path))) # fmt: skip + continue + cands.append((ch, qual, entry, "removed", None)) + elif new[qual][4] != entry[4] and entry[0] in ("def", "field"): + cands.append((ch, qual, entry, "resigned", new[qual])) + if at is None and not old: + cands.append((ch, "", ("module", 0, 0, False, ""), "removed", None)) + rows = direct + if cands: + rows += reader_rows(ctx, cands) + rows += call_arity_rows(ctx) + return rows + + +def reader_rows(ctx: Ctx, cands: list) -> list: + names = sorted({q.split(".")[-1] for _, q, e, *_ in cands if e[0] != "module"}) + stems = sorted( + { + PurePosixPath(c[0].path).stem + for c in cands + if c[2][0] == "module" or c[0].head is None + } + ) + words = sorted(set(names) | set(stems) - {"__init__"}) + mentioning = ( + sorted( + { + p + for p, _, _ in ctx.after.grep(words, ["-w", "-F", "-l"]) + if p.endswith(".py") + } + ) + if words + else [] + ) + ctx.after.read(mentioning) + rows = [] + for ch, qual, entry, kind, new_entry in cands: + name, is_method = qual.split(".")[-1], entry[3] + own_hunks = [ + (h, h + max(hl, 1) - 1) + for b, bl, h, hl in ch.hunks + if b <= entry[2] and entry[1] <= b + max(bl, 1) - 1 + ] + moved_elsewhere = False + refs, calls = [], [] + for path in mentioning: + tree = ctx.after.ast(path) + text = ctx.after.text(path) or "" + if tree is None: + continue + if entry[0] == "module": + found = [ + (line, None) + for b, stem, imp, line in iter_imports(path, tree) + if in_module(ch.path, stem) + or in_module(ch.path, stem if imp is None else f"{stem}/{imp}") + ] + else: + if is_method and not re.search( + r"\b" + re.escape(qual.split(".")[0]) + r"\b", text + ): + continue + if ( + path != ch.path + and not is_method + and kind == "removed" + and any( + isinstance( + n, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef) + ) + and n.name == name + for n in tree.body + ) + ): + moved_elsewhere = True + break + found = symbol_references(tree, path, ch.path, name, is_method) + other = ctx.changes.get(path) + for line, node in found: + if path == ch.path and any(a <= line <= b for a, b in own_hunks): + continue + refs.append(cp(path, line, other is not None and line in other.added)) + if node is not None: + calls.append((path, tree, node)) + if moved_elsewhere or not refs: + continue + arch = arch_of(ch.path) + if kind == "removed": + what = "module" if entry[0] == "module" else qual + rows.append(make_row("symbols", ch.path, entry[1], qual, "mismatch", + f"{what} is removed but still referenced after", refs, arch=arch)) # fmt: skip + continue + problems = [] + defn = ( + next( + ( + n + for n in ast.walk(ctx.after.ast(ch.path)) + if isinstance(n, DEFINITION_FUNCS) and n.lineno == new_entry[1] + ), + None, + ) + if entry[0] == "def" + else None + ) + if defn is not None and not defn.decorator_list: + for path, tree, node in calls: + for call in ast.walk(tree): + if isinstance(call, ast.Call) and call.func is node: + why = call_problem(defn, call, skip_first=is_method) + if why: + problems.append((path, call.lineno, why)) + if problems: + rows.append(make_row("symbols", ch.path, new_entry[1], qual, "mismatch", + f"{qual} was re-signed and a reader no longer fits: {problems[0][2]} at {problems[0][0]}:{problems[0][1]}", + refs, arch=arch)) # fmt: skip + else: + rows.append(make_row("symbols", ch.path, new_entry[1], qual, "changed", + f"{qual} was re-signed; {len(refs)} reader(s) read it", refs, arch=arch)) # fmt: skip + return rows + + +def call_arity_rows(ctx: Ctx) -> list: + """Added calls to a module-level function with exactly one def, omitting a required parameter.""" + calls = [] # (change, call node, def name, module stem or None for "this file") + for ch in ctx.changes.values(): + if not ch.path.endswith(".py") or not ch.added or ch.tag == "other": + continue + tree = ctx.after.ast(ch.path) + if tree is None: + continue + bound = {n: (s, i) for n, s, i, _ in iter_imports(ch.path, tree)} + for node in ast.walk(tree): + if not isinstance(node, ast.Call) or node.lineno not in ch.added: + continue + if any(isinstance(a, ast.Starred) for a in node.args) or any( + k.arg is None for k in node.keywords + ): + continue + func = node.func + if isinstance(func, ast.Name): + stem, name = bound.get(func.id, (None, func.id)) + if name is not None: + calls.append((ch, node, name, stem)) + elif ( + isinstance(func, ast.Attribute) + and isinstance(func.value, ast.Name) + and func.value.id in bound + ): + stem, name = bound[func.value.id] + calls.append( + (ch, node, func.attr, stem if name is None else f"{stem}/{name}") + ) + if not calls: + return [] + names = sorted({c[2] for c in calls}) + patterns = [ + rf"^[[:space:]]*(async[[:space:]]+)?def[[:space:]]+({'|'.join(names[i : i + 100])})[[:space:]]*\(" + for i in range(0, len(names), 100) + ] + definitions = {} + for path, line, text in ctx.after.grep(patterns, ["-E"]): + if path.endswith(".py"): + definitions.setdefault(re.search(r"def\s+(\w+)", text).group(1), []).append( + (path, line) + ) + unique = {n: d[0] for n, d in definitions.items() if len(d) == 1} + ctx.after.read(sorted({p for p, _ in unique.values()})) + rows = [] + for ch, call, name, stem in calls: + if name not in unique: + continue + path, line = unique[name] + if (path != ch.path) if stem is None else not in_module(path, stem): + continue + tree = ctx.after.ast(path) + defn = next( + ( + n + for n in getattr(tree, "body", []) + if isinstance(n, DEFINITION_FUNCS) and n.lineno == line + ), + None, + ) + if defn is None or defn.decorator_list: + continue + missing = required_missing(defn, call) + if missing: + other = ctx.changes.get(path) + rows.append(make_row("symbols", ch.path, call.lineno, f"call:{name}", "mismatch", + f"call {name}(...) omits required parameter(s) {', '.join(missing)}; sole def at {path}:{line}", + [cp(path, line, other is not None and line in other.added)], + arch=arch_of(ch.path, path))) # fmt: skip + return rows + + +# -- surfaces --------------------------------------------------------------------------------------- + + +def surfaces_rows(ctx: Ctx) -> list: + rows, seen = [], set() + for surface in ctx.surfaces("before") + ctx.surfaces("after"): + key = ( + surface.manifest, + surface.name, + surface.module, + surface.function, + surface.cpp_mirror, + ) + if key in seen: + continue + seen.add(key) + py_changed = False + line = 0 + for side in ("before", "after"): + tree = ctx.tree_of(side).ast(surface.module) + node = ( + function_nodes(tree).get(surface.function) if tree is not None else None + ) + if node is not None and side == "after": + line = node.lineno + dump = None if node is None else ast.dump(without_docstrings(node)) + if side == "before": + before_dump = dump + py_changed = before_dump != dump + cpp_changed = ctx.moved(surface.cpp_mirror) if surface.cpp_mirror else False + test_changed = ctx.moved(surface.test) if surface.test else False + manifest_changed = ctx.moved(surface.manifest) + counter = [cp(surface.cpp_mirror, 0, cpp_changed)] if surface.cpp_mirror else [] + if surface.test: + counter.append(cp(surface.test, 0, test_changed)) + arch = surface.arch or arch_of(surface.module, surface.cpp_mirror) + name = f"{surface.name}:{surface.function}" + broken = [ + what + for what, path in ( + ("python module", surface.module), + ("C++ mirror", surface.cpp_mirror), + ) + if path and ctx.after.read([path])[path] is None + ] + if not broken and dump is None and (py_changed or manifest_changed): + broken = [f"function {surface.function}"] + if broken: + rows.append(make_row("surfaces", surface.manifest, 0, name, "mismatch", + f"declared launch_surface {surface.name} points at a missing {', '.join(broken)} after the change", + counter, arch=arch)) # fmt: skip + elif py_changed or cpp_changed or manifest_changed: + side = ( + ("python side" if py_changed else "") + + (" and " if py_changed and cpp_changed else "") + + ("C++ mirror" if cpp_changed else "") + ) + if manifest_changed and not (py_changed or cpp_changed): + side = "manifest entry" + other = ( + "the counterpart did not change in code" + if not (py_changed and cpp_changed) + else "both sides changed" + ) + rows.append(make_row("surfaces", surface.module if py_changed else surface.cpp_mirror or surface.manifest, + line if py_changed else 0, name, "changed", + f"launch_surface {surface.name}: {side} changed; {other}", counter, arch=arch)) # fmt: skip + return rows + + +# -- data ------------------------------------------------------------------------------------------- + + +def descriptor_delta(old: str, new: str) -> str: + """What changed between two versions of a descriptor, with JSON types preserved.""" + + def parse(text): + try: + doc = json.loads(text) if text else {} + except ValueError: + return None + return doc if isinstance(doc, dict) else None + + before, after = parse(old), parse(new) + if before is None or after is None: + return "content is not comparable JSON" + + def entries(doc): + items = doc.get("kernelDescriptors") + return { + str(i.get("name", i.get("id", n))): json.dumps(i, sort_keys=True) + for n, i in enumerate(items if isinstance(items, list) else []) + if isinstance(i, dict) + } + + old_e, new_e = entries(before), entries(after) + changed = sum(1 for k in old_e.keys() & new_e.keys() if old_e[k] != new_e[k]) + other = sorted( + k + for k in (before.keys() | after.keys()) - {"kernelDescriptors"} + if json.dumps(before.get(k), sort_keys=True) + != json.dumps(after.get(k), sort_keys=True) + ) + return ( + f"kernel descriptors {len(old_e)} -> {len(new_e)} " + f"(+{len(new_e.keys() - old_e.keys())}, -{len(old_e.keys() - new_e.keys())}, {changed} changed)" + + (f"; other keys changed: {', '.join(other)}" if other else "") + ) + + +def generic_members(text): + """{(generic, member)} of gpu_generic_targets.json, or None when the shape is unknown.""" + try: + generics = json.loads(text).get("generics") + except (ValueError, AttributeError): + return None + if not isinstance(generics, dict): + return None + out = set() + for generic, members in generics.items(): + if not isinstance(members, list): + return None + out |= {(generic, str(m)) for m in members} + return out + + +def data_rows(ctx: Ctx) -> list: + rows = [] + for ch in ctx.changes.values(): + path = ch.path + if ch.tag == "generic-targets" and path == GENERIC_TARGETS_JSON: + old, new = generic_members(decode(ch.base) or ""), generic_members( + decode(ch.head) or "" + ) + if ch.cls in ("data-equal", "whitespace-only"): + rows.append(make_row("data", path, 0, path, "inert", ch.cls)) + elif old is None or new is None: + rows.append( + make_row( + "data", + path, + 0, + path, + "changed", + "generic-target table not readable as generics -> members", + ) + ) + else: + for generic, member in sorted(old - new): + rows.append(make_row("data", path, 0, f"{generic}:{member}", "changed", + f"generic target {generic}: member {member} removed", arch=arch_of(member))) # fmt: skip + for generic, member in sorted(new - old): + rows.append(make_row("data", path, 0, f"{generic}:{member}", "changed", + f"generic target {generic}: member {member} added", arch=arch_of(member))) # fmt: skip + if old == new: + before, after = json.loads(decode(ch.base)), json.loads( + decode(ch.head) + ) + keys = sorted(k for k in before.keys() | after.keys() + if k not in ("$comment", "source", "generics") + and json.dumps(before.get(k), sort_keys=True) != json.dumps(after.get(k), sort_keys=True)) # fmt: skip + rows.append(make_row("data", path, 0, path, "changed" if keys else "inert", + f"membership unchanged; keys changed: {', '.join(keys)}" if keys else "membership unchanged; comment or source keys only")) # fmt: skip + elif ch.cls == "dvc-pointer": + old = parse_dvc_pointer(decode(ch.base) or "") + new = parse_dvc_pointer(decode(ch.head) or "") + shown = ", ".join( + f"{p}: {old.get(p, 'none')[:8]} -> {new.get(p, 'none')[:8]}" + for p in sorted(old.keys() | new.keys()) + if old.get(p) != new.get(p) + ) + rows.append(make_row("data", path, 0, path, "changed", + f"catalog content changed, not inspected ({shown})", arch=arch_of(path), foldable="dvc")) # fmt: skip + elif ( + ch.tag == "descriptor" + and ch.cls not in ("docs",) + and path.endswith(DESCRIPTOR_SUFFIXES) + ): + if ch.cls in ("data-equal", "whitespace-only"): + rows.append(make_row("data", path, 0, path, "inert", ch.cls)) + else: + rows.append(make_row("data", path, 0, path, "changed", + "descriptor content changed: " + descriptor_delta(decode(ch.base) or "", decode(ch.head) or ""), + arch=arch_of(path))) # fmt: skip + elif ch.tag == "descriptor" and ch.cls not in ("docs",): + rows.append( + make_row( + "data", + path, + 0, + path, + "changed", + "descriptor-tree file changed: not understood", + arch=arch_of(path), + ) + ) + return rows + + +# -- one view ----------------------------------------------------------------------------------------- + + +def analyse_view(before: Tree, after: Tree, label: str): + ctx = Ctx(before, after, label) + rows = [] + for ch in ctx.changes.values(): + row = file_row(ctx, ch) + if row: + rows.append(row) + for fn in (kernarg_rows, symbol_rows, surfaces_rows, data_rows): + rows += fn(ctx) + for row in rows: + row["view"] = label + return ctx, rows + + +def merge_views(per_view: list) -> list: + """Rows of the first view, plus the rows of later views that are new or stronger there.""" + rows = list(per_view[0]) + keyed = {(r["kind"], r["path"], r["name"]): r for r in rows} + for later in per_view[1:]: + for row in later: + prior = keyed.get((row["kind"], row["path"], row["name"])) + if prior is None or SEVERITY[row["status"]] > SEVERITY[prior["status"]]: + row["merged_only"] = True + rows.append(row) + return rows + + +# -- emit fold --------------------------------------------------------------------------------------- + +EMIT_SIZES = {"i8": 1, "i16": 2, "i32": 4, "float": 4, "half": 2, "i64": 8, "double": 8} + + +def load_emit(path) -> dict: + try: + data = json.loads(Path(path).read_text(encoding="utf-8")) + except (OSError, ValueError) as exc: + raise rl.Fail(f"cannot read emit file {path}: {exc}") from None + if ( + not isinstance(data, dict) + or data.get("schema") != 1 + or data.get("tool") != "impact_emit" + ): + raise rl.Fail(f"{path} is not impact_emit schema 1 output") + return data + + +def emit_agrees(params, entry): + """None when the head params and a static C++ list agree, else why they do not.""" + names = [p[0] for p in params] + if names != entry["names"]: + return f"names {names} vs {entry['names']} ({list_diff(entry['names'], names)})" + for (name, typ), size in zip(params, entry["sizes"]): + want = "ptr" if typ.startswith("ptr") else EMIT_SIZES.get(typ) + if size is not None and want is not None and size != want: + return f"{name}: IR type {typ} vs C++ {size if size == 'ptr' else f'{size}-byte'}" + return None + + +def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: + """Apply impact_emit output to the base..head view's rows (see the module docstring).""" + families = emit.get("families") or [] + out = [] + surfaces = ctx.surfaces("after") + equal = bool(families) and not emit.get("not_inspected") + for fam in families: + arch, builder = fam.get("arch"), fam.get("builder") + quiet = not any( + fam.get(k) + for k in ("params_changed", "ir_changed", "only_in_base", "only_in_head") + ) + failures = fam.get("failures") or {} + if not quiet or any(failures.get(side) for side in ("base", "head")): + equal = False + params = (fam.get("params") or {}).get("head") + source = fam.get("source", "") + if fam.get("params_changed") and params: + entries = [] + for s in surfaces: + if ( + s.name == "kernargs" + and s.module == source + and s.function == builder + and s.cpp_mirror + ): + entries += [ + (s.cpp_mirror, e) + for e in cpp_kernel_lists(ctx.after.text(s.cpp_mirror) or "") + ] + if not entries: + for p in sorted(ctx.after.paths): + if ( + p.startswith(ENGINE + "packs/") + and p.endswith(C_SUFFIXES) + and arch + and arch in p.lower() + ): + entries += [ + (p, e) for e in cpp_kernel_lists(ctx.after.text(p) or "") + ] + names = [p[0] for p in params] + entries = [ + (p, e) for p, e in entries if e["names"] and related(e["names"], names) + ] + bad = [(p, e, emit_agrees(params, e)) for p, e in entries] + bad = [b for b in bad if b[2]] + counter = [cp(source, 0, True)] + if bad: + p, e, why = bad[0] + out.append(make_row("kernargs", p, e["line"], f"emit:{arch}:{builder}", "mismatch", + f"emitted params of {builder} ({arch}) disagree with static list {e['func']}(): {why}", + counter, arch=arch)) # fmt: skip + else: + note = ( + f"consistent with {len(entries)} static C++ list(s)" + if entries + else "no static C++ list to compare" + ) + out.append(make_row("kernargs", source, 0, f"emit:{arch}:{builder}", "changed", + f"emitted params of {builder} changed for {len(fam['params_changed'])} variant(s); {note}", + counter, arch=arch)) # fmt: skip + elif fam.get("params_changed"): + out.append(make_row("kernargs", source, 0, f"emit:{arch}:{builder}", "changed", + f"emitted params of {builder} changed for {len(fam['params_changed'])} variant(s); no param list recorded", arch=arch)) # fmt: skip + if fam.get("ir_changed"): + out.append(make_row("kernargs", source, 0, f"emit-ir:{arch}:{builder}", "changed", + f"emitted IR of {builder} changed for {len(fam['ir_changed'])} variant(s)", arch=arch)) # fmt: skip + failed = {side: v for side, v in failures.items() if v} + if failed: + out.append(make_row("kernargs", source, 0, f"emit-fail:{arch}:{builder}", "changed", + "variants failed to build: " + "; ".join(f"{s}: {len(v)}" for s, v in sorted(failed.items())), arch=arch)) # fmt: skip + for item in emit.get("not_inspected") or []: + out.append(make_row("kernargs", item.get("kdp", ""), 0, f"emit-skip:{item.get('kdp')}:{item.get('side')}", + "changed", f"not inspected ({item.get('side')}): {item.get('reason')}")) # fmt: skip + if equal: + dvc_dirs = [ + str(PurePosixPath(f["kdp"]).parent) + "/" + for f in families + if f.get("spec_origin") == "registry_enumerator" and f.get("kdp") + ] + for row in rows: + if ( + row["view"] != ctx.label + or row["status"] != "changed" + or not row["foldable"] + ): + continue + if row["foldable"] == "dvc": + pointer_dir = str(PurePosixPath(row["path"]).parent) + "/" + if not any( + d.startswith(pointer_dir) or pointer_dir.startswith(d) + for d in dvc_dirs + ): + continue + row.update( + status="inert", + reason="catalog regenerated: every shipped variant equal", + ) + else: + row.update( + status="inert", + reason="emitted IR and params equal for every shipped variant", + ) + for row in out: + row["view"] = ctx.label + return rows + out + + +# -- rollup and output ----------------------------------------------------------------------------- + + +def rollup(rows: list) -> str: + live = [r for r in rows if r["status"] != "outside"] + if any(r["status"] == "mismatch" for r in live): + return "Runtime impact: BREAKS" + changed = [r for r in live if r["status"] == "changed"] + if changed: + archs = sorted({a.strip() for r in changed for a in r["arch"].split(",")}) + return "Runtime impact: NEEDS-DEVICE " + ", ".join(archs) + if live: + return ( + f"Runtime impact: NONE ({len({r['path'] for r in live})} files inspected)" + ) + return "Runtime impact: N/A (no surface file)" + + +def anchor(row) -> str: + return f"{row['path']}:{row['line']}" if row["line"] else row["path"] + + +def render_rows(rows: list) -> str: + def esc(text): + return str(text).replace("|", "\\|").replace("\n", " ") + + lines = [ + "| Anchor | View | Status | Reason | Counterparts |", + "|---|---|---|---|---|", + ] + for r in sorted( + rows, key=lambda r: (-SEVERITY[r["status"]], r["path"], r["line"], r["name"]) + ): + view = r["view"] + (" (merged-only)" if r["merged_only"] else "") + counter = "; ".join( + f"{anchor(c)} ({'moved' if c['moved'] else 'unmoved'})" + for c in r["counterparts"][:6] + ) + ( + f"; +{len(r['counterparts']) - 6} more" + if len(r["counterparts"]) > 6 + else "" + ) + lines.append( + f"| {esc(anchor(r))} | {view} | {r['status']} | {esc(r['reason'])} | {esc(counter)} |" + ) + return "\n".join(lines) + + +def render_files(ctx_changes: list, rows: list) -> str: + worst = {} + for r in rows: + if SEVERITY[r["status"]] >= SEVERITY[worst.get(r["path"], "outside")]: + worst[r["path"]] = r["status"] + lines = ["| Path | Surface | Class | Status | Functions |", "|---|---|---|---|---|"] + for ch in ctx_changes: + lines.append( + f"| {ch.path} | {ch.tag} | {ch.cls} | {worst.get(ch.path, 'outside')} | {', '.join(ch.funcs[:6])} |" + ) + return "\n".join(lines) + + +# -- command line -------------------------------------------------------------------------------------- + +KINDS = { + "files": ("file",), + "kernargs": ("kernargs",), + "symbols": ("symbols",), + "surfaces": ("surfaces",), + "data": ("data",), +} + + +def resolve(args): + """(repo, (base commit, tree), (head commit, tree), (tip commit, tree) or None).""" + if args.round_dir: + rd = args.round_dir + repo = rl.pin_value(rd, "CLONE") + base, head = rl.pin_value(rd, "BASE"), rl.pin_value(rd, "HEAD") + if not (repo and base and head): + raise rl.Fail(f"PIN.md in {rd} lacks CLONE, BASE or HEAD") + tip = args.target_tip or rl.pin_value(rd, "TARGET_TIP") + else: + if not (args.repo and args.base and args.head): + raise rl.Fail("give --repo, --base and --head, or --round-dir") + repo, base, head, tip = args.repo, args.base, args.head, args.target_tip + found = resolve_revs(repo, [base, head] + ([tip] if tip else [])) + tip_pair = found[2] if tip and found[2][0] != found[0][0] else None + return repo, found[0], found[1], tip_pair + + +def run(args): + repo, (base, base_tree_id), (head, head_tree_id), tip_pair = resolve(args) + if base_tree_id == head_tree_id: + raise rl.Fail("empty diff") + base_tree, head_tree = Tree(repo, base_tree_id), Tree(repo, head_tree_id) + views = [ + dict( + label="base..head", + before=base[:12], + after=head[:12], + status="ok", + reason="", + ) + ] + ctx, rows = analyse_view(base_tree, head_tree, "base..head") + if not ctx.changes: + raise rl.Fail("empty diff") + views[0]["files"] = len(ctx.changes) + per_view = [rows] + if tip_pair: + tip, tip_tree_id = tip_pair + tree_id, why = merged_tree(repo, tip, head) + view = dict( + label="merged", + before=tip[:12], + after=tree_id[:12] if tree_id else "", + status="ok", + reason="", + ) + views.append(view) + if tree_id is None: + view.update(status="skipped", reason=why) + surface_hit = False + if why.startswith("merge conflict in "): + surface_hit = any( + tag_of(p.strip()) not in OUTSIDE_TAGS for p in why[18:].split(",") + ) + per_view.append([ + dict(make_row("views", "(merged view)", 0, "merged", "changed" if surface_hit or not why.startswith("merge conflict in ") else "outside", + f"merged view skipped: {why}; a race with the target cannot be excluded"), + view="merged", merged_only=True) + ]) # fmt: skip + else: + mctx, mrows = analyse_view( + Tree(repo, tip_tree_id), Tree(repo, tree_id), "merged" + ) + view["files"] = len(mctx.changes) + per_view.append(mrows) + if args.cmd == "report" and args.emit: + per_view[0] = fold_emit(ctx, per_view[0], load_emit(args.emit)) + return ctx, views, merge_views(per_view) + + +def public(row) -> dict: + return {k: v for k, v in row.items() if k != "foldable"} + + +def cmd(args) -> None: + ctx, views, rows = run(args) + selected = ( + rows + if args.cmd == "report" + else [ + r + for r in rows + if r["kind"] in KINDS[args.cmd] or (args.cmd == "files" and False) + ] + ) + files = sorted(ctx.changes.values(), key=lambda c: c.path) + worst = {} + for r in rows: + if SEVERITY[r["status"]] >= SEVERITY[worst.get(r["path"], "outside")]: + worst[r["path"]] = r["status"] + if args.cmd == "files": + selected = [r for r in rows if r["kind"] == "file"] + if args.json: + payload = {"views": views, "rows": [public(r) for r in selected]} + if args.cmd in ("files", "report"): + payload["files"] = [ + {"path": c.path, "surface": c.tag, "class": c.cls, "functions": c.funcs, + "status": worst.get(c.path, "outside")} for c in files + ] # fmt: skip + if args.cmd == "report": + payload["rollup"] = rollup(rows) + sys.stdout.write(json.dumps(payload, indent=2) + "\n") + return + out = [] + for v in views: + tail = f"; {v['reason']}" if v["status"] != "ok" else "" + out.append( + f"view {v['label']}: {v['before']} -> {v['after']}, {v.get('files', 0)} files, {v['status']}{tail}" + ) + if args.cmd == "files": + out += ["", render_files(files, rows)] + elif selected: + out += ["", render_rows(selected)] + else: + out += ["", f"no {args.cmd} rows"] + if args.cmd == "report": + out += ["", rollup(rows)] + sys.stdout.write("\n".join(out) + "\n") + + +def main(argv=None) -> int: + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + sub = parser.add_subparsers(dest="cmd", required=True) + for name in ("files", "kernargs", "symbols", "surfaces", "data", "report"): + p = sub.add_parser(name, help=f"{name} rows of the diff") + p.add_argument("--repo", help="git directory (clone or worktree)") + p.add_argument("--base", help="base revision (merge base)") + p.add_argument("--head", help="head revision") + p.add_argument( + "--target-tip", + help="target branch tip; adds the merged view when it differs from base", + ) + p.add_argument( + "--round-dir", + help="round directory holding PIN.md (CLONE, BASE, HEAD, KEY)", + ) + p.add_argument("--json", action="store_true", help="machine-readable output") + if name == "report": + p.add_argument("--emit", help="impact_emit.py output to fold in") + args = parser.parse_args(argv) + try: + cmd(args) + except rl.Fail as exc: + print(f"impact: {exc}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py new file mode 100644 index 000000000000..1635c09fe6a1 --- /dev/null +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py @@ -0,0 +1,685 @@ +#!/usr/bin/env python3 +# Copyright (c) Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT +"""Emit the kernel ABI and emitted-IR facts of every shipped rocKE variant at two revisions. + + run --repo DIR --base REV --head REV [--flavor F] [--timeout S] --out FILE.json + +REV is any commit or tree id. Each side is `git archive`d into a temp dir (the repo is +only read) and a child interpreter with a scrubbed environment builds every shipped rocKE +variant there, lowers it to LLVM IR with the Python backend (no GPU, comgr or network), +and records the kernarg list parsed from the IR `define` line plus the SHA-256 of the IR +text. This EXECUTES rocKE code from the reviewed revisions, like building the head does. + +Variants come from every `*.kdp.json` under the shipped descriptor root whose +`kernelDescriptors[].kernel_source` names a rocKE `source` + `builder`. A KDP held by +DVC (`artifacts.dvc`, no `.kdp.json`) is regenerated with that side's +`IngestorGenerator/tools/registry_enumerator.py`; when neither works the KDP is listed +under `not_inspected`, never treated as equal. A variant is its descriptor `name`; a +family is (compile arch, source, builder): a `target: generic` variant compiles once per generic +target (`gfx11-generic`), so the shard arches that share it collapse into one family whose +`members` lists them. A descriptor's own `arch` narrows the KDP-level one. `ir_changed` lists every variant whose IR digest +differs, so it includes `params_changed`. A variant that fails to build is recorded under +`failures` and takes no part in the comparison. + +The flavor defaults to the top rung of `_LLVM_FLAVOR_LADDER` in the head's +`rocke/platform/python/rocke/core/lower_llvm.py`. Exit 0 when the analysis ran, whatever +it found; 1 with one line `impact_emit: ` on stderr when it could not run. +""" + +import argparse +import ast +import hashlib +import io +import json +import os +import re +import shutil +import subprocess +import sys +import tarfile +import tempfile +import time +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +PROVIDER = "dnn-providers/hip-kernel-provider" +ROCKE_PLATFORM = f"{PROVIDER}/rocke/platform/python" +ROCKE_LIBRARY = f"{PROVIDER}/rocke/library" +PACKAGING = f"{PROVIDER}/descriptor-packaging/python" +DESCRIPTORS = f"{PROVIDER}/src/engines/kernel_ingestor_engine/descriptors" +GENERATOR = "projects/hipdnn/tools/IngestorGenerator" +ENUMERATOR = f"{GENERATOR}/tools/registry_enumerator.py" +MANIFESTS = f"{GENERATOR}/configs" +LOWER_LLVM = f"{ROCKE_PLATFORM}/rocke/core/lower_llvm.py" + +REQUIRED_TREES = (ROCKE_PLATFORM, ROCKE_LIBRARY, PACKAGING) +OPTIONAL_TREES = (DESCRIPTORS, GENERATOR) +SCRUBBED_PREFIXES = ("ROCKE_", "HIP_", "ROCM_", "PYTHON") + + +class Fatal(Exception): + """The analysis could not run; the message is the one stderr line.""" + + +# --------------------------------------------------------------------------- git + + +def git(repo, *args, text=True): + cmd = ["git", "-C", str(repo), "-c", "core.quotepath=off", *args] + try: + r = subprocess.run(cmd, capture_output=True) + except OSError as e: + raise Fatal(f"cannot run git: {e}") + if r.returncode: + err = r.stderr.decode("utf-8", "replace").strip().splitlines() + raise Fatal(f"git {args[0]} failed: {err[-1] if err else r.returncode}") + return r.stdout.decode("utf-8", "replace") if text else r.stdout + + +def existing_paths(repo, rev, paths): + out = git(repo, "ls-tree", "--name-only", rev, "--", *paths) + return set(out.splitlines()) + + +def extract(repo, rev, dest): + """`git archive` the trees needed from REV into DEST (regular files and dirs only).""" + have = existing_paths(repo, rev, REQUIRED_TREES + OPTIONAL_TREES) + missing = [p for p in REQUIRED_TREES if p not in have] + if missing: + raise Fatal(f"{rev} lacks {', '.join(missing)}") + data = git( + repo, + "archive", + "--format=tar", + rev, + "--", + *sorted(have), + text=False, + ) + root = Path(dest).resolve() + with tarfile.open(fileobj=io.BytesIO(data)) as tf: + for m in tf: + target = (root / m.name).resolve() + if root != target and root not in target.parents: + raise Fatal(f"{rev}: archive member escapes the extract: {m.name}") + if m.isdir(): + target.mkdir(parents=True, exist_ok=True) + elif m.isfile(): + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(tf.extractfile(m).read()) + + +def default_flavor(repo, rev): + """Top rung of `_LLVM_FLAVOR_LADDER` in REV's lower_llvm.py, parsed without importing. + Older trees have no ladder; their newest flavor is the last entry of `LLVM_FLAVORS`. + """ + src = git(repo, "show", f"{rev}:{LOWER_LLVM}") + try: + tree = ast.parse(src) + except SyntaxError as e: + raise Fatal(f"cannot parse {LOWER_LLVM} at {rev}: {e}; pass --flavor") + consts, ladder, flavors = {}, None, None + for node in tree.body: + if isinstance(node, ast.Assign): + names, value = [ + t for t in node.targets if isinstance(t, ast.Name) + ], node.value + elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name): + names, value = [node.target], node.value + else: + continue + for n in names: + if n.id == "_LLVM_FLAVOR_LADDER": + ladder = value + elif n.id == "LLVM_FLAVORS": + flavors = value + elif isinstance(value, ast.Constant) and isinstance(value.value, str): + consts[n.id] = value.value + if ladder is None or not isinstance(ladder, (ast.Tuple, ast.List)): + names = flavors.elts if isinstance(flavors, (ast.Tuple, ast.List)) else [] + known = [ + consts.get(e.id) if isinstance(e, ast.Name) else getattr(e, "value", None) + for e in names + ] + if known and isinstance(known[-1], str): + return known[-1] + raise Fatal( + f"no _LLVM_FLAVOR_LADDER or LLVM_FLAVORS in {LOWER_LLVM} at {rev}; pass --flavor" + ) + rungs = [] + for el in ladder.elts: + if not (isinstance(el, ast.Tuple) and len(el.elts) == 2): + continue + major, flavor = el.elts + if isinstance(flavor, ast.Name): + flavor = consts.get(flavor.id) + elif isinstance(flavor, ast.Constant): + flavor = flavor.value + else: + flavor = None + if isinstance(major, ast.Constant) and isinstance(flavor, str): + rungs.append((major.value, flavor)) + if not rungs: + raise Fatal(f"cannot resolve _LLVM_FLAVOR_LADDER at {rev}; pass --flavor") + return max(rungs)[1] + + +# ----------------------------------------------------------------------- IR parse + +_ATTR_ADDRSPACE = re.compile(r"addrspace\(\d+\)$") + + +def _split_top(text, sep=","): + parts, depth, cur = [], 0, [] + for ch in text: + if ch in "([{<": + depth += 1 + elif ch in ")]}>": + depth -= 1 + if ch == sep and depth == 0: + parts.append("".join(cur)) + cur = [] + else: + cur.append(ch) + if "".join(cur).strip(): + parts.append("".join(cur)) + return [p.strip() for p in parts] + + +def _balanced_close(text, start): + depth = 0 + for i in range(start, len(text)): + if text[i] == "(": + depth += 1 + elif text[i] == ")": + depth -= 1 + if depth == 0: + return i + return -1 + + +def _param_type(text): + """The leading type of one parameter, as written (`ptr addrspace(1)`, `<4 x float>`, `i32`).""" + text = text.strip() + if text and text[0] in "<[{": + close = {"<": ">", "[": "]", "{": "}"}[text[0]] + depth = 0 + for i, ch in enumerate(text): + depth += ch == text[0] + depth -= ch == close + if depth == 0: + return text[: i + 1] + return text + words = text.split() + if not words: + return "" + if words[0] == "ptr" and len(words) > 1 and _ATTR_ADDRSPACE.match(words[1]): + return f"ptr {words[1]}" + return words[0] + + +def parse_kernargs(ir_text): + """[(name, type)] of the kernel's `define ... @k(...)` line. + + The entry point is the first `define` mentioning `amdgpu_kernel`, else the first + `define`. Raises ValueError when the text has none. + """ + lines = [ln for ln in ir_text.splitlines() if ln.lstrip().startswith("define")] + if not lines: + raise ValueError("no `define` line in the IR") + line = next((ln for ln in lines if "amdgpu_kernel" in ln), lines[0]) + at = line.find("@") + open_ = line.find("(", at) + if at < 0 or open_ < 0: + raise ValueError(f"unparsable define line: {line[:120]}") + close = _balanced_close(line, open_) + if close < 0: + raise ValueError(f"unbalanced define line: {line[:120]}") + params = [] + for raw in _split_top(line[open_ + 1 : close]): + m = re.search(r"%(\"[^\"]*\"|[\w.$-]+)\s*$", raw) + name = m.group(1).strip('"') if m else "" + body = raw[: m.start()] if m else raw + params.append([name, _param_type(body)]) + return params + + +# -------------------------------------------------------------------------- child +# Everything below up to `parent side` runs in the extract's scrubbed interpreter. + + +def _arch_list(value): + if isinstance(value, str): + return [value] + return [a for a in value or [] if isinstance(a, str)] + + +def _discover(root): + """(variants, not_inspected) of one extract: variants are dicts with name, arch, + source, builder, spec, target, kdp, origin.""" + base = Path(root) + desc = base / DESCRIPTORS + variants, skipped = [], [] + if not desc.is_dir(): + return variants, skipped + dvc_dirs = [] + for dirpath, dirnames, filenames in os.walk(desc): + dirnames.sort() + here = Path(dirpath) + kdps = sorted(f for f in filenames if f.endswith(".kdp.json")) + for f in kdps: + rel = (here / f).relative_to(base).as_posix() + try: + doc = json.loads((here / f).read_text(encoding="utf-8")) + except (OSError, ValueError) as e: + skipped.append({"kdp": rel, "reason": f"unreadable KDP: {e}"}) + continue + for d in doc.get("kernelDescriptors") or []: + ks = d.get("kernel_source") or {} + if not (ks.get("source") and ks.get("builder")): + continue + # A descriptor's own `arch` narrows the KDP-level one. + arches = _arch_list(d.get("arch")) or _arch_list(doc.get("arch")) + if not arches: + skipped.append({"kdp": rel, "reason": f"{d['name']}: no `arch`"}) + continue + variants.append( + dict( + name=d["name"], + arches=arches, + source=ks["source"], + builder=ks["builder"], + spec=ks.get("spec") or {}, + target=ks.get("target", "native"), + kdp=rel, + origin="kdp-in-git", + ) + ) + if "artifacts.dvc" in filenames and not kdps: + dvc_dirs.append(here) + for d in dvc_dirs: + got, why = _regenerate(base, d) + if why: + kdp = (d / f"{d.name}.kdp.json").relative_to(base).as_posix() + skipped.append({"kdp": kdp, "reason": why}) + else: + variants.extend(got) + return variants, skipped + + +def _regenerate(base, dvc_dir): + """Variants of a DVC-held KDP from the registry enumerator, or (None, reason).""" + rel_dir = dvc_dir.relative_to(base / DESCRIPTORS).as_posix() + kdp = (dvc_dir / f"{dvc_dir.name}.kdp.json").relative_to(base).as_posix() + enum = base / ENUMERATOR + cfgs = base / MANIFESTS + if not enum.is_file(): + return ( + None, + "KDP is DVC-held and registry_enumerator.py is absent at this revision", + ) + try: + import yaml + except ImportError: + return None, "KDP is DVC-held and PyYAML is not importable" + manifest = None + for p in sorted(cfgs.glob("*.engine.yaml")) if cfgs.is_dir() else []: + try: + doc = yaml.safe_load(p.read_text(encoding="utf-8")) + except (OSError, yaml.YAMLError): + continue + if isinstance(doc, dict) and doc.get("authored_subpath") == rel_dir: + manifest = p + break + if manifest is None: + return ( + None, + f"KDP is DVC-held and no *.engine.yaml manifest has authored_subpath {rel_dir}", + ) + out = base / ".impact_emit" / f"{dvc_dir.name}.enum.yaml" + out.parent.mkdir(exist_ok=True) + try: + r = subprocess.run( + [sys.executable, str(enum), "--manifest", str(manifest), "--out", str(out)], + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + cwd=str(base), + timeout=int(os.environ.get("IMPACT_EMIT_ENUM_TIMEOUT", "600")), + ) + except subprocess.TimeoutExpired: + return None, "registry_enumerator.py timed out" + if r.returncode: + tail = (r.stderr or r.stdout).strip().splitlines() + return ( + None, + f"registry_enumerator.py exited {r.returncode}: {tail[-1] if tail else ''}", + ) + try: + pack = yaml.safe_load(out.read_text(encoding="utf-8"))["packs"][0] + except (OSError, KeyError, IndexError, TypeError, yaml.YAMLError) as e: + return None, f"registry_enumerator.py output unusable: {type(e).__name__}: {e}" + got = [] + for k in pack.get("kernels") or []: + ks = k.get("kernel_source") or {} + if not (ks.get("source") and ks.get("builder")): + continue + arches = _arch_list(k.get("arch")) or _arch_list(pack.get("arch")) + if not arches: + return None, f"registry_enumerator.py output has no arch for {k['name']}" + got.append( + dict( + name=k["name"], + arches=arches, + source=ks["source"], + builder=ks["builder"], + spec=ks.get("spec") or {}, + target=ks.get("target", "native"), + kdp=kdp, + origin="registry_enumerator", + ) + ) + if not got: + return None, "registry_enumerator.py produced no rocKE kernels" + return got, None + + +class _Helpers: + """The hkp_pack / rocke helpers resolved by name; private names move between refs.""" + + def __init__(self): + self.error = None + self.missing_optional = set() + try: + from hkp_pack import rocke_compile as rc + + from rocke.helpers import compile as rcompile + except Exception as e: + self.error = f"helpers not importable: {type(e).__name__}: {e}" + return + need = {} + for name in ("_module_from_source", "_resolve_spec_class", "build_spec"): + need[name] = getattr(rc, name, None) + need["_lower_llvm_via_backend"] = getattr( + rcompile, "_lower_llvm_via_backend", None + ) + absent = [n for n, f in need.items() if not callable(f)] + if absent: + self.error = f"helpers missing at this revision: {', '.join(absent)}" + return + self.module_from_source = need["_module_from_source"] + self.resolve_spec_class = need["_resolve_spec_class"] + self.build_spec = need["build_spec"] + self.lower = need["_lower_llvm_via_backend"] + self.compile_arch = getattr(rc, "rocke_compile_arch", None) + self.require_arch_sig = getattr(rc, "_require_spec_arch_signature", None) + self.check_predicate = getattr(rc, "_check_support_predicate", None) + + +def _compile_arch(h, shard, target): + if callable(h.compile_arch): + return h.compile_arch(shard, target) + if target != "native": + raise RuntimeError( + f"rocke_compile_arch missing; cannot resolve target {target!r}" + ) + return shard + + +def _build_one(h, v, arch): + from importlib import import_module + + module = import_module(h.module_from_source(v["source"])) + fn = getattr(module, v["builder"]) + cls = h.resolve_spec_class(module, fn) + if callable(h.require_arch_sig): + h.require_arch_sig(fn, v["builder"]) + obj = h.build_spec(cls, v["spec"]) + if callable(h.check_predicate): + h.check_predicate(module, v["builder"], obj, arch) + kd = fn(obj, arch=arch) + text = h.lower(kd, arch=arch, backend="python", spec=None) + return { + "params": parse_kernargs(text), + "ir": hashlib.sha256(text.encode("utf-8")).hexdigest(), + } + + +def child_main(root, out): + # The script's own directory must not shadow rocKE modules. + here = str(Path(__file__).resolve().parent) + sys.path[:] = [p for p in sys.path if os.path.abspath(p or ".") != here] + t0 = time.time() + variants, skipped = _discover(root) + h = _Helpers() + fams = {} + # One family per (compile arch, source, builder): the packer compiles once per + # compile arch, so shard arches that share a generic target collapse into one family. + for v in variants: + for shard in v["arches"]: + err = h.error + carch = shard + if not err: + try: + carch = _compile_arch(h, shard, v["target"]) + except Exception as e: + err = f"{type(e).__name__}: {str(e)[:300]}" + key = (carch, v["source"], v["builder"]) + fam = fams.setdefault( + key, + { + "arch": carch, + "source": v["source"], + "builder": v["builder"], + "kdp": v["kdp"], + "spec_origin": v["origin"], + "members": [], + "names": [], + "built": {}, + "failures": {}, + }, + ) + if shard not in fam["members"]: + fam["members"].append(shard) + if v["name"] in fam["names"]: + continue # same variant reached through another member arch + fam["names"].append(v["name"]) + if err: + fam["failures"][v["name"]] = err + continue + try: + fam["built"][v["name"]] = _build_one(h, v, carch) + except Exception as e: + fam["failures"][v["name"]] = f"{type(e).__name__}: {str(e)[:300]}" + doc = { + "families": list(fams.values()), + "not_inspected": skipped, + "seconds": round(time.time() - t0, 1), + } + Path(out).write_text(json.dumps(doc), encoding="utf-8", newline="\n") + return 0 + + +# ------------------------------------------------------------------- parent side + + +def scrubbed_env(root, flavor): + env = { + k: v + for k, v in os.environ.items() + if not k.upper().startswith(SCRUBBED_PREFIXES) + } + base = Path(root) + env["PYTHONPATH"] = os.pathsep.join( + str(base / p) for p in (ROCKE_PLATFORM, ROCKE_LIBRARY, PACKAGING) + ) + env["PYTHONDONTWRITEBYTECODE"] = "1" + env["PYTHONUTF8"] = "1" + env["ROCKE_LLVM_FLAVOR"] = flavor + env["ROCKE_PROVIDER_ROOT"] = str(base / PROVIDER) + return env + + +def run_side(repo, rev, flavor, timeout, label): + tmp = tempfile.mkdtemp(prefix="impact_emit_") + try: + extract(repo, rev, tmp) + out = Path(tmp) / ".impact_emit_side.json" + cmd = [ + sys.executable, + str(Path(__file__).resolve()), + "child", + "--root", + tmp, + "--out", + str(out), + ] + try: + r = subprocess.run( + cmd, + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + cwd=tmp, + env=scrubbed_env(tmp, flavor), + timeout=timeout, + ) + except subprocess.TimeoutExpired: + raise Fatal(f"{label} ({rev}) timed out after {timeout}s") + if r.returncode or not out.is_file(): + tail = (r.stderr or r.stdout).strip().splitlines() + raise Fatal( + f"{label} ({rev}) child exited {r.returncode}: {tail[-1] if tail else ''}" + ) + return json.loads(out.read_text(encoding="utf-8")) + finally: + shutil.rmtree(tmp, ignore_errors=True) + + +def compare(base_side, head_side): + def by_key(side): + return {(f["arch"], f["source"], f["builder"]): f for f in side["families"]} + + b, h = by_key(base_side), by_key(head_side) + out = [] + for key in sorted(set(b) | set(h)): + fb, fh = b.get(key), h.get(key) + names_b = fb["names"] if fb else [] + names_h = fh["names"] if fh else [] + built_b = fb["built"] if fb else {} + built_h = fh["built"] if fh else {} + common = [n for n in names_h if n in built_h and n in built_b] + params_changed = [ + n for n in common if built_b[n]["params"] != built_h[n]["params"] + ] + ir_changed = [n for n in common if built_b[n]["ir"] != built_h[n]["ir"]] + shown = (params_changed or common or [None])[0] + if shown is None: + shown = next((n for n in names_h if n in built_h), None) + if shown is None: + shown = next((n for n in names_b if n in built_b), None) + pick = fh or fb + members = sorted( + set((fb or {}).get("members", [])) | set((fh or {}).get("members", [])) + ) + out.append( + { + "arch": key[0], + "source": key[1], + "builder": key[2], + "kdp": pick["kdp"], + "spec_origin": pick["spec_origin"], + "members": members, + "variants": {"base": len(names_b), "head": len(names_h)}, + "only_in_base": [n for n in names_b if n not in names_h], + "only_in_head": [n for n in names_h if n not in names_b], + "params_changed": params_changed, + "ir_changed": ir_changed, + "params": { + "base": built_b[shown]["params"] if shown in built_b else None, + "head": built_h[shown]["params"] if shown in built_h else None, + }, + "failures": { + "base": dict(fb["failures"]) if fb else {}, + "head": dict(fh["failures"]) if fh else {}, + }, + } + ) + return out + + +def cmd_run(a): + repo = Path(a.repo) + if not repo.is_dir(): + raise Fatal(f"--repo {a.repo} is not a directory") + flavor = a.flavor or default_flavor(repo, a.head) + t0 = time.time() + with ThreadPoolExecutor(max_workers=2) as pool: # the sides are independent + fb = pool.submit(run_side, repo, a.base, flavor, a.timeout, "base") + fh = pool.submit(run_side, repo, a.head, flavor, a.timeout, "head") + base_side, head_side = fb.result(), fh.result() + families = compare(base_side, head_side) + not_inspected = [ + {"kdp": n["kdp"], "side": side, "reason": n["reason"]} + for side, doc in (("base", base_side), ("head", head_side)) + for n in doc["not_inspected"] + ] + seconds = round(time.time() - t0, 1) + doc = { + "schema": 1, + "tool": "impact_emit", + "flavor": flavor, + "base": a.base, + "head": a.head, + "families": families, + "not_inspected": not_inspected, + "seconds": seconds, + } + try: + Path(a.out).write_text( + json.dumps(doc, indent=1) + "\n", encoding="utf-8", newline="\n" + ) + except OSError as e: + raise Fatal(f"cannot write {a.out}: {e}") + for f in families: + fails = len(f["failures"]["base"]) + len(f["failures"]["head"]) + print( + f"{f['arch']} {f['source']}:{f['builder']} variants {f['variants']['base']}/" + f"{f['variants']['head']} only_base {len(f['only_in_base'])} only_head " + f"{len(f['only_in_head'])} params_changed {len(f['params_changed'])} " + f"ir_changed {len(f['ir_changed'])} failures {fails} ({f['spec_origin']})" + ) + for n in not_inspected: + print(f"not inspected [{n['side']}] {n['kdp']}: {n['reason']}") + print(f"flavor {flavor}, {len(families)} families, {seconds}s") + return 0 + + +def main(argv=None): + p = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + sub = p.add_subparsers(dest="cmd", required=True) + r = sub.add_parser("run", help="compare two revisions") + r.add_argument("--repo", required=True) + r.add_argument("--base", required=True) + r.add_argument("--head", required=True) + r.add_argument("--flavor") + r.add_argument("--timeout", type=int, default=900, help="seconds per side") + r.add_argument("--out", required=True) + c = sub.add_parser("child") # internal: runs inside the scrubbed interpreter + c.add_argument("--root", required=True) + c.add_argument("--out", required=True) + args = p.parse_args(argv) + if args.cmd == "child": + return child_main(args.root, args.out) + try: + return cmd_run(args) + except Fatal as e: + print(f"impact_emit: {e}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py index 282e1a447505..d804ee7faf75 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py @@ -50,6 +50,15 @@ ("tooling", ("projects/hipdnn/tools/", "tools/")), ] +# Changes under these trigger the IMPACT lane (lanes/runtime-impact.md): the kernel ingestor's +# producers, packer, native packs, SDK ingestor headers and generator configs. +IMPACT_PREFIXES = ( + "dnn-providers/hip-kernel-provider/", + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/", + "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json", + "projects/hipdnn/tools/IngestorGenerator/", +) + PROJECT_ROOTS = ("projects", "dnn-providers", "shared") LICENSE_STEMS = {"LICENSE", "CONTRIBUTING", "SECURITY", "README"} CONCERN_LANES = { @@ -65,6 +74,7 @@ "DOCS", "RUN", "DRIFT", + "IMPACT", } @@ -335,6 +345,8 @@ def concern_scope(lane: str, rows): or re.fullmatch(r"requirements.*\.txt", name(r[0])) or re.search(r"\.(c|cc|cpp|cxx|h|hh|hpp|hxx|hip|cu|cuh)$", name(r[0])) ] + if lane == "IMPACT": + return [r for r in rows if r[0].startswith(IMPACT_PREFIXES)] return rows @@ -392,9 +404,9 @@ def write_packet(rd: Path, lane: str, kind: str, rows, areas, sampled, parent) - def cmd_packets(args) -> None: """Write packets/.md for each lane of owners.tsv (files with numstat rows, areas, Orient paths, `Sampled:`) and each concern lane named by --lanes (changed files in - scope). BUILD and DEPS scope to their trigger files, other concern lanes take every - changed file; DRIFT also lists drift.txt and is N/A when its Trigger is `no`. The diff - is not read.""" + scope). BUILD, DEPS and IMPACT scope to their trigger files, other concern lanes take + every changed file; DRIFT also lists drift.txt and is N/A when its Trigger is `no`. The + diff is not read.""" rd = Path(args.round_dir) rows = {r[0]: r for r in read_numstat(rd)} areas = read_areas(rd) diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py b/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py new file mode 100644 index 000000000000..15b1583bf388 --- /dev/null +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py @@ -0,0 +1,550 @@ +# Copyright (c) Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT +"""Unit tests for the hipdnn-deep-review impact.py helper (kernel ABI and dispatch facts of a diff). + +Every case builds a throwaway git repository under tmp_path with paths the script classifies, so no +test depends on this clone's history or on rocKE. Git runs for real with the user's configuration +isolated. +""" + +import importlib.util +import json +import subprocess +from pathlib import Path +from types import ModuleType + +import pytest + +SCRIPT = ( + Path(__file__).resolve().parent.parent + / "skills" + / "hipdnn-deep-review" + / "scripts" + / "impact.py" +) + +HKP = "dnn-providers/hip-kernel-provider/" +BUILDER = HKP + "rocke/library/kernels/gfx950/attn.py" +KERNELS_INIT = HKP + "rocke/library/kernels/__init__.py" +OTHER_KERNEL = HKP + "rocke/library/kernels/gfx942/other.py" +PIN = HKP + "descriptor-packaging/tests/test_pin.py" +PACK = HKP + "src/engines/kernel_ingestor_engine/packs/Gfx950Native.cpp" +KDP = ( + HKP + + "src/engines/kernel_ingestor_engine/descriptors/rocKE/gfx950_attn/gfx950_attn.kdp.json" +) +MANIFEST = "projects/hipdnn/tools/IngestorGenerator/configs/gfx950_attn.engine.yaml" +HEADER = "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/Plan.hpp" +UTIL = HKP + "descriptor-packaging/python/hkp_pack/util.py" +READER = HKP + "descriptor-packaging/tests/test_use.py" +GENERIC = "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json" + +BUILD = """\ +def build_attn(b, spec): + b.param("q_ptr", PTR) + b.param("k_ptr", PTR) + b.param("scale", F32) + if spec.sinks: + b.param("sink_ptr", PTR) + return b +""" +BUILD_REORDERED = BUILD.replace(' b.param("q_ptr", PTR)\n b.param("k_ptr", PTR)\n', + ' b.param("k_ptr", PTR)\n b.param("q_ptr", PTR)\n') # fmt: skip +PIN_TEST = ( + 'def test_sig(signature):\n assert signature == ["q_ptr", "k_ptr", "scale"]\n' +) +PIN_TEST_NEW = PIN_TEST.replace('"q_ptr", "k_ptr"', '"k_ptr", "q_ptr"') +PACK_STATIC = """\ +std::vector attnSignature() +{ + return {KernelArgument{"global_buffer", PTR, 0, "q_ptr"}, + KernelArgument{"global_buffer", PTR, 8, "k_ptr"}, + KernelArgument{"by_value", F32, 16, "scale"}}; +} +""" +PACK_DYNAMIC = """\ +std::vector attnSignature(const KernelDefinition& kernel) +{ + std::vector sig; + sig.emplace_back(KernelArgument{"global_buffer", 8, 0, "q_ptr"}); + return sig; +} +""" +MANIFEST_YAML = f"""\ +arch: gfx950 +provider_root: {HKP.rstrip("/")} +launch_surface: + - name: kernargs + python_source: kernels/gfx950/attn.py:build_attn + cpp_mirror: {PACK} + kmd_fields: [] + guard: none + test: none +""" +KDP_JSON = json.dumps( + { + "kernelDescriptors": [ + {"name": "v", "kernel_source": {"source": "kernels/gfx950/attn.py"}} + ] + } +) + + +@pytest.fixture(scope="module") +def mod() -> ModuleType: + spec = importlib.util.spec_from_file_location("hipdnn_deep_review_impact", SCRIPT) + assert spec and spec.loader, f"could not load {SCRIPT}" + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.fixture(autouse=True) +def isolated_git(tmp_path_factory, monkeypatch): + empty = tmp_path_factory.mktemp("gitcfg") / "gitconfig" + empty.write_text("") + monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(empty)) + monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1") + for who in ("AUTHOR", "COMMITTER"): + monkeypatch.setenv(f"GIT_{who}_NAME", "Test") + monkeypatch.setenv(f"GIT_{who}_EMAIL", "test@example.com") + + +def git(repo: Path, *args: str) -> str: + done = subprocess.run( + ["git", "-C", str(repo), *args], + check=True, + capture_output=True, + text=True, + encoding="utf-8", + ) + return done.stdout.strip() + + +_COUNTER = {} + + +def commit( + repo: Path, files: dict, message: str = "c", parent: str | None = None +) -> str: + """One commit on top of `parent` (default: the last commit made in `repo`), written with a + single fast-import so a test spawns few git processes. A None value deletes the file. + """ + parent = parent or _COUNTER.get(repo) + n = _COUNTER[str(repo) + "#n"] = _COUNTER.get(str(repo) + "#n", 0) + 1 + msg = message.encode() + stream = [f"commit refs/heads/b{n}\nmark :1\ncommitter T {n} +0000\n".encode(), + b"data %d\n%s\n" % (len(msg), msg)] # fmt: skip + if parent: + stream.append(f"from {parent}\n".encode()) + for path, content in files.items(): + if content is None: + stream.append(f"D {path}\n".encode()) + continue + data = content if isinstance(content, bytes) else content.encode("utf-8") + stream.append( + f"M 100644 inline {path}\ndata {len(data)}\n".encode() + data + b"\n" + ) + marks = repo.parent / "marks" + subprocess.run(["git", "-C", str(repo), "fast-import", "--quiet", f"--export-marks={marks}"], + input=b"".join(stream), check=True, capture_output=True) # fmt: skip + sha = marks.read_text().split()[1] + _COUNTER[repo] = sha + return sha + + +def make_repo(tmp_path: Path, base: dict) -> tuple: + repo = tmp_path / "repo" + repo.mkdir(parents=True) + git(repo, "init", "-q", "-b", "main") + return repo, commit(repo, base, "base") + + +def branch_commit(repo: Path, start: str, files: dict, name: str) -> str: + return commit(repo, files, name, parent=start) + + +def set_config(repo: Path, **values) -> None: + with (repo / ".git" / "config").open("a", encoding="utf-8") as cfg: + for key, value in values.items(): + section, name = key.split("__") + cfg.write(f"[{section}]\n\t{name} = {value}\n") + + +def report(mod, capsys, repo, base, head, *extra, cmd="report") -> dict: + rc = mod.main( + [cmd, "--repo", str(repo), "--base", base, "--head", head, "--json", *extra] + ) + out = capsys.readouterr().out + assert rc == 0, out + return json.loads(out) + + +def rows_of(data, kind=None, status=None): + return [ + r + for r in data["rows"] + if (kind is None or r["kind"] == kind) + and (status is None or r["status"] == status) + ] + + +def file_class(data, path): + return next(f["class"] for f in data["files"] if f["path"] == path) + + +def abi_repo(tmp_path, extra=None): + """base: builder + literal pin; returns (repo, base).""" + return make_repo(tmp_path, {BUILDER: BUILD, PIN: PIN_TEST, **(extra or {})}) + + +def test_reordered_builder_with_unmoved_pin_is_mismatch(mod, capsys, tmp_path): + # catches: copy search that misses literal lists in test files + repo, base = abi_repo(tmp_path) + head = commit(repo, {BUILDER: BUILD_REORDERED}) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "kernargs", "mismatch") + assert row["path"] == BUILDER + assert [(c["path"], c["moved"]) for c in row["counterparts"]] == [(PIN, False)] + assert data["rollup"] == "Runtime impact: BREAKS" + + +def test_builder_and_copies_updated_together_is_changed(mod, capsys, tmp_path): + # catches: any list edit reported as a break + repo, base = abi_repo(tmp_path) + head = commit(repo, {BUILDER: BUILD_REORDERED, PIN: PIN_TEST_NEW}) + data = report(mod, capsys, repo, base, head) + assert not rows_of(data, status="mismatch") + (row,) = rows_of(data, "kernargs") + assert row["status"] == "changed" and row["counterparts"][0]["moved"] is True + + +def test_comment_only_touch_of_pin_is_not_moved(mod, capsys, tmp_path): + # catches: a comment edit of a counterpart counted as the counterpart following the producer + repo, base = abi_repo(tmp_path) + head = commit(repo, {BUILDER: BUILD_REORDERED, PIN: "# touched\n" + PIN_TEST}) + data = report(mod, capsys, repo, base, head) + assert [r["path"] for r in rows_of(data, "kernargs", "mismatch")] == [BUILDER] + + +def test_pin_on_target_tip_only_is_mismatch_in_merged_view_only(mod, capsys, tmp_path): + # catches: merged view not computed, or computed against the merge base + repo, base = make_repo(tmp_path, {BUILDER: BUILD}) + head = branch_commit(repo, base, {BUILDER: BUILD_REORDERED}, "feature") + tip = branch_commit(repo, base, {PIN: PIN_TEST}, "target") + data = report(mod, capsys, repo, base, head, "--target-tip", tip) + (bad,) = rows_of(data, "kernargs", "mismatch") + assert bad["merged_only"] and bad["view"] == "merged" + assert [r["status"] for r in rows_of(data, "kernargs") if not r["merged_only"]] == [ + "changed" + ] + assert data["rollup"] == "Runtime impact: BREAKS" + # --round-dir takes the same tip from PIN.md's TARGET_TIP + rd = tmp_path / "round" + rd.mkdir() + (rd / "PIN.md").write_text( + f"CLONE: {repo}\nBASE: {base}\nHEAD: {head}\nTARGET_TIP: {tip}\n", + encoding="utf-8", + ) + assert mod.main(["report", "--round-dir", str(rd), "--json"]) == 0 + assert json.loads(capsys.readouterr().out)["rollup"] == "Runtime impact: BREAKS" + + +def test_merge_conflict_skips_merged_view_with_reason(mod, capsys, tmp_path): + # catches: a crash on conflict, or a clean NONE when the race cannot be excluded + repo, base = make_repo(tmp_path, {BUILDER: BUILD}) + head = branch_commit( + repo, base, {BUILDER: BUILD.replace("scale", "scale_a")}, "feature" + ) + tip = branch_commit( + repo, base, {BUILDER: BUILD.replace("scale", "scale_b")}, "target" + ) + data = report(mod, capsys, repo, base, head, "--target-tip", tip) + merged = next(v for v in data["views"] if v["label"] == "merged") + assert ( + merged["status"] == "skipped" + and "conflict" in merged["reason"] + and BUILDER in merged["reason"] + ) + assert data["rollup"] != "Runtime impact: NONE (1 files inspected)" + assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) + + +def test_static_cpp_list_disagreeing_with_builder_is_mismatch(mod, capsys, tmp_path): + # catches: static KernelArgument lists not parsed + repo, base = abi_repo(tmp_path, {PACK: PACK_STATIC}) + head = commit(repo, {BUILDER: BUILD_REORDERED}) + data = report(mod, capsys, repo, base, head) + paths = { + c["path"] + for r in rows_of(data, "kernargs", "mismatch") + for c in r["counterparts"] + } + assert paths == {PACK, PIN} + + +def test_declared_pair_checks_static_list_and_flags_dynamic(mod, capsys, tmp_path): + # catches: declared launch_surface pairs unused; a dynamic signature read as agreeing + repo, base = abi_repo(tmp_path, {PACK: PACK_STATIC, MANIFEST: MANIFEST_YAML}) + head = commit( + repo, + {PACK: PACK_STATIC.replace("PTR, 0", "PTR, 0 ").replace('"k_ptr"', '"k_ptr2"')}, + ) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "kernargs", "mismatch") + assert row["path"] == PACK and "disagrees" in row["reason"] + repo2, base2 = abi_repo( + tmp_path / "dyn", {PACK: PACK_DYNAMIC, MANIFEST: MANIFEST_YAML} + ) + head2 = commit(repo2, {PACK: PACK_DYNAMIC + "// edit\nint x = 1;\n"}) + data2 = report(mod, capsys, repo2, base2, head2) + dynamic = [r for r in rows_of(data2, "kernargs") if "dynamic" in r["reason"]] + assert dynamic and not rows_of(data2, status="mismatch") + assert data2["rollup"].startswith("Runtime impact: NEEDS-DEVICE gfx950") + + +def test_cpp_string_literal_edit_is_code_not_whitespace(mod, capsys, tmp_path): + # catches: whitespace deletion instead of collapse ("attn fwd" == "attnfwd") + repo, base = make_repo( + tmp_path, {PACK: 'const char* kName = "attn fwd";\nint f() { return 1; }\n'} + ) + head = commit( + repo, {PACK: 'const char* kName = "attnfwd";\nint f() { return 1; }\n'} + ) + assert ( + file_class(report(mod, capsys, repo, base, head, cmd="files"), PACK) == "code" + ) + spaced = commit( + repo, {PACK: 'const char* kName = "attnfwd";\nint f() { return 1; }\n'} + ) + assert ( + file_class(report(mod, capsys, repo, head, spaced, cmd="files"), PACK) + == "whitespace-only" + ) + + +def test_json_type_change_is_code_and_key_reorder_is_data_equal(mod, capsys, tmp_path): + # catches: JSON compared without types (3 == 3.0); key order read as change + repo, base = make_repo(tmp_path, {KDP: '{"a": 3, "b": [1, 2]}\n'}) + typed = commit(repo, {KDP: '{"a": 3.0, "b": [1, 2]}\n'}) + data = report(mod, capsys, repo, base, typed, cmd="data") + assert ( + file_class(report(mod, capsys, repo, base, typed, cmd="files"), KDP) == "code" + ) + assert [r["status"] for r in data["rows"]] == ["changed"] + reordered = commit(repo, {KDP: '{"b": [1, 2], "a": 3.0}\n'}) + assert ( + file_class(report(mod, capsys, repo, typed, reordered, cmd="files"), KDP) + == "data-equal" + ) + assert [ + r["status"] + for r in report(mod, capsys, repo, typed, reordered, cmd="data")["rows"] + ] == ["inert"] + + +def test_removed_function_with_reader_is_mismatch_docstring_mention_is_not( + mod, capsys, tmp_path +): + # catches: word-match readers (docstrings) and removals with a live importer missed + util = "def helper():\n return 1\n\n\ndef keep():\n return 2\n" + used = "from hkp_pack.util import helper\n\n\ndef test_it():\n assert helper() == 1\n" + repo, base = make_repo(tmp_path, {UTIL: util, READER: used}) + head = commit(repo, {UTIL: "def keep():\n return 2\n"}) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "symbols", "mismatch") + assert row["name"] == "helper" and {c["path"] for c in row["counterparts"]} == { + READER + } + docs_only = 'def test_it():\n """No longer calls helper()."""\n' + repo2, base2 = make_repo(tmp_path / "doc", {UTIL: util, READER: docs_only}) + head2 = commit(repo2, {UTIL: "def keep():\n return 2\n"}) + assert not rows_of(report(mod, capsys, repo2, base2, head2), "symbols") + + +def test_removed_override_is_not_removed_while_a_base_defines_it(mod, capsys, tmp_path): + # catches: a method dropped from a subclass reported removed although a base still provides it + pkg = HKP + "descriptor-packaging/python/hkp_pack/" + base_py = "class Base:\n def helper(self):\n return 1\n" + sub_py = "from .base import Base\n\n\nclass Sub(Base):\n def helper(self):\n return 2\n" + bare_sub = "from .base import Base\n\n\nclass Sub(Base):\n pass\n" + used = ( + "from hkp_pack.sub import Sub\n\n\ndef test_it():\n assert Sub().helper()\n" + ) + files = {pkg + "base.py": base_py, pkg + "sub.py": sub_py, READER: used} + repo, base = make_repo(tmp_path / "a", files) + head = commit(repo, {pkg + "sub.py": bare_sub}) + assert not rows_of(report(mod, capsys, repo, base, head), "symbols") + repo, base = make_repo(tmp_path / "b", files) + head = commit( + repo, {pkg + "sub.py": bare_sub, pkg + "base.py": "class Base:\n pass\n"} + ) + mismatched = rows_of(report(mod, capsys, repo, base, head), "symbols", "mismatch") + assert "Sub.helper" in {r["name"] for r in mismatched} + unknown = sub_py.replace( + "from .base import Base", "from vendor import Mystery" + ).replace("(Base)", "(Mystery)") + repo, base = make_repo(tmp_path / "c", {**files, pkg + "sub.py": unknown}) + head = commit( + repo, + { + pkg + + "sub.py": unknown.replace( + " def helper(self):\n return 2\n", " pass\n" + ) + }, + ) + (row,) = rows_of(report(mod, capsys, repo, base, head), "symbols") + assert ( + row["status"] == "changed" + and row["reason"] == "override removed; base Mystery not resolved" + ) + + +def test_generic_target_member_removed_names_target_and_member(mod, capsys, tmp_path): + # catches: membership diff absent; a data-equal call on a changed table + table = {"schemaVersion": 1, "generics": {"gfx11-generic": ["gfx1100", "gfx1151"]}} + repo, base = make_repo(tmp_path, {GENERIC: json.dumps(table)}) + table["generics"]["gfx11-generic"] = ["gfx1100"] + head = commit(repo, {GENERIC: json.dumps(table)}) + (row,) = rows_of(report(mod, capsys, repo, base, head, cmd="data")) + assert ( + row["name"] == "gfx11-generic:gfx1151" + and "gfx11-generic" in row["reason"] + and "gfx1151" in row["reason"] + ) + assert row["status"] == "changed" + + +def test_empty_diff_is_an_error_never_none(mod, capsys, tmp_path): + # catches: an empty diff reported as NONE + repo, base = abi_repo(tmp_path) + head = commit(repo, {}, "empty") + assert ( + mod.main(["report", "--repo", str(repo), "--base", base, "--head", head]) == 1 + ) + captured = capsys.readouterr() + assert captured.err.strip() == "impact: empty diff" and "NONE" not in captured.out + + +def test_user_git_config_does_not_change_results(mod, capsys, tmp_path): + # catches: parsing that breaks under diff.noprefix, mnemonicPrefix or grep.column + repo, base = abi_repo(tmp_path) + set_config( + repo, + diff__noprefix="true", + diff__mnemonicPrefix="true", + grep__column="true", + grep__lineNumber="true", + ) + head = commit(repo, {BUILDER: BUILD_REORDERED}) + data = report(mod, capsys, repo, base, head) + assert [r["path"] for r in rows_of(data, "kernargs", "mismatch")] == [BUILDER] + assert rows_of(data, "kernargs", "mismatch")[0]["line"] == 1 + + +def test_surface_code_not_understood_is_changed_and_comment_edits_are_inert( + mod, capsys, tmp_path +): + # catches: path-only inertness; a surface file that cannot be read reported inert + repo, base = make_repo(tmp_path, {HEADER: "// a\nint f();\n", PACK: "int g();\n"}) + head = commit(repo, {HEADER: "// b\nint f();\n", PACK: b"\xff\xfe\x00bin"}) + data = report(mod, capsys, repo, base, head) + assert {r["path"]: r["status"] for r in data["rows"]} == { + HEADER: "inert", + PACK: "changed", + } + comment = commit(repo, {HEADER: "// c\nint f();\n"}) + data = report(mod, capsys, repo, head, comment) + assert data["rollup"] == "Runtime impact: NONE (1 files inspected)" + + +def test_no_surface_file_is_na(mod, capsys, tmp_path): + # catches: docs and test-only diffs reported as a runtime impact + repo, base = make_repo(tmp_path, {"projects/hipdnn/docs/x.md": "a\n", PIN: "# a\n"}) + head = commit(repo, {"projects/hipdnn/docs/x.md": "b\n", PIN: "# b\n"}) + data = report(mod, capsys, repo, base, head) + assert {r["status"] for r in data["rows"]} == {"outside"} + assert data["rollup"] == "Runtime impact: N/A (no surface file)" + + +def test_ancestor_init_of_a_consumed_module_is_consumed(mod, capsys, tmp_path): + # catches: import_module running a package __init__ that no descriptor names + base_files = { + BUILDER: BUILD, + KDP: KDP_JSON, + KERNELS_INIT: "VERSION = 1\n", + OTHER_KERNEL: "def f():\n return 1\n", + } + repo, base = make_repo(tmp_path, base_files) + head = commit( + repo, {KERNELS_INIT: "VERSION = 2\n", OTHER_KERNEL: "def f():\n return 2\n"} + ) + data = report(mod, capsys, repo, base, head) + status = {r["path"]: r["status"] for r in rows_of(data, "file")} + assert status[KERNELS_INIT] == "changed" + assert status[OTHER_KERNEL] == "inert" + + +def write_emit(path, **family): + base = dict(arch="gfx950", source=BUILDER, builder="build_attn", kdp=KDP, spec_origin="kdp-in-git", + variants={"base": 1, "head": 1}, only_in_base=[], only_in_head=[], params_changed=[], + ir_changed=[], params={"base": None, "head": None}, failures={"base": {}, "head": {}}) # fmt: skip + base.update(family) + doc = {"schema": 1, "tool": "impact_emit", "flavor": "llvm23", "base": "b", "head": "h", + "families": [base], "not_inspected": [], "seconds": 1.0} # fmt: skip + Path(path).write_text(json.dumps(doc), encoding="utf-8") + return str(path) + + +def test_emit_fold_equal_variants_make_shipped_module_edits_inert( + mod, capsys, tmp_path +): + # catches: a body edit of a shipped module always NEEDS-DEVICE even with proof of equal IR + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BUILD.replace("return b", "x = 1\n return b")}) + plain = report(mod, capsys, repo, base, head) + assert plain["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + equal = report( + mod, capsys, repo, base, head, "--emit", write_emit(tmp_path / "eq.json") + ) + assert equal["rollup"] == "Runtime impact: NONE (1 files inspected)" + changed = report( + mod, + capsys, + repo, + base, + head, + "--emit", + write_emit(tmp_path / "ir.json", ir_changed=["v"]), + ) + assert changed["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +def test_emit_params_changed_vs_unchanged_static_list_is_mismatch( + mod, capsys, tmp_path +): + # catches: emitted params never compared with the native pack's static list; mismatch downgraded + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, PACK: PACK_STATIC}) + head = commit(repo, {BUILDER: BUILD.replace("return b", "x = 1\n return b")}) + wider = [["q_ptr", "ptr"], ["k_ptr", "ptr"], ["batch", "i32"], ["scale", "float"]] + data = report(mod, capsys, repo, base, head, "--emit", + write_emit(tmp_path / "p.json", params_changed=["v"], params={"base": None, "head": wider})) # fmt: skip + (row,) = rows_of(data, "kernargs", "mismatch") + assert row["path"] == PACK and "batch" in row["reason"] + assert data["rollup"] == "Runtime impact: BREAKS" + # the same params as the static list: consistent, so changed rather than mismatch + same = [["q_ptr", "ptr"], ["k_ptr", "ptr"], ["scale", "float"]] + data = report(mod, capsys, repo, base, head, "--emit", + write_emit(tmp_path / "s.json", params_changed=["v"], params={"base": None, "head": same})) # fmt: skip + assert not rows_of(data, status="mismatch") + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + # a static mismatch is never folded away by an otherwise equal emit + repo2, base2 = make_repo( + tmp_path / "keep", {BUILDER: BUILD, PIN: PIN_TEST, KDP: KDP_JSON} + ) + head2 = commit(repo2, {BUILDER: BUILD_REORDERED}) + data = report( + mod, capsys, repo2, base2, head2, "--emit", write_emit(tmp_path / "e2.json") + ) + assert data["rollup"] == "Runtime impact: BREAKS" diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py new file mode 100644 index 000000000000..68919bfbc6cc --- /dev/null +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py @@ -0,0 +1,441 @@ +# Copyright (c) Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT +"""Tests for the hipdnn-deep-review impact_emit.py script. + +A tiny fake provider tree (fake `hkp_pack` and `rocke` packages with the helper names +the child resolves, a builder module, a KDP) lives in a temporary git repo; the lowering +returns deterministic IR text, so no rocKE, comgr or GPU is involved. +""" + +import importlib.util +import json +import os +import subprocess +import textwrap +from pathlib import Path + +import pytest + +SCRIPT = ( + Path(__file__).resolve().parent.parent + / "skills" + / "hipdnn-deep-review" + / "scripts" + / "impact_emit.py" +) + +PROVIDER = "dnn-providers/hip-kernel-provider" +DESC = f"{PROVIDER}/src/engines/kernel_ingestor_engine/descriptors/rocKE/fake" +BUILDER = f"{PROVIDER}/rocke/library/kernels/gfx_fake/fake_attn.py" + +HELPERS = """ +import dataclasses +def _module_from_source(source): + return source[:-3].replace("/", ".") if source.endswith(".py") else source.replace("/", ".") +def _resolve_spec_class(module, fn): + return module.Spec +def build_spec(cls, data): + return cls(**data) +""" + +COMPILE = """ +import os +def _lower_llvm_via_backend(kd, *, arch, backend, spec): + assert backend == "python" + args = ", ".join(f"{t} noalias %{n}" if t.startswith("ptr") else f"{t} %{n}" for n, t in kd["params"]) + return (f"; flavor={os.environ.get('ROCKE_LLVM_FLAVOR')} arch={arch}\\n; body={kd['body']}\\n" + f"define amdgpu_kernel void @fake({args}) #0 {{\\n ret void\\n}}\\n") +""" + +LOWER_LLVM = """ +LLVM_FLAVOR_LLVM20 = "llvm20" +LLVM_FLAVOR_LLVM23 = "llvm23" +_LLVM_FLAVOR_LADDER: tuple = ( + (0, LLVM_FLAVOR_LLVM20), + (23, LLVM_FLAVOR_LLVM23), +) +""" + + +def builder_src(params=(), body="v1"): + ps = "".join(f' p.append(("{n}", "{t}"))\n' for n, t in params) + return ( + "import dataclasses\n" + "@dataclasses.dataclass\n" + "class Spec:\n n: int = 1\n boom: bool = False\n\n" + "def build_fake(spec, arch):\n" + " if spec.boom:\n raise ValueError('boom')\n" + ' p = [("q", "ptr addrspace(1)"), ("n", "i32")]\n' + f"{ps}" + f' return {{"params": p, "body": "{body}"}}\n' + ) + + +KDP_VARIANTS = ({"name": "v_a", "spec": {"n": 1}}, {"name": "v_b", "spec": {"n": 2}}) + + +def kdp_json(variants=KDP_VARIANTS, arch=("gfx950",), target=None): + return json.dumps( + { + "arch": list(arch), + "kernelDescriptors": [ + { + "name": v["name"], + **({"arch": v["arch"]} if "arch" in v else {}), + "kernel_source": { + "kind": "rocke", + "source": "kernels/gfx_fake/fake_attn.py", + "builder": "build_fake", + "spec": v["spec"], + **({"target": target} if target else {}), + }, + } + for v in variants + ], + } + ) + + +@pytest.fixture +def emit(): + spec = importlib.util.spec_from_file_location( + "hipdnn_deep_review_impact_emit", SCRIPT + ) + assert spec and spec.loader + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +GENERIC_HELPERS = ( + HELPERS + + """ +def rocke_compile_arch(arch, target="native"): + if target == "native": + return arch + return {"gfx11": "gfx11-generic", "gfx12": "gfx12-generic"}[arch[:5]] +""" +) + + +def tree_files( + params=(), body="v1", variants=KDP_VARIANTS, arch=("gfx950",), target=None +): + """Every file of the fake provider tree, as {repo path: text}.""" + py = f"{PROVIDER}/rocke/platform/python" + files = { + f"{py}/rocke/__init__.py": "", + f"{py}/rocke/helpers/__init__.py": "", + f"{py}/rocke/core/__init__.py": "", + f"{PROVIDER}/rocke/library/kernels/__init__.py": "", + f"{PROVIDER}/rocke/library/kernels/gfx_fake/__init__.py": "", + f"{PROVIDER}/descriptor-packaging/python/hkp_pack/__init__.py": "", + f"{py}/rocke/helpers/compile.py": COMPILE, + f"{py}/rocke/core/lower_llvm.py": LOWER_LLVM, + f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py": HELPERS, + BUILDER: builder_src(params, body), + f"{DESC}/fake.kdp.json": kdp_json(variants, arch, target), + } + return files + + +GEN = "projects/hipdnn/tools/IngestorGenerator" +ENUMERATOR = textwrap.dedent( + """ + import argparse, json, os + p = argparse.ArgumentParser(); p.add_argument("--manifest"); p.add_argument("--out") + a = p.parse_args() + assert os.environ["ROCKE_PROVIDER_ROOT"] + ks = {"source": "kernels/gfx_fake/fake_attn.py", "builder": "build_fake", "spec": {"n": 1}} + open(a.out, "w").write(json.dumps({"packs": [{"arch": ["gfx950"], "kernels": [ + {"name": "enum_a", "kernel_source": ks}]}]})) + """ +) + + +def dvc_files(): + files = tree_files() + del files[f"{DESC}/fake.kdp.json"] + files[f"{DESC}/artifacts.dvc"] = "outs:\n- path: artifacts\n" + return files + + +def scenarios(): + s = {"base": tree_files()} + s["params"] = tree_files(params=[("batch", "i32")]) + s["body"] = tree_files(body="v2") + s["removed"] = tree_files(variants=KDP_VARIANTS[:1]) + s["reordered"] = tree_files(variants=tuple(reversed(KDP_VARIANTS))) + s["bad"] = tree_files( + variants=KDP_VARIANTS + ({"name": "v_bad", "spec": {"boom": True}},) + ) + s["nohelper"] = tree_files() + s["nohelper"][ + f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py" + ] = HELPERS.replace("build_spec", "renamed_build_spec") + s["dvc"] = dvc_files() + s["dvc_enum"] = dvc_files() + s["dvc_enum"][f"{GEN}/configs/fake.engine.yaml"] = "authored_subpath: rocKE/fake\n" + s["dvc_enum"][f"{GEN}/tools/registry_enumerator.py"] = ENUMERATOR + narrowed = (KDP_VARIANTS[0], {**KDP_VARIANTS[1], "arch": ["gfx1100"]}) + s["generic"] = tree_files( + variants=narrowed, arch=("gfx1100", "gfx1101", "gfx1200"), target="generic" + ) + s["generic"][ + f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py" + ] = GENERIC_HELPERS + s["bare"] = {"README.md": "x"} + return s + + +def fast_import_stream(commits): + """A `git fast-import` stream: one commit per scenario, tagged with its name.""" + out = bytearray() + + def data(b): + out.extend(b"data %d\n" % len(b) + b + b"\n") + + for name, files in commits.items(): + out.extend( + f"commit refs/heads/work\ncommitter t 1700000000 +0000\n".encode() + ) + data(name.encode()) + out.extend(b"deleteall\n") + for path, text in files.items(): + out.extend(f"M 100644 inline {path}\n".encode()) + data(text.encode("utf-8")) + out.extend(f"\nreset refs/tags/{name}\nfrom refs/heads/work\n\n".encode()) + return bytes(out) + + +@pytest.fixture(scope="module") +def repo(tmp_path_factory): + """One repo whose tags are the scenarios (a single git process builds all of them).""" + root = tmp_path_factory.mktemp("impact_emit") + cfg = root / "gitconfig" + cfg.write_text("") + mp = pytest.MonkeyPatch() + for k, v in { + "GIT_CONFIG_GLOBAL": str(cfg), + "GIT_CONFIG_NOSYSTEM": "1", + "GIT_AUTHOR_NAME": "t", + "GIT_AUTHOR_EMAIL": "t@example.com", + "GIT_COMMITTER_NAME": "t", + "GIT_COMMITTER_EMAIL": "t@example.com", + }.items(): + mp.setenv(k, v) + r = root / "repo" + subprocess.run(["git", "init", "-q", str(r)], check=True) + subprocess.run( + ["git", "-C", str(r), "fast-import", "--quiet"], + input=fast_import_stream(scenarios()), + check=True, + ) + yield r + mp.undo() + + +def run(emit, repo, base, head, tmp_path, *extra): + out = tmp_path / "out.json" + rc = emit.main( + [ + "run", + "--repo", + str(repo), + "--base", + base, + "--head", + head, + "--out", + str(out), + *extra, + ] + ) + return rc, (json.loads(out.read_text(encoding="utf-8")) if out.exists() else None) + + +def only_family(doc): + assert len(doc["families"]) == 1 + return doc["families"][0] + + +def test_identical_sides_report_no_change(emit, repo, tmp_path): + rc, doc = run(emit, repo, "base", "base", tmp_path) + assert rc == 0 + f = only_family(doc) + assert (f["arch"], f["builder"], f["spec_origin"]) == ( + "gfx950", + "build_fake", + "kdp-in-git", + ) + assert f["kdp"] == f"{DESC}/fake.kdp.json" + assert f["members"] == ["gfx950"] + assert f["variants"] == {"base": 2, "head": 2} + for k in ("only_in_base", "only_in_head", "params_changed", "ir_changed"): + assert f[k] == [], k + assert f["failures"] == {"base": {}, "head": {}} + assert f["params"]["head"] == [["q", "ptr addrspace(1)"], ["n", "i32"]] + assert doc["flavor"] == "llvm23" # top rung of the ladder in the head tree + assert doc["not_inspected"] == [] + + +def test_extra_param_lists_every_variant_and_shows_head_list(emit, repo, tmp_path): + rc, doc = run(emit, repo, "base", "params", tmp_path) + assert rc == 0 + f = only_family(doc) + assert f["params_changed"] == ["v_a", "v_b"] + assert set(f["ir_changed"]) == {"v_a", "v_b"} + assert f["params"]["base"] == [["q", "ptr addrspace(1)"], ["n", "i32"]] + assert f["params"]["head"][-1] == ["batch", "i32"] + + +def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): + _, doc = run(emit, repo, "base", "body", tmp_path) + f = only_family(doc) + assert f["params_changed"] == [] + assert f["ir_changed"] == ["v_a", "v_b"] + + +def test_removed_variant_is_only_in_base_and_unchanged_one_is_not_flagged( + emit, repo, tmp_path +): + _, doc = run(emit, repo, "base", "removed", tmp_path) + f = only_family(doc) + assert f["only_in_base"] == ["v_b"] + assert f["only_in_head"] == [] + assert f["variants"] == {"base": 2, "head": 1} + assert f["params_changed"] == f["ir_changed"] == [] + + +def test_variants_are_matched_by_name_not_position(emit, repo, tmp_path): + _, doc = run(emit, repo, "base", "reordered", tmp_path) + f = only_family(doc) + assert f["only_in_base"] == f["only_in_head"] == f["ir_changed"] == [] + + +def test_raising_builder_is_a_recorded_failure_and_exit_zero(emit, repo, tmp_path): + rc, doc = run(emit, repo, "bad", "bad", tmp_path) + assert rc == 0 + f = only_family(doc) + assert f["variants"] == {"base": 3, "head": 3} + assert f["failures"]["head"] == {"v_bad": "ValueError: boom"} + assert f["failures"]["base"] == {"v_bad": "ValueError: boom"} + assert f["params_changed"] == f["ir_changed"] == [] + + +def test_missing_helper_fails_every_variant_instead_of_skipping(emit, repo, tmp_path): + rc, doc = run(emit, repo, "nohelper", "nohelper", tmp_path) + assert rc == 0 + f = only_family(doc) + assert set(f["failures"]["head"]) == {"v_a", "v_b"} + assert "build_spec" in f["failures"]["head"]["v_a"] + + +def test_dvc_held_kdp_without_enumerator_is_not_inspected(emit, repo, tmp_path): + rc, doc = run(emit, repo, "dvc", "dvc", tmp_path) + assert rc == 0 + assert doc["families"] == [] + assert sorted(n["side"] for n in doc["not_inspected"]) == ["base", "head"] + n = doc["not_inspected"][0] + assert n["kdp"] == f"{DESC}/fake.kdp.json" + assert "registry_enumerator" in n["reason"] + + +def test_dvc_held_kdp_is_regenerated_by_the_enumerator_of_that_side( + emit, repo, tmp_path +): + _, doc = run(emit, repo, "dvc_enum", "dvc_enum", tmp_path) + f = only_family(doc) + assert f["spec_origin"] == "registry_enumerator" + assert f["variants"] == {"base": 1, "head": 1} + assert f["kdp"] == f"{DESC}/fake.kdp.json" + assert doc["not_inspected"] == [] + + +def test_generic_members_collapse_and_descriptor_arch_narrows_the_kdp_arch( + emit, repo, tmp_path +): + rc, doc = run(emit, repo, "generic", "generic", tmp_path) + assert rc == 0 + fams = {f["arch"]: f for f in doc["families"]} + assert set(fams) == {"gfx11-generic", "gfx12-generic"} + g11, g12 = fams["gfx11-generic"], fams["gfx12-generic"] + assert g11["members"] == ["gfx1100", "gfx1101"] + assert g11["variants"] == {"base": 2, "head": 2} # v_a once, not once per member + assert g12["members"] == ["gfx1200"] + assert g12["variants"] == {"base": 1, "head": 1} # v_b is gfx1100-only + assert g11["failures"] == g12["failures"] == {"base": {}, "head": {}} + + +def test_flavor_is_pinned_in_the_child_and_overridable( + emit, repo, tmp_path, monkeypatch +): + monkeypatch.setenv("ROCKE_LLVM_FLAVOR", "llvm20") # caller env must not leak + _, doc = run(emit, repo, "base", "base^{tree}", tmp_path, "--flavor", "llvm22") + assert doc["flavor"] == "llvm22" + assert doc["head"] == "base^{tree}" # a tree id is a valid rev + assert only_family(doc)["ir_changed"] == [] + _, doc = run(emit, repo, "base", "base", tmp_path) + assert doc["flavor"] == "llvm23" + + +def test_unreadable_rev_exits_one_with_one_line(emit, repo, tmp_path, capsys): + rc, doc = run(emit, repo, "base", "no-such-rev", tmp_path) + assert rc == 1 and doc is None + err = capsys.readouterr().err.strip().splitlines() + assert len(err) == 1 and err[0].startswith("impact_emit: ") + + +def test_tree_without_rocke_exits_one(emit, repo, tmp_path, capsys): + rc, _ = run(emit, repo, "bare", "bare", tmp_path, "--flavor", "llvm23") + assert rc == 1 + assert "lacks" in capsys.readouterr().err + + +@pytest.mark.parametrize( + "line, expected", + [ + ( + "define amdgpu_kernel void @attn(ptr addrspace(1) noalias nocapture readonly align 16 %q, " + "ptr addrspace(1) noalias %o, i32 noundef %batch, float %scale, <4 x float> %v, " + "ptr %flat) #1 {", + [ + ["q", "ptr addrspace(1)"], + ["o", "ptr addrspace(1)"], + ["batch", "i32"], + ["scale", "float"], + ["v", "<4 x float>"], + ["flat", "ptr"], + ], + ), + ("define amdgpu_kernel void @k() #0 {", []), + ], +) +def test_parse_kernargs_reads_names_and_types(emit, line, expected): + ir = f"; header\ndeclare i32 @llvm.foo(i32)\n{line}\n ret void\n}}\n" + assert emit.parse_kernargs(ir) == expected + + +def test_parse_kernargs_prefers_the_kernel_over_helper_functions(emit): + ir = "define internal void @helper(i32 %a) {\n}\ndefine amdgpu_kernel void @k(i64 %z) #0 {\n}\n" + assert emit.parse_kernargs(ir) == [["z", "i64"]] + with pytest.raises(ValueError): + emit.parse_kernargs("; no functions\n") + + +@pytest.mark.parametrize( + "source, expected", + [ + ( + 'A = "llvm20"\nB = "llvm23"\n_LLVM_FLAVOR_LADDER = ((0, A), (23, B))\nLLVM_FLAVORS = (A,)\n', + "llvm23", + ), + ('A = "llvm20"\nB = "llvm22"\nLLVM_FLAVORS: tuple = (A, B)\n', "llvm22"), + ], +) +def test_default_flavor_is_the_ladder_top_else_the_newest_known_flavor( + emit, monkeypatch, source, expected +): + # catches: a tree from before the ladder existed failing instead of using its newest flavor + monkeypatch.setattr(emit, "git", lambda *args, **kw: source) + assert emit.default_flavor(".", "rev") == expected diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py index 5218394e343c..6346c8d4975b 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py @@ -471,3 +471,19 @@ def test_packets_drift_lane_follows_the_trigger(part, tmp_path): (rd / "drift.txt").write_text("Target tip: abc\nTrigger: yes\n") run(part, "packets", rd, "--lanes", "DRIFT") assert "drift.txt" in (rd / "packets" / "DRIFT.md").read_text() + + +def test_impact_scope_is_the_ingestor_path_only(part): + inside = ( + "dnn-providers/hip-kernel-provider/rocke/library/kernels/gfx950/attention_dense.py", + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/Descriptors.hpp", + "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json", + "projects/hipdnn/tools/IngestorGenerator/configs/gfx950_attention_dense.engine.yaml", + ) + outside = ( + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/EnginePluginApi.h", + "dnn-providers/miopen-provider/src/a.cpp", + "projects/hipdnn/tools/DescriptorGenerator/gen.py", + ) + rows = [(p, 1, "") for p in inside + outside] + assert {r[0] for r in part.concern_scope("IMPACT", rows)} == set(inside) From db2145e1588cd0e6fa2d6f89655e248e8cd06086 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Thu, 22 Oct 2026 18:39:39 -0600 Subject: [PATCH 02/13] fix(hipdnn): close false-safe paths in the deep review impact lane impact.py could report a change as safe when it was not. A removed Python function lost every reader that still imported it as soon as any other file defined a function of the same name, which per-architecture builders do by design. A code edit to the generic-target header produced no row and rolled up as no surface file. A builder whose argument changed type kept its declared C++ list reported as agreeing, because the pair was compared by name only. And an emitter output file was folded into any report, so a stale file from an earlier head, one family for an unrelated source, or a family that built no variant could turn shipped-module rows inert. A removed symbol now keeps every reader bound to its old module. The generic-target header is a surface file in impact.py and a trigger in partition.py. Declared pairs compare each argument's size and kind as well as its name, resolving types held in module or function variables, and report when types could not be compared. impact_emit.py records the tree ids it archived, impact.py refuses an emit file made for other trees or with a malformed shape, and an all-equal emit folds only rows on modules a built family imports. A builder whose parameters change only in which are conditional, or a declared C++ list that shares too few names to compare, now gets a row instead of silence. A method removed from a base class and read through a subclass or an untyped receiver is reported as changed when the reading file's imports reach the class. The emitter's child runs with an allow-listed environment that passes no credentials, in its own process group so a timeout ends the whole tree, and with the newest LLVM flavor both revisions know. Two variants with one name but different specs are recorded as a failure rather than collapsed. C++ classification follows comment line splices and raw strings with encoding prefixes. A skipped merged view stays changed whenever the change touches the surface, and CMake and requirements files are labelled as left to the BUILD and DEPS lanes in the rows and the rollup instead of reading as no impact. The lane brief documents the emitter failure rule and the remaining blind spots of the symbol check, and the runbook counts the emitter's two workers against the core budget. ALMIOPEN-2834 --- .../ai/skills/hipdnn-deep-review/RUNBOOK.md | 7 +- .../ai/skills/hipdnn-deep-review/SKILL.md | 4 +- .../lanes/runtime-impact.md | 34 +- .../hipdnn-deep-review/scripts/impact.py | 499 +++++++++++++++--- .../hipdnn-deep-review/scripts/impact_emit.py | 195 +++++-- .../hipdnn-deep-review/scripts/partition.py | 4 +- .../tools/ai/tests/test_deep_review_impact.py | 389 +++++++++++++- .../ai/tests/test_deep_review_impact_emit.py | 123 ++++- .../ai/tests/test_deep_review_partition.py | 24 + 9 files changed, 1102 insertions(+), 177 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md index 010f196b1ff7..d5bef0d9e058 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md @@ -157,7 +157,7 @@ its trigger holds, else mark it `N/A`; when unsure, run it. | `DOCS` | [docs](lanes/docs.md) | Accuracy and concision of docs and comments, including edited public headers | A documentation or source file changes | | `DRIFT` | [drift](lanes/drift.md) | The change merged onto the target's current tip: conflicts, broken target-side contracts, tests on the merge | `drift.txt` says `Trigger: yes` | | `RUN` | [build-validate](lanes/build-validate.md) | Building the head and running the relevant tests | Always | -| `IMPACT` | [runtime-impact](lanes/runtime-impact.md) | Kernel ABI and dispatch contracts of the kernel ingestor (kernarg lists, launch surfaces, matchers, descriptors, generic targets) at base, head and the merged tree; which shipped variants change and which device runs would settle it | A file under `dnn-providers/hip-kernel-provider/`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, `projects/hipdnn/tools/IngestorGenerator/` or `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` changes | +| `IMPACT` | [runtime-impact](lanes/runtime-impact.md) | Kernel ABI and dispatch contracts of the kernel ingestor (kernarg lists, launch surfaces, matchers, descriptors, generic targets) at base, head and the merged tree; which shipped variants change and which device runs would settle it | A file under `dnn-providers/hip-kernel-provider/`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, `projects/hipdnn/tools/IngestorGenerator/`, `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` or `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` changes | `ORCH` (process findings you file), `SCOUT`, `SYNTH` ([orient](lanes/orient.md)) and `VER` ([verify](lanes/verify.md)) are not partition choices. Merge two lanes into one agent when the @@ -219,7 +219,8 @@ message. Agents see only their dispatch: each begins by reading `dispatch-contex `IMPACT` (its emitter builds every shipped variant twice), then the longest lanes (largest `CORR`, then `TESTQ`), `DUP` and `DOCS` last. `RUN`, `DRIFT`, an approved `ROCK` build, `ROCK`'s configure diff (`Build host: yes`), or any other lane that builds or mutates, run concurrently in their own `$SCRATCH` trees and -split `Cores:` between them while they overlap, `RUN` taking the larger share. +split `Cores:` between them while they overlap, `RUN` taking the larger share. `IMPACT`'s emitter does not +read `Cores:`: it builds the two sides on two concurrent workers, so count two cores against `Cores:` while it runs. ```markdown Role: | SCOUT | SYNTH | VER- | WRITER; round ; follow-up f if any @@ -251,7 +252,7 @@ waits; the answer resumes the round (*Iterate*, same head). A follow-up dispatch (approved request, decision, Handoff) carries the lane's report path and the item only, and writes `lanes/-f.md`, numbering IDs after the lane's last `p`. -`AUTH`, `TESTQ` and `DRIFT` run in the first fan-out without waiting for `RUN`. When `RUN` returns, dispatch +`AUTH`, `TESTQ`, `DRIFT` and `IMPACT` run in the first fan-out without waiting for `RUN`. When `RUN` returns, dispatch `AUTH-f1` with the `AUTH` and `RUN` report paths: it reconciles the claims and the **Consumers** list against `RUN`'s executed counts ([lanes/author-testing.md](lanes/author-testing.md)). When `TESTQ` wrote `M1: UNVERIFIED`, also dispatch `TESTQ-f1` with the `TESTQ` and `RUN` report paths: it settles M1 from diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md index afa2b12a1b90..60d2ee66bd59 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md @@ -16,8 +16,8 @@ what each agent reads, and `lanes/` holds the briefs. `RUN` builds with [hipdnn-superbuild-test](../hipdnn-superbuild-test/SKILL.md); install those skills alongside this one. Prerequisites: network access, an authenticated `gh` with read access, `git` 2.38 or later (`pin.py` uses `git merge-tree --write-tree`), and Python 3 (it runs the scripts under -`$SKILL/scripts/`); `pre-commit` and `pytest` for the lanes that run validators. The `IMPACT` lane needs Python 3.10 or newer, -PyYAML, and Jinja2 for its emitter. `RUN` and verifiers also need +`$SKILL/scripts/`); `pre-commit` and `pytest` for the lanes that run validators. The `IMPACT` lane's `impact.py` needs +Python 3.10 or newer and PyYAML (it reads the `*.engine.yaml` launch surfaces); its emitter also needs Jinja2. `RUN` and verifiers also need a ROCm install (Linux: `/opt/rocm` or `ROCM_PATH`; Windows: the wheel SDK that [hipdnn-superbuild](../hipdnn-superbuild/SKILL.md) provisions), CMake, Ninja and about 25 GB free in `$SCRATCH`; without them `defect` and `test-gap` findings stay `UNVERIFIED` and the review closes only through human `ACCEPTED-RISK` decisions, recorded with `ledger.py set` (`PIN.md` records `Build host`). diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md index f7f11600b107..050c40f48cc1 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -8,7 +8,9 @@ ingestor, rocKE and SDPA flags ON, whatever their defaults. You judge; two scripts collect facts. `impact.py` reads git objects only. `impact_emit.py` imports and runs rocKE code from the reviewed trees (a `git archive` extract, in a child process -with a timeout and a scrubbed environment): the same exposure as `RUN` building the head. +whose whole process tree is killed at its timeout, with an environment reduced to system, temp, +locale and Python user-site variables, so credentials such as `GH_TOKEN` are not passed): the +same code execution as `RUN` building the head. ## Contracts @@ -18,11 +20,12 @@ with a timeout and a scrubbed environment): the same exposure as `RUN` building | Launch surface | Python grid, block, LDS and tile tables | the `cpp_mirror` files a `launch_surface:` entry names in `projects/hipdnn/tools/IngestorGenerator/configs/*.engine.yaml` | | Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | | Shipped variants | `*.kdp.json` (DVC-held for gfx950; regenerated by the generator's registry enumerator) | loader, native packs | -| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` | packer and loader | +| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` and `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` | packer and loader | | Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | -Test code and CMake are not yours: they are `outside` for status, but tests are still searched -as copies and readers, so a stale pin or a stale import is yours. +Test code, CMake and `requirements*.txt` are not yours: they are `outside` for status (the rollup +names how many files it left to `BUILD` and `DEPS`), but tests are still searched as copies and +readers, so a stale pin or a stale import is yours. ## Method @@ -44,17 +47,23 @@ as copies and readers, so a stale pin or a stale import is yours. digest: ```bash + rm -f "$SCRATCH/IMPACT/emit.json" python3 "$SKILL/scripts/impact_emit.py" run --repo "$CLONE" --base "$BASE" --head "$HEAD" --out "$SCRATCH/IMPACT/emit.json" python3 "$SKILL/scripts/impact.py" report --round-dir . --emit "$SCRATCH/IMPACT/emit.json" > lanes/IMPACT-evidence/report-emit.md ``` - Record the flavor it used. A family listed under `failures` or a KDP under `not_inspected` - leaves its rows `changed`: never read a failure as equal. + The emitter exiting 1 (it could not run, or a side timed out) leaves no proof: return `STOP:` + with its message, and never fold an `emit.json` from an earlier run. `impact.py` refuses an + emit file made for another base or head, or one it cannot parse. Record the flavor it used. A + family listed under `failures` or a KDP under `not_inspected` leaves its rows `changed`: never + read a failure as equal. An all-equal emit folds only the rows of modules a built family + imports. 3. Judge every row whose status is not `inert` or `outside`. Read both sides at `$HEAD` (and the merged tree when the row says `merged-only`) before filing. 4. End **Checked, no finding** with the rollup line the script printed, corrected by your judgment and saying why when it differs: - `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`. + `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`, + keeping any `not judged here:` note the script added. ## From row to finding @@ -62,7 +71,7 @@ as copies and readers, so a stale pin or a stale import is yours. |---|---| | `mismatch` | A `source-claim` at `BLOCKING` ([../policy.md](../policy.md): an ABI break without a migration path). **Where**: the producer line and every disagreeing copy. **Proof**: the command and both quoted lists. A `merged-only` mismatch breaks the target once merged: **Fix** is to rebase and update the named copy. If the same mismatch exists at `$BASE`, it is pre-existing: `FUTURE WORK` at most | | `changed`, dispatch | Read the C++ function and its Python counterpart. A divergence (a constant, a range, a tile set, a mask or layout rule that the other side does not share) is a `source-claim` at `IMPORTANT` or higher, quoting both sides. Consistent: no finding | -| `changed`, emitted code or arguments | Consistent with every copy: no finding. The variants changed on an architecture need a device run there | +| `changed`, emitted code or arguments | Consistent with every copy, names and types: no finding. A row that says `types not compared` or `cannot compare` is not consistency: compare the types and sizes of both lists by hand. The variants changed on an architecture need a device run there | | `changed`, descriptor or generic target | Check the loader's rules for the changed fields (`projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/DescriptorLoader.hpp`); a value the loader rejects or ignores is a finding. A membership change names the architectures whose archives change | | `inert`, `outside` | Count them under **Checked, no finding**; list each `inert` reason once | @@ -83,4 +92,11 @@ requests** and add `AUTH: requires with the ingestor flags ON` un Known gaps: HIP-kind kernels (`ConvNative`, `PointwiseNative`) have their C++ argument lists checked but not their HIP producers; a dynamic C++ signature (`Gfx1151WmmaAttentionNative`) cannot be compared statically; one LLVM flavor is emitted per run, so an edit that changes only -another flavor's lowering reads `inert`. +another flavor's lowering reads `inert`. The symbol check sees a reader only through an import +of the defining module, `m.name` on it, or an attribute read in a file that names the class; it +cannot see `from m import *`, `getattr(m, "f")`, a dotted import read in full (`import a.b.m`, +then `a.b.m.f()`), or a module reached only through +`importlib.import_module(f"...{arch}...")` (the emitter covers that last one for shipped +variants, a bare `report` does not). A method removed from a base class and read only through a +subclass or an untyped receiver, in a file whose imports reach the class's module, gives a +`changed` row, not a `mismatch`: read the receivers. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py index da998163b842..5efd8736aae0 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py @@ -9,13 +9,14 @@ impact.py report ... --emit FILE.json Emits facts, never severity beyond the row statuses and the rollup line. Reads git objects only -(cat-file, diff, ls-tree, grep, merge-tree); the repository and its worktrees are never written -(`merge-tree --write-tree` adds one tree object, no ref, no worktree). Exit 0 when the analysis +(cat-file, diff, ls-tree, grep, merge-tree); no ref, index or worktree is written +(`merge-tree --write-tree` writes the merged trees to the object store). Exit 0 when the analysis ran, whatever it found; exit 1 with one line `impact: ` on stderr when it could not. Views. Each fact is computed at base -> head and, when a target tip differs from base, also at the merged tree `git merge-tree --write-tree ` (before = the tip, after = the merge). A -conflict skips the merged view and says why. A row that holds only in the merged view is marked +conflict skips the merged view and says why; the skip is `changed` when the change touches the +surface or the conflict does. A row that holds only in the merged view is marked `merged-only`: that is the race class (a parallel PR landed a copy the diff cannot see). `--round-dir` reads PIN.md (CLONE, BASE, HEAD, TARGET_TIP); a tip equal to BASE gives no merged view. @@ -24,15 +25,19 @@ (a contract side changed, or static analysis cannot settle it; a surface file not understood is always changed), inert (touched, provably without runtime effect), outside (not on the surface; counted, never dropped). Test files and CMake are outside for status but are still searched as -kernarg copies and as symbol readers. +kernarg copies and as symbol readers. CMake and `requirements*.txt` belong to the BUILD and DEPS +lanes; the rollup names how many it left to them. Rollup (last line of `report`): - Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file) - -`report --emit FILE.json` folds in impact_emit.py output: changed params vs a static C++ list is a -mismatch; changed IR is `changed` for that arch; when every family is equal (nothing changed, -nothing failed, nothing not inspected) the rocKE rows that were `changed` only because a shipped -module changed become `inert`. Static kernarg mismatch rows are never downgraded. + Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected[; not judged here: ...]) + | N/A (no surface file[; not judged here: ...]) + +`report --emit FILE.json` folds in impact_emit.py output (schema 2), which must have been made for +the same base and head trees: changed params vs a static C++ list is a mismatch; changed IR is +`changed` for that arch; when every family is equal (nothing changed, nothing failed, nothing not +inspected) the rows that were `changed` only because a shipped module changed become `inert`, but +only on modules a family with built variants imports. Static kernarg mismatch rows are never +downgraded. """ import argparse @@ -108,7 +113,10 @@ ("generator", GEN + "*"), ("provider", HKP + "*"), ) -OUTSIDE_TAGS = {"carve-out", "test", "cmake", "other"} +OUTSIDE_TAGS = {"carve-out", "test", "cmake", "deps", "other"} +#: Outside tags another deep-review lane judges; the rollup names them so N/A is not read +#: as "no impact" for these files. +OWNER_LANES = {"cmake": "BUILD", "deps": "DEPS"} #: Tags whose files are read as copies or readers although they are outside for status. SEARCHED_TAGS = {"test"} @@ -207,6 +215,8 @@ def __init__(self, repo, oid): self.repo, self.oid = repo, oid self.blobs, self.asts, self.lists, self._under = {}, {}, {}, {} self._paths = None + #: module_index and module_imports caches. + self.stems, self.imports = None, {} @property def paths(self) -> set: @@ -414,16 +424,23 @@ def is_cmake(path: str) -> bool: return PurePosixPath(path).name == "CMakeLists.txt" or path.endswith(".cmake") +def is_requirements(path: str) -> bool: + return re.fullmatch(r"requirements.*\.txt", PurePosixPath(path).name) is not None + + def is_doc(path: str) -> bool: name = PurePosixPath(path).name - return (path.endswith(DOC_SUFFIXES) and not is_cmake(path)) or name == ".gitignore" + return ( + path.endswith(DOC_SUFFIXES) and not is_cmake(path) and not is_requirements(path) + ) or name == ".gitignore" def tag_of(path: str) -> str: - if is_cmake(path) and ( - path.startswith(SEARCH) or path.startswith("projects/hipdnn/") - ): - return "cmake" + if path.startswith(SEARCH) or path.startswith("projects/hipdnn/"): + if is_cmake(path): + return "cmake" + if is_requirements(path): + return "deps" for tag, pattern in SURFACE_TABLE: if fnmatch.fnmatchcase(path, pattern): return tag @@ -449,20 +466,28 @@ def generated_rule(path: str): return None +#: A C++ raw string opener with any encoding prefix: `R"x(`, `u8R"(`, `LR"(`, `uR"(`, `UR"(`. +_RAW = re.compile(r'(?:u8|u|U|L)?R"([^()\\ ]{0,16})\(') + + +def raw_string_end(text: str, i: int): + """End offset of the raw string literal starting at `i`, or None when none starts there.""" + if text[i] not in "RuUL" or (i and (text[i - 1].isalnum() or text[i - 1] == "_")): + return None + match = _RAW.match(text, i) + if not match: + return None + close = text.find(")" + match.group(1) + '"', match.end()) + return len(text) if close < 0 else close + len(match.group(1)) + 2 + + def mask_cpp(text: str, keep_strings: bool = False) -> str: """Comments become spaces and literal contents `x`; length and newlines are preserved.""" out, i, n = [], 0, len(text) - raw = re.compile(r'R"([^()\\ ]{0,16})\(') while i < n: ch = text[i] - match = ( - raw.match(text, i) - if ch == "R" and (i == 0 or not text[i - 1].isalnum()) - else None - ) - if match: - close = text.find(")" + match.group(1) + '"', match.end()) - end = n if close < 0 else close + len(match.group(1)) + 2 + end = raw_string_end(text, i) + if end is not None: seg = text[i:end] out.append(seg if keep_strings else re.sub(r"[^\n]", "x", seg)) i = end @@ -477,9 +502,12 @@ def mask_cpp(text: str, keep_strings: bool = False) -> str: out.append(seg[0] + re.sub(r"[^\n]", "x", seg[1:-1]) + seg[-1]) i += len(seg) elif text.startswith("//", i): + # A backslash before the newline splices the next line into the comment. j = text.find("\n", i) + while j > 0 and text[i:j].rstrip("\r").endswith("\\"): + j = text.find("\n", j + 1) j = n if j < 0 else j - out.append(" " * (j - i)) + out.append(re.sub(r"[^\n]", " ", text[i:j])) i = j elif text.startswith("/*", i): j = text.find("*/", i + 2) @@ -511,14 +539,19 @@ def strip_hash_comments(text: str) -> str: return "\n".join(lines) -def collapse_space(text: str, quotes: str) -> str: +def collapse_space(text: str, quotes: str, raw: bool = False) -> str: """Whitespace runs outside string literals become one space, or one newline when the run - holds one. Literals stay verbatim and tokens never join (`"attn fwd"` != `"attnfwd"`). + holds one. Literals stay verbatim and tokens never join (`"attn fwd"` != `"attnfwd"`); + `raw` keeps C++ raw strings verbatim too, across lines. """ out, i, n = [], 0, len(text) while i < n: ch = text[i] - if ch in quotes: + end = raw_string_end(text, i) if raw else None + if end is not None: + out.append(text[i:end]) + i = end + elif ch in quotes: j = i + 1 while j < n and text[j] != ch and text[j] != "\n": j += 2 if text[j] == "\\" else 1 @@ -631,10 +664,12 @@ def classify(ch: Change) -> str: if base.replace("\r\n", "\n") == head.replace("\r\n", "\n"): return "whitespace-only" if path.endswith(C_SUFFIXES): - if collapse_space(base, "\"'") == collapse_space(head, "\"'"): + if collapse_space(base, "\"'", raw=True) == collapse_space( + head, "\"'", raw=True + ): return "whitespace-only" - if collapse_space(strip_c_comments(base), "\"'") == collapse_space( - strip_c_comments(head), "\"'" + if collapse_space(strip_c_comments(base), "\"'", raw=True) == collapse_space( + strip_c_comments(head), "\"'", raw=True ): return "comment-only" elif is_cmake(path): @@ -980,6 +1015,57 @@ def cpp_arg_size(arg: str): return _SIZEOF.get(m.group(1).replace(" ", "")) if m else None +#: Byte size of a rocKE scalar type named in `b.param`; `PtrType(...)` is a pointer. +_BUILDER_SIZES = {"I8": 1, "U8": 1, "I16": 2, "U16": 2, "F16": 2, "BF16": 2, "I32": 4, + "U32": 4, "F32": 4, "I64": 8, "U64": 8, "F64": 8} # fmt: skip + + +def decl_size(decl: str): + """Size of the type a `b.param` declaration names first: `ptr`, bytes, or None.""" + args = split_args(decl) if decl else [] + first = args[0].strip() if args else "" + if first.startswith("PtrType(") or first == "PTR": + return "ptr" + return _BUILDER_SIZES.get(first) + + +def size_text(size) -> str: + return "pointer" if size == "ptr" else f"{size}-byte" + + +def name_bindings(statements) -> dict: + """Name -> source text of the simple `NAME = expr` assignments among `statements`.""" + out = {} + for node in statements: + if ( + isinstance(node, ast.Assign) + and len(node.targets) == 1 + and isinstance(node.targets[0], ast.Name) + ): + out[node.targets[0].id] = ast.unparse(node.value) + elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name): + if node.value is not None: + out[node.target.id] = ast.unparse(node.value) + return out + + +def resolved_decls(tree, qual: str, decls: list) -> list: + """`decls` with the module's and the function's own `NAME = expr` bindings substituted + once, so `b.param("scale", SC)` changes when `SC = F16` becomes `SC = I64`.""" + if tree is None: + return decls + scope = name_bindings(tree.body) + fn = function_nodes(tree).get(qual) + if fn is not None: + scope.update(name_bindings(fn.body)) + if not scope: + return decls + word = re.compile( + r"\b(" + "|".join(sorted(map(re.escape, scope), key=len, reverse=True)) + r")\b" + ) + return [word.sub(lambda m: scope[m.group(1)], d) for d in decls] + + def cpp_kernel_lists(text: str) -> list: """Every `std::vector`-returning function: one entry per static list (a multi-launch pack holds several, each its own consumer), or one dynamic entry. @@ -1276,6 +1362,18 @@ class Closure: shipped: dict = field(default_factory=dict) #: archs whose descriptors sit behind DVC: their kernel modules are roots by directory. opaque: set = field(default_factory=set) + #: module -> [(imported module, line, eager)], ancestor `__init__.py` files included. + graph: dict = field(default_factory=dict) + + def reach(self, start: str) -> set: + """Every module that importing `start` loads.""" + seen, stack = set(), [start] + while stack: + current = stack.pop() + if current not in seen: + seen.add(current) + stack += [t for t, _, _ in self.graph.get(current, ())] + return seen def build_closure(tree: Tree) -> Closure: @@ -1344,7 +1442,7 @@ def build_closure(tree: Tree) -> Closure: roots.append( (f"{path}:{line}", target, None, path.startswith(PRODUCTION_DIRS)) ) - graph, body = {}, {} + graph, body = out.graph, {} frontier = sorted({t for _, t, _, _ in roots}) while frontier: tree.read(frontier) @@ -1556,14 +1654,18 @@ def file_row(ctx: Ctx, ch: Change): 0, path, "outside", - f"{tag}: not on the runtime-integration surface", + ( + f"{tag}: judged by the {OWNER_LANES[tag]} lane, not by this report" + if tag in OWNER_LANES + else f"{tag}: not on the runtime-integration surface" + ), ) if cls == "docs": return make_row(kind, path, 0, path, "outside", "documentation") if cls in ("comment-only", "whitespace-only", "data-equal"): return make_row(kind, path, 0, path, "inert", cls) arch = arch_of(path) - if tag in ("descriptor", "generic-targets") or cls == "dvc-pointer": + if tag == "descriptor" or path == GENERIC_TARGETS_JSON or cls == "dvc-pointer": return None # data rows carry these files if cls == "generated": sources = generated_rule(path) @@ -1620,6 +1722,7 @@ def file_row(ctx: Ctx, ch: Change): reason = { "native-pack": "native pack code changed", "sdk-ingestor": "ingestor SDK header changed", + "generic-targets": "generic-target header changed", "kpack-runtime": "kpack runtime changed", "packer": "packer code changed", "generator": "ingestor generator changed", @@ -1704,10 +1807,19 @@ def kernarg_rows(ctx: Ctx) -> list: arch=arch, foldable=True)) # fmt: skip elif old.names != new.names: work.append(Work(ch, qual, old, new, new.lineno)) - elif old.decls != new.decls: + elif old.conditional != new.conditional: + rows.append(make_row("kernargs", ch.path, new.lineno, qual, "changed", + f"b.param list of {qual} keeps its names but not which are conditional: " + f"{old.marked()} -> {new.marked()}; a variant may now pass a different list", + arch=arch, foldable=True)) # fmt: skip + else: + before = resolved_decls(bt, qual, old.decls) + after = resolved_decls(at, qual, new.decls) + if before == after: + continue diff = [ f"{n}({b or '-'} -> {a or '-'})" - for n, b, a in zip(new.names, old.decls, new.decls) + for n, b, a in zip(new.names, before, after) if b != a ] rows.append(make_row("kernargs", ch.path, new.lineno, qual, "changed", @@ -1765,12 +1877,24 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: producer is not None and producer.cls in MOVING, ) ] + moved = (producer is not None and producer.cls in MOVING) or ( + mirror is not None and mirror.cls in MOVING + ) + sizes = dict( + zip( + pl.names, + map(decl_size, resolved_decls(tree, surface.function, pl.decls)), + ) + ) + compared, closest = False, 0 + arch = surface.arch or arch_of(surface.cpp_mirror) for entry in cpp_kernel_lists(text): key = (surface.cpp_mirror, entry["line"]) if key in reported: + compared = True continue - arch = surface.arch or arch_of(surface.cpp_mirror) if entry["dynamic"] or entry["unnamed"]: + compared = True if mirror is not None and mirror.cls in MOVING: kind = "dynamic" if entry["dynamic"] else "static but unnamed" rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", @@ -1780,18 +1904,41 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: if not related(entry["names"], pl.names) and not compatible( entry["names"], pl ): + closest = max(closest, len(set(entry["names"]) & set(pl.names))) continue - if compatible(entry["names"], pl): - if (producer is not None and producer.cls in MOVING) or ( - mirror is not None and mirror.cls in MOVING - ): - rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", - "changed", f"{entry['func']}() agrees with {surface.function}", counter, arch=arch)) # fmt: skip - else: + compared = True + if not compatible(entry["names"], pl): rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", "mismatch", f"static list {entry['func']}() disagrees with builder {surface.function} " f"({list_diff(entry['names'], pl.names)}): {entry['names']} vs {pl.marked()}", counter, arch=arch)) # fmt: skip + continue + differ, unknown = [], [] + for name, size in zip(entry["names"], entry["sizes"]): + want = sizes.get(name) + if size is None or want is None: + unknown.append(name) + elif size != want: + differ.append( + f"{name}: builder {size_text(want)}, C++ {size_text(size)}" + ) + if differ: + rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", + "mismatch", f"static list {entry['func']}() names agree with builder {surface.function} " + f"but types do not: {'; '.join(differ[:4])}", counter, arch=arch)) # fmt: skip + elif moved: + if not unknown: + note = "names and types agree" + elif len(unknown) == len(entry["names"]): + note = "names agree; types not compared" + else: + note = f"names agree; types not compared for {', '.join(unknown)}" + rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", + "changed", f"{entry['func']}() vs {surface.function}: {note}", counter, arch=arch)) # fmt: skip + if not compared and moved: + rows.append(make_row("kernargs", surface.cpp_mirror, 0, f"declared:{surface.function}", + "changed", f"no static list in the C++ mirror shares two names with {surface.function} " + f"(closest shares {closest}): cannot compare", counter, arch=arch)) # fmt: skip return rows @@ -1911,6 +2058,63 @@ def in_module(path: str, stem: str) -> bool: return any(path == f or path.endswith("/" + f) for f in files) +def module_index(tree: Tree) -> dict: + """Import stem -> repo path of every Python file, keyed as `in_module` matches: the exact + `/a/b/m`, and each trailing `a/b/m`, `b/m`, `m` (a package is its `__init__.py`).""" + if tree.stems is None: + tree.stems = {} + for path in sorted(p for p in tree.paths if p.endswith(".py")): + stem = path[:-3].removesuffix("/__init__") + tree.stems.setdefault("/" + stem, path) + parts = stem.split("/") + for i in range(len(parts)): + tree.stems.setdefault("/".join(parts[i:]), path) + return tree.stems + + +def module_imports(tree: Tree, path: str) -> set: + """Repo files that importing `path` loads directly: `import a.b.c` loads `a`, `a.b` and + `a.b.c`; `from m import x` loads `m` and, when `x` is a submodule, `m/x`.""" + if path not in tree.imports: + module, index, out = tree.ast(path), module_index(tree), set() + for node in ast.walk(module) if module is not None else (): + if isinstance(node, ast.Import): + for alias in node.names: + parts = alias.name.split(".") + out |= { + index.get("/".join(parts[:i])) for i in range(1, len(parts) + 1) + } + for _, stem, imported, _ in ( + iter_imports(path, module) if module is not None else () + ): + if imported is not None: + out |= {index.get(stem), index.get(f"{stem}/{imported}")} + tree.imports[path] = out - {None, path} + return tree.imports[path] + + +def modules_reaching(tree: Tree, starts, target: str) -> set: + """The `starts` whose imports, followed transitively, load `target`: one shared search + with batched reads, then a reverse walk from `target`.""" + graph, frontier = {}, sorted(set(starts)) + while frontier: + tree.read([p for p in frontier if p not in tree.imports]) + for path in frontier: + graph[path] = module_imports(tree, path) + frontier = sorted({t for p in frontier for t in graph[p]} - graph.keys()) + importers = {} + for path, targets in graph.items(): + for t in targets: + importers.setdefault(t, []).append(path) + seen, stack = {target}, [target] + while stack: + for path in importers.get(stack.pop(), ()): + if path not in seen: + seen.add(path) + stack.append(path) + return set(starts) & seen + + def symbol_references(tree, path: str, defining: str, name: str, is_method: bool): """[(line, node)] of the AST reads of `name`, a symbol of `defining`. A method or field is an attribute load; a function or class is an import of the name from the defining module, @@ -2154,8 +2358,7 @@ def reader_rows(ctx: Ctx, cands: list) -> list: for b, bl, h, hl in ch.hunks if b <= entry[2] and entry[1] <= b + max(bl, 1) - 1 ] - moved_elsewhere = False - refs, calls = [], [] + refs, calls, loose = [], [], [] for path in mentioning: tree = ctx.after.ast(path) text = ctx.after.text(path) or "" @@ -2172,21 +2375,18 @@ def reader_rows(ctx: Ctx, cands: list) -> list: if is_method and not re.search( r"\b" + re.escape(qual.split(".")[0]) + r"\b", text ): - continue - if ( - path != ch.path - and not is_method - and kind == "removed" - and any( - isinstance( - n, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef) + # `.name` read through a subclass or an untyped receiver: the class + # cannot be told, so it can only make the removal `changed`. + loose += [ + cp(path, line, False) + for line, _ in symbol_references( + tree, path, ch.path, name, True ) - and n.name == name - for n in tree.body - ) - ): - moved_elsewhere = True - break + ] + continue + # Only reads bound to the old module count (an import from it, `m.name` on + # it, or an unbound read in the file itself), so a same-named def elsewhere + # neither hides a stale importer nor turns an unrelated file into a reader. found = symbol_references(tree, path, ch.path, name, is_method) other = ctx.changes.get(path) for line, node in found: @@ -2195,9 +2395,22 @@ def reader_rows(ctx: Ctx, cands: list) -> list: refs.append(cp(path, line, other is not None and line in other.added)) if node is not None: calls.append((path, tree, node)) - if moved_elsewhere or not refs: - continue arch = arch_of(ch.path) + if not refs: + # Only a method of a class that survives (a removed class has its own row; a + # field's `.name` is too common a word to stand for a reader), read in a file + # whose imports reach the defining module. + survives = qual.split(".")[0] in module_bindings(ctx.after.ast(ch.path)) + if kind == "removed" and entry[0] == "def" and survives and loose: + reaching = modules_reaching( + ctx.after, {c["path"] for c in loose}, ch.path + ) + loose = [c for c in loose if c["path"] in reaching] + if loose and kind == "removed" and entry[0] == "def" and survives: + rows.append(make_row("symbols", ch.path, entry[1], qual, "changed", + f"{qual} is removed; {len(loose)} `.{name}` read(s) in files that never name " + f"{qual.split('.')[0]}: their receiver's class cannot be told", loose, arch=arch)) # fmt: skip + continue if kind == "removed": what = "module" if entry[0] == "module" else qual rows.append(make_row("symbols", ch.path, entry[1], qual, "mismatch", @@ -2548,13 +2761,79 @@ def load_emit(path) -> dict: raise rl.Fail(f"cannot read emit file {path}: {exc}") from None if ( not isinstance(data, dict) - or data.get("schema") != 1 + or data.get("schema") != 2 or data.get("tool") != "impact_emit" ): - raise rl.Fail(f"{path} is not impact_emit schema 1 output") + raise rl.Fail(f"{path} is not impact_emit schema 2 output") + problem = emit_shape_problem(data) + if problem: + raise rl.Fail(f"emit file {path}: {problem}") return data +def emit_shape_problem(data: dict): + """Why an impact_emit document cannot be folded, or None: every field the fold reads + must have its type, so a malformed file fails instead of crashing or folding.""" + + def strings(value): + return isinstance(value, list) and all(isinstance(v, str) for v in value) + + def count(value): + return isinstance(value, int) and not isinstance(value, bool) and value >= 0 + + def params(value): + return value is None or ( + isinstance(value, list) + and all(isinstance(p, list) and len(p) == 2 and strings(p) for p in value) + ) + + for key in ("base_tree", "head_tree"): + if not ( + isinstance(data.get(key), str) + and re.fullmatch(r"[0-9a-f]{40,64}", data[key]) + ): + return f"`{key}` is not a tree id" + if not isinstance(data.get("families"), list): + return "`families` is not a list" + skipped = data.get("not_inspected") + if not isinstance(skipped, list) or not all(isinstance(i, dict) for i in skipped): + return "`not_inspected` is not a list of objects" + for n, fam in enumerate(data["families"]): + if not isinstance(fam, dict): + return f"family {n} is not an object" + if not all( + isinstance(fam.get(k), str) and fam[k] + for k in ("arch", "source", "builder") + ): + return f"family {n} lacks arch, source or builder" + variants = fam.get("variants") + if not ( + isinstance(variants, dict) + and count(variants.get("base")) + and count(variants.get("head")) + ): + return f"family {n}: `variants` lacks base and head counts" + lists = ("only_in_base", "only_in_head", "params_changed", "ir_changed") + if not all(strings(fam.get(k)) for k in lists): + return f"family {n}: {', '.join(lists)} must be lists of names" + failures, built = fam.get("failures"), fam.get("params") + if not ( + isinstance(failures, dict) + and all(isinstance(failures.get(s), dict) for s in ("base", "head")) + ): + return f"family {n}: `failures` lacks base and head objects" + if not ( + isinstance(built, dict) + and all(params(built.get(s)) for s in ("base", "head")) + ): + return f"family {n}: `params` is not base and head [name, type] lists" + if not isinstance(fam.get("kdp", ""), str) or not isinstance( + fam.get("spec_origin", ""), str + ): + return f"family {n}: `kdp` or `spec_origin` is not a string" + return None + + def emit_agrees(params, entry): """None when the head params and a static C++ list agree, else why they do not.""" names = [p[0] for p in params] @@ -2567,23 +2846,43 @@ def emit_agrees(params, entry): return None +def emit_source(ctx: Ctx, source: str) -> str: + """Repo path of a family's `source`: descriptors name it relative to the rocKE library + (`kernels/gfx950/attn.py`); an unresolvable source is returned as given.""" + for tree in (ctx.after, ctx.before): + if source in tree.paths: + return source + known = { + p for p in tree.paths if p.endswith(".py") and p.startswith(IMPORT_ROOTS) + } + found = resolve_path_literal(source, known) + if found: + return found + return source + + def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: """Apply impact_emit output to the base..head view's rows (see the module docstring).""" - families = emit.get("families") or [] + families = emit["families"] out = [] surfaces = ctx.surfaces("after") - equal = bool(families) and not emit.get("not_inspected") + equal = bool(families) and not emit["not_inspected"] + #: modules a built variant imports: only rows on these paths can be proven equal. + covered = set() for fam in families: - arch, builder = fam.get("arch"), fam.get("builder") + arch, builder = fam["arch"], fam["builder"] quiet = not any( - fam.get(k) + fam[k] for k in ("params_changed", "ir_changed", "only_in_base", "only_in_head") ) - failures = fam.get("failures") or {} - if not quiet or any(failures.get(side) for side in ("base", "head")): + failures = fam["failures"] + if not quiet or any(failures[side] for side in ("base", "head")): equal = False - params = (fam.get("params") or {}).get("head") - source = fam.get("source", "") + params = fam["params"]["head"] + source = emit_source(ctx, fam["source"]) + if fam["variants"]["head"] > 0: + for side in ("before", "after"): + covered |= ctx.closure(side).reach(source) if fam.get("params_changed") and params: entries = [] for s in surfaces: @@ -2646,7 +2945,9 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: dvc_dirs = [ str(PurePosixPath(f["kdp"]).parent) + "/" for f in families - if f.get("spec_origin") == "registry_enumerator" and f.get("kdp") + if f.get("spec_origin") == "registry_enumerator" + and f.get("kdp") + and f["variants"]["head"] > 0 ] for row in rows: if ( @@ -2666,7 +2967,7 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: status="inert", reason="catalog regenerated: every shipped variant equal", ) - else: + elif row["path"] in covered: row.update( status="inert", reason="emitted IR and params equal for every shipped variant", @@ -2687,11 +2988,20 @@ def rollup(rows: list) -> str: if changed: archs = sorted({a.strip() for r in changed for a in r["arch"].split(",")}) return "Runtime impact: NEEDS-DEVICE " + ", ".join(archs) + left = {} + for r in rows: + lane = OWNER_LANES.get(tag_of(r["path"])) if r["kind"] == "file" else None + if lane and r["status"] == "outside": + left.setdefault(lane, set()).add(r["path"]) + note = ( + "; not judged here: " + + ", ".join(f"{len(p)} file(s) for {lane}" for lane, p in sorted(left.items())) + if left + else "" + ) if live: - return ( - f"Runtime impact: NONE ({len({r['path'] for r in live})} files inspected)" - ) - return "Runtime impact: N/A (no surface file)" + return f"Runtime impact: NONE ({len({r['path'] for r in live})} files inspected{note})" + return f"Runtime impact: N/A (no surface file{note})" def anchor(row) -> str: @@ -2771,6 +3081,15 @@ def run(args): if base_tree_id == head_tree_id: raise rl.Fail("empty diff") base_tree, head_tree = Tree(repo, base_tree_id), Tree(repo, head_tree_id) + emit = None + if args.cmd == "report" and args.emit: + emit = load_emit(args.emit) + made_for = (emit["base_tree"], emit["head_tree"]) + if made_for != (base_tree_id, head_tree_id): + raise rl.Fail( + f"emit file {args.emit} was made for trees {made_for[0][:12]}..{made_for[1][:12]}, " + f"not {base_tree_id[:12]}..{head_tree_id[:12]}; run impact_emit.py on this base and head" + ) views = [ dict( label="base..head", @@ -2798,13 +3117,21 @@ def run(args): views.append(view) if tree_id is None: view.update(status="skipped", reason=why) - surface_hit = False - if why.startswith("merge conflict in "): - surface_hit = any( - tag_of(p.strip()) not in OUTSIDE_TAGS for p in why[18:].split(",") + # Skipping the merged view loses the race check for every surface file of the + # change, wherever the conflict sits; a conflicted test is also a searched copy. + matters = any(r["status"] != "outside" for r in rows) + if ( + why.startswith("merge conflict in ") + and why != "merge conflict in unknown files" + ): + matters = matters or any( + tag_of(p.strip()) not in OUTSIDE_TAGS - SEARCHED_TAGS + for p in why[18:].split(",") ) + else: + matters = True per_view.append([ - dict(make_row("views", "(merged view)", 0, "merged", "changed" if surface_hit or not why.startswith("merge conflict in ") else "outside", + dict(make_row("views", "(merged view)", 0, "merged", "changed" if matters else "outside", f"merged view skipped: {why}; a race with the target cannot be excluded"), view="merged", merged_only=True) ]) # fmt: skip @@ -2814,8 +3141,8 @@ def run(args): ) view["files"] = len(mctx.changes) per_view.append(mrows) - if args.cmd == "report" and args.emit: - per_view[0] = fold_emit(ctx, per_view[0], load_emit(args.emit)) + if emit is not None: + per_view[0] = fold_emit(ctx, per_view[0], emit) return ctx, views, merge_views(per_view) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py index 1635c09fe6a1..99513b546152 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py @@ -6,7 +6,7 @@ run --repo DIR --base REV --head REV [--flavor F] [--timeout S] --out FILE.json REV is any commit or tree id. Each side is `git archive`d into a temp dir (the repo is -only read) and a child interpreter with a scrubbed environment builds every shipped rocKE +only read) and a child interpreter with an allow-listed environment builds every shipped rocKE variant there, lowers it to LLVM IR with the Python backend (no GPU, comgr or network), and records the kernarg list parsed from the IR `define` line plus the SHA-256 of the IR text. This EXECUTES rocKE code from the reviewed revisions, like building the head does. @@ -22,8 +22,13 @@ differs, so it includes `params_changed`. A variant that fails to build is recorded under `failures` and takes no part in the comparison. -The flavor defaults to the top rung of `_LLVM_FLAVOR_LADDER` in the head's -`rocke/platform/python/rocke/core/lower_llvm.py`. Exit 0 when the analysis ran, whatever +The flavor defaults to the newest flavor the head and the base both know (the top rung +of `_LLVM_FLAVOR_LADDER` in `rocke/platform/python/rocke/core/lower_llvm.py`, else the last +of `LLVM_FLAVORS`); with none shared it is the head's newest and the base fails loudly. +The child gets an allow-listed environment and runs in its own process group, which +`--timeout` kills as a whole. Each side's tree id (`base_tree`, `head_tree`, resolved once +with `git rev-parse REV^{tree}` and the one that is archived) is recorded so impact.py +can refuse a file made for other revisions. Exit 0 when the analysis ran, whatever it found; 1 with one line `impact_emit: ` on stderr when it could not run. """ @@ -35,6 +40,7 @@ import os import re import shutil +import signal import subprocess import sys import tarfile @@ -55,7 +61,6 @@ REQUIRED_TREES = (ROCKE_PLATFORM, ROCKE_LIBRARY, PACKAGING) OPTIONAL_TREES = (DESCRIPTORS, GENERATOR) -SCRUBBED_PREFIXES = ("ROCKE_", "HIP_", "ROCM_", "PYTHON") class Fatal(Exception): @@ -77,17 +82,25 @@ def git(repo, *args, text=True): return r.stdout.decode("utf-8", "replace") if text else r.stdout +def resolve_tree(repo, rev): + """The tree id REV names (a commit or a tree); archived and recorded so that a + consumer can tell the file belongs to the revisions it asked about.""" + return git(repo, "rev-parse", "--verify", f"{rev}^{{tree}}").strip() + + def existing_paths(repo, rev, paths): out = git(repo, "ls-tree", "--name-only", rev, "--", *paths) return set(out.splitlines()) -def extract(repo, rev, dest): - """`git archive` the trees needed from REV into DEST (regular files and dirs only).""" +def extract(repo, rev, dest, name=None): + """`git archive` the trees needed from REV into DEST (regular files and dirs only). + NAME is how errors refer to REV.""" + name = name or rev have = existing_paths(repo, rev, REQUIRED_TREES + OPTIONAL_TREES) missing = [p for p in REQUIRED_TREES if p not in have] if missing: - raise Fatal(f"{rev} lacks {', '.join(missing)}") + raise Fatal(f"{name} lacks {', '.join(missing)}") data = git( repo, "archive", @@ -102,7 +115,7 @@ def extract(repo, rev, dest): for m in tf: target = (root / m.name).resolve() if root != target and root not in target.parents: - raise Fatal(f"{rev}: archive member escapes the extract: {m.name}") + raise Fatal(f"{name}: archive member escapes the extract: {m.name}") if m.isdir(): target.mkdir(parents=True, exist_ok=True) elif m.isfile(): @@ -110,15 +123,15 @@ def extract(repo, rev, dest): target.write_bytes(tf.extractfile(m).read()) -def default_flavor(repo, rev): - """Top rung of `_LLVM_FLAVOR_LADDER` in REV's lower_llvm.py, parsed without importing. - Older trees have no ladder; their newest flavor is the last entry of `LLVM_FLAVORS`. +def _known_flavors(src, where): + """Flavors of one lower_llvm.py source, oldest first, parsed without importing: the + rungs of `_LLVM_FLAVOR_LADDER` by major version; older trees have no ladder, so + their `LLVM_FLAVORS` in order. """ - src = git(repo, "show", f"{rev}:{LOWER_LLVM}") try: tree = ast.parse(src) except SyntaxError as e: - raise Fatal(f"cannot parse {LOWER_LLVM} at {rev}: {e}; pass --flavor") + raise Fatal(f"cannot parse {LOWER_LLVM} at {where}: {e}; pass --flavor") consts, ladder, flavors = {}, None, None for node in tree.body: if isinstance(node, ast.Assign): @@ -143,9 +156,9 @@ def default_flavor(repo, rev): for e in names ] if known and isinstance(known[-1], str): - return known[-1] + return [k for k in known if isinstance(k, str)] raise Fatal( - f"no _LLVM_FLAVOR_LADDER or LLVM_FLAVORS in {LOWER_LLVM} at {rev}; pass --flavor" + f"no _LLVM_FLAVOR_LADDER or LLVM_FLAVORS in {LOWER_LLVM} at {where}; pass --flavor" ) rungs = [] for el in ladder.elts: @@ -161,8 +174,28 @@ def default_flavor(repo, rev): if isinstance(major, ast.Constant) and isinstance(flavor, str): rungs.append((major.value, flavor)) if not rungs: - raise Fatal(f"cannot resolve _LLVM_FLAVOR_LADDER at {rev}; pass --flavor") - return max(rungs)[1] + raise Fatal(f"cannot resolve _LLVM_FLAVOR_LADDER at {where}; pass --flavor") + return [flavor for _, flavor in sorted(rungs)] + + +def default_flavor(repo, rev): + """Newest flavor of REV: the top ladder rung, else the last entry of `LLVM_FLAVORS`.""" + src = git(repo, "show", f"{rev}:{LOWER_LLVM}") + return _known_flavors(src, rev)[-1] + + +def shared_default_flavor(repo, base, head): + """The head's newest flavor that the base also knows, so a base from before the head's + top rung is not failed wholesale. With none shared, the head's top (it then fails + loudly); a base whose flavors cannot be read leaves the head's top as well.""" + head_known = _known_flavors(git(repo, "show", f"{head}:{LOWER_LLVM}"), head) + try: + base_known = set( + _known_flavors(git(repo, "show", f"{base}:{LOWER_LLVM}"), base) + ) + except Fatal: + return head_known[-1] + return next((f for f in reversed(head_known) if f in base_known), head_known[-1]) # ----------------------------------------------------------------------- IR parse @@ -246,7 +279,7 @@ def parse_kernargs(ir_text): # -------------------------------------------------------------------------- child -# Everything below up to `parent side` runs in the extract's scrubbed interpreter. +# Everything below up to `parent side` runs in the extract's isolated interpreter. def _arch_list(value): @@ -340,14 +373,10 @@ def _regenerate(base, dvc_dir): out = base / ".impact_emit" / f"{dvc_dir.name}.enum.yaml" out.parent.mkdir(exist_ok=True) try: - r = subprocess.run( + r = run_tree( [sys.executable, str(enum), "--manifest", str(manifest), "--out", str(out)], - capture_output=True, - text=True, - encoding="utf-8", - errors="replace", + int(os.environ.get("IMPACT_EMIT_ENUM_TIMEOUT", "600")), cwd=str(base), - timeout=int(os.environ.get("IMPACT_EMIT_ENUM_TIMEOUT", "600")), ) except subprocess.TimeoutExpired: return None, "registry_enumerator.py timed out" @@ -454,7 +483,7 @@ def child_main(root, out): t0 = time.time() variants, skipped = _discover(root) h = _Helpers() - fams = {} + fams, specs = {}, {} # One family per (compile arch, source, builder): the packer compiles once per # compile arch, so shard arches that share a generic target collapse into one family. for v in variants: @@ -484,8 +513,15 @@ def child_main(root, out): if shard not in fam["members"]: fam["members"].append(shard) if v["name"] in fam["names"]: - continue # same variant reached through another member arch + # same variant reached through another member arch, unless the specs differ + if specs[key, v["name"]] != v["spec"]: + fam["built"].pop(v["name"], None) + fam["failures"][ + v["name"] + ] = f"{v['name']}: two variants share this name with different specs" + continue fam["names"].append(v["name"]) + specs[key, v["name"]] = v["spec"] if err: fam["failures"][v["name"]] = err continue @@ -505,12 +541,22 @@ def child_main(root, out): # ------------------------------------------------------------------- parent side -def scrubbed_env(root, flavor): - env = { - k: v - for k, v in os.environ.items() - if not k.upper().startswith(SCRUBBED_PREFIXES) - } +#: System, temp and locale variables, plus where Python finds user-site packages (`pip +#: install --user` of Jinja2 or PyYAML): APPDATA on Windows, PYTHONUSERBASE anywhere. +_ENV_KEEP = frozenset( + "PATH PATHEXT SYSTEMROOT SYSTEMDRIVE WINDIR COMSPEC TEMP TMP TMPDIR HOME " + "USERPROFILE APPDATA LOCALAPPDATA PYTHONUSERBASE LANG TZ".split() +) +_ENV_KEEP_PREFIXES = ("LC_", "IMPACT_EMIT_") + + +def child_env(root, flavor): + """The child's environment: an allow-list of the parent's, then the pinned variables.""" + env = {} + for k, v in os.environ.items(): + name = k.upper() if os.name == "nt" else k + if name in _ENV_KEEP or name.startswith(_ENV_KEEP_PREFIXES): + env[k] = v base = Path(root) env["PYTHONPATH"] = os.pathsep.join( str(base / p) for p in (ROCKE_PLATFORM, ROCKE_LIBRARY, PACKAGING) @@ -522,10 +568,65 @@ def scrubbed_env(root, flavor): return env -def run_side(repo, rev, flavor, timeout, label): +def _kill_tree(proc): + if os.name == "nt": + try: + subprocess.run( + ["taskkill", "/F", "/T", "/PID", str(proc.pid)], capture_output=True + ) + except OSError: + pass + else: + try: + os.killpg(proc.pid, signal.SIGKILL) + except OSError: + pass + try: + proc.kill() # the leader, in case the tree kill did not reach it + except OSError: + pass + + +def run_tree(cmd, timeout, **kw): + """subprocess.run with captured text output that, on timeout, kills the child's whole + process tree and raises TimeoutExpired without waiting for pipes a grandchild holds. + """ + if os.name == "nt": + kw["creationflags"] = kw.get("creationflags", 0) | ( + subprocess.CREATE_NEW_PROCESS_GROUP + ) + else: + kw["start_new_session"] = True + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + encoding="utf-8", + errors="replace", + **kw, + ) + try: + out, err = proc.communicate(timeout=timeout) + except subprocess.TimeoutExpired: + _kill_tree(proc) + try: + proc.wait(timeout=10) + proc.communicate(timeout=5) + except subprocess.TimeoutExpired: + pass # give up on pipes some survivor still holds + raise + except BaseException: + _kill_tree(proc) + proc.wait() + raise + return subprocess.CompletedProcess(cmd, proc.returncode, out, err) + + +def run_side(repo, rev, tree, flavor, timeout, label): tmp = tempfile.mkdtemp(prefix="impact_emit_") try: - extract(repo, rev, tmp) + extract(repo, tree, tmp, rev) out = Path(tmp) / ".impact_emit_side.json" cmd = [ sys.executable, @@ -537,16 +638,7 @@ def run_side(repo, rev, flavor, timeout, label): str(out), ] try: - r = subprocess.run( - cmd, - capture_output=True, - text=True, - encoding="utf-8", - errors="replace", - cwd=tmp, - env=scrubbed_env(tmp, flavor), - timeout=timeout, - ) + r = run_tree(cmd, timeout, cwd=tmp, env=child_env(tmp, flavor)) except subprocess.TimeoutExpired: raise Fatal(f"{label} ({rev}) timed out after {timeout}s") if r.returncode or not out.is_file(): @@ -615,11 +707,14 @@ def cmd_run(a): repo = Path(a.repo) if not repo.is_dir(): raise Fatal(f"--repo {a.repo} is not a directory") - flavor = a.flavor or default_flavor(repo, a.head) + # Resolve each side once; the same ids are archived and recorded. + base_tree = resolve_tree(repo, a.base) + head_tree = resolve_tree(repo, a.head) + flavor = a.flavor or shared_default_flavor(repo, base_tree, head_tree) t0 = time.time() with ThreadPoolExecutor(max_workers=2) as pool: # the sides are independent - fb = pool.submit(run_side, repo, a.base, flavor, a.timeout, "base") - fh = pool.submit(run_side, repo, a.head, flavor, a.timeout, "head") + fb = pool.submit(run_side, repo, a.base, base_tree, flavor, a.timeout, "base") + fh = pool.submit(run_side, repo, a.head, head_tree, flavor, a.timeout, "head") base_side, head_side = fb.result(), fh.result() families = compare(base_side, head_side) not_inspected = [ @@ -629,11 +724,13 @@ def cmd_run(a): ] seconds = round(time.time() - t0, 1) doc = { - "schema": 1, + "schema": 2, "tool": "impact_emit", "flavor": flavor, "base": a.base, "head": a.head, + "base_tree": base_tree, + "head_tree": head_tree, "families": families, "not_inspected": not_inspected, "seconds": seconds, @@ -668,7 +765,7 @@ def main(argv=None): r.add_argument("--flavor") r.add_argument("--timeout", type=int, default=900, help="seconds per side") r.add_argument("--out", required=True) - c = sub.add_parser("child") # internal: runs inside the scrubbed interpreter + c = sub.add_parser("child") # internal: runs inside the isolated interpreter c.add_argument("--root", required=True) c.add_argument("--out", required=True) args = p.parse_args(argv) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py index d804ee7faf75..2d85260c23d9 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py @@ -51,10 +51,12 @@ ] # Changes under these trigger the IMPACT lane (lanes/runtime-impact.md): the kernel ingestor's -# producers, packer, native packs, SDK ingestor headers and generator configs. +# producers, packer, native packs, SDK ingestor headers, generic-target table and header, and +# generator configs. Keep in step with impact.py's SURFACE_TABLE. IMPACT_PREFIXES = ( "dnn-providers/hip-kernel-provider/", "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/", + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets", "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json", "projects/hipdnn/tools/IngestorGenerator/", ) diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py b/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py index 15b1583bf388..1c526f68901f 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py @@ -304,18 +304,18 @@ def test_declared_pair_checks_static_list_and_flags_dynamic(mod, capsys, tmp_pat def test_cpp_string_literal_edit_is_code_not_whitespace(mod, capsys, tmp_path): - # catches: whitespace deletion instead of collapse ("attn fwd" == "attnfwd") + # catches: whitespace runs inside a literal collapsed like code ("attn fwd" == "attn fwd") repo, base = make_repo( - tmp_path, {PACK: 'const char* kName = "attn fwd";\nint f() { return 1; }\n'} + tmp_path, {PACK: 'const char* kName = "attn fwd";\nint f() { return 1; }\n'} ) head = commit( - repo, {PACK: 'const char* kName = "attnfwd";\nint f() { return 1; }\n'} + repo, {PACK: 'const char* kName = "attn fwd";\nint f() { return 1; }\n'} ) assert ( file_class(report(mod, capsys, repo, base, head, cmd="files"), PACK) == "code" ) spaced = commit( - repo, {PACK: 'const char* kName = "attnfwd";\nint f() { return 1; }\n'} + repo, {PACK: 'const char* kName = "attn fwd";\nint f() { return 1; }\n'} ) assert ( file_class(report(mod, capsys, repo, head, spaced, cmd="files"), PACK) @@ -395,10 +395,7 @@ def test_removed_override_is_not_removed_while_a_base_defines_it(mod, capsys, tm }, ) (row,) = rows_of(report(mod, capsys, repo, base, head), "symbols") - assert ( - row["status"] == "changed" - and row["reason"] == "override removed; base Mystery not resolved" - ) + assert row["status"] == "changed" and row["name"] == "Sub.helper" def test_generic_target_member_removed_names_target_and_member(mod, capsys, tmp_path): @@ -424,7 +421,8 @@ def test_empty_diff_is_an_error_never_none(mod, capsys, tmp_path): mod.main(["report", "--repo", str(repo), "--base", base, "--head", head]) == 1 ) captured = capsys.readouterr() - assert captured.err.strip() == "impact: empty diff" and "NONE" not in captured.out + assert captured.err.startswith("impact: ") and len(captured.err.splitlines()) == 1 + assert "NONE" not in captured.out def test_user_git_config_does_not_change_results(mod, capsys, tmp_path): @@ -486,13 +484,28 @@ def test_ancestor_init_of_a_consumed_module_is_consumed(mod, capsys, tmp_path): assert status[OTHER_KERNEL] == "inert" -def write_emit(path, **family): - base = dict(arch="gfx950", source=BUILDER, builder="build_attn", kdp=KDP, spec_origin="kdp-in-git", - variants={"base": 1, "head": 1}, only_in_base=[], only_in_head=[], params_changed=[], - ir_changed=[], params={"base": None, "head": None}, failures={"base": {}, "head": {}}) # fmt: skip - base.update(family) - doc = {"schema": 1, "tool": "impact_emit", "flavor": "llvm23", "base": "b", "head": "h", - "families": [base], "not_inspected": [], "seconds": 1.0} # fmt: skip +def tree_of(repo, rev) -> str: + return git(repo, "rev-parse", f"{rev}^{{tree}}") + + +def family(**fields) -> dict: + """One emitted family of the builder, every variant equal unless `fields` say otherwise; + `source` is relative to the rocKE library, as descriptors name it.""" + fam = dict(arch="gfx950", source="kernels/gfx950/attn.py", builder="build_attn", kdp=KDP, + spec_origin="kdp-in-git", variants={"base": 1, "head": 1}, only_in_base=[], + only_in_head=[], params_changed=[], ir_changed=[], params={"base": None, "head": None}, + failures={"base": {}, "head": {}}) # fmt: skip + fam.update(fields) + return fam + + +def write_emit(path, repo, base, head, families=None, **fields): + """An impact_emit document made for base..head, holding `families` (default: one family + built from `fields`).""" + doc = {"schema": 2, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, + "base_tree": tree_of(repo, base), "head_tree": tree_of(repo, head), + "families": [family(**fields)] if families is None else families, + "not_inspected": [], "seconds": 1.0} # fmt: skip Path(path).write_text(json.dumps(doc), encoding="utf-8") return str(path) @@ -506,7 +519,13 @@ def test_emit_fold_equal_variants_make_shipped_module_edits_inert( plain = report(mod, capsys, repo, base, head) assert plain["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" equal = report( - mod, capsys, repo, base, head, "--emit", write_emit(tmp_path / "eq.json") + mod, + capsys, + repo, + base, + head, + "--emit", + write_emit(tmp_path / "eq.json", repo, base, head), ) assert equal["rollup"] == "Runtime impact: NONE (1 files inspected)" changed = report( @@ -516,7 +535,7 @@ def test_emit_fold_equal_variants_make_shipped_module_edits_inert( base, head, "--emit", - write_emit(tmp_path / "ir.json", ir_changed=["v"]), + write_emit(tmp_path / "ir.json", repo, base, head, ir_changed=["v"]), ) assert changed["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" @@ -529,14 +548,14 @@ def test_emit_params_changed_vs_unchanged_static_list_is_mismatch( head = commit(repo, {BUILDER: BUILD.replace("return b", "x = 1\n return b")}) wider = [["q_ptr", "ptr"], ["k_ptr", "ptr"], ["batch", "i32"], ["scale", "float"]] data = report(mod, capsys, repo, base, head, "--emit", - write_emit(tmp_path / "p.json", params_changed=["v"], params={"base": None, "head": wider})) # fmt: skip + write_emit(tmp_path / "p.json", repo, base, head, params_changed=["v"], params={"base": None, "head": wider})) # fmt: skip (row,) = rows_of(data, "kernargs", "mismatch") assert row["path"] == PACK and "batch" in row["reason"] assert data["rollup"] == "Runtime impact: BREAKS" # the same params as the static list: consistent, so changed rather than mismatch same = [["q_ptr", "ptr"], ["k_ptr", "ptr"], ["scale", "float"]] data = report(mod, capsys, repo, base, head, "--emit", - write_emit(tmp_path / "s.json", params_changed=["v"], params={"base": None, "head": same})) # fmt: skip + write_emit(tmp_path / "s.json", repo, base, head, params_changed=["v"], params={"base": None, "head": same})) # fmt: skip assert not rows_of(data, status="mismatch") assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" # a static mismatch is never folded away by an otherwise equal emit @@ -545,6 +564,334 @@ def test_emit_params_changed_vs_unchanged_static_list_is_mismatch( ) head2 = commit(repo2, {BUILDER: BUILD_REORDERED}) data = report( - mod, capsys, repo2, base2, head2, "--emit", write_emit(tmp_path / "e2.json") + mod, + capsys, + repo2, + base2, + head2, + "--emit", + write_emit(tmp_path / "e2.json", repo2, base2, head2), + ) + assert data["rollup"] == "Runtime impact: BREAKS" + + +BODY_EDIT = BUILD.replace("return b", "x = 1\n return b") +GT_HEADER = "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets.hpp" +HKP_PACK = HKP + "descriptor-packaging/python/hkp_pack/" + + +def sig(name, names): + """A static `KernelArgument` list function holding `names`, all 4-byte by-value.""" + items = ",\n ".join(f'KernelArgument{{"by_value", F32, {8 * i}, "{n}"}}' for i, n in enumerate(names)) # fmt: skip + return f"std::vector {name}()\n{{\n return {{{items}}};\n}}\n" + + +def test_removed_function_keeps_its_importer_when_another_file_defines_the_name( + mod, capsys, tmp_path +): + # catches: any same-named def elsewhere read as "moved", dropping every live importer + util = "def helper():\n return 1\n\n\ndef keep():\n return 2\n" + used = "from hkp_pack.util import helper\n\n\ndef test_it():\n assert helper() == 1\n" + other = HKP_PACK + "other_util.py" + files = {UTIL: util, READER: used, other: "def helper():\n return 3\n"} + repo, base = make_repo(tmp_path / "a", files) + head = commit(repo, {UTIL: "def keep():\n return 2\n"}) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "symbols", "mismatch") + assert row["name"] == "helper" and {c["path"] for c in row["counterparts"]} == { + READER + } + # a real move: the reader now imports the new home, so nothing reads the old one + repo, base = make_repo(tmp_path / "b", {UTIL: util, READER: used}) + moved = {UTIL: "def keep():\n return 2\n", other: "def helper():\n return 1\n", + READER: used.replace("hkp_pack.util", "hkp_pack.other_util")} # fmt: skip + head = commit(repo, moved) + assert not rows_of(report(mod, capsys, repo, base, head), "symbols", "mismatch") + + +def test_generic_targets_header_code_edit_is_changed_not_na(mod, capsys, tmp_path): + # catches: the header tagged a surface but dropped as if it were the JSON table + text = "inline bool isGeneric(int a) { return a > 3; }\n" + repo, base = make_repo(tmp_path, {GT_HEADER: text}) + head = commit(repo, {GT_HEADER: text.replace("> 3", "> 4")}) + data = report(mod, capsys, repo, base, head) + assert [r["status"] for r in data["rows"] if r["path"] == GT_HEADER] == ["changed"] + assert data["rollup"].startswith("Runtime impact: NEEDS-DEVICE") + + +def test_emit_made_for_another_base_or_head_is_refused(mod, capsys, tmp_path): + # catches: a previous round's emit.json folded into this round's report + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + old = commit(repo, {BUILDER: BODY_EDIT}) + head = commit(repo, {BUILDER: BODY_EDIT.replace("x = 1", "x = 2")}) + stale = write_emit(tmp_path / "stale.json", repo, base, old) + argv = [ + "report", + "--repo", + str(repo), + "--base", + base, + "--head", + head, + "--emit", + stale, + ] + assert mod.main(argv) == 1 + captured = capsys.readouterr() + assert captured.err.startswith("impact: ") and "Runtime impact" not in captured.out + + +@pytest.mark.parametrize( + "spoil", + [ + lambda d: d.update(schema=1), + lambda d: d.update(not_inspected={}), + lambda d: d["families"][0].update(failures=[]), + lambda d: d["families"][0].pop("variants"), + lambda d: d["families"][0].update(params={"base": None, "head": "q_ptr"}), + ], + ids=[ + "schema-1", + "not-inspected-dict", + "failures-list", + "no-variants", + "params-str", + ], +) +def test_malformed_emit_exits_one_instead_of_folding_or_crashing( + mod, capsys, tmp_path, spoil +): + # catches: a malformed emit crashing with a traceback, or folding on missing fields + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BODY_EDIT}) + path = Path(write_emit(tmp_path / "e.json", repo, base, head)) + doc = json.loads(path.read_text(encoding="utf-8")) + spoil(doc) + path.write_text(json.dumps(doc), encoding="utf-8") + argv = [ + "report", + "--repo", + str(repo), + "--base", + base, + "--head", + head, + "--emit", + str(path), + ] + assert mod.main(argv) == 1 + assert capsys.readouterr().err.startswith("impact: ") + + +def test_emit_with_no_families_proves_nothing(mod, capsys, tmp_path): + # catches: an empty family list read as "every shipped variant equal" + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BODY_EDIT}) + emit = write_emit(tmp_path / "none.json", repo, base, head, families=[]) + data = report(mod, capsys, repo, base, head, "--emit", emit) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +@pytest.mark.parametrize("key", ["only_in_base", "only_in_head"]) +def test_emit_with_an_added_or_removed_variant_does_not_fold( + mod, capsys, tmp_path, key +): + # catches: a variant-set change ignored when deciding every shipped variant is equal + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BODY_EDIT}) + emit = write_emit(tmp_path / "e.json", repo, base, head, **{key: ["v2"]}) + data = report(mod, capsys, repo, base, head, "--emit", emit) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +@pytest.mark.parametrize( + "fam", + [ + family(arch="gfx942", source="kernels/gfx942/other.py", builder="build_other"), + family(variants={"base": 0, "head": 0}), + ], + ids=["unrelated-source", "no-variants-built"], +) +def test_emit_folds_only_rows_a_built_family_imports(mod, capsys, tmp_path, fam): + # catches: one quiet family, or an empty one, folding every shipped-module row + other = "def build_other(b, spec):\n return b\n" + repo, base = make_repo( + tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, OTHER_KERNEL: other} ) + head = commit(repo, {BUILDER: BODY_EDIT}) + emit = write_emit(tmp_path / "e.json", repo, base, head, families=[fam]) + data = report(mod, capsys, repo, base, head, "--emit", emit) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +def test_declared_static_list_reordered_against_builder_is_mismatch( + mod, capsys, tmp_path +): + # catches: the declared-pair check comparing names as a set rather than in order + repo, base = make_repo( + tmp_path, {BUILDER: BUILD, PACK: PACK_STATIC, MANIFEST: MANIFEST_YAML} + ) + swapped = PACK_STATIC.replace('"q_ptr"', "@@").replace('"k_ptr"', '"q_ptr"').replace("@@", '"k_ptr"') # fmt: skip + head = commit(repo, {PACK: swapped}) + data = report(mod, capsys, repo, base, head) + assert [r["path"] for r in rows_of(data, "kernargs", "mismatch")] == [PACK] + + +@pytest.mark.parametrize( + "names", [["scale", "stride"], ["q_ptr", "k_ptr", "a", "b", "c", "d"]] +) +def test_unrelated_static_list_in_the_same_pack_is_not_a_mismatch( + mod, capsys, tmp_path, names +): + # catches: `related` loosened to one shared name, or without its half-overlap rule + pack = PACK_STATIC + "\n" + sig("otherSignature", names) + repo, base = make_repo( + tmp_path, {BUILDER: BUILD, PACK: pack, MANIFEST: MANIFEST_YAML} + ) + head = commit(repo, {BUILDER: BODY_EDIT}) + data = report(mod, capsys, repo, base, head) + assert not rows_of(data, status="mismatch") + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +def test_type_only_builder_change_against_declared_static_list_is_mismatch( + mod, capsys, tmp_path +): + # catches: a declared pair compared by names only and reported as agreeing + repo, base = abi_repo(tmp_path, {PACK: PACK_STATIC, MANIFEST: MANIFEST_YAML}) + head = commit(repo, {BUILDER: BUILD.replace('"scale", F32', '"scale", I64')}) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "kernargs", "mismatch") + assert row["path"] == PACK and "scale" in row["reason"] assert data["rollup"] == "Runtime impact: BREAKS" + + +@pytest.mark.parametrize( + "before, after", + [ + ("SC = F32\n\n\n" + BUILD.replace('"scale", F32', '"scale", SC'), + "SC = I64\n\n\n" + BUILD.replace('"scale", F32', '"scale", SC')), + (BUILD, BUILD.replace(' b.param("scale", F32)\n', + ' if spec.scaled:\n b.param("scale", F32)\n')), + ], + ids=["type-in-a-variable", "parameter-made-conditional"], +) # fmt: skip +def test_builder_list_change_with_the_same_names_is_a_kernargs_row( + mod, capsys, tmp_path, before, after +): + # catches: a type held in a variable, or a change in which params are conditional, unseen + repo, base = make_repo(tmp_path, {BUILDER: before}) + head = commit(repo, {BUILDER: after}) + rows = rows_of(report(mod, capsys, repo, base, head), "kernargs") + assert [(r["name"], r["status"]) for r in rows] == [("build_attn", "changed")] + + +def test_declared_mirror_with_no_comparable_list_stays_visible(mod, capsys, tmp_path): + # catches: a declared C++ list sharing fewer than two names skipped without a row + pack = sig("staleSignature", ["scale", "stride"]) + repo, base = make_repo( + tmp_path, {BUILDER: BUILD, PACK: pack, MANIFEST: MANIFEST_YAML} + ) + head = commit(repo, {BUILDER: BODY_EDIT}) + rows = [r for r in rows_of(report(mod, capsys, repo, base, head), "kernargs") if r["path"] == PACK] # fmt: skip + assert [r["status"] for r in rows] == ["changed"] + + +@pytest.mark.parametrize( + "before, after", + [ + ("// note\nint x = 1;\n", "// note \\\nint x = 1;\n"), + ('auto s = u8R"(a"b // c)";\n', 'auto s = u8R"(a"b // d)";\n'), + ('auto s = R"(a\n b)";\n', 'auto s = R"(a\n b)";\n'), + ], + ids=["comment-splices-next-line", "prefixed-raw-string", "multi-line-raw-string"], +) +def test_cpp_edits_behind_splices_and_raw_strings_are_code( + mod, capsys, tmp_path, before, after +): + # catches: a `//` comment ended before a `\`-newline, or raw strings read as plain strings + repo, base = make_repo(tmp_path, {PACK: before}) + head = commit(repo, {PACK: after}) + data = report(mod, capsys, repo, base, head, cmd="files") + assert file_class(data, PACK) == "code" + + +def test_dependency_pin_is_left_to_deps_and_named_in_the_rollup(mod, capsys, tmp_path): + # catches: requirements.txt read as documentation, so a pin bump reads as plain N/A + req = HKP + "rocke/platform/requirements.txt" + repo, base = make_repo(tmp_path, {req: "numpy==1.26\n"}) + head = commit(repo, {req: "numpy==2.0\n"}) + data = report(mod, capsys, repo, base, head) + (row,) = data["rows"] + assert row["status"] == "outside" and "DEPS" in row["reason"] + assert data["rollup"].startswith("Runtime impact: N/A") and "DEPS" in data["rollup"] + + +def test_method_removed_from_a_base_read_only_through_a_subclass_is_changed( + mod, capsys, tmp_path +): + # catches: a removed method whose readers never name its class reported as no impact, or + # any same-named attribute read anywhere (a dict's `.helper`) taken for a reader + files = { + HKP_PACK + "base.py": "class Base:\n def helper(self):\n return 1\n", + HKP_PACK + "sub.py": "from .base import Base\n\n\nclass Sub(Base):\n pass\n", + HKP_PACK + "cfg.py": "def pick(d):\n return d.helper()\n", + READER: "from hkp_pack.sub import Sub\n\n\ndef test_it():\n assert Sub().helper()\n", + HKP + + "descriptor-packaging/tests/test_dotted.py": ( + "import hkp_pack.sub\n\n\ndef test_it():\n assert hkp_pack.sub.Sub().helper()\n" + ), + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {HKP_PACK + "base.py": "class Base:\n pass\n"}) + (row,) = rows_of(report(mod, capsys, repo, base, head), "symbols") + assert (row["name"], row["status"]) == ("Base.helper", "changed") + assert {c["path"] for c in row["counterparts"]} == { + READER, + HKP + "descriptor-packaging/tests/test_dotted.py", + } + + +def test_conflict_only_in_a_test_still_marks_the_skipped_merged_view( + mod, capsys, tmp_path +): + # catches: a skipped merged view counted as outside when only a test file conflicts + repo, base = make_repo(tmp_path / "a", {BUILDER: BUILD, PIN: PIN_TEST}) + head = branch_commit(repo, base, {BUILDER: BODY_EDIT, PIN: PIN_TEST + "# head\n"}, "feature") # fmt: skip + tip = branch_commit(repo, base, {PIN: PIN_TEST + "# tip\n"}, "target") + data = report(mod, capsys, repo, base, head, "--target-tip", tip) + assert ( + next(v for v in data["views"] if v["label"] == "merged")["status"] == "skipped" + ) + assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) + # a docs-only change conflicting in docs still has no surface file + doc = "projects/hipdnn/docs/x.md" + repo, base = make_repo(tmp_path / "b", {doc: "a\n"}) + head = branch_commit(repo, base, {doc: "b\n"}, "feature") + tip = branch_commit(repo, base, {doc: "c\n"}, "target") + data = report(mod, capsys, repo, base, head, "--target-tip", tip) + assert data["rollup"] == "Runtime impact: N/A (no surface file)" + + +def test_unrelated_target_history_skips_the_merged_view_as_changed( + mod, capsys, tmp_path +): + # catches: a merge failure other than a conflict downgraded to outside + repo, base = make_repo(tmp_path, {BUILDER: BUILD}) + head = commit(repo, {BUILDER: BODY_EDIT}) + _COUNTER.pop(repo, None) + tip = commit(repo, {"README.md": "orphan\n"}, parent=None) + data = report(mod, capsys, repo, base, head, "--target-tip", tip) + assert ( + next(v for v in data["views"] if v["label"] == "merged")["status"] == "skipped" + ) + assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) + + +def test_non_ascii_path_is_reported_under_its_real_name(mod, capsys, tmp_path): + # catches: git quoting non-ASCII paths (core.quotepath) so rows name a mangled path + odd = HKP + "rocke/library/kernels/gfx950/attn_\u00e9.py" + repo, base = make_repo(tmp_path, {odd: "def f():\n return 1\n", KDP: KDP_JSON}) + head = commit(repo, {odd: "def f():\n return 2\n"}) + data = report(mod, capsys, repo, base, head) + assert odd in {r["path"] for r in data["rows"]} diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py index 68919bfbc6cc..9e142bf7b892 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py @@ -12,6 +12,7 @@ import os import subprocess import textwrap +import time from pathlib import Path import pytest @@ -184,6 +185,14 @@ def scenarios(): s["generic"][ f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py" ] = GENERIC_HELPERS + s["dupname"] = tree_files( + body="v2", + variants=( + KDP_VARIANTS[0], + {"name": "v_a", "spec": {"n": 3}}, + KDP_VARIANTS[1], + ), + ) s["bare"] = {"README.md": "x"} return s @@ -254,6 +263,12 @@ def run(emit, repo, base, head, tmp_path, *extra): return rc, (json.loads(out.read_text(encoding="utf-8")) if out.exists() else None) +def git_out(repo, *args): + return subprocess.run( + ["git", "-C", str(repo), *args], check=True, capture_output=True, text=True + ).stdout.strip() + + def only_family(doc): assert len(doc["families"]) == 1 return doc["families"][0] @@ -290,7 +305,13 @@ def test_extra_param_lists_every_variant_and_shows_head_list(emit, repo, tmp_pat def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): - _, doc = run(emit, repo, "base", "body", tmp_path) + rc, doc = run(emit, repo, "base", "body", tmp_path) + assert rc == 0 + assert doc["schema"] == 2 + # the recorded ids are the real tree ids, and differ when the sides differ + assert doc["base_tree"] == git_out(repo, "rev-parse", "base^{tree}") + assert doc["head_tree"] == git_out(repo, "rev-parse", "body^{tree}") + assert doc["base_tree"] != doc["head_tree"] f = only_family(doc) assert f["params_changed"] == [] assert f["ir_changed"] == ["v_a", "v_b"] @@ -318,8 +339,9 @@ def test_raising_builder_is_a_recorded_failure_and_exit_zero(emit, repo, tmp_pat assert rc == 0 f = only_family(doc) assert f["variants"] == {"base": 3, "head": 3} - assert f["failures"]["head"] == {"v_bad": "ValueError: boom"} - assert f["failures"]["base"] == {"v_bad": "ValueError: boom"} + for side in ("head", "base"): + assert set(f["failures"][side]) == {"v_bad"} + assert f["failures"][side]["v_bad"].startswith("ValueError") assert f["params_changed"] == f["ir_changed"] == [] @@ -328,7 +350,7 @@ def test_missing_helper_fails_every_variant_instead_of_skipping(emit, repo, tmp_ assert rc == 0 f = only_family(doc) assert set(f["failures"]["head"]) == {"v_a", "v_b"} - assert "build_spec" in f["failures"]["head"]["v_a"] + assert f["failures"]["head"]["v_a"] def test_dvc_held_kdp_without_enumerator_is_not_inspected(emit, repo, tmp_path): @@ -338,7 +360,7 @@ def test_dvc_held_kdp_without_enumerator_is_not_inspected(emit, repo, tmp_path): assert sorted(n["side"] for n in doc["not_inspected"]) == ["base", "head"] n = doc["not_inspected"][0] assert n["kdp"] == f"{DESC}/fake.kdp.json" - assert "registry_enumerator" in n["reason"] + assert n["reason"] def test_dvc_held_kdp_is_regenerated_by_the_enumerator_of_that_side( @@ -374,11 +396,26 @@ def test_flavor_is_pinned_in_the_child_and_overridable( _, doc = run(emit, repo, "base", "base^{tree}", tmp_path, "--flavor", "llvm22") assert doc["flavor"] == "llvm22" assert doc["head"] == "base^{tree}" # a tree id is a valid rev + tree = git_out(repo, "rev-parse", "base^{tree}") + assert doc["head_tree"] == doc["base_tree"] == tree assert only_family(doc)["ir_changed"] == [] _, doc = run(emit, repo, "base", "base", tmp_path) assert doc["flavor"] == "llvm23" +def test_dup_name_with_different_specs_is_a_failure_not_silently_one_variant( + emit, repo, tmp_path +): + # catches: the second of two same-named variants being dropped unnoticed + rc, doc = run(emit, repo, "base", "dupname", tmp_path) + assert rc == 0 + f = only_family(doc) + assert "v_a" in f["failures"]["head"] + assert f["ir_changed"] == ["v_b"] # v_a takes no part in the comparison + rc, doc = run(emit, repo, "dupname", "dupname", tmp_path) + assert only_family(doc)["failures"]["base"].keys() == {"v_a"} + + def test_unreadable_rev_exits_one_with_one_line(emit, repo, tmp_path, capsys): rc, doc = run(emit, repo, "base", "no-such-rev", tmp_path) assert rc == 1 and doc is None @@ -389,7 +426,7 @@ def test_unreadable_rev_exits_one_with_one_line(emit, repo, tmp_path, capsys): def test_tree_without_rocke_exits_one(emit, repo, tmp_path, capsys): rc, _ = run(emit, repo, "bare", "bare", tmp_path, "--flavor", "llvm23") assert rc == 1 - assert "lacks" in capsys.readouterr().err + assert capsys.readouterr().err.startswith("impact_emit: ") @pytest.mark.parametrize( @@ -439,3 +476,77 @@ def test_default_flavor_is_the_ladder_top_else_the_newest_known_flavor( # catches: a tree from before the ladder existed failing instead of using its newest flavor monkeypatch.setattr(emit, "git", lambda *args, **kw: source) assert emit.default_flavor(".", "rev") == expected + + +LADDER_22 = 'A = "llvm20"\nB = "llvm22"\n_LLVM_FLAVOR_LADDER = ((0, A), (22, B))\n' +LADDER_23 = ( + 'A = "llvm20"\nB = "llvm22"\nC = "llvm23"\n' + "_LLVM_FLAVOR_LADDER = ((0, A), (22, B), (23, C))\n" +) + + +@pytest.mark.parametrize( + "base_src, head_src, expected", + [ + (LADDER_22, LADDER_23, "llvm22"), # base predates the head's top rung + (LADDER_23, LADDER_23, "llvm23"), + (LADDER_23, LADDER_22, "llvm22"), + ('X = "llvm19"\nLLVM_FLAVORS = (X,)\n', LADDER_23, "llvm23"), # none shared + ], +) +def test_shared_default_flavor_is_the_heads_newest_that_the_base_knows( + emit, monkeypatch, base_src, head_src, expected +): + # catches: a base lacking the head's top flavor failing every variant + srcs = {"b": base_src, "h": head_src} + monkeypatch.setattr( + emit, "git", lambda repo, cmd, spec, **kw: srcs[spec.split(":")[0]] + ) + assert emit.shared_default_flavor(".", "b", "h") == expected + + +def test_child_env_is_an_allow_list_that_pins_the_rocke_variables( + emit, tmp_path, monkeypatch +): + # catches: parent credentials and import paths reaching the executed rocKE code + for k, v in { + "GH_TOKEN": "t", + "AWS_SECRET_ACCESS_KEY": "s", + "ROCKE_FOO": "1", + "HIP_VISIBLE_DEVICES": "0", + "PYTHONPATH": "/bogus", + "ROCKE_LLVM_FLAVOR": "llvm20", + "LC_ALL": "C", + "IMPACT_EMIT_ENUM_TIMEOUT": "7", + }.items(): + monkeypatch.setenv(k, v) + env = emit.child_env(tmp_path, "llvm23") + names = {k.upper() for k in env} + assert not {"GH_TOKEN", "AWS_SECRET_ACCESS_KEY", "ROCKE_FOO"} & names + assert "HIP_VISIBLE_DEVICES" not in names + assert "PATH" in names and env["LC_ALL"] == "C" + assert env["IMPACT_EMIT_ENUM_TIMEOUT"] == "7" + assert env["ROCKE_LLVM_FLAVOR"] == "llvm23" + assert "/bogus" not in env["PYTHONPATH"] + assert str(tmp_path) in env["PYTHONPATH"] + assert env["ROCKE_PROVIDER_ROOT"].startswith(str(tmp_path)) + assert env["PYTHONDONTWRITEBYTECODE"] == env["PYTHONUTF8"] == "1" + + +def test_timeout_kills_a_grandchild_holding_the_pipes_and_exits_one( + emit, repo, tmp_path, monkeypatch, capsys +): + # catches: --timeout not bounding wall time when a grandchild outlives the child + fake = tmp_path / "fake_child.py" + fake.write_text( + "import subprocess, sys, time\n" + "subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(120)'])\n" + "time.sleep(120)\n" + ) + monkeypatch.setattr(emit, "__file__", str(fake)) + t0 = time.time() + rc, doc = run(emit, repo, "base", "base", tmp_path, "--timeout", "2") + assert time.time() - t0 < 60 + assert rc == 1 and doc is None + err = capsys.readouterr().err.strip().splitlines() + assert len(err) == 1 and err[0].startswith("impact_emit: ") diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py index 6346c8d4975b..e5f96de48f5b 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py @@ -478,6 +478,7 @@ def test_impact_scope_is_the_ingestor_path_only(part): "dnn-providers/hip-kernel-provider/rocke/library/kernels/gfx950/attention_dense.py", "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/Descriptors.hpp", "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json", + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets.hpp", "projects/hipdnn/tools/IngestorGenerator/configs/gfx950_attention_dense.engine.yaml", ) outside = ( @@ -487,3 +488,26 @@ def test_impact_scope_is_the_ingestor_path_only(part): ) rows = [(p, 1, "") for p in inside + outside] assert {r[0] for r in part.concern_scope("IMPACT", rows)} == set(inside) + + +def test_packets_impact_lane_takes_ingestor_files_only(part, tmp_path, capsys): + # catches: IMPACT not registered as a concern lane, or scoped to every changed file + for sub in ("a", "b"): + (tmp_path / sub).mkdir() + header = ( + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets.hpp" + ) + other = "projects/hipdnn/backend/a.cpp" + rd = make_round(tmp_path / "a", [(3, 1, header), (2, 0, other)]) + run(part, "areas", rd) + run(part, "plan", rd) + run(part, "packets", rd, "--lanes", "IMPACT") + packet = (rd / "packets" / "IMPACT.md").read_text() + assert header in packet and other not in packet + rd = make_round(tmp_path / "b", [(2, 0, other)]) + run(part, "areas", rd) + run(part, "plan", rd) + capsys.readouterr() + run(part, "packets", rd, "--lanes", "IMPACT") + assert not (rd / "packets" / "IMPACT.md").exists() + assert "IMPACT: no changed file in scope, N/A" in capsys.readouterr().out From ad029a80c6402cae072954c04a7f3e454f6f1d51 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Fri, 23 Oct 2026 12:51:58 -0600 Subject: [PATCH 03/13] fix(hipdnn): close more false-safe paths in the deep review impact lane The emitter compared shipped variants by their LLVM IR alone, so a change that reaches the code object without touching the IR folded every shipped-module row to inert. Dropping the COMGR optimisation level from -O3 to -O2 for every packed kernel read as no runtime impact. impact_emit.py now records each variant's COMGR inputs as well (the ISA name, the codegen options per kernel and the fresh-process compile flag), writes them as schema 3, and fails every variant when the packer pins a lowering backend other than Python. impact.py refuses older emit files, keeps a family with changed COMGR inputs from folding, and never folds rows on code that runs after lowering, rocke/helpers/compile.py and rocke/runtime/, because an equal IR digest says nothing about it. A changed DVC pointer no longer folds either. The emitter regenerates only the variant list of a DVC-held catalog, which cannot prove the file's other fields equal. Module reachability missed two paths. An import of a name from its package (from rocke.helpers import f) stopped at the package __init__, so a body edit of the module that defines f, which a shipped builder runs, read inert. The import now also reaches the defining module through the package's re-exports. And a module the packer reaches got only the architectures of the builders that happen to import it too, although the packer compiles every packed variant through it, so the device run it named missed the others. Such a module now ships on every architecture. Three paths were reported as runtime impact that are not. A rocKE C++ engine source is inert while no provider build file outside rocKE names the engine's targets and the packer pins the Python backend, since no shipped kernel is then built by the engine. CI test-category lists are left to the BUILD lane. And a merge conflict with the target tip no longer discards the whole merged view: the conflicted tree is analysed, rows that read a conflicted file are dropped, and one row stands in for the conflicted files, so a contract race in files that merged cleanly is still found. The lane brief documents each rule and the remaining gaps. ALMIOPEN-2834 --- .../lanes/runtime-impact.md | 24 +- .../hipdnn-deep-review/scripts/impact.py | 311 ++++++++++++------ .../hipdnn-deep-review/scripts/impact_emit.py | 48 ++- .../tools/ai/tests/test_deep_review_impact.py | 213 +++++++++++- .../ai/tests/test_deep_review_impact_emit.py | 39 ++- 5 files changed, 515 insertions(+), 120 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md index 050c40f48cc1..784463873678 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -19,13 +19,13 @@ same code execution as `RUN` building the head. | Kernel arguments (ABI) | rocKE builder `b.param(...)` sequence under `dnn-providers/hip-kernel-provider/rocke/library/kernels/` | static `KernelArgument` lists in `src/engines/kernel_ingestor_engine/packs/*.cpp`, literal pins in tests, descriptor `signature` arrays | | Launch surface | Python grid, block, LDS and tile tables | the `cpp_mirror` files a `launch_surface:` entry names in `projects/hipdnn/tools/IngestorGenerator/configs/*.engine.yaml` | | Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | -| Shipped variants | `*.kdp.json` (DVC-held for gfx950; regenerated by the generator's registry enumerator) | loader, native packs | +| Shipped variants | `*.kdp.json` (DVC-held for gfx950; the emitter regenerates its variant list with the generator's registry enumerator, so a changed DVC pointer stays `changed`: the file's other fields are not read) | loader, native packs | | Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` and `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` | packer and loader | | Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | -Test code, CMake and `requirements*.txt` are not yours: they are `outside` for status (the rollup -names how many files it left to `BUILD` and `DEPS`), but tests are still searched as copies and -readers, so a stale pin or a stale import is yours. +Test code, CMake, CI test-category lists and `requirements*.txt` are not yours: they are `outside` +for status (the rollup names how many files it left to `BUILD` and `DEPS`), but tests are still +searched as copies and readers, so a stale pin or a stale import is yours. ## Method @@ -43,8 +43,8 @@ readers, so a stale pin or a stale import is yours. 2. Run the emitter when any rocKE or `hkp_pack` Python file changed in code, including rows the report calls `inert`: the static reachability it uses (a module counts as shipped when a shipped builder imports it) misses a call through a dynamic lookup. The emitter builds every shipped - variant at both sides (a few minutes, CPU only) and records each variant's argument list and IR - digest: + variant at both sides (a few minutes, CPU only) and records each variant's argument list, IR + digest and COMGR inputs (ISA name, codegen options, fresh-process compile): ```bash rm -f "$SCRATCH/IMPACT/emit.json" @@ -57,7 +57,8 @@ readers, so a stale pin or a stale import is yours. emit file made for another base or head, or one it cannot parse. Record the flavor it used. A family listed under `failures` or a KDP under `not_inspected` leaves its rows `changed`: never read a failure as equal. An all-equal emit folds only the rows of modules a built family - imports. + imports, and never a row on code that runs after lowering (`rocke/helpers/compile.py`, + `rocke/runtime/`): the IR digest does not cover it. 3. Judge every row whose status is not `inert` or `outside`. Read both sides at `$HEAD` (and the merged tree when the row says `merged-only`) before filing. 4. End **Checked, no finding** with the rollup line the script printed, corrected by your @@ -86,7 +87,8 @@ requests** and add `AUTH: requires with the ingestor flags ON` un - `API`: a changed declaration in a plugin_sdk header, or a `.fbs` change. - `BUILD`: flag matrix, option defaults, test registration, install rules. - `ROCK`, `AUTH`: whether CI builds and tests the change; you supply the required pairs only. -- `DRIFT`: a `merged-only` row, so the merged build and tests cover the copy you name. +- `DRIFT`: a `merged-only` row, so the merged build and tests cover the copy you name; and the + conflicted files of a merged view the report marks `conflicted`, which it does not analyse. - `TESTB`: a contract with no test that would fail on the break you describe. Known gaps: HIP-kind kernels (`ConvNative`, `PointwiseNative`) have their C++ argument lists @@ -99,4 +101,8 @@ then `a.b.m.f()`), or a module reached only through `importlib.import_module(f"...{arch}...")` (the emitter covers that last one for shipped variants, a bare `report` does not). A method removed from a base class and read only through a subclass or an untyped receiver, in a file whose imports reach the class's module, gives a -`changed` row, not a `mismatch`: read the receivers. +`changed` row, not a `mismatch`: read the receivers. The emitter compares what goes into COMGR, +not the COMGR library that compiles it: a change to which comgr the packer loads is the +`BUILD` and `DEPS` lanes'. A rocKE C++ engine source (`rocke/platform/cpp/`) reads `inert` only +while no provider build file outside rocKE links the engine and the packer pins the Python +backend; the row says so, so check that both still hold for the change you review. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py index 5efd8736aae0..828d788df3a8 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py @@ -15,9 +15,11 @@ Views. Each fact is computed at base -> head and, when a target tip differs from base, also at the merged tree `git merge-tree --write-tree ` (before = the tip, after = the merge). A -conflict skips the merged view and says why; the skip is `changed` when the change touches the -surface or the conflict does. A row that holds only in the merged view is marked -`merged-only`: that is the race class (a parallel PR landed a copy the diff cannot see). +conflicted merge is still analysed, except for the conflicted files: rows that read one are dropped +and one row stands in for them, `changed` when a conflicted file is on the surface or a test. A +merge that writes no tree skips the merged view as `changed`. A row that holds only in the merged +view is marked `merged-only`: that is the race class (a parallel PR landed a copy the diff cannot +see). `--round-dir` reads PIN.md (CLONE, BASE, HEAD, TARGET_TIP); a tip equal to BASE gives no merged view. @@ -25,19 +27,22 @@ (a contract side changed, or static analysis cannot settle it; a surface file not understood is always changed), inert (touched, provably without runtime effect), outside (not on the surface; counted, never dropped). Test files and CMake are outside for status but are still searched as -kernarg copies and as symbol readers. CMake and `requirements*.txt` belong to the BUILD and DEPS -lanes; the rollup names how many it left to them. +kernarg copies and as symbol readers. CMake and CI test-category lists belong to the BUILD lane, +`requirements*.txt` to DEPS; the rollup names how many it left to them. A rocKE C++ engine source +is `inert` while no provider build file outside rocKE links the engine and the packer pins the +Python lowering backend; any other non-Python rocKE file is not understood. Rollup (last line of `report`): Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected[; not judged here: ...]) | N/A (no surface file[; not judged here: ...]) -`report --emit FILE.json` folds in impact_emit.py output (schema 2), which must have been made for -the same base and head trees: changed params vs a static C++ list is a mismatch; changed IR is -`changed` for that arch; when every family is equal (nothing changed, nothing failed, nothing not -inspected) the rows that were `changed` only because a shipped module changed become `inert`, but -only on modules a family with built variants imports. Static kernarg mismatch rows are never -downgraded. +`report --emit FILE.json` folds in impact_emit.py output (schema 3), which must have been made for +the same base and head trees: changed params vs a static C++ list is a mismatch; changed IR or +COMGR inputs are `changed` for that arch; when every family is equal (nothing changed, nothing +failed, nothing not inspected) the rows that were `changed` only because a shipped module changed +become `inert`, but only on modules a family with built variants imports, and never on code that +runs after lowering (COMPILE_STEP) or on a DVC pointer. Static kernarg mismatch rows are never +downgraded. A module that the packer reaches ships on every architecture. """ import argparse @@ -92,6 +97,8 @@ *[("carve-out", HKP + f"src/*/*/{d}/*") for d, _ in _CARVE], *[("carve-out", HKP + f"config/*{k}*") for _, k in _CARVE], *[("carve-out", HKP + f"*{k}*_test_categories*.yaml") for _, k in _CARVE], + # CI test-category lists register tests; nothing at run time reads them. + ("test-categories", HKP + "*_test_categories*.yaml"), ("test", SDK + "tests/*"), ("test", HKP + "src/tests/*"), ("test", HKP + "src/integration_tests/*"), @@ -113,12 +120,27 @@ ("generator", GEN + "*"), ("provider", HKP + "*"), ) -OUTSIDE_TAGS = {"carve-out", "test", "cmake", "deps", "other"} +OUTSIDE_TAGS = {"carve-out", "test", "test-categories", "cmake", "deps", "other"} #: Outside tags another deep-review lane judges; the rollup names them so N/A is not read #: as "no impact" for these files. -OWNER_LANES = {"cmake": "BUILD", "deps": "DEPS"} +OWNER_LANES = {"cmake": "BUILD", "test-categories": "BUILD", "deps": "DEPS"} #: Tags whose files are read as copies or readers although they are outside for status. SEARCHED_TAGS = {"test"} +#: rocKE code that runs after lowering (the COMGR step) or at load time: an equal emitted IR +#: digest says nothing about it, so its rows never fold. +COMPILE_STEP = ( + IMPORT_ROOTS[1] + "rocke/helpers/compile.py", + IMPORT_ROOTS[1] + "rocke/runtime/", +) +#: The rocKE C++ engine (`rocke_core`, the `rocke_engine` binding). +ROCKE_CPP = HKP + "rocke/platform/cpp/" +#: The packer module that pins the lowering backend of every packed rocKE kernel. +PACKER_BACKEND = PACKER + "python/hkp_pack/rocke_compile.py" +_PYTHON_BACKEND_PIN = re.compile( + r"""^_BACKEND\s*=\s*["']python["']\s*(?:#.*)?$""", re.M +) +# `rocke_core\w*` also covers the `rocke_core_objects` OBJECT library both engine libraries use. +_ENGINE_LINK = re.compile(r"\b(?:rocke_core\w*|rocke_shared|rocke_engine|ckc::\w+)\b") GENERATED = ( ( @@ -395,7 +417,10 @@ def collect_changes(before: Tree, after: Tree) -> list: def merged_tree(repo, tip: str, head: str): - """(tree oid, None) of the clean merge of tip and head, or (None, reason).""" + """(tree oid, conflicted paths, None) of the merge of tip and head, or (None, None, reason). + + A conflicted merge still writes a tree, with conflict markers in the conflicted files only; + every other path holds its merged content and can be analysed.""" done = subprocess.run( ["git", "-C", str(repo), *GIT_CONFIG, "merge-tree", "--write-tree", "--name-only", tip, head], capture_output=True, @@ -403,18 +428,18 @@ def merged_tree(repo, tip: str, head: str): ) # fmt: skip lines = done.stdout.decode("utf-8", errors="replace").splitlines() if done.returncode == 0 and lines: - return lines[0].strip(), None + return lines[0].strip(), [], None if done.returncode == 1 and lines: files = [] for line in lines[1:]: if not line.strip(): break files.append(line.strip()) - return None, "merge conflict in " + ( - ", ".join(files) if files else "unknown files" - ) + if files and re.fullmatch(r"[0-9a-f]{40,64}", lines[0].strip()): + return lines[0].strip(), files, None + return None, None, "merge conflict in unknown files" err = done.stderr.decode("utf-8", errors="replace").strip() - return None, f"git merge-tree failed: {err or 'exit ' + str(done.returncode)}" + return None, None, f"git merge-tree failed: {err or 'exit ' + str(done.returncode)}" # -- Surface tags and content classes ------------------------------------------------ @@ -1285,16 +1310,31 @@ def eager_nodes(tree): stack.extend(ast.iter_child_nodes(node)) -def import_targets(path: str, tree, known, eager: bool = False) -> list: +def from_base(path: str, node: ast.ImportFrom): + """Module path parts a `from ... import` in `path` names, or None when it is relative in a + file outside the import roots.""" + if not node.level: + return node.module.split(".") if node.module else [] + own = module_name(path) + if not own: + return None + package = own.split(".") + if not path.endswith("__init__.py"): + package = package[:-1] + base = ( + package[: len(package) - (node.level - 1)] if node.level > 1 else list(package) + ) + return base + (node.module.split(".") if node.module else []) + + +def import_targets(path: str, tree, known, eager: bool = False, reexport=None) -> list: """[(target path, line)] for every rocKE module a file imports; `eager` keeps only the - imports that run at import time.""" + imports that run at import time. `reexport(init, name)`, when given, names the module a + package `__init__.py` takes `name` from: `from pkg import f` then also targets the module + that defines `f`, whose body the importer runs when it calls `f`.""" targets = [] if tree is None: return targets - own = module_name(path) - package = own.split(".") if own else [] - if own and not path.endswith("__init__.py"): - package = package[:-1] for node in eager_nodes(tree) if eager else ast.walk(tree): if isinstance(node, ast.Import): for alias in node.names: @@ -1302,24 +1342,27 @@ def import_targets(path: str, tree, known, eager: bool = False) -> list: (resolve_module(alias.name.split("."), known), node.lineno) ) elif isinstance(node, ast.ImportFrom): - if node.level: - if not own: - continue - base = ( - package[: len(package) - (node.level - 1)] - if node.level > 1 - else list(package) - ) - base += node.module.split(".") if node.module else [] - else: - base = node.module.split(".") if node.module else [] + base = from_base(path, node) + if base is None: + continue + package = resolve_module(base, known) for alias in node.names: target = ( None if alias.name == "*" else resolve_module(base + [alias.name], known) ) - targets.append((target or resolve_module(base, known), node.lineno)) + if ( + target is None + and reexport + and alias.name != "*" + and package + and package.endswith("__init__.py") + ): + source = reexport(package, alias.name) + if source and source != package: + targets.append((source, node.lineno)) + targets.append((target or package, node.lineno)) elif isinstance(node, ast.Call): func = node.func name = ( @@ -1358,7 +1401,8 @@ class Closure: #: rocKE module -> the root that reaches it (a descriptor, packer or tool import, or an #: eager `__init__` import chain; every ancestor `__init__.py` of a consumed module is in). consumed: dict = field(default_factory=dict) - #: module -> archs of the production roots whose ordinary imports reach its body. + #: module -> archs of the production roots whose ordinary imports reach its body; + #: ALL_ARCHS when a root without an arch (the packer, a tool) reaches it. shipped: dict = field(default_factory=dict) #: archs whose descriptors sit behind DVC: their kernel modules are roots by directory. opaque: set = field(default_factory=set) @@ -1380,6 +1424,32 @@ def build_closure(tree: Tree) -> Closure: known = {p for p in tree.paths if p.endswith(".py") and p.startswith(IMPORT_ROOTS)} out = Closure() roots = [] # (label, target, arch, production) + tree.read(sorted(p for p in known if p.endswith("__init__.py"))) + memo = {} + + def reexport(init: str, name: str): + """The module package `init` binds `name` from, followed through packages; the None + placeholder ends a cycle.""" + if (init, name) not in memo: + memo[init, name] = None + module = tree.ast(init) + found = None + for node in module.body if module is not None else (): + if found or not isinstance(node, ast.ImportFrom): + continue + base = from_base(init, node) + for alias in node.names if base is not None else (): + if (alias.asname or alias.name) != name or alias.name == "*": + continue + found = resolve_module( + base + [alias.name], known + ) or resolve_module(base, known) + if found and found.endswith("__init__.py") and found != init: + found = reexport(found, alias.name) or found + break + memo[init, name] = found + return memo[init, name] + descriptors = sorted( p for p in tree.paths if p.startswith(DESC_DIR) and p.endswith(".json") ) @@ -1438,7 +1508,9 @@ def build_closure(tree: Tree) -> Closure: for path, data in tree.read(importers).items(): text = decode(data) if text and mention.search(text): - for target, line in import_targets(path, tree.ast(path), known): + for target, line in import_targets( + path, tree.ast(path), known, reexport=reexport + ): roots.append( (f"{path}:{line}", target, None, path.startswith(PRODUCTION_DIRS)) ) @@ -1448,7 +1520,9 @@ def build_closure(tree: Tree) -> Closure: tree.read(frontier) for path in frontier: is_init = path.endswith("__init__.py") - imports = import_targets(path, tree.ast(path), known, eager=is_init) + imports = import_targets( + path, tree.ast(path), known, eager=is_init, reexport=reexport + ) body[path] = [] if is_init else sorted({t for t, _ in imports}) graph[path] = [(t, line, is_init) for t, line in imports] + [ (a, 0, False) for a in ancestor_inits(path, known) @@ -1482,8 +1556,9 @@ def build_closure(tree: Tree) -> Closure: continue seen.add(current) archs = out.shipped.setdefault(current, set()) - if arch: - archs.add(arch) + # A root without an arch is the packer or one of its tools: it compiles every + # packed variant, so what it reaches ships on every architecture. + archs.add(arch or ALL_ARCHS) stack += body.get(current, []) return out @@ -1521,7 +1596,7 @@ class Ctx: def __init__(self, before: Tree, after: Tree, label: str): self.before, self.after, self.label = before, after, label self.changes = {c.path: c for c in collect_changes(before, after)} - self._closures, self._surfaces = {}, {} + self._closures, self._surfaces, self._engine = {}, {}, None def closure(self, side) -> Closure: if side not in self._closures: @@ -1547,6 +1622,36 @@ def moved(self, path: str) -> bool: ch = self.changes.get(path) return ch is not None and ch.cls in MOVING + def engine_unshipped(self): + """Why the rocKE C++ engine reaches no shipped kernel on both sides, or None.""" + if self._engine is None: + self._engine = all( + cpp_engine_unshipped(t) for t in (self.before, self.after) + ) + if not self._engine: + return None + return ( + "rocKE C++ engine source: no provider build file outside rocKE links the engine," + " and the packer pins the Python lowering backend" + ) + + +def cpp_engine_unshipped(tree: Tree) -> bool: + """True when the packer pins the Python lowering backend and no provider CMake file + outside rocKE names the C++ engine's targets: then no shipped kernel is built by it. + """ + pin = tree.text(PACKER_BACKEND) + if not pin or not _PYTHON_BACKEND_PIN.search(pin): + return False + cmake = [ + p for p in tree.under(HKP) if is_cmake(p) and not p.startswith(HKP + "rocke/") + ] + for data in tree.read(cmake).values(): + text = decode(data) + if text is None or _ENGINE_LINK.search(strip_hash_comments(text)): + return False + return True + def func_note(funcs: list) -> str: if not funcs: @@ -1599,23 +1704,35 @@ def rocke_status(ctx: Ctx, ch: Change): """(status, reason, arch, foldable) of a rocKE file whose code changed.""" path = ch.path if not path.endswith(".py") or not path.startswith(IMPORT_ROOTS): + if path.startswith(ROCKE_CPP): + why = ctx.engine_unshipped() + if why: + return "inert", why, None, False return ( "changed", "rocKE file that is not an importable Python module: not understood", arch_of(path), False, ) + # The emitter compares IR text; code after lowering never folds on its evidence. + fold = not path.startswith(COMPILE_STEP) + step = ( + "" + if fold + else "; runs after lowering, so an equal emitted IR does not cover it" + ) side = ctx.closure("before" if ch.head is None else "after") before = after = side archs = set(side.shipped.get(path, ())) if path in side.shipped: - arch = ", ".join(sorted(archs)) if archs else None + arch = ALL_ARCHS if ALL_ARCHS in archs else ", ".join(sorted(archs)) return ( "changed", "module body is reachable from a shipped variant; emitted code or params may differ" - + (f" ({', '.join(sorted(archs))})" if archs else ""), + + (f" ({arch})" if archs else "") + + step, arch or arch_of(path), - True, + fold, ) why = after.consumed.get(path) or before.consumed.get(path) if why: @@ -1623,17 +1740,17 @@ def rocke_status(ctx: Ctx, ch: Change): if bt is None or at is None: return ( "changed", - f"imported module added, removed or unparsable ({why})", + f"imported module added, removed or unparsable ({why}){step}", arch_of(path), - True, + fold, ) diff = surface_changes(bt, at) if diff: return ( "changed", - f"imported by {why}; module surface changed: {'; '.join(diff[:3])}", + f"imported by {why}; module surface changed: {'; '.join(diff[:3])}{step}", arch_of(path), - True, + fold, ) return ( "inert", @@ -2692,7 +2809,8 @@ def data_rows(ctx: Ctx) -> list: if old.get(p) != new.get(p) ) rows.append(make_row("data", path, 0, path, "changed", - f"catalog content changed, not inspected ({shown})", arch=arch_of(path), foldable="dvc")) # fmt: skip + f"catalog content changed, not inspected ({shown}): the DVC-held file is not read here, " + "and a regenerated variant list does not prove its other fields equal", arch=arch_of(path))) # fmt: skip elif ( ch.tag == "descriptor" and ch.cls not in ("docs",) @@ -2752,6 +2870,8 @@ def merge_views(per_view: list) -> list: # -- emit fold --------------------------------------------------------------------------------------- EMIT_SIZES = {"i8": 1, "i16": 2, "i32": 4, "float": 4, "half": 2, "i64": 8, "double": 8} +#: The impact_emit.py output this script reads; 3 added `comgr_changed`. +EMIT_SCHEMA = 3 def load_emit(path) -> dict: @@ -2761,10 +2881,12 @@ def load_emit(path) -> dict: raise rl.Fail(f"cannot read emit file {path}: {exc}") from None if ( not isinstance(data, dict) - or data.get("schema") != 2 + or data.get("schema") != EMIT_SCHEMA or data.get("tool") != "impact_emit" ): - raise rl.Fail(f"{path} is not impact_emit schema 2 output") + raise rl.Fail( + f"{path} is not impact_emit schema {EMIT_SCHEMA} output; rerun impact_emit.py" + ) problem = emit_shape_problem(data) if problem: raise rl.Fail(f"emit file {path}: {problem}") @@ -2813,7 +2935,13 @@ def params(value): and count(variants.get("head")) ): return f"family {n}: `variants` lacks base and head counts" - lists = ("only_in_base", "only_in_head", "params_changed", "ir_changed") + lists = ( + "only_in_base", + "only_in_head", + "params_changed", + "ir_changed", + "comgr_changed", + ) if not all(strings(fam.get(k)) for k in lists): return f"family {n}: {', '.join(lists)} must be lists of names" failures, built = fam.get("failures"), fam.get("params") @@ -2873,7 +3001,13 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: arch, builder = fam["arch"], fam["builder"] quiet = not any( fam[k] - for k in ("params_changed", "ir_changed", "only_in_base", "only_in_head") + for k in ( + "params_changed", + "ir_changed", + "comgr_changed", + "only_in_base", + "only_in_head", + ) ) failures = fam["failures"] if not quiet or any(failures[side] for side in ("base", "head")): @@ -2934,6 +3068,10 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: if fam.get("ir_changed"): out.append(make_row("kernargs", source, 0, f"emit-ir:{arch}:{builder}", "changed", f"emitted IR of {builder} changed for {len(fam['ir_changed'])} variant(s)", arch=arch)) # fmt: skip + if fam["comgr_changed"]: + out.append(make_row("kernargs", source, 0, f"emit-comgr:{arch}:{builder}", "changed", + f"COMGR inputs of {builder} (ISA, codegen options or fresh-process compile) changed for " + f"{len(fam['comgr_changed'])} variant(s): the code object can differ where the IR does not", arch=arch)) # fmt: skip failed = {side: v for side, v in failures.items() if v} if failed: out.append(make_row("kernargs", source, 0, f"emit-fail:{arch}:{builder}", "changed", @@ -2942,35 +3080,16 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: out.append(make_row("kernargs", item.get("kdp", ""), 0, f"emit-skip:{item.get('kdp')}:{item.get('side')}", "changed", f"not inspected ({item.get('side')}): {item.get('reason')}")) # fmt: skip if equal: - dvc_dirs = [ - str(PurePosixPath(f["kdp"]).parent) + "/" - for f in families - if f.get("spec_origin") == "registry_enumerator" - and f.get("kdp") - and f["variants"]["head"] > 0 - ] for row in rows: if ( - row["view"] != ctx.label - or row["status"] != "changed" - or not row["foldable"] + row["view"] == ctx.label + and row["status"] == "changed" + and row["foldable"] + and row["path"] in covered ): - continue - if row["foldable"] == "dvc": - pointer_dir = str(PurePosixPath(row["path"]).parent) + "/" - if not any( - d.startswith(pointer_dir) or pointer_dir.startswith(d) - for d in dvc_dirs - ): - continue - row.update( - status="inert", - reason="catalog regenerated: every shipped variant equal", - ) - elif row["path"] in covered: row.update( status="inert", - reason="emitted IR and params equal for every shipped variant", + reason="emitted IR, params and COMGR inputs equal for every shipped variant", ) for row in out: row["view"] = ctx.label @@ -3106,7 +3225,7 @@ def run(args): per_view = [rows] if tip_pair: tip, tip_tree_id = tip_pair - tree_id, why = merged_tree(repo, tip, head) + tree_id, conflicts, why = merged_tree(repo, tip, head) view = dict( label="merged", before=tip[:12], @@ -3116,22 +3235,10 @@ def run(args): ) views.append(view) if tree_id is None: + # No merged tree at all: the race check is lost for every file of the change. view.update(status="skipped", reason=why) - # Skipping the merged view loses the race check for every surface file of the - # change, wherever the conflict sits; a conflicted test is also a searched copy. - matters = any(r["status"] != "outside" for r in rows) - if ( - why.startswith("merge conflict in ") - and why != "merge conflict in unknown files" - ): - matters = matters or any( - tag_of(p.strip()) not in OUTSIDE_TAGS - SEARCHED_TAGS - for p in why[18:].split(",") - ) - else: - matters = True per_view.append([ - dict(make_row("views", "(merged view)", 0, "merged", "changed" if matters else "outside", + dict(make_row("views", "(merged view)", 0, "merged", "changed", f"merged view skipped: {why}; a race with the target cannot be excluded"), view="merged", merged_only=True) ]) # fmt: skip @@ -3140,6 +3247,22 @@ def run(args): Tree(repo, tip_tree_id), Tree(repo, tree_id), "merged" ) view["files"] = len(mctx.changes) + if conflicts: + # Conflicted files hold markers: drop every row that reads one, and stand in + # for them with one row. A conflicted test is a searched copy, so it counts. + bad = set(conflicts) + mrows = [r for r in mrows if r["path"] not in bad + and not any(c["path"] in bad for c in r["counterparts"])] # fmt: skip + shown = ", ".join(conflicts) + view.update(status="conflicted", reason=f"merge conflict in {shown}") + matters = any( + tag_of(p) not in OUTSIDE_TAGS - SEARCHED_TAGS for p in conflicts + ) + mrows.append( + dict(make_row("views", "(merged view)", 0, "merged", "changed" if matters else "outside", + f"merged view conflicts in {shown}: those files are not analysed in it, " + "so a race on them cannot be excluded"), view="merged", merged_only=True) + ) # fmt: skip per_view.append(mrows) if emit is not None: per_view[0] = fold_emit(ctx, per_view[0], emit) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py index 99513b546152..fcbe6e112feb 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py @@ -8,8 +8,11 @@ REV is any commit or tree id. Each side is `git archive`d into a temp dir (the repo is only read) and a child interpreter with an allow-listed environment builds every shipped rocKE variant there, lowers it to LLVM IR with the Python backend (no GPU, comgr or network), -and records the kernarg list parsed from the IR `define` line plus the SHA-256 of the IR -text. This EXECUTES rocKE code from the reviewed revisions, like building the head does. +and records the kernarg list parsed from the IR `define` line, the SHA-256 of the IR +text, and the COMGR inputs the packer adds to that text (ISA name, codegen options, +fresh-process compile): a code object can change while its IR does not. A packer that +pins another lowering backend fails every variant. This EXECUTES rocKE code from the +reviewed revisions, like building the head does. Variants come from every `*.kdp.json` under the shipped descriptor root whose `kernelDescriptors[].kernel_source` names a rocKE `source` + `builder`. A KDP held by @@ -442,9 +445,39 @@ def __init__(self): self.resolve_spec_class = need["_resolve_spec_class"] self.build_spec = need["build_spec"] self.lower = need["_lower_llvm_via_backend"] + self.rcompile = rcompile self.compile_arch = getattr(rc, "rocke_compile_arch", None) self.require_arch_sig = getattr(rc, "_require_spec_arch_signature", None) self.check_predicate = getattr(rc, "_check_support_predicate", None) + # The packer's lowering backend; a tree without the pin is taken as Python. + backend = getattr(rc, "_BACKEND", "python") + if backend != "python": + self.error = ( + f"the packer lowers with backend {backend!r}; " + "impact_emit lowers with the Python backend only" + ) + + +def _comgr_inputs(rcompile, kd, arch): + """What the packer's COMGR step takes besides the IR text: the ISA name and, per kernel, + the codegen options and whether it compiles in a fresh process. None when this revision + has no `_comgr_options_for_kernel`.""" + options_for = getattr(rcompile, "_comgr_options_for_kernel", None) + if not callable(options_for): + return None + kernels = getattr(kd, "kernels", None) # a KernelModule links several kernels + kernels = list(kernels) if isinstance(kernels, (list, tuple)) else [kd] + attr = getattr(rcompile, "COMGR_FRESH_PROCESS_ATTR", None) + resolve = getattr(rcompile, "_resolve_compile_target", None) + return { + "isa": ( + resolve(arch, "amdgcn-amd-amdhsa--gfx950")[1] if callable(resolve) else None + ), + "options": [list(options_for(k)) for k in kernels], + "fresh_process": [ + bool(attr and (getattr(k, "attrs", None) or {}).get(attr)) for k in kernels + ], + } def _compile_arch(h, shard, target): @@ -473,6 +506,7 @@ def _build_one(h, v, arch): return { "params": parse_kernargs(text), "ir": hashlib.sha256(text.encode("utf-8")).hexdigest(), + "comgr": _comgr_inputs(h.rcompile, kd, arch), } @@ -668,6 +702,10 @@ def by_key(side): n for n in common if built_b[n]["params"] != built_h[n]["params"] ] ir_changed = [n for n in common if built_b[n]["ir"] != built_h[n]["ir"]] + # A side that cannot tell its COMGR inputs (None) differs from one that can. + comgr_changed = [ + n for n in common if built_b[n].get("comgr") != built_h[n].get("comgr") + ] shown = (params_changed or common or [None])[0] if shown is None: shown = next((n for n in names_h if n in built_h), None) @@ -690,6 +728,7 @@ def by_key(side): "only_in_head": [n for n in names_h if n not in names_b], "params_changed": params_changed, "ir_changed": ir_changed, + "comgr_changed": comgr_changed, "params": { "base": built_b[shown]["params"] if shown in built_b else None, "head": built_h[shown]["params"] if shown in built_h else None, @@ -724,7 +763,7 @@ def cmd_run(a): ] seconds = round(time.time() - t0, 1) doc = { - "schema": 2, + "schema": 3, "tool": "impact_emit", "flavor": flavor, "base": a.base, @@ -747,7 +786,8 @@ def cmd_run(a): f"{f['arch']} {f['source']}:{f['builder']} variants {f['variants']['base']}/" f"{f['variants']['head']} only_base {len(f['only_in_base'])} only_head " f"{len(f['only_in_head'])} params_changed {len(f['params_changed'])} " - f"ir_changed {len(f['ir_changed'])} failures {fails} ({f['spec_origin']})" + f"ir_changed {len(f['ir_changed'])} comgr_changed {len(f['comgr_changed'])} " + f"failures {fails} ({f['spec_origin']})" ) for n in not_inspected: print(f"not inspected [{n['side']}] {n['kdp']}: {n['reason']}") diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py b/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py index 1c526f68901f..2befe07a0686 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py @@ -250,8 +250,11 @@ def test_pin_on_target_tip_only_is_mismatch_in_merged_view_only(mod, capsys, tmp assert json.loads(capsys.readouterr().out)["rollup"] == "Runtime impact: BREAKS" -def test_merge_conflict_skips_merged_view_with_reason(mod, capsys, tmp_path): - # catches: a crash on conflict, or a clean NONE when the race cannot be excluded +def test_merge_conflict_on_a_surface_file_marks_the_merged_view_changed( + mod, capsys, tmp_path +): + # catches: a crash on conflict, a clean NONE when the race cannot be excluded, or rows + # read from a file full of conflict markers repo, base = make_repo(tmp_path, {BUILDER: BUILD}) head = branch_commit( repo, base, {BUILDER: BUILD.replace("scale", "scale_a")}, "feature" @@ -262,12 +265,32 @@ def test_merge_conflict_skips_merged_view_with_reason(mod, capsys, tmp_path): data = report(mod, capsys, repo, base, head, "--target-tip", tip) merged = next(v for v in data["views"] if v["label"] == "merged") assert ( - merged["status"] == "skipped" + merged["status"] == "conflicted" and "conflict" in merged["reason"] and BUILDER in merged["reason"] ) assert data["rollup"] != "Runtime impact: NONE (1 files inspected)" - assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) + merged_rows = [r for r in data["rows"] if r["view"] == "merged"] + assert [(r["path"], r["status"]) for r in merged_rows] == [ + ("(merged view)", "changed") + ] + + +def test_conflict_elsewhere_still_finds_the_merged_view_race(mod, capsys, tmp_path): + # catches: one conflicted file, even one no runtime reads, discarding the whole merged + # view and with it a pin race on files that merged cleanly + doc = "AGENTS.md" + repo, base = make_repo(tmp_path, {BUILDER: BUILD, doc: "a\n"}) + head = branch_commit(repo, base, {BUILDER: BUILD_REORDERED, doc: "b\n"}, "feature") + tip = branch_commit(repo, base, {PIN: PIN_TEST, doc: "c\n"}, "target") + data = report(mod, capsys, repo, base, head, "--target-tip", tip) + merged = next(v for v in data["views"] if v["label"] == "merged") + assert merged["status"] == "conflicted" and doc in merged["reason"] + (bad,) = rows_of(data, "kernargs", "mismatch") + assert bad["merged_only"] and bad["view"] == "merged" + (stand_in,) = [r for r in data["rows"] if r["path"] == "(merged view)"] + assert stand_in["status"] == "outside" + assert data["rollup"] == "Runtime impact: BREAKS" def test_static_cpp_list_disagreeing_with_builder_is_mismatch(mod, capsys, tmp_path): @@ -493,8 +516,8 @@ def family(**fields) -> dict: `source` is relative to the rocKE library, as descriptors name it.""" fam = dict(arch="gfx950", source="kernels/gfx950/attn.py", builder="build_attn", kdp=KDP, spec_origin="kdp-in-git", variants={"base": 1, "head": 1}, only_in_base=[], - only_in_head=[], params_changed=[], ir_changed=[], params={"base": None, "head": None}, - failures={"base": {}, "head": {}}) # fmt: skip + only_in_head=[], params_changed=[], ir_changed=[], comgr_changed=[], + params={"base": None, "head": None}, failures={"base": {}, "head": {}}) # fmt: skip fam.update(fields) return fam @@ -502,7 +525,7 @@ def family(**fields) -> dict: def write_emit(path, repo, base, head, families=None, **fields): """An impact_emit document made for base..head, holding `families` (default: one family built from `fields`).""" - doc = {"schema": 2, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, + doc = {"schema": 3, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, "base_tree": tree_of(repo, base), "head_tree": tree_of(repo, head), "families": [family(**fields)] if families is None else families, "not_inspected": [], "seconds": 1.0} # fmt: skip @@ -644,14 +667,14 @@ def test_emit_made_for_another_base_or_head_is_refused(mod, capsys, tmp_path): @pytest.mark.parametrize( "spoil", [ - lambda d: d.update(schema=1), + lambda d: d.update(schema=2), lambda d: d.update(not_inspected={}), lambda d: d["families"][0].update(failures=[]), lambda d: d["families"][0].pop("variants"), lambda d: d["families"][0].update(params={"base": None, "head": "q_ptr"}), ], ids=[ - "schema-1", + "schema-2-without-comgr-inputs", "not-inspected-dict", "failures-list", "no-variants", @@ -852,16 +875,17 @@ def test_method_removed_from_a_base_read_only_through_a_subclass_is_changed( } -def test_conflict_only_in_a_test_still_marks_the_skipped_merged_view( +def test_conflict_only_in_a_test_still_marks_the_merged_view_changed( mod, capsys, tmp_path ): - # catches: a skipped merged view counted as outside when only a test file conflicts + # catches: a conflicted test counted as outside, though it is a searched copy repo, base = make_repo(tmp_path / "a", {BUILDER: BUILD, PIN: PIN_TEST}) head = branch_commit(repo, base, {BUILDER: BODY_EDIT, PIN: PIN_TEST + "# head\n"}, "feature") # fmt: skip tip = branch_commit(repo, base, {PIN: PIN_TEST + "# tip\n"}, "target") data = report(mod, capsys, repo, base, head, "--target-tip", tip) assert ( - next(v for v in data["views"] if v["label"] == "merged")["status"] == "skipped" + next(v for v in data["views"] if v["label"] == "merged")["status"] + == "conflicted" ) assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) # a docs-only change conflicting in docs still has no surface file @@ -895,3 +919,168 @@ def test_non_ascii_path_is_reported_under_its_real_name(mod, capsys, tmp_path): head = commit(repo, {odd: "def f():\n return 2\n"}) data = report(mod, capsys, repo, base, head) assert odd in {r["path"] for r in data["rows"]} + + +ROCKE_PY = HKP + "rocke/platform/python/rocke/" +PACKER_PY = HKP_PACK + "rocke_compile.py" + + +def test_module_the_packer_reaches_ships_on_every_arch(mod, capsys, tmp_path): + # catches: a module the packer compiles every variant through, imported by name from its + # package (`from rocke.core import K`), reported only for the arch whose builder also + # imports it, so the device run named misses the others + shared = ROCKE_PY + "core/ir.py" + files = { + BUILDER: "from rocke.core.ir import K\n" + BUILD, + KDP: KDP_JSON, + shared: "K = 1\n", + ROCKE_PY + "core/__init__.py": "from .ir import K\n", + PACKER_PY: "def load():\n from rocke.core import K\n return K\n", + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {shared: "K = 2\n"}) + data = report(mod, capsys, repo, base, head) + (row,) = [r for r in rows_of(data, "file") if r["path"] == shared] + assert (row["status"], row["arch"]) == ("changed", "all-ingestor-archs") + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE all-ingestor-archs" + + +def test_body_edit_of_a_helper_a_builder_imports_from_its_package_is_changed( + mod, capsys, tmp_path +): + # catches: `from rocke.helpers import f` resolved to the package __init__ only, so a body + # edit of the module that defines f, which the shipped builder runs, reads inert + helper = ROCKE_PY + "helpers/x.py" + files = { + BUILDER: "from rocke.helpers import f\n" + BUILD, + KDP: KDP_JSON, + ROCKE_PY + "helpers/__init__.py": "from .x import f\n", + helper: "def f():\n return 1\n", + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {helper: "def f():\n return 2\n"}) + data = report(mod, capsys, repo, base, head) + (row,) = [r for r in rows_of(data, "file") if r["path"] == helper] + assert (row["status"], row["arch"]) == ("changed", "gfx950") + + +def test_compile_step_edit_never_folds_on_equal_ir(mod, capsys, tmp_path): + # catches: a COMGR option change (-O3 to -O2) folded inert because the IR is equal + step = ROCKE_PY + "helpers/compile.py" + opts = 'def _comgr_options_for_kernel(k):\n return ["-O3"]\n' + files = { + BUILDER: "from rocke.helpers.compile import _comgr_options_for_kernel\n" + + BUILD, + KDP: KDP_JSON, + step: opts, + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {step: opts.replace("-O3", "-O2")}) + emit = write_emit(tmp_path / "eq.json", repo, base, head) + data = report(mod, capsys, repo, base, head, "--emit", emit) + (row,) = [r for r in rows_of(data, "file") if r["path"] == step] + assert row["status"] == "changed" and "after lowering" in row["reason"] + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +def test_emit_comgr_inputs_changed_keep_shipped_rows_changed(mod, capsys, tmp_path): + # catches: a builder attribute that changes only the COMGR options or the fresh-process + # compile (the IR is equal) folded inert + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BODY_EDIT}) + emit = write_emit(tmp_path / "c.json", repo, base, head, comgr_changed=["v"]) + data = report(mod, capsys, repo, base, head, "--emit", emit) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + assert [r["name"] for r in rows_of(data, "kernargs")] == [ + "emit-comgr:gfx950:build_attn" + ] + assert [r["status"] for r in rows_of(data, "file")] == ["changed"] + + +def test_reexport_chain_past_three_packages_still_ships_the_defining_module( + mod, capsys, tmp_path +): + # catches: a depth cap or a memoised dead end in the re-export walk, so `from rocke.a + # import f` stops short of the module defining f and a body edit of it reads inert + chain = "abcd" + files = { + ROCKE_PY + f"{p}/__init__.py": f"from rocke.{q} import f\n" + for p, q in zip(chain, chain[1:]) + } + files[ROCKE_PY + "d/__init__.py"] = "from rocke.d.e import f\n" + defining = ROCKE_PY + "d/e.py" + files[defining] = "def f():\n return 1\n" + files[HKP_PACK + "a1.py"] = "from rocke.a import f\n" + files[HKP_PACK + "a2.py"] = "from rocke.d import f\n" + repo, base = make_repo(tmp_path, files) + head = commit(repo, {defining: "def f():\n return 2\n"}) + data = report(mod, capsys, repo, base, head) + assert [(r["path"], r["status"]) for r in rows_of(data, "file")] == [ + (defining, "changed") + ] + + +ENGINE_SRC = HKP + "rocke/platform/cpp/core/a.cpp" +PROVIDER_CMAKE = HKP + "src/CMakeLists.txt" + + +@pytest.mark.parametrize( + "cmake, pin, status", + [ + ("add_library(p x.cpp)\n# rocke_core is not linked\n", '_BACKEND = "python"\n', "inert"), + ("target_link_libraries(p PRIVATE rocke_core)\n", '_BACKEND = "python"\n', "changed"), + ("add_library(p SHARED $)\n", '_BACKEND = "python"\n', "changed"), + ("add_library(p x.cpp)\n", '_BACKEND = "cpp"\n', "changed"), + ], + ids=["unlinked-python-pin", "linked", "engine-objects", "cpp-pin"], +) # fmt: skip +def test_rocke_cpp_engine_source_is_inert_only_while_nothing_ships_it( + mod, capsys, tmp_path, cmake, pin, status +): + # catches: every rocKE C++ engine edit read as NEEDS-DEVICE on every arch though no shipped + # kernel is built by the engine; and the reverse, an engine the provider links or the + # packer lowers with read as inert + files = { + ENGINE_SRC: "int f() { return 1; }\n", + PROVIDER_CMAKE: cmake, + PACKER_PY: pin, + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {ENGINE_SRC: "int f() { return 2; }\n"}) + data = report(mod, capsys, repo, base, head) + assert {r["path"]: r["status"] for r in rows_of(data, "file")} == { + ENGINE_SRC: status + } + + +def test_ci_test_category_list_is_left_to_build(mod, capsys, tmp_path): + # catches: a CI test-category list read as provider code, so enrolling a test in a CI + # tier reads as NEEDS-DEVICE on every arch + cats = HKP + "ROCKE_ENGINE_test_categories_external.yaml" + repo, base = make_repo(tmp_path, {cats: "quick:\n - a\n"}) + head = commit(repo, {cats: "quick:\n - a\n - b\n"}) + data = report(mod, capsys, repo, base, head) + (row,) = data["rows"] + assert row["status"] == "outside" and "BUILD" in row["reason"] + assert ( + data["rollup"].startswith("Runtime impact: N/A") and "BUILD" in data["rollup"] + ) + + +def test_changed_dvc_pointer_is_not_folded_by_a_regenerated_catalog( + mod, capsys, tmp_path +): + # catches: a new DVC-held catalog read as equal because the enumerator regenerates the + # same variant list from source, while the file's other fields went unread + ptr = KDP.rsplit("/", 1)[0] + "/artifacts.dvc" + pointer = "outs:\n- md5: {}.dir\n path: artifacts\n" + repo, base = make_repo(tmp_path, {BUILDER: BUILD, ptr: pointer.format("a" * 32)}) + head = commit(repo, {ptr: pointer.format("b" * 32)}) + emit = write_emit( + tmp_path / "e.json", repo, base, head, spec_origin="registry_enumerator" + ) + data = report(mod, capsys, repo, base, head, "--emit", emit) + assert [(r["path"], r["status"]) for r in rows_of(data, "data")] == [ + (ptr, "changed") + ] + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py index 9e142bf7b892..04c1f921f81e 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py @@ -57,6 +57,15 @@ def _lower_llvm_via_backend(kd, *, arch, backend, spec): ) """ +# The packer's COMGR inputs: options per kernel, a fresh-process attribute, the ISA name. +OPTIONS = """ +COMGR_FRESH_PROCESS_ATTR = "comgr_fresh_process" +def _comgr_options_for_kernel(kernel): + return ["-O3"] +def _resolve_compile_target(arch, isa): + return arch, "amdgcn-amd-amdhsa--" + arch +""" + def builder_src(params=(), body="v1"): ps = "".join(f' p.append(("{n}", "{t}"))\n' for n, t in params) @@ -194,6 +203,14 @@ def scenarios(): ), ) s["bare"] = {"README.md": "x"} + compile_py = f"{PROVIDER}/rocke/platform/python/rocke/helpers/compile.py" + for name, level in (("opts_o3", "-O3"), ("opts_o2", "-O2")): + s[name] = tree_files() + s[name][compile_py] = COMPILE + OPTIONS.replace("-O3", level) + s["cpp_pin"] = tree_files() + s["cpp_pin"][ + f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py" + ] = (HELPERS + '_BACKEND = "cpp"\n') return s @@ -307,7 +324,7 @@ def test_extra_param_lists_every_variant_and_shows_head_list(emit, repo, tmp_pat def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): rc, doc = run(emit, repo, "base", "body", tmp_path) assert rc == 0 - assert doc["schema"] == 2 + assert doc["schema"] == 3 # the recorded ids are the real tree ids, and differ when the sides differ assert doc["base_tree"] == git_out(repo, "rev-parse", "base^{tree}") assert doc["head_tree"] == git_out(repo, "rev-parse", "body^{tree}") @@ -345,6 +362,26 @@ def test_raising_builder_is_a_recorded_failure_and_exit_zero(emit, repo, tmp_pat assert f["params_changed"] == f["ir_changed"] == [] +def test_comgr_option_change_with_equal_ir_is_comgr_changed(emit, repo, tmp_path): + # catches: a COMGR option change (the code object differs, the IR does not) reported + # as every variant equal + _, doc = run(emit, repo, "opts_o3", "opts_o2", tmp_path) + f = only_family(doc) + assert f["ir_changed"] == [] and f["params_changed"] == [] + assert f["comgr_changed"] == ["v_a", "v_b"] + _, doc = run(emit, repo, "opts_o3", "opts_o3", tmp_path) + assert only_family(doc)["comgr_changed"] == [] + + +def test_packer_pinned_to_another_backend_fails_every_variant(emit, repo, tmp_path): + # catches: Python-lowered IR compared as if it were what a packer pinned to the C++ + # backend ships + _, doc = run(emit, repo, "cpp_pin", "cpp_pin", tmp_path) + f = only_family(doc) + assert set(f["failures"]["head"]) == {"v_a", "v_b"} + assert "'cpp'" in f["failures"]["head"]["v_a"] + + def test_missing_helper_fails_every_variant_instead_of_skipping(emit, repo, tmp_path): rc, doc = run(emit, repo, "nohelper", "nohelper", tmp_path) assert rc == 0 From f019d5cf6d0b0450fb8ecda62482bebfd5a6aacd Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Tue, 27 Oct 2026 11:20:24 -0600 Subject: [PATCH 04/13] feat(hipdnn): add the hipdnn-ingestor-impact skill The question the deep review's IMPACT lane answers, whether a change breaks the kernel ingestor at run time while the default build stays green, is also asked outside a review: on a single commit, a branch before it is opened as a pull request, uncommitted work, or before the ingestor flags are turned ON by default. Running a multi-agent review for that is out of proportion, and the scripts that collect the facts were reachable only from inside the deep-review skill. The new hipdnn-ingestor-impact skill owns impact.py and impact_emit.py and answers the question on its own. It pins the revisions for each input form, runs the static report and, when rocKE or hkp_pack Python changed in code, the emitter, judges each row, and reports the rollup with the device runs that would settle what static analysis cannot. Its reference holds what both uses share: the contracts, the row statuses and rollup, the emitter's rules, how each row is judged, and the known gaps. The deep review consumes the skill the same way it consumes the superbuild skills. The IMPACT lane calls the scripts from the sibling skill directory and keeps only what a review adds: where the evidence goes, when the lane stops, how a judged row becomes a finding, and its handoffs. impact.py no longer imports the review's helper library or reads its PIN.md; the lane passes the clone, base, head and target tip explicitly. The tests move with the scripts, the skill validator checks the new skill's scripts and command fields, and the skill catalogs list it. ALMIOPEN-2834 --- AGENTS.md | 3 +- projects/hipdnn/docs/ai-rules.md | 7 +- .../ai/skills/hipdnn-deep-review/RUNBOOK.md | 2 +- .../ai/skills/hipdnn-deep-review/SKILL.md | 14 +- .../lanes/runtime-impact.md | 88 ++++--------- .../hipdnn-deep-review/scripts/partition.py | 2 +- .../ai/skills/hipdnn-ingestor-impact/SKILL.md | 110 ++++++++++++++++ .../hipdnn-ingestor-impact/agents/openai.yaml | 4 + .../hipdnn-ingestor-impact/reference.md | 121 ++++++++++++++++++ .../scripts/impact.py | 63 ++++----- .../scripts/impact_emit.py | 0 ...view_impact.py => test_ingestor_impact.py} | 15 +-- ...t_emit.py => test_ingestor_impact_emit.py} | 8 +- projects/hipdnn/tools/ai/validate-skills.py | 2 + 14 files changed, 312 insertions(+), 127 deletions(-) create mode 100644 projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md create mode 100644 projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/agents/openai.yaml create mode 100644 projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md rename projects/hipdnn/tools/ai/skills/{hipdnn-deep-review => hipdnn-ingestor-impact}/scripts/impact.py (98%) rename projects/hipdnn/tools/ai/skills/{hipdnn-deep-review => hipdnn-ingestor-impact}/scripts/impact_emit.py (100%) rename projects/hipdnn/tools/ai/tests/{test_deep_review_impact.py => test_ingestor_impact.py} (98%) rename projects/hipdnn/tools/ai/tests/{test_deep_review_impact_emit.py => test_ingestor_impact_emit.py} (99%) diff --git a/AGENTS.md b/AGENTS.md index 34b56c5f6695..e0852aac25bc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -77,8 +77,9 @@ All under [`projects/hipdnn/tools/ai/skills/`](projects/hipdnn/tools/ai/skills/) | `hipdnn-superbuild-test` | Running tests against an existing hipDNN superbuild. | | `hipdnn-tidy` | Running clang-tidy on hipDNN, scoped to the files you changed. | | `hipdnn-ingestor-engine` | Creating or extending a generic-kernel-ingestor integration. | +| `hipdnn-ingestor-impact` | Checking whether a kernel ingestor change breaks kernel ABI or dispatch at run time while the build stays green, without a full review. | | `hipdnn-uhd` | Taking a hipDNN engine through the UHD heuristic lifecycle on the local GPU: generating, training, judging, validating, installing, and triaging heuristics and their training data. | -| `hipdnn-deep-review` | Reviewing a hipDNN or provider change in depth with parallel agents until its findings close, or checking whether a kernel ingestor change breaks kernel ABI or dispatch at run time (its `IMPACT` lane and `impact.py`). | +| `hipdnn-deep-review` | Reviewing a hipDNN or provider change in depth with parallel agents until its findings close. Its `IMPACT` lane runs the `hipdnn-ingestor-impact` scripts. | | `hipdnn-codegen` (at [`projects/hipdnn/tools/DescriptorGenerator/.claude/skills/`](projects/hipdnn/tools/DescriptorGenerator/.claude/skills/hipdnn-codegen/SKILL.md)) | Generating operation boilerplate from a YAML config. | | `rfc-review` | Reviewing a technical RFC across the structured perspectives. Follow-ups: `rfc-review-compatibility`, `rfc-review-ops`, `rfc-review-security`. | | `rfc-backlog` | Turning an RFC into a backlog of Jira stories and tasks. | diff --git a/projects/hipdnn/docs/ai-rules.md b/projects/hipdnn/docs/ai-rules.md index 34a6168b9d1d..a041c7653269 100644 --- a/projects/hipdnn/docs/ai-rules.md +++ b/projects/hipdnn/docs/ai-rules.md @@ -31,8 +31,8 @@ When a user asks for a workflow covered by a project skill, tell them the projec - Deprecated stub that redirects to `hipdnn-pr-quality` (review assist). Same behavior: announce deprecation, then defer. - `tools/ai/skills/hipdnn-deep-review/SKILL.md` - Runbook for a multi-agent deep review of a hipDNN or provider PR, branch or commit range: scouts map each affected area, focused reviewer lanes run in parallel, every finding is verified by source proof, reproduction or mutation, and a ledger carries findings across rounds until no blocking or important one stays open. Owns its review policy; the review is advisory and never posts without approval. - - Suggest this skill when the user asks for a deep, thorough or multi-agent review, or wants to iterate a review until all findings close. Use `hipdnn-pr-quality` for drafting PR text or a single-pass review. Requires subagents and its sibling skills `hipdnn-superbuild` and `hipdnn-superbuild-test`. - - Its `IMPACT` lane reports kernel ingestor changes that break at run time while the build stays green: kernel argument lists out of step with their C++ and test copies (also against the target branch's tip, which catches two PRs that each pass alone), dispatch predicates, descriptors and generic targets, and which shipped variants' emitted code changed. Suggest `scripts/impact.py report` from that skill for a quick answer on a hip-kernel-provider, rocKE or ingestor change without a full review. + - Suggest this skill when the user asks for a deep, thorough or multi-agent review, or wants to iterate a review until all findings close. Use `hipdnn-pr-quality` for drafting PR text or a single-pass review. Requires subagents and its sibling skills `hipdnn-superbuild`, `hipdnn-superbuild-test` and `hipdnn-ingestor-impact`. + - Its `IMPACT` lane runs the `hipdnn-ingestor-impact` scripts and turns their rows into review findings. - `tools/ai/skills/hipdnn-superbuild/SKILL.md` - Builds hipDNN together with one or more providers via the repository-root superbuild presets (`hipdnn-providers`, `miopen-provider`, `hipblaslt-provider`, `hip-kernel-provider`, `hipdnn-samples`, etc.), in a single CMake invocation. On Windows it auto-runs the wheel-based ROCm setup when no SDK path is supplied. - Suggest this skill when the user asks to build hipDNN with providers, run a superbuild preset, rebuild after a rebase or merge, or set up a fresh build from the repo root. Prefer it over the standalone build whenever providers are involved. @@ -48,6 +48,9 @@ When a user asks for a workflow covered by a project skill, tell them the projec - `tools/ai/skills/hipdnn-ingestor-engine/SKILL.md` - Creates or extends a hipDNN generic-kernel-ingestor integration: graph and kernel contracts, descriptors, native hooks, packaging, host checks, and device correctness. - Suggest this skill when the user asks to add or extend an ingestor-based engine. +- `tools/ai/skills/hipdnn-ingestor-impact/SKILL.md` + - Reports kernel ingestor changes that break at run time while the build stays green: kernel argument lists out of step with their C++ and test copies (also against the target branch's tip, which catches two PRs that each pass alone), dispatch predicates, descriptors and generic targets, removed Python symbols still read, and which shipped variants' emitted code changed. Reads git objects; its optional emitter runs rocKE on the CPU. Advisory: it never builds, tests or posts. + - Suggest this skill for a quick answer on a hip-kernel-provider, rocKE or ingestor change without a full review, and before turning the ingestor flags ON by default. Use `hipdnn-deep-review` for a full review; its `IMPACT` lane runs the same scripts. - `tools/ai/skills/rfc-review/SKILL.md`, with the focused follow-ups `rfc-review-compatibility`, `rfc-review-ops`, and `rfc-review-security` - Review a technical RFC or design document and produce a written review report. They are not for implementation PRs. - Suggest these skills when the user asks to review an RFC, design doc, ADR, or proposal. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md index d5bef0d9e058..50809f9ae8db 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md @@ -8,7 +8,7 @@ needs subagents; without them, stop and tell the user to run `hipdnn-pr-quality` `` is the PR number, `` the round. `/` comes from the PR URL, else `origin` of `$CLONE`, else `ROCm/rocm-libraries`; pass `-R /` to every `gh pr` and -`gh issue` call. `pin.py`, `partition.py`, `ledger.py`, `comments.py`, `impact.py` and `impact_emit.py` are in `$SKILL/scripts/`; +`gh issue` call. `pin.py`, `partition.py`, `ledger.py` and `comments.py` are in `$SKILL/scripts/` (the `IMPACT` lane's scripts are in the sibling skill `hipdnn-ingestor-impact`); a script name below stands for `python3 "$SKILL/scripts/"` (`python` or `py -3` on Windows, as in SKILL.md *Platforms*). Each prints terse results and exits non-zero with a one-line reason. After pinning, commands run from `$EVIDENCE/r/` (sourcing `env.sh` changes to it); GNU coreutils, grep and sed are assumed. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md index 60d2ee66bd59..7780b631f500 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/SKILL.md @@ -1,6 +1,6 @@ --- name: hipdnn-deep-review -description: "Multi-agent deep review of a hipDNN or dnn-providers PR, branch or commit range. Scouts map each affected area, focused reviewer lanes run in parallel, every finding is verified by source proof, reproduction or mutation, and a ledger carries findings across rounds until no blocking or important one stays open. Its IMPACT lane reports kernel ingestor changes that break at run time while the build stays green (kernel ABI, dispatch, descriptors), and its script answers that question standalone. For a PR body or a quick single-pass review, use hipdnn-pr-quality." +description: "Multi-agent deep review of a hipDNN or dnn-providers PR, branch or commit range. Scouts map each affected area, focused reviewer lanes run in parallel, every finding is verified by source proof, reproduction or mutation, and a ledger carries findings across rounds until no blocking or important one stays open. Its IMPACT lane reports kernel ingestor changes that break at run time while the build stays green (kernel ABI, dispatch, descriptors) with the scripts of hipdnn-ingestor-impact, which answers that question without a review. For a PR body or a quick single-pass review, use hipdnn-pr-quality." argument-hint: "[ | branch: | ..] evidence: scratch: [clone:] [intent:] [terms:] [knowledge:] [round:]" allowed-tools: Bash, Read, Write, Edit, Grep, Glob, Task --- @@ -13,11 +13,12 @@ scouts, lanes and verifiers, merge their output and write the report; you do not code. Your formats are in [report.md](report.md); [RUNBOOK.md](RUNBOOK.md) *Fan out* says what each agent reads, and `lanes/` holds the briefs. `RUN` builds with [hipdnn-superbuild](../hipdnn-superbuild/SKILL.md) and tests with -[hipdnn-superbuild-test](../hipdnn-superbuild-test/SKILL.md); install those skills alongside this one. +[hipdnn-superbuild-test](../hipdnn-superbuild-test/SKILL.md); `IMPACT` runs the scripts of +[hipdnn-ingestor-impact](../hipdnn-ingestor-impact/SKILL.md). Install those skills alongside this one. Prerequisites: network access, an authenticated `gh` with read access, `git` 2.38 or later (`pin.py` uses `git merge-tree --write-tree`), and Python 3 (it runs the scripts under -`$SKILL/scripts/`); `pre-commit` and `pytest` for the lanes that run validators. The `IMPACT` lane's `impact.py` needs -Python 3.10 or newer and PyYAML (it reads the `*.engine.yaml` launch surfaces); its emitter also needs Jinja2. `RUN` and verifiers also need +`$SKILL/scripts/`); `pre-commit` and `pytest` for the lanes that run validators. The `IMPACT` lane needs +Python 3.10 or newer and PyYAML; its emitter also needs Jinja2 ([hipdnn-ingestor-impact](../hipdnn-ingestor-impact/SKILL.md) *Prerequisites*). `RUN` and verifiers also need a ROCm install (Linux: `/opt/rocm` or `ROCM_PATH`; Windows: the wheel SDK that [hipdnn-superbuild](../hipdnn-superbuild/SKILL.md) provisions), CMake, Ninja and about 25 GB free in `$SCRATCH`; without them `defect` and `test-gap` findings stay `UNVERIFIED` and the review closes only through human `ACCEPTED-RISK` decisions, recorded with `ledger.py set` (`PIN.md` records `Build host`). @@ -26,9 +27,8 @@ Platforms: Linux, macOS and Windows. On Windows run the skill from Git Bash (Git `bash`, `awk`, `sed`, `grep` and `comm`. Blocks write the interpreter as `python3`; on Windows that name is the Microsoft Store stub, so use `python` or `py -3` instead. Scripts read and write UTF-8 and LF whatever the host's locale. -For a quick answer without a review, `impact.py report --repo --base --head ` (add `--target-tip -` for an open PR whose target moved) prints the kernel ABI and dispatch contracts a change touches, as the `IMPACT` -lane ([lanes/runtime-impact.md](lanes/runtime-impact.md)) collects them, ending in a `Runtime impact:` line. +For a quick answer without a review, use [hipdnn-ingestor-impact](../hipdnn-ingestor-impact/SKILL.md): it runs the +same scripts as the `IMPACT` lane ([lanes/runtime-impact.md](lanes/runtime-impact.md)) and ends in a `Runtime impact:` line. ## Entry contract diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md index 784463873678..f50c3c5a7c84 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -2,26 +2,15 @@ Find changes that break the kernel ingestor at run time while the build stays green: a kernel's argument list (its ABI) that no longer matches a copy, a dispatch predicate that drifts from the -kernel it selects, a descriptor or generic-target edit that changes what ships. The ingestor C++ -compiles only with `HIPDNN_ENABLE_KERNEL_INGESTOR` ON, so judge the change as built with the -ingestor, rocKE and SDPA flags ON, whatever their defaults. +kernel it selects, a descriptor or generic-target edit that changes what ships. -You judge; two scripts collect facts. `impact.py` reads git objects only. `impact_emit.py` -imports and runs rocKE code from the reviewed trees (a `git archive` extract, in a child process -whose whole process tree is killed at its timeout, with an environment reduced to system, temp, -locale and Python user-site variables, so credentials such as `GH_TOKEN` are not passed): the -same code execution as `RUN` building the head. - -## Contracts - -| Contract | Producer | Copies the change must keep in step | -|---|---|---| -| Kernel arguments (ABI) | rocKE builder `b.param(...)` sequence under `dnn-providers/hip-kernel-provider/rocke/library/kernels/` | static `KernelArgument` lists in `src/engines/kernel_ingestor_engine/packs/*.cpp`, literal pins in tests, descriptor `signature` arrays | -| Launch surface | Python grid, block, LDS and tile tables | the `cpp_mirror` files a `launch_surface:` entry names in `projects/hipdnn/tools/IngestorGenerator/configs/*.engine.yaml` | -| Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | -| Shipped variants | `*.kdp.json` (DVC-held for gfx950; the emitter regenerates its variant list with the generator's registry enumerator, so a changed DVC pointer stays `changed`: the file's other fields are not read) | loader, native packs | -| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` and `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` | packer and loader | -| Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | +You judge; two scripts from the sibling skill +[hipdnn-ingestor-impact](../../hipdnn-ingestor-impact/SKILL.md) collect facts. Read its +[reference](../../hipdnn-ingestor-impact/reference.md) first: it holds the contracts, the row +statuses and rollup, the emitter's rules, how to judge each row, and the known gaps. This brief adds +what a review needs: where the evidence goes, when to stop, and how a judged row becomes a finding. +`impact_emit.py` imports and runs rocKE code from the reviewed trees: the same code execution as +`RUN` building the head. Test code, CMake, CI test-category lists and `requirements*.txt` are not yours: they are `outside` for status (the rollup names how many files it left to `BUILD` and `DEPS`), but tests are still @@ -35,32 +24,25 @@ searched as copies and readers, so a stale pin or a stale import is yours. ```bash . ./env.sh; mkdir -p "$SCRATCH/IMPACT" lanes/IMPACT-evidence - python3 "$SKILL/scripts/impact.py" report --round-dir . > lanes/IMPACT-evidence/report.md + IMPACT_SCRIPTS="$SKILL/../hipdnn-ingestor-impact/scripts" + python3 "$IMPACT_SCRIPTS/impact.py" report --repo "$CLONE" --base "$BASE" --head "$HEAD" --target-tip "$TARGET_TIP" > lanes/IMPACT-evidence/report.md ``` Sourcing `env.sh` moves to the round directory. Exit 1 means the script could not run: return `STOP:` with its message. -2. Run the emitter when any rocKE or `hkp_pack` Python file changed in code, including rows the - report calls `inert`: the static reachability it uses (a module counts as shipped when a shipped - builder imports it) misses a call through a dynamic lookup. The emitter builds every shipped - variant at both sides (a few minutes, CPU only) and records each variant's argument list, IR - digest and COMGR inputs (ISA name, codegen options, fresh-process compile): +2. Run the emitter when the reference's *The emitter* says to: ```bash rm -f "$SCRATCH/IMPACT/emit.json" - python3 "$SKILL/scripts/impact_emit.py" run --repo "$CLONE" --base "$BASE" --head "$HEAD" --out "$SCRATCH/IMPACT/emit.json" - python3 "$SKILL/scripts/impact.py" report --round-dir . --emit "$SCRATCH/IMPACT/emit.json" > lanes/IMPACT-evidence/report-emit.md + python3 "$IMPACT_SCRIPTS/impact_emit.py" run --repo "$CLONE" --base "$BASE" --head "$HEAD" --out "$SCRATCH/IMPACT/emit.json" + python3 "$IMPACT_SCRIPTS/impact.py" report --repo "$CLONE" --base "$BASE" --head "$HEAD" --target-tip "$TARGET_TIP" --emit "$SCRATCH/IMPACT/emit.json" > lanes/IMPACT-evidence/report-emit.md ``` The emitter exiting 1 (it could not run, or a side timed out) leaves no proof: return `STOP:` - with its message, and never fold an `emit.json` from an earlier run. `impact.py` refuses an - emit file made for another base or head, or one it cannot parse. Record the flavor it used. A - family listed under `failures` or a KDP under `not_inspected` leaves its rows `changed`: never - read a failure as equal. An all-equal emit folds only the rows of modules a built family - imports, and never a row on code that runs after lowering (`rocke/helpers/compile.py`, - `rocke/runtime/`): the IR digest does not cover it. -3. Judge every row whose status is not `inert` or `outside`. Read both sides at `$HEAD` (and the - merged tree when the row says `merged-only`) before filing. + with its message, and never fold an `emit.json` from an earlier run. Record the flavor it used. +3. Judge every row whose status is not `inert` or `outside` with the reference's *Judging the + rows*. Read both sides at `$HEAD` (and the merged tree when the row says `merged-only`) before + filing. 4. End **Checked, no finding** with the rollup line the script printed, corrected by your judgment and saying why when it differs: `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`, @@ -68,19 +50,16 @@ searched as copies and readers, so a stale pin or a stale import is yours. ## From row to finding -| Status | Action | +| Judgment | Finding | |---|---| -| `mismatch` | A `source-claim` at `BLOCKING` ([../policy.md](../policy.md): an ABI break without a migration path). **Where**: the producer line and every disagreeing copy. **Proof**: the command and both quoted lists. A `merged-only` mismatch breaks the target once merged: **Fix** is to rebase and update the named copy. If the same mismatch exists at `$BASE`, it is pre-existing: `FUTURE WORK` at most | -| `changed`, dispatch | Read the C++ function and its Python counterpart. A divergence (a constant, a range, a tile set, a mask or layout rule that the other side does not share) is a `source-claim` at `IMPORTANT` or higher, quoting both sides. Consistent: no finding | -| `changed`, emitted code or arguments | Consistent with every copy, names and types: no finding. A row that says `types not compared` or `cannot compare` is not consistency: compare the types and sizes of both lists by hand. The variants changed on an architecture need a device run there | -| `changed`, descriptor or generic target | Check the loader's rules for the changed fields (`projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/DescriptorLoader.hpp`); a value the loader rejects or ignores is a finding. A membership change names the architectures whose archives change | -| `inert`, `outside` | Count them under **Checked, no finding**; list each `inert` reason once | +| A `mismatch` that holds | A `source-claim` at `BLOCKING` ([../policy.md](../policy.md): an ABI break without a migration path). **Where**: the producer line and every disagreeing copy. **Proof**: the command and both quoted lists. A `merged-only` mismatch: **Fix** is to rebase and update the named copy. The same mismatch at `$BASE` is pre-existing: `FUTURE WORK` at most | +| A dispatch divergence | A `source-claim` at `IMPORTANT` or higher, quoting both sides | +| A descriptor or generic-target value the loader rejects or ignores | A finding, citing the loader rule | +| No break (`changed` but consistent), `inert`, `outside` | No finding. Count them under **Checked, no finding**; list each `inert` reason once | -For every `changed` row without a finding, name the device run that would settle it: the ctest -entry (find it from the pack or kernel family name in the test sources and `ctest -N -R ` -in `RUN`'s build when one exists), the architecture, and the flags. Put each one under **Approval -requests** and add `AUTH: requires with the ingestor flags ON` under **Handoffs**; -`AUTH` decides whether a run executed. +For every `changed` row without a finding, put the device run the reference says to name under +**Approval requests**, and add `AUTH: requires with the ingestor flags ON` under +**Handoffs**; `AUTH` decides whether a run executed. ## Handoffs @@ -91,18 +70,5 @@ requests** and add `AUTH: requires with the ingestor flags ON` un conflicted files of a merged view the report marks `conflicted`, which it does not analyse. - `TESTB`: a contract with no test that would fail on the break you describe. -Known gaps: HIP-kind kernels (`ConvNative`, `PointwiseNative`) have their C++ argument lists -checked but not their HIP producers; a dynamic C++ signature (`Gfx1151WmmaAttentionNative`) -cannot be compared statically; one LLVM flavor is emitted per run, so an edit that changes only -another flavor's lowering reads `inert`. The symbol check sees a reader only through an import -of the defining module, `m.name` on it, or an attribute read in a file that names the class; it -cannot see `from m import *`, `getattr(m, "f")`, a dotted import read in full (`import a.b.m`, -then `a.b.m.f()`), or a module reached only through -`importlib.import_module(f"...{arch}...")` (the emitter covers that last one for shipped -variants, a bare `report` does not). A method removed from a base class and read only through a -subclass or an untyped receiver, in a file whose imports reach the class's module, gives a -`changed` row, not a `mismatch`: read the receivers. The emitter compares what goes into COMGR, -not the COMGR library that compiles it: a change to which comgr the packer loads is the -`BUILD` and `DEPS` lanes'. A rocKE C++ engine source (`rocke/platform/cpp/`) reads `inert` only -while no provider build file outside rocKE links the engine and the packer pins the Python -backend; the row says so, so check that both still hold for the change you review. +Known gaps are in the reference's *Known gaps*; a row that one of them touches is not proof either +way. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py index 2d85260c23d9..225ebc0fa578 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py @@ -52,7 +52,7 @@ # Changes under these trigger the IMPACT lane (lanes/runtime-impact.md): the kernel ingestor's # producers, packer, native packs, SDK ingestor headers, generic-target table and header, and -# generator configs. Keep in step with impact.py's SURFACE_TABLE. +# generator configs. Keep in step with SURFACE_TABLE in hipdnn-ingestor-impact's scripts/impact.py. IMPACT_PREFIXES = ( "dnn-providers/hip-kernel-provider/", "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/", diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md new file mode 100644 index 000000000000..7bfe307c1c6a --- /dev/null +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md @@ -0,0 +1,110 @@ +--- +name: hipdnn-ingestor-impact +description: "Check whether a rocm-libraries change (PR, branch, commit range or uncommitted work) breaks the hipDNN kernel ingestor at run time while the default build stays green: kernel argument lists (ABI) out of step with their C++ and test copies, also against the target branch's tip, dispatch and launch-surface edits, descriptor and generic-target changes, removed Python symbols still read, and which shipped rocKE variants' emitted code changed. Reads git objects; the optional emitter runs rocKE on the CPU. Ends in a Runtime impact: BREAKS | NEEDS-DEVICE | NONE | N/A line. For a full multi-agent review, use hipdnn-deep-review, whose IMPACT lane runs the same scripts." +argument-hint: "[ | branch: | .. | commit: | worktree] [emit]" +allowed-tools: Bash, Read, Grep, Glob +--- + +# hipDNN Ingestor Impact + +Use this skill when the user asks whether a change can break the kernel ingestor at run time, and +suggest it for any change under `dnn-providers/hip-kernel-provider/`, the SDK ingestor headers, +`projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` or `projects/hipdnn/tools/IngestorGenerator/`. +[reference.md](reference.md) holds the contracts, row statuses, emitter rules and known gaps; read +it before judging rows. + +The answer is advisory. It never builds, runs tests, dispatches CI or posts anything. + +## Prerequisites + +`git` 2.38 or later (`merge-tree --write-tree`), Python 3.10 or newer and PyYAML; the emitter also +needs Jinja2. Run from a rocm-libraries clone or worktree. On Windows, `python3` is often the +Microsoft Store stub: use `python` or `py -3`. + +## Workflow + +1. **Locate the scripts.** Skills are host-level, not tied to a repo checkout: default to the + scripts bundled with the skill you were invoked from, `/scripts/`, where + `` holds this file. Use the copy in a checkout only when developing this skill. + +2. **Pin the revisions** as full SHAs, once. Never pass `FETCH_HEAD` to a later command: another + fetch can rewrite it in between. + - **Open PR ``.** `gh pr view -R ROCm/rocm-libraries --json baseRefName,headRefOid`, then + `git fetch origin pull//head`. `HEAD` is `headRefOid`, `TIP` is + `git rev-parse origin/`, and `BASE` is `git merge-base `. + - **Merged PR.** The repository squash-merges: take `mergeCommit` from `gh pr view`; `BASE` is + its first parent (`^`), `HEAD` the merge commit, and there is no `TIP`. + - **Branch ``.** `TIP` is `origin/develop` (or the branch it targets), `HEAD` the branch + tip, `BASE` their merge base. + - **`..`** or **commit ``** (`^..`). No `TIP` unless the user names + a target that moved. + - **Uncommitted work.** The scripts read commits only. `git stash create` makes a commit of the + tracked and staged changes without touching the worktree, the index or any ref: `HEAD` is its + output and `BASE` is `HEAD` of the checkout. Stage a new file first, or it is left out. An + empty output means there is nothing to analyse. + +3. **Collect the static facts.** Drop `--target-tip ` here and below when there is no `TIP`: + + ```bash + python3 /scripts/impact.py report --repo . --base --head --target-tip + ``` + + Exit 1 means it could not run: report its message, never a verdict. + +4. **Run the emitter** when any rocKE or `hkp_pack` Python file changed in code, including rows the + report calls `inert`, or when the user asks for `emit`. It builds every shipped variant at both + sides on two CPU workers for a few minutes; on a shared host, run one at a time. Put its output + outside the repository and delete any earlier file first: + + ```bash + rm -f /emit.json + python3 /scripts/impact_emit.py run --repo . --base --head --out /emit.json + python3 /scripts/impact.py report --repo . --base --head --target-tip --emit /emit.json + ``` + + Here `` is any directory outside the repository. An emitter exit 1 leaves no proof: + keep the static report and say the emitted code was not compared. + +5. **Judge every row** whose status is not `inert` or `outside`, with + [reference.md](reference.md) *Judging the rows*. Read both sides at `` (`git show + :`), and at the merged tree when the row says `merged-only`. + +6. **Report** in the format below. + +## Report + +```markdown +Runtime impact: (judged: ) +Analysed: .. target tip: emitter: + +| Row | Status | Judgment | Evidence (path:line at HEAD or merged tree) | + +Device runs that would settle the changed rows +- on with the ingestor, rocKE and SDPA flags ON + +Not judged here: +Known gaps that apply: +``` + +Group `inert` and `outside` rows into one count each, with each `inert` reason listed once. Write +`none` for an empty section; do not drop it. + +## Never claim + +- **Device behaviour.** `NONE` means no runtime impact was found statically. It never means the + engine dispatches or computes correctly on a device. +- **CI green.** A build or test run with the ingestor flags OFF, or on runners with no GPU, is no + evidence about this question. State the flag values a quoted run actually had. +- **Cross-architecture results.** Never carry a result from one architecture to another, and expand + a generic target such as `gfx11-generic` to its members. +- **Safety of files not inspected.** An `outside` file was counted, not checked. + +## Related skills + +- [hipdnn-deep-review](../hipdnn-deep-review/SKILL.md): a full review whose `IMPACT` lane runs these + scripts and turns the rows into findings. +- [hipdnn-superbuild](../hipdnn-superbuild/SKILL.md) and + [hipdnn-superbuild-test](../hipdnn-superbuild-test/SKILL.md): the flags-ON build and the device + runs this skill names. +- [hipdnn-ingestor-engine](../hipdnn-ingestor-engine/SKILL.md): building or extending an ingestor + integration. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/agents/openai.yaml b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/agents/openai.yaml new file mode 100644 index 000000000000..adfa916dad7a --- /dev/null +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/agents/openai.yaml @@ -0,0 +1,4 @@ +interface: + display_name: "hipDNN Ingestor Impact" + short_description: "Find kernel ingestor changes that break at run time" + default_prompt: "Use $hipdnn-ingestor-impact to check whether the given hipDNN change breaks the kernel ingestor at run time: run its scripts, judge each row from the files at the analysed revision, and report the rollup and the device runs that would settle it." diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md new file mode 100644 index 000000000000..22d65cee71d8 --- /dev/null +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -0,0 +1,121 @@ +# Ingestor impact reference + +What the two scripts check, what their rows mean, and how to judge them. [SKILL.md](SKILL.md) runs +them on their own; the `IMPACT` lane of +[hipdnn-deep-review](../hipdnn-deep-review/lanes/runtime-impact.md) runs them inside a review and +maps the judged rows to findings. + +The kernel ingestor C++ compiles only with `HIPDNN_ENABLE_KERNEL_INGESTOR` ON, so judge a change as +built with the ingestor, rocKE and SDPA flags ON, whatever their defaults. A default build compiles +the ingestor out, and several contracts below are restated by hand with no compiler check, so a +change that breaks them leaves the default build green. + +## Contracts + +| Contract | Producer | Copies the change must keep in step | +|---|---|---| +| Kernel arguments (ABI) | rocKE builder `b.param(...)` sequence under `dnn-providers/hip-kernel-provider/rocke/library/kernels/` | static `KernelArgument` lists in `src/engines/kernel_ingestor_engine/packs/*.cpp`, literal pins in tests, descriptor `signature` arrays | +| Launch surface | Python grid, block, LDS and tile tables | the `cpp_mirror` files a `launch_surface:` entry names in `projects/hipdnn/tools/IngestorGenerator/configs/*.engine.yaml` | +| Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | +| Shipped variants | `*.kdp.json` (DVC-held for gfx950; the emitter regenerates its variant list with the generator's registry enumerator, so a changed DVC pointer stays `changed`: the file's other fields are not read) | loader, native packs | +| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` and `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` | packer and loader | +| Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | + +Test code, CMake, CI test-category lists and `requirements*.txt` are not judged here: they are +`outside` for status, but tests are still searched as copies and readers, so a stale pin or a stale +import is reported. + +## The scripts + +- `scripts/impact.py` reads git objects only (`cat-file`, `diff`, `ls-tree`, `grep`, + `merge-tree`); it writes no ref, index or worktree. Its subcommands are `files`, `kernargs`, + `symbols`, `surfaces`, `data` and `report`; `report` runs all of them and ends in the rollup line. +- `scripts/impact_emit.py` imports and runs rocKE code from the two trees: a `git archive` extract + per side, in a child process whose whole process tree is killed at its timeout, with an + environment reduced to system, temp, locale and Python user-site variables, so credentials such + as `GH_TOKEN` are not passed. That is the same code execution as building the head. It builds + every shipped variant at both sides on the CPU (no GPU, comgr or network): a few minutes, with + the two sides on two concurrent workers. + +Both exit 0 when the analysis ran, whatever it found, and 1 with one line (`impact: ` or +`impact_emit: `) on stderr when they could not run. An exit 1 is never a verdict. + +## Views + +The report covers base to head and, when `--target-tip` differs from the base, the head merged into +that tip (`git merge-tree --write-tree`). A row that holds only in the merged view is marked +`merged-only`: two PRs that each pass alone, where the other one landed a copy this diff cannot +see. A conflicted merge is still analysed except for its conflicted files, which one `changed` row +stands in for when one of them is on the surface or a test. + +## Row statuses and the rollup + +| Status | Meaning | +|---|---| +| `mismatch` | A producer and a copy disagree, or a removed symbol still has a reader | +| `changed` | A contract side changed, or static analysis cannot settle it; a surface file the script does not understand is always `changed` | +| `inert` | Touched, and provably without runtime effect; the row gives the reason | +| `outside` | Not on the surface; counted, never dropped | + +The last line of `report` is the rollup: +`Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`. +`BREAKS` is any `mismatch`, `NEEDS-DEVICE` any `changed`, `NONE` all surface rows `inert`, `N/A` no +surface file. An empty diff is an error, never `NONE`. A `not judged here:` note counts the files +left to other checks: `BUILD` is CMake and CI test-category lists, `DEPS` is `requirements*.txt` +(named after the hipdnn-deep-review lanes that judge them). + +## The emitter + +Run it when any rocKE or `hkp_pack` Python file changed in code, including rows the report calls +`inert`: the static reachability it uses (a module counts as shipped when a shipped builder imports +it) misses a call through a dynamic lookup. It records each variant's argument list, IR digest and +COMGR inputs (ISA name, codegen options, fresh-process compile); `impact.py report --emit` folds the +result into the rows. + +- The emitter exiting 1 (it could not run, or a side timed out) leaves no proof. Never fold an emit + file from an earlier run. `impact.py` refuses an emit file made for another base or head, or one + it cannot parse. +- Record the flavor it used. One LLVM flavor is emitted per run (`--flavor` picks another). +- A family listed under `failures` or a KDP under `not_inspected` leaves its rows `changed`: never + read a failure as equal. +- An all-equal emit folds only the rows of modules a built family imports, and never a row on code + that runs after lowering (`rocke/helpers/compile.py`, `rocke/runtime/`): the IR digest does not + cover it. A static kernarg `mismatch` is never downgraded. + +## Judging the rows + +Read both sides at the head revision, and at the merged tree when the row says `merged-only`, before +deciding. + +| Status | Judgment | +|---|---| +| `mismatch` | A break: quote the producer line and every disagreeing copy. A `merged-only` mismatch breaks the target once merged; the fix is to rebase and update the named copy. If the same mismatch exists at the base, it predates the change | +| `changed`, dispatch | Read the C++ function and its Python counterpart. A divergence (a constant, a range, a tile set, a mask or layout rule that the other side does not share) is a break; quote both sides. Consistent: no break | +| `changed`, emitted code or arguments | Consistent with every copy, names and types: no break. A row that says `types not compared` or `cannot compare` is not consistency: compare the types and sizes of both lists by hand. The variants changed on an architecture need a device run there | +| `changed`, descriptor or generic target | Check the loader's rules for the changed fields (`projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/DescriptorLoader.hpp`); a value the loader rejects or ignores is a break. A membership change names the architectures whose archives change | +| `inert`, `outside` | Count them; list each `inert` reason once | + +For every `changed` row that is not a break, name the device run that would settle it: the ctest +entry (find it from the pack or kernel family name in the test sources, and `ctest -N -R ` in +a flags-ON build when one exists), the architecture, and the flags. + +## Known gaps + +- HIP-kind kernels (`ConvNative`, `PointwiseNative`) have their C++ argument lists checked but not + their HIP producers. +- A dynamic C++ signature (`Gfx1151WmmaAttentionNative`) cannot be compared statically. +- One LLVM flavor is emitted per run, so an edit that changes only another flavor's lowering reads + `inert`. +- The symbol check sees a reader only through an import of the defining module, `m.name` on it, or + an attribute read in a file that names the class. It cannot see `from m import *`, + `getattr(m, "f")`, a dotted import read in full (`import a.b.m`, then `a.b.m.f()`), or a module + reached only through `importlib.import_module(f"...{arch}...")` (the emitter covers that last one + for shipped variants; a bare `report` does not). +- A method removed from a base class and read only through a subclass or an untyped receiver, in a + file whose imports reach the class's module, gives a `changed` row, not a `mismatch`: read the + receivers. +- The emitter compares what goes into COMGR, not the COMGR library that compiles it: a change to + which comgr the packer loads is a build and dependency question. +- A rocKE C++ engine source (`rocke/platform/cpp/`) reads `inert` only while no provider build file + outside rocKE links the engine and the packer pins the Python backend; the row says so, so check + that both still hold for the change. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py similarity index 98% rename from projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py rename to projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 828d788df3a8..69019ee02d45 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -5,7 +5,6 @@ Usage: impact.py --repo DIR --base REV --head REV [--target-tip REV] [--json] - impact.py --round-dir DIR [--json] impact.py report ... --emit FILE.json Emits facts, never severity beyond the row statuses and the rollup line. Reads git objects only @@ -20,17 +19,17 @@ merge that writes no tree skips the merged view as `changed`. A row that holds only in the merged view is marked `merged-only`: that is the race class (a parallel PR landed a copy the diff cannot see). -`--round-dir` reads PIN.md (CLONE, BASE, HEAD, TARGET_TIP); a tip equal to BASE gives no merged -view. +A tip equal to BASE gives no merged view. Row status: mismatch (producer and a copy disagree, a removed symbol still has a reader), changed (a contract side changed, or static analysis cannot settle it; a surface file not understood is always changed), inert (touched, provably without runtime effect), outside (not on the surface; counted, never dropped). Test files and CMake are outside for status but are still searched as -kernarg copies and as symbol readers. CMake and CI test-category lists belong to the BUILD lane, -`requirements*.txt` to DEPS; the rollup names how many it left to them. A rocKE C++ engine source -is `inert` while no provider build file outside rocKE links the engine and the packer pins the -Python lowering backend; any other non-Python rocKE file is not understood. +kernarg copies and as symbol readers. CMake and CI test-category lists are left to BUILD, +`requirements*.txt` to DEPS (the hipdnn-deep-review lanes that judge them); the rollup names how +many it left to each. A rocKE C++ engine source is `inert` while no provider build file outside +rocKE links the engine and the packer pins the Python lowering backend; any other non-Python rocKE +file is not understood. Rollup (last line of `report`): Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected[; not judged here: ...]) @@ -56,8 +55,10 @@ from dataclasses import dataclass, field from pathlib import Path, PurePosixPath -sys.path.insert(0, str(Path(__file__).resolve().parent)) -import reviewlib as rl # noqa: E402 + +class Fail(Exception): + """A user-facing failure; main() prints it as one line and exits 1.""" + # -- Tables ------------------------------------------------------------------- @@ -121,8 +122,8 @@ ("provider", HKP + "*"), ) OUTSIDE_TAGS = {"carve-out", "test", "test-categories", "cmake", "deps", "other"} -#: Outside tags another deep-review lane judges; the rollup names them so N/A is not read -#: as "no impact" for these files. +#: Outside tags another check judges (named after the hipdnn-deep-review lane); the rollup names +#: them so N/A is not read as "no impact" for these files. OWNER_LANES = {"cmake": "BUILD", "test-categories": "BUILD", "deps": "DEPS"} #: Tags whose files are read as copies or readers although they are outside for status. SEARCHED_TAGS = {"test"} @@ -205,7 +206,7 @@ def git(repo, *args, stdin=None, ok=(0,)) -> bytes: ) if done.returncode not in ok: err = done.stderr.decode("utf-8", errors="replace").strip() - raise rl.Fail(f"git {args[0]}: {err or 'exit ' + str(done.returncode)}") + raise Fail(f"git {args[0]}: {err or 'exit ' + str(done.returncode)}") return done.stdout @@ -217,7 +218,7 @@ def resolve_revs(repo, revs) -> list: ) lines = done.stdout.decode("utf-8", errors="replace").split() if done.returncode != 0 or len(lines) != len(args): - raise rl.Fail(f"cannot resolve {', '.join(revs)} to a commit") + raise Fail(f"cannot resolve {', '.join(revs)} to a commit") return list(zip(lines[0::2], lines[1::2])) @@ -629,7 +630,7 @@ def load_data(path: str, text: str): else: data = require_yaml().safe_load(text) return json.dumps(data, sort_keys=True, default=repr) - except rl.Fail: + except Fail: raise except Exception: # noqa: BLE001 - any parse failure means "not data-equal" return None @@ -639,7 +640,7 @@ def require_yaml(): try: import yaml # noqa: PLC0415 - optional until a YAML file needs parsing except ImportError: - raise rl.Fail( + raise Fail( "PyYAML is required to read *.yaml (python -m pip install pyyaml)" ) from None return yaml @@ -2878,18 +2879,18 @@ def load_emit(path) -> dict: try: data = json.loads(Path(path).read_text(encoding="utf-8")) except (OSError, ValueError) as exc: - raise rl.Fail(f"cannot read emit file {path}: {exc}") from None + raise Fail(f"cannot read emit file {path}: {exc}") from None if ( not isinstance(data, dict) or data.get("schema") != EMIT_SCHEMA or data.get("tool") != "impact_emit" ): - raise rl.Fail( + raise Fail( f"{path} is not impact_emit schema {EMIT_SCHEMA} output; rerun impact_emit.py" ) problem = emit_shape_problem(data) if problem: - raise rl.Fail(f"emit file {path}: {problem}") + raise Fail(f"emit file {path}: {problem}") return data @@ -3179,17 +3180,9 @@ def render_files(ctx_changes: list, rows: list) -> str: def resolve(args): """(repo, (base commit, tree), (head commit, tree), (tip commit, tree) or None).""" - if args.round_dir: - rd = args.round_dir - repo = rl.pin_value(rd, "CLONE") - base, head = rl.pin_value(rd, "BASE"), rl.pin_value(rd, "HEAD") - if not (repo and base and head): - raise rl.Fail(f"PIN.md in {rd} lacks CLONE, BASE or HEAD") - tip = args.target_tip or rl.pin_value(rd, "TARGET_TIP") - else: - if not (args.repo and args.base and args.head): - raise rl.Fail("give --repo, --base and --head, or --round-dir") - repo, base, head, tip = args.repo, args.base, args.head, args.target_tip + if not (args.repo and args.base and args.head): + raise Fail("give --repo, --base and --head") + repo, base, head, tip = args.repo, args.base, args.head, args.target_tip found = resolve_revs(repo, [base, head] + ([tip] if tip else [])) tip_pair = found[2] if tip and found[2][0] != found[0][0] else None return repo, found[0], found[1], tip_pair @@ -3198,14 +3191,14 @@ def resolve(args): def run(args): repo, (base, base_tree_id), (head, head_tree_id), tip_pair = resolve(args) if base_tree_id == head_tree_id: - raise rl.Fail("empty diff") + raise Fail("empty diff") base_tree, head_tree = Tree(repo, base_tree_id), Tree(repo, head_tree_id) emit = None if args.cmd == "report" and args.emit: emit = load_emit(args.emit) made_for = (emit["base_tree"], emit["head_tree"]) if made_for != (base_tree_id, head_tree_id): - raise rl.Fail( + raise Fail( f"emit file {args.emit} was made for trees {made_for[0][:12]}..{made_for[1][:12]}, " f"not {base_tree_id[:12]}..{head_tree_id[:12]}; run impact_emit.py on this base and head" ) @@ -3220,7 +3213,7 @@ def run(args): ] ctx, rows = analyse_view(base_tree, head_tree, "base..head") if not ctx.changes: - raise rl.Fail("empty diff") + raise Fail("empty diff") views[0]["files"] = len(ctx.changes) per_view = [rows] if tip_pair: @@ -3333,17 +3326,13 @@ def main(argv=None) -> int: "--target-tip", help="target branch tip; adds the merged view when it differs from base", ) - p.add_argument( - "--round-dir", - help="round directory holding PIN.md (CLONE, BASE, HEAD, KEY)", - ) p.add_argument("--json", action="store_true", help="machine-readable output") if name == "report": p.add_argument("--emit", help="impact_emit.py output to fold in") args = parser.parse_args(argv) try: cmd(args) - except rl.Fail as exc: + except Fail as exc: print(f"impact: {exc}", file=sys.stderr) return 1 return 0 diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py similarity index 100% rename from projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/impact_emit.py rename to projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py similarity index 98% rename from projects/hipdnn/tools/ai/tests/test_deep_review_impact.py rename to projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index 2befe07a0686..bb4134775000 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -1,6 +1,6 @@ # Copyright (c) Advanced Micro Devices, Inc., or its affiliates. # SPDX-License-Identifier: MIT -"""Unit tests for the hipdnn-deep-review impact.py helper (kernel ABI and dispatch facts of a diff). +"""Unit tests for hipdnn-ingestor-impact's impact.py (kernel ABI and dispatch facts of a diff). Every case builds a throwaway git repository under tmp_path with paths the script classifies, so no test depends on this clone's history or on rocKE. Git runs for real with the user's configuration @@ -18,7 +18,7 @@ SCRIPT = ( Path(__file__).resolve().parent.parent / "skills" - / "hipdnn-deep-review" + / "hipdnn-ingestor-impact" / "scripts" / "impact.py" ) @@ -92,7 +92,7 @@ def build_attn(b, spec): @pytest.fixture(scope="module") def mod() -> ModuleType: - spec = importlib.util.spec_from_file_location("hipdnn_deep_review_impact", SCRIPT) + spec = importlib.util.spec_from_file_location("hipdnn_ingestor_impact", SCRIPT) assert spec and spec.loader, f"could not load {SCRIPT}" module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) @@ -239,15 +239,6 @@ def test_pin_on_target_tip_only_is_mismatch_in_merged_view_only(mod, capsys, tmp "changed" ] assert data["rollup"] == "Runtime impact: BREAKS" - # --round-dir takes the same tip from PIN.md's TARGET_TIP - rd = tmp_path / "round" - rd.mkdir() - (rd / "PIN.md").write_text( - f"CLONE: {repo}\nBASE: {base}\nHEAD: {head}\nTARGET_TIP: {tip}\n", - encoding="utf-8", - ) - assert mod.main(["report", "--round-dir", str(rd), "--json"]) == 0 - assert json.loads(capsys.readouterr().out)["rollup"] == "Runtime impact: BREAKS" def test_merge_conflict_on_a_surface_file_marks_the_merged_view_changed( diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py similarity index 99% rename from projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py rename to projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index 04c1f921f81e..000e982bab2c 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -1,6 +1,6 @@ # Copyright (c) Advanced Micro Devices, Inc., or its affiliates. # SPDX-License-Identifier: MIT -"""Tests for the hipdnn-deep-review impact_emit.py script. +"""Tests for the hipdnn-ingestor-impact impact_emit.py script. A tiny fake provider tree (fake `hkp_pack` and `rocke` packages with the helper names the child resolves, a builder module, a KDP) lives in a temporary git repo; the lowering @@ -20,7 +20,7 @@ SCRIPT = ( Path(__file__).resolve().parent.parent / "skills" - / "hipdnn-deep-review" + / "hipdnn-ingestor-impact" / "scripts" / "impact_emit.py" ) @@ -108,9 +108,7 @@ def kdp_json(variants=KDP_VARIANTS, arch=("gfx950",), target=None): @pytest.fixture def emit(): - spec = importlib.util.spec_from_file_location( - "hipdnn_deep_review_impact_emit", SCRIPT - ) + spec = importlib.util.spec_from_file_location("hipdnn_ingestor_impact_emit", SCRIPT) assert spec and spec.loader module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) diff --git a/projects/hipdnn/tools/ai/validate-skills.py b/projects/hipdnn/tools/ai/validate-skills.py index 2102dfe86106..0d91d6bbf0c0 100644 --- a/projects/hipdnn/tools/ai/validate-skills.py +++ b/projects/hipdnn/tools/ai/validate-skills.py @@ -48,6 +48,7 @@ "pin.py", "reviewlib.py", ), + "hipdnn-ingestor-impact": ("impact.py", "impact_emit.py"), "hipdnn-trigger-ci": ("trigger_ci.py",), "hipdnn-uhd": ( "candidate_tree.py", @@ -65,6 +66,7 @@ { "hipdnn-deep-review", "hipdnn-ingestor-engine", + "hipdnn-ingestor-impact", "hipdnn-integration-testing", "hipdnn-pr-quality", "hipdnn-superbuild", From 34b5b4a94533122d8b7257dfd37f5d2c57feff24 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Tue, 27 Oct 2026 16:16:06 -0600 Subject: [PATCH 05/13] fix(hipdnn): pair ingestor impact checks with multi-launch and method surfaces New ingestor engines read as BREAKS when nothing was broken. A launch_surface entry whose python_source names a method, such as a launch_block defined on a tuning spec class, was reported as pointing at a missing function, because impact.py looked the symbol up at module level only while launch_surface.py --check accepts any function of that name. A declared kernargs pair whose function is a SignatureBuilder factory, which has no b.param list, was reported as a missing builder. Every static list of a multi-launch pack was compared with the one declared builder, so the reduce kernel's argument list disagreed with the segment builder. A copy of an argument list that lacks a parameter the builder newly adds only conditionally was counted as stale, although every variant that leaves the condition off still passes that list. And the emitter lowered a builder returning a KernelModule as a single kernel, so every variant of such a family failed and its rows could not be settled. impact.py now resolves launch_surface symbols by name anywhere in the module and reports a function missing only when no function of that name exists. A declared kernargs pair compares each static C++ list with the b.param builder its names match, searched in the surface's module and the manifest's own source module with the declared function first, and names that builder in the row; a list that matches no builder is still compared with the closest one and reported as a mismatch, and a mirror that holds no KernelArgument list says so. A copy that still matches the new list without its conditional parameters is changed rather than mismatched. impact_emit.py lowers a KernelModule kernel by kernel, as the packer does, and records each companion launch's argument list; its output is schema 4. impact.py folds it by comparing each static C++ list with the launch it mirrors, so a companion's changed arguments are checked like the primary's. The reference documents the pairing rules and the remaining gaps. ALMIOPEN-2834 --- .../hipdnn-ingestor-impact/reference.md | 30 +- .../hipdnn-ingestor-impact/scripts/impact.py | 311 +++++++++++++----- .../scripts/impact_emit.py | 60 +++- .../tools/ai/tests/test_ingestor_impact.py | 184 ++++++++++- .../ai/tests/test_ingestor_impact_emit.py | 61 +++- 5 files changed, 548 insertions(+), 98 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md index 22d65cee71d8..e6bfb0c9d219 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -25,6 +25,21 @@ Test code, CMake, CI test-category lists and `requirements*.txt` are not judged `outside` for status, but tests are still searched as copies and readers, so a stale pin or a stale import is reported. +How the copies are paired: + +- A `launch_surface` entry's `python_source` symbol resolves as `launch_surface.py --check` + resolves it: any function of that name in the module, a method or nested function included. It + is reported missing only when no function of that name exists. +- A declared `kernargs` pair compares each static C++ list in its `cpp_mirror` with the builder + whose `b.param` list it matches, searched in the surface's module and the manifest's own + `source` module, the declared function first. A multi-launch pack holds one list per launch (a + segment and a reduce kernel), and a declared function may be a `SignatureBuilder` factory with no + `b.param` list; the row then names the builder it was paired with. A list that matches no + builder is compared with the builder it shares most names with, and is a `mismatch`. +- A copy that holds the old list is `changed`, not `mismatch`, when it still matches the new + list with the new conditional parameters left out: variants that do not enable them pass that + list. The variants that do enable them are what the emitter or a device run settles. + ## The scripts - `scripts/impact.py` reads git objects only (`cat-file`, `diff`, `ls-tree`, `grep`, @@ -69,8 +84,10 @@ left to other checks: `BUILD` is CMake and CI test-category lists, `DEPS` is `re Run it when any rocKE or `hkp_pack` Python file changed in code, including rows the report calls `inert`: the static reachability it uses (a module counts as shipped when a shipped builder imports it) misses a call through a dynamic lookup. It records each variant's argument list, IR digest and -COMGR inputs (ISA name, codegen options, fresh-process compile); `impact.py report --emit` folds the -result into the rows. +COMGR inputs (ISA name, codegen options, fresh-process compile); a builder that returns a +`KernelModule` is lowered kernel by kernel, as the packer does, and each companion launch's +argument list is recorded and compared too. `impact.py report --emit` folds the result into the +rows. - The emitter exiting 1 (it could not run, or a side timed out) leaves no proof. Never fold an emit file from an earlier run. `impact.py` refuses an emit file made for another base or head, or one @@ -119,3 +136,12 @@ a flags-ON build when one exists), the architecture, and the flags. - A rocKE C++ engine source (`rocke/platform/cpp/`) reads `inert` only while no provider build file outside rocKE links the engine and the packer pins the Python backend; the row says so, so check that both still hold for the change. +- A static list is paired with the builder its names match. A C++ list that by mistake matches a + different builder of the same modules exactly is compared with that one, not the launch it is + used for; the row names the pairing, so check it. +- The emitter compares emitted arguments with the static C++ lists only for variants whose + arguments changed between base and head. The variants of a new engine have no base side, so + their lists are checked statically only. +- Only `std::vector` functions are read as C++ argument lists. A list built by + helper calls instead (`gfx942Pointer("query_ptr", ...)`) is not parsed: its declared pair reads + `changed` with `cannot compare`, so compare it with the builder by hand. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 69019ee02d45..5e76e9a5c7a4 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -35,8 +35,9 @@ Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected[; not judged here: ...]) | N/A (no surface file[; not judged here: ...]) -`report --emit FILE.json` folds in impact_emit.py output (schema 3), which must have been made for -the same base and head trees: changed params vs a static C++ list is a mismatch; changed IR or +`report --emit FILE.json` folds in impact_emit.py output (schema 4), which must have been made for +the same base and head trees: changed params of any launch (a KernelModule builder's companions +included) vs the static C++ list that mirrors that launch is a mismatch; changed IR or COMGR inputs are `changed` for that arch; when every family is equal (nothing changed, nothing failed, nothing not inspected) the rows that were `changed` only because a shipped module changed become `inert`, but only on modules a family with built variants imports, and never on code that @@ -943,6 +944,16 @@ def visit(node, qual): return found +def defs_named(tree, name) -> list: + """[(qualname, FunctionDef)] of every def called `name`, methods and nested ones included, + in source order: how `launch_surface.py --check` resolves a `python_source` symbol. + """ + if tree is None or not name: + return [] + found = [(q, n) for q, n in function_nodes(tree).items() if n.name == name] + return sorted(found, key=lambda item: item[1].lineno) + + def string_lists(path: str, text: str, tree=None) -> list: """Ordered string-literal lists of 2+ items: [(names, start, end, function)]. Python by AST; other text by quoted runs inside `{...}` or `[...]`.""" @@ -1219,10 +1230,26 @@ class Surface: manifest: str name: str module: str - function: str + #: The symbol `python_source` names, or None when its locator is prose. + function: str | None cpp_mirror: str test: str arch: str | None + #: The manifest's own `source` module and `builder`: what the packer compiles. + builder_module: str | None = None + builder: str | None = None + + +#: `launch_surface.py`'s locator symbol: an identifier, or `Class::method` for C++. +_LEADING_SYMBOL = re.compile(r"[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*") + + +def leading_symbol(locator: str): + """The symbol a `python_source` locator names, by `launch_surface.py`'s rule: the first + word after the path when it is an identifier, else None (the locator is prose).""" + rest = locator.split(":", 1)[1].strip() if ":" in locator else locator.strip() + token = rest.split(None, 1)[0] if rest.split() else "" + return token if _LEADING_SYMBOL.fullmatch(token) else None def load_surfaces(tree: Tree) -> list: @@ -1244,21 +1271,26 @@ def load_surfaces(tree: Tree) -> list: manifest.get("launch_surface"), list ): continue - root = str(manifest.get("provider_root") or HKP).rstrip("/") + "/" + library = ( + str(manifest.get("provider_root") or HKP).rstrip("/") + "/rocke/library/" + ) + source, builder = manifest.get("source"), manifest.get("builder") + own = isinstance(source, str) and isinstance(builder, str) for entry in manifest["launch_surface"]: - source = entry.get("python_source") if isinstance(entry, dict) else None - if not isinstance(source, str) or ":" not in source: + locator = entry.get("python_source") if isinstance(entry, dict) else None + if not isinstance(locator, str) or ":" not in locator: continue - module, function = source.rsplit(":", 1) out.append( Surface( path, str(entry.get("name")), - root + "rocke/library/" + module.strip(), - function.strip(), + library + locator.split(":", 1)[0].strip(), + leading_symbol(locator), str(entry.get("cpp_mirror") or "").split(":", 1)[0].strip(), str(entry.get("test") or "").split(":", 1)[0].strip(), str(manifest.get("arch") or "") or None, + library + source.strip() if own else None, + builder.strip() if own else None, ) ) return out @@ -1862,18 +1894,20 @@ class Work: line: int -def find_copies(ctx: Ctx, work: list) -> dict: - """(path, qual) -> counterparts holding the old kernarg list, searched in both trees by the - ordered-name sequence. A copy still holding the old list after is unmoved; one that held it - before and no longer does moved with the producer.""" +def find_copies(ctx: Ctx, work: list) -> tuple: + """((path, qual) -> counterparts holding the old kernarg list, (path, line, moved) -> the + names that counterpart holds), searched in both trees by the ordered-name sequence. A copy + still holding the old list after is unmoved; one that held it before and no longer does moved + with the producer.""" targets = {} for w in work: for names in (w.old.names, w.old.unconditional): if len(names) > 1: targets.setdefault(tuple(names), set()).add((w.change.path, w.qual)) copies = {(w.change.path, w.qual): [] for w in work} + held_names = {} if not targets: - return copies + return copies, held_names patterns = [f'"{n}"' for n in sorted({names[0] for names in targets})] hits = grep_trees([ctx.before, ctx.after], patterns, ["-F", "-l"]) candidates = { @@ -1898,7 +1932,8 @@ def find_copies(ctx: Ctx, work: list) -> dict: entry = cp(path, before[0], True) if entry and entry not in copies[target]: copies[target].append(entry) - return copies + held_names[(path, entry["line"], entry["moved"])] = list(names) + return copies, held_names def kernarg_rows(ctx: Ctx) -> list: @@ -1945,10 +1980,17 @@ def kernarg_rows(ctx: Ctx) -> list: "; a type or attribute change moves the kernarg layout", arch=arch, foldable=True)) # fmt: skip if work: - copies = find_copies(ctx, work) + copies, held_names = find_copies(ctx, work) for w in work: found = copies[(w.change.path, w.qual)] unmoved = [c for c in found if not c["moved"]] + # A copy without params the new list adds only conditionally is still one of its + # forms: only the variants that enable them disagree, which the emitter settles. + stale = [ + c for c in unmoved + if w.new is None + or not compatible(held_names[(c["path"], c["line"], False)], w.new) + ] # fmt: skip if w.new is None: change = "absent after (deleted or renamed)" else: @@ -1959,54 +2001,93 @@ def kernarg_rows(ctx: Ctx) -> list: if found else "no copy found" ) + if unmoved and not stale: + where += ( + "; each still matches the new list without its conditional params, so " + "only variants that enable them can disagree" + ) rows.append(make_row("kernargs", w.change.path, w.line, w.qual, - "mismatch" if unmoved else "changed", + "mismatch" if stale else "changed", f"b.param list of {w.qual} changed ({change}); {where}", - found, arch=arch_of(w.change.path), foldable=not unmoved)) # fmt: skip + found, arch=arch_of(w.change.path), foldable=not stale)) # fmt: skip rows += declared_kernarg_rows(ctx, seen_lists) return rows +def builder_candidates(ctx: Ctx, surface: Surface) -> list: + """[(module, qual, ParamList, tree)] of the `b.param` builders a declared pair's static lists + can be compared with, best first: the declared function, the manifest's `builder`, then every + other builder of the surface's module and of the manifest's `source` module. A multi-launch + pack holds one list per launch, and a declared function may be a signature factory with no + `b.param` list of its own.""" + found = [] + for module in dict.fromkeys( + m for m in (surface.module, surface.builder_module) if m + ): + tree = ctx.after.ast(module) + if tree is not None: + found += [(module, q, pl, tree) for q, pl in param_lists(tree).items()] + + def rank(cand): + bare = cand[1].rsplit(".", 1)[-1] + if cand[0] == surface.module and bare == surface.function: + return 0 + return 1 if cand[0] == surface.builder_module and bare == surface.builder else 2 + + return sorted(found, key=rank) + + +def pair_builder(names: list, cands: list): + """(candidate, matches) for one static list: the best-ranked candidate whose params it + matches; else the related candidate sharing most names, which it disagrees with; else + (None, False).""" + + def shared(cand): + return len(set(names) & set(cand[2].names)) + + for pool, matches in ( + ([c for c in cands if compatible(names, c[2])], True), + ([c for c in cands if related(names, c[2].names)], False), + ): + if pool: + return max(pool, key=shared), matches # max keeps the first of equals + return None, False + + def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: - """Compare each declared `kernargs` pair's static C++ lists with its builder, after.""" + """Compare each declared `kernargs` pair's static C++ lists with the builders they mirror, + after: each list with the builder whose `b.param` list it matches (see builder_candidates). + """ rows = [] for surface in ctx.surfaces("after"): if surface.name != "kernargs" or not surface.cpp_mirror: continue - producer, mirror = ctx.changes.get(surface.module), ctx.changes.get( - surface.cpp_mirror - ) - if producer is None and mirror is None: + paths = (surface.module, surface.builder_module, surface.cpp_mirror) + sides = [ctx.changes.get(p) for p in dict.fromkeys(p for p in paths if p)] + if not any(sides): continue + moved = any(c is not None and c.cls in MOVING for c in sides) + mirror = ctx.changes.get(surface.cpp_mirror) + declared = surface.function or PurePosixPath(surface.module).name tree = ctx.after.ast(surface.module) - lists = param_lists(tree) if tree is not None else {} - pl = lists.get(surface.function) text = ctx.after.text(surface.cpp_mirror) - if pl is None or text is None: - what = "builder" if pl is None else "C++ mirror" - rows.append(make_row("kernargs", surface.cpp_mirror, 0, f"declared:{surface.function}", - "mismatch", f"declared kernargs pair is broken at the after side: {what} of " - f"{surface.module}:{surface.function} is missing", arch=surface.arch)) # fmt: skip + missing = ( + f"python module {surface.module}" if tree is None + else f"function {surface.function} of {surface.module}" + if surface.function and not defs_named(tree, surface.function) + else f"C++ mirror {surface.cpp_mirror}" if text is None else None + ) # fmt: skip + if missing: + rows.append(make_row("kernargs", surface.cpp_mirror, 0, f"declared:{declared}", "mismatch", + f"declared kernargs pair is broken at the after side: {missing} is missing", + arch=surface.arch)) # fmt: skip continue - counter = [ - cp( - surface.module, - pl.lineno, - producer is not None and producer.cls in MOVING, - ) - ] - moved = (producer is not None and producer.cls in MOVING) or ( - mirror is not None and mirror.cls in MOVING - ) - sizes = dict( - zip( - pl.names, - map(decl_size, resolved_decls(tree, surface.function, pl.decls)), - ) - ) + cands = builder_candidates(ctx, surface) + fallback = [cp(surface.module, 0, ctx.moved(surface.module))] compared, closest = False, 0 arch = surface.arch or arch_of(surface.cpp_mirror) - for entry in cpp_kernel_lists(text): + lists = cpp_kernel_lists(text) + for entry in lists: key = (surface.cpp_mirror, entry["line"]) if key in reported: compared = True @@ -2017,20 +2098,31 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: kind = "dynamic" if entry["dynamic"] else "static but unnamed" rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", "changed", f"{entry['func']}() is a {kind} kernarg signature: it cannot be " - f"compared with {surface.function} statically", counter, arch=arch)) # fmt: skip + f"compared with {declared} statically", fallback, arch=arch)) # fmt: skip continue - if not related(entry["names"], pl.names) and not compatible( - entry["names"], pl - ): - closest = max(closest, len(set(entry["names"]) & set(pl.names))) + cand, matches = pair_builder(entry["names"], cands) + if cand is None: + for c in cands: + closest = max(closest, len(set(entry["names"]) & set(c[2].names))) continue compared = True - if not compatible(entry["names"], pl): + module, qual, pl, ptree = cand + counter = [cp(module, pl.lineno, ctx.moved(module))] + if module == surface.module and qual.rsplit(".", 1)[-1] == surface.function: + paired = qual + else: + paired = ( + f"{qual} (paired by its names; the manifest declares {declared})" + ) + if not matches: rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", - "mismatch", f"static list {entry['func']}() disagrees with builder {surface.function} " + "mismatch", f"static list {entry['func']}() disagrees with builder {paired} " f"({list_diff(entry['names'], pl.names)}): {entry['names']} vs {pl.marked()}", counter, arch=arch)) # fmt: skip continue + sizes = dict( + zip(pl.names, map(decl_size, resolved_decls(ptree, qual, pl.decls))) + ) differ, unknown = [], [] for name, size in zip(entry["names"], entry["sizes"]): want = sizes.get(name) @@ -2042,7 +2134,7 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: ) if differ: rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", - "mismatch", f"static list {entry['func']}() names agree with builder {surface.function} " + "mismatch", f"static list {entry['func']}() names agree with builder {paired} " f"but types do not: {'; '.join(differ[:4])}", counter, arch=arch)) # fmt: skip elif moved: if not unknown: @@ -2052,11 +2144,22 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: else: note = f"names agree; types not compared for {', '.join(unknown)}" rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", - "changed", f"{entry['func']}() vs {surface.function}: {note}", counter, arch=arch)) # fmt: skip + "changed", f"{entry['func']}() vs {paired}: {note}", counter, arch=arch)) # fmt: skip if not compared and moved: - rows.append(make_row("kernargs", surface.cpp_mirror, 0, f"declared:{surface.function}", - "changed", f"no static list in the C++ mirror shares two names with {surface.function} " - f"(closest shares {closest}): cannot compare", counter, arch=arch)) # fmt: skip + if not lists: + why = ( + "the C++ mirror holds no `std::vector` function (its list is " + "built another way)" + ) + elif cands: + why = ( + "no static list in the C++ mirror shares two names with any `b.param` builder " + f"of {', '.join(dict.fromkeys(c[0] for c in cands))} (closest shares {closest})" + ) + else: + why = f"{declared} declares no `b.param` list and no other builder was found" + rows.append(make_row("kernargs", surface.cpp_mirror, 0, f"declared:{declared}", + "changed", f"{why}: cannot compare", fallback, arch=arch)) # fmt: skip return rows @@ -2654,19 +2757,24 @@ def surfaces_rows(ctx: Ctx) -> list: if key in seen: continue seen.add(key) - py_changed = False - line = 0 + dumps, line = {}, 0 for side in ("before", "after"): tree = ctx.tree_of(side).ast(surface.module) - node = ( - function_nodes(tree).get(surface.function) if tree is not None else None + if surface.function is None: + # a prose locator names no symbol, so the module is the python side + dumps[side] = None if tree is None else surface.module + continue + nodes = [n for _, n in defs_named(tree, surface.function)] + if nodes and side == "after": + line = nodes[0].lineno + dumps[side] = ( + [ast.dump(without_docstrings(n)) for n in nodes] if nodes else None ) - if node is not None and side == "after": - line = node.lineno - dump = None if node is None else ast.dump(without_docstrings(node)) - if side == "before": - before_dump = dump - py_changed = before_dump != dump + dump = dumps["after"] + if surface.function is None: + py_changed = ctx.moved(surface.module) + else: + py_changed = dumps["before"] != dump cpp_changed = ctx.moved(surface.cpp_mirror) if surface.cpp_mirror else False test_changed = ctx.moved(surface.test) if surface.test else False manifest_changed = ctx.moved(surface.manifest) @@ -2674,7 +2782,9 @@ def surfaces_rows(ctx: Ctx) -> list: if surface.test: counter.append(cp(surface.test, 0, test_changed)) arch = surface.arch or arch_of(surface.module, surface.cpp_mirror) - name = f"{surface.name}:{surface.function}" + name = ( + f"{surface.name}:{surface.function or PurePosixPath(surface.module).name}" + ) broken = [ what for what, path in ( @@ -2871,8 +2981,9 @@ def merge_views(per_view: list) -> list: # -- emit fold --------------------------------------------------------------------------------------- EMIT_SIZES = {"i8": 1, "i16": 2, "i32": 4, "float": 4, "half": 2, "i64": 8, "double": 8} -#: The impact_emit.py output this script reads; 3 added `comgr_changed`. -EMIT_SCHEMA = 3 +#: The impact_emit.py output this script reads; 3 added `comgr_changed`, 4 `companions` (the +#: kernarg list of each companion launch of a KernelModule builder). +EMIT_SCHEMA = 4 def load_emit(path) -> dict: @@ -2956,6 +3067,22 @@ def params(value): and all(params(built.get(s)) for s in ("base", "head")) ): return f"family {n}: `params` is not base and head [name, type] lists" + companions = fam.get("companions") + if not ( + isinstance(companions, dict) + and all( + companions.get(s) is None + or ( + isinstance(companions[s], dict) + and all( + isinstance(r, str) and p is not None and params(p) + for r, p in companions[s].items() + ) + ) + for s in ("base", "head") + ) + ): + return f"family {n}: `companions` is not base and head role -> [name, type] lists" if not isinstance(fam.get("kdp", ""), str) or not isinstance( fam.get("spec_origin", ""), str ): @@ -3019,13 +3146,20 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: for side in ("before", "after"): covered |= ctx.closure(side).reach(source) if fam.get("params_changed") and params: + # one kernarg list per launch: the builder's primary, then each companion by role + launches = [(builder, params)] + [ + (f"{builder}[{role}]", p) + for role, p in sorted((fam["companions"]["head"] or {}).items()) + ] entries = [] for s in surfaces: if ( s.name == "kernargs" - and s.module == source - and s.function == builder and s.cpp_mirror + and ( + (s.module == source and s.function == builder) + or (s.builder_module == source and s.builder == builder) + ) ): entries += [ (s.cpp_mirror, e) @@ -3042,17 +3176,30 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: entries += [ (p, e) for e in cpp_kernel_lists(ctx.after.text(p) or "") ] - names = [p[0] for p in params] - entries = [ - (p, e) for p, e in entries if e["names"] and related(e["names"], names) - ] - bad = [(p, e, emit_agrees(params, e)) for p, e in entries] - bad = [b for b in bad if b[2]] + paired = [] + for p, e in entries: + near = [ + launch + for launch in launches + if e["names"] and related(e["names"], [n for n, _ in launch[1]]) + ] + if near: + # the launch the list names exactly, else the one sharing most names + label, lp = max( + near, + key=lambda launch: ( + [n for n, _ in launch[1]] == e["names"], + len(set(e["names"]) & {n for n, _ in launch[1]}), + ), + ) + paired.append((p, e, label, emit_agrees(lp, e))) + entries = paired + bad = [b for b in paired if b[3]] counter = [cp(source, 0, True)] if bad: - p, e, why = bad[0] + p, e, label, why = bad[0] out.append(make_row("kernargs", p, e["line"], f"emit:{arch}:{builder}", "mismatch", - f"emitted params of {builder} ({arch}) disagree with static list {e['func']}(): {why}", + f"emitted params of {label} ({arch}) disagree with static list {e['func']}(): {why}", counter, arch=arch)) # fmt: skip else: note = ( diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index fcbe6e112feb..8ac4f0ae69ef 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -10,9 +10,11 @@ variant there, lowers it to LLVM IR with the Python backend (no GPU, comgr or network), and records the kernarg list parsed from the IR `define` line, the SHA-256 of the IR text, and the COMGR inputs the packer adds to that text (ISA name, codegen options, -fresh-process compile): a code object can change while its IR does not. A packer that -pins another lowering backend fails every variant. This EXECUTES rocKE code from the -reviewed revisions, like building the head does. +fresh-process compile): a code object can change while its IR does not. A builder that +returns a `KernelModule` is lowered kernel by kernel, as the packer does: `params` is the +primary's list, `companions` each companion launch's list by role, and the digest covers +every kernel. A packer that pins another lowering backend fails every variant. This +EXECUTES rocKE code from the reviewed revisions, like building the head does. Variants come from every `*.kdp.json` under the shipped descriptor root whose `kernelDescriptors[].kernel_source` names a rocKE `source` + `builder`. A KDP held by @@ -447,6 +449,13 @@ def __init__(self): self.lower = need["_lower_llvm_via_backend"] self.rcompile = rcompile self.compile_arch = getattr(rc, "rocke_compile_arch", None) + # A builder may return a KernelModule (a primary plus role-keyed companions) that the + # packer links into one code object; a revision without the type has no such builder. + try: + from rocke.helpers import KernelModule + except ImportError: + KernelModule = None + self.kernel_module = KernelModule self.require_arch_sig = getattr(rc, "_require_spec_arch_signature", None) self.check_predicate = getattr(rc, "_check_support_predicate", None) # The packer's lowering backend; a tree without the pin is taken as Python. @@ -502,12 +511,29 @@ def _build_one(h, v, arch): if callable(h.check_predicate): h.check_predicate(module, v["builder"], obj, arch) kd = fn(obj, arch=arch) - text = h.lower(kd, arch=arch, backend="python", spec=None) - return { - "params": parse_kernargs(text), - "ir": hashlib.sha256(text.encode("utf-8")).hexdigest(), + # A KernelModule is lowered kernel by kernel, as the packer's `compile_module` does; each + # companion is its own launch with its own kernarg list. + is_module = h.kernel_module is not None and isinstance(kd, h.kernel_module) + launches = [("", kd.primary), *kd.companions.items()] if is_module else [("", kd)] + texts = [h.lower(k, arch=arch, backend="python", spec=None) for _, k in launches] + digest = hashlib.sha256() + for (role, _), text in zip(launches, texts): + digest.update(f"{role}\0{len(text)}\0".encode("utf-8") + text.encode("utf-8")) + built = { + "params": parse_kernargs(texts[0]), + "ir": ( + digest.hexdigest() + if is_module + else hashlib.sha256(texts[0].encode()).hexdigest() + ), "comgr": _comgr_inputs(h.rcompile, kd, arch), } + if is_module: + built["companions"] = { + role: parse_kernargs(text) + for (role, _), text in zip(launches[1:], texts[1:]) + } + return built def child_main(root, out): @@ -685,6 +711,11 @@ def run_side(repo, rev, tree, flavor, timeout, label): shutil.rmtree(tmp, ignore_errors=True) +def _launch_params(built): + """The kernarg lists of one built variant: the primary's, then each companion's by role.""" + return built["params"], built.get("companions") + + def compare(base_side, head_side): def by_key(side): return {(f["arch"], f["source"], f["builder"]): f for f in side["families"]} @@ -699,7 +730,9 @@ def by_key(side): built_h = fh["built"] if fh else {} common = [n for n in names_h if n in built_h and n in built_b] params_changed = [ - n for n in common if built_b[n]["params"] != built_h[n]["params"] + n + for n in common + if _launch_params(built_b[n]) != _launch_params(built_h[n]) ] ir_changed = [n for n in common if built_b[n]["ir"] != built_h[n]["ir"]] # A side that cannot tell its COMGR inputs (None) differs from one that can. @@ -733,6 +766,15 @@ def by_key(side): "base": built_b[shown]["params"] if shown in built_b else None, "head": built_h[shown]["params"] if shown in built_h else None, }, + # role -> kernarg list of each companion launch of a KernelModule builder + "companions": { + "base": ( + built_b[shown].get("companions") if shown in built_b else None + ), + "head": ( + built_h[shown].get("companions") if shown in built_h else None + ), + }, "failures": { "base": dict(fb["failures"]) if fb else {}, "head": dict(fh["failures"]) if fh else {}, @@ -763,7 +805,7 @@ def cmd_run(a): ] seconds = round(time.time() - t0, 1) doc = { - "schema": 3, + "schema": 4, "tool": "impact_emit", "flavor": flavor, "base": a.base, diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index bb4134775000..b008f0154147 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -508,7 +508,8 @@ def family(**fields) -> dict: fam = dict(arch="gfx950", source="kernels/gfx950/attn.py", builder="build_attn", kdp=KDP, spec_origin="kdp-in-git", variants={"base": 1, "head": 1}, only_in_base=[], only_in_head=[], params_changed=[], ir_changed=[], comgr_changed=[], - params={"base": None, "head": None}, failures={"base": {}, "head": {}}) # fmt: skip + params={"base": None, "head": None}, companions={"base": None, "head": None}, + failures={"base": {}, "head": {}}) # fmt: skip fam.update(fields) return fam @@ -516,7 +517,7 @@ def family(**fields) -> dict: def write_emit(path, repo, base, head, families=None, **fields): """An impact_emit document made for base..head, holding `families` (default: one family built from `fields`).""" - doc = {"schema": 3, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, + doc = {"schema": 4, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, "base_tree": tree_of(repo, base), "head_tree": tree_of(repo, head), "families": [family(**fields)] if families is None else families, "not_inspected": [], "seconds": 1.0} # fmt: skip @@ -658,18 +659,24 @@ def test_emit_made_for_another_base_or_head_is_refused(mod, capsys, tmp_path): @pytest.mark.parametrize( "spoil", [ - lambda d: d.update(schema=2), + lambda d: d.update(schema=3), lambda d: d.update(not_inspected={}), lambda d: d["families"][0].update(failures=[]), lambda d: d["families"][0].pop("variants"), lambda d: d["families"][0].update(params={"base": None, "head": "q_ptr"}), + lambda d: d["families"][0].pop("companions"), + lambda d: d["families"][0].update( + companions={"base": None, "head": {"reduce": None}} + ), ], ids=[ - "schema-2-without-comgr-inputs", + "schema-3-without-companions", "not-inspected-dict", "failures-list", "no-variants", "params-str", + "no-companions", + "companion-without-params", ], ) def test_malformed_emit_exits_one_instead_of_folding_or_crashing( @@ -811,6 +818,175 @@ def test_declared_mirror_with_no_comparable_list_stays_visible(mod, capsys, tmp_ assert [r["status"] for r in rows] == ["changed"] +def manifest(*surfaces, source=None) -> str: + """An engine manifest with `(name, python_source, cpp_mirror)` surfaces and, when given, the + `(source, builder)` the packer compiles.""" + lines = ["arch: gfx950", f"provider_root: {HKP.rstrip('/')}"] + if source: + lines += [f"source: {source[0]}", f"builder: {source[1]}"] + lines.append("launch_surface:") + for name, locator, mirror in surfaces: + lines += [f" - name: {name}", f" python_source: {locator}", f" cpp_mirror: {mirror}", + " kmd_fields: []", " guard: none", " test: none"] # fmt: skip + return "\n".join(lines) + "\n" + + +def ptr_sig(name, names): + """A static `KernelArgument` list function holding `names`, all pointers.""" + items = ",\n ".join(f'KernelArgument{{"global_buffer", PTR, {8 * i}, "{n}"}}' for i, n in enumerate(names)) # fmt: skip + return f"std::vector {name}()\n{{\n return {{{items}}};\n}}\n" + + +GEOM = HKP + "src/engines/kernel_ingestor_engine/packs/Gfx950Geometry.hpp" +WITH_METHOD = ( + BUILD + + "\n\nclass Tuning:\n def launch_block(self):\n return (64, 1, 1)\n" +) + + +@pytest.mark.parametrize( + "after, status", + [(WITH_METHOD.replace("(64, 1, 1)", "(128, 1, 1)"), "changed"), + (WITH_METHOD.replace("def launch_block", "def launch_blk"), "mismatch")], + ids=["method-edited", "method-renamed"], +) # fmt: skip +def test_launch_surface_naming_a_method_resolves_like_launch_surface_check( + mod, capsys, tmp_path, after, status +): + # catches: a python_source symbol looked up only at module level, so every method-defined + # surface reads "missing"; or a renamed method no longer reported missing + block = manifest(("block", "kernels/gfx950/attn.py:launch_block", GEOM)) + repo, base = make_repo( + tmp_path, {BUILDER: WITH_METHOD, GEOM: "int block();\n", MANIFEST: block} + ) + head = commit(repo, {BUILDER: after}) + rows = rows_of(report(mod, capsys, repo, base, head), "surfaces") + assert [(r["name"], r["status"]) for r in rows] == [("block:launch_block", status)] + + +SIG_FACTORY = HKP + "rocke/library/kernels/common/sig.py" +FACTORY = 'def attn_signature(dtype):\n return SignatureBuilder().ptr("q_ptr", dtype).build()\n' +FACTORY_MANIFEST = manifest( + ("kernargs", "kernels/common/sig.py:attn_signature", PACK), + source=("kernels/gfx950/attn.py", "build_attn"), +) + + +@pytest.mark.parametrize( + "edit, status, says", + [({PACK: PACK_STATIC + "int unrelated = 1;\n"}, "changed", "the manifest declares attn_signature"), + ({PACK: PACK_STATIC.replace('"q_ptr"', "@@").replace('"k_ptr"', '"q_ptr"').replace("@@", '"k_ptr"')}, + "mismatch", "disagrees with builder build_attn"), + ({SIG_FACTORY: FACTORY.replace("def attn_signature", "def attn_sig")}, + "mismatch", "function attn_signature")], + ids=["mirror-edited", "mirror-reordered", "factory-renamed"], +) # fmt: skip +def test_kernargs_pair_declaring_a_signature_factory_compares_the_manifest_builder( + mod, capsys, tmp_path, edit, status, says +): + # catches: a declared function with no `b.param` list reported as a missing builder, so a + # SignatureBuilder factory reads BREAKS; or the fallback hiding a real disagreement + repo, base = make_repo(tmp_path, {BUILDER: BUILD, SIG_FACTORY: FACTORY, PACK: PACK_STATIC, + MANIFEST: FACTORY_MANIFEST}) # fmt: skip + head = commit(repo, edit) + rows = [r for r in rows_of(report(mod, capsys, repo, base, head), "kernargs") if r["path"] == PACK] # fmt: skip + assert [r["status"] for r in rows] == [status] + assert says in rows[0]["reason"] + + +TWO_LAUNCHES = """\ +def build_seg(b, spec): + b.param("part_ptr", PTR) + b.param("q_ptr", PTR) + b.param("lens_ptr", PTR) + b.param("scale", F32) + return b + + +def build_reduce(b, spec): + b.param("out_ptr", PTR) + b.param("part_ptr", PTR) + b.param("lens_ptr", PTR) + return b +""" +SEG_SIG = ptr_sig("segSignature", ["part_ptr", "q_ptr", "lens_ptr"]).replace( + '"lens_ptr"}', + '"lens_ptr"},\n KernelArgument{"by_value", F32, 24, "scale"}', +) + + +@pytest.mark.parametrize( + "reduce_names, status", + [(["out_ptr", "part_ptr", "lens_ptr"], "changed"), (["out_ptr", "lens_ptr"], "mismatch")], + ids=["reduce-list-agrees", "reduce-list-drops-a-param"], +) # fmt: skip +def test_multi_launch_pack_compares_each_list_with_its_own_builder( + mod, capsys, tmp_path, reduce_names, status +): + # catches: every static list of a multi-launch pack compared with the one declared builder, + # so the reduce list reads as disagreeing with the segment builder; or a stale reduce list + # no longer caught + seg = manifest(("kernargs", "kernels/gfx950/attn.py:build_seg", PACK)) + pack = SEG_SIG + "\n" + ptr_sig("reduceSignature", reduce_names) + repo, base = make_repo(tmp_path, {BUILDER: TWO_LAUNCHES, PACK: pack, MANIFEST: seg}) + head = commit( + repo, {BUILDER: TWO_LAUNCHES.replace("return b", "x = 1\n return b")} + ) + rows = rows_of(report(mod, capsys, repo, base, head), "kernargs") + by_func = {r["name"]: r for r in rows if r["path"] == PACK} + assert by_func["declared:segSignature"]["status"] == "changed" + reduce = by_func["declared:reduceSignature"] + assert reduce["status"] == status and "build_reduce" in reduce["reason"] + + +@pytest.mark.parametrize( + "added, status", + [(' if spec.lse:\n b.param("lse_ptr", PTR)\n', "changed"), + (' b.param("lse_ptr", PTR)\n', "mismatch")], + ids=["conditional-param-added", "unconditional-param-added"], +) # fmt: skip +def test_copy_without_a_newly_added_conditional_param_is_not_a_mismatch( + mod, capsys, tmp_path, added, status +): + # catches: a copy of the list without an optional param, which variants that do not enable + # it still pass, read as a break; or a copy missing a required new param read as fine + repo, base = abi_repo(tmp_path) + head = commit( + repo, {BUILDER: BUILD.replace(" return b", added + " return b")} + ) + (row,) = rows_of(report(mod, capsys, repo, base, head), "kernargs") + assert row["status"] == status and row["counterparts"][0]["path"] == PIN + + +@pytest.mark.parametrize( + "reduce_params, status", + [([["out_ptr", "ptr"], ["part_ptr", "ptr"], ["lens_ptr", "ptr"]], None), + ([["out_ptr", "ptr"], ["part_ptr", "ptr"], ["lens_ptr", "ptr"], ["lse_ptr", "ptr"]], "mismatch")], + ids=["companion-agrees", "companion-gains-a-param"], +) # fmt: skip +def test_emit_fold_compares_each_companion_launch_with_its_static_list( + mod, capsys, tmp_path, reduce_params, status +): + # catches: only a KernelModule's primary launch compared with the pack, so a companion's + # changed kernargs read as consistent + pack = ( + PACK_STATIC + + "\n" + + ptr_sig("reduceSignature", ["out_ptr", "part_ptr", "lens_ptr"]) + ) + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, PACK: pack}) + head = commit(repo, {BUILDER: BODY_EDIT}) + primary = [["q_ptr", "ptr"], ["k_ptr", "ptr"], ["scale", "float"]] + emit = write_emit(tmp_path / "m.json", repo, base, head, params_changed=["v"], + params={"base": None, "head": primary}, + companions={"base": None, "head": {"reduce": reduce_params}}) # fmt: skip + data = report(mod, capsys, repo, base, head, "--emit", emit) + bad = rows_of(data, "kernargs", "mismatch") + assert [r["status"] for r in bad] == ([status] if status else []) + if status: + assert "build_attn[reduce]" in bad[0]["reason"] and bad[0]["path"] == PACK + + @pytest.mark.parametrize( "before, after", [ diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index 000e982bab2c..c4237139a007 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -81,6 +81,40 @@ def builder_src(params=(), body="v1"): ) +# rocke.helpers.KernelModule in miniature: a primary kernel plus role-keyed companions. +KERNEL_MODULE = """ +class KernelModule: + def __init__(self, primary, companions): + self.primary, self.companions = primary, dict(companions) + @property + def kernels(self): + return (self.primary, *self.companions.values()) +""" + + +def module_builder_src(reduce_params=(), reduce_body="r1"): + """A builder returning a two-launch KernelModule: the primary, then a `reduce` companion.""" + ps = "".join(f', ("{n}", "{t}")' for n, t in reduce_params) + return ( + "import dataclasses\n" + "from rocke.helpers import KernelModule\n" + "@dataclasses.dataclass\n" + "class Spec:\n n: int = 1\n boom: bool = False\n\n" + "def build_fake(spec, arch):\n" + ' p = [("q", "ptr addrspace(1)"), ("n", "i32")]\n' + f' r = [("out", "ptr addrspace(1)"){ps}]\n' + ' return KernelModule({"params": p, "body": "v1"},\n' + f' {{"reduce": {{"params": r, "body": "{reduce_body}"}}}})\n' + ) + + +def module_files(**kw): + files = tree_files() + files[f"{PROVIDER}/rocke/platform/python/rocke/helpers/__init__.py"] = KERNEL_MODULE + files[BUILDER] = module_builder_src(**kw) + return files + + KDP_VARIANTS = ({"name": "v_a", "spec": {"n": 1}}, {"name": "v_b", "spec": {"n": 2}}) @@ -209,6 +243,9 @@ def scenarios(): s["cpp_pin"][ f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py" ] = (HELPERS + '_BACKEND = "cpp"\n') + s["module"] = module_files() + s["module_wide"] = module_files(reduce_params=[("lse", "ptr addrspace(1)")]) + s["module_body"] = module_files(reduce_body="r2") return s @@ -322,7 +359,7 @@ def test_extra_param_lists_every_variant_and_shows_head_list(emit, repo, tmp_pat def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): rc, doc = run(emit, repo, "base", "body", tmp_path) assert rc == 0 - assert doc["schema"] == 3 + assert doc["schema"] == 4 # the recorded ids are the real tree ids, and differ when the sides differ assert doc["base_tree"] == git_out(repo, "rev-parse", "base^{tree}") assert doc["head_tree"] == git_out(repo, "rev-parse", "body^{tree}") @@ -332,6 +369,28 @@ def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): assert f["ir_changed"] == ["v_a", "v_b"] +@pytest.mark.parametrize( + "head, params_changed, ir_changed", + [("module_wide", ["v_a", "v_b"], ["v_a", "v_b"]), ("module_body", [], ["v_a", "v_b"])], + ids=["companion-gains-a-param", "companion-body-only"], +) # fmt: skip +def test_kernel_module_builder_records_and_compares_every_launch( + emit, repo, tmp_path, head, params_changed, ir_changed +): + # catches: a KernelModule passed whole to the single-kernel lowering (every variant fails), + # or only its primary lowered, so a companion launch's kernargs or body change reads equal + _, doc = run(emit, repo, "module", head, tmp_path) + f = only_family(doc) + assert f["failures"] == {"base": {}, "head": {}} + assert f["params"]["head"] == [["q", "ptr addrspace(1)"], ["n", "i32"]] + reduce = [["out", "ptr addrspace(1)"]] + ( + [["lse", "ptr addrspace(1)"]] if head == "module_wide" else [] + ) + assert f["companions"]["head"] == {"reduce": reduce} + assert f["params_changed"] == params_changed + assert f["ir_changed"] == ir_changed + + def test_removed_variant_is_only_in_base_and_unchanged_one_is_not_flagged( emit, repo, tmp_path ): From 7edb059888fde685e0a6e122c97156965e44c3ee Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Wed, 28 Oct 2026 11:58:04 -0600 Subject: [PATCH 06/13] fix(hipdnn): resolve launch surface symbols as launch_surface.py does impact.py resolved a launch_surface python_source symbol through the def and class bodies of the module only, so a function defined under a module-level if, try or with passed launch_surface.py --check but read as missing, or went unseen when it changed. It now matches every function of that name anywhere in the module, as the generator's own check does, and compares all of them. The reference now states when a C++ argument list that matches no builder is compared at all (it must share at least two names, and half of its own, with one), and quotes the rows a list built by helper calls produces, including that a dynamic list gives no pair row when only the builder changes. New test cases cover a second function of the same name changing, an edit to the manifest's source builder alone (a body edit reads changed, an argument type change reads mismatch), and a pack that only the manifest's source builder can pair with an emitted companion launch, since the pack-name fallback cannot find it. ALMIOPEN-2834 --- .../hipdnn-ingestor-impact/reference.md | 21 +++++--- .../hipdnn-ingestor-impact/scripts/impact.py | 16 +++--- .../tools/ai/tests/test_ingestor_impact.py | 51 +++++++++++++------ 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md index e6bfb0c9d219..b3c20ebee3d4 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -28,14 +28,18 @@ import is reported. How the copies are paired: - A `launch_surface` entry's `python_source` symbol resolves as `launch_surface.py --check` - resolves it: any function of that name in the module, a method or nested function included. It - is reported missing only when no function of that name exists. + resolves it: any function of that name anywhere in the module (`ast.walk`), a method, a nested + function or a def under `if`, `try` or `with` included. Every function of that name is compared, + and the symbol is reported missing only when none exists. - A declared `kernargs` pair compares each static C++ list in its `cpp_mirror` with the builder whose `b.param` list it matches, searched in the surface's module and the manifest's own `source` module, the declared function first. A multi-launch pack holds one list per launch (a segment and a reduce kernel), and a declared function may be a `SignatureBuilder` factory with no `b.param` list; the row then names the builder it was paired with. A list that matches no - builder is compared with the builder it shares most names with, and is a `mismatch`. + builder but is related to one (it shares at least two names, and at least half of its own) is + compared with the related builder it shares most names with, and is a `mismatch`. An unrelated + list is skipped; when no list of the mirror was compared, one `changed` row says `cannot + compare`. - A copy that holds the old list is `changed`, not `mismatch`, when it still matches the new list with the new conditional parameters left out: variants that do not enable them pass that list. The variants that do enable them are what the emitter or a device run settles. @@ -142,6 +146,11 @@ a flags-ON build when one exists), the architecture, and the flags. - The emitter compares emitted arguments with the static C++ lists only for variants whose arguments changed between base and head. The variants of a new engine have no base side, so their lists are checked statically only. -- Only `std::vector` functions are read as C++ argument lists. A list built by - helper calls instead (`gfx942Pointer("query_ptr", ...)`) is not parsed: its declared pair reads - `changed` with `cannot compare`, so compare it with the builder by hand. +- Only `std::vector` functions holding `KernelArgument{...}` literals are read as + static C++ argument lists. A list built by helper calls instead (`gfx942Pointer("query_ptr", + ...)`) is not parsed. When such a function exists, its row reads "`() is a dynamic kernarg + signature: it cannot be compared with statically`", and only when the mirror file + changed; when there is none, the pair's row reads "`the C++ mirror holds no + std::vector function (its list is built another way): cannot compare`". With + only the builder edited, a dynamic list gives no pair row. Compare such a list with the builder + by hand. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 5e76e9a5c7a4..ac07ca5a3c44 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -945,13 +945,17 @@ def visit(node, qual): def defs_named(tree, name) -> list: - """[(qualname, FunctionDef)] of every def called `name`, methods and nested ones included, - in source order: how `launch_surface.py --check` resolves a `python_source` symbol. - """ + """Every def called `name` anywhere in `tree`, in source order: methods, nested functions and + defs under `if`, `try` or `with` included. This is how `launch_surface.py --check` resolves a + `python_source` symbol (`ast.walk`, matched by name).""" if tree is None or not name: return [] - found = [(q, n) for q, n in function_nodes(tree).items() if n.name == name] - return sorted(found, key=lambda item: item[1].lineno) + found = [ + n + for n in ast.walk(tree) + if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and n.name == name + ] + return sorted(found, key=lambda n: (n.lineno, n.col_offset)) def string_lists(path: str, text: str, tree=None) -> list: @@ -2764,7 +2768,7 @@ def surfaces_rows(ctx: Ctx) -> list: # a prose locator names no symbol, so the module is the python side dumps[side] = None if tree is None else surface.module continue - nodes = [n for _, n in defs_named(tree, surface.function)] + nodes = defs_named(tree, surface.function) if nodes and side == "after": line = nodes[0].lineno dumps[side] = ( diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index b008f0154147..3a81fc1c5d75 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -842,22 +842,34 @@ def ptr_sig(name, names): BUILD + "\n\nclass Tuning:\n def launch_block(self):\n return (64, 1, 1)\n" ) +TWO_METHODS = ( + WITH_METHOD + + "\n\nclass Other:\n def launch_block(self):\n return (32, 1, 1)\n" +) +UNDER_TRY = ( + BUILD + + "\n\ntry:\n def launch_block():\n return (64, 1, 1)\nexcept ImportError:\n pass\n" +) @pytest.mark.parametrize( - "after, status", - [(WITH_METHOD.replace("(64, 1, 1)", "(128, 1, 1)"), "changed"), - (WITH_METHOD.replace("def launch_block", "def launch_blk"), "mismatch")], - ids=["method-edited", "method-renamed"], + "before, after, status", + [(WITH_METHOD, WITH_METHOD.replace("(64, 1, 1)", "(128, 1, 1)"), "changed"), + (WITH_METHOD, WITH_METHOD.replace("def launch_block", "def launch_blk"), "mismatch"), + (TWO_METHODS, TWO_METHODS.replace("(32, 1, 1)", "(16, 1, 1)"), "changed"), + (UNDER_TRY, UNDER_TRY.replace("(64, 1, 1)", "(128, 1, 1)"), "changed")], + ids=["method-edited", "method-renamed", "second-same-named-method-edited", + "def-under-module-level-try-edited"], ) # fmt: skip def test_launch_surface_naming_a_method_resolves_like_launch_surface_check( - mod, capsys, tmp_path, after, status + mod, capsys, tmp_path, before, after, status ): - # catches: a python_source symbol looked up only at module level, so every method-defined - # surface reads "missing"; or a renamed method no longer reported missing + # catches: a python_source symbol looked up only at module level or only through def and + # class bodies, so a method or a def under `try:` reads "missing" or goes unseen; only the + # first of several same-named defs compared; or a renamed method no longer reported missing block = manifest(("block", "kernels/gfx950/attn.py:launch_block", GEOM)) repo, base = make_repo( - tmp_path, {BUILDER: WITH_METHOD, GEOM: "int block();\n", MANIFEST: block} + tmp_path, {BUILDER: before, GEOM: "int block();\n", MANIFEST: block} ) head = commit(repo, {BUILDER: after}) rows = rows_of(report(mod, capsys, repo, base, head), "surfaces") @@ -878,14 +890,18 @@ def test_launch_surface_naming_a_method_resolves_like_launch_surface_check( ({PACK: PACK_STATIC.replace('"q_ptr"', "@@").replace('"k_ptr"', '"q_ptr"').replace("@@", '"k_ptr"')}, "mismatch", "disagrees with builder build_attn"), ({SIG_FACTORY: FACTORY.replace("def attn_signature", "def attn_sig")}, - "mismatch", "function attn_signature")], - ids=["mirror-edited", "mirror-reordered", "factory-renamed"], + "mismatch", "function attn_signature"), + ({BUILDER: BODY_EDIT}, "changed", "the manifest declares attn_signature"), + ({BUILDER: BUILD.replace('"scale", F32', '"scale", F64')}, "mismatch", "types do not")], + ids=["mirror-edited", "mirror-reordered", "factory-renamed", "source-builder-edited", + "source-builder-type-changed"], ) # fmt: skip def test_kernargs_pair_declaring_a_signature_factory_compares_the_manifest_builder( mod, capsys, tmp_path, edit, status, says ): # catches: a declared function with no `b.param` list reported as a missing builder, so a - # SignatureBuilder factory reads BREAKS; or the fallback hiding a real disagreement + # SignatureBuilder factory reads BREAKS; the fallback hiding a real disagreement; or an edit + # to the manifest's `source` builder alone not checking the pair repo, base = make_repo(tmp_path, {BUILDER: BUILD, SIG_FACTORY: FACTORY, PACK: PACK_STATIC, MANIFEST: FACTORY_MANIFEST}) # fmt: skip head = commit(repo, edit) @@ -968,13 +984,18 @@ def test_emit_fold_compares_each_companion_launch_with_its_static_list( mod, capsys, tmp_path, reduce_params, status ): # catches: only a KernelModule's primary launch compared with the pack, so a companion's - # changed kernargs read as consistent - pack = ( + # changed kernargs read as consistent; or a pack found only by the arch-named fallback, + # not through the manifest's `source` builder that the family names + # no arch in the pack's name, so only the manifest's builder can pair it + pack = HKP + "src/engines/kernel_ingestor_engine/packs/AttnNative.cpp" + lists = ( PACK_STATIC + "\n" + ptr_sig("reduceSignature", ["out_ptr", "part_ptr", "lens_ptr"]) ) - repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, PACK: pack}) + declared = FACTORY_MANIFEST.replace(PACK, pack) + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, SIG_FACTORY: FACTORY, + pack: lists, MANIFEST: declared}) # fmt: skip head = commit(repo, {BUILDER: BODY_EDIT}) primary = [["q_ptr", "ptr"], ["k_ptr", "ptr"], ["scale", "float"]] emit = write_emit(tmp_path / "m.json", repo, base, head, params_changed=["v"], @@ -984,7 +1005,7 @@ def test_emit_fold_compares_each_companion_launch_with_its_static_list( bad = rows_of(data, "kernargs", "mismatch") assert [r["status"] for r in bad] == ([status] if status else []) if status: - assert "build_attn[reduce]" in bad[0]["reason"] and bad[0]["path"] == PACK + assert "build_attn[reduce]" in bad[0]["reason"] and bad[0]["path"] == pack @pytest.mark.parametrize( From 232251c120b4ed60105e463d060f648b39943ef7 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Mon, 2 Nov 2026 15:41:17 -0600 Subject: [PATCH 07/13] feat(hipdnn): test ingestor support predicates and C++ restatements in impact The ingestor impact skill compared kernel argument lists and launch-surface declarations between rocKE and their C++ copies, but never what either side accepts. A support predicate could admit a spec its builder asserts on, a module's companion predicate could accept a head count its primary refuses, and a hand-restated C++ function could return a wrapped value for an input the Python refuses. While it builds each shipped variant, the emitter now records the specs the build passes to the module's other build functions. After the builds it puts each builder's support predicate, as the packer resolves it, to a table of one-field variations of the shipped specs. A spec the predicate accepts and the builder raises on is a builder finding. A value outside the sign range of every shipped spec that one predicate of a module accepts and another refuses is a sign finding. A finding the base revision's table also has is marked as pre-existing, and the emit schema becomes 5. impact.py lists the tables in their own section and prints a Pack-time line above the runtime verdict. The line counts the findings a change introduces and the older ones it ships for the first time, and stays apart from the runtime verdict: the tables never change a row or the rollup. The skill and the deep-review IMPACT lane gain a judge-written C++ differential table for header-only restatements, a C++ tables summary line beside the two verdicts, and a severity scale for every judged finding. ALMIOPEN-2834 --- .../lanes/runtime-impact.md | 16 +- .../ai/skills/hipdnn-ingestor-impact/SKILL.md | 40 +- .../hipdnn-ingestor-impact/reference.md | 134 +++++++ .../hipdnn-ingestor-impact/scripts/impact.py | 148 +++++++- .../scripts/impact_emit.py | 345 +++++++++++++++++- .../tools/ai/tests/test_ingestor_impact.py | 111 +++++- .../ai/tests/test_ingestor_impact_emit.py | 121 +++++- 7 files changed, 883 insertions(+), 32 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md index f50c3c5a7c84..29e7d4453f10 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -42,11 +42,17 @@ searched as copies and readers, so a stale pin or a stale import is yours. with its message, and never fold an `emit.json` from an earlier run. Record the flavor it used. 3. Judge every row whose status is not `inert` or `outside` with the reference's *Judging the rows*. Read both sides at `$HEAD` (and the merged tree when the row says `merged-only`) before - filing. -4. End **Checked, no finding** with the rollup line the script printed, corrected by your - judgment and saying why when it differs: + filing. Judge each predicate-table finding not marked `at_base` with the reference's *Predicate tables*. + For each `changed` `launch_surface` row whose C++ side is a header-only, HIP-free function, write + the throwaway C++ table of the reference's *C++ differential tables* in `$SCRATCH/IMPACT/` + when `Build host: yes`. Keep its driver and output under `lanes/IMPACT-evidence/`. +4. End **Checked, no finding** with the two lines the script printed last, each corrected by your + judgment and saying why when it differs: the `Pack-time:` line (`NEW `, `FIRST-SHIPPED `, + `CLEAN` or `NOT RUN`; see the reference's *Predicate tables*), then `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`, - keeping any `not judged here:` note the script added. + keeping any `not judged here:` note the script added. Follow them with your own + `C++ tables: of disagree (...) | written, all agree | none written ()` line, so a + qualifying row with no table is visible. ## From row to finding @@ -56,6 +62,8 @@ searched as copies and readers, so a stale pin or a stale import is yours. | A dispatch divergence | A `source-claim` at `IMPORTANT` or higher, quoting both sides | | A descriptor or generic-target value the loader rejects or ignores | A finding, citing the loader rule | | No break (`changed` but consistent), `inert`, `outside` | No finding. Count them under **Checked, no finding**; list each `inert` reason once | +| A predicate-table finding in state `new` or `first shipped` that holds | A `defect` at `IMPORTANT`: the packer accepts a spec its builder rejects, or a value the kernel cannot use. For `first shipped`, say the predicate is older and ships for the first time. **Proof**: the table's row (predicate, field, values, seed, the builder's error). A finding `at base` predates the change: `FUTURE WORK` at most | +| A C++ table disagreement that holds | A `source-claim`: `BLOCKING` when a valid row's values differ, or when no C++ refusal on the engine's call path refuses a row Python refuses. `IMPORTANT` when the engine path refuses it but the header's own function does not, and another caller reaches the function directly. **Proof**: the harness, the driver command and the disagreeing rows | For every `changed` row without a finding, put the device run the reference says to name under **Approval requests**, and add `AUTH: requires with the ingestor flags ON` under diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md index 7bfe307c1c6a..16204f36c164 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md @@ -1,6 +1,6 @@ --- name: hipdnn-ingestor-impact -description: "Check whether a rocm-libraries change (PR, branch, commit range or uncommitted work) breaks the hipDNN kernel ingestor at run time while the default build stays green: kernel argument lists (ABI) out of step with their C++ and test copies, also against the target branch's tip, dispatch and launch-surface edits, descriptor and generic-target changes, removed Python symbols still read, and which shipped rocKE variants' emitted code changed. Reads git objects; the optional emitter runs rocKE on the CPU. Ends in a Runtime impact: BREAKS | NEEDS-DEVICE | NONE | N/A line. For a full multi-agent review, use hipdnn-deep-review, whose IMPACT lane runs the same scripts." +description: "Check whether a rocm-libraries change (PR, branch, commit range or uncommitted work) breaks the hipDNN kernel ingestor at run time while the default build stays green: kernel argument lists (ABI) out of step with their C++ and test copies, also against the target branch's tip, dispatch and launch-surface edits, descriptor and generic-target changes, removed Python symbols still read, which shipped rocKE variants' emitted code changed, support predicates that accept specs their builders reject, and C++ restatements whose values or refusals differ from Python's. Reads git objects; the optional emitter runs rocKE on the CPU. Ends in a Runtime impact: BREAKS | NEEDS-DEVICE | NONE | N/A line. For a full multi-agent review, use hipdnn-deep-review, whose IMPACT lane runs the same scripts." argument-hint: "[ | branch: | .. | commit: | worktree] [emit]" allowed-tools: Bash, Read, Grep, Glob --- @@ -13,13 +13,15 @@ suggest it for any change under `dnn-providers/hip-kernel-provider/`, the SDK in [reference.md](reference.md) holds the contracts, row statuses, emitter rules and known gaps; read it before judging rows. -The answer is advisory. It never builds, runs tests, dispatches CI or posts anything. +The answer is advisory. It never builds the product, runs its tests, dispatches CI or posts +anything; the only thing it compiles is the throwaway C++ table of step 6. ## Prerequisites `git` 2.38 or later (`merge-tree --write-tree`), Python 3.10 or newer and PyYAML; the emitter also needs Jinja2. Run from a rocm-libraries clone or worktree. On Windows, `python3` is often the -Microsoft Store stub: use `python` or `py -3`. +Microsoft Store stub: use `python` or `py -3`. The optional C++ tables (step 6) need a host C++ +compiler; on Windows, the ROCm wheel's `clang++` with the MSVC `INCLUDE` and `LIB` set. ## Workflow @@ -49,11 +51,13 @@ Microsoft Store stub: use `python` or `py -3`. python3 /scripts/impact.py report --repo . --base --head --target-tip ``` - Exit 1 means it could not run: report its message, never a verdict. + Exit 1 means it could not run: report its message, never a verdict. The commands here write + `python3`; on Windows use `python` or `py -3` (see *Prerequisites*). 4. **Run the emitter** when any rocKE or `hkp_pack` Python file changed in code, including rows the report calls `inert`, or when the user asks for `emit`. It builds every shipped variant at both - sides on two CPU workers for a few minutes; on a shared host, run one at a time. Put its output + sides on two CPU workers for a few minutes, then puts each builder's support predicate to a + table ([reference.md](reference.md) *Predicate tables*); on a host that other people or agents use at the same time, run one emitter at a time. Put its output outside the repository and delete any earlier file first: ```bash @@ -67,14 +71,24 @@ Microsoft Store stub: use `python` or `py -3`. 5. **Judge every row** whose status is not `inert` or `outside`, with [reference.md](reference.md) *Judging the rows*. Read both sides at `` (`git show - :`), and at the merged tree when the row says `merged-only`. + :`), and at the merged tree when the row says `merged-only`. Judge each predicate + table finding not marked `at_base` with *Predicate tables*. -6. **Report** in the format below. +6. **Write a C++ table** for each `changed` `launch_surface` row whose C++ side is a header-only, + HIP-free function, when a host compiler is available: [reference.md](reference.md) *C++ + differential tables*. Work in a scratch directory outside the repository and delete it after. + A row whose C++ side is a matcher in `*Native.cpp` gets no table: read it by hand and say so in + *C++ table detail*. When a row qualifies but no table can be written (no compiler, the Python + side cannot be imported), say so on the report's `C++ tables:` line rather than leaving it out. + +7. **Report** in the format below. ## Report ```markdown Runtime impact: (judged: ) +Pack-time: (judged: ) +C++ tables: of tables disagree (: , ; ...) | written, all agree | none written () Analysed: .. target tip: emitter: | Row | Status | Judgment | Evidence (path:line at HEAD or merged tree) | @@ -82,10 +96,20 @@ Analysed: .. target tip: emitter: on with the ingestor, rocKE and SDPA flags ON -Not judged here: +Predicate tables: +| Builder | Check | Field | Values | State (new, first shipped, at base) | Judgment | + +C++ table detail: + +Not judged here: Known gaps that apply: ``` +`` on the `C++ tables:` line counts every table with at least one disagreeing row, whatever the +judgment; the judgment and severity follow in the parenthesis. Every judged finding (a row, a +predicate-table finding, a C++ table disagreement) carries a severity from +[reference.md](reference.md) *Severities*: `BLOCKING`, `IMPORTANT` or `SUGGESTION`. + Group `inert` and `outside` rows into one count each, with each `inert` reason listed once. Write `none` for an empty section; do not drop it. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md index b3c20ebee3d4..5434ab636c66 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -103,6 +103,130 @@ rows. that runs after lowering (`rocke/helpers/compile.py`, `rocke/runtime/`): the IR digest does not cover it. A static kernarg `mismatch` is never downgraded. +## Predicate tables + +The emitter also puts each builder's support predicate to the test, on both sides, after the +builds. It works on the family's shipped builder, and on every other `build_*` function of its +module that the builds called (a `KernelModule`'s kernel builders, such as a `reduce` companion's). +It takes the shipped specs (an inner builder's are the specs the builds passed it) and varies one +int or bool field at a time from up to four seeds. The values are the shipped ones, 0, -1, and the +negation, double, half and 1.5 times of each shipped positive value. It asks the predicate the +packer would resolve (`_resolve_support_predicate`). + +| Check | Finding | +|---|---| +| `builder` | The predicate accepts the spec and the builder raises on it: the packer would pass a spec its builder rejects. The detail names the exception and the `file:line` that raised it. | +| `sign` | Between two builders of one family, on a field both specs have: a value outside the sign range of every shipped value (<= 0 where all are >= 1, < 0 where all are >= 0) that this predicate accepts and the other refuses. | + +The `sign` check compares only predicates built into the same module. A lone predicate that +accepts 0 is not a finding: many fields mean "off" or "default" at 0 or -1. + +`at_base` marks two things: + +- **On a table**: the base ran the same builder's table. +- **On a finding**: the base's table had the same check on the same field, so the finding + predates the change. + +`report` gives each finding a state and lists the findings in their own section, in this order: + +| State | When | +|---|---| +| `new` | The base's table of the same builder lacked it; or the base did not table the builder and the change edits its predicate or that builder (a function the diff's hunks fall in, in the family's source module) | +| `first shipped` | The base did not table the builder and the change touches neither: an older predicate defect that the change ships for the first time | +| `at base` | The base's table had it: it predates the change | + +These are pack-time facts. `report` never turns them into rows, so they never change the rollup. +Instead it prints a `Pack-time:` line just above the `Runtime impact:` line: + +| Line | Meaning | +|---|---| +| `NEW ` | The `new` findings | +| `FIRST-SHIPPED ` | The `first shipped` findings | +| `CLEAN ( tables)` | Every table ran in full; any finding is `at base` | +| `NOT RUN ()` | No emit file, or no shipped rocKE builder to table | +| `NOT RUN for of tables (); no finding in the rest` | No finding, but a table was skipped or truncated, so the line cannot read clean | + +`NEW` and `FIRST-SHIPPED` can appear together, followed by `; not run for ...` when some table +did not run. + +A table that cannot run says why (`skipped`): the packer has no resolver at that revision, no +predicate takes the spec, or the specs are not dataclasses. A family's tables stop after +`IMPACT_EMIT_TABLE_SECONDS` (default 60) and say `truncated`. + +Judge each finding whose state is `new` or `first shipped`. Read the predicate and the +builder at `HEAD`. + +- A `builder` finding is a defect unless something refuses that spec before the packer. +- A `sign` finding is a defect when the builder or a launch uses the field as a count, size or + index. + +A finding that holds is `IMPORTANT` (*Severities*). A finding that is `at base` predates the +change and gets no severity of its own; mention it only when the change makes it reachable. + +## C++ differential tables (judge-written) + +The scripts compare the names, order and types of a C++ copy, never its values or what it refuses. +For a `changed` `launch_surface` row whose C++ side is header-only and includes no HIP header +(geometry, split rule, workspace, arguments), settle the values with a throwaway table. A +`kernargs` or `workspace` row qualifies like any other, whether its C++ list is built from +`KernelArgument` literals or helper calls. One table compares one C++ function with its Python +counterpart; a header that restates several functions gives several tables, and the +`C++ tables:` count is per function: + +1. Extract the side with `git archive ` into a scratch directory, as the emitter + does. Never use the worktree. +2. Write a small `main()` that includes the header, reads one input row per line from stdin and + prints the C++ result or `throw `. Compile it with a host compiler, adding `-I` for the + header's directory and `projects/hipdnn/plugin_sdk/include`. On Windows use the ROCm wheel's + `clang++` with the MSVC `INCLUDE` and `LIB` set. No hipDNN build is needed. +3. Write a Python driver that imports the `python_source` module (or the class the `guard:` text + names) from the extract. It builds the rows, runs both sides and prints each row where they + disagree. The rows are each value the shipped catalog uses for the fields the C++ side reads + (each value in at least one row, not the cross product), plus each Python refusal's boundary + just inside and just outside it: every `raise` and `return False, ...` in the function and in + the `__post_init__` of the classes it builds. When the Python function compiles or launches + kernels, stub that layer (no GPU, comgr or kernel cache) and say in *C++ table detail* what + was stubbed. Keep a table to a few thousand rows: sample each field's values, do not + enumerate their combinations. +4. Run it at `HEAD`, and on the merged tree when the row is `merged-only` or the target moved + rocKE. + +Judge every disagreement, naming the function on each side: + +| Disagreement | Severity | +|---|---| +| A valid row whose values differ | `BLOCKING` | +| Python refuses, C++ accepts, and no C++ refusal on the engine's call path refuses it first | `BLOCKING` | +| Python refuses, C++ accepts; a C++ refusal on the engine's call path refuses it first (name it), but the function itself does not enforce the precondition, and some call site outside that guarded path reaches the function (a test included, whatever inputs that call site passes) | `IMPORTANT` | +| The same, with no call site outside the guarded path | No finding; list it in *C++ table detail* | +| C++ refuses, Python accepts, on an input the matcher lets through | `IMPORTANT`: the engine declines what rocKE serves | +| C++ refuses, Python accepts, on an input the matcher refuses first | No finding; list it in *C++ table detail* | + +The `IMPORTANT` row applies on purpose to any function a unit test calls directly: a test is a +caller outside the guarded path, and the finding marks a precondition the function relies on but +does not check, as `gfx942StridedKvArguments` on #14259 did. + +Matchers in `*Native.cpp` need the graph data types, so they get no table: read them by hand and +say so in *C++ table detail*. Delete the scratch directory when done. + +Summarise the outcome on the `C++ tables:` line at the top of the report, beside the two verdict +lines. `` counts every table with at least one disagreeing row, whatever the judgment, so a +disagreement judged harmless still shows in the count. For each disagreeing table, the line gives +the function, what disagrees and the severity. When a row qualified but no table was written, the +line says `none written` and why. + +## Severities + +Every judged finding in the report carries one: + +| Severity | Use for | +|---|---| +| `BLOCKING` | A break on the engine's path: a holding `mismatch` row, a dispatch divergence, values that differ on a valid row, a refusal no C++ check on the engine path restates | +| `IMPORTANT` | Real risk off the engine's path or not yet reachable: a predicate-table finding that holds, a precondition the function does not enforce while another caller reaches it, the engine declining what rocKE serves | +| `SUGGESTION` | Anything else worth fixing in code the change touches | + +A `changed` row that is consistent is not a finding; it names the device run that settles it. + ## Judging the rows Read both sides at the head revision, and at the merged tree when the row says `merged-only`, before @@ -154,3 +278,13 @@ a flags-ON build when one exists), the architecture, and the flags. std::vector function (its list is built another way): cannot compare`". With only the builder edited, a dynamic list gives no pair row. Compare such a list with the builder by hand. +- A predicate table varies one field at a time, so a defect that needs two fields to move + together is out of reach, unless one seed already has the other value. It builds the accepted + specs but does not lower them, so a builder that only fails in lowering reads clean. It finds + inner builders only through the module's own `build_*` globals: a builder imported under + another name, or called through a dict, is not tabled. +- `new` versus `first shipped` reads only the family's source module. A predicate the module + imports from elsewhere, edited there by the change, reads `first shipped`. Check where the + predicate is defined before calling a `first shipped` finding older. +- No table, scripted or judge-written, sees a change made in step on both sides: Python and C++ + still agree. That is a test-quality question. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index ac07ca5a3c44..e9944869d441 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -31,18 +31,23 @@ rocKE links the engine and the packer pins the Python lowering backend; any other non-Python rocKE file is not understood. -Rollup (last line of `report`): +Rollup (last two lines of `report`): + Pack-time: NEW [, FIRST-SHIPPED ][; not run for ...] | FIRST-SHIPPED | CLEAN ( tables) + | NOT RUN () | NOT RUN for of tables (); no finding in the rest Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected[; not judged here: ...]) | N/A (no surface file[; not judged here: ...]) -`report --emit FILE.json` folds in impact_emit.py output (schema 4), which must have been made for +`report --emit FILE.json` folds in impact_emit.py output (schema 5), which must have been made for the same base and head trees: changed params of any launch (a KernelModule builder's companions included) vs the static C++ list that mirrors that launch is a mismatch; changed IR or COMGR inputs are `changed` for that arch; when every family is equal (nothing changed, nothing failed, nothing not inspected) the rows that were `changed` only because a shipped module changed become `inert`, but only on modules a family with built variants imports, and never on code that runs after lowering (COMPILE_STEP) or on a DVC pointer. Static kernarg mismatch rows are never -downgraded. A module that the packer reaches ships on every architecture. +downgraded. A module that the packer reaches ships on every architecture. The emit file's +predicate tables (a predicate that accepts a spec its builder raises on, or a value outside the +sign range of every shipped spec) are pack-time facts: `report` lists them in their own section, +new at head before pre-existing, and they never change a row or the rollup. """ import argparse @@ -2986,8 +2991,10 @@ def merge_views(per_view: list) -> list: EMIT_SIZES = {"i8": 1, "i16": 2, "i32": 4, "float": 4, "half": 2, "i64": 8, "double": 8} #: The impact_emit.py output this script reads; 3 added `comgr_changed`, 4 `companions` (the -#: kernarg list of each companion launch of a KernelModule builder). -EMIT_SCHEMA = 4 +#: kernarg list of each companion launch of a KernelModule builder), 5 `predicate_tables`. +EMIT_SCHEMA = 5 +#: The text fields of one predicate table. +TABLE_TEXT = ("arch", "source", "builder", "inner", "predicate", "skipped") def load_emit(path) -> dict: @@ -3036,6 +3043,29 @@ def params(value): skipped = data.get("not_inspected") if not isinstance(skipped, list) or not all(isinstance(i, dict) for i in skipped): return "`not_inspected` is not a list of objects" + tables = data.get("predicate_tables") + if not isinstance(tables, list): + return "`predicate_tables` is not a list" + for n, t in enumerate(tables): + if not ( + isinstance(t, dict) + and all(isinstance(t.get(k), str) for k in TABLE_TEXT) + and count(t.get("rows")) + and isinstance(t.get("truncated"), bool) + and isinstance(t.get("at_base"), bool) + and isinstance(t.get("findings"), list) + ): + return f"predicate table {n} lacks {', '.join(TABLE_TEXT)}, rows, truncated, at_base or findings" + for f in t["findings"]: + if not ( + isinstance(f, dict) + and f.get("check") in ("builder", "sign") + and all(isinstance(f.get(k), str) for k in ("field", "seed", "detail")) + and isinstance(f.get("values"), list) + and all(type(v) in (int, bool) for v in f["values"]) + and isinstance(f.get("at_base"), bool) + ): + return f"predicate table {n}: a finding lacks check, field, values, seed, detail or at_base" for n, fam in enumerate(data["families"]): if not isinstance(fam, dict): return f"family {n} is not an object" @@ -3305,6 +3335,102 @@ def esc(text): return "\n".join(lines) +def table_edited(ctx, table) -> bool: + """True when the change edits the table's predicate or the builder it tabled, in the + family's source module (the functions the diff's hunks fall in).""" + ch = ctx.changes.get(emit_source(ctx, table["source"])) + names = [n for n in (table["predicate"], table["inner"] or table["builder"]) if n] + return bool(ch) and any( + f == n or f.startswith(n + ".") for f in ch.funcs for n in names + ) + + +def finding_state(table, finding, edited) -> str: + """`new` (the base's table lacked it, or the base did not table the builder and the + change edits its predicate or builder), `first shipped` (the base did not table the + builder and the change does not touch either: an older defect that now ships), or + `at base` (the base's table had it).""" + if finding["at_base"]: + return "at base" + if table["at_base"] or (edited is not None and edited(table)): + return "new" + return "first shipped" + + +def pack_time(tables, edited=None) -> str: + """The pack-time line: what the predicate tables found that the change ships, kept apart + from the runtime rollup. NEW and FIRST-SHIPPED count findings by `finding_state`; EDITED + (a table -> bool, see `table_edited`) tells a predicate the change wrote from an older one. + CLEAN needs every table to have run in full: a skipped or truncated table is not run for + its builder, so it never reads clean.""" + if tables is None: + return "Pack-time: NOT RUN (emitter not run)" + if not tables: + return "Pack-time: NOT RUN (no shipped rocKE builder to table)" + states = [finding_state(t, f, edited) for t in tables for f in t["findings"]] + new, first = states.count("new"), states.count("first shipped") + missed = [t for t in tables if t["skipped"] or t["truncated"]] + why = sorted({t["skipped"] or "truncated" for t in missed}) + gap = f"not run for {len(missed)} of {len(tables)} tables ({'; '.join(why)})" + if new or first: + parts = ([f"NEW {new}"] if new else []) + ( + [f"FIRST-SHIPPED {first}"] if first else [] + ) + return "Pack-time: " + ", ".join(parts) + (f"; {gap}" if missed else "") + if missed: + return f"Pack-time: NOT RUN {gap[len('not run '):]}; no finding in the rest" + return f"Pack-time: CLEAN ({len(tables)} tables)" + + +def render_tables(tables: list, edited=None) -> str: + """The emit file's predicate tables: findings the change introduces, then older defects + that ship for the first time, then pre-existing ones (`finding_state`), then the tables + that could not run. Pack-time facts; no row or rollup reads them.""" + + def esc(text): + return str(text).replace("|", "\\|").replace("\n", " ") + + def state(t, f): + return finding_state(t, f, edited) + + found = [(t, f) for t in tables for f in t["findings"]] + order = {"new": 0, "first shipped": 1, "at base": 2} + found.sort(key=lambda x: order[state(*x)]) + counts = {s: sum(1 for x in found if state(*x) == s) for s in order} + skipped = [t for t in tables if t["skipped"]] + truncated = [t for t in tables if t["truncated"]] + lines = [ + f"Predicate tables (pack time, not in the rollup): {len(tables)} tables; findings: " + f"{counts['new']} new, {counts['first shipped']} first shipped, " + f"{counts['at base']} at base; {len(skipped)} skipped, {len(truncated)} truncated" + ] + if found: + lines += [ + "", + "| Builder | Arch | Predicate | Check | Field | Values | State | Detail |", + "|---|---|---|---|---|---|---|---|", + ] + for t, f in found: + who = f"{t['source']}:{t['builder']}" + ( + f" [{t['inner']}]" if t["inner"] else "" + ) + values = ", ".join(repr(v) for v in f["values"]) + lines.append( + f"| {esc(who)} | {t['arch']} | {esc(t['predicate'])} | {f['check']} | {esc(f['field'])} " + f"| {esc(values)} | {state(t, f)} | {esc(f['detail'])}; seed {esc(f['seed'])} |" + ) + reasons = {} + for t in skipped: + reasons.setdefault(t["skipped"], []).append( + t["builder"] + (f"[{t['inner']}]" if t["inner"] else "") + ) + for why, who in sorted(reasons.items()): + lines.append( + f"skipped ({len(who)}): {why}: {', '.join(sorted(set(who)))[:300]}" + ) + return "\n".join(lines) + + def render_files(ctx_changes: list, rows: list) -> str: worst = {} for r in rows: @@ -3410,7 +3536,8 @@ def run(args): per_view.append(mrows) if emit is not None: per_view[0] = fold_emit(ctx, per_view[0], emit) - return ctx, views, merge_views(per_view) + tables = emit["predicate_tables"] if emit is not None else None + return ctx, views, merge_views(per_view), tables def public(row) -> dict: @@ -3418,7 +3545,7 @@ def public(row) -> dict: def cmd(args) -> None: - ctx, views, rows = run(args) + ctx, views, rows, tables = run(args) selected = ( rows if args.cmd == "report" @@ -3443,7 +3570,10 @@ def cmd(args) -> None: "status": worst.get(c.path, "outside")} for c in files ] # fmt: skip if args.cmd == "report": + payload["pack_time"] = pack_time(tables, lambda t: table_edited(ctx, t)) payload["rollup"] = rollup(rows) + if tables is not None: + payload["predicate_tables"] = tables sys.stdout.write(json.dumps(payload, indent=2) + "\n") return out = [] @@ -3459,7 +3589,9 @@ def cmd(args) -> None: else: out += ["", f"no {args.cmd} rows"] if args.cmd == "report": - out += ["", rollup(rows)] + if tables is not None: + out += ["", render_tables(tables, lambda t: table_edited(ctx, t))] + out += ["", pack_time(tables, lambda t: table_edited(ctx, t)), rollup(rows)] sys.stdout.write("\n".join(out) + "\n") diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index 8ac4f0ae69ef..b1ca79058b14 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -27,6 +27,18 @@ differs, so it includes `params_changed`. A variant that fails to build is recorded under `failures` and takes no part in the comparison. +Predicate tables (`predicate_tables`, head side). For each family's builder, and for each other +`build_*` function of its module that the builds called (a KernelModule's kernel builders), the +child takes the shipped specs (for an inner builder: the specs the builds passed it), varies one +int or bool field at a time from up to four seeds (the shipped values, 0, -1, and the negation, +double, half and 1.5 times of each shipped positive value), and asks the predicate the packer +resolves (`_resolve_support_predicate`). An accepted spec that the builder raises on is a +`builder` finding. Between two builders of one family, a value outside the sign range of every +shipped value (<= 0 where all are >= 1, < 0 where all are >= 0) of a field they share that one +predicate accepts and the other refuses is a `sign` finding on the one that accepts. A finding +the base's table also has (same check, same field) is marked `at_base`. A family's tables stop +after `IMPACT_EMIT_TABLE_SECONDS` (default 60) and say `truncated`. + The flavor defaults to the newest flavor the head and the base both know (the top rung of `_LLVM_FLAVOR_LADDER` in `rocke/platform/python/rocke/core/lower_llvm.py`, else the last of `LLVM_FLAVORS`); with none shared it is the head's newest and the base fails loudly. @@ -39,7 +51,9 @@ import argparse import ast +import dataclasses import hashlib +import inspect import io import json import os @@ -51,6 +65,7 @@ import tarfile import tempfile import time +import traceback from concurrent.futures import ThreadPoolExecutor from pathlib import Path @@ -458,6 +473,9 @@ def __init__(self): self.kernel_module = KernelModule self.require_arch_sig = getattr(rc, "_require_spec_arch_signature", None) self.check_predicate = getattr(rc, "_check_support_predicate", None) + # The packer's own predicate lookup (alias, derived name, then is_valid*), which the + # predicate tables reuse so they test the predicate the packer would consult. + self.resolve_predicate = getattr(rc, "_resolve_support_predicate", None) # The packer's lowering backend; a tree without the pin is taken as Python. backend = getattr(rc, "_BACKEND", "python") if backend != "python": @@ -499,7 +517,33 @@ def _compile_arch(h, shard, target): return shard -def _build_one(h, v, arch): +def _record_inner_builders(module, shipped): + """Wrap MODULE's other `build_*` functions so each call records (name, spec) while the + shipped builder runs; returns (calls, restore). A KernelModule builder reaches its kernels' + builders through the module's globals, so this sees the spec of every companion launch. + """ + calls, saved = [], {} + for name, obj in list(vars(module).items()): + if name.startswith("build_") and name != shipped and inspect.isfunction(obj): + + def recorder(*args, _name=name, _fn=obj, **kwargs): + if args: + calls.append((_name, args[0])) + return _fn(*args, **kwargs) + + saved[name] = obj + setattr(module, name, recorder) + + def restore(): + for name, obj in saved.items(): + setattr(module, name, obj) + + return calls, restore + + +def _build_one(h, v, arch, capture=None): + """Build and lower one variant. CAPTURE, a dict, receives the module, the spec object + and the (inner builder, spec) calls the build made, for the predicate tables.""" from importlib import import_module module = import_module(h.module_from_source(v["source"])) @@ -510,7 +554,13 @@ def _build_one(h, v, arch): obj = h.build_spec(cls, v["spec"]) if callable(h.check_predicate): h.check_predicate(module, v["builder"], obj, arch) - kd = fn(obj, arch=arch) + calls, restore = _record_inner_builders(module, v["builder"]) + try: + kd = fn(obj, arch=arch) + finally: + restore() + if capture is not None: + capture.update(module=module, spec=obj, inner=calls) # A KernelModule is lowered kernel by kernel, as the packer's `compile_module` does; each # companion is its own launch with its own kernarg list. is_module = h.kernel_module is not None and isinstance(kd, h.kernel_module) @@ -536,6 +586,247 @@ def _build_one(h, v, arch): return built +# ------------------------------------------------------------ predicate tables (child) + +#: Seeds per table, and how many distinct shipped values of one int field seed its domain. +_TABLE_SEEDS = 4 +_TABLE_BASE_INTS = 6 + + +def _spec_key(spec): + return repr(dataclasses.asdict(spec)) + + +def _domains(specs): + """{field: (kind, shipped ints)} for the int and bool fields of the shipped specs. An int + field may also be None in some specs (an Optional override).""" + out = {} + for f in dataclasses.fields(specs[0]): + vals = [getattr(s, f.name, None) for s in specs] + ints = sorted({x for x in vals if type(x) is int}) + if ints and all(x is None or type(x) is int for x in vals): + out[f.name] = ("int", ints) + elif all(type(x) is bool for x in vals): + out[f.name] = ("bool", []) + return out + + +def _candidates(kind, ints): + """Values to try for one field: the shipped ones, 0, -1, and around each shipped positive + value its negation, double, half and one and a half times.""" + if kind == "bool": + return [False, True] + pos = [x for x in ints if x > 0] + if len(pos) > _TABLE_BASE_INTS: + pos = sorted(set(pos[:4] + pos[-2:])) + out = set(ints if len(ints) <= _TABLE_BASE_INTS else pos) | {0, -1} + for x in pos: + out |= {-x, 2 * x, x // 2, x + x // 2} + return sorted(out) + + +def _seeds(specs, domains): + """Up to _TABLE_SEEDS shipped specs, picked greedily to cover the most (field, value) pairs.""" + chosen, seen, pool = [], set(), list(specs) + while pool and len(chosen) < _TABLE_SEEDS: + best = max( + pool, key=lambda s: len({(f, repr(getattr(s, f))) for f in domains} - seen) + ) + gain = {(f, repr(getattr(best, f))) for f in domains} - seen + if chosen and not gain: + break + chosen.append(best) + seen |= gain + pool.remove(best) + return chosen + + +def _verdict(pred, spec, kw): + res = pred(spec, **kw) + return bool(res[0]) if isinstance(res, tuple) else bool(res) + + +def _where(exc): + """`file:line` of the frame that raised EXC.""" + frames = traceback.extract_tb(exc.__traceback__) + return f"{Path(frames[-1].filename).name}:{frames[-1].lineno}" if frames else "?" + + +class _Table: + """One builder's table: the record that goes into the emit file, and what the + same-module sign check needs (the predicate, its first seed, the field domains).""" + + def __init__(self, name): + self.name = name + self.record = { + "predicate": "", + "rows": 0, + "skipped": "", + "truncated": False, + "findings": [], + } + self.found = {} + self.pred = self.kw = self.seeds = self.domains = None + + def note(self, check, field, value, seed, detail): + f = self.found.setdefault( + (check, field), + { + "check": check, + "field": field, + "values": [], + "seed": ", ".join(f"{k}={getattr(seed, k)!r}" for k in self.domains)[ + :300 + ], + "detail": detail, + }, + ) + if value not in f["values"] and len(f["values"]) < 8: + f["values"].append(value) + + def accepts(self, seed, field, value): + """The predicate's verdict on SEED with FIELD set to VALUE; a spec that cannot be + constructed, or a predicate that raises, is no acceptance.""" + try: + return _verdict( + self.pred, dataclasses.replace(seed, **{field: value}), self.kw + ) + except Exception: + return False + + def finish(self): + self.record["findings"] = sorted( + self.found.values(), key=lambda f: (f["check"], f["field"]) + ) + for f in self.record["findings"]: + f["values"].sort(key=lambda v: (type(v) is bool, v)) + return self.record + + +def _predicate_table(h, module, name, catalog, arch, deadline): + """The table of one builder: its shipped specs varied one field at a time, each variation + put to the predicate the packer would consult. A spec the predicate accepts is built; a + builder that raises on it is a `builder` finding.""" + t = _Table(name) + rec = t.record + if not callable(h.resolve_predicate): + rec["skipped"] = "hkp_pack has no _resolve_support_predicate at this revision" + return t + try: + pname, pred = h.resolve_predicate(module, name) + except Exception as e: + rec["skipped"] = f"predicate lookup raised {type(e).__name__}: {str(e)[:200]}" + return t + if pred is None: + rec["skipped"] = "no support predicate takes its spec" + return t + rec["predicate"] = pname + specs = list({_spec_key(s): s for s in catalog}.values()) if catalog else [] + if not specs or not all( + dataclasses.is_dataclass(s) and not isinstance(s, type) for s in specs + ): + rec["skipped"] = "no shipped spec, or a spec that is not a dataclass instance" + return t + t.domains = _domains(specs) + t.seeds = _seeds(specs, t.domains) + t.pred = pred + t.kw = {"arch": arch} if "arch" in inspect.signature(pred).parameters else {} + fn = getattr(module, name) + bkw = {"arch": arch} if "arch" in inspect.signature(fn).parameters else {} + for seed in t.seeds: + for field, (kind, ints) in t.domains.items(): + for value in _candidates(kind, ints): + current = getattr(seed, field) + if value == current and type(value) is type(current): + continue + if time.time() >= deadline: + rec["truncated"] = True + break + try: + spec = dataclasses.replace(seed, **{field: value}) + accepted = _verdict(pred, spec, t.kw) + except Exception: + continue # refused at construction, or the predicate has no verdict + rec["rows"] += 1 + if not accepted: + continue + try: + fn(spec, **bkw) + except Exception as e: + t.note("builder", field, value, seed, + f"{pname} accepts it; {name} raises {type(e).__name__}: {str(e)[:200]} at {_where(e)}") # fmt: skip + return t + + +def _same_module_signs(tables, deadline): + """`sign` findings between the tables of one family (a KernelModule's builders): for a + field two of them share, a value outside the sign range of every shipped value (<= 0 where + all are >= 1, < 0 where all are >= 0) that one predicate accepts and the other refuses. + """ + live = [t for t in tables if t.pred is not None and t.seeds] + for a in live: + for b in live: + if a is b: + continue + for field, (kind, ints_a) in a.domains.items(): + kind_b, ints_b = b.domains.get(field, (None, [])) + if kind != "int" or kind_b != "int": + continue + low = min(ints_a[0], ints_b[0]) + if low < 0: + continue + values = sorted( + v + for v in set(_candidates("int", ints_a)) + | set(_candidates("int", ints_b)) + if v < 0 or (low >= 1 and v == 0) + ) + for v in values: + if time.time() >= deadline: + a.record["truncated"] = True + return + if a.accepts(a.seeds[0], field, v) and not b.accepts( + b.seeds[0], field, v + ): + a.note("sign", field, v, a.seeds[0], + f"{a.record['predicate']} accepts {field}={v}; {b.record['predicate']}, the predicate of " + f"{b.name} in the same module, refuses it; every shipped value is >= {low}") # fmt: skip + + +def _tables(h, fams, captures): + """One table per family's shipped builder and per inner builder its builds called.""" + seconds = float(os.environ.get("IMPACT_EMIT_TABLE_SECONDS", "60")) + out = [] + for key, fam in fams.items(): + got = captures.get(key) + if not got: + continue + module = got["module"] + inner = {} + for name, spec in got["inner"]: + inner.setdefault(name, []).append(spec) + deadline = time.time() + seconds + tables = [ + _predicate_table(h, module, name, catalog, fam["arch"], deadline) + for name, catalog in [ + (fam["builder"], got["specs"]), + *sorted(inner.items()), + ] + ] + _same_module_signs(tables, deadline) + for t in tables: + out.append( + { + "arch": fam["arch"], + "source": fam["source"], + "builder": fam["builder"], + "inner": "" if t.name == fam["builder"] else t.name, + **t.finish(), + } + ) + return out + + def child_main(root, out): # The script's own directory must not shadow rocKE modules. here = str(Path(__file__).resolve().parent) @@ -543,7 +834,7 @@ def child_main(root, out): t0 = time.time() variants, skipped = _discover(root) h = _Helpers() - fams, specs = {}, {} + fams, specs, captures = {}, {}, {} # One family per (compile arch, source, builder): the packer compiles once per # compile arch, so shard arches that share a generic target collapse into one family. for v in variants: @@ -586,11 +877,19 @@ def child_main(root, out): fam["failures"][v["name"]] = err continue try: - fam["built"][v["name"]] = _build_one(h, v, carch) + capture = {} + fam["built"][v["name"]] = _build_one(h, v, carch, capture) except Exception as e: fam["failures"][v["name"]] = f"{type(e).__name__}: {str(e)[:300]}" + continue + got = captures.setdefault( + key, {"module": capture["module"], "specs": [], "inner": []} + ) + got["specs"].append(capture["spec"]) + got["inner"] += capture["inner"] doc = { "families": list(fams.values()), + "tables": _tables(h, fams, captures), "not_inspected": skipped, "seconds": round(time.time() - t0, 1), } @@ -784,6 +1083,33 @@ def by_key(side): return out +def compare_tables(base_side, head_side): + """The head's predicate tables. `at_base` on a table says the base ran the same builder's + table (it was shipped and tabled there); on a finding, that the base's table has a finding of + the same check on the same field, so it predates the change. A builder the base did not + table can carry an old predicate defect that ships for the first time.""" + + def key(t): + return (t["arch"], t["source"], t["builder"], t["inner"]) + + seen = { + key(t): {(f["check"], f["field"]) for f in t["findings"]} + for t in base_side.get("tables", []) + if not t["skipped"] + } + return [ + { + **t, + "at_base": key(t) in seen, + "findings": [ + {**f, "at_base": (f["check"], f["field"]) in seen.get(key(t), set())} + for f in t["findings"] + ], + } + for t in head_side.get("tables", []) + ] + + def cmd_run(a): repo = Path(a.repo) if not repo.is_dir(): @@ -798,6 +1124,7 @@ def cmd_run(a): fh = pool.submit(run_side, repo, a.head, head_tree, flavor, a.timeout, "head") base_side, head_side = fb.result(), fh.result() families = compare(base_side, head_side) + tables = compare_tables(base_side, head_side) not_inspected = [ {"kdp": n["kdp"], "side": side, "reason": n["reason"]} for side, doc in (("base", base_side), ("head", head_side)) @@ -805,7 +1132,7 @@ def cmd_run(a): ] seconds = round(time.time() - t0, 1) doc = { - "schema": 4, + "schema": 5, "tool": "impact_emit", "flavor": flavor, "base": a.base, @@ -813,6 +1140,7 @@ def cmd_run(a): "base_tree": base_tree, "head_tree": head_tree, "families": families, + "predicate_tables": tables, "not_inspected": not_inspected, "seconds": seconds, } @@ -833,6 +1161,13 @@ def cmd_run(a): ) for n in not_inspected: print(f"not inspected [{n['side']}] {n['kdp']}: {n['reason']}") + for t in tables: + who = t["builder"] + (f"[{t['inner']}]" if t["inner"] else "") + state = t["skipped"] or ( + f"{t['rows']} rows, {len(t['findings'])} findings" + + (", truncated" if t["truncated"] else "") + ) + print(f"table {t['arch']} {t['source']}:{who}: {state}") print(f"flavor {flavor}, {len(families)} families, {seconds}s") return 0 diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index 3a81fc1c5d75..e61584a878c2 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -514,17 +514,104 @@ def family(**fields) -> dict: return fam -def write_emit(path, repo, base, head, families=None, **fields): +def write_emit(path, repo, base, head, families=None, tables=(), **fields): """An impact_emit document made for base..head, holding `families` (default: one family - built from `fields`).""" - doc = {"schema": 4, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, + built from `fields`) and the predicate `tables`.""" + doc = {"schema": 5, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, "base_tree": tree_of(repo, base), "head_tree": tree_of(repo, head), "families": [family(**fields)] if families is None else families, - "not_inspected": [], "seconds": 1.0} # fmt: skip + "predicate_tables": list(tables), "not_inspected": [], "seconds": 1.0} # fmt: skip Path(path).write_text(json.dumps(doc), encoding="utf-8") return str(path) +def table(findings=(), **fields) -> dict: + """One predicate table of the builder, tabled at base too, holding `findings`.""" + t = dict(arch="gfx950", source="kernels/gfx950/attn.py", builder="build_attn", inner="", + predicate="supports_attn", rows=12, skipped="", truncated=False, at_base=True, + findings=list(findings)) # fmt: skip + t.update(fields) + return t + + +def finding(check="builder", field="tile", at_base=False, **fields) -> dict: + f = dict(check=check, field=field, values=[24], seed="tile=16", at_base=at_base, + detail="supports_attn accepts it; build_attn raises AssertionError: tile") # fmt: skip + f.update(fields) + return f + + +def test_predicate_tables_are_listed_new_first_and_never_move_a_row_or_the_rollup( + mod, capsys, tmp_path +): + # catches: a pack-time table finding turned into a row (changing the rollup), a + # pre-existing finding, or an older one on a builder that first ships now, counted as new, + # a finding on a builder the change edits counted as an older one, or the tables dropped + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BODY_EDIT}) # edits build_attn + plain = write_emit(tmp_path / "plain.json", repo, base, head) + tables = [table([finding(at_base=True, field="n", check="sign", values=[0, -1])]), + table([finding(field="block")], inner="build_new", at_base=False), + table([finding(field="k")], at_base=False), + table([finding()], inner="build_reduce", predicate="supports_reduce")] # fmt: skip + with_tables = write_emit(tmp_path / "t.json", repo, base, head, tables=tables) + a = report(mod, capsys, repo, base, head, "--emit", plain) + b = report(mod, capsys, repo, base, head, "--emit", with_tables) + assert (a["rows"], a["rollup"]) == (b["rows"], b["rollup"]) + assert b["predicate_tables"] == tables and a["predicate_tables"] == [] + argv = [ + "report", + "--repo", + str(repo), + "--base", + base, + "--head", + head, + "--emit", + with_tables, + ] + assert mod.main(argv) == 0 + text = capsys.readouterr().out + assert "2 new, 1 first shipped, 1 at base" in text + new_tabled = text.index("| tile | 24 | new |") + new_edited = text.index("| k | 24 | new |") + first_ship = text.index("| block | 24 | first shipped |") + old = text.index("| sign | n | 0, -1 | at base |") + assert ( + max(new_tabled, new_edited) < first_ship < old < text.index("Runtime impact:") + ) + # the pack-time line sits right above the runtime verdict, which stays the last line + assert text.rstrip().splitlines()[-2:] == [ + "Pack-time: NEW 2, FIRST-SHIPPED 1", + b["rollup"], + ] + assert b["pack_time"] == "Pack-time: NEW 2, FIRST-SHIPPED 1" + assert a["pack_time"] == "Pack-time: NOT RUN (no shipped rocKE builder to table)" + static = report(mod, capsys, repo, base, head) + assert static["pack_time"] == "Pack-time: NOT RUN (emitter not run)" + + +@pytest.mark.parametrize( + "tables, line", + [ + ([table([finding(at_base=True)])], "Pack-time: CLEAN (1 tables)"), + ([table([finding(at_base=False)], at_base=False)], "Pack-time: FIRST-SHIPPED 1"), + ([table([finding()]), table(skipped="no support predicate takes its spec")], + "Pack-time: NEW 1; not run for 1 of 2 tables (no support predicate takes its spec)"), + ([table(), table(truncated=True)], + "Pack-time: NOT RUN for 1 of 2 tables (truncated); no finding in the rest"), + ], + ids=["pre-existing-only-is-clean", "first-shipped", "new-with-a-skipped-table", + "truncated-is-never-clean"], +) # fmt: skip +def test_pack_time_line_counts_only_what_the_change_ships_and_never_reads_clean_unrun( + mod, tables, line +): + # catches: a pre-existing finding counted as new, a newly shipped older defect hidden, + # or a skipped or truncated table reported as CLEAN + assert mod.pack_time(tables) == line + + def test_emit_fold_equal_variants_make_shipped_module_edits_inert( mod, capsys, tmp_path ): @@ -659,7 +746,7 @@ def test_emit_made_for_another_base_or_head_is_refused(mod, capsys, tmp_path): @pytest.mark.parametrize( "spoil", [ - lambda d: d.update(schema=3), + lambda d: d.update(schema=4), lambda d: d.update(not_inspected={}), lambda d: d["families"][0].update(failures=[]), lambda d: d["families"][0].pop("variants"), @@ -668,15 +755,27 @@ def test_emit_made_for_another_base_or_head_is_refused(mod, capsys, tmp_path): lambda d: d["families"][0].update( companions={"base": None, "head": {"reduce": None}} ), + lambda d: d.pop("predicate_tables"), + lambda d: d.update(predicate_tables=[table([finding(check="maybe")])]), + lambda d: d.update(predicate_tables=[table([finding(values=["24"])])]), + lambda d: d.update(predicate_tables=[table(rows=None)]), + lambda d: d.update( + predicate_tables=[{k: v for k, v in table().items() if k != "at_base"}] + ), ], ids=[ - "schema-3-without-companions", + "schema-4-without-tables", "not-inspected-dict", "failures-list", "no-variants", "params-str", "no-companions", "companion-without-params", + "no-predicate-tables", + "table-finding-unknown-check", + "table-finding-str-value", + "table-without-rows", + "table-without-at-base", ], ) def test_malformed_emit_exits_one_instead_of_folding_or_crashing( diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index c4237139a007..c4908300b9ab 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -115,6 +115,62 @@ def module_files(**kw): return files +# The packer's predicate lookup in miniature: `supports_`. +RESOLVER = """ +def _resolve_support_predicate(module, builder): + name = "supports_" + builder[len("build_"):] + fn = getattr(module, name, None) + return (name, fn) if callable(fn) else (None, None) +""" + + +def table_builder_src(reduce_check="spec.n % 8 == 0"): + """A KernelModule builder for the predicate tables. Its predicate accepts any positive + tile, but the builder asserts the tile divides the block. Its `reduce` companion's spec + shares the field `n` (8 times the primary's), its predicate is REDUCE_CHECK, and the + primary's predicate refuses n <= 0.""" + return textwrap.dedent( + f""" + import dataclasses + from rocke.helpers import KernelModule + + @dataclasses.dataclass(frozen=True) + class Spec: + n: int = 1 + block: int = 32 + tile: int = 16 + + @dataclasses.dataclass(frozen=True) + class RSpec: + n: int = 8 + + def supports_fake(spec, arch="x"): + return spec.n >= 1 and spec.block >= 1 and spec.tile >= 1, "ok" + + def supports_reduce(spec, arch="x"): + return {reduce_check}, "ok" + + def build_reduce(spec, arch): + return {{"params": [("out", "ptr addrspace(1)")], "body": "r"}} + + def build_fake(spec, arch): + assert spec.block % spec.tile == 0, "tile must divide block" + p = [("q", "ptr addrspace(1)"), ("n", "i32")] + return KernelModule({{"params": p, "body": "v1"}}, + {{"reduce": build_reduce(RSpec(n=8 * spec.n), arch=arch)}}) + """ + ) + + +def table_files(**kw): + files = module_files() + files[f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py"] = ( + HELPERS + RESOLVER + ) + files[BUILDER] = table_builder_src(**kw) + return files + + KDP_VARIANTS = ({"name": "v_a", "spec": {"n": 1}}, {"name": "v_b", "spec": {"n": 2}}) @@ -246,6 +302,8 @@ def scenarios(): s["module"] = module_files() s["module_wide"] = module_files(reduce_params=[("lse", "ptr addrspace(1)")]) s["module_body"] = module_files(reduce_body="r2") + s["tables"] = table_files() + s["tables_fixed"] = table_files(reduce_check="spec.n >= 1 and spec.n % 8 == 0") return s @@ -359,7 +417,7 @@ def test_extra_param_lists_every_variant_and_shows_head_list(emit, repo, tmp_pat def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): rc, doc = run(emit, repo, "base", "body", tmp_path) assert rc == 0 - assert doc["schema"] == 4 + assert doc["schema"] == 5 # the recorded ids are the real tree ids, and differ when the sides differ assert doc["base_tree"] == git_out(repo, "rev-parse", "base^{tree}") assert doc["head_tree"] == git_out(repo, "rev-parse", "body^{tree}") @@ -391,6 +449,67 @@ def test_kernel_module_builder_records_and_compares_every_launch( assert f["ir_changed"] == ir_changed +def findings_of(doc): + """{(inner builder or "", check, field): finding} over every predicate table.""" + return { + (t["inner"], f["check"], f["field"]): f + for t in doc["predicate_tables"] + for f in t["findings"] + } + + +def test_predicate_tables_flag_what_the_builder_rejects_and_a_companion_sign_gap( + emit, repo, tmp_path +): + # catches: a predicate that accepts a tile the builder asserts on (#14259 S-1), and a + # companion predicate that accepts n <= 0 where the module's own predicate refuses it + # (#14242 r4385341723), reported as nothing; or a field every predicate guards flagged + _, doc = run(emit, repo, "module", "tables", tmp_path) + found = findings_of(doc) + assert set(found) == {("", "builder", "tile"), ("build_reduce", "sign", "n")} + tile = found["", "builder", "tile"] + assert 24 in tile["values"] and "AssertionError" in tile["detail"] + assert set(found["build_reduce", "sign", "n"]["values"]) == {-16, -8, 0} + # the base's tables could not run (no resolver there): nothing predates the change, and + # no table counts as tabled at base + assert not any(f["at_base"] for f in found.values()) + assert not any(t["at_base"] for t in doc["predicate_tables"]) + (main,) = [t for t in doc["predicate_tables"] if not t["inner"]] + assert (main["predicate"], main["skipped"], main["truncated"]) == ( + "supports_fake", + "", + False, + ) + + +def test_predicate_table_findings_also_at_base_are_pre_existing_and_a_fix_clears_one( + emit, repo, tmp_path +): + # catches: a finding the base already had reported as new, or one the head fixed kept + _, doc = run(emit, repo, "tables", "tables", tmp_path) + assert all(t["at_base"] for t in doc["predicate_tables"]) + assert all(f["at_base"] for f in findings_of(doc).values()) + _, doc = run(emit, repo, "tables", "tables_fixed", tmp_path) + found = findings_of(doc) + assert ( + set(found) == {("", "builder", "tile")} + and found["", "builder", "tile"]["at_base"] + ) + + +def test_predicate_tables_say_why_they_did_not_run_and_stop_at_their_budget( + emit, repo, tmp_path, monkeypatch +): + # catches: a revision without the packer's resolver read as "no findings", or a table + # that runs past its budget instead of stopping and saying so + _, doc = run(emit, repo, "base", "base", tmp_path) + (t,) = doc["predicate_tables"] + assert t["skipped"].startswith("hkp_pack has no _resolve_support_predicate") + monkeypatch.setenv("IMPACT_EMIT_TABLE_SECONDS", "0") + _, doc = run(emit, repo, "tables", "tables", tmp_path) + assert all(t["truncated"] and t["rows"] == 0 for t in doc["predicate_tables"]) + + def test_removed_variant_is_only_in_base_and_unchanged_one_is_not_flagged( emit, repo, tmp_path ): From 1b47edb4b9d1c73eebbef59e0bd48b40fa87068f Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Tue, 3 Nov 2026 11:59:53 -0600 Subject: [PATCH 08/13] fix(hipdnn): close ingestor impact gaps that read a break as no impact The ingestor impact report could call a change NONE or N/A when it was not. Multi-launch UKDs, which list their launches under `sources`, were neither rooted in the shipped closure nor built by the emitter, so an edit to such a kernel read as reached by no shipped variant. A launch-surface grid or block helper was compared as its named def alone, so a change in the code it hands off to folded to inert on equal emitted IR. A flatc header counted as regenerated beside any change under the generator or the flatbuffers SDK, tests included, and plugin SDK headers that the descriptor loader compiles in were outside the surface. The closure now roots on every launch, and the emitter groups a multi-launch UKD's launches by source, builder, spec and target as the packer does, listing any it cannot read as not inspected; the emit schema becomes 6. A host-side surface function is compared together with every def, class and assignment it reaches by name. A flatc header is regenerated only beside its own schema or flag file, and SDK headers in the include closure of the ingestor SDK headers, with the generic-target table's CMake template, are on the surface. Device arch lists come from the UKD and KDP arch arrays with generic targets expanded, and a file outside the shipped closure widens the arch its path names to every arch a KDP named for it ships. With a target tip, the emit fold also applies to merged-view rows whose module content is the head's. A member read through a same-named class imported from another module is no longer a reader of the removed one. A global_buffer argument declared with a non-pointer size disagrees with a pointer parameter, a path-only python_source locator is accepted, a static list related to the declared builder is compared with it before a smaller builder it happens to fit, and flatc_flags.txt is read as build input. The registry enumerator stays in the emitter child's process group, so a timeout ends it too. ALMIOPEN-2834 --- .../ai/skills/hipdnn-deep-review/RUNBOOK.md | 2 + .../lanes/runtime-impact.md | 2 +- .../hipdnn-ingestor-impact/reference.md | 60 ++- .../hipdnn-ingestor-impact/scripts/impact.py | 434 +++++++++++++++--- .../scripts/impact_emit.py | 155 +++++-- .../tools/ai/tests/test_ingestor_impact.py | 218 ++++++++- .../ai/tests/test_ingestor_impact_emit.py | 226 ++++++++- 7 files changed, 952 insertions(+), 145 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md index 849cf8b26272..7edcd920089b 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md @@ -353,6 +353,8 @@ Findings whose `Where` is in `affected.txt`, and every `process` finding, are af each lane the delta touches, every lane `FAILED` earlier (closing its `ORCH` finding when it completes), and always `AUTH` and `LEAK`: a new head brings new commits, PR text and CI runs. Re-run `DRIFT` whenever the round's `drift.txt` says `Trigger: yes`: the target moves between rounds. +Re-run `IMPACT` whenever the target tip moved since its last run, on a new head or the same one: +its merged view is computed against that tip and goes stale with it. Re-verify every affected `OPEN` or `UNVERIFIED` finding. A lane not re-run carries forward (Coverage and Lanes rows copied, marked `carried from r`). diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md index 29e7d4453f10..0e46495d2200 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -59,7 +59,7 @@ searched as copies and readers, so a stale pin or a stale import is yours. | Judgment | Finding | |---|---| | A `mismatch` that holds | A `source-claim` at `BLOCKING` ([../policy.md](../policy.md): an ABI break without a migration path). **Where**: the producer line and every disagreeing copy. **Proof**: the command and both quoted lists. A `merged-only` mismatch: **Fix** is to rebase and update the named copy. The same mismatch at `$BASE` is pre-existing: `FUTURE WORK` at most | -| A dispatch divergence | A `source-claim` at `IMPORTANT` or higher, quoting both sides | +| A dispatch divergence | A `source-claim` at `BLOCKING`, quoting both sides | | A descriptor or generic-target value the loader rejects or ignores | A finding, citing the loader rule | | No break (`changed` but consistent), `inert`, `outside` | No finding. Count them under **Checked, no finding**; list each `inert` reason once | | A predicate-table finding in state `new` or `first shipped` that holds | A `defect` at `IMPORTANT`: the packer accepts a spec its builder rejects, or a value the kernel cannot use. For `first shipped`, say the predicate is older and ships for the first time. **Proof**: the table's row (predicate, field, values, seed, the builder's error). A finding `at base` predates the change: `FUTURE WORK` at most | diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md index 5434ab636c66..36e62d57fdd8 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -18,7 +18,8 @@ change that breaks them leaves the default build green. | Launch surface | Python grid, block, LDS and tile tables | the `cpp_mirror` files a `launch_surface:` entry names in `projects/hipdnn/tools/IngestorGenerator/configs/*.engine.yaml` | | Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | | Shipped variants | `*.kdp.json` (DVC-held for gfx950; the emitter regenerates its variant list with the generator's registry enumerator, so a changed DVC pointer stays `changed`: the file's other fields are not read) | loader, native packs | -| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` and `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` | packer and loader | +| Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` and its CMake template `projects/hipdnn/plugin_sdk/cmake/GpuGenericTargetsTable.hpp.in` | packer and loader | +| Loader code | the ingestor SDK headers under `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, and every plugin SDK header they include (`ArchMatch.hpp`, `heuristics/uhd/`; tagged `sdk-loader`) | the native packs and the loader that compile them | | Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | Test code, CMake, CI test-category lists and `requirements*.txt` are not judged here: they are @@ -30,19 +31,36 @@ How the copies are paired: - A `launch_surface` entry's `python_source` symbol resolves as `launch_surface.py --check` resolves it: any function of that name anywhere in the module (`ast.walk`), a method, a nested function or a def under `if`, `try` or `with` included. Every function of that name is compared, - and the symbol is reported missing only when none exists. + and the symbol is reported missing only when none exists. A locator that is a path alone names + no symbol, and the whole module is its Python side. +- A host-side surface function (a grid or block helper: anything but the builder the packer + compiles, or a `kernargs` entry) is compared together with every top-level def, class and + assignment it reaches by name, followed through `from` imports. A grid that hands off to + `NONPERSIST_DECODES[...].grid` changes when that class does, and no emitted IR covers it. - A declared `kernargs` pair compares each static C++ list in its `cpp_mirror` with the builder whose `b.param` list it matches, searched in the surface's module and the manifest's own `source` module, the declared function first. A multi-launch pack holds one list per launch (a segment and a reduce kernel), and a declared function may be a `SignatureBuilder` factory with no - `b.param` list; the row then names the builder it was paired with. A list that matches no - builder but is related to one (it shares at least two names, and at least half of its own) is - compared with the related builder it shares most names with, and is a `mismatch`. An unrelated - list is skipped; when no list of the mirror was compared, one `changed` row says `cannot - compare`. + `b.param` list; the row then names the builder it was paired with. A list related to the + declared builder (it shares at least two names, and at least half of its own) is compared with + it unless another builder shares more names, so a list cut short of the declared builder's + params is a `mismatch`, not a match with a smaller builder. A list that matches no builder but + is related to one is compared with the related builder it shares most names with, and is a + `mismatch`. An unrelated list is skipped; when no list of the mirror was compared, one + `changed` row says `cannot compare`. +- A `global_buffer` entry is a pointer unless its declared size says otherwise; a 4-byte one + disagrees with a pointer param, as `requireSignatureMatch` refuses it. The C++ side has no type + field, so the scripts compare names, order and sizes. - A copy that holds the old list is `changed`, not `mismatch`, when it still matches the new list with the new conditional parameters left out: variants that do not enable them pass that list. The variants that do enable them are what the emitter or a device run settles. +- A `NEEDS-DEVICE` arch list comes from the shipped descriptors: the UKD's `arch` array, else its + KDP's, with generic targets expanded through `gpu_generic_targets.json`. A file outside the + shipped closure (a pack, a descriptor, a DVC pointer) names its arch by path, widened to every + arch a KDP named for that arch ships on. +- A flatc `*_generated.h` reads as regenerated only beside a change to its own `.fbs` or + `flatc_flags.txt` (for the flatbuffers SDK, `schemas/.fbs` and `cmake/flatc_flags.txt`). + Any other generated artifact needs a change under its source tree; a test file never counts. ## The scripts @@ -52,9 +70,10 @@ How the copies are paired: - `scripts/impact_emit.py` imports and runs rocKE code from the two trees: a `git archive` extract per side, in a child process whose whole process tree is killed at its timeout, with an environment reduced to system, temp, locale and Python user-site variables, so credentials such - as `GH_TOKEN` are not passed. That is the same code execution as building the head. It builds - every shipped variant at both sides on the CPU (no GPU, comgr or network): a few minutes, with - the two sides on two concurrent workers. + as `GH_TOKEN` are not passed. That is the same code execution as building the head, and it is + not a sandbox: the child keeps `HOME` (`USERPROFILE` on Windows) and network access, and only + tokens held in environment variables are dropped. It builds every shipped variant at both sides + on the CPU (no GPU or comgr): a few minutes, with the two sides on two concurrent workers. Both exit 0 when the analysis ran, whatever it found, and 1 with one line (`impact: ` or `impact_emit: `) on stderr when they could not run. An exit 1 is never a verdict. @@ -77,7 +96,7 @@ stands in for when one of them is on the surface or a test. | `outside` | Not on the surface; counted, never dropped | The last line of `report` is the rollup: -`Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`. +`Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`. `BREAKS` is any `mismatch`, `NEEDS-DEVICE` any `changed`, `NONE` all surface rows `inert`, `N/A` no surface file. An empty diff is an error, never `NONE`. A `not judged here:` note counts the files left to other checks: `BUILD` is CMake and CI test-category lists, `DEPS` is `requirements*.txt` @@ -90,8 +109,9 @@ Run it when any rocKE or `hkp_pack` Python file changed in code, including rows it) misses a call through a dynamic lookup. It records each variant's argument list, IR digest and COMGR inputs (ISA name, codegen options, fresh-process compile); a builder that returns a `KernelModule` is lowered kernel by kernel, as the packer does, and each companion launch's -argument list is recorded and compared too. `impact.py report --emit` folds the result into the -rows. +argument list is recorded and compared too. A multi-launch UKD (`sources`) gives one variant per +distinct source, builder, spec and target of its launches; one the emitter cannot read is listed +under `not_inspected`. `impact.py report --emit` folds the result into the rows. - The emitter exiting 1 (it could not run, or a side timed out) leaves no proof. Never fold an emit file from an earlier run. `impact.py` refuses an emit file made for another base or head, or one @@ -101,7 +121,8 @@ rows. read a failure as equal. - An all-equal emit folds only the rows of modules a built family imports, and never a row on code that runs after lowering (`rocke/helpers/compile.py`, `rocke/runtime/`): the IR digest does not - cover it. A static kernarg `mismatch` is never downgraded. + cover it. A static kernarg `mismatch` is never downgraded. In the merged view it folds only the + rows of modules whose merged content is the head's: the emitter never saw the target's. ## Predicate tables @@ -165,7 +186,7 @@ change and gets no severity of its own; mention it only when the change makes it ## C++ differential tables (judge-written) -The scripts compare the names, order and types of a C++ copy, never its values or what it refuses. +The scripts compare the names, order and sizes of a C++ copy, never its values or what it refuses. For a `changed` `launch_surface` row whose C++ side is header-only and includes no HIP header (geometry, split rule, workspace, arguments), settle the values with a throwaway table. A `kernargs` or `workspace` row qualifies like any other, whether its C++ list is built from @@ -236,7 +257,7 @@ deciding. |---|---| | `mismatch` | A break: quote the producer line and every disagreeing copy. A `merged-only` mismatch breaks the target once merged; the fix is to rebase and update the named copy. If the same mismatch exists at the base, it predates the change | | `changed`, dispatch | Read the C++ function and its Python counterpart. A divergence (a constant, a range, a tile set, a mask or layout rule that the other side does not share) is a break; quote both sides. Consistent: no break | -| `changed`, emitted code or arguments | Consistent with every copy, names and types: no break. A row that says `types not compared` or `cannot compare` is not consistency: compare the types and sizes of both lists by hand. The variants changed on an architecture need a device run there | +| `changed`, emitted code or arguments | Consistent with every copy, names and sizes: no break. A row that says `sizes not compared` or `cannot compare` is not consistency: compare the sizes of both lists by hand. The variants changed on an architecture need a device run there | | `changed`, descriptor or generic target | Check the loader's rules for the changed fields (`projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/DescriptorLoader.hpp`); a value the loader rejects or ignores is a break. A membership change names the architectures whose archives change | | `inert`, `outside` | Count them; list each `inert` reason once | @@ -264,9 +285,10 @@ a flags-ON build when one exists), the architecture, and the flags. - A rocKE C++ engine source (`rocke/platform/cpp/`) reads `inert` only while no provider build file outside rocKE links the engine and the packer pins the Python backend; the row says so, so check that both still hold for the change. -- A static list is paired with the builder its names match. A C++ list that by mistake matches a - different builder of the same modules exactly is compared with that one, not the launch it is - used for; the row names the pairing, so check it. +- A static list is paired with the builder its names match, the declared builder first. A C++ + list that by mistake matches a different builder of the same modules exactly, sharing more names + with it than with the declared one, is compared with that one, not the launch it is used for; + the row names the pairing, so check it. - The emitter compares emitted arguments with the static C++ lists only for variants whose arguments changed between base and head. The variants of a new engine have no base side, so their lists are checked statically only. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index e9944869d441..8188b114092f 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -29,7 +29,12 @@ `requirements*.txt` to DEPS (the hipdnn-deep-review lanes that judge them); the rollup names how many it left to each. A rocKE C++ engine source is `inert` while no provider build file outside rocKE links the engine and the packer pins the Python lowering backend; any other non-Python rocKE -file is not understood. +file is not understood. A plugin SDK header in the include closure of the ingestor SDK headers is on +the surface (`sdk-loader`). A `NEEDS-DEVICE` arch list comes from the KDP and UKD `arch` arrays +(generic targets expanded); a file outside the shipped closure names its arch by path, widened to +every arch a KDP named for that arch ships on. A host-side `launch_surface` function (anything but +the builder the packer compiles) is compared together with every def, class and assignment it +reaches by name, since no emitted IR covers it. Rollup (last two lines of `report`): Pack-time: NEW [, FIRST-SHIPPED ][; not run for ...] | FIRST-SHIPPED | CLEAN ( tables) @@ -37,13 +42,14 @@ Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected[; not judged here: ...]) | N/A (no surface file[; not judged here: ...]) -`report --emit FILE.json` folds in impact_emit.py output (schema 5), which must have been made for +`report --emit FILE.json` folds in impact_emit.py output (schema 6), which must have been made for the same base and head trees: changed params of any launch (a KernelModule builder's companions included) vs the static C++ list that mirrors that launch is a mismatch; changed IR or COMGR inputs are `changed` for that arch; when every family is equal (nothing changed, nothing failed, nothing not inspected) the rows that were `changed` only because a shipped module changed become `inert`, but only on modules a family with built variants imports, and never on code that -runs after lowering (COMPILE_STEP) or on a DVC pointer. Static kernarg mismatch rows are never +runs after lowering (COMPILE_STEP) or on a DVC pointer. In the merged view the fold applies only to +rows of modules whose merged blob is the head's. Static kernarg mismatch rows are never downgraded. A module that the packer reaches ships on every architecture. The emit file's predicate tables (a predicate that accepts a spec its builder raises on, or a value outside the sign range of every shipped spec) are pack-time facts: `report` lists them in their own section, @@ -52,6 +58,7 @@ import argparse import ast +import copy import fnmatch import hashlib import json @@ -118,6 +125,8 @@ class Fail(Exception): ("sdk-ingestor", SDK_INGESTOR + "*"), ("generic-targets", GENERIC_TARGETS_JSON), ("generic-targets", SDK + "include/hipdnn_plugin_sdk/GpuGenericTargets*"), + # The CMake template of GpuGenericTargetsTable.hpp, which GpuGenericTargets.hpp includes. + ("generic-targets", SDK + "cmake/GpuGenericTargets*.in"), ("descriptor", DESC_DIR + "*"), ("native-pack", ENGINE + "*"), ("native-pack", HKP + "src/core/Container.cpp"), @@ -149,17 +158,22 @@ class Fail(Exception): # `rocke_core\w*` also covers the `rocke_core_objects` OBJECT library both engine libraries use. _ENGINE_LINK = re.compile(r"\b(?:rocke_core\w*|rocke_shared|rocke_engine|ckc::\w+)\b") +FLATBUFFERS_SDK = "projects/hipdnn/flatbuffers_sdk/" +#: Generated file pattern -> the source prefixes that regenerate it (test files never do). A +#: flatc header has no prefix: its sources are its own schema and flag file (`flatc_inputs`). GENERATED = ( ( "*/rocke/*/golden/*_sha256.json", (HKP + "rocke/library/kernels/", HKP + "rocke/platform/"), ), - ("*_generated.h", (GEN, "projects/hipdnn/flatbuffers_sdk/")), + ("*_generated.h", ()), ("*/hkp-packed.marker", (PACKER + "python/hkp_pack/",)), ("*.provenance.json.gz", (PACKER + "python/hkp_pack/",)), ("*.kpack", (PACKER + "python/hkp_pack/",)), ) DOC_SUFFIXES = (".md", ".rst", ".txt") +#: `.txt` files a build reads: flatc's flag lists. +BUILD_TXT = ("flatc_flags.txt",) C_SUFFIXES = ( ".c", ".cc", @@ -246,6 +260,8 @@ def __init__(self, repo, oid): self._paths = None #: module_index and module_imports caches. self.stems, self.imports = None, {} + #: kdp_archs caches: KDP path -> archs, and generic target -> members. + self.kdps, self.generics = None, {} @property def paths(self) -> set: @@ -409,8 +425,14 @@ def collect_changes(before: Tree, after: Tree) -> list: text = out.decode("utf-8", errors="replace") changes = parse_raw(text) hunks = parse_hunks(out) + loader = None for ch in changes: ch.tag = tag_of(ch.path) + if ch.tag == "other" and ch.path.startswith(SDK) and not ch.submodule: + if loader is None: + loader = loader_headers(before) | loader_headers(after) + if ch.path in loader: + ch.tag = "sdk-loader" readable = [c.path for c in changes if not c.submodule and c.tag != "other"] fetch([(t, p) for p in readable for t in (before, after)]) for ch in changes: @@ -423,6 +445,33 @@ def collect_changes(before: Tree, after: Tree) -> list: return sorted(changes, key=lambda c: c.path) +_SDK_INCLUDE = re.compile( + r'^[ \t]*#[ \t]*include[ \t]*[<"](hipdnn_plugin_sdk/[^>"]+)[>"]', re.M +) + + +def loader_headers(tree: Tree) -> set: + """SDK files compiled into the descriptor loader: the include closure, inside the plugin + SDK, of the ingestor SDK headers (`DescriptorLoader.hpp` among them). A header CMake + configures stands for its `cmake/.in` template.""" + include = SDK + "include/" + paths = tree.paths + seen = set() + frontier = sorted(p for p in tree.under(SDK_INGESTOR) if p.endswith(C_SUFFIXES)) + while frontier: + seen |= set(frontier) + found = set() + for data in tree.read(frontier).values(): + for match in _SDK_INCLUDE.finditer(strip_c_comments(decode(data) or "")): + target = include + match.group(1) + template = f"{SDK}cmake/{PurePosixPath(target).name}.in" + found.add( + target if target in paths or template not in paths else template + ) + frontier = sorted(found - seen) + return seen + + def merged_tree(repo, tip: str, head: str): """(tree oid, conflicted paths, None) of the merge of tip and head, or (None, None, reason). @@ -463,7 +512,10 @@ def is_requirements(path: str) -> bool: def is_doc(path: str) -> bool: name = PurePosixPath(path).name return ( - path.endswith(DOC_SUFFIXES) and not is_cmake(path) and not is_requirements(path) + path.endswith(DOC_SUFFIXES) + and not is_cmake(path) + and not is_requirements(path) + and name not in BUILD_TXT ) or name == ".gitignore" @@ -491,6 +543,63 @@ def arch_of(*texts): return None +def arch_names(*texts) -> set: + """Every architecture name in `texts`, lowercased.""" + return {m.group(0).lower() for text in texts for m in _ARCH.finditer(text or "")} + + +def generic_map(tree) -> dict: + """Generic target -> its member archs, from the tree's gpu_generic_targets.json.""" + out = {} + for generic, member in generic_members(tree.text(GENERIC_TARGETS_JSON) or "") or (): + out.setdefault(generic, set()).add(member) + return out + + +def expand_archs(archs, generics) -> set: + """`archs` with each generic target replaced by its members.""" + return {m for a in archs for m in generics.get(a, (a,))} + + +def descriptor_archs(kdp: dict, ukd: dict, generics) -> set: + """Archs a UKD ships on: its own `arch`, else its KDP's, generic targets expanded.""" + + def listed(value): + if isinstance(value, str): + return [value] + return ( + [a for a in value if isinstance(a, str)] if isinstance(value, list) else [] + ) + + return expand_archs(listed(ukd.get("arch")) or listed(kdp.get("arch")), generics) + + +def kdp_archs(tree) -> dict: + """KDP path -> every arch its UKDs ship on, for the KDPs git holds (a DVC-held one is + absent). Also caches the tree's generic-target map in `tree.generics`.""" + if tree.kdps is None: + tree.generics, tree.kdps = generic_map(tree), {} + paths = sorted(p for p in tree.under(DESC_DIR) if p.endswith(".kdp.json")) + for path, data in tree.read(paths).items(): + try: + doc = json.loads(decode(data) or "") + except ValueError: + continue + if not isinstance(doc, dict): + continue + ukds = doc.get("kernelDescriptors") + ukds = ( + [u for u in ukds if isinstance(u, dict)] + if isinstance(ukds, list) + else [] + ) + tree.kdps[path] = set().union( + descriptor_archs(doc, {}, tree.generics), + *(descriptor_archs(doc, u, tree.generics) for u in ukds), + ) + return tree.kdps + + def generated_rule(path: str): for pattern, sources in GENERATED: if fnmatch.fnmatchcase(path, pattern): @@ -498,6 +607,28 @@ def generated_rule(path: str): return None +def flatc_inputs(header: str) -> tuple: + """The schema and flag file flatc regenerates `header` from: a flatbuffers SDK header from + `schemas/.fbs` with hipDNN's `cmake/flatc_flags.txt`, any other from the sibling + `.fbs` and `flatc_flags.txt`.""" + pure = PurePosixPath(header) + stem = pure.name.removesuffix("_generated.h") + if header.startswith(FLATBUFFERS_SDK): + return ( + f"{FLATBUFFERS_SDK}schemas/{stem}.fbs", + "projects/hipdnn/cmake/flatc_flags.txt", + ) + return f"{pure.parent}/{stem}.fbs", f"{pure.parent}/flatc_flags.txt" + + +def regenerates(source: str, artifact: str) -> bool: + """Whether a change to `source` regenerates the generated file `artifact`.""" + if artifact.endswith("_generated.h"): + return source in flatc_inputs(artifact) + prefixes = generated_rule(artifact) or () + return source.startswith(prefixes) and tag_of(source) != "test" + + #: A C++ raw string opener with any encoding prefix: `R"x(`, `u8R"(`, `LR"(`, `uR"(`, `UR"(`. _RAW = re.compile(r'(?:u8|u|U|L)?R"([^()\\ ]{0,16})\(') @@ -679,7 +810,7 @@ def classify(ch: Change) -> str: dvc-pointer or binary. Decided by content, never by path (except docs and generated). """ path = ch.path - if generated_rule(path): + if generated_rule(path) is not None: return "generated" base = "" if ch.base is None else decode(ch.base) head = "" if ch.head is None else decode(ch.head) @@ -1147,10 +1278,12 @@ def cpp_kernel_lists(text: str) -> list: name = _STR.fullmatch(args[3]) if len(args) >= 4 else None names.append(name.group(1) if name else "") kind = _STR.fullmatch(args[0]) if args else None - sizes.append( - "ptr" if kind and kind.group(1) == "global_buffer" - else cpp_arg_size(args[1]) if len(args) > 1 else None - ) # fmt: skip + size = cpp_arg_size(args[1]) if len(args) > 1 else None + # A global_buffer is a pointer, unless its declared size says it is not: + # requireSignatureMatch refuses a pointer argument of another size. + if kind and kind.group(1) == "global_buffer" and size in (None, 8): + size = "ptr" + sizes.append(size) unnamed = any(not n for n in names) out.append(dict(func=fn.group(1), line=entries[0][1], names=None if unnamed else names, sizes=sizes, dynamic=False, unnamed=unnamed)) # fmt: skip @@ -1287,7 +1420,8 @@ def load_surfaces(tree: Tree) -> list: own = isinstance(source, str) and isinstance(builder, str) for entry in manifest["launch_surface"]: locator = entry.get("python_source") if isinstance(entry, dict) else None - if not isinstance(locator, str) or ":" not in locator: + # A locator is a path, optionally `:symbol` or prose after it. + if not isinstance(locator, str) or not locator.split(":", 1)[0].strip(): continue out.append( Surface( @@ -1465,7 +1599,7 @@ def reach(self, start: str) -> set: def build_closure(tree: Tree) -> Closure: known = {p for p in tree.paths if p.endswith(".py") and p.startswith(IMPORT_ROOTS)} out = Closure() - roots = [] # (label, target, arch, production) + roots = [] # (label, target, archs (empty: every arch), production) tree.read(sorted(p for p in known if p.endswith("__init__.py"))) memo = {} @@ -1495,28 +1629,46 @@ def reexport(init: str, name: str): descriptors = sorted( p for p in tree.paths if p.startswith(DESC_DIR) and p.endswith(".json") ) + generics = generic_map(tree) for path, data in tree.read(descriptors).items(): text = decode(data) - if not text or "kernel_source" not in text: + if not text or ("kernel_source" not in text and '"sources"' not in text): continue try: doc = json.loads(text) except ValueError: continue - seen = set() - for node in walk_json(doc): - source = node.get("kernel_source") - if isinstance(source, dict) and isinstance(source.get("source"), str): - target = resolve_path_literal(source["source"], known) - if target and source["source"] not in seen: - seen.add(source["source"]) - match = re.search( - r'"source"\s*:\s*"' + re.escape(source["source"]) + '"', text - ) - line = text.count("\n", 0, match.start()) + 1 if match else 0 - roots.append( - (f"{path}:{line}", target, arch_of(path, target), True) - ) + if not isinstance(doc, dict): + continue + ukds = doc.get("kernelDescriptors") + ukds = ( + [u for u in ukds if isinstance(u, dict)] + if isinstance(ukds, list) + else [doc] + ) + found = {} # source literal -> archs of every UKD that names it + for ukd in ukds: + archs = descriptor_archs(doc, {} if ukd is doc else ukd, generics) + for node in walk_json(ukd): + # A single launch is `kernel_source`; a multi-launch UKD keys each launch's + # kernel_source by name under `sources`. + launches = [node.get("kernel_source")] + if isinstance(node.get("sources"), dict): + launches += node["sources"].values() + for source in launches: + if isinstance(source, dict) and isinstance( + source.get("source"), str + ): + found.setdefault(source["source"], set()).update(archs) + for literal, archs in found.items(): + target = resolve_path_literal(literal, known) + if not target: + continue + match = re.search(r'"source"\s*:\s*"' + re.escape(literal) + '"', text) + line = text.count("\n", 0, match.start()) + 1 if match else 0 + if not archs: + archs = expand_archs(arch_names(path, target), generics) + roots.append((f"{path}:{line}", target, archs, True)) for path in sorted( p for p in tree.paths if p.startswith(DESC_DIR) and p.endswith(".dvc") ): @@ -1529,7 +1681,7 @@ def reexport(init: str, name: str): and arch in mod.split("/") and not mod.endswith("__init__.py") ): - roots.append((path, mod, arch, True)) + roots.append((path, mod, expand_archs({arch}, generics), True)) packages = sorted( { p[len(r) :].split("/")[0].removesuffix(".py") @@ -1554,7 +1706,7 @@ def reexport(init: str, name: str): path, tree.ast(path), known, reexport=reexport ): roots.append( - (f"{path}:{line}", target, None, path.startswith(PRODUCTION_DIRS)) + (f"{path}:{line}", target, set(), path.startswith(PRODUCTION_DIRS)) ) graph, body = out.graph, {} frontier = sorted({t for _, t, _, _ in roots}) @@ -1588,7 +1740,7 @@ def reexport(init: str, name: str): ), ) ) - for _label, target, arch, production in roots: + for _label, target, root_archs, production in roots: if not production: continue seen, stack = set(), [target] @@ -1600,7 +1752,7 @@ def reexport(init: str, name: str): archs = out.shipped.setdefault(current, set()) # A root without an arch is the packer or one of its tools: it compiles every # packed variant, so what it reaches ships on every architecture. - archs.add(arch or ALL_ARCHS) + archs.update(root_archs or {ALL_ARCHS}) stack += body.get(current, []) return out @@ -1664,6 +1816,22 @@ def moved(self, path: str) -> bool: ch = self.changes.get(path) return ch is not None and ch.cls in MOVING + def archs_named(self, *texts): + """The archs the gfx names in `texts` stand for, or None (every arch) when they name + none. A name is widened to every arch a KDP named for it ships on, either side (the + `gfx1151_wmma_attention` KDP ships gfx1100 to gfx1201), generic targets expanded. + """ + names = arch_names(*texts) + if not names: + return None + out = set() + for tree in (self.before, self.after): + for path, archs in kdp_archs(tree).items(): + if names & arch_names(path): + out |= archs + out |= expand_archs(names, tree.generics) + return ", ".join(sorted(out)) + def engine_unshipped(self): """Why the rocKE C++ engine reaches no shipped kernel on both sides, or None.""" if self._engine is None: @@ -1753,7 +1921,7 @@ def rocke_status(ctx: Ctx, ch: Change): return ( "changed", "rocKE file that is not an importable Python module: not understood", - arch_of(path), + ctx.archs_named(path), False, ) # The emitter compares IR text; code after lowering never folds on its evidence. @@ -1773,7 +1941,7 @@ def rocke_status(ctx: Ctx, ch: Change): "module body is reachable from a shipped variant; emitted code or params may differ" + (f" ({arch})" if archs else "") + step, - arch or arch_of(path), + arch, fold, ) why = after.consumed.get(path) or before.consumed.get(path) @@ -1783,7 +1951,7 @@ def rocke_status(ctx: Ctx, ch: Change): return ( "changed", f"imported module added, removed or unparsable ({why}){step}", - arch_of(path), + ctx.archs_named(path), fold, ) diff = surface_changes(bt, at) @@ -1791,7 +1959,7 @@ def rocke_status(ctx: Ctx, ch: Change): return ( "changed", f"imported by {why}; module surface changed: {'; '.join(diff[:3])}{step}", - arch_of(path), + ctx.archs_named(path), fold, ) return ( @@ -1823,16 +1991,15 @@ def file_row(ctx: Ctx, ch: Change): return make_row(kind, path, 0, path, "outside", "documentation") if cls in ("comment-only", "whitespace-only", "data-equal"): return make_row(kind, path, 0, path, "inert", cls) - arch = arch_of(path) if tag == "descriptor" or path == GENERIC_TARGETS_JSON or cls == "dvc-pointer": return None # data rows carry these files + arch = ctx.archs_named(path) if cls == "generated": - sources = generated_rule(path) other = next( ( p - for p in ctx.changes - if p != path and p.startswith(sources) and ctx.changes[p].cls in MOVING + for p in sorted(ctx.changes) + if p != path and ctx.changes[p].cls in MOVING and regenerates(p, path) ), None, ) @@ -1877,10 +2044,11 @@ def file_row(ctx: Ctx, ch: Change): arch=arch, foldable=fold, ) - arch = arch_of(path, " ".join(ch.funcs)) + arch = ctx.archs_named(path, " ".join(ch.funcs)) reason = { "native-pack": "native pack code changed", "sdk-ingestor": "ingestor SDK header changed", + "sdk-loader": "SDK header the descriptor loader includes changed", "generic-targets": "generic-target header changed", "kpack-runtime": "kpack runtime changed", "packer": "packer code changed", @@ -1957,7 +2125,7 @@ def kernarg_rows(ctx: Ctx) -> list: new_lists = param_lists(at) if at is not None else {} new_funcs = function_nodes(at) if at is not None else {} old_funcs = function_nodes(bt) - arch = arch_of(ch.path) + arch = ctx.archs_named(ch.path) for qual in sorted(old_lists): old, new = old_lists[qual], new_lists.get(qual) if new is None: @@ -2018,7 +2186,7 @@ def kernarg_rows(ctx: Ctx) -> list: rows.append(make_row("kernargs", w.change.path, w.line, w.qual, "mismatch" if stale else "changed", f"b.param list of {w.qual} changed ({change}); {where}", - found, arch=arch_of(w.change.path), foldable=not stale)) # fmt: skip + found, arch=ctx.archs_named(w.change.path), foldable=not stale)) # fmt: skip rows += declared_kernarg_rows(ctx, seen_lists) return rows @@ -2046,18 +2214,24 @@ def rank(cand): return sorted(found, key=rank) -def pair_builder(names: list, cands: list): +def pair_builder(names: list, cands: list, declared=None): """(candidate, matches) for one static list: the best-ranked candidate whose params it matches; else the related candidate sharing most names, which it disagrees with; else - (None, False).""" + (None, False). The `declared` builder, when the list is related to it, wins over any + candidate that shares no more names: a list cut short of the declared builder's own params + must not pass as a smaller builder's (a five-entry cut of the dense signature matches + `_build_attention_dense_persistent`).""" def shared(cand): return len(set(names) & set(cand[2].names)) - for pool, matches in ( - ([c for c in cands if compatible(names, c[2])], True), - ([c for c in cands if related(names, c[2].names)], False), + fits = [c for c in cands if compatible(names, c[2])] + near = [c for c in cands if related(names, c[2].names)] + if any(c is declared for c in near) and shared(declared) >= shared( + max(fits or near, key=shared) ): + return declared, compatible(names, declared[2]) + for pool, matches in ((fits, True), (near, False)): if pool: return max(pool, key=shared), matches # max keeps the first of equals return None, False @@ -2092,9 +2266,18 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: arch=surface.arch)) # fmt: skip continue cands = builder_candidates(ctx, surface) + own = next( + ( + c + for c in cands + if c[0] == surface.module + and c[1].rsplit(".", 1)[-1] == surface.function + ), + None, + ) fallback = [cp(surface.module, 0, ctx.moved(surface.module))] compared, closest = False, 0 - arch = surface.arch or arch_of(surface.cpp_mirror) + arch = ctx.archs_named(surface.arch) or ctx.archs_named(surface.cpp_mirror) lists = cpp_kernel_lists(text) for entry in lists: key = (surface.cpp_mirror, entry["line"]) @@ -2109,7 +2292,7 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: "changed", f"{entry['func']}() is a {kind} kernarg signature: it cannot be " f"compared with {declared} statically", fallback, arch=arch)) # fmt: skip continue - cand, matches = pair_builder(entry["names"], cands) + cand, matches = pair_builder(entry["names"], cands, own) if cand is None: for c in cands: closest = max(closest, len(set(entry["names"]) & set(c[2].names))) @@ -2144,14 +2327,14 @@ def declared_kernarg_rows(ctx: Ctx, reported: set) -> list: if differ: rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", "mismatch", f"static list {entry['func']}() names agree with builder {paired} " - f"but types do not: {'; '.join(differ[:4])}", counter, arch=arch)) # fmt: skip + f"but sizes do not: {'; '.join(differ[:4])}", counter, arch=arch)) # fmt: skip elif moved: if not unknown: - note = "names and types agree" + note = "names and sizes agree" elif len(unknown) == len(entry["names"]): - note = "names agree; types not compared" + note = "names agree; sizes not compared" else: - note = f"names agree; types not compared for {', '.join(unknown)}" + note = f"names agree; sizes not compared for {', '.join(unknown)}" rows.append(make_row("kernargs", surface.cpp_mirror, entry["line"], f"declared:{entry['func']}", "changed", f"{entry['func']}() vs {paired}: {note}", counter, arch=arch)) # fmt: skip if not compared and moved: @@ -2544,7 +2727,7 @@ def symbol_rows(ctx: Ctx) -> list: continue if provided: direct.append(make_row("symbols", ch.path, entry[1], qual, "changed", - f"override removed; base {provided} not resolved", arch=arch_of(ch.path))) # fmt: skip + f"override removed; base {provided} not resolved", arch=ctx.archs_named(ch.path))) # fmt: skip continue cands.append((ch, qual, entry, "removed", None)) elif new[qual][4] != entry[4] and entry[0] in ("def", "field"): @@ -2558,6 +2741,21 @@ def symbol_rows(ctx: Ctx) -> list: return rows +def binds_elsewhere(path: str, tree, defining: str, name: str) -> bool: + """Whether `path` binds `name` only from somewhere other than `defining`: an import from + another module, or its own def or class. Its reads of `name` are then not of `defining`'s. + """ + if path == defining: + return False + imports = [(stem, imp) for b, stem, imp, _ in iter_imports(path, tree) if b == name] + if any(imp is not None and in_module(defining, stem) for stem, imp in imports): + return False + return bool(imports) or any( + isinstance(n, (*DEFINITION_FUNCS, ast.ClassDef)) and n.name == name + for n in ast.walk(tree) + ) + + def reader_rows(ctx: Ctx, cands: list) -> list: names = sorted({q.split(".")[-1] for _, q, e, *_ in cands if e[0] != "module"}) stems = sorted( @@ -2602,6 +2800,12 @@ def reader_rows(ctx: Ctx, cands: list) -> list: or in_module(ch.path, stem if imp is None else f"{stem}/{imp}") ] else: + if is_method and binds_elsewhere( + path, tree, ch.path, qual.split(".")[0] + ): + # The file's class of that name comes from another module (a moved class + # keeps its name), so its `.name` reads are not reads of this one. + continue if is_method and not re.search( r"\b" + re.escape(qual.split(".")[0]) + r"\b", text ): @@ -2625,7 +2829,7 @@ def reader_rows(ctx: Ctx, cands: list) -> list: refs.append(cp(path, line, other is not None and line in other.added)) if node is not None: calls.append((path, tree, node)) - arch = arch_of(ch.path) + arch = ctx.archs_named(ch.path) if not refs: # Only a method of a class that survives (a removed class has its own row; a # field's `.name` is too common a word to stand for a reader), read in a file @@ -2746,13 +2950,68 @@ def call_arity_rows(ctx: Ctx) -> list: rows.append(make_row("symbols", ch.path, call.lineno, f"call:{name}", "mismatch", f"call {name}(...) omits required parameter(s) {', '.join(missing)}; sole def at {path}:{line}", [cp(path, line, other is not None and line in other.added)], - arch=arch_of(ch.path, path))) # fmt: skip + arch=ctx.archs_named(ch.path, path))) # fmt: skip return rows # -- surfaces --------------------------------------------------------------------------------------- +def reached_dumps(tree: Tree, path: str, nodes) -> list: + """Sorted dumps of `nodes` (defs of `path`) and of every top-level def, class and assignment + they reach by name, followed through `from` imports into other repo modules. A host-side + surface function (a grid or block helper) can hand its work to such code + (`NONPERSIST_DECODES[...].grid`), and no emitted IR covers any of it.""" + index, tops, seen, out = module_index(tree), {}, set(), [] + + def bindings(where): + """Name -> [node] of a module's top-level defs, classes and assignments, and name -> + (module, name) of its `from` imports.""" + if where not in tops: + module, nodes, imports = tree.ast(where), {}, {} + for node in module.body if module is not None else (): + if isinstance(node, (*DEFINITION_FUNCS, ast.ClassDef)): + nodes.setdefault(node.name, []).append(node) + elif isinstance(node, (ast.Assign, ast.AnnAssign)): + targets = ( + node.targets if isinstance(node, ast.Assign) else [node.target] + ) + for target in targets: + for name in ast.walk(target): + if isinstance(name, ast.Name): + nodes.setdefault(name.id, []).append(node) + for bound, stem, imported, _ in ( + iter_imports(where, module) if module is not None else () + ): + found = index.get(stem) + if imported is not None and found: + imports[bound] = (found, imported) + tops[where] = nodes, imports + return tops[where] + + work = [(path, node) for node in nodes] + while work: + where, node = work.pop() + if (where, id(node)) in seen: + continue + seen.add((where, id(node))) + # A copy: the cached tree of another module stays as the other checks read it. + out.append(f"{where}: {ast.dump(without_docstrings(copy.deepcopy(node)))}") + names = {n.id for n in ast.walk(node) if isinstance(n, ast.Name)} + pending = [(where, name) for name in sorted(names)] + resolved = set() + while pending: + module, name = pending.pop() + if (module, name) in resolved: + continue + resolved.add((module, name)) + local, imports = bindings(module) + work += [(module, n) for n in local.get(name, ())] + if name in imports: + pending.append(imports[name]) + return sorted(out) + + def surfaces_rows(ctx: Ctx) -> list: rows, seen = [], set() for surface in ctx.surfaces("before") + ctx.surfaces("after"): @@ -2767,6 +3026,12 @@ def surfaces_rows(ctx: Ctx) -> list: continue seen.add(key) dumps, line = {}, 0 + # A builder the packer compiles is covered by the emitter; any other host-side function + # is compared together with the code it reaches. + built = surface.name == "kernargs" or ( + surface.module == surface.builder_module + and surface.function == surface.builder + ) for side in ("before", "after"): tree = ctx.tree_of(side).ast(surface.module) if surface.function is None: @@ -2776,9 +3041,12 @@ def surfaces_rows(ctx: Ctx) -> list: nodes = defs_named(tree, surface.function) if nodes and side == "after": line = nodes[0].lineno - dumps[side] = ( - [ast.dump(without_docstrings(n)) for n in nodes] if nodes else None - ) + if not nodes: + dumps[side] = None + elif built: + dumps[side] = [ast.dump(without_docstrings(n)) for n in nodes] + else: + dumps[side] = reached_dumps(ctx.tree_of(side), surface.module, nodes) dump = dumps["after"] if surface.function is None: py_changed = ctx.moved(surface.module) @@ -2790,7 +3058,9 @@ def surfaces_rows(ctx: Ctx) -> list: counter = [cp(surface.cpp_mirror, 0, cpp_changed)] if surface.cpp_mirror else [] if surface.test: counter.append(cp(surface.test, 0, test_changed)) - arch = surface.arch or arch_of(surface.module, surface.cpp_mirror) + arch = ctx.archs_named(surface.arch) or ctx.archs_named( + surface.module, surface.cpp_mirror + ) name = ( f"{surface.name}:{surface.function or PurePosixPath(surface.module).name}" ) @@ -2930,7 +3200,7 @@ def data_rows(ctx: Ctx) -> list: ) rows.append(make_row("data", path, 0, path, "changed", f"catalog content changed, not inspected ({shown}): the DVC-held file is not read here, " - "and a regenerated variant list does not prove its other fields equal", arch=arch_of(path))) # fmt: skip + "and a regenerated variant list does not prove its other fields equal", arch=ctx.archs_named(path))) # fmt: skip elif ( ch.tag == "descriptor" and ch.cls not in ("docs",) @@ -2941,7 +3211,7 @@ def data_rows(ctx: Ctx) -> list: else: rows.append(make_row("data", path, 0, path, "changed", "descriptor content changed: " + descriptor_delta(decode(ch.base) or "", decode(ch.head) or ""), - arch=arch_of(path))) # fmt: skip + arch=ctx.archs_named(path))) # fmt: skip elif ch.tag == "descriptor" and ch.cls not in ("docs",): rows.append( make_row( @@ -2951,7 +3221,7 @@ def data_rows(ctx: Ctx) -> list: path, "changed", "descriptor-tree file changed: not understood", - arch=arch_of(path), + arch=ctx.archs_named(path), ) ) return rows @@ -2991,8 +3261,9 @@ def merge_views(per_view: list) -> list: EMIT_SIZES = {"i8": 1, "i16": 2, "i32": 4, "float": 4, "half": 2, "i64": 8, "double": 8} #: The impact_emit.py output this script reads; 3 added `comgr_changed`, 4 `companions` (the -#: kernarg list of each companion launch of a KernelModule builder), 5 `predicate_tables`. -EMIT_SCHEMA = 5 +#: kernarg list of each companion launch of a KernelModule builder), 5 `predicate_tables`, 6 the +#: launches of multi-launch (`sources`) UKDs. +EMIT_SCHEMA = 6 #: The text fields of one predicate table. TABLE_TEXT = ("arch", "source", "builder", "inner", "predicate", "skipped") @@ -3151,8 +3422,11 @@ def emit_source(ctx: Ctx, source: str) -> str: return source -def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: - """Apply impact_emit output to the base..head view's rows (see the module docstring).""" +def fold_emit(ctx: Ctx, rows: list, emit: dict, merged=None) -> list: + """Apply impact_emit output to the base..head view's rows (see the module docstring). + `merged`, (merged view Ctx, its rows), has the same fold applied in place to the rows of + modules whose merged blob is the head's: the emitter proved that content, nothing else. + """ families = emit["families"] out = [] surfaces = ctx.surfaces("after") @@ -3262,6 +3536,10 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: out.append(make_row("kernargs", item.get("kdp", ""), 0, f"emit-skip:{item.get('kdp')}:{item.get('side')}", "changed", f"not inspected ({item.get('side')}): {item.get('reason')}")) # fmt: skip if equal: + fold = dict( + status="inert", + reason="emitted IR, params and COMGR inputs equal for every shipped variant", + ) for row in rows: if ( row["view"] == ctx.label @@ -3269,10 +3547,19 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict) -> list: and row["foldable"] and row["path"] in covered ): - row.update( - status="inert", - reason="emitted IR, params and COMGR inputs equal for every shipped variant", - ) + row.update(fold) + mctx, mrows = merged or (None, []) + for row in mrows: + head, there = ctx.changes.get(row["path"]), mctx.changes.get(row["path"]) + if ( + row["status"] == "changed" + and row["foldable"] + and row["path"] in covered + and head is not None + and there is not None + and there.blob_head == head.blob_head + ): + row.update(fold) for row in out: row["view"] = ctx.label return rows + out @@ -3492,7 +3779,7 @@ def run(args): if not ctx.changes: raise Fail("empty diff") views[0]["files"] = len(ctx.changes) - per_view = [rows] + per_view, merged = [rows], None if tip_pair: tip, tip_tree_id = tip_pair tree_id, conflicts, why = merged_tree(repo, tip, head) @@ -3534,8 +3821,9 @@ def run(args): "so a race on them cannot be excluded"), view="merged", merged_only=True) ) # fmt: skip per_view.append(mrows) + merged = mctx, mrows if emit is not None: - per_view[0] = fold_emit(ctx, per_view[0], emit) + per_view[0] = fold_emit(ctx, per_view[0], emit, merged) tables = emit["predicate_tables"] if emit is not None else None return ctx, views, merge_views(per_view), tables diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index b1ca79058b14..412de3ce178b 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -17,7 +17,12 @@ EXECUTES rocKE code from the reviewed revisions, like building the head does. Variants come from every `*.kdp.json` under the shipped descriptor root whose -`kernelDescriptors[].kernel_source` names a rocKE `source` + `builder`. A KDP held by +`kernelDescriptors[].kernel_source` names a rocKE `source` + `builder`. A UKD with +`sources` (several launches) yields one variant per distinct (source, builder, spec, +target) of its launches, as hkp_pack compiles them: the UKD `name` when there is one, else +`@[+...]` with the group's launch names sorted. A `sources` UKD +or entry that cannot be read (not rocke, no source or builder, both `kernel_source` and +`sources`) is listed under `not_inspected`. A KDP held by DVC (`artifacts.dvc`, no `.kdp.json`) is regenerated with that side's `IngestorGenerator/tools/registry_enumerator.py`; when neither works the KDP is listed under `not_inspected`, never treated as equal. A variant is its descriptor `name`; a @@ -308,6 +313,71 @@ def _arch_list(value): return [a for a in value or [] if isinstance(a, str)] +def _spec_json(spec): + return json.dumps(spec, sort_keys=True, default=str) + + +def _ukd_variants(d, arches, kdp, origin): + """(variants, reason) of one descriptor dict, as hkp_pack enumerates it. A single + `kernel_source` is one variant named by the descriptor (none when it lacks source and + builder, e.g. a hip kernel). A `sources` UKD groups its launches by (source, builder, + spec, target); each group is one variant, named by the descriptor when there is one group, + else `@[+...]` with the launch names sorted. REASON, set when the + descriptor cannot be read, is for `not_inspected` and the variants are then empty. + """ + has_ks, has_sources = "kernel_source" in d, "sources" in d + name = d.get("name") or d.get("id") or "?" + if has_ks and has_sources: + return [], f"{name}: carries both `kernel_source` and `sources`" + if has_sources: + entries = d["sources"] + if not isinstance(entries, dict) or not entries: + return [], f"{name}: `sources` is not a non-empty object" + groups = {} + for launch, ks in entries.items(): + if not isinstance(ks, dict): + return [], f"{name}: sources entry `{launch}` is not an object" + if ks.get("kind") != "rocke": + return ( + [], + f"{name}: sources entry `{launch}` has kind {ks.get('kind')!r}, not rocke", + ) + if not (ks.get("source") and ks.get("builder")): + return ( + [], + f"{name}: sources entry `{launch}` lacks `source` or `builder`", + ) + key = ( + ks["source"], + ks["builder"], + _spec_json(ks.get("spec") or {}), + ks.get("target", "native"), + ) + groups.setdefault(key, (ks, []))[1].append(launch) + out = [] + for ks, launches in groups.values(): + vname = name if len(groups) == 1 else f"{name}@{'+'.join(sorted(launches))}" + out.append(_variant(vname, arches, ks, kdp, origin)) + return out, None + ks = d.get("kernel_source") or {} + if not (ks.get("source") and ks.get("builder")): + return [], None + return [_variant(d["name"], arches, ks, kdp, origin)], None + + +def _variant(name, arches, ks, kdp, origin): + return dict( + name=name, + arches=arches, + source=ks["source"], + builder=ks["builder"], + spec=ks.get("spec") or {}, + target=ks.get("target", "native"), + kdp=kdp, + origin=origin, + ) + + def _discover(root): """(variants, not_inspected) of one extract: variants are dicts with name, arch, source, builder, spec, target, kdp, origin.""" @@ -329,30 +399,19 @@ def _discover(root): skipped.append({"kdp": rel, "reason": f"unreadable KDP: {e}"}) continue for d in doc.get("kernelDescriptors") or []: - ks = d.get("kernel_source") or {} - if not (ks.get("source") and ks.get("builder")): - continue # A descriptor's own `arch` narrows the KDP-level one. arches = _arch_list(d.get("arch")) or _arch_list(doc.get("arch")) - if not arches: + vs, reason = _ukd_variants(d, arches, rel, "kdp-in-git") + if reason: + skipped.append({"kdp": rel, "reason": reason}) + elif vs and not arches: skipped.append({"kdp": rel, "reason": f"{d['name']}: no `arch`"}) - continue - variants.append( - dict( - name=d["name"], - arches=arches, - source=ks["source"], - builder=ks["builder"], - spec=ks.get("spec") or {}, - target=ks.get("target", "native"), - kdp=rel, - origin="kdp-in-git", - ) - ) + else: + variants.extend(vs) if "artifacts.dvc" in filenames and not kdps: dvc_dirs.append(here) for d in dvc_dirs: - got, why = _regenerate(base, d) + got, why = _regenerate(base, d, skipped) if why: kdp = (d / f"{d.name}.kdp.json").relative_to(base).as_posix() skipped.append({"kdp": kdp, "reason": why}) @@ -361,8 +420,10 @@ def _discover(root): return variants, skipped -def _regenerate(base, dvc_dir): - """Variants of a DVC-held KDP from the registry enumerator, or (None, reason).""" +def _regenerate(base, dvc_dir, unreadable): + """Variants of a DVC-held KDP from the registry enumerator, or (None, reason). A kernel + the enumerator emitted that cannot be read is appended to UNREADABLE (not_inspected). + """ rel_dir = dvc_dir.relative_to(base / DESCRIPTORS).as_posix() kdp = (dvc_dir / f"{dvc_dir.name}.kdp.json").relative_to(base).as_posix() enum = base / ENUMERATOR @@ -397,6 +458,7 @@ def _regenerate(base, dvc_dir): [sys.executable, str(enum), "--manifest", str(manifest), "--out", str(out)], int(os.environ.get("IMPACT_EMIT_ENUM_TIMEOUT", "600")), cwd=str(base), + own_group=False, ) except subprocess.TimeoutExpired: return None, "registry_enumerator.py timed out" @@ -410,28 +472,18 @@ def _regenerate(base, dvc_dir): pack = yaml.safe_load(out.read_text(encoding="utf-8"))["packs"][0] except (OSError, KeyError, IndexError, TypeError, yaml.YAMLError) as e: return None, f"registry_enumerator.py output unusable: {type(e).__name__}: {e}" - got = [] + got, bad = [], [] for k in pack.get("kernels") or []: - ks = k.get("kernel_source") or {} - if not (ks.get("source") and ks.get("builder")): - continue arches = _arch_list(k.get("arch")) or _arch_list(pack.get("arch")) - if not arches: + vs, reason = _ukd_variants(k, arches, kdp, "registry_enumerator") + if reason: + bad.append({"kdp": kdp, "reason": reason}) + elif vs and not arches: return None, f"registry_enumerator.py output has no arch for {k['name']}" - got.append( - dict( - name=k["name"], - arches=arches, - source=ks["source"], - builder=ks["builder"], - spec=ks.get("spec") or {}, - target=ks.get("target", "native"), - kdp=kdp, - origin="registry_enumerator", - ) - ) - if not got: + got.extend(vs) + if not got and not bad: return None, "registry_enumerator.py produced no rocKE kernels" + unreadable.extend(bad) return got, None @@ -927,7 +979,7 @@ def child_env(root, flavor): return env -def _kill_tree(proc): +def _kill_tree(proc, own_group=True): if os.name == "nt": try: subprocess.run( @@ -935,7 +987,7 @@ def _kill_tree(proc): ) except OSError: pass - else: + elif own_group: try: os.killpg(proc.pid, signal.SIGKILL) except OSError: @@ -946,15 +998,20 @@ def _kill_tree(proc): pass -def run_tree(cmd, timeout, **kw): +def run_tree(cmd, timeout, *, own_group=True, **kw): """subprocess.run with captured text output that, on timeout, kills the child's whole process tree and raises TimeoutExpired without waiting for pipes a grandchild holds. + OWN_GROUP (default) makes the child lead a new process group (POSIX session) that the + kill takes as a whole; with False the child stays in the caller's group, so a caller + that is itself killed as a group takes the child with it, and only the child is killed + here (plus its tree on Windows). """ if os.name == "nt": - kw["creationflags"] = kw.get("creationflags", 0) | ( - subprocess.CREATE_NEW_PROCESS_GROUP - ) - else: + if own_group: + kw["creationflags"] = kw.get("creationflags", 0) | ( + subprocess.CREATE_NEW_PROCESS_GROUP + ) + elif own_group: kw["start_new_session"] = True proc = subprocess.Popen( cmd, @@ -968,7 +1025,7 @@ def run_tree(cmd, timeout, **kw): try: out, err = proc.communicate(timeout=timeout) except subprocess.TimeoutExpired: - _kill_tree(proc) + _kill_tree(proc, own_group) try: proc.wait(timeout=10) proc.communicate(timeout=5) @@ -976,7 +1033,7 @@ def run_tree(cmd, timeout, **kw): pass # give up on pipes some survivor still holds raise except BaseException: - _kill_tree(proc) + _kill_tree(proc, own_group) proc.wait() raise return subprocess.CompletedProcess(cmd, proc.returncode, out, err) @@ -1132,7 +1189,7 @@ def cmd_run(a): ] seconds = round(time.time() - t0, 1) doc = { - "schema": 5, + "schema": 6, "tool": "impact_emit", "flavor": flavor, "base": a.base, diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index e61584a878c2..bea5601b5098 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -517,7 +517,7 @@ def family(**fields) -> dict: def write_emit(path, repo, base, head, families=None, tables=(), **fields): """An impact_emit document made for base..head, holding `families` (default: one family built from `fields`) and the predicate `tables`.""" - doc = {"schema": 5, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, + doc = {"schema": 6, "tool": "impact_emit", "flavor": "llvm23", "base": base, "head": head, "base_tree": tree_of(repo, base), "head_tree": tree_of(repo, head), "families": [family(**fields)] if families is None else families, "predicate_tables": list(tables), "not_inspected": [], "seconds": 1.0} # fmt: skip @@ -991,7 +991,7 @@ def test_launch_surface_naming_a_method_resolves_like_launch_surface_check( ({SIG_FACTORY: FACTORY.replace("def attn_signature", "def attn_sig")}, "mismatch", "function attn_signature"), ({BUILDER: BODY_EDIT}, "changed", "the manifest declares attn_signature"), - ({BUILDER: BUILD.replace('"scale", F32', '"scale", F64')}, "mismatch", "types do not")], + ({BUILDER: BUILD.replace('"scale", F32', '"scale", F64')}, "mismatch", "sizes do not")], ids=["mirror-edited", "mirror-reordered", "factory-renamed", "source-builder-edited", "source-builder-type-changed"], ) # fmt: skip @@ -1175,6 +1175,7 @@ def test_conflict_only_in_a_test_still_marks_the_merged_view_changed( == "conflicted" ) assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE all-ingestor-archs" # a docs-only change conflicting in docs still has no surface file doc = "projects/hipdnn/docs/x.md" repo, base = make_repo(tmp_path / "b", {doc: "a\n"}) @@ -1197,6 +1198,7 @@ def test_unrelated_target_history_skips_the_merged_view_as_changed( next(v for v in data["views"] if v["label"] == "merged")["status"] == "skipped" ) assert any(r["view"] == "merged" and r["status"] == "changed" for r in data["rows"]) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE all-ingestor-archs" def test_non_ascii_path_is_reported_under_its_real_name(mod, capsys, tmp_path): @@ -1371,3 +1373,215 @@ def test_changed_dvc_pointer_is_not_folded_by_a_regenerated_catalog( (ptr, "changed") ] assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +DECODE = HKP + "rocke/library/kernels/gfx1151/decode.py" +DESCRIPTORS = HKP + "src/engines/kernel_ingestor_engine/descriptors/rocKE/" +DECODE_KDP = DESCRIPTORS + "gfx1151_decode/gfx1151_decode.kdp.json" +GFX11_TABLE = json.dumps( + {"schemaVersion": 1, "generics": {"gfx11-generic": ["gfx1100", "gfx1151"]}} +) + + +def test_multi_launch_ukd_roots_its_module_on_the_kdp_arch_list(mod, capsys, tmp_path): + # catches: `sources` launches skipped by the shipped closure, so a decode body edit reads + # "reached by no shipped variant"; or the device list read from the path (gfx1151 alone) + # instead of the KDP's arch array, generic targets expanded + launch = { + "kind": "rocke", + "source": "kernels/gfx1151/decode.py", + "builder": "build_decode", + } + kdp = {"arch": ["gfx11-generic", "gfx1200"], "kernelDescriptors": [ + {"name": "d", "sources": {"decode": launch, "merge": {**launch, "role": "merge"}}}]} # fmt: skip + body = "def build_decode(spec):\n return {}\n" + files = {DECODE: body.format(1), DECODE_KDP: json.dumps(kdp), GENERIC: GFX11_TABLE} + repo, base = make_repo(tmp_path, files) + head = commit(repo, {DECODE: body.format(2)}) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "file") + assert row["status"] == "changed" + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx1100, gfx1151, gfx1200" + + +def test_pack_named_for_an_arch_needs_every_arch_its_kdp_ships(mod, capsys, tmp_path): + # catches: a NEEDS-DEVICE list read from the first gfx name in the path, naming one arch of + # the several the KDP named for it ships (UKD-level arrays included) + pack = HKP + "src/engines/kernel_ingestor_engine/packs/Gfx1151Wmma.cpp" + kdp = {"arch": ["gfx1100", "gfx1151"], "kernelDescriptors": [ + {"name": "a", "arch": ["gfx1200", "gfx1201"], "kernel_source": {"source": "x.py"}}]} # fmt: skip + files = {pack: "int f() { return 1; }\n", DECODE_KDP: json.dumps(kdp)} + repo, base = make_repo(tmp_path, files) + head = commit(repo, {pack: "int f() { return 2; }\n"}) + data = report(mod, capsys, repo, base, head) + assert ( + data["rollup"] + == "Runtime impact: NEEDS-DEVICE gfx1100, gfx1151, gfx1200, gfx1201" + ) + + +def test_grid_surface_handing_off_to_another_module_is_not_folded_by_equal_ir( + mod, capsys, tmp_path +): + # catches: a host-side surface compared as its named def alone, so an edit of the class it + # hands off to (`NONPERSIST_DECODES[...].grid`) folds to NONE on equal IR + decode = HKP + "rocke/library/kernels/common/decode.py" + cls = 'class Qb:\n def grid(self, spec):\n return (1, 2, 3)\n\n\nDECODES = {"a": Qb()}\n' + builder = ("from kernels.common.decode import DECODES\n\n\ndef attn_grid(spec):\n" + " return DECODES[spec.kind].grid(spec)\n\n\n" + BUILD) # fmt: skip + grid = manifest(("grid", "kernels/gfx950/attn.py:attn_grid", GEOM)) + files = { + BUILDER: builder, + decode: cls, + KDP: KDP_JSON, + GEOM: "int grid();\n", + MANIFEST: grid, + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {decode: cls.replace("(1, 2, 3)", "(2, 1, 3)")}) + emit = write_emit(tmp_path / "eq.json", repo, base, head) + data = report(mod, capsys, repo, base, head, "--emit", emit) + (row,) = rows_of(data, "surfaces") + assert (row["name"], row["status"]) == ("grid:attn_grid", "changed") + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +SERIALIZATION = HKP + "src/engines/kernel_ingestor_engine/serialization/" +PLAN_H = SERIALIZATION + "ingestor_plan_generated.h" +PLAN_HEADER = "struct P { int VT_OFFSET = 4; int VT_NAME = 6; };\n" + + +@pytest.mark.parametrize( + "extra, status", + [({}, "changed"), + ({"projects/hipdnn/tools/IngestorGenerator/tests/helpers.py": "x = 2\n"}, "changed"), + ({SERIALIZATION + "ingestor_plan.fbs": "table P { b: int; }\n"}, "inert"), + ({SERIALIZATION + "flatc_flags.txt": "--scoped-enums\n"}, "inert")], + ids=["header-alone", "beside-a-generator-test", "beside-its-schema", "beside-its-flags"], +) # fmt: skip +def test_generated_header_is_regenerated_only_with_its_own_schema_or_flags( + mod, capsys, tmp_path, extra, status +): + # catches: a hand edit of a flatc header read as regenerated because any file under the + # generator or the flatbuffers SDK moved, a test included; a header changed alone read + # inert; or flatc_flags.txt read as documentation, so a flag change regenerates nothing + files = {PLAN_H: PLAN_HEADER, SERIALIZATION + "ingestor_plan.fbs": "table P { a: int; }\n", + SERIALIZATION + "flatc_flags.txt": "--gen-mutable\n", + "projects/hipdnn/tools/IngestorGenerator/tests/helpers.py": "x = 1\n"} # fmt: skip + repo, base = make_repo(tmp_path, files) + swapped = ( + PLAN_HEADER.replace("= 4", "= @").replace("= 6", "= 4").replace("= @", "= 6") + ) + head = commit(repo, {PLAN_H: swapped, **extra}) + data = report(mod, capsys, repo, base, head) + (row,) = [r for r in rows_of(data, "file") if r["path"] == PLAN_H] + assert row["status"] == status + + +SDK_INC = "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/" + + +def test_sdk_header_the_loader_includes_is_on_the_surface(mod, capsys, tmp_path): + # catches: SDK headers outside ingestor/ that the loader compiles in (ArchMatch.hpp) or + # the generic-target table's CMake template tagged `other`, so an edit reads N/A + loader = SDK_INC + "ingestor/DescriptorLoader.hpp" + arch_match, other = SDK_INC + "ArchMatch.hpp", SDK_INC + "Unrelated.hpp" + template = "projects/hipdnn/plugin_sdk/cmake/GpuGenericTargetsTable.hpp.in" + body = "inline bool f(int a) {{ return a > {}; }}\n" + files = {loader: "#include \n", arch_match: body.format(3), + other: body.format(3), template: "constexpr int k = 1;\n"} # fmt: skip + repo, base = make_repo(tmp_path, files) + head = commit(repo, {arch_match: body.format(4), other: body.format(4), + template: "constexpr int k = 2;\n"}) # fmt: skip + data = report(mod, capsys, repo, base, head) + status = {r["path"]: r["status"] for r in rows_of(data, "file")} + assert status == {arch_match: "changed", other: "outside", template: "changed"} + + +def test_member_read_of_a_moved_class_is_not_a_removal_reader(mod, capsys, tmp_path): + # catches: a member reader matched by its class's name alone, so a class moved to another + # module under the same name reads `Space.outer_knob is removed but still referenced` + old, new, user = HKP_PACK + "tuning.py", HKP_PACK + "waves.py", HKP_PACK + "user.py" + cls = "class Space:\n def outer_knob(self):\n return 1\n" + reader = "from hkp_pack.tuning import Space\n\n\ndef f():\n return Space().outer_knob()\n" + repo, base = make_repo(tmp_path, {old: cls, user: reader}) + moved = {old: "X = 1\n", new: cls} + head = commit( + repo, {**moved, user: reader.replace("hkp_pack.tuning", "hkp_pack.waves")} + ) + assert not rows_of(report(mod, capsys, repo, base, head), "symbols", "mismatch") + # a reader still importing the class from its old module does read the removed one + stale = commit(repo, moved, parent=base) + rows = rows_of(report(mod, capsys, repo, base, stale), "symbols", "mismatch") + assert "Space.outer_knob" in {r["name"] for r in rows} + + +def test_target_tip_keeps_the_emit_fold_for_modules_the_head_owns( + mod, capsys, tmp_path +): + # catches: the emit fold applied to base..head only, so the merged view re-adds the + # shipped module's row as changed and merged-only, and no run with a tip reaches NONE + repo, base = make_repo( + tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, "README.md": "a\n"} + ) + head = branch_commit(repo, base, {BUILDER: BODY_EDIT}, "feature") + tip = branch_commit(repo, base, {"README.md": "b\n"}, "target") + emit = write_emit(tmp_path / "eq.json", repo, base, head) + data = report(mod, capsys, repo, base, head, "--target-tip", tip, "--emit", emit) + assert data["rollup"] == "Runtime impact: NONE (1 files inspected)" + assert not [r for r in data["rows"] if r["merged_only"]] + # the target edits the module too: the merged content is not what the emitter built + extra = "def extra():\n return 1\n\n\n" + BUILD + tip = branch_commit(repo, base, {BUILDER: extra}, "target2") + data = report(mod, capsys, repo, base, head, "--target-tip", tip, "--emit", emit) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + + +def test_emit_params_reordered_against_the_static_list_is_mismatch( + mod, capsys, tmp_path +): + # catches: emitted params compared with the static C++ list as sorted names + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON, PACK: PACK_STATIC}) + head = commit(repo, {BUILDER: BODY_EDIT}) + swapped = [["k_ptr", "ptr"], ["q_ptr", "ptr"], ["scale", "float"]] + emit = write_emit(tmp_path / "p.json", repo, base, head, params_changed=["v"], + params={"base": None, "head": swapped}) # fmt: skip + (row,) = rows_of(report(mod, capsys, repo, base, head, "--emit", emit), "kernargs", "mismatch") # fmt: skip + assert row["path"] == PACK and "reordered" in row["reason"] + + +def test_global_buffer_declared_four_bytes_disagrees_with_a_pointer_param( + mod, capsys, tmp_path +): + # catches: every global_buffer read as a pointer whatever its declared size, so an I32 + # q_ptr reads as agreeing although requireSignatureMatch refuses it + files = {BUILDER: BUILD, PACK: PACK_STATIC, MANIFEST: MANIFEST_YAML} + repo, base = make_repo(tmp_path, files) + head = commit(repo, {PACK: PACK_STATIC.replace("PTR, 0,", "I32, 0,")}) + (row,) = rows_of(report(mod, capsys, repo, base, head), "kernargs", "mismatch") + assert "q_ptr: builder pointer, C++ 4-byte" in row["reason"] + + +def test_path_only_python_source_still_checks_the_declared_pair(mod, capsys, tmp_path): + # catches: a `python_source` with no `:symbol` dropped, though launch_surface.py --check + # accepts it, so a type-only builder change reads NEEDS-DEVICE instead of BREAKS + path_only = MANIFEST_YAML.replace("attn.py:build_attn", "attn.py") + repo, base = abi_repo(tmp_path, {PACK: PACK_STATIC, MANIFEST: path_only}) + head = commit(repo, {BUILDER: BUILD.replace('"scale", F32', '"scale", I64')}) + data = report(mod, capsys, repo, base, head) + (row,) = rows_of(data, "kernargs", "mismatch") + assert row["path"] == PACK and "scale" in row["reason"] + assert data["rollup"] == "Runtime impact: BREAKS" + + +def test_static_list_cut_short_of_the_declared_builder_is_mismatch( + mod, capsys, tmp_path +): + # catches: a cut-down C++ list paired with a smaller builder it happens to fit (a five-entry + # cut of the dense signature matches the persistent builder), so it reads changed + small = '\n\ndef build_small(b, spec):\n b.param("q_ptr", PTR)\n b.param("k_ptr", PTR)\n return b\n' + files = {BUILDER: BUILD + small, PACK: PACK_STATIC, MANIFEST: MANIFEST_YAML} + repo, base = make_repo(tmp_path, files) + head = commit(repo, {PACK: ptr_sig("attnSignature", ["q_ptr", "k_ptr"])}) + (row,) = rows_of(report(mod, capsys, repo, base, head), "kernargs", "mismatch") + assert "builder build_attn" in row["reason"] diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index c4908300b9ab..c55d3c81a85f 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -275,6 +275,11 @@ def scenarios(): s["dvc_enum"] = dvc_files() s["dvc_enum"][f"{GEN}/configs/fake.engine.yaml"] = "authored_subpath: rocKE/fake\n" s["dvc_enum"][f"{GEN}/tools/registry_enumerator.py"] = ENUMERATOR + s["dvc_enum_sources"] = dvc_files() + s["dvc_enum_sources"][ + f"{GEN}/configs/fake.engine.yaml" + ] = "authored_subpath: rocKE/fake\n" + s["dvc_enum_sources"][f"{GEN}/tools/registry_enumerator.py"] = ENUMERATOR_SOURCES narrowed = (KDP_VARIANTS[0], {**KDP_VARIANTS[1], "arch": ["gfx1100"]}) s["generic"] = tree_files( variants=narrowed, arch=("gfx1100", "gfx1101", "gfx1200"), target="generic" @@ -417,7 +422,7 @@ def test_extra_param_lists_every_variant_and_shows_head_list(emit, repo, tmp_pat def test_body_only_change_is_ir_changed_not_params(emit, repo, tmp_path): rc, doc = run(emit, repo, "base", "body", tmp_path) assert rc == 0 - assert doc["schema"] == 5 + assert doc["schema"] == 6 # the recorded ids are the real tree ids, and differ when the sides differ assert doc["base_tree"] == git_out(repo, "rev-parse", "base^{tree}") assert doc["head_tree"] == git_out(repo, "rev-parse", "body^{tree}") @@ -763,3 +768,222 @@ def test_timeout_kills_a_grandchild_holding_the_pipes_and_exits_one( assert rc == 1 and doc is None err = capsys.readouterr().err.strip().splitlines() assert len(err) == 1 and err[0].startswith("impact_emit: ") + + +def sources_kdp(ukds, arch=("gfx950",)): + return json.dumps({"arch": list(arch), "kernelDescriptors": ukds}) + + +KS = { + "kind": "rocke", + "source": "kernels/gfx_fake/fake_attn.py", + "builder": "build_fake", + "spec": {"n": 1}, +} + + +def discover(emit, tmp_path, ukds): + kdp = tmp_path / emit.DESCRIPTORS / "rocKE" / "x" / "x.kdp.json" + kdp.parent.mkdir(parents=True) + kdp.write_text(sources_kdp(ukds)) + return emit._discover(tmp_path) + + +def test_sources_ukd_sharing_source_builder_spec_is_one_variant_named_by_the_ukd( + emit, tmp_path +): + # catches: a multi-launch UKD skipped, or one variant per launch (a second build and a + # name that is not the UKD's) + ukd = {"name": "attn", "sources": {"decode": KS, "merge": {**KS, "role": "merge"}}} + variants, skipped = discover(emit, tmp_path, [ukd]) + assert skipped == [] + assert len(variants) == 1 + v = variants[0] + assert (v["name"], v["source"], v["builder"], v["spec"], v["target"]) == ( + "attn", + KS["source"], + "build_fake", + {"n": 1}, + "native", + ) + assert v["arches"] == ["gfx950"] and v["origin"] == "kdp-in-git" + + +def test_sources_ukd_launches_with_different_specs_are_variants_named_by_launch( + emit, tmp_path +): + # catches: launches differing in spec collapsed into one variant, or named alike + ukd = { + "name": "attn", + "sources": { + "merge": {**KS, "spec": {"n": 2}}, + "decode": KS, + "decode2": {**KS, "spec": {"n": 1}}, + }, + } + variants, skipped = discover(emit, tmp_path, [ukd]) + assert skipped == [] + assert {v["name"]: v["spec"] for v in variants} == { + "attn@decode+decode2": {"n": 1}, + "attn@merge": {"n": 2}, + } + + +def test_sources_spec_key_order_does_not_split_a_variant(emit, tmp_path): + # catches: a group key that compares spec dicts by identity or insertion order + ukd = { + "name": "attn", + "sources": { + "a": {**KS, "spec": {"n": 1, "m": 2}}, + "b": {**KS, "spec": {"m": 2, "n": 1}}, + }, + } + variants, _ = discover(emit, tmp_path, [ukd]) + assert [v["name"] for v in variants] == ["attn"] + + +@pytest.mark.parametrize( + "sources", + [ + {"a": {"kind": "rocke", "source": "s.py"}}, + {"a": {"kind": "rocke", "builder": "build_x"}}, + {"a": {**KS, "kind": "hip"}}, + {"a": "not-an-object"}, + {}, + ["a"], + ], + ids=[ + "no-builder", + "no-source", + "not-rocke", + "entry-not-object", + "empty", + "not-object", + ], +) +def test_unreadable_sources_ukd_is_not_inspected_and_yields_no_variant( + emit, tmp_path, sources +): + # catches: a `sources` UKD or entry the emitter cannot read dropped silently + good = {"name": "ok", "kernel_source": KS} + variants, skipped = discover( + emit, tmp_path, [{"name": "bad", "sources": sources}, good] + ) + assert [v["name"] for v in variants] == ["ok"] + assert len(skipped) == 1 + assert skipped[0]["kdp"].endswith("x/x.kdp.json") + assert skipped[0]["reason"].startswith("bad: ") + + +def test_ukd_with_both_kernel_source_and_sources_is_not_inspected(emit, tmp_path): + variants, skipped = discover( + emit, tmp_path, [{"name": "both", "kernel_source": KS, "sources": {"a": KS}}] + ) + assert variants == [] + assert [s["reason"].split(":")[0] for s in skipped] == ["both"] + + +def test_single_kernel_source_without_source_and_builder_is_still_skipped_silently( + emit, tmp_path +): + variants, skipped = discover( + emit, + tmp_path, + [{"name": "hip", "kernel_source": {"kind": "hip", "file": "k.hip"}}], + ) + assert variants == [] and skipped == [] + + +ENUMERATOR_SOURCES = textwrap.dedent( + """ + import argparse, json + p = argparse.ArgumentParser(); p.add_argument("--manifest"); p.add_argument("--out") + a = p.parse_args() + ks = {"kind": "rocke", "source": "kernels/gfx_fake/fake_attn.py", + "builder": "build_fake", "spec": {"n": 1}} + open(a.out, "w").write(json.dumps({"packs": [{"arch": ["gfx950"], "kernels": [ + {"name": "enum_m", "sources": {"decode": ks, "merge": {**ks, "role": "merge"}}}, + {"name": "enum_bad", "sources": {"x": {"kind": "rocke", "source": "s.py"}}}]}]})) + """ +) + + +def test_regenerated_sources_ukd_is_one_variant_and_an_unreadable_one_is_not_inspected( + emit, repo, tmp_path +): + # catches: the enumerator's `sources` kernels read differently from the git-tracked ones + _, doc = run(emit, repo, "dvc_enum_sources", "dvc_enum_sources", tmp_path) + f = only_family(doc) + assert f["variants"] == {"base": 1, "head": 1} + assert f["failures"] == {"base": {}, "head": {}} + assert sorted( + (n["side"], n["reason"].split(":")[0]) for n in doc["not_inspected"] + ) == [ + ("base", "enum_bad"), + ("head", "enum_bad"), + ] + + +def family(names, params_by_name, ir="ir"): + return { + "arch": "gfx950", + "source": "s.py", + "builder": "build_x", + "kdp": "k.kdp.json", + "spec_origin": "kdp-in-git", + "names": list(names), + "built": { + n: {"params": params_by_name[n], "ir": ir, "comgr": None} for n in names + }, + "failures": {}, + } + + +def test_compare_params_reordered_is_params_changed(emit): + # catches: params compared as a set or by sorted names, so a reorder (a different + # kernarg ABI) reads unchanged + qn = [["q", "ptr"], ["n", "i32"]] + nq = [["n", "i32"], ["q", "ptr"]] + (f,) = emit.compare( + {"families": [family(["v"], {"v": qn})]}, + {"families": [family(["v"], {"v": nq})]}, + ) + assert f["params_changed"] == ["v"] + assert f["ir_changed"] == [] + + +def test_compare_lists_variants_only_on_each_side(emit): + # catches: only_in_base and only_in_head swapped or merged, and a family present on + # one side only losing its variants + p = [["q", "ptr"]] + (f,) = emit.compare( + {"families": [family(["keep", "gone"], {"keep": p, "gone": p})]}, + {"families": [family(["keep", "new"], {"keep": p, "new": p})]}, + ) + assert f["only_in_base"] == ["gone"] + assert f["only_in_head"] == ["new"] + (g,) = emit.compare({"families": []}, {"families": [family(["new"], {"new": p})]}) + assert g["only_in_head"] == ["new"] and g["only_in_base"] == [] + assert g["variants"] == {"base": 0, "head": 1} + (g,) = emit.compare({"families": [family(["old"], {"old": p})]}, {"families": []}) + assert g["only_in_base"] == ["old"] and g["only_in_head"] == [] + + +@pytest.mark.skipif(os.name == "nt", reason="process groups are POSIX") +def test_run_tree_own_group_false_keeps_the_child_in_the_callers_group(emit): + # catches: an enumerator launched through run_tree landing in its own session, out of + # reach of the killpg that bounds the child + code = [emit.sys.executable, "-c", "import os; print(os.getpgrp())"] + inherited = emit.run_tree(code, 30, own_group=False) + own = emit.run_tree(code, 30) + assert int(inherited.stdout) == os.getpgrp() + assert int(own.stdout) != os.getpgrp() + + +@pytest.mark.skipif(os.name == "nt", reason="process groups are POSIX") +def test_run_tree_own_group_false_still_times_out_and_kills_the_child(emit): + code = [emit.sys.executable, "-c", "import time; time.sleep(60)"] + t0 = time.time() + with pytest.raises(subprocess.TimeoutExpired): + emit.run_tree(code, 1, own_group=False) + assert time.time() - t0 < 30 From 423c836e3e4e9a69e9785ead3382606261d26276 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Tue, 3 Nov 2026 14:18:40 -0600 Subject: [PATCH 09/13] fix(hipdnn): harden ingestor impact probes, parsing and lane wiring A support-predicate probe calls the real builder on specs the predicate accepts, and a builder that never returns on such a spec held the emitter child until its timeout, losing the argument and IR comparison of every shipped variant. The probes now run in a worker thread that a family's budget bounds: a family past it is abandoned, its tables and those after it read as timed out, and the document is still written. The emitter builds its output document in one function that the report's loader is tested against, so the two schema numbers cannot drift apart. A b.param call under a with block, a try body or an if True branch always runs, so it is no longer marked conditional, and a copy missing it is a mismatch again. A static KernelArgument list with a comment inside a literal's braces is read with the comment blanked rather than taken for an argument. A launch-surface module that stops parsing is reported as such instead of as a missing function, and an emit family without both base and head params or companions is refused rather than crashing the fold. The deep-review partition triggers the IMPACT lane on every plugin SDK header and on the generic-target CMake template, which the report now treats as surface. The lane's emitter block is self-contained, the rollup format and the severity of a descriptor value the loader rejects agree with the reference, and the reference names the probe cut, the probe timeout, the emitter's environment variables and the git version the merged view needs. ALMIOPEN-2834 --- .../lanes/runtime-impact.md | 5 +- .../hipdnn-deep-review/scripts/partition.py | 7 +- .../hipdnn-ingestor-impact/reference.md | 20 +- .../hipdnn-ingestor-impact/scripts/impact.py | 96 ++++--- .../scripts/impact_emit.py | 156 +++++++---- .../ai/tests/test_deep_review_partition.py | 34 ++- .../tools/ai/tests/test_ingestor_impact.py | 180 ++++++++++++- .../ai/tests/test_ingestor_impact_emit.py | 251 ++++++++++++++++++ .../tools/ai/tests/test_skills_tooling.py | 15 +- 9 files changed, 655 insertions(+), 109 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md index 0e46495d2200..df10e9fad2f8 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/lanes/runtime-impact.md @@ -33,6 +33,7 @@ searched as copies and readers, so a stale pin or a stale import is yours. 2. Run the emitter when the reference's *The emitter* says to: ```bash + . ./env.sh; IMPACT_SCRIPTS="$SKILL/../hipdnn-ingestor-impact/scripts" rm -f "$SCRATCH/IMPACT/emit.json" python3 "$IMPACT_SCRIPTS/impact_emit.py" run --repo "$CLONE" --base "$BASE" --head "$HEAD" --out "$SCRATCH/IMPACT/emit.json" python3 "$IMPACT_SCRIPTS/impact.py" report --repo "$CLONE" --base "$BASE" --head "$HEAD" --target-tip "$TARGET_TIP" --emit "$SCRATCH/IMPACT/emit.json" > lanes/IMPACT-evidence/report-emit.md @@ -49,7 +50,7 @@ searched as copies and readers, so a stale pin or a stale import is yours. 4. End **Checked, no finding** with the two lines the script printed last, each corrected by your judgment and saying why when it differs: the `Pack-time:` line (`NEW `, `FIRST-SHIPPED `, `CLEAN` or `NOT RUN`; see the reference's *Predicate tables*), then - `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`, + `Runtime impact: BREAKS | NEEDS-DEVICE | NONE ( files inspected) | N/A (no surface file)`, keeping any `not judged here:` note the script added. Follow them with your own `C++ tables: of disagree (...) | written, all agree | none written ()` line, so a qualifying row with no table is visible. @@ -60,7 +61,7 @@ searched as copies and readers, so a stale pin or a stale import is yours. |---|---| | A `mismatch` that holds | A `source-claim` at `BLOCKING` ([../policy.md](../policy.md): an ABI break without a migration path). **Where**: the producer line and every disagreeing copy. **Proof**: the command and both quoted lists. A `merged-only` mismatch: **Fix** is to rebase and update the named copy. The same mismatch at `$BASE` is pre-existing: `FUTURE WORK` at most | | A dispatch divergence | A `source-claim` at `BLOCKING`, quoting both sides | -| A descriptor or generic-target value the loader rejects or ignores | A finding, citing the loader rule | +| A descriptor or generic-target value the loader rejects or ignores | A `source-claim` at `BLOCKING`, citing the loader rule | | No break (`changed` but consistent), `inert`, `outside` | No finding. Count them under **Checked, no finding**; list each `inert` reason once | | A predicate-table finding in state `new` or `first shipped` that holds | A `defect` at `IMPORTANT`: the packer accepts a spec its builder rejects, or a value the kernel cannot use. For `first shipped`, say the predicate is older and ships for the first time. **Proof**: the table's row (predicate, field, values, seed, the builder's error). A finding `at base` predates the change: `FUTURE WORK` at most | | A C++ table disagreement that holds | A `source-claim`: `BLOCKING` when a valid row's values differ, or when no C++ refusal on the engine's call path refuses a row Python refuses. `IMPORTANT` when the engine path refuses it but the header's own function does not, and another caller reaches the function directly. **Proof**: the harness, the driver command and the disagreeing rows | diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py index a97be7aa736a..aacd66a03395 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/scripts/partition.py @@ -52,12 +52,13 @@ ] # Changes under these trigger the IMPACT lane (lanes/runtime-impact.md): the kernel ingestor's -# producers, packer, native packs, SDK ingestor headers, generic-target table and header, and +# producers, packer, native packs, plugin SDK headers (the loader's include closure is worked out +# per tree, so every header triggers), the generic-target table and its CMake template, and # generator configs. Keep in step with SURFACE_TABLE in hipdnn-ingestor-impact's scripts/impact.py. IMPACT_PREFIXES = ( "dnn-providers/hip-kernel-provider/", - "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/", - "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets", + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/", + "projects/hipdnn/plugin_sdk/cmake/GpuGenericTargets", "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json", "projects/hipdnn/tools/IngestorGenerator/", ) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md index 36e62d57fdd8..47add2be478b 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -67,13 +67,18 @@ How the copies are paired: - `scripts/impact.py` reads git objects only (`cat-file`, `diff`, `ls-tree`, `grep`, `merge-tree`); it writes no ref, index or worktree. Its subcommands are `files`, `kernargs`, `symbols`, `surfaces`, `data` and `report`; `report` runs all of them and ends in the rollup line. + The merged view needs git 2.38 or later (`merge-tree --write-tree`); an older git skips it as + `changed`. - `scripts/impact_emit.py` imports and runs rocKE code from the two trees: a `git archive` extract per side, in a child process whose whole process tree is killed at its timeout, with an environment reduced to system, temp, locale and Python user-site variables, so credentials such as `GH_TOKEN` are not passed. That is the same code execution as building the head, and it is not a sandbox: the child keeps `HOME` (`USERPROFILE` on Windows) and network access, and only tokens held in environment variables are dropped. It builds every shipped variant at both sides - on the CPU (no GPU or comgr): a few minutes, with the two sides on two concurrent workers. + on the CPU (no GPU or comgr): a few minutes, with the two sides on two concurrent workers. Every + `IMPACT_EMIT_*` variable passes through to the child; it reads `IMPACT_EMIT_TABLE_SECONDS` + (see *Predicate tables*) and `IMPACT_EMIT_ENUM_TIMEOUT`, the seconds the registry enumerator + gets for a DVC-held catalog (default 600, inside the per-side `--timeout` of 900). Both exit 0 when the analysis ran, whatever it found, and 1 with one line (`impact: ` or `impact_emit: `) on stderr when they could not run. An exit 1 is never a verdict. @@ -131,8 +136,9 @@ builds. It works on the family's shipped builder, and on every other `build_*` f module that the builds called (a `KernelModule`'s kernel builders, such as a `reduce` companion's). It takes the shipped specs (an inner builder's are the specs the builds passed it) and varies one int or bool field at a time from up to four seeds. The values are the shipped ones, 0, -1, and the -negation, double, half and 1.5 times of each shipped positive value. It asks the predicate the -packer would resolve (`_resolve_support_predicate`). +negation, double, half and 1.5 times of each shipped positive value. A field that ships more than +six distinct ints is cut to its four smallest and two largest positive values, which stand in for +the shipped ones too. It asks the predicate the packer would resolve (`_resolve_support_predicate`). | Check | Finding | |---|---| @@ -172,7 +178,11 @@ did not run. A table that cannot run says why (`skipped`): the packer has no resolver at that revision, no predicate takes the spec, or the specs are not dataclasses. A family's tables stop after -`IMPACT_EMIT_TABLE_SECONDS` (default 60) and say `truncated`. +`IMPACT_EMIT_TABLE_SECONDS` (default 60) and say `truncated`. A builder call that never returns +(a probed spec it loops on) is abandoned 5 s past that budget: that family's tables and those of +the families after it read `skipped: probe timed out`, and the argument and IR comparison is +still written. A builder stuck in native code that holds the interpreter lock still runs the side +to its `--timeout`. Judge each finding whose state is `new` or `first shipped`. Read the predicate and the builder at `HEAD`. @@ -242,7 +252,7 @@ Every judged finding in the report carries one: | Severity | Use for | |---|---| -| `BLOCKING` | A break on the engine's path: a holding `mismatch` row, a dispatch divergence, values that differ on a valid row, a refusal no C++ check on the engine path restates | +| `BLOCKING` | A break on the engine's path: a holding `mismatch` row, a dispatch divergence, values that differ on a valid row, a refusal no C++ check on the engine path restates, a descriptor or generic-target value the loader rejects or ignores | | `IMPORTANT` | Real risk off the engine's path or not yet reachable: a predicate-table finding that holds, a precondition the function does not enforce while another caller reaches it, the engine declining what rocKE serves | | `SUGGESTION` | Anything else worth fixing in code the change touches | diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 8188b114092f..2b21431a7088 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -203,6 +203,7 @@ class Fail(Exception): ast.AsyncWith, ast.Match, ) + ((ast.TryStar,) if hasattr(ast, "TryStar") else ()) +TRY_NODES = (ast.Try,) + ((ast.TryStar,) if hasattr(ast, "TryStar") else ()) ZERO_SHA = "0" * 40 GIT_CONFIG = ( "-c", "core.quotepath=off", @@ -1004,12 +1005,38 @@ def marked(self) -> list: return [n + "?" if c else n for n, c in zip(self.names, self.conditional)] +def always_runs(node, field: str) -> bool: + """Whether the code under `field` of the compound `node` runs whenever `node` does: a + `with` body, a `try` body and `finally`, the head expression of a branch or loop, and the + body of an `if` whose test is a true constant.""" + if isinstance(node, (ast.With, ast.AsyncWith)): + return True + if isinstance(node, TRY_NODES): + return field in ("body", "finalbody") + if isinstance(node, ast.If) and field == "body": + return isinstance(node.test, ast.Constant) and bool(node.test.value) + return field in ("test", "iter", "subject") + + +def branch_children(node, conditional: bool): + """(child, conditional) for each child node, a child marked when it may not run.""" + if not isinstance(node, CONDITIONAL_NODES): + for child in ast.iter_child_nodes(node): + yield child, conditional + return + for field, value in ast.iter_fields(node): + branch = conditional or not always_runs(node, field) + for child in value if isinstance(value, list) else [value]: + if isinstance(child, ast.AST): + yield child, branch + + def param_lists(tree) -> dict: """Function qualname -> its `b.param` names in source order, conditional ones marked.""" result, calls = {}, {} def visit(node, qual, function, conditional): - for child in ast.iter_child_nodes(node): + for child, branch in branch_children(node, conditional): if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)): name = f"{qual}.{child.name}" if qual else child.name result.setdefault(name, ParamList(child.lineno)) @@ -1025,13 +1052,8 @@ def visit(node, qual, function, conditional): and isinstance(child.func, ast.Attribute) and child.func.attr == "param" ): - calls.setdefault(function, []).append((child, conditional)) - visit( - child, - qual, - function, - conditional or isinstance(child, CONDITIONAL_NODES), - ) + calls.setdefault(function, []).append((child, branch)) + visit(child, qual, function, branch) visit(tree, "", None, False) for function, found in calls.items(): @@ -1249,6 +1271,9 @@ def cpp_kernel_lists(text: str) -> list: [{func, line, names, sizes, dynamic, unnamed}]; `names` is None unless every entry is a `KernelArgument{kind, size, offset, "name"}` literal with a non-empty name.""" code = mask_cpp(text) + # Arguments are read with comments blanked and string literals kept, so a comment inside + # the braces is not taken for an argument. + plain = strip_c_comments(text) out = [] for fn in _KA_FUNC.finditer(code): open_at = fn.end() - 1 @@ -1264,7 +1289,7 @@ def cpp_kernel_lists(text: str) -> list: for i in range(open_at, close): if i in literals: end = match_brace(code, literals[i].end() - 1) - args = split_args(text[literals[i].end() : end]) + args = split_args(plain[literals[i].end() : end]) groups.setdefault(stack[-1] if stack else open_at, []).append( (args, text.count("\n", 0, i) + 1) ) @@ -1932,7 +1957,6 @@ def rocke_status(ctx: Ctx, ch: Change): else "; runs after lowering, so an equal emitted IR does not cover it" ) side = ctx.closure("before" if ch.head is None else "after") - before = after = side archs = set(side.shipped.get(path, ())) if path in side.shipped: arch = ALL_ARCHS if ALL_ARCHS in archs else ", ".join(sorted(archs)) @@ -1944,7 +1968,7 @@ def rocke_status(ctx: Ctx, ch: Change): arch, fold, ) - why = after.consumed.get(path) or before.consumed.get(path) + why = side.consumed.get(path) if why: bt, at = ctx.ast(ch, "before"), ctx.ast(ch, "after") if bt is None or at is None: @@ -3072,12 +3096,18 @@ def surfaces_rows(ctx: Ctx) -> list: ) if path and ctx.after.read([path])[path] is None ] - if not broken and dump is None and (py_changed or manifest_changed): - broken = [f"function {surface.function}"] + problem = None if broken: + problem = f"points at a missing {', '.join(broken)} after the change" + elif dump is None and (py_changed or manifest_changed): + problem = ( + f"points at python module {surface.module}, which does not parse after the change" + if ctx.after.ast(surface.module) is None + else f"points at a missing function {surface.function} after the change" + ) + if problem: rows.append(make_row("surfaces", surface.manifest, 0, name, "mismatch", - f"declared launch_surface {surface.name} points at a missing {', '.join(broken)} after the change", - counter, arch=arch)) # fmt: skip + f"declared launch_surface {surface.name} {problem}", counter, arch=arch)) # fmt: skip elif py_changed or cpp_changed or manifest_changed: side = ( ("python side" if py_changed else "") @@ -3369,19 +3399,22 @@ def params(value): return f"family {n}: `failures` lacks base and head objects" if not ( isinstance(built, dict) - and all(params(built.get(s)) for s in ("base", "head")) + and all(s in built and params(built[s]) for s in ("base", "head")) ): return f"family {n}: `params` is not base and head [name, type] lists" companions = fam.get("companions") if not ( isinstance(companions, dict) and all( - companions.get(s) is None - or ( - isinstance(companions[s], dict) - and all( - isinstance(r, str) and p is not None and params(p) - for r, p in companions[s].items() + s in companions + and ( + companions[s] is None + or ( + isinstance(companions[s], dict) + and all( + isinstance(r, str) and p is not None and params(p) + for r, p in companions[s].items() + ) ) ) for s in ("base", "head") @@ -3596,10 +3629,12 @@ def anchor(row) -> str: return f"{row['path']}:{row['line']}" if row["line"] else row["path"] -def render_rows(rows: list) -> str: - def esc(text): - return str(text).replace("|", "\\|").replace("\n", " ") +def esc(text) -> str: + """`text` as one Markdown table cell: pipes escaped, newlines flattened.""" + return str(text).replace("|", "\\|").replace("\n", " ") + +def render_rows(rows: list) -> str: lines = [ "| Anchor | View | Status | Reason | Counterparts |", "|---|---|---|---|---|", @@ -3674,9 +3709,6 @@ def render_tables(tables: list, edited=None) -> str: that ship for the first time, then pre-existing ones (`finding_state`), then the tables that could not run. Pack-time facts; no row or rollup reads them.""" - def esc(text): - return str(text).replace("|", "\\|").replace("\n", " ") - def state(t, f): return finding_state(t, f, edited) @@ -3837,19 +3869,13 @@ def cmd(args) -> None: selected = ( rows if args.cmd == "report" - else [ - r - for r in rows - if r["kind"] in KINDS[args.cmd] or (args.cmd == "files" and False) - ] + else [r for r in rows if r["kind"] in KINDS[args.cmd]] ) files = sorted(ctx.changes.values(), key=lambda c: c.path) worst = {} for r in rows: if SEVERITY[r["status"]] >= SEVERITY[worst.get(r["path"], "outside")]: worst[r["path"]] = r["status"] - if args.cmd == "files": - selected = [r for r in rows if r["kind"] == "file"] if args.json: payload = {"views": views, "rows": [public(r) for r in selected]} if args.cmd in ("files", "report"): diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index 412de3ce178b..271ca4358bc1 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -42,7 +42,10 @@ shipped value (<= 0 where all are >= 1, < 0 where all are >= 0) of a field they share that one predicate accepts and the other refuses is a `sign` finding on the one that accepts. A finding the base's table also has (same check, same field) is marked `at_base`. A family's tables stop -after `IMPACT_EMIT_TABLE_SECONDS` (default 60) and say `truncated`. +after `IMPACT_EMIT_TABLE_SECONDS` (default 60) and say `truncated`. The probes run in a worker +thread: a builder that never returns on a spec the predicate accepts costs only its family +(and the families after it) the tables, which read `skipped: "probe timed out"`, once the +family is 5 s past its budget; the families and `not_inspected` are still written and compared. The flavor defaults to the newest flavor the head and the base both know (the top rung of `_LLVM_FLAVOR_LADDER` in `rocke/platform/python/rocke/core/lower_llvm.py`, else the last @@ -69,6 +72,7 @@ import sys import tarfile import tempfile +import threading import time import traceback from concurrent.futures import ThreadPoolExecutor @@ -643,10 +647,14 @@ def _build_one(h, v, arch, capture=None): #: Seeds per table, and how many distinct shipped values of one int field seed its domain. _TABLE_SEEDS = 4 _TABLE_BASE_INTS = 6 +#: Seconds a family's probes may run past their budget before the family is abandoned. +_TABLE_GRACE = 5 def _spec_key(spec): - return repr(dataclasses.asdict(spec)) + if dataclasses.is_dataclass(spec) and not isinstance(spec, type): + return repr(dataclasses.asdict(spec)) + return repr(id(spec)) # not a dataclass: the table says so, it needs no key def _domains(specs): @@ -845,38 +853,71 @@ def _same_module_signs(tables, deadline): f"{b.name} in the same module, refuses it; every shipped value is >= {low}") # fmt: skip +def _table_names(fam, got): + return [fam["builder"], *sorted({n for n, _ in got["inner"]})] + + +def _table_record(fam, t): + return { + "arch": fam["arch"], + "source": fam["source"], + "builder": fam["builder"], + "inner": "" if t.name == fam["builder"] else t.name, + **t.finish(), + } + + +def _family_tables(h, fam, got, seconds): + """The table records of one family: its shipped builder's and each inner builder's.""" + module = got["module"] + inner = {} + for name, spec in got["inner"]: + inner.setdefault(name, []).append(spec) + deadline = time.time() + seconds + tables = [ + _predicate_table(h, module, name, catalog, fam["arch"], deadline) + for name, catalog in [(fam["builder"], got["specs"]), *sorted(inner.items())] + ] + _same_module_signs(tables, deadline) + return [_table_record(fam, t) for t in tables] + + def _tables(h, fams, captures): - """One table per family's shipped builder and per inner builder its builds called.""" + """(records, stuck): one table per family's shipped builder and per inner builder its + builds called. The probes run in a worker thread; a family whose probes run past its + IMPACT_EMIT_TABLE_SECONDS budget plus _TABLE_GRACE (a builder that never returns on a spec + the predicate accepts) is abandoned with every table not yet finished, and STUCK is True. + """ seconds = float(os.environ.get("IMPACT_EMIT_TABLE_SECONDS", "60")) - out = [] - for key, fam in fams.items(): - got = captures.get(key) - if not got: - continue - module = got["module"] - inner = {} - for name, spec in got["inner"]: - inner.setdefault(name, []).append(spec) - deadline = time.time() + seconds - tables = [ - _predicate_table(h, module, name, catalog, fam["arch"], deadline) - for name, catalog in [ - (fam["builder"], got["specs"]), - *sorted(inner.items()), - ] - ] - _same_module_signs(tables, deadline) - for t in tables: - out.append( - { - "arch": fam["arch"], - "source": fam["source"], - "builder": fam["builder"], - "inner": "" if t.name == fam["builder"] else t.name, - **t.finish(), - } - ) - return out + plan = [(fam, captures[key]) for key, fam in fams.items() if captures.get(key)] + done, state = [], {"at": (0, time.time()), "error": None} + + def work(): + try: + for i, (fam, got) in enumerate(plan): + state["at"] = (i, time.time()) + done.append(_family_tables(h, fam, got, seconds)) + except BaseException as e: # re-raised in the caller's thread + state["error"] = e + + worker = threading.Thread(target=work, daemon=True) + worker.start() + while worker.is_alive(): + worker.join(0.1) + if time.time() - state["at"][1] > seconds + _TABLE_GRACE: + break + stuck = worker.is_alive() + if state["error"] is not None: + raise state["error"] + finished = list(done) + out = [r for recs in finished for r in recs] + if stuck: + for fam, got in plan[len(finished) :]: + for name in _table_names(fam, got): + t = _Table(name) + t.record["skipped"] = "probe timed out" + out.append(_table_record(fam, t)) + return out, stuck def child_main(root, out): @@ -939,13 +980,16 @@ def child_main(root, out): ) got["specs"].append(capture["spec"]) got["inner"] += capture["inner"] + tables, stuck = _tables(h, fams, captures) doc = { "families": list(fams.values()), - "tables": _tables(h, fams, captures), + "tables": tables, "not_inspected": skipped, "seconds": round(time.time() - t0, 1), } Path(out).write_text(json.dumps(doc), encoding="utf-8", newline="\n") + if stuck: + os._exit(0) # a probe thread still spins in a builder; the document is written return 0 @@ -1167,6 +1211,30 @@ def key(t): ] +def build_doc( + a, flavor, base_tree, head_tree, base_side, head_side, families, tables, seconds +): + """The emit document (schema 6) of one run, from the two sides' child documents and + their compared families and tables.""" + return { + "schema": 6, + "tool": "impact_emit", + "flavor": flavor, + "base": a.base, + "head": a.head, + "base_tree": base_tree, + "head_tree": head_tree, + "families": families, + "predicate_tables": tables, + "not_inspected": [ + {"kdp": n["kdp"], "side": side, "reason": n["reason"]} + for side, doc in (("base", base_side), ("head", head_side)) + for n in doc["not_inspected"] + ], + "seconds": seconds, + } + + def cmd_run(a): repo = Path(a.repo) if not repo.is_dir(): @@ -1182,25 +1250,11 @@ def cmd_run(a): base_side, head_side = fb.result(), fh.result() families = compare(base_side, head_side) tables = compare_tables(base_side, head_side) - not_inspected = [ - {"kdp": n["kdp"], "side": side, "reason": n["reason"]} - for side, doc in (("base", base_side), ("head", head_side)) - for n in doc["not_inspected"] - ] seconds = round(time.time() - t0, 1) - doc = { - "schema": 6, - "tool": "impact_emit", - "flavor": flavor, - "base": a.base, - "head": a.head, - "base_tree": base_tree, - "head_tree": head_tree, - "families": families, - "predicate_tables": tables, - "not_inspected": not_inspected, - "seconds": seconds, - } + doc = build_doc( + a, flavor, base_tree, head_tree, base_side, head_side, families, tables, seconds + ) + not_inspected = doc["not_inspected"] try: Path(a.out).write_text( json.dumps(doc, indent=1) + "\n", encoding="utf-8", newline="\n" diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py index dd3bfc721974..ddd22c1d1dda 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py @@ -482,10 +482,12 @@ def test_impact_scope_is_the_ingestor_path_only(part): "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/Descriptors.hpp", "projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json", "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets.hpp", + "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/EnginePluginApi.h", + "projects/hipdnn/plugin_sdk/cmake/GpuGenericTargetsTable.hpp.in", "projects/hipdnn/tools/IngestorGenerator/configs/gfx950_attention_dense.engine.yaml", ) outside = ( - "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/EnginePluginApi.h", + "projects/hipdnn/plugin_sdk/tests/a.cpp", "dnn-providers/miopen-provider/src/a.cpp", "projects/hipdnn/tools/DescriptorGenerator/gen.py", ) @@ -547,3 +549,33 @@ def test_orient_gate_accepts_added_files_and_flags_paths_absent_at_base( (rd / "CONTEXT.md").write_text("Area `src/old.cpp:1` and dir `src`.\n") run(part, "orient-gate", rd) assert capsys.readouterr().out == "" + + +def test_impact_lane_id_is_a_provisional_id(part): + """A finding ID from the IMPACT lane is recognised, like every other report lane.""" + # catches: IMPACT dropped from CONCERN_LANES/REPORT_LANES, so IMPACT findings are + # invisible to the ledger and comment-reference checks. + assert part.PROVISIONAL_ID.findall("see IMPACT1-p2 and CORR1-p2") == [ + "IMPACT1-p2", + "CORR1-p2", + ] + + +def test_every_ingestor_impact_surface_pattern_triggers_the_impact_lane(part): + """IMPACT_PREFIXES must cover each non-outside SURFACE_TABLE pattern's literal prefix, + or a change to that surface never reaches the IMPACT lane.""" + # catches: a SURFACE_TABLE entry added under a path IMPACT_PREFIXES does not list. + impact = SCRIPT.parent.parent.parent / "hipdnn-ingestor-impact" / "scripts" + spec = importlib.util.spec_from_file_location( + "hipdnn_ingestor_impact", impact / "impact.py" + ) + assert spec and spec.loader, f"could not load {impact / 'impact.py'}" + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + uncovered = [ + (tag, pattern) + for tag, pattern in module.SURFACE_TABLE + if tag not in module.OUTSIDE_TAGS + and not pattern.split("*", 1)[0].startswith(part.IMPACT_PREFIXES) + ] + assert uncovered == [] diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index bea5601b5098..e25a684247dc 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -7,8 +7,10 @@ isolated. """ +import ast import importlib.util import json +import re import subprocess from pathlib import Path from types import ModuleType @@ -180,6 +182,18 @@ def report(mod, capsys, repo, base, head, *extra, cmd="report") -> dict: return json.loads(out) +def git_version() -> tuple: + out = subprocess.run(["git", "--version"], capture_output=True, text=True).stdout + found = re.search(r"(\d+)\.(\d+)", out) + return tuple(map(int, found.groups())) if found else (0, 0) + + +#: The merged view runs `git merge-tree --write-tree`, which git 2.38 added. +needs_merge_tree = pytest.mark.skipif( + git_version() < (2, 38), reason="merge-tree --write-tree needs git 2.38" +) + + def rows_of(data, kind=None, status=None): return [ r @@ -227,6 +241,7 @@ def test_comment_only_touch_of_pin_is_not_moved(mod, capsys, tmp_path): assert [r["path"] for r in rows_of(data, "kernargs", "mismatch")] == [BUILDER] +@needs_merge_tree def test_pin_on_target_tip_only_is_mismatch_in_merged_view_only(mod, capsys, tmp_path): # catches: merged view not computed, or computed against the merge base repo, base = make_repo(tmp_path, {BUILDER: BUILD}) @@ -241,6 +256,7 @@ def test_pin_on_target_tip_only_is_mismatch_in_merged_view_only(mod, capsys, tmp assert data["rollup"] == "Runtime impact: BREAKS" +@needs_merge_tree def test_merge_conflict_on_a_surface_file_marks_the_merged_view_changed( mod, capsys, tmp_path ): @@ -267,6 +283,7 @@ def test_merge_conflict_on_a_surface_file_marks_the_merged_view_changed( ] +@needs_merge_tree def test_conflict_elsewhere_still_finds_the_merged_view_race(mod, capsys, tmp_path): # catches: one conflicted file, even one no runtime reads, discarding the whole merged # view and with it a pin race on files that merged cleanly @@ -1057,14 +1074,19 @@ def test_multi_launch_pack_compares_each_list_with_its_own_builder( @pytest.mark.parametrize( "added, status", [(' if spec.lse:\n b.param("lse_ptr", PTR)\n', "changed"), - (' b.param("lse_ptr", PTR)\n', "mismatch")], - ids=["conditional-param-added", "unconditional-param-added"], + (' b.param("lse_ptr", PTR)\n', "mismatch"), + (' with b.scope():\n b.param("lse_ptr", PTR)\n', "mismatch"), + (' try:\n b.param("lse_ptr", PTR)\n finally:\n pass\n', "mismatch"), + (' if True:\n b.param("lse_ptr", PTR)\n', "mismatch")], + ids=["conditional-param-added", "unconditional-param-added", "under-with", + "under-try-body", "under-if-true"], ) # fmt: skip def test_copy_without_a_newly_added_conditional_param_is_not_a_mismatch( mod, capsys, tmp_path, added, status ): # catches: a copy of the list without an optional param, which variants that do not enable - # it still pass, read as a break; or a copy missing a required new param read as fine + # it still pass, read as a break; or a copy missing a required new param read as fine, + # including one under a `with`, a `try` body or `if True:`, which always run repo, base = abi_repo(tmp_path) head = commit( repo, {BUILDER: BUILD.replace(" return b", added + " return b")} @@ -1083,9 +1105,8 @@ def test_emit_fold_compares_each_companion_launch_with_its_static_list( mod, capsys, tmp_path, reduce_params, status ): # catches: only a KernelModule's primary launch compared with the pack, so a companion's - # changed kernargs read as consistent; or a pack found only by the arch-named fallback, - # not through the manifest's `source` builder that the family names - # no arch in the pack's name, so only the manifest's builder can pair it + # changed kernargs read as consistent; or a pack named for no arch, which only the + # manifest's `source` builder can pair, left unpaired pack = HKP + "src/engines/kernel_ingestor_engine/packs/AttnNative.cpp" lists = ( PACK_STATIC @@ -1162,6 +1183,7 @@ def test_method_removed_from_a_base_read_only_through_a_subclass_is_changed( } +@needs_merge_tree def test_conflict_only_in_a_test_still_marks_the_merged_view_changed( mod, capsys, tmp_path ): @@ -1185,6 +1207,7 @@ def test_conflict_only_in_a_test_still_marks_the_merged_view_changed( assert data["rollup"] == "Runtime impact: N/A (no surface file)" +@needs_merge_tree def test_unrelated_target_history_skips_the_merged_view_as_changed( mod, capsys, tmp_path ): @@ -1516,6 +1539,7 @@ def test_member_read_of_a_moved_class_is_not_a_removal_reader(mod, capsys, tmp_p assert "Space.outer_knob" in {r["name"] for r in rows} +@needs_merge_tree def test_target_tip_keeps_the_emit_fold_for_modules_the_head_owns( mod, capsys, tmp_path ): @@ -1585,3 +1609,147 @@ def test_static_list_cut_short_of_the_declared_builder_is_mismatch( head = commit(repo, {PACK: ptr_sig("attnSignature", ["q_ptr", "k_ptr"])}) (row,) = rows_of(report(mod, capsys, repo, base, head), "kernargs", "mismatch") assert "builder build_attn" in row["reason"] + + +@pytest.mark.parametrize( + "locator", + [ + '"kernels/gfx950/attn.py: (the launch builder)"', + "kernels/gfx950/attn.py:launch_block", + ], + ids=["prose-locator", "named-function"], +) +def test_surface_module_that_stops_parsing_says_so(mod, capsys, tmp_path, locator): + # catches: an unparsable surface module reported as "a missing function None" (prose + # locator) or as a missing function that is still in the text + surface = manifest(("block", locator, GEOM)) + files = {BUILDER: WITH_METHOD, GEOM: "int block();\n", MANIFEST: surface} + repo, base = make_repo(tmp_path, files) + head = commit(repo, {BUILDER: WITH_METHOD + "\ndef broken(b:\n"}) + (row,) = rows_of(report(mod, capsys, repo, base, head), "surfaces") + assert row["status"] == "mismatch" and "does not parse" in row["reason"] + assert "missing" not in row["reason"] + + +def test_comment_inside_a_kernel_argument_literal_is_not_an_argument(mod): + # catches: a literal's arguments split from the unmasked text, so a comment in the braces + # makes the list unnamed and it is never compared with its builder + text = """std::vector sig() +{ + return {KernelArgument{"global_buffer", PTR, 0, // kind, size, offset + "q_ptr"}, + KernelArgument{"by_value", F32, 8, /* a, b */ "scale"}}; +} +""" + (entry,) = mod.cpp_kernel_lists(text) + assert (entry["names"], entry["sizes"]) == (["q_ptr", "scale"], ["ptr", 4]) + + +def emit_doc() -> dict: + """A well-formed impact_emit document with one quiet family and no tables.""" + return {"schema": 6, "tool": "impact_emit", "base_tree": "a" * 40, "head_tree": "b" * 40, + "families": [family()], "predicate_tables": [], "not_inspected": []} # fmt: skip + + +@pytest.mark.parametrize( + "spoil", + [lambda d: d["families"][0].update(params={}), + lambda d: d["families"][0].update(companions={}), + lambda d: d.update(predicate_tables=[{k: v for k, v in table().items() if k != "truncated"}]), + lambda d: d.update(predicate_tables=[{k: v for k, v in table().items() if k != "at_base"}])], + ids=["params-without-sides", "companions-without-sides", "table-without-truncated", + "table-without-at-base"], +) # fmt: skip +def test_emit_shape_guard_refuses_a_missing_side_or_table_flag(mod, spoil): + # catches: a family without base and head params or companions passing the guard, so the + # fold dies on a KeyError instead of the one-line refusal; or a table flag left unchecked + doc = emit_doc() + assert mod.emit_shape_problem(doc) is None + spoil(doc) + assert mod.emit_shape_problem(doc) is not None + + +def test_emit_shape_guard_accepts_a_bool_valued_finding(mod): + # catches: finding values narrowed to int, so a real emit file whose probe varied a bool + # field is refused + doc = emit_doc() + doc["predicate_tables"] = [table([finding(values=[True, False])])] + assert mod.emit_shape_problem(doc) is None + + +def test_predicate_tables_text_lists_skipped_tables_and_escapes_cells(mod): + # catches: the skipped-table lines dropped from the text report, or a `|` in a builder + # name or detail breaking the Markdown table + tables = [table([finding(detail="raises at a|b")], builder="build|x"), + table(skipped="no predicate takes its spec", builder="build_y"), + table(truncated=True)] # fmt: skip + text = mod.render_tables(tables) + assert "1 skipped, 1 truncated" in text + assert "skipped (1): no predicate takes its spec: build_y" in text + row = next(line for line in text.splitlines() if "raises at" in line) + assert "build\\|x" in row and "a\\|b" in row + + +PREDICATE = "def supports_attn(spec):\n def ok(v):\n return v > 0\n\n return ok(spec.tile)\n" + + +@pytest.mark.parametrize( + "edited", + [PREDICATE.replace("return ok(spec.tile)", "return ok(spec.tile) and True"), + PREDICATE.replace("v > 0", "v >= 0")], + ids=["predicate-body", "def-nested-in-the-predicate"], +) # fmt: skip +def test_finding_on_a_predicate_the_change_edits_is_new(mod, capsys, tmp_path, edited): + # catches: only the builder's name checked against the edited functions, or a function + # nested in the predicate not counted as the predicate, so a finding the change writes + # reads as an older one shipping for the first time + repo, base = make_repo( + tmp_path, {BUILDER: BUILD + "\n\n" + PREDICATE, KDP: KDP_JSON} + ) + head = commit(repo, {BUILDER: BUILD + "\n\n" + edited}) + emit = write_emit(tmp_path / "t.json", repo, base, head, + tables=[table([finding()], at_base=False)]) # fmt: skip + data = report(mod, capsys, repo, base, head, "--emit", emit) + assert data["pack_time"] == "Pack-time: NEW 1" + + +LAUNCH_SURFACE = ( + Path(__file__).resolve().parent.parent.parent + / "IngestorGenerator" + / "tools" + / "launch_surface.py" +) + + +@pytest.fixture(scope="module") +def launch_surface() -> ModuleType: + spec = importlib.util.spec_from_file_location("launch_surface_rule", LAUNCH_SURFACE) + assert spec and spec.loader, f"could not load {LAUNCH_SURFACE}" + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize( + "locator", + ["kernels/a.py:build_attn", "kernels/a.py:Handler::prepare", "kernels/a.py:prepare -- note", + "kernels/a.py: prepare() trusts the KMD", "kernels/a.py", "kernels/a.py:"], +) # fmt: skip +def test_locator_symbol_rule_is_launch_surface_checks(mod, launch_surface, locator): + # catches: impact.py's restatement of launch_surface.py's locator rule drifting from it, + # although reference.md says the two resolve a python_source alike + assert mod.leading_symbol(locator) == launch_surface._leading_symbol(locator) + + +@pytest.mark.parametrize( + "source", + ["def f():\n pass\n", "class C:\n def f(self):\n pass\n", + "def g():\n def f():\n pass\n", "try:\n def f():\n pass\nexcept E:\n pass\n", + "with x:\n def f():\n pass\n", "async def f():\n pass\n", "f = 1\n# def f():\n"], + ids=["top-level", "method", "nested", "under-try", "under-with", "async", "absent"], +) # fmt: skip +def test_python_symbol_lookup_is_launch_surface_checks(mod, launch_surface, source): + # catches: impact.py's restatement of launch_surface.py's symbol lookup drifting from it + # (an async def, a def under `with` or a nested def found by one and not the other) + found = bool(mod.defs_named(ast.parse(source), "f")) + assert found == launch_surface.python_symbol_exists(source, "f") diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index c55d3c81a85f..6d27b3ef5114 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -10,8 +10,10 @@ import importlib.util import json import os +import sys import subprocess import textwrap +import types import time from pathlib import Path @@ -308,6 +310,18 @@ def scenarios(): s["module_wide"] = module_files(reduce_params=[("lse", "ptr addrspace(1)")]) s["module_body"] = module_files(reduce_body="r2") s["tables"] = table_files() + s["tables_hang"] = table_files() + s["tables_hang"][BUILDER] = ( + s["tables_hang"][BUILDER] + .replace( + 'assert spec.block % spec.tile == 0, "tile must divide block"', + "while spec.tile < 1:\n pass", + ) + .replace( + 'return spec.n >= 1 and spec.block >= 1 and spec.tile >= 1, "ok"', + 'return True, "ok"', + ) + ) s["tables_fixed"] = table_files(reduce_check="spec.n >= 1 and spec.n % 8 == 0") return s @@ -515,6 +529,243 @@ def test_predicate_tables_say_why_they_did_not_run_and_stop_at_their_budget( assert all(t["truncated"] and t["rows"] == 0 for t in doc["predicate_tables"]) +def test_a_builder_that_hangs_on_a_probed_spec_costs_only_the_tables( + emit, repo, tmp_path, monkeypatch +): + # catches: the probe loop sharing the child's --timeout, so one builder that never + # returns on a spec its predicate accepts (tile=0) kills the child and the whole run + # exits 1 with no JSON although every variant built and compared + monkeypatch.setenv("IMPACT_EMIT_TABLE_SECONDS", "0.2") + t0 = time.time() + rc, doc = run( + emit, repo, "tables_hang", "tables_hang", tmp_path, "--timeout", "120" + ) + assert time.time() - t0 < 100 + assert rc == 0 + f = only_family(doc) + assert f["variants"] == {"base": 2, "head": 2} + assert f["failures"] == {"base": {}, "head": {}} + assert {t["inner"] for t in doc["predicate_tables"]} == {"", "build_reduce"} + assert all(t["skipped"] == "probe timed out" for t in doc["predicate_tables"]) + + +def probe_src(fields, pred, body="", reduce_pred=None): + """A builder module for one predicate-table case. FIELDS are the Spec fields, PRED the + full `supports_fake` def ("" for none), BODY lines run first in `build_fake`. With + REDUCE_PRED, `build_fake` returns a KernelModule whose companion is built by the + inner `build_reduce` (spec `RSpec(n=spec.n)`), guarded by that predicate.""" + fields = "\n".join(f" {f}" for f in fields) + body = "\n".join(f" {b}" for b in body) + inner = ( + "\n@dataclasses.dataclass(frozen=True)\nclass RSpec:\n n: int = 1\n\n" + f"def supports_reduce(spec, arch='x'):\n return {reduce_pred}\n\n" + "def build_reduce(spec, arch):\n" + ' return {"params": [("o", "ptr addrspace(1)")], "body": "r"}\n' + if reduce_pred + else "" + ) + tail = ( + ' return KernelModule({"params": p, "body": "v"},' + ' {"r": build_reduce(RSpec(n=spec.n), arch=arch)})\n' + if reduce_pred + else ' return {"params": p, "body": "v"}\n' + ) + return ( + "import dataclasses\nfrom rocke.helpers import KernelModule\n\n" + f"@dataclasses.dataclass(frozen=True)\nclass Spec:\n{fields}\n\n{pred}\n{inner}\n" + f"def build_fake(spec, arch):\n{body}\n" + ' p = [("q", "ptr addrspace(1)")]\n' + f"{tail}" + ) + + +def probe_tables(emit, tmp_path, src, specs): + """The head tables the child writes for a tree whose builder is SRC and whose KDP ships + SPECS (one variant each).""" + variants = tuple({"name": f"v{i}", "spec": s} for i, s in enumerate(specs)) + files = tree_files(variants=variants) + files[f"{PROVIDER}/rocke/platform/python/rocke/helpers/__init__.py"] = KERNEL_MODULE + files[f"{PROVIDER}/descriptor-packaging/python/hkp_pack/rocke_compile.py"] = ( + HELPERS + RESOLVER + ) + files[BUILDER] = src + root = tmp_path / "tree" + for path, text in files.items(): + (root / path).parent.mkdir(parents=True, exist_ok=True) + (root / path).write_text(text, encoding="utf-8") + out = tmp_path / "side.json" + r = emit.run_tree( + [sys.executable, str(SCRIPT), "child", "--root", str(root), "--out", str(out)], + 120, + cwd=str(root), + env=emit.child_env(str(root), "llvm23"), + ) + assert r.returncode == 0, r.stderr + return json.loads(out.read_text(encoding="utf-8"))["tables"] + + +ACCEPT = "def supports_fake(spec, arch='x'):\n return {}, 'ok'\n" +RAISE_AT_3 = ["if spec.n == 3:", " raise ValueError('three')"] + + +@pytest.mark.parametrize( + "src, specs, skipped, found", + [ + ( # a bare-bool predicate, not a (bool, reason) tuple + probe_src( + ["n: int = 1"], + "def supports_fake(spec, arch='x'):\n return spec.n >= 1\n", + RAISE_AT_3, + ), + [{"n": 1}, {"n": 2}], + "", + {("", "builder", "n"): [3]}, + ), + ( # a predicate that takes no arch + probe_src( + ["n: int = 1"], + "def supports_fake(spec):\n return spec.n >= 1, 'ok'\n", + RAISE_AT_3, + ), + [{"n": 1}, {"n": 2}], + "", + {("", "builder", "n"): [3]}, + ), + ( # a bool field shipped True: False is the value only the bool candidates reach + probe_src( + ["flag: bool = True"], + ACCEPT.format("True"), + ["if not spec.flag:", " raise ValueError('off')"], + ), + [{"flag": True}], + "", + {("", "builder", "flag"): [False]}, + ), + ( # more than six shipped values: only the first four and last two seed the + # derived values (15 from 10, never 75 from 50) + probe_src( + ["n: int = 1"], + ACCEPT.format("True"), + ["if spec.n in (15, 75):", " raise ValueError('x')"], + ), + [{"n": 10 * i} for i in range(1, 9)], + "", + {("", "builder", "n"): [15]}, + ), + ( # a finding only the second seed reaches (n=3 with m=2) + probe_src( + ["n: int = 1", "m: int = 1"], + ACCEPT.format("True"), + ["if spec.n == 3 and spec.m == 2:", " raise ValueError('x')"], + ), + [{"n": 1, "m": 1}, {"n": 2, "m": 2}], + "", + {("", "builder", "n"): [3]}, + ), + ( # a field shipping 0: only the negative values are outside its sign range + probe_src(["n: int = 1"], ACCEPT.format("True"), reduce_pred="spec.n >= 0"), + [{"n": 0}, {"n": 1}], + "", + {("", "sign", "n"): [-1]}, + ), + ( # a field shipping a negative value has no sign range to leave + probe_src(["n: int = 1"], ACCEPT.format("True"), reduce_pred="spec.n >= 0"), + [{"n": -2}, {"n": 1}], + "", + {}, + ), + ( + probe_src(["n: int = 1"], "", RAISE_AT_3), + [{"n": 1}, {"n": 2}], + "no support predicate takes its spec", + {}, + ), + ( # a spec class that is not a dataclass + probe_src(["n: int = 1"], ACCEPT.format("True")).replace( + "@dataclasses.dataclass(frozen=True)\nclass Spec:", + "class Spec:\n def __init__(self, n=1):\n self.n = n\n", + ), + [{"n": 1}], + "no shipped spec, or a spec that is not a dataclass instance", + {}, + ), + ], + ids=[ + "bare-bool-predicate", + "predicate-without-arch", + "bool-field", + "many-shipped-values", + "second-seed", + "ships-zero", + "ships-negative", + "no-predicate", + "non-dataclass-spec", + ], +) +def test_predicate_table_probes_each_kind_of_field_and_predicate( + emit, tmp_path, src, specs, skipped, found +): + # catches: a bare-bool predicate read as res[0]; a predicate without `arch` called with + # one; a bool field probed with only True; the many-shipped-values cut dropped; one seed + # instead of several; the sign guard for a field that ships 0 or a negative value; a + # missing predicate or a non-dataclass spec reported as "no findings" with no reason + tables = probe_tables(emit, tmp_path, src, specs) + assert tables[0]["skipped"] == skipped + got = { + (t["inner"], f["check"], f["field"]): f["values"] + for t in tables + for f in t["findings"] + } + assert got == found + + +def test_a_finding_is_at_base_only_for_the_same_check_on_the_same_field(emit): + # catches: a finding marked pre-existing because the base tabled the builder, or + # because it found another check on the field + def table(*findings): + return { + "arch": "gfx950", + "source": "s.py", + "builder": "build_x", + "inner": "", + "skipped": "", + "findings": [{"check": c, "field": f} for c, f in findings], + } + + base = {"tables": [table(("sign", "n"), ("builder", "m"))]} + head = {"tables": [table(("builder", "n"), ("sign", "n"), ("builder", "m"))]} + (t,) = emit.compare_tables(base, head) + assert t["at_base"] + assert [(f["check"], f["field"], f["at_base"]) for f in t["findings"]] == [ + ("builder", "n", False), + ("sign", "n", True), + ("builder", "m", True), + ] + + +def test_the_emitted_document_is_accepted_by_impacts_loader(emit, tmp_path): + # catches: the emitter's schema literal and impact.py's EMIT_SCHEMA bumped one-sided, + # so every emit file is then rejected; or a document field the loader reads changing + spec = importlib.util.spec_from_file_location( + "hipdnn_ingestor_impact_for_emit", SCRIPT.with_name("impact.py") + ) + impact = importlib.util.module_from_spec(spec) + spec.loader.exec_module(impact) + side = {"not_inspected": [{"kdp": "k.kdp.json", "reason": "r"}]} + sides = {"base": side, "head": side} + names = ["v"] + fam = family(names, {"v": [["q", "ptr"]]}) + args = types.SimpleNamespace(base="b", head="h") + tree = "0" * 40 + doc = emit.build_doc( + args, "llvm23", tree, tree, sides["base"], sides["head"], + emit.compare({"families": [fam]}, {"families": [fam]}), [], 1.0, + ) # fmt: skip + path = tmp_path / "emit.json" + path.write_text(json.dumps(doc), encoding="utf-8") + assert impact.load_emit(path)["schema"] == doc["schema"] + + def test_removed_variant_is_only_in_base_and_unchanged_one_is_not_flagged( emit, repo, tmp_path ): diff --git a/projects/hipdnn/tools/ai/tests/test_skills_tooling.py b/projects/hipdnn/tools/ai/tests/test_skills_tooling.py index ecbd8f1ab4ce..587564258545 100644 --- a/projects/hipdnn/tools/ai/tests/test_skills_tooling.py +++ b/projects/hipdnn/tools/ai/tests/test_skills_tooling.py @@ -294,12 +294,15 @@ def test_validate_skill_ignores_claude_fields_outside_frontmatter( assert any("allowed-tools" in error for error in errors) -def test_expected_scripts_cover_every_deep_review_script(validate_mod): - """A deep-review script added or removed shows up in EXPECTED_SCRIPTS.""" - on_disk = { - p.name for p in (SKILLS_DIR / "hipdnn-deep-review" / "scripts").glob("*.py") - } - assert set(validate_mod.EXPECTED_SCRIPTS["hipdnn-deep-review"]) == on_disk +@pytest.mark.parametrize("skill", ["hipdnn-deep-review", "hipdnn-ingestor-impact"]) +def test_expected_scripts_cover_every_script_of_a_fully_listed_skill( + validate_mod, skill +): + """A script added or removed in these skills' scripts/ dir shows up in EXPECTED_SCRIPTS.""" + # catches: deleting the skill's EXPECTED_SCRIPTS entry (validate-skills then stops + # checking its scripts exist) or shipping a script the entry never lists. + on_disk = {p.name for p in (SKILLS_DIR / skill / "scripts").glob("*.py")} + assert set(validate_mod.EXPECTED_SCRIPTS[skill]) == on_disk def test_symlink_target_recognises_a_git_materialized_link(validate_mod, tmp_path): From bb5fc75eaf65317fd0dfd8ec0b93825e1af6b345 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Tue, 3 Nov 2026 14:41:09 -0600 Subject: [PATCH 10/13] fix(hipdnn): follow develop's flavor and descriptor layout in ingestor impact The rocKE LLVM flavor ladder moved from lower_llvm.py into rocke/core/llvm_profile.py, which lower_llvm.py now imports it from, so the emitter could no longer find a default flavor on any tree that has the new module. It now reads the ladder from llvm_profile.py when the tree has it and from lower_llvm.py otherwise. The packer accepts hip and hsaco entries in a multi-launch UKD's sources map. Such a UKD has nothing for the emitter to build, so it is skipped like a single hip launch instead of being listed as not inspected, which would have kept every emit file from folding. FlyDSL keeps one DVC-held catalog per architecture in its own producer tree; only a catalog under the rocKE tree now counts as shipping the rocKE kernels of its architecture. ALMIOPEN-2834 --- .../hipdnn-ingestor-impact/scripts/impact.py | 6 ++- .../scripts/impact_emit.py | 46 +++++++++++++------ .../tools/ai/tests/test_ingestor_impact.py | 29 ++++++++++++ .../ai/tests/test_ingestor_impact_emit.py | 43 +++++++++++++++-- 4 files changed, 104 insertions(+), 20 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 2b21431a7088..99c2dbe2cc40 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -81,6 +81,8 @@ class Fail(Exception): IMPORT_ROOTS = (ROCKE_LIB, HKP + "rocke/platform/python/") ENGINE = HKP + "src/engines/kernel_ingestor_engine/" DESC_DIR = ENGINE + "descriptors/" +#: The rocKE producer's descriptor tree; FlyDSL and other producers have their own. +ROCKE_DESC_DIR = DESC_DIR + "rocKE/" PACKER = HKP + "descriptor-packaging/" GEN = "projects/hipdnn/tools/IngestorGenerator/" SDK = "projects/hipdnn/plugin_sdk/" @@ -1694,8 +1696,10 @@ def reexport(init: str, name: str): if not archs: archs = expand_archs(arch_names(path, target), generics) roots.append((f"{path}:{line}", target, archs, True)) + # A DVC-held catalog stands for the rocKE kernels of its arch only under the rocKE + # producer tree; another producer's catalog (FlyDSL) ships no rocKE module. for path in sorted( - p for p in tree.paths if p.startswith(DESC_DIR) and p.endswith(".dvc") + p for p in tree.paths if p.startswith(ROCKE_DESC_DIR) and p.endswith(".dvc") ): arch = arch_of(path) if arch: diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index 271ca4358bc1..4876fe07b8ba 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -48,7 +48,8 @@ family is 5 s past its budget; the families and `not_inspected` are still written and compared. The flavor defaults to the newest flavor the head and the base both know (the top rung -of `_LLVM_FLAVOR_LADDER` in `rocke/platform/python/rocke/core/lower_llvm.py`, else the last +of `_LLVM_FLAVOR_LADDER` in `rocke/platform/python/rocke/core/llvm_profile.py`, or in +`lower_llvm.py` in trees from before that module, else the last of `LLVM_FLAVORS`); with none shared it is the head's newest and the base fails loudly. The child gets an allow-listed environment and runs in its own process group, which `--timeout` kills as a whole. Each side's tree id (`base_tree`, `head_tree`, resolved once @@ -87,6 +88,8 @@ ENUMERATOR = f"{GENERATOR}/tools/registry_enumerator.py" MANIFESTS = f"{GENERATOR}/configs" LOWER_LLVM = f"{ROCKE_PLATFORM}/rocke/core/lower_llvm.py" +#: Where newer trees define the flavor ladder; lower_llvm.py re-exports it from here. +LLVM_PROFILE = f"{ROCKE_PLATFORM}/rocke/core/llvm_profile.py" REQUIRED_TREES = (ROCKE_PLATFORM, ROCKE_LIBRARY, PACKAGING) OPTIONAL_TREES = (DESCRIPTORS, GENERATOR) @@ -152,15 +155,15 @@ def extract(repo, rev, dest, name=None): target.write_bytes(tf.extractfile(m).read()) -def _known_flavors(src, where): - """Flavors of one lower_llvm.py source, oldest first, parsed without importing: the - rungs of `_LLVM_FLAVOR_LADDER` by major version; older trees have no ladder, so +def _known_flavors(src, where, path=LOWER_LLVM): + """Flavors of one flavor-defining source (PATH), oldest first, parsed without importing: + the rungs of `_LLVM_FLAVOR_LADDER` by major version; older trees have no ladder, so their `LLVM_FLAVORS` in order. """ try: tree = ast.parse(src) except SyntaxError as e: - raise Fatal(f"cannot parse {LOWER_LLVM} at {where}: {e}; pass --flavor") + raise Fatal(f"cannot parse {path} at {where}: {e}; pass --flavor") consts, ladder, flavors = {}, None, None for node in tree.body: if isinstance(node, ast.Assign): @@ -187,7 +190,7 @@ def _known_flavors(src, where): if known and isinstance(known[-1], str): return [k for k in known if isinstance(k, str)] raise Fatal( - f"no _LLVM_FLAVOR_LADDER or LLVM_FLAVORS in {LOWER_LLVM} at {where}; pass --flavor" + f"no _LLVM_FLAVOR_LADDER or LLVM_FLAVORS in {path} at {where}; pass --flavor" ) rungs = [] for el in ladder.elts: @@ -207,21 +210,25 @@ def _known_flavors(src, where): return [flavor for _, flavor in sorted(rungs)] +def _flavors_at(repo, rev): + """Known flavors of REV, oldest first, read from `llvm_profile.py` when the tree has it + and from `lower_llvm.py` before that module existed.""" + path = LLVM_PROFILE if existing_paths(repo, rev, [LLVM_PROFILE]) else LOWER_LLVM + return _known_flavors(git(repo, "show", f"{rev}:{path}"), rev, path) + + def default_flavor(repo, rev): """Newest flavor of REV: the top ladder rung, else the last entry of `LLVM_FLAVORS`.""" - src = git(repo, "show", f"{rev}:{LOWER_LLVM}") - return _known_flavors(src, rev)[-1] + return _flavors_at(repo, rev)[-1] def shared_default_flavor(repo, base, head): """The head's newest flavor that the base also knows, so a base from before the head's top rung is not failed wholesale. With none shared, the head's top (it then fails loudly); a base whose flavors cannot be read leaves the head's top as well.""" - head_known = _known_flavors(git(repo, "show", f"{head}:{LOWER_LLVM}"), head) + head_known = _flavors_at(repo, head) try: - base_known = set( - _known_flavors(git(repo, "show", f"{base}:{LOWER_LLVM}"), base) - ) + base_known = set(_flavors_at(repo, base)) except Fatal: return head_known[-1] return next((f for f in reversed(head_known) if f in base_known), head_known[-1]) @@ -317,6 +324,10 @@ def _arch_list(value): return [a for a in value or [] if isinstance(a, str)] +#: Packer `kernel_source` kinds with no rocKE build: the emitter has nothing to lower for them. +_OTHER_KINDS = {"hip", "hsaco", "kpack", "embedded_source"} + + def _spec_json(spec): return json.dumps(spec, sort_keys=True, default=str) @@ -326,8 +337,10 @@ def _ukd_variants(d, arches, kdp, origin): `kernel_source` is one variant named by the descriptor (none when it lacks source and builder, e.g. a hip kernel). A `sources` UKD groups its launches by (source, builder, spec, target); each group is one variant, named by the descriptor when there is one group, - else `@[+...]` with the launch names sorted. REASON, set when the - descriptor cannot be read, is for `not_inspected` and the variants are then empty. + else `@[+...]` with the launch names sorted. A `sources` UKD whose + launches all share one other packer kind (hip, hsaco) has no rocKE build and gives none, + as a single hip launch does. REASON, set when the descriptor cannot be read, is for + `not_inspected` and the variants are then empty. """ has_ks, has_sources = "kernel_source" in d, "sources" in d name = d.get("name") or d.get("id") or "?" @@ -337,6 +350,11 @@ def _ukd_variants(d, arches, kdp, origin): entries = d["sources"] if not isinstance(entries, dict) or not entries: return [], f"{name}: `sources` is not a non-empty object" + kinds = { + ks.get("kind") if isinstance(ks, dict) else None for ks in entries.values() + } + if len(kinds) == 1 and kinds & _OTHER_KINDS: + return [], None groups = {} for launch, ks in entries.items(): if not isinstance(ks, dict): diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index e25a684247dc..ff40c936d597 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -1753,3 +1753,32 @@ def test_python_symbol_lookup_is_launch_surface_checks(mod, launch_surface, sour # (an async def, a def under `with` or a nested def found by one and not the other) found = bool(mod.defs_named(ast.parse(source), "f")) assert found == launch_surface.python_symbol_exists(source, "f") + + +@pytest.mark.parametrize( + "catalog, status", + [ + ("FlyDSL/rmsnorm/gfx942.dvc", "inert"), + ("rocKE/gfx942_attn/artifacts.dvc", "changed"), + ], + ids=["another-producer", "rocke"], +) +def test_only_a_rocke_catalog_ships_the_rocke_kernels_of_its_arch( + mod, capsys, tmp_path, catalog, status +): + # catches: another producer's DVC-held catalog (FlyDSL keeps one per arch) taken to ship + # every rocKE kernel of that arch, so an unshipped kernel edit reads NEEDS-DEVICE + pointer = HKP + "src/engines/kernel_ingestor_engine/descriptors/" + catalog + body = "def f():\n return {}\n" + files = { + OTHER_KERNEL: body.format(1), + pointer: "outs:\n- md5: a.dir\n path: artifacts\n", + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {OTHER_KERNEL: body.format(2)}) + (row,) = [ + r + for r in rows_of(report(mod, capsys, repo, base, head), "file") + if r["path"] == OTHER_KERNEL + ] + assert row["status"] == status diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index 6d27b3ef5114..991c7bcc32ee 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -968,12 +968,32 @@ def test_shared_default_flavor_is_the_heads_newest_that_the_base_knows( ): # catches: a base lacking the head's top flavor failing every variant srcs = {"b": base_src, "h": head_src} - monkeypatch.setattr( - emit, "git", lambda repo, cmd, spec, **kw: srcs[spec.split(":")[0]] - ) + + def fake_git(repo, cmd, *rest, **kw): + return "" if cmd == "ls-tree" else srcs[rest[0].split(":")[0]] + + monkeypatch.setattr(emit, "git", fake_git) assert emit.shared_default_flavor(".", "b", "h") == expected +def test_default_flavor_follows_the_ladder_into_llvm_profile(emit, monkeypatch): + # catches: the ladder read from lower_llvm.py only, which newer trees reduce to an import + # from rocke/core/llvm_profile.py, so every run fails with "no _LLVM_FLAVOR_LADDER" + files = { + emit.LOWER_LLVM: "from .llvm_profile import LLVM_FLAVORS, _LLVM_FLAVOR_LADDER\n", + emit.LLVM_PROFILE: LADDER_23, + } + + def fake_git(repo, cmd, *rest, **kw): + if cmd == "ls-tree": + return "\n".join(p for p in rest[-1:] if p in files) + return files[rest[0].split(":", 1)[1]] + + monkeypatch.setattr(emit, "git", fake_git) + assert emit.default_flavor(".", "rev") == "llvm23" + assert emit.shared_default_flavor(".", "b", "h") == "llvm23" + + def test_child_env_is_an_allow_list_that_pins_the_rocke_variables( emit, tmp_path, monkeypatch ): @@ -1098,7 +1118,7 @@ def test_sources_spec_key_order_does_not_split_a_variant(emit, tmp_path): [ {"a": {"kind": "rocke", "source": "s.py"}}, {"a": {"kind": "rocke", "builder": "build_x"}}, - {"a": {**KS, "kind": "hip"}}, + {"a": KS, "b": {**KS, "kind": "hip"}}, {"a": "not-an-object"}, {}, ["a"], @@ -1106,7 +1126,7 @@ def test_sources_spec_key_order_does_not_split_a_variant(emit, tmp_path): ids=[ "no-builder", "no-source", - "not-rocke", + "mixed-kinds", "entry-not-object", "empty", "not-object", @@ -1145,6 +1165,19 @@ def test_single_kernel_source_without_source_and_builder_is_still_skipped_silent assert variants == [] and skipped == [] +@pytest.mark.parametrize("kind", ["hip", "hsaco"]) +def test_multi_launch_ukd_of_another_kind_is_skipped_like_a_single_launch( + emit, tmp_path, kind +): + # catches: a hip or hsaco `sources` UKD, which the packer accepts and the emitter has + # nothing to build for, listed as not inspected, so no emit file could ever fold + launch = {"kind": kind, "file": "k.co", "symbol": "k"} + variants, skipped = discover( + emit, tmp_path, [{"name": kind, "sources": {"a": launch, "b": dict(launch)}}] + ) + assert variants == [] and skipped == [] + + ENUMERATOR_SOURCES = textwrap.dedent( """ import argparse, json From b2a5fda52625930876796b1356d97b94d907d3d5 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Wed, 4 Nov 2026 11:59:01 -0600 Subject: [PATCH 11/13] fix(hipdnn): keep ingestor impact folds and readers sound across targets The emitter proves that the base and the base plus the change emit the same code. A merged-view row folded whenever its own file in the merge equalled the head's, which says nothing about a target that changed a module the change imports. Such a row now folds only when the target changed no descriptor and no module of the shipped closure since the base. A class a reader imports through its package's __init__ re-export now resolves to the module that defines it, so a member removed from that class while the reader still uses it is a mismatch again rather than a read of some other class. A launch-surface helper's reach also follows module aliases, so code it calls as m.f through import a.b as m or from a import b as m is compared with it. The plugin SDK include closure follows quoted includes beside the including header, and an emitter row for a generic-target family names the member architectures it was built for. ALMIOPEN-2834 --- .../ai/skills/hipdnn-deep-review/RUNBOOK.md | 5 +- .../hipdnn-ingestor-impact/reference.md | 16 ++- .../hipdnn-ingestor-impact/scripts/impact.py | 124 +++++++++++++----- .../tools/ai/tests/test_ingestor_impact.py | 97 ++++++++++++++ 4 files changed, 200 insertions(+), 42 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md index 69c135e7b553..80a6dfeb77ba 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md @@ -355,8 +355,9 @@ Findings whose `Where` is in `affected.txt`, and every `process` finding, are af each lane the delta touches, every lane `FAILED` earlier (closing its `ORCH` finding when it completes), and always `AUTH` and `LEAK`: a new head brings new commits, PR text and CI runs. Re-run `DRIFT` whenever the round's `drift.txt` says `Trigger: yes`: the target moves between rounds. -Re-run `IMPACT` whenever the target tip moved since its last run, on a new head or the same one: -its merged view is computed against that tip and goes stale with it. +Re-run `IMPACT` whenever the round's pinned `TARGET_TIP` differs from the tip of its last run: its +merged view is computed against that tip and goes stale with it. Only *Pin and intake* records a +tip: a round reopened on the same head keeps its pinned tip, so its `IMPACT` re-run does too. Re-verify every affected `OPEN` or `UNVERIFIED` finding. A lane not re-run carries forward (Coverage and Lanes rows copied, marked `carried from r`). diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md index 47add2be478b..966b27b9ae90 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/reference.md @@ -19,7 +19,7 @@ change that breaks them leaves the default build green. | Applicability | Python spec validity and constants (scale range, tile legality) | native pack matchers and scores | | Shipped variants | `*.kdp.json` (DVC-held for gfx950; the emitter regenerates its variant list with the generator's registry enumerator, so a changed DVC pointer stays `changed`: the file's other fields are not read) | loader, native packs | | Generic targets | `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` and its CMake template `projects/hipdnn/plugin_sdk/cmake/GpuGenericTargetsTable.hpp.in` | packer and loader | -| Loader code | the ingestor SDK headers under `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, and every plugin SDK header they include (`ArchMatch.hpp`, `heuristics/uhd/`; tagged `sdk-loader`) | the native packs and the loader that compile them | +| Loader code | the ingestor SDK headers under `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, and every plugin SDK header they include, by `` or by a quoted path beside the including header (`ArchMatch.hpp`, `heuristics/uhd/`; tagged `sdk-loader`) | the native packs and the loader that compile them | | Python symbols | rocKE and `hkp_pack` modules | other modules and tests that import them | Test code, CMake, CI test-category lists and `requirements*.txt` are not judged here: they are @@ -35,8 +35,10 @@ How the copies are paired: no symbol, and the whole module is its Python side. - A host-side surface function (a grid or block helper: anything but the builder the packer compiles, or a `kernargs` entry) is compared together with every top-level def, class and - assignment it reaches by name, followed through `from` imports. A grid that hands off to - `NONPERSIST_DECODES[...].grid` changes when that class does, and no emitted IR covers it. + assignment it reaches by name, followed through `from` imports and module aliases (`import a.b + as m`, then `m.f`). A grid that hands off to `NONPERSIST_DECODES[...].grid` changes when that + class does, and no emitted IR covers it. A call through an argument (`spec.grid()` on an + un-annotated `spec`) is not followed: see *Known gaps*. - A declared `kernargs` pair compares each static C++ list in its `cpp_mirror` with the builder whose `b.param` list it matches, searched in the surface's module and the manifest's own `source` module, the declared function first. A multi-launch pack holds one list per launch (a @@ -127,7 +129,9 @@ under `not_inspected`. `impact.py report --emit` folds the result into the rows. - An all-equal emit folds only the rows of modules a built family imports, and never a row on code that runs after lowering (`rocke/helpers/compile.py`, `rocke/runtime/`): the IR digest does not cover it. A static kernarg `mismatch` is never downgraded. In the merged view it folds only the - rows of modules whose merged content is the head's: the emitter never saw the target's. + rows of modules whose merged content is the head's, and only when the target changed no + descriptor and no module the shipped code imports since the base: the emitter compared the base + with the change, and a target that moved what the change builds on makes another program. ## Predicate tables @@ -277,6 +281,10 @@ a flags-ON build when one exists), the architecture, and the flags. ## Known gaps +- A host-side surface function is compared with the code it reaches by name and through module + aliases, not with a method called on an argument whose class the code does not name + (`spec.grid()` on an un-annotated `spec`). An edit to such a method can still fold to `NONE` + on an all-equal emit: read the surface function's callees when its row folds. - HIP-kind kernels (`ConvNative`, `PointwiseNative`) have their C++ argument lists checked but not their HIP producers. - A dynamic C++ signature (`Gfx1151WmmaAttentionNative`) cannot be compared statically. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 99c2dbe2cc40..389de22e8944 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -49,7 +49,8 @@ failed, nothing not inspected) the rows that were `changed` only because a shipped module changed become `inert`, but only on modules a family with built variants imports, and never on code that runs after lowering (COMPILE_STEP) or on a DVC pointer. In the merged view the fold applies only to -rows of modules whose merged blob is the head's. Static kernarg mismatch rows are never +rows of modules whose merged blob is the head's, and only while the target changed no descriptor +and no module of the shipped closure since the base. Static kernarg mismatch rows are never downgraded. A module that the packer reaches ships on every architecture. The emit file's predicate tables (a predicate that accepts a spec its builder raises on, or a value outside the sign range of every shipped spec) are pack-time facts: `report` lists them in their own section, @@ -62,6 +63,7 @@ import fnmatch import hashlib import json +import posixpath import re import subprocess import sys @@ -448,15 +450,14 @@ def collect_changes(before: Tree, after: Tree) -> list: return sorted(changes, key=lambda c: c.path) -_SDK_INCLUDE = re.compile( - r'^[ \t]*#[ \t]*include[ \t]*[<"](hipdnn_plugin_sdk/[^>"]+)[>"]', re.M -) +_SDK_INCLUDE = re.compile(r'^[ \t]*#[ \t]*include[ \t]*([<"])([^>"]+)[>"]', re.M) def loader_headers(tree: Tree) -> set: """SDK files compiled into the descriptor loader: the include closure, inside the plugin - SDK, of the ingestor SDK headers (`DescriptorLoader.hpp` among them). A header CMake - configures stands for its `cmake/.in` template.""" + SDK, of the ingestor SDK headers (`DescriptorLoader.hpp` among them): `` from the SDK's include root, and a quoted include also beside the including header. A + header CMake configures stands for its `cmake/.in` template.""" include = SDK + "include/" paths = tree.paths seen = set() @@ -464,9 +465,18 @@ def loader_headers(tree: Tree) -> set: while frontier: seen |= set(frontier) found = set() - for data in tree.read(frontier).values(): + for path, data in tree.read(frontier).items(): for match in _SDK_INCLUDE.finditer(strip_c_comments(decode(data) or "")): - target = include + match.group(1) + quote, name = match.groups() + beside = posixpath.normpath( + posixpath.join(posixpath.dirname(path), name) + ) + if quote == '"' and beside in paths: + found.add(beside) + continue + if not name.startswith("hipdnn_plugin_sdk/"): + continue + target = include + name template = f"{SDK}cmake/{PurePosixPath(target).name}.in" found.add( target if target in paths or template not in paths else template @@ -2769,19 +2779,18 @@ def symbol_rows(ctx: Ctx) -> list: return rows -def binds_elsewhere(path: str, tree, defining: str, name: str) -> bool: - """Whether `path` binds `name` only from somewhere other than `defining`: an import from - another module, or its own def or class. Its reads of `name` are then not of `defining`'s. - """ +def binds_elsewhere( + tree: Tree, known: list, path: str, defining: str, name: str +) -> bool: + """Whether the class `name` that `path` reads is another module's than `defining`'s: its own + class of that name, or one imported from elsewhere, followed through package `__init__` + re-exports (`from rocke.dispatch.tuning import X` reaches the module defining X). Its + `.member` reads are then not of `defining`'s class. A name that does not resolve (the class + was removed, or comes from outside the repository) is not elsewhere.""" if path == defining: return False - imports = [(stem, imp) for b, stem, imp, _ in iter_imports(path, tree) if b == name] - if any(imp is not None and in_module(defining, stem) for stem, imp in imports): - return False - return bool(imports) or any( - isinstance(n, (*DEFINITION_FUNCS, ast.ClassDef)) and n.name == name - for n in ast.walk(tree) - ) + found = resolve_class(tree, known, path, name) + return found is not None and found[0] != defining def reader_rows(ctx: Ctx, cands: list) -> list: @@ -2807,6 +2816,7 @@ def reader_rows(ctx: Ctx, cands: list) -> list: ) ctx.after.read(mentioning) rows = [] + known = sorted(p for p in ctx.after.paths if p.endswith(".py")) for ch, qual, entry, kind, new_entry in cands: name, is_method = qual.split(".")[-1], entry[3] own_hunks = [ @@ -2829,7 +2839,7 @@ def reader_rows(ctx: Ctx, cands: list) -> list: ] else: if is_method and binds_elsewhere( - path, tree, ch.path, qual.split(".")[0] + ctx.after, known, path, ch.path, qual.split(".")[0] ): # The file's class of that name comes from another module (a moved class # keeps its name), so its `.name` reads are not reads of this one. @@ -2987,16 +2997,18 @@ def call_arity_rows(ctx: Ctx) -> list: def reached_dumps(tree: Tree, path: str, nodes) -> list: """Sorted dumps of `nodes` (defs of `path`) and of every top-level def, class and assignment - they reach by name, followed through `from` imports into other repo modules. A host-side + they reach by name, followed through `from` imports and through module aliases (`import + a.b as m` or `from a import b as m`, then `m.f`) into other repo modules. A host-side surface function (a grid or block helper) can hand its work to such code (`NONPERSIST_DECODES[...].grid`), and no emitted IR covers any of it.""" index, tops, seen, out = module_index(tree), {}, set(), [] def bindings(where): - """Name -> [node] of a module's top-level defs, classes and assignments, and name -> - (module, name) of its `from` imports.""" + """Name -> [node] of a module's top-level defs, classes and assignments, name -> + (module, name) of its `from` imports, and alias -> module of its module imports. + """ if where not in tops: - module, nodes, imports = tree.ast(where), {}, {} + module, nodes, imports, modules = tree.ast(where), {}, {}, {} for node in module.body if module is not None else (): if isinstance(node, (*DEFINITION_FUNCS, ast.ClassDef)): nodes.setdefault(node.name, []).append(node) @@ -3011,10 +3023,14 @@ def bindings(where): for bound, stem, imported, _ in ( iter_imports(where, module) if module is not None else () ): - found = index.get(stem) - if imported is not None and found: - imports[bound] = (found, imported) - tops[where] = nodes, imports + submodule = index.get(f"{stem}/{imported}") if imported else None + if submodule: # `from a import b as m`, b a module + modules[bound] = submodule + elif imported is None and index.get(stem): # `import a.b as m` + modules[bound] = index[stem] + if imported is not None and index.get(stem): + imports[bound] = (index[stem], imported) + tops[where] = nodes, imports, modules return tops[where] work = [(path, node) for node in nodes] @@ -3027,13 +3043,21 @@ def bindings(where): out.append(f"{where}: {ast.dump(without_docstrings(copy.deepcopy(node)))}") names = {n.id for n in ast.walk(node) if isinstance(n, ast.Name)} pending = [(where, name) for name in sorted(names)] + modules = bindings(where)[2] + pending += sorted( + (modules[n.value.id], n.attr) + for n in ast.walk(node) + if isinstance(n, ast.Attribute) + and isinstance(n.value, ast.Name) + and n.value.id in modules + ) resolved = set() while pending: module, name = pending.pop() if (module, name) in resolved: continue resolved.add((module, name)) - local, imports = bindings(module) + local, imports, _ = bindings(module) work += [(module, n) for n in local.get(name, ())] if name in imports: pending.append(imports[name]) @@ -3459,10 +3483,27 @@ def emit_source(ctx: Ctx, source: str) -> str: return source +def tip_moved_the_closure(ctx: Ctx, mctx: Ctx, covered: set) -> bool: + """Whether the target changed, between the base and its tip, a file the shipped code + imports (the covered modules and every module a root reaches, at the tip and in the + merge) or a descriptor. The emitter compared base with base plus the change; that proves + nothing about the tip plus the change once the tip moved what the change builds on. + """ + out = git(ctx.before.repo, "diff", "--name-only", "--no-renames", "-z", + ctx.before.oid, mctx.before.oid) # fmt: skip + moved = {p for p in out.decode("utf-8", errors="replace").split("\0") if p} + closure = set(covered) + for side in ("before", "after"): + closure |= set(mctx.closure(side).consumed) + return any(p in closure or p.startswith(DESC_DIR) for p in moved) + + def fold_emit(ctx: Ctx, rows: list, emit: dict, merged=None) -> list: """Apply impact_emit output to the base..head view's rows (see the module docstring). `merged`, (merged view Ctx, its rows), has the same fold applied in place to the rows of - modules whose merged blob is the head's: the emitter proved that content, nothing else. + modules whose merged blob is the head's, and only while the target left the shipped + closure as the base had it (`tip_moved_the_closure`): the emitter proved that, nothing + else. """ families = emit["families"] out = [] @@ -3472,6 +3513,15 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict, merged=None) -> list: covered = set() for fam in families: arch, builder = fam["arch"], fam["builder"] + # A generic-target family names the shard archs it compiled for, not the generic one. + members = fam.get("members") + devices = ( + ", ".join(sorted(members)) + if isinstance(members, list) + and members + and all(isinstance(m, str) for m in members) + else arch + ) quiet = not any( fam[k] for k in ( @@ -3545,7 +3595,7 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict, merged=None) -> list: p, e, label, why = bad[0] out.append(make_row("kernargs", p, e["line"], f"emit:{arch}:{builder}", "mismatch", f"emitted params of {label} ({arch}) disagree with static list {e['func']}(): {why}", - counter, arch=arch)) # fmt: skip + counter, arch=devices)) # fmt: skip else: note = ( f"consistent with {len(entries)} static C++ list(s)" @@ -3554,21 +3604,21 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict, merged=None) -> list: ) out.append(make_row("kernargs", source, 0, f"emit:{arch}:{builder}", "changed", f"emitted params of {builder} changed for {len(fam['params_changed'])} variant(s); {note}", - counter, arch=arch)) # fmt: skip + counter, arch=devices)) # fmt: skip elif fam.get("params_changed"): out.append(make_row("kernargs", source, 0, f"emit:{arch}:{builder}", "changed", - f"emitted params of {builder} changed for {len(fam['params_changed'])} variant(s); no param list recorded", arch=arch)) # fmt: skip + f"emitted params of {builder} changed for {len(fam['params_changed'])} variant(s); no param list recorded", arch=devices)) # fmt: skip if fam.get("ir_changed"): out.append(make_row("kernargs", source, 0, f"emit-ir:{arch}:{builder}", "changed", - f"emitted IR of {builder} changed for {len(fam['ir_changed'])} variant(s)", arch=arch)) # fmt: skip + f"emitted IR of {builder} changed for {len(fam['ir_changed'])} variant(s)", arch=devices)) # fmt: skip if fam["comgr_changed"]: out.append(make_row("kernargs", source, 0, f"emit-comgr:{arch}:{builder}", "changed", f"COMGR inputs of {builder} (ISA, codegen options or fresh-process compile) changed for " - f"{len(fam['comgr_changed'])} variant(s): the code object can differ where the IR does not", arch=arch)) # fmt: skip + f"{len(fam['comgr_changed'])} variant(s): the code object can differ where the IR does not", arch=devices)) # fmt: skip failed = {side: v for side, v in failures.items() if v} if failed: out.append(make_row("kernargs", source, 0, f"emit-fail:{arch}:{builder}", "changed", - "variants failed to build: " + "; ".join(f"{s}: {len(v)}" for s, v in sorted(failed.items())), arch=arch)) # fmt: skip + "variants failed to build: " + "; ".join(f"{s}: {len(v)}" for s, v in sorted(failed.items())), arch=devices)) # fmt: skip for item in emit.get("not_inspected") or []: out.append(make_row("kernargs", item.get("kdp", ""), 0, f"emit-skip:{item.get('kdp')}:{item.get('side')}", "changed", f"not inspected ({item.get('side')}): {item.get('reason')}")) # fmt: skip @@ -3586,6 +3636,8 @@ def fold_emit(ctx: Ctx, rows: list, emit: dict, merged=None) -> list: ): row.update(fold) mctx, mrows = merged or (None, []) + if mctx is not None and tip_moved_the_closure(ctx, mctx, covered): + mrows = [] for row in mrows: head, there = ctx.changes.get(row["path"]), mctx.changes.get(row["path"]) if ( diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index ff40c936d597..92b49c3f743d 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -1782,3 +1782,100 @@ def test_only_a_rocke_catalog_ships_the_rocke_kernels_of_its_arch( if r["path"] == OTHER_KERNEL ] assert row["status"] == status + + +@needs_merge_tree +def test_merged_view_does_not_fold_when_the_target_moved_what_the_change_imports( + mod, capsys, tmp_path +): + # catches: a merged-view row folded because its own file equals the head's, although the + # target changed a module that file imports, so the emitter's base-vs-head proof says + # nothing about the merged program + helper = ROCKE_PY + "helpers/w.py" + use = "from rocke.helpers.w import W\n" + files = {BUILDER: use + BUILD, KDP: KDP_JSON, helper: "W = 32\n"} + repo, base = make_repo(tmp_path, files) + head = branch_commit(repo, base, {BUILDER: use + BODY_EDIT}, "feature") + tip = branch_commit(repo, base, {helper: "W = 64\n"}, "target") + emit = write_emit(tmp_path / "eq.json", repo, base, head) + data = report(mod, capsys, repo, base, head, "--target-tip", tip, "--emit", emit) + assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" + assert all(r["merged_only"] for r in rows_of(data, status="changed")) + + +def test_member_removed_from_a_class_read_through_its_package_is_mismatch( + mod, capsys, tmp_path +): + # catches: a reader importing the class from its package (`from hkp_pack.tuning import + # Space`, re-exported by the package __init__) taken for another module's class, so the + # removal of a member it still reads is not a break + pkg = HKP_PACK + "tuning/" + cls = "class Space:\n def keep(self):\n return 0\n\n def probe_knob(self):\n return 1\n" + reader = "from hkp_pack.tuning import Space\n\n\ndef f():\n return Space().probe_knob()\n" + files = {pkg + "__init__.py": "from .waves import Space\n", pkg + "waves.py": cls, + HKP_PACK + "user.py": reader} # fmt: skip + repo, base = make_repo(tmp_path, files) + head = commit( + repo, {pkg + "waves.py": cls.split("\n\n def probe_knob")[0] + "\n"} + ) + rows = rows_of(report(mod, capsys, repo, base, head), "symbols", "mismatch") + assert "Space.probe_knob" in {r["name"] for r in rows} + + +@pytest.mark.parametrize( + "imports, call", + [("import kernels.common.decode as dec\n", "dec.grid_of(spec)"), + ("from kernels.common import decode as dec\n", "dec.grid_of(spec)")], + ids=["import-as", "from-import-module"], +) # fmt: skip +def test_grid_surface_reaching_a_module_alias_is_not_folded_by_equal_ir( + mod, capsys, tmp_path, imports, call +): + # catches: a host-side surface's reach following bare names and `from m import f` only, + # so a helper called through a module alias (`m.f(spec)`) changes unseen and folds to NONE + decode = HKP + "rocke/library/kernels/common/decode.py" + helper = "def grid_of(spec):\n return (1, 2, 3)\n" + builder = imports + f"\n\ndef attn_grid(spec):\n return {call}\n\n\n" + BUILD + grid = manifest(("grid", "kernels/gfx950/attn.py:attn_grid", GEOM)) + files = { + BUILDER: builder, + decode: helper, + KDP: KDP_JSON, + GEOM: "int grid();\n", + MANIFEST: grid, + } + repo, base = make_repo(tmp_path, files) + head = commit(repo, {decode: helper.replace("(1, 2, 3)", "(2, 1, 3)")}) + emit = write_emit(tmp_path / "eq.json", repo, base, head) + data = report(mod, capsys, repo, base, head, "--emit", emit) + (row,) = rows_of(data, "surfaces") + assert (row["name"], row["status"]) == ("grid:attn_grid", "changed") + + +def test_sdk_header_a_loader_header_includes_by_quoted_path_is_on_the_surface( + mod, capsys, tmp_path +): + # catches: only `` includes followed, so a header a loader header + # pulls in by a quoted path beside it (`#include "IUhdAdapter.hpp"`) reads N/A + uhd = SDK_INC + "heuristics/uhd/" + body = "inline int f() {{ return {}; }}\n" + files = {SDK_INC + "ingestor/DescriptorLoader.hpp": "#include \n", + uhd + "A.hpp": '#include "adapters/B.hpp"\n', uhd + "adapters/B.hpp": body.format(1)} # fmt: skip + repo, base = make_repo(tmp_path, files) + head = commit(repo, {uhd + "adapters/B.hpp": body.format(2)}) + data = report(mod, capsys, repo, base, head) + assert [r["status"] for r in rows_of(data, "file")] == ["changed"] + + +def test_emit_row_of_a_generic_target_family_names_its_member_archs( + mod, capsys, tmp_path +): + # catches: an emit row naming the generic compile target (gfx11-generic), which is no + # device a NEEDS-DEVICE run can be asked for + repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) + head = commit(repo, {BUILDER: BODY_EDIT}) + fam = family(arch="gfx11-generic", members=["gfx1151", "gfx1100"], ir_changed=["v"]) + data = report(mod, capsys, repo, base, head, "--emit", + write_emit(tmp_path / "g.json", repo, base, head, families=[fam])) # fmt: skip + (row,) = [r for r in rows_of(data, "kernargs") if r["name"].startswith("emit-ir:")] + assert row["arch"] == "gfx1100, gfx1151" From 3219facbbf5c0dc57d0ff3fe88c9422c9d048c34 Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Wed, 4 Nov 2026 17:35:25 -0600 Subject: [PATCH 12/13] fix(hipdnn): keep ingestor impact fallbacks and test the fold guard A reader's class is resolved through the repository first, but when it does not resolve to a repository class the old rules still decide: a name imported from another module (an external package included), or a def or class of that name defined in the file, is not the defining module's class. Dropping those rules read a member call on such a name as a removed member still in use. The emitter lists a descriptor whose kernel source is not an object, or whose kind, source, builder or target is not a string, under not_inspected instead of failing its child process on an unhashable family key. New test cases cover the merged-view fold guard for a target that edits only a descriptor, the generic-target member fallback on every emit row kind, and a quoted SDK include resolved from the include root. The deep-review runbook, the skill's trigger text and the emitter docstring now match the code. ALMIOPEN-2834 --- .../ai/skills/hipdnn-deep-review/RUNBOOK.md | 2 +- .../ai/skills/hipdnn-ingestor-impact/SKILL.md | 3 +- .../hipdnn-ingestor-impact/scripts/impact.py | 27 +++++-- .../scripts/impact_emit.py | 35 ++++++-- .../ai/tests/test_deep_review_partition.py | 2 +- .../tools/ai/tests/test_ingestor_impact.py | 81 +++++++++++++++---- .../ai/tests/test_ingestor_impact_emit.py | 30 ++++++- 7 files changed, 146 insertions(+), 34 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md index 80a6dfeb77ba..7ae09176aace 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-deep-review/RUNBOOK.md @@ -151,7 +151,7 @@ its trigger holds, else mark it `N/A`; when unsure, run it. | `DOCS` | [docs](lanes/docs.md) | Accuracy and concision of docs and comments, including edited public headers | A documentation or source file changes | | `DRIFT` | [drift](lanes/drift.md) | The change merged onto the target's current tip: conflicts, broken target-side contracts, tests on the merge | `drift.txt` says `Trigger: yes` | | `RUN` | [build-validate](lanes/build-validate.md) | Building the head and running the relevant tests | Always | -| `IMPACT` | [runtime-impact](lanes/runtime-impact.md) | Kernel ABI and dispatch contracts of the kernel ingestor (kernarg lists, launch surfaces, matchers, descriptors, generic targets) at base, head and the merged tree; which shipped variants change and which device runs would settle it | A file under `dnn-providers/hip-kernel-provider/`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/`, `projects/hipdnn/tools/IngestorGenerator/`, `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` or `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/GpuGenericTargets*` changes | +| `IMPACT` | [runtime-impact](lanes/runtime-impact.md) | Kernel ABI and dispatch contracts of the kernel ingestor (kernarg lists, launch surfaces, matchers, descriptors, generic targets) at base, head and the merged tree; which shipped variants change and which device runs would settle it | A file under `dnn-providers/hip-kernel-provider/`, `projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/` (any plugin SDK header: the report works out which ones the descriptor loader includes), `projects/hipdnn/tools/IngestorGenerator/`, `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` or `projects/hipdnn/plugin_sdk/cmake/GpuGenericTargets*` changes | `ORCH` (process findings you file), `SCOUT`, `SYNTH` ([orient](lanes/orient.md)) and `VER` ([verify](lanes/verify.md)) are not partition choices. Merge two lanes into one agent when the diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md index 16204f36c164..c30797fc5a5b 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/SKILL.md @@ -8,7 +8,8 @@ allowed-tools: Bash, Read, Grep, Glob # hipDNN Ingestor Impact Use this skill when the user asks whether a change can break the kernel ingestor at run time, and -suggest it for any change under `dnn-providers/hip-kernel-provider/`, the SDK ingestor headers, +suggest it for any change under `dnn-providers/hip-kernel-provider/`, any plugin SDK header +(`projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/`, `projects/hipdnn/plugin_sdk/cmake/GpuGenericTargets*`), `projects/hipdnn/plugin_sdk/data/gpu_generic_targets.json` or `projects/hipdnn/tools/IngestorGenerator/`. [reference.md](reference.md) holds the contracts, row statuses, emitter rules and known gaps; read it before judging rows. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 389de22e8944..555853cbb5bf 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -2780,17 +2780,28 @@ def symbol_rows(ctx: Ctx) -> list: def binds_elsewhere( - tree: Tree, known: list, path: str, defining: str, name: str + tree: Tree, known: list, path: str, module, defining: str, name: str ) -> bool: - """Whether the class `name` that `path` reads is another module's than `defining`'s: its own - class of that name, or one imported from elsewhere, followed through package `__init__` - re-exports (`from rocke.dispatch.tuning import X` reaches the module defining X). Its - `.member` reads are then not of `defining`'s class. A name that does not resolve (the class - was removed, or comes from outside the repository) is not elsewhere.""" + """Whether the class `name` that `path` (parsed as `module`) reads is another than + `defining`'s. When the name resolves to a repository class, followed through package + `__init__` re-exports (`from rocke.dispatch.tuning import X` reaches the module defining X), + that class decides. Otherwise the file binds it elsewhere when it imports it from another + module (an external package included) or defines a def or class of that name anywhere, + nested ones included. Its `.member` reads are then not of `defining`'s class.""" if path == defining: return False found = resolve_class(tree, known, path, name) - return found is not None and found[0] != defining + if found is not None: + return found[0] != defining + imports = [ + (stem, imp) for b, stem, imp, _ in iter_imports(path, module) if b == name + ] + if any(imp is not None and in_module(defining, stem) for stem, imp in imports): + return False + return bool(imports) or any( + isinstance(n, (*DEFINITION_FUNCS, ast.ClassDef)) and n.name == name + for n in ast.walk(module) + ) def reader_rows(ctx: Ctx, cands: list) -> list: @@ -2839,7 +2850,7 @@ def reader_rows(ctx: Ctx, cands: list) -> list: ] else: if is_method and binds_elsewhere( - ctx.after, known, path, ch.path, qual.split(".")[0] + ctx.after, known, path, tree, ch.path, qual.split(".")[0] ): # The file's class of that name comes from another module (a moved class # keeps its name), so its `.name` reads are not reads of this one. diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index 4876fe07b8ba..eca9b8554e1e 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -20,8 +20,10 @@ `kernelDescriptors[].kernel_source` names a rocKE `source` + `builder`. A UKD with `sources` (several launches) yields one variant per distinct (source, builder, spec, target) of its launches, as hkp_pack compiles them: the UKD `name` when there is one, else -`@[+...]` with the group's launch names sorted. A `sources` UKD -or entry that cannot be read (not rocke, no source or builder, both `kernel_source` and +`@[+...]` with the group's launch names sorted. A UKD whose launches all +share one other packer kind (hip, hsaco, kpack, embedded_source) has no rocKE build: it gives no +variant and is not listed, as a single hip launch is not. A UKD that cannot be read (launches that +mix kinds, no source or builder, a field that is not a string, both `kernel_source` and `sources`) is listed under `not_inspected`. A KDP held by DVC (`artifacts.dvc`, no `.kdp.json`) is regenerated with that side's `IngestorGenerator/tools/registry_enumerator.py`; when neither works the KDP is listed @@ -339,8 +341,8 @@ def _ukd_variants(d, arches, kdp, origin): spec, target); each group is one variant, named by the descriptor when there is one group, else `@[+...]` with the launch names sorted. A `sources` UKD whose launches all share one other packer kind (hip, hsaco) has no rocKE build and gives none, - as a single hip launch does. REASON, set when the descriptor cannot be read, is for - `not_inspected` and the variants are then empty. + as a single hip launch does. REASON, set when the descriptor cannot be read (a field that is + not of its JSON type included), is for `not_inspected` and the variants are then empty. """ has_ks, has_sources = "kernel_source" in d, "sources" in d name = d.get("name") or d.get("id") or "?" @@ -351,7 +353,8 @@ def _ukd_variants(d, arches, kdp, origin): if not isinstance(entries, dict) or not entries: return [], f"{name}: `sources` is not a non-empty object" kinds = { - ks.get("kind") if isinstance(ks, dict) else None for ks in entries.values() + ks.get("kind") if isinstance(ks, dict) and _is_str(ks.get("kind")) else None + for ks in entries.values() } if len(kinds) == 1 and kinds & _OTHER_KINDS: return [], None @@ -369,6 +372,13 @@ def _ukd_variants(d, arches, kdp, origin): [], f"{name}: sources entry `{launch}` lacks `source` or `builder`", ) + if not all( + _is_str(ks.get(k, "native")) for k in ("source", "builder", "target") + ): + return ( + [], + f"{name}: sources entry `{launch}` has a `source`, `builder` or `target` that is not a string", + ) key = ( ks["source"], ks["builder"], @@ -381,12 +391,25 @@ def _ukd_variants(d, arches, kdp, origin): vname = name if len(groups) == 1 else f"{name}@{'+'.join(sorted(launches))}" out.append(_variant(vname, arches, ks, kdp, origin)) return out, None - ks = d.get("kernel_source") or {} + ks = d.get("kernel_source") + if ks is None: + return [], None + if not isinstance(ks, dict): + return [], f"{name}: `kernel_source` is not an object" if not (ks.get("source") and ks.get("builder")): return [], None + if not all(_is_str(ks.get(k, "native")) for k in ("source", "builder", "target")): + return ( + [], + f"{name}: `kernel_source` has a `source`, `builder` or `target` that is not a string", + ) return [_variant(d["name"], arches, ks, kdp, origin)], None +def _is_str(value) -> bool: + return isinstance(value, str) + + def _variant(name, arches, ks, kdp, origin): return dict( name=name, diff --git a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py index ddd22c1d1dda..9260a49146df 100644 --- a/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py +++ b/projects/hipdnn/tools/ai/tests/test_deep_review_partition.py @@ -476,7 +476,7 @@ def test_packets_drift_lane_follows_the_trigger(part, tmp_path): assert "drift.txt" in (rd / "packets" / "DRIFT.md").read_text() -def test_impact_scope_is_the_ingestor_path_only(part): +def test_impact_scope_is_the_ingestor_paths_and_plugin_sdk_headers(part): inside = ( "dnn-providers/hip-kernel-provider/rocke/library/kernels/gfx950/attention_dense.py", "projects/hipdnn/plugin_sdk/include/hipdnn_plugin_sdk/ingestor/Descriptors.hpp", diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index 92b49c3f743d..80bf60c696f0 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -1784,19 +1784,27 @@ def test_only_a_rocke_catalog_ships_the_rocke_kernels_of_its_arch( assert row["status"] == status +MERGE_HELPER = ROCKE_PY + "helpers/w.py" + + @needs_merge_tree +@pytest.mark.parametrize( + "target", + [{MERGE_HELPER: "W = 64\n"}, + {KDP: KDP_JSON.replace('"name": "v"', '"name": "v2"')}], + ids=["imported-module", "descriptor"], +) # fmt: skip def test_merged_view_does_not_fold_when_the_target_moved_what_the_change_imports( - mod, capsys, tmp_path + mod, capsys, tmp_path, target ): # catches: a merged-view row folded because its own file equals the head's, although the - # target changed a module that file imports, so the emitter's base-vs-head proof says - # nothing about the merged program - helper = ROCKE_PY + "helpers/w.py" + # target changed a module that file imports, or a descriptor (the variant set the emitter + # built), so the emitter's base-vs-head proof says nothing about the merged program use = "from rocke.helpers.w import W\n" - files = {BUILDER: use + BUILD, KDP: KDP_JSON, helper: "W = 32\n"} + files = {BUILDER: use + BUILD, KDP: KDP_JSON, MERGE_HELPER: "W = 32\n"} repo, base = make_repo(tmp_path, files) head = branch_commit(repo, base, {BUILDER: use + BODY_EDIT}, "feature") - tip = branch_commit(repo, base, {helper: "W = 64\n"}, "target") + tip = branch_commit(repo, base, target, "target") emit = write_emit(tmp_path / "eq.json", repo, base, head) data = report(mod, capsys, repo, base, head, "--target-tip", tip, "--emit", emit) assert data["rollup"] == "Runtime impact: NEEDS-DEVICE gfx950" @@ -1852,30 +1860,71 @@ def test_grid_surface_reaching_a_module_alias_is_not_folded_by_equal_ir( assert (row["name"], row["status"]) == ("grid:attn_grid", "changed") +@pytest.mark.parametrize( + "include, included", + [('#include "adapters/B.hpp"\n', "heuristics/uhd/adapters/B.hpp"), + ('#include "hipdnn_plugin_sdk/ArchMatch.hpp"\n', "ArchMatch.hpp")], + ids=["beside-the-header", "from-the-sdk-root"], +) # fmt: skip def test_sdk_header_a_loader_header_includes_by_quoted_path_is_on_the_surface( - mod, capsys, tmp_path + mod, capsys, tmp_path, include, included ): # catches: only `` includes followed, so a header a loader header - # pulls in by a quoted path beside it (`#include "IUhdAdapter.hpp"`) reads N/A + # pulls in by a quoted path beside it (`#include "IUhdAdapter.hpp"`) reads N/A; or a quoted + # include that is not beside the header no longer resolved from the SDK's include root uhd = SDK_INC + "heuristics/uhd/" body = "inline int f() {{ return {}; }}\n" files = {SDK_INC + "ingestor/DescriptorLoader.hpp": "#include \n", - uhd + "A.hpp": '#include "adapters/B.hpp"\n', uhd + "adapters/B.hpp": body.format(1)} # fmt: skip + uhd + "A.hpp": include, SDK_INC + included: body.format(1)} # fmt: skip repo, base = make_repo(tmp_path, files) - head = commit(repo, {uhd + "adapters/B.hpp": body.format(2)}) + head = commit(repo, {SDK_INC + included: body.format(2)}) data = report(mod, capsys, repo, base, head) assert [r["status"] for r in rows_of(data, "file")] == ["changed"] +@pytest.mark.parametrize( + "members, arch", + [(["gfx1151", "gfx1100"], "gfx1100, gfx1151"), (None, "gfx11-generic"), + ([], "gfx11-generic"), (["gfx1100", 3], "gfx11-generic")], + ids=["members", "no-members", "empty-members", "non-string-member"], +) # fmt: skip def test_emit_row_of_a_generic_target_family_names_its_member_archs( - mod, capsys, tmp_path + mod, capsys, tmp_path, members, arch ): # catches: an emit row naming the generic compile target (gfx11-generic), which is no - # device a NEEDS-DEVICE run can be asked for + # device a NEEDS-DEVICE run can be asked for, on any emit row kind; or a family whose + # `members` is missing or malformed losing its arch instead of keeping the generic one repo, base = make_repo(tmp_path, {BUILDER: BUILD, KDP: KDP_JSON}) head = commit(repo, {BUILDER: BODY_EDIT}) - fam = family(arch="gfx11-generic", members=["gfx1151", "gfx1100"], ir_changed=["v"]) + fields = dict(arch="gfx11-generic", ir_changed=["v"], comgr_changed=["v"], + failures={"base": {}, "head": {"v": "boom"}}) # fmt: skip + if members is not None: + fields["members"] = members data = report(mod, capsys, repo, base, head, "--emit", - write_emit(tmp_path / "g.json", repo, base, head, families=[fam])) # fmt: skip - (row,) = [r for r in rows_of(data, "kernargs") if r["name"].startswith("emit-ir:")] - assert row["arch"] == "gfx1100, gfx1151" + write_emit(tmp_path / "g.json", repo, base, head, families=[family(**fields)])) # fmt: skip + rows = {r["name"].split(":")[0]: r["arch"] for r in rows_of(data, "kernargs")} + assert rows == {"emit-ir": arch, "emit-comgr": arch, "emit-fail": arch} + + +@pytest.mark.parametrize( + "reader", + ["from external_pkg import Space\n\n\ndef f():\n return Space().probe_knob()\n", + "def f():\n class Space:\n def probe_knob(self):\n return 2\n\n return Space().probe_knob()\n", + "def Space():\n return object()\n\n\ndef f():\n return Space().probe_knob()\n", + "class Space:\n def probe_knob(self):\n return 2\n\n\ndef f():\n return Space().probe_knob()\n"], + ids=["external-import", "nested-class", "same-named-def", "own-class"], +) # fmt: skip +def test_member_read_of_another_class_of_the_same_name_is_not_a_removal_reader( + mod, capsys, tmp_path, reader +): + # catches: only a repository class the import chain resolves to counted as "another + # class", so a reader whose `Space` comes from an external package, a nested class or a + # same-named def reads `Space.probe_knob is removed but still referenced` (false BREAKS) + cls = "class Space:\n def keep(self):\n return 0\n\n def probe_knob(self):\n return 1\n" + files = {HKP_PACK + "tuning.py": cls, HKP_PACK + "user.py": reader} + repo, base = make_repo(tmp_path, files) + head = commit( + repo, {HKP_PACK + "tuning.py": cls.split("\n\n def probe_knob")[0] + "\n"} + ) + rows = rows_of(report(mod, capsys, repo, base, head), "symbols", "mismatch") + assert not [r for r in rows if r["name"] == "Space.probe_knob"] diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index 991c7bcc32ee..941b2ec847df 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -1122,6 +1122,10 @@ def test_sources_spec_key_order_does_not_split_a_variant(emit, tmp_path): {"a": "not-an-object"}, {}, ["a"], + {"a": {**KS, "kind": ["rocke"]}}, + {"a": {**KS, "source": ["s.py"]}}, + {"a": {**KS, "builder": {"name": "build_x"}}}, + {"a": {**KS, "target": ["generic"]}}, ], ids=[ "no-builder", @@ -1130,12 +1134,18 @@ def test_sources_spec_key_order_does_not_split_a_variant(emit, tmp_path): "entry-not-object", "empty", "not-object", + "kind-list", + "source-list", + "builder-object", + "target-list", ], ) def test_unreadable_sources_ukd_is_not_inspected_and_yields_no_variant( emit, tmp_path, sources ): - # catches: a `sources` UKD or entry the emitter cannot read dropped silently + # catches: a `sources` UKD or entry the emitter cannot read dropped silently, or one whose + # kind, source, builder or target is not a string crashing the child (unhashable key) + # instead of being listed good = {"name": "ok", "kernel_source": KS} variants, skipped = discover( emit, tmp_path, [{"name": "bad", "sources": sources}, good] @@ -1165,6 +1175,24 @@ def test_single_kernel_source_without_source_and_builder_is_still_skipped_silent assert variants == [] and skipped == [] +@pytest.mark.parametrize( + "kernel_source", + [{**KS, "source": ["s.py"]}, {**KS, "builder": {"name": "build_x"}}, + {**KS, "target": ["generic"]}, ["not", "an", "object"]], + ids=["source-list", "builder-object", "target-list", "not-object"], +) # fmt: skip +def test_malformed_single_kernel_source_is_not_inspected(emit, tmp_path, kernel_source): + # catches: a single launch whose source, builder or target is not a string, or a + # kernel_source that is not an object, crashing the child (the family key is unhashable) + # instead of being listed under not_inspected + good = {"name": "ok", "kernel_source": KS} + variants, skipped = discover( + emit, tmp_path, [{"name": "bad", "kernel_source": kernel_source}, good] + ) + assert [v["name"] for v in variants] == ["ok"] + assert [s["reason"].split(":")[0] for s in skipped] == ["bad"] + + @pytest.mark.parametrize("kind", ["hip", "hsaco"]) def test_multi_launch_ukd_of_another_kind_is_skipped_like_a_single_launch( emit, tmp_path, kind From bfc7f131d25e3227a9a607c7fe6876c95ad86f6f Mon Sep 17 00:00:00 2001 From: Garby Gilbert Date: Thu, 5 Nov 2026 10:31:34 -0600 Subject: [PATCH 13/13] fix(hipdnn): read unresolved repository imports as ingestor readers When a reader's class does not resolve through the repository, only an import from outside the repository now proves it is another class. An import from a repository package whose export the resolver cannot follow (an alias such as `Space = waves.Space`, or a lazy export) may still be the defining module's class, so its read of a removed member stays a break instead of being dropped. The emitter lists a descriptor whose `name` is not a string under not_inspected, where it used to fail its child process on an unhashable key. A single-launch descriptor without a `name` is named by its `id`, as a multi-launch one already was. The launch string check lives in one helper, the docstring lists every non-rocKE packer kind, and new tests cover a missing or null `kernel_source` and the kpack and embedded source kinds. ALMIOPEN-2834 --- .../hipdnn-ingestor-impact/scripts/impact.py | 12 ++- .../scripts/impact_emit.py | 81 +++++++++++-------- .../tools/ai/tests/test_ingestor_impact.py | 13 ++- .../ai/tests/test_ingestor_impact_emit.py | 37 +++++++-- 4 files changed, 98 insertions(+), 45 deletions(-) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py index 555853cbb5bf..f55a8d1ff414 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact.py @@ -2785,9 +2785,11 @@ def binds_elsewhere( """Whether the class `name` that `path` (parsed as `module`) reads is another than `defining`'s. When the name resolves to a repository class, followed through package `__init__` re-exports (`from rocke.dispatch.tuning import X` reaches the module defining X), - that class decides. Otherwise the file binds it elsewhere when it imports it from another - module (an external package included) or defines a def or class of that name anywhere, - nested ones included. Its `.member` reads are then not of `defining`'s class.""" + that class decides. Otherwise the file binds it elsewhere when it imports it from outside the + repository or defines a def or class of that name anywhere, nested ones included. An import + from a repository module whose chain does not resolve (`X = waves.X`, a lazy export) may + still be `defining`'s class, so it is not elsewhere. Its `.member` reads are then not of + `defining`'s class.""" if path == defining: return False found = resolve_class(tree, known, path, name) @@ -2798,7 +2800,9 @@ def binds_elsewhere( ] if any(imp is not None and in_module(defining, stem) for stem, imp in imports): return False - return bool(imports) or any( + if any(not any(in_module(p, stem) for p in known) for stem, _ in imports): + return True + return any( isinstance(n, (*DEFINITION_FUNCS, ast.ClassDef)) and n.name == name for n in ast.walk(module) ) diff --git a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py index eca9b8554e1e..fd8805222083 100644 --- a/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py +++ b/projects/hipdnn/tools/ai/skills/hipdnn-ingestor-impact/scripts/impact_emit.py @@ -334,15 +334,28 @@ def _spec_json(spec): return json.dumps(spec, sort_keys=True, default=str) +def _non_string_fields(ks) -> bool: + """Whether a launch's `source`, `builder` or `target` is not a string: it cannot be built, + and as part of a family key it would fail the child (an unhashable list or object). + """ + return not all( + isinstance(ks.get(k, "native"), str) for k in ("source", "builder", "target") + ) + + +_NON_STRING = "has a `source`, `builder` or `target` that is not a string" + + def _ukd_variants(d, arches, kdp, origin): """(variants, reason) of one descriptor dict, as hkp_pack enumerates it. A single `kernel_source` is one variant named by the descriptor (none when it lacks source and builder, e.g. a hip kernel). A `sources` UKD groups its launches by (source, builder, spec, target); each group is one variant, named by the descriptor when there is one group, else `@[+...]` with the launch names sorted. A `sources` UKD whose - launches all share one other packer kind (hip, hsaco) has no rocKE build and gives none, - as a single hip launch does. REASON, set when the descriptor cannot be read (a field that is - not of its JSON type included), is for `not_inspected` and the variants are then empty. + launches all share one other packer kind (hip, hsaco, kpack, embedded_source) has no rocKE + build and gives none, as a single hip launch does. The name is the descriptor's `name`, else + its `id`. REASON, set when the descriptor cannot be read (a field that is not of its JSON + type included), is for `not_inspected` and the variants are then empty. """ has_ks, has_sources = "kernel_source" in d, "sources" in d name = d.get("name") or d.get("id") or "?" @@ -353,7 +366,11 @@ def _ukd_variants(d, arches, kdp, origin): if not isinstance(entries, dict) or not entries: return [], f"{name}: `sources` is not a non-empty object" kinds = { - ks.get("kind") if isinstance(ks, dict) and _is_str(ks.get("kind")) else None + ( + ks.get("kind") + if isinstance(ks, dict) and isinstance(ks.get("kind"), str) + else None + ) for ks in entries.values() } if len(kinds) == 1 and kinds & _OTHER_KINDS: @@ -372,13 +389,8 @@ def _ukd_variants(d, arches, kdp, origin): [], f"{name}: sources entry `{launch}` lacks `source` or `builder`", ) - if not all( - _is_str(ks.get(k, "native")) for k in ("source", "builder", "target") - ): - return ( - [], - f"{name}: sources entry `{launch}` has a `source`, `builder` or `target` that is not a string", - ) + if _non_string_fields(ks): + return [], f"{name}: sources entry `{launch}` {_NON_STRING}" key = ( ks["source"], ks["builder"], @@ -386,28 +398,33 @@ def _ukd_variants(d, arches, kdp, origin): ks.get("target", "native"), ) groups.setdefault(key, (ks, []))[1].append(launch) - out = [] - for ks, launches in groups.values(): - vname = name if len(groups) == 1 else f"{name}@{'+'.join(sorted(launches))}" - out.append(_variant(vname, arches, ks, kdp, origin)) - return out, None - ks = d.get("kernel_source") - if ks is None: - return [], None - if not isinstance(ks, dict): - return [], f"{name}: `kernel_source` is not an object" - if not (ks.get("source") and ks.get("builder")): - return [], None - if not all(_is_str(ks.get(k, "native")) for k in ("source", "builder", "target")): - return ( - [], - f"{name}: `kernel_source` has a `source`, `builder` or `target` that is not a string", + named = [ + (ks, sorted(launches) if len(groups) > 1 else None) + for ks, launches in groups.values() + ] + else: + ks = d.get("kernel_source") + if ks is None: + return [], None + if not isinstance(ks, dict): + return [], f"{name}: `kernel_source` is not an object" + if not (ks.get("source") and ks.get("builder")): + return [], None + if _non_string_fields(ks): + return [], f"{name}: `kernel_source` {_NON_STRING}" + named = [(ks, None)] + if not isinstance(name, str): + return [], f"{name}: the descriptor `name` is not a string" + return [ + _variant( + f"{name}@{'+'.join(launches)}" if launches else name, + arches, + ks, + kdp, + origin, ) - return [_variant(d["name"], arches, ks, kdp, origin)], None - - -def _is_str(value) -> bool: - return isinstance(value, str) + for ks, launches in named + ], None def _variant(name, arches, ks, kdp, origin): diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py index 80bf60c696f0..eaac18870385 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact.py @@ -1811,16 +1811,23 @@ def test_merged_view_does_not_fold_when_the_target_moved_what_the_change_imports assert all(r["merged_only"] for r in rows_of(data, status="changed")) +@pytest.mark.parametrize( + "export", + ["from .waves import Space\n", "from . import waves\n\nSpace = waves.Space\n"], + ids=["re-export", "unresolved-alias"], +) def test_member_removed_from_a_class_read_through_its_package_is_mismatch( - mod, capsys, tmp_path + mod, capsys, tmp_path, export ): # catches: a reader importing the class from its package (`from hkp_pack.tuning import # Space`, re-exported by the package __init__) taken for another module's class, so the - # removal of a member it still reads is not a break + # removal of a member it still reads is not a break; also when the package's export is + # one the resolver cannot follow (`Space = waves.Space`), which is still a repository + # import and so not proof of another class pkg = HKP_PACK + "tuning/" cls = "class Space:\n def keep(self):\n return 0\n\n def probe_knob(self):\n return 1\n" reader = "from hkp_pack.tuning import Space\n\n\ndef f():\n return Space().probe_knob()\n" - files = {pkg + "__init__.py": "from .waves import Space\n", pkg + "waves.py": cls, + files = {pkg + "__init__.py": export, pkg + "waves.py": cls, HKP_PACK + "user.py": reader} # fmt: skip repo, base = make_repo(tmp_path, files) head = commit( diff --git a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py index 941b2ec847df..0fff83aa6e33 100644 --- a/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py +++ b/projects/hipdnn/tools/ai/tests/test_ingestor_impact_emit.py @@ -1164,15 +1164,40 @@ def test_ukd_with_both_kernel_source_and_sources_is_not_inspected(emit, tmp_path assert [s["reason"].split(":")[0] for s in skipped] == ["both"] +@pytest.mark.parametrize( + "ukd", + [{"name": "hip", "kernel_source": {"kind": "hip", "file": "k.hip"}}, + {"name": "plain"}, {"name": "nulled", "kernel_source": None}], + ids=["hip", "no-kernel-source", "null-kernel-source"], +) # fmt: skip def test_single_kernel_source_without_source_and_builder_is_still_skipped_silently( - emit, tmp_path + emit, tmp_path, ukd ): + variants, skipped = discover(emit, tmp_path, [ukd]) + assert variants == [] and skipped == [] + + +@pytest.mark.parametrize( + "ukd", + [{"name": ["bad"], "kernel_source": KS}, {"name": {"bad": 1}, "sources": {"a": KS}}], + ids=["single-list-name", "sources-object-name"], +) # fmt: skip +def test_descriptor_whose_name_is_not_a_string_is_not_inspected(emit, tmp_path, ukd): + # catches: a list or object `name` passing the launch checks and failing the child, where + # it keys the specs by variant name (unhashable), instead of being listed variants, skipped = discover( - emit, - tmp_path, - [{"name": "hip", "kernel_source": {"kind": "hip", "file": "k.hip"}}], + emit, tmp_path, [ukd, {"name": "ok", "kernel_source": KS}] ) - assert variants == [] and skipped == [] + assert [v["name"] for v in variants] == ["ok"] + assert len(skipped) == 1 + assert skipped[0]["reason"].endswith("the descriptor `name` is not a string") + + +def test_single_kernel_source_without_a_name_is_named_by_its_id(emit, tmp_path): + # catches: a nameless descriptor failing discovery (KeyError) where a `sources` UKD of the + # same shape is named by its id + variants, skipped = discover(emit, tmp_path, [{"id": "u1", "kernel_source": KS}]) + assert [v["name"] for v in variants] == ["u1"] and skipped == [] @pytest.mark.parametrize( @@ -1193,7 +1218,7 @@ def test_malformed_single_kernel_source_is_not_inspected(emit, tmp_path, kernel_ assert [s["reason"].split(":")[0] for s in skipped] == ["bad"] -@pytest.mark.parametrize("kind", ["hip", "hsaco"]) +@pytest.mark.parametrize("kind", ["hip", "hsaco", "kpack", "embedded_source"]) def test_multi_launch_ukd_of_another_kind_is_skipped_like_a_single_launch( emit, tmp_path, kind ):