From a67e1cd1ada438378cd3feb47642e9f12c50d93f Mon Sep 17 00:00:00 2001 From: erweiw Date: Fri, 31 Jul 2026 14:44:33 -0700 Subject: [PATCH 1/2] Cache NPU device probe to avoid per-dispatch xrt-smi subprocess 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) --- amd_triton_npu/backend/driver.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/amd_triton_npu/backend/driver.py b/amd_triton_npu/backend/driver.py index ba13897..bcb15b6 100644 --- a/amd_triton_npu/backend/driver.py +++ b/amd_triton_npu/backend/driver.py @@ -6,6 +6,7 @@ import logging import tempfile import sys +import functools import os, subprocess, tempfile, platform import importlib.util @@ -406,7 +407,11 @@ def _dump_ir_if_needed(files): shutil.copy(f, os.path.join(air_proj_path, os.path.basename(f))) +@functools.lru_cache(maxsize=1) def get_npu_device_info(): + # Cached: physical NPU devices do not change within a process, and this + # is called on the per-dispatch hot path (via detect_npu_version). Without + # caching, each kernel launch would spawn an `xrt-smi examine` subprocess. try: import re From ba6afbc9b60e82450b134307a8105c657359fb37 Mon Sep 17 00:00:00 2001 From: erweiw Date: Fri, 31 Jul 2026 14:49:13 -0700 Subject: [PATCH 2/2] Remove duplicate tempfile import 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) --- amd_triton_npu/backend/driver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amd_triton_npu/backend/driver.py b/amd_triton_npu/backend/driver.py index bcb15b6..167196b 100644 --- a/amd_triton_npu/backend/driver.py +++ b/amd_triton_npu/backend/driver.py @@ -8,7 +8,7 @@ import sys import functools -import os, subprocess, tempfile, platform +import os, subprocess, platform import importlib.util import importlib.metadata import shutil