diff --git a/amd_triton_npu/backend/driver.py b/amd_triton_npu/backend/driver.py index 7887cff..807d7d4 100644 --- a/amd_triton_npu/backend/driver.py +++ b/amd_triton_npu/backend/driver.py @@ -3,6 +3,7 @@ import hashlib import json +import logging import tempfile import sys import sysconfig @@ -23,6 +24,16 @@ from air.ir import * import air.passmanager +logger = logging.getLogger(__name__) +logger.setLevel(logging.CRITICAL) +if os.getenv("AMD_TRITON_NPU_DEBUG", "0") == "1": + logger.setLevel(logging.DEBUG) +if not logger.handlers: + _handler = logging.StreamHandler() + _handler.setFormatter(logging.Formatter("%(message)s")) + logger.addHandler(_handler) +logger.propagate = False + autotune_time = False @@ -176,10 +187,10 @@ def get_npu_device_info(): return devices except subprocess.CalledProcessError as e: - print("Failed to run xrt-smi:", e.stderr) + logger.error("Failed to run xrt-smi: %s", e.stderr) return [] except Exception as e: - print("Unexpected error:", str(e)) + logger.exception("Unexpected error during NPU device detection") return [] @@ -434,7 +445,7 @@ def _get_transform_ir_string(): f"Use an absolute path or run from the directory containing the script." ) with open(custom_script_path, "r") as f: - print(f"Using custom tiling script from: {custom_script_path}") + logger.debug("Using custom tiling script from: %s", custom_script_path) user_script = f.read() return _inject_transform_library(user_script) @@ -1335,28 +1346,30 @@ def launch( # Check for compile-only mode if os.getenv("AMD_TRITON_NPU_COMPILE_ONLY", "0") == "1": - print(f"Compile-only mode: binaries cached at {cache_path}") + logger.debug("Compile-only mode: binaries cached at %s", cache_path) if output_format == "elf": - print(f" elf: {cache_elf_path}") + logger.debug(" elf: %s", cache_elf_path) else: - print(f" xclbin: {cache_xclbin_path}") - print(f" insts: {cache_insts_path}") + logger.debug(" xclbin: %s", cache_xclbin_path) + logger.debug(" insts: %s", cache_insts_path) return None else: - print( - "got cache path: " - + cache_path - + " compilation is therefore skipped (delete cache path to force recompile)." + logger.debug( + "got cache path: %s compilation is therefore skipped " + "(delete cache path to force recompile).", + cache_path, ) # Check for compile-only mode (cache hit) if os.getenv("AMD_TRITON_NPU_COMPILE_ONLY", "0") == "1": - print(f"Compile-only mode (cache hit): binaries at {cache_path}") + logger.debug( + "Compile-only mode (cache hit): binaries at %s", cache_path + ) if output_format == "elf": - print(f" elf: {cache_elf_path}") + logger.debug(" elf: %s", cache_elf_path) else: - print(f" xclbin: {cache_xclbin_path}") - print(f" insts: {cache_insts_path}") + logger.debug(" xclbin: %s", cache_xclbin_path) + logger.debug(" insts: %s", cache_insts_path) return None # Load and launch the compiled kernel.