Skip to content

Cache NPU device probe to avoid per-dispatch xrt-smi subprocess - #86

Merged
erwei-xilinx merged 2 commits into
mainfrom
fix-xrt-smi-memoization
Jul 31, 2026
Merged

Cache NPU device probe to avoid per-dispatch xrt-smi subprocess#86
erwei-xilinx merged 2 commits into
mainfrom
fix-xrt-smi-memoization

Conversation

@erwei-xilinx

Copy link
Copy Markdown
Collaborator

Summary

  • get_npu_device_info() runs xrt-smi examine (~30 ms subprocess) to detect the NPU generation (npu1/npu2).
  • It is reachable from the per-dispatch hot path via detect_npu_version(), so every kernel launch could spawn a subprocess just to re-answer a question whose result is constant for the process lifetime.
  • Memoize it with functools.lru_cache(maxsize=1). Physical devices don't change within a process. The AMD_TRITON_NPU_TARGET cross-compile override still short-circuits in detect_npu_version() before the probe is ever called, so that path is unaffected.

Impact

Measured on NPU2/Strix, this removes a uniform ~30 ms/dispatch overhead on hot paths that reach detect_npu_version() per launch, with no behavior change (same device detection result, just computed once).

Test plan

  • python -m py_compile on the changed file
  • Device detection still returns the correct generation (npu2) after caching
  • CI build

🤖 Generated with Claude Code

get_npu_device_info() shells out to `xrt-smi examine` (~30ms) to detect
the NPU generation. It is reachable from the per-dispatch hot path via
detect_npu_version(), so without caching each kernel launch could spawn
a subprocess just to re-answer a question whose result is constant for
the life of the process. Memoize it with functools.lru_cache; the
AMD_TRITON_NPU_TARGET override still short-circuits before the probe.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 31, 2026 21:44

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.

🟢 Ready to approve

The functional change is small and low-risk, and the only noted issue is a minor import cleanup nit.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR optimizes the AMD Triton NPU backend hot path by memoizing NPU device probing so xrt-smi examine is executed once per process instead of potentially once per dispatch.

Changes:

  • Added functools.lru_cache(maxsize=1) to get_npu_device_info() to avoid repeated xrt-smi examine subprocess calls.
  • Added an explanatory comment documenting why the probe result is safe to cache for process lifetime.
  • Updated imports to include functools.
File summaries
File Description
amd_triton_npu/backend/driver.py Caches NPU device probe results to eliminate per-dispatch subprocess overhead.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread amd_triton_npu/backend/driver.py Outdated
tempfile was imported both standalone and in the multi-import line;
drop the redundant one. Addresses review feedback on PR #86.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@erwei-xilinx
erwei-xilinx merged commit 8696bcb into main Jul 31, 2026
2 of 3 checks passed
@erwei-xilinx
erwei-xilinx deleted the fix-xrt-smi-memoization branch July 31, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants