Skip to content

Commit bc7a3e3

Browse files
hunhoffeclaude
andauthored
Expose target architecture via Device.arch (#2485) (#3108)
Co-authored-by: Claude Opus 4 (1M context) <noreply@anthropic.com>
1 parent e094875 commit bc7a3e3

5 files changed

Lines changed: 30 additions & 11 deletions

File tree

include/aie-c/TargetModel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ MLIR_CAPI_EXPORTED int aieTargetModelRows(AieTargetModel targetModel);
5555
/// Returns true if this is an NPU target model.
5656
MLIR_CAPI_EXPORTED bool aieTargetModelIsNPU(AieTargetModel targetModel);
5757

58+
/// Returns the AIE architecture (as the underlying value of
59+
/// xilinx::AIE::AIEArch: AIE1=1, AIE2=2, AIE2p=3).
60+
MLIR_CAPI_EXPORTED uint32_t
61+
aieTargetModelGetTargetArch(AieTargetModel targetModel);
62+
5863
/// Returns the tile type for the given coordinates.
5964
MLIR_CAPI_EXPORTED uint32_t
6065
aieTargetModelGetTileType(AieTargetModel targetModel, int col, int row);

lib/CAPI/TargetModel.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ bool aieTargetModelIsNPU(AieTargetModel targetModel) {
201201
return unwrap(targetModel).hasProperty(xilinx::AIE::AIETargetModel::IsNPU);
202202
}
203203

204+
uint32_t aieTargetModelGetTargetArch(AieTargetModel targetModel) {
205+
return static_cast<uint32_t>(unwrap(targetModel).getTargetArch());
206+
}
207+
204208
uint32_t aieTargetModelGetColumnShift(AieTargetModel targetModel) {
205209
return unwrap(targetModel).getColumnShift();
206210
}

python/AIEMLIRModule.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ NB_MODULE(_aie, m) {
358358
[](PyAieTargetModel &self) {
359359
return aieTargetModelIsNPU(self.get());
360360
})
361+
.def("get_target_arch",
362+
[](PyAieTargetModel &self) {
363+
return aieTargetModelGetTargetArch(self.get());
364+
})
361365
.def("get_column_shift",
362366
[](PyAieTargetModel &self) {
363367
return aieTargetModelGetColumnShift(self.get());

python/iron/device/device.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Generator
1010

1111
from ... import ir # type: ignore
12-
from ...dialects._aie_enum_gen import AIETileType, WireBundle # type: ignore
12+
from ...dialects._aie_enum_gen import AIEArch, AIETileType, WireBundle # type: ignore
1313
from ...dialects.aie import (
1414
AIEDevice,
1515
logical_tile,
@@ -45,6 +45,11 @@ def rows(self) -> int:
4545
"""Number of rows in the device tile array."""
4646
return self._tm.rows()
4747

48+
@property
49+
def arch(self) -> AIEArch:
50+
"""AIE architecture of the device (AIE1, AIE2, or AIE2p)."""
51+
return AIEArch(self._tm.get_target_arch())
52+
4853
def _validate_coordinates(self, col, row):
4954
"""Raise ValueError if coordinates are outside the device grid."""
5055
if col < 0 or col >= self._tm.columns() or row < 0 or row >= self._tm.rows():

python/utils/jit.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from aie.extras.context import mlir_mod_ctx
1616
from .compile import compile_mlir_module, compile_external_kernel
1717
from .npukernel import NPUKernel
18-
from aie.dialects.aie import AIEDevice
1918
from .compile.cache.circular_cache import CircularCache
2019
from .compile.cache.utils import _create_function_cache_key, file_lock
2120
from .compile import NPU_CACHE_HOME
@@ -48,8 +47,10 @@ def jit(function=None, use_cache=True):
4847

4948
@functools.wraps(function)
5049
def decorator(*args, **kwargs):
51-
from aie.iron.device import NPU1, NPU2, NPU1Col1, NPU2Col1
50+
from aie.iron.device import Device
5251
from aie.iron.kernel import ExternalFunction
52+
from aie.dialects._aie_enum_gen import AIEArch
53+
from aie.dialects.aie import get_target_model
5354
from . import DefaultNPURuntime
5455

5556
if DefaultNPURuntime is None:
@@ -118,17 +119,17 @@ def decorator(*args, **kwargs):
118119

119120
current_device = DefaultNPURuntime.device()
120121

121-
# Determine target architecture based on device type
122-
if isinstance(current_device, (NPU2, NPU2Col1)):
123-
target_arch = "aie2p"
124-
elif isinstance(current_device, (NPU1, NPU1Col1)):
125-
target_arch = "aie2"
126-
elif current_device in (AIEDevice.npu2, AIEDevice.npu2_1col):
122+
# Determine target architecture from the device's target model.
123+
if isinstance(current_device, Device):
124+
arch = current_device.arch
125+
else:
126+
arch = AIEArch(get_target_model(current_device).get_target_arch())
127+
if arch == AIEArch.AIE2p:
127128
target_arch = "aie2p"
128-
elif current_device in (AIEDevice.npu1, AIEDevice.npu1_1col):
129+
elif arch == AIEArch.AIE2:
129130
target_arch = "aie2"
130131
else:
131-
raise RuntimeError(f"Unsupported device type: {type(current_device)}")
132+
raise RuntimeError(f"Unsupported device arch: {arch}")
132133

133134
# Hash of the IR string, ExternalFunction compiler options, and target architecture
134135
module_hash = hash_module(mlir_module, external_kernels, target_arch)

0 commit comments

Comments
 (0)