Skip to content

Wire TOSA admission and the compiler helper for IDENTITY (#89) - #136

Open
aravishankar-mp wants to merge 2 commits into
task/xdna-execution-lifecyclefrom
task/xdna-tosa-identity
Open

Wire TOSA admission and the compiler helper for IDENTITY (#89)#136
aravishankar-mp wants to merge 2 commits into
task/xdna-execution-lifecyclefrom
task/xdna-tosa-identity

Conversation

@aravishankar-mp

Copy link
Copy Markdown
Contributor

Why

Fourth code ticket of the AMD XDNA wayfinder map (#78): TOSA graphs now compile and run on the NPU. Implements the compiler-helper contract (#84) and TOSA admission (#83) for the first tier operator — BF16 IDENTITY — end to end.

Stacked: based on task/xdna-execution-lifecycle (PR #134), which is based on task/xdna-ffi-buffers (PR #127). Review #127#134 → this. Rebases onto main as they land.

Highlights:

  • src/lower.rs — portable TOSA admission: parse + analyze_for + a strict subset check that turns an artifact into a validated CompilerSpec (integers and enums only) and rejects anything outside the BF16 IDENTITY subset before any subprocess runs. Unit-tested on every host (CI): admits BF16 identity; rejects FP32, non-line-size shapes, and the wrong target.
  • compiler/xdna_compile.py — the embedded aiecc helper: an IDENTITY IRON template + spec-driven compile + a toolchain-identity mode, self-configuring the pinned toolchain (NPU_RUNTIME=hrx for the unfolded ABI, Peano/xclbinutil/HRX paths) under a cleared environment.
  • src/compiler.rs — the bounded subprocess driver: cleared env + pinned prefix + private workdir, wall-clock timeout, own process group, and a content-addressed artifact cache (spec ‖ toolchain ‖ helper source). Never a Cargo dependency, never in-process.
  • src/native.rs load_program — precompiled artifacts load directly; a TOSA artifact is admitted, compiled, and built into an executable (shared build_executable). compile_artifact exposes the admit-then-compile path without a device — the offline / catalog-population use from Decide the compiler-helper subprocess contract #84.
  • Tests — CI admission unit tests; a hardware-free golden artifact test (compile IDENTITY, assert xclbin magic + FP32 rejection); and the on-NPU test compiling a TOSA BF16 IDENTITY through the helper and asserting output == input.

Deferred: MATMUL (#90), MAX_POOL2D (#91), full conformance + device-loss tier-2 (#92). The compute tiers add IRON templates to the same helper and dtype/op arms to admission.

Compatibility

  • No wire effect. No accepted or emitted protocol bytes change.

Checklist

  • Does not alter payload lengths, ownership, reset, error, timeout, or feature-negotiation behavior.
  • Authoritative conformance inputs untouched.
  • Public Rust API: adds compile_artifact and the portable lower::{admit, CompilerSpec, ...}; the TOSA path in load_program. No core/guest/device/transport changes.
  • No dependency/feature/target moves platform behavior into a portable crate — native path and compiler gated behind va_xdna; the compiler is a subprocess, not a linked dependency.
  • Unsafe under audit: no new unsafe (the compiler driver is safe code); SAFETY.md notes compiler.rs is safe; the FFI/native audit and release-policy entry stand.
  • Deferred features unadvertised: only BF16 IDENTITY compiles; other ops/dtypes and the integer target are rejected at admission.

Verification

On the reference machine (Fedora 44, Krackan NPU, pinned toolchain):

cargo test -p virtio-accel-xdna            # incl. on-NPU TOSA IDENTITY compile+run (output == input)
# placeholder path (no HRX — the CI default): admission unit tests run here
cargo fmt --all -- --check
cargo clippy -p virtio-accel-xdna --all-targets --all-features -- -D warnings
RUSTDOCFLAGS=-D warnings cargo doc -p virtio-accel-xdna --no-deps
python3 ci/check-release-policy.py         # 17 published packages OK
python3 ci/publish-dry-run.py              # ordered dry run passed for all 17 crates

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Wires end-to-end TOSA BF16 IDENTITY support for the AMD XDNA backend by adding a portable TOSA admission layer, a bounded compiler-helper subprocess that produces precompiled artifacts, and native load_program support to admit+compile TOSA artifacts at load time (with new on-hardware tests).

Changes:

  • Add portable TOSA admission (lower::admit) that validates a strict BF16 IDENTITY subset and emits a CompilerSpec.
  • Add a bounded compiler-helper subprocess driver (src/compiler.rs) plus embedded helper script (compiler/xdna_compile.py) and a content-addressed artifact cache.
  • Extend native load_program to accept either precompiled artifacts directly or TOSA artifacts via admit-then-compile; add hardware and compiler tests.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
docs/release-policy.md Updates release policy entry to reflect the new tosa-build dev dependency usage.
docs/portability.md Updates portability docs to describe compilation and on-device testing behavior.
crates/virtio-accel-xdna/tests/hardware.rs Adds hardware-free compile test and on-NPU BF16 IDENTITY compile+run test.
crates/virtio-accel-xdna/src/native.rs Extends load_program to accept TOSA artifacts by admitting+compiling them; factors out executable construction.
crates/virtio-accel-xdna/src/lower.rs Implements portable TOSA admission into CompilerSpec for BF16 IDENTITY subset + unit tests.
crates/virtio-accel-xdna/src/lib.rs Re-exports admission API/types and adds public compile_artifact entry point (va_xdna).
crates/virtio-accel-xdna/src/compiler.rs Adds bounded subprocess compiler driver with cache and helper embedding.
crates/virtio-accel-xdna/SAFETY.md Updates safety scope notes to include the safe compiler driver and compiled path.
crates/virtio-accel-xdna/README.md Updates crate README to describe new TOSA compilation support and offline compilation path.
crates/virtio-accel-xdna/compiler/xdna_compile.py Adds the embedded compiler helper script (compile + identity modes).
crates/virtio-accel-xdna/Cargo.toml Adds virtio-accel-tosa-build as a dev-dependency for test graph construction.
Cargo.lock Records the new dev-dependency resolution.
Suppressed comments (2)

crates/virtio-accel-xdna/src/compiler.rs:172

  • The helper is placed in its own process group (process_group(0)), but on timeout only the immediate child is killed. Any subprocesses spawned by the toolchain may survive, breaking the “bounded subprocess” guarantee. Consider killing the whole process group on timeout (e.g., killpg/SIGKILL) in addition to child.kill().
                    if Instant::now() >= deadline {
                        let _ = child.kill();
                        let _ = child.wait();
                        return Err(external(code::TIMEOUT));

crates/virtio-accel-xdna/src/compiler.rs:125

  • cache_key hashes only the toolchain path, not a measured toolchain identity. If the toolchain is updated in-place (same prefix), the cache can serve stale artifacts. Since the helper already has an identity mode, consider incorporating its output (and/or hashes/mtimes of key tool binaries) into the cache key so updates invalidate correctly.
        1u32.hash(&mut hasher); // key schema version
        spec.hash(&mut hasher);
        self.toolchain.hash(&mut hasher);
        HELPER_SOURCE.hash(&mut hasher);
        format!("{:016x}", hasher.finish())

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/virtio-accel-xdna/src/compiler.rs
Comment thread crates/virtio-accel-xdna/src/lower.rs
@SnowCheetos
SnowCheetos force-pushed the task/xdna-tosa-identity branch from 18cff66 to 4012fc2 Compare August 25, 2026 03:32
@SnowCheetos SnowCheetos added area: protocol Wire ABI, specification, and compatibility area: engine Command dispatch, state, ownership, and reset area: backend Accelerator traits, mock backend, and provider conformance labels Aug 25, 2026
aravishankar-mp added a commit that referenced this pull request Aug 25, 2026
- submit re-checks BufferDesc::allows_access as defense in depth
  (PermissionDenied, mirroring OpenVINO; the host-side check remains the
  contract's requirement). Closes the corresponding #138 item.
- The artifact-cache key mixes in the measured toolchain identity (the
  helper's identity mode: mlir_aie / llvm-aie versions), so an in-place
  toolchain update under the same prefix can never serve stale
  artifacts. The probe runs once per driver, lazily; a failed probe
  keys the cache on the failure marker.
- Cache staging filenames carry the pid so two processes compiling the
  same key cannot collide on a staging path.
- check()'s doc no longer claims the rendered HRX message is surfaced
  (it is freed unread; no logging facility exists at this layer), and
  the example's docs no longer defer program loading to a later ticket.

The negative-dimension admission comment on #136 was already fixed by
the review commit (usize::try_from in admit_identity); the two compile-
error comments on #134 are false positives (size_of is in the Rust 2024
prelude, and Option<BackendError> is Copy so unwrap_or reads through
the MutexGuard) - CI compiles both.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@aravishankar-mp
aravishankar-mp force-pushed the task/xdna-tosa-identity branch from 4012fc2 to 46aff9c Compare August 25, 2026 19:09
SnowCheetos
SnowCheetos previously approved these changes Aug 25, 2026
aravishankar-mp and others added 2 commits August 25, 2026 16:45
Implements the compiler-helper contract (issue #84) and TOSA admission
(issue #83) for the BF16 IDENTITY tier, end to end on hardware:

- src/lower.rs: portable TOSA admission (parse + analyze_for + strict
  subset check) turning an artifact into a CompilerSpec, rejecting
  anything outside the BF16 IDENTITY subset before any subprocess runs.
  Unit-tested on every host (admits bf16 identity; rejects fp32,
  non-line-size, wrong target).
- compiler/xdna_compile.py: the embedded aiecc helper (IDENTITY IRON
  template + spec-driven compile + toolchain identity), self-configuring
  the pinned toolchain under a cleared environment.
- src/compiler.rs: the bounded subprocess driver — cleared env + pinned
  prefix + private workdir, wall-clock timeout, process group, and a
  content-addressed artifact cache (spec + toolchain + helper source).
- src/native.rs load_program: precompiled artifacts load directly; a
  TOSA artifact is admitted, compiled, and built into an executable
  (shared build_executable path). compile_artifact exposes the
  admit-then-compile path without a device (offline / catalog mode).
- tests: CI admission unit tests; a hardware-free golden artifact test
  (compile IDENTITY, assert xclbin magic + rejection of fp32); and the
  on-NPU test compiling a TOSA BF16 IDENTITY and matching output == input.

Hosts without HRX compile the portable admission surface, artifact codec,
and placeholder, with no unsafe. Local gates green: fmt, clippy, tests
(placeholder admission in CI + on-hardware), doc under -D warnings,
release-policy, and publish-dry-run for all 17 crates.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The admission items in lower.rs (admit, CompilerSpec, SpecOp, SpecDType,
AdmitError, IDENTITY_LINE_SIZE) are consumed only by va_xdna code
(compiler.rs, compile_artifact), so a non-HRX placeholder build sees them
as dead code and -D dead-code fails style-and-api. They belong in the
public API regardless: portable admissibility checking is a real, useful
capability and compile_artifact is already public. Re-export them
unconditionally from the crate root.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: backend Accelerator traits, mock backend, and provider conformance area: engine Command dispatch, state, ownership, and reset area: protocol Wire ABI, specification, and compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants