Refactor generated kernels into an immutable registry - #12
Merged
Conversation
Problem: The generated Strategy B ELF service and native runtime were singletons, so a second physically qualified generated kernel could not coexist without replacing process-global artifact state. Ownership: This is downstream torch-xdna artifact admission, compiler cache, native XRT runtime, Inductor wiring, tests, and documentation. It does not change PyTorch, Triton-XDNA, MLIR-AIR, XRT, firmware, or driver ownership. Implementation boundary: Replace the add-specific service with a closed manifest-driven registry keyed by device-artifact cache and receipt identities. Store independently constructed native ELF states per immutable key, reject either identity being reused for another specification, and retain the exact legacy runtime-stats evidence schema. Keep native capability selection and all product admission validators restricted to the already proven BF16[1024] add. Test Plan: - python -m pytest -q -p no:cacheprovider - python -m pytest -q -p no:cacheprovider tests/test_acceptance_physical.py tests/test_alpha_physical_evidence_validator.py tests/test_generated_elf_native_contract.py tests/test_generated_kernel_registry.py tests/test_strategy_b_initialization.py tests/test_inductor_adapter.py - python -m ruff check . - python -m ruff format --check . - python -m mypy --follow-imports=skip --ignore-missing-imports --strict build_support.py torch_xdna/_artifact_paths.py torch_xdna/_capability.py torch_xdna/_cache_runtime.py torch_xdna/_compiler_artifacts.py torch_xdna/_compiler_cache.py torch_xdna/_compiler_contracts.py torch_xdna/_generated_kernel_registry.py torch_xdna/_triton_adapter.py scripts/_acceptance_physical.py scripts/_bootstrap_receipt.py scripts/_package_inventory.py scripts/run_acceptance.py scripts/validate_alpha_physical_evidence.py scripts/validate_physical_evidence.py scripts/validate_physical_range.py scripts/verify_artifacts.py tests/physical_support.py - python tests/physical_inductor_strategy_b_bfloat16_add.py - xrt-smi examine --device DEVICE --report platform Physical evidence: A rebuilt non-editable CPython 3.12 extension passed cold, warm, and trapped-wrapper BF16[1024] add launches on RyzenAI-npu4. All 1,024 raw BF16 words matched, outputs remained on xdna:0 until copy-back, ERT completed with positive intervals, the exact direct three-BO artifact loaded once, unsupported aliasing had no side effects, and post-run XRT health passed. Limitations: The product manifest and native validators still admit only the exact proven BF16[1024] add. Independent multi-entry physical execution, matmul, broader shapes, and multi-artifact provisioning remain unclaimed until separately qualified. AI-assisted development: OpenAI Codex assisted implementation, testing, and review; the human maintainer retains responsibility for the change.
There was a problem hiding this comment.
Pull request overview
This PR refactors Strategy B generated-kernel artifact admission from a single-purpose service into a manifest-driven, immutable registry, enabling safe coexistence with additional physically-qualified generated kernels while preserving the existing BF16[1024] add-only boundary and native capability authority.
Changes:
- Replaced the Strategy B artifact singleton/service with a closed
XdnaGeneratedKernelRegistrykeyed by (device-artifact cache digest, receipt identity digest). - Updated the Python ↔ native binding contract and runtime state to support a per-entry generated-kernel registry (including a new native state introspection endpoint).
- Migrated and expanded tests/docs/workflow gating to cover the new registry and renamed native binding entrypoint.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| torch_xdna/inductor.py | Switches Strategy B artifact binding from the old service to the generated-kernel registry during codegen/binding. |
| torch_xdna/compiler.py | Threads the generated-kernel registry through compile-contract registration. |
| torch_xdna/init.py | Initializes/adopts the generated-kernel registry during runtime initialization (replacing Strategy B service admission). |
| torch_xdna/_generated_kernel_registry.py | Adds the manifest-driven registry implementation and key/entry types. |
| torch_xdna/_strategy_b_artifact_service.py | Removes the old singleton-style Strategy B artifact service. |
| torch_xdna/_compiler_artifacts.py | Renames/updates the native binder entrypoint to _bind_generated_kernel. |
| torch_xdna/csrc/XdnaRuntime.h | Replaces single generated-ELF state with a keyed generated-kernel registry and new query API. |
| torch_xdna/csrc/XdnaRuntime.cpp | Implements composite-keyed publication/binding and per-entry state tracking for generated kernels. |
| torch_xdna/csrc/bindings.cpp | Renames the pybind binding entrypoint and adds _generated_kernel_registry_state. |
| tests/test_workflow_contracts.py | Updates workflow contract assertions for the new registry module. |
| tests/test_strategy_b_initialization.py | Updates initialization tests to validate registry-based admission. |
| tests/test_strategy_b_artifact_service.py | Removes tests for the deleted Strategy B artifact service. |
| tests/test_inductor_adapter.py | Updates adapter tests to use the registry path and per-family binding. |
| tests/test_generated_kernel_registry.py | Adds focused unit tests for manifest/registry behavior and failure modes. |
| tests/test_generated_elf_native_contract.py | Updates native contract tests for composite-key registry and renamed entrypoint. |
| tests/test_compiler_artifacts.py | Updates binder naming to match the renamed native entrypoint. |
| pyproject.toml | Updates hardware-free Python file list to include the new registry module. |
| docs/COMPILER_CACHE.md | Documents the new registry structure and its invariants/boundary. |
| docs/alpha/STRATEGY_B_COMPILER_CONTRACT.md | Updates the contract documentation to describe keyed binding semantics. |
| .github/workflows/hardware-free.yml | Updates workflow file allowlist for the new registry module. |
Suppressed comments (2)
torch_xdna/csrc/XdnaRuntime.cpp:737
bind_generated_kernel()still logs/checks the fork-safety operation as "bind an XDNA generated ELF". Since the API and Python entrypoint were renamed to "generated kernel", this message is now inconsistent and makes debugging harder.
GeneratedElfObservation XdnaRuntime::bind_generated_kernel(
CompilerElfSpec spec,
std::vector<char> elf_data) {
check_not_in_bad_fork("bind an XDNA generated ELF");
validate_generated_elf_spec(spec);
torch_xdna/csrc/XdnaRuntime.cpp:846
launch_generated_bfloat16_add()still uses the fork-safety operation string "launch an XDNA generated ELF" even though the binding/publication surface was renamed to "generated kernel". Aligning the message avoids confusion when diagnosing bad-fork failures.
void XdnaRuntime::launch_generated_bfloat16_add(
XdnaAllocation& lhs,
XdnaAllocation& rhs,
XdnaAllocation& output,
const std::string& expected_device_artifact_cache_key_sha256,
const std::string& expected_artifact_receipt_identity_sha256) {
check_not_in_bad_fork("launch an XDNA generated ELF");
TORCH_CHECK(
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+984
to
+987
| selection = decision.get("selection") | ||
| if not isinstance(selection, dict): | ||
| raise _unsupported("Strategy B source has no native kernel selection") | ||
| entry = registry.require_family(str(selection.get("kernel_family"))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Why
The current singleton could not safely coexist with a second physically qualified generated kernel. This change creates the narrow runtime and artifact-admission foundation needed by the matmul work without claiming matmul or changing the active product surface.
Validation
Physical evidence
The exact commit tree was exercised from a fresh compiler cache on RyzenAI-npu4 with XRT 2.20:
Boundary
This PR does not add matmul, a second manifest entry, a new operator, a public API, fallback behavior, a compiler fork, or artifact distribution. The manifest and native validators remain restricted to the one already qualified BF16 add until the next receipt is independently proven.
AI-assisted development: OpenAI Codex assisted implementation, testing, and review. The human maintainer retains responsibility for the change.