Cache NPU device probe to avoid per-dispatch xrt-smi subprocess - #86
Merged
Conversation
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>
7 tasks
Contributor
There was a problem hiding this comment.
🟢 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)toget_npu_device_info()to avoid repeatedxrt-smi examinesubprocess 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.
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>
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
get_npu_device_info()runsxrt-smi examine(~30 ms subprocess) to detect the NPU generation (npu1/npu2).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.functools.lru_cache(maxsize=1). Physical devices don't change within a process. TheAMD_TRITON_NPU_TARGETcross-compile override still short-circuits indetect_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_compileon the changed file🤖 Generated with Claude Code