Skip to content

Add Python config API for NPU backend settings#47

Merged
erwei-xilinx merged 3 commits into
mainfrom
python-config-api
Apr 11, 2026
Merged

Add Python config API for NPU backend settings#47
erwei-xilinx merged 3 commits into
mainfrom
python-config-api

Conversation

@erwei-xilinx

Copy link
Copy Markdown
Collaborator

Summary

  • Adds amd_triton_npu/backend/config.py with a singleton npu_config object that provides programmatic control over 5 settings previously only available via environment variables
  • Replaces all ad-hoc os.getenv calls in driver.py and compiler.py with reads from the config singleton
  • Eliminates the duplicated _get_air_project_path() function that existed in both files

API

from triton.backends.amd_triton_npu.config import npu_config, set_config, config_context

# Direct attribute assignment
npu_config.bf16_emulation = True
npu_config.compile_only = True
npu_config.air_project_path = "/tmp/my_project"
npu_config.transform_tiling_script = "/path/to/tiling.mlir"
npu_config.output_format = "elf"  # or "xclbin" or None for auto-detect

# Dict-style
set_config(bf16_emulation=True, compile_only=False)

# Context manager for temporary overrides
with config_context(compile_only=True):
    kernel[grid](a, b, c)

All existing environment variables continue to work as fallback defaults — fully backward compatible.

Config property Env var fallback Default
compile_only AMD_TRITON_NPU_COMPILE_ONLY False
transform_tiling_script AIR_TRANSFORM_TILING_SCRIPT None
bf16_emulation AMD_TRITON_NPU_BF16_EMULATION False
output_format AMD_TRITON_NPU_OUTPUT_FORMAT None (auto-detect)
air_project_path AMD_TRITON_NPU_AIR_PROJECT_PATH ./air_project

Closes #43

Test plan

  • Verify import works: python -c "from triton.backends.amd_triton_npu.config import npu_config, set_config, config_context"
  • Verify env var fallback: AMD_TRITON_NPU_COMPILE_ONLY=1 python -c "from triton.backends.amd_triton_npu.config import npu_config; assert npu_config.compile_only"
  • Verify programmatic override wins over env var: AMD_TRITON_NPU_BF16_EMULATION=1 python -c "from triton.backends.amd_triton_npu.config import npu_config; npu_config.bf16_emulation = False; assert not npu_config.bf16_emulation"
  • Run existing examples with env vars to verify no regressions

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings April 10, 2026 00:08

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

Adds a process-global Python configuration API for the AMD Triton NPU backend, enabling programmatic control of backend settings that were previously only configurable via environment variables, while keeping env vars as backward-compatible fallbacks.

Changes:

  • Introduces amd_triton_npu/backend/config.py exposing a singleton npu_config, plus set_config() and config_context() for overrides.
  • Replaces direct os.getenv(...) reads in the driver/compiler with npu_config properties and removes duplicated air project path helper logic.
  • Adds amd_triton_npu/backend/__init__.py to support package/module structure.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
amd_triton_npu/backend/config.py New config singleton + helpers with env-var fallbacks
amd_triton_npu/backend/driver.py Swaps env var reads for config access; removes duplicated path helper
amd_triton_npu/backend/compiler.py Uses config for air project path dumping; removes duplicated helper
amd_triton_npu/backend/__init__.py Adds package marker for backend module

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread amd_triton_npu/backend/config.py
Comment thread amd_triton_npu/backend/config.py
Comment thread amd_triton_npu/backend/config.py
Comment thread amd_triton_npu/backend/config.py Outdated
Comment thread amd_triton_npu/backend/driver.py Outdated
Comment thread amd_triton_npu/backend/driver.py Outdated
@ypapadop-amd

Copy link
Copy Markdown

I think we should also add #44

@erwei-xilinx erwei-xilinx force-pushed the python-config-api branch 3 times, most recently from 9de8e09 to ca7c539 Compare April 11, 2026 05:35
erwei-xilinx and others added 3 commits April 10, 2026 22:39
Introduces a centralized config module (config.py) with a singleton
npu_config object that provides programmatic control over the 5 settings
previously only available via environment variables. Env vars are
preserved as fallback defaults for backward compatibility.

Closes #43

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Treat None as "unset" in air_project_path setter (reverts to
  env var / default instead of crashing with Path(None))
- Use __dict__.copy() in config_context() so new fields are
  automatically saved/restored
- Mention both config property and env var in error messages
  for output_format and transform_tiling_script
- Incorporate PR #44 logging changes (rebase on top of
  feature/logging-knob-issue-40)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Points to erwei-xilinx/triton-shared@c043a85 which adds TPtrIR
to TritonShared plugin LINK_LIBS, fixing the flaky parallel build
failure (facebookincubator/triton-shared#19).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@erwei-xilinx erwei-xilinx merged commit 8d566ba into main Apr 11, 2026
2 of 3 checks passed
@erwei-xilinx erwei-xilinx deleted the python-config-api branch April 11, 2026 06:30
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.

Feature: mechanism to control driver from Python

3 participants