Host Runtime (aie.utils.hostruntime)¶
Host-side helpers for selecting a device, allocating NPU-accessible tensors, running compiled designs, and sharing common CLI / argument plumbing across programming examples.
Import the public surface from aie.utils.hostruntime (or via the
aie.utils / iron re-exports where noted). The symbols below are rendered
from the Python package under python/utils/hostruntime/ using the same
mkdocstrings path layout as the other Python API pages (utils.*, not the
installed aie.utils.* import name).
Runtime abstractions¶
Abstract runtime types that concrete backends (for example XRT) implement.
HostRuntimeError ¶
Bases: Exception
Error raised when a NPU kernel encounters an error during runtime operations.
KernelHandle ¶
Bases: ABC
Abstract representation that represents a kernel already registered/loaded with a runtime.
KernelResult ¶
Bases: ABC
A wrapper around data produced as the result of running a kernel
Initialize the KernelResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
npu_time
|
int
|
The execution time on the NPU in nanoseconds. |
required |
trace_config
|
TraceConfig | None
|
Configuration for tracing. Defaults to None. |
None
|
Source code in python/utils/hostruntime/hostruntime.py
npu_time
property
¶
Get the NPU execution time.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The execution time in nanoseconds. |
trace_config
property
¶
Get the trace configuration.
Returns:
| Type | Description |
|---|---|
TraceConfig | None
|
TraceConfig | None: The trace configuration if available, else None. |
has_trace ¶
Check if trace data is available.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if trace configuration is present, False otherwise. |
is_success
abstractmethod
¶
Check if the kernel execution was successful.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise. |
HostRuntime ¶
Bases: ABC
An abstract class for a generic host runtime
check_device_consistency ¶
Check if the overridden device is loadable on the runtime device.
A 1- or N-column variant of a generation (e.g. NPU1Col1) is loadable on a wider device of the same generation (e.g. a 4-column NPU1), so we accept any override whose arch matches and whose column count is <= the runtime device's column count.
Source code in python/utils/hostruntime/hostruntime.py
cleanup ¶
Release any cached device/runtime resources held by this runtime.
Base implementation is a no-op: a plain runtime holds nothing to release. Caching runtimes override this to free hardware contexts, loaded executables, instruction buffers, etc. Safe to call even if the runtime never ran anything.
Source code in python/utils/hostruntime/hostruntime.py
evict_context ¶
Drop any cached device context associated with xclbin_path.
Recovery hook invoked after the driver rejects a submit against a stale
context (e.g. an XRT IOCTL EINVAL) so the next load rebuilds a fresh
context. Base implementation is a no-op for runtimes that keep no
evictable context cache.
Source code in python/utils/hostruntime/hostruntime.py
load
abstractmethod
¶
load(npu_kernel: NPUKernel, **kwargs) -> KernelHandle
Load an NPU kernel into the runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
npu_kernel
|
NPUKernel
|
The NPU kernel to load. |
required |
**kwargs
|
Additional arguments for loading. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
KernelHandle |
KernelHandle
|
A handle to the loaded kernel. |
Source code in python/utils/hostruntime/hostruntime.py
run
abstractmethod
¶
run(
kernel_handle: KernelHandle,
args,
trace_config: TraceConfig | None = None,
fail_on_error: bool = True,
only_if_loaded=False,
**kwargs
) -> KernelResult
Run a loaded kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel_handle
|
KernelHandle
|
The handle to the loaded kernel. |
required |
args
|
Arguments to pass to the kernel. |
required | |
trace_config
|
TraceConfig | None
|
Configuration for tracing. Defaults to None. |
None
|
fail_on_error
|
bool
|
Whether to raise an exception on kernel failure. Defaults to True. |
True
|
only_if_loaded
|
bool
|
If True, only run if already loaded. Defaults to False. |
False
|
**kwargs
|
Additional arguments. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
KernelResult |
KernelResult
|
The result of the kernel execution. |
Source code in python/utils/hostruntime/hostruntime.py
load_and_run ¶
load_and_run(
npu_kernel: NPUKernel, run_args: list, **kwargs
) -> tuple[KernelHandle, KernelResult]
Load and run an NPU kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
npu_kernel
|
NPUKernel
|
The NPU kernel to load and run. |
required |
run_args
|
list
|
Arguments to pass to the kernel. |
required |
**kwargs
|
Additional arguments passed to load. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[KernelHandle, KernelResult]
|
tuple[KernelHandle, KernelResult]: A tuple containing the kernel handle and the execution result. |
Source code in python/utils/hostruntime/hostruntime.py
device
abstractmethod
¶
Get the device associated with this runtime.
Returns:
| Name | Type | Description |
|---|---|---|
Device |
Device
|
The device object. |
read_insts_binary
classmethod
¶
Reads instructions from a binary file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
insts_path
|
Path
|
Path to the binary instruction file. |
required |
Returns:
| Type | Description |
|---|---|
|
np.ndarray: Array of uint32 instructions. |
Source code in python/utils/hostruntime/hostruntime.py
read_insts
classmethod
¶
Reads instructions from the given file.
If the file extension is .bin, uses binary read. If the file extension is .txt, uses sequence (text) read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
insts_path
|
Path
|
Path to the instruction file. |
required |
Returns:
| Type | Description |
|---|---|
|
np.ndarray: Array of instructions. |
Raises:
| Type | Description |
|---|---|
HostRuntimeError
|
If the file extension is not supported. |
Source code in python/utils/hostruntime/hostruntime.py
prepare_args_for_trace
classmethod
¶
Prepare arguments for tracing by appending necessary buffers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[Tensor]
|
List of input/output tensors. |
required |
trace_config
|
TraceConfig
|
Trace configuration. |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
list[Tensor]: The updated list of tensors with trace buffers appended. |
Source code in python/utils/hostruntime/hostruntime.py
extract_trace_from_args
classmethod
¶
extract_trace_from_args(
args: list[Tensor], trace_config: TraceConfig
) -> tuple[ndarray, ndarray | None]
Extract trace and control buffers from the arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[Tensor]
|
List of tensors used in execution. |
required |
trace_config
|
TraceConfig
|
Trace configuration. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray | None]
|
tuple[np.ndarray, np.ndarray | None]: A tuple containing the trace buffer and optionally the control buffer. |
Source code in python/utils/hostruntime/hostruntime.py
process_trace
classmethod
¶
Process the trace buffer and control buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace_buffer
|
ndarray
|
The trace data buffer. |
required |
ctrl_buffer
|
ndarray
|
The control packet buffer. |
required |
trace_config
|
TraceConfig
|
Trace configuration. |
required |
verbosity
|
int
|
Verbosity level. Defaults to 0. |
0
|
Source code in python/utils/hostruntime/hostruntime.py
verify_results
classmethod
¶
Verify the results of the kernel execution against reference data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
io_args
|
list[Tensor]
|
List of input/output tensors. |
required |
refs
|
dict | None
|
Dictionary mapping index to reference numpy array. Defaults to None (empty dict). |
None
|
verbosity
|
int
|
Verbosity level. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
Number of errors found. |
Raises:
| Type | Description |
|---|---|
HostRuntimeError
|
If a reference index is out of bounds. |
Source code in python/utils/hostruntime/hostruntime.py
run_test ¶
Run a test for the given NPU kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
npu_kernel
|
NPUKernel
|
The NPU kernel to test. |
required |
io_args
|
list[Tensor]
|
List of input/output tensors. |
required |
ref
|
dict
|
Reference data for verification. |
required |
verify
|
bool
|
Whether to verify results. Defaults to True. |
True
|
verbosity
|
int
|
Verbosity level. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
0 if successful, 1 otherwise. |
Source code in python/utils/hostruntime/hostruntime.py
Tensor and device utilities¶
Device selection, the abstract Tensor type, and numerical helpers used when
comparing host results.
Host runtime utilities: device selection, tensor allocation, and numerical helpers.
Tensor ¶
Bases: ABC
Tensor object backed by NPU or CPU memory.
The class provides common tensor operations such as creation, filling with values, and accessing data.
Initialize the tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape_or_data
|
tuple or array - like
|
|
required |
dtype
|
dtype
|
Data type of the tensor. Defaults to np.uint32. |
uint32
|
device
|
str
|
Device string identifier (e.g., 'npu', 'cpu'). Defaults to 'npu'. |
'npu'
|
Source code in python/utils/hostruntime/tensor_class.py
data
abstractmethod
property
¶
Subclasses must implement a data property.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The underlying data of the tensor. |
shape
abstractmethod
property
¶
Subclasses must implement a shape property.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, ...]
|
The shape of the tensor. |
to ¶
Moves the tensor to a specified target device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_device
|
str
|
The target device. |
required |
Returns:
| Type | Description |
|---|---|
|
The tensor object on the target device. |
Source code in python/utils/hostruntime/tensor_class.py
numpy ¶
Returns a NumPy view of the tensor data on host memory.
This method ensures that data is first synchronized from the device (e.g., NPU) to the host before returning the array.
Returns:
| Type | Description |
|---|---|
|
np.ndarray: The tensor's data as a NumPy array. |
Note: For NPU tensors, this method causes implicit data synchronization from device to host to ensure the returned array reflects the current device state.
Source code in python/utils/hostruntime/tensor_class.py
to_torch ¶
Returns a torch tensor sharing the data in this tensor if possible.
Syncs from device first if the tensor is on the NPU.
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: A torch tensor containing the data. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch is not installed. |
Source code in python/utils/hostruntime/tensor_class.py
torch_view ¶
Returns a torch tensor sharing this buffer's host memory without syncing from device.
Unlike to_torch(), this does NOT sync from the NPU first. Marks the buffer as CPU-resident so that a subsequent .to("npu") call (or the NPU operator's implicit sync) will push the written data to device. Use this on write paths where the caller is about to overwrite the buffer contents.
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: A zero-copy torch tensor view of the host-side buffer. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch is not installed. |
Source code in python/utils/hostruntime/tensor_class.py
from_torch
classmethod
¶
Returns a tensor with a copy of the data in the torch_tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
torch_tensor
|
Tensor
|
The source torch tensor. |
required |
device
|
str
|
The target device. Defaults to None. |
None
|
**kwargs
|
Additional arguments for tensor creation. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A new tensor containing the data from the torch tensor. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch is not installed. |
Source code in python/utils/hostruntime/tensor_class.py
fill_ ¶
Fills the tensor with a scalar value (in-place operation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The scalar value to fill the tensor with. |
required |
Note: For NPU tensors, this method syncs the filled data to device after modification.
Source code in python/utils/hostruntime/tensor_class.py
numel ¶
Calculates the number of elements in the tensor.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The total number of elements in the tensor. |
ones
classmethod
¶
Returns a tensor filled with ones, with shape defined by size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int...
|
Shape of the tensor, passed as separate ints or a single tuple/list. |
()
|
out
|
Tensor
|
Optional output tensor to write into. |
None
|
dtype
|
dtype
|
Desired dtype. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
**kwargs
|
Additional keyword args. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A one-filled tensor. |
Source code in python/utils/hostruntime/tensor_class.py
zeros
classmethod
¶
Returns a tensor filled with zeros, with shape defined by size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int...
|
Shape of the tensor, passed as separate ints or a single tuple/list. |
()
|
out
|
Tensor
|
Optional output tensor to write into. |
None
|
dtype
|
dtype
|
Desired dtype. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
**kwargs
|
Additional keyword args. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A zero-filled tensor. |
Source code in python/utils/hostruntime/tensor_class.py
full
classmethod
¶
Returns a tensor of shape size filled with fill_value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int or tuple/list of int
|
Shape of the returned tensor. |
required |
fill_value
|
scalar
|
Value to fill the tensor with. |
required |
out
|
Tensor
|
Optional output tensor to write into. |
None
|
dtype
|
dtype
|
Desired dtype. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
**kwargs
|
Additional keyword args. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A tensor filled with |
Source code in python/utils/hostruntime/tensor_class.py
randint
classmethod
¶
Returns a tensor filled with random integers uniformly sampled from [low, high).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
int
|
Lowest integer to be drawn (inclusive). |
required |
high
|
int
|
One above the highest integer to be drawn (exclusive). |
required |
size
|
tuple
|
Shape of the returned tensor. |
required |
out
|
Tensor
|
Optional tensor to write the result into. |
None
|
dtype
|
dtype
|
Data type. Defaults to np.int64. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
generator
|
Generator
|
Source RNG for reproducibility. If None, uses np.random module-level state. |
None
|
**kwargs
|
Additional arguments passed to the constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A tensor with random integers. |
Source code in python/utils/hostruntime/tensor_class.py
rand
classmethod
¶
Returns a tensor filled with random numbers from a uniform distribution on [0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int...
|
Variable number of integers or a single tuple defining the shape. |
()
|
out
|
Tensor
|
Output tensor to write into. |
None
|
dtype
|
dtype
|
Desired data type. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
generator
|
Generator
|
Source RNG for reproducibility. If None, uses np.random module-level state. |
None
|
**kwargs
|
Additional arguments passed to constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A tensor with random values in [0, 1). |
Source code in python/utils/hostruntime/tensor_class.py
arange
classmethod
¶
Returns a tensor with values from the interval [start, end) with spacing step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
number
|
Start of interval. Defaults to 0. |
0
|
end
|
number
|
End of interval (exclusive). Required if only one argument is given. |
None
|
step
|
number
|
Gap between elements. Defaults to 1. |
1
|
shape
|
tuple
|
If given, reshape the 1-D sequence to this shape.
|
None
|
dtype
|
dtype
|
Desired output data type. Inferred if not provided. |
None
|
out
|
Tensor
|
Optional tensor to write output to (must match shape and dtype). |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor containing the sequence (1-D by default, or |
Source code in python/utils/hostruntime/tensor_class.py
zeros_like
classmethod
¶
Creates a new tensor with the same shape as other, filled with zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Tensor
|
The reference tensor to copy shape from. |
required |
dtype
|
dtype
|
Data type of the new tensor. Defaults to other's dtype. |
None
|
device
|
str
|
Target device. Defaults to other's device. |
None
|
**kwargs
|
Additional keyword arguments forwarded to the constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A new zero-filled tensor with the same shape. |
Source code in python/utils/hostruntime/tensor_class.py
set_current_device ¶
Set the current device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
Device
|
The device to set as current. |
required |
bfloat16_safe_allclose ¶
Check if two arrays are element-wise equal within a tolerance, handling bfloat16 safely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
The data type of the arrays. |
required | |
arr1
|
First input array. |
required | |
arr2
|
Second input array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the arrays are equal within tolerance, False otherwise. |
Source code in python/utils/hostruntime/__init__.py
Tensor ¶
Bases: ABC
Tensor object backed by NPU or CPU memory.
The class provides common tensor operations such as creation, filling with values, and accessing data.
Initialize the tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape_or_data
|
tuple or array - like
|
|
required |
dtype
|
dtype
|
Data type of the tensor. Defaults to np.uint32. |
uint32
|
device
|
str
|
Device string identifier (e.g., 'npu', 'cpu'). Defaults to 'npu'. |
'npu'
|
Source code in python/utils/hostruntime/tensor_class.py
data
abstractmethod
property
¶
Subclasses must implement a data property.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The underlying data of the tensor. |
shape
abstractmethod
property
¶
Subclasses must implement a shape property.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, ...]
|
The shape of the tensor. |
to ¶
Moves the tensor to a specified target device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_device
|
str
|
The target device. |
required |
Returns:
| Type | Description |
|---|---|
|
The tensor object on the target device. |
Source code in python/utils/hostruntime/tensor_class.py
numpy ¶
Returns a NumPy view of the tensor data on host memory.
This method ensures that data is first synchronized from the device (e.g., NPU) to the host before returning the array.
Returns:
| Type | Description |
|---|---|
|
np.ndarray: The tensor's data as a NumPy array. |
Note: For NPU tensors, this method causes implicit data synchronization from device to host to ensure the returned array reflects the current device state.
Source code in python/utils/hostruntime/tensor_class.py
to_torch ¶
Returns a torch tensor sharing the data in this tensor if possible.
Syncs from device first if the tensor is on the NPU.
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: A torch tensor containing the data. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch is not installed. |
Source code in python/utils/hostruntime/tensor_class.py
torch_view ¶
Returns a torch tensor sharing this buffer's host memory without syncing from device.
Unlike to_torch(), this does NOT sync from the NPU first. Marks the buffer as CPU-resident so that a subsequent .to("npu") call (or the NPU operator's implicit sync) will push the written data to device. Use this on write paths where the caller is about to overwrite the buffer contents.
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: A zero-copy torch tensor view of the host-side buffer. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch is not installed. |
Source code in python/utils/hostruntime/tensor_class.py
from_torch
classmethod
¶
Returns a tensor with a copy of the data in the torch_tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
torch_tensor
|
Tensor
|
The source torch tensor. |
required |
device
|
str
|
The target device. Defaults to None. |
None
|
**kwargs
|
Additional arguments for tensor creation. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A new tensor containing the data from the torch tensor. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If torch is not installed. |
Source code in python/utils/hostruntime/tensor_class.py
fill_ ¶
Fills the tensor with a scalar value (in-place operation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The scalar value to fill the tensor with. |
required |
Note: For NPU tensors, this method syncs the filled data to device after modification.
Source code in python/utils/hostruntime/tensor_class.py
numel ¶
Calculates the number of elements in the tensor.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The total number of elements in the tensor. |
ones
classmethod
¶
Returns a tensor filled with ones, with shape defined by size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int...
|
Shape of the tensor, passed as separate ints or a single tuple/list. |
()
|
out
|
Tensor
|
Optional output tensor to write into. |
None
|
dtype
|
dtype
|
Desired dtype. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
**kwargs
|
Additional keyword args. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A one-filled tensor. |
Source code in python/utils/hostruntime/tensor_class.py
zeros
classmethod
¶
Returns a tensor filled with zeros, with shape defined by size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int...
|
Shape of the tensor, passed as separate ints or a single tuple/list. |
()
|
out
|
Tensor
|
Optional output tensor to write into. |
None
|
dtype
|
dtype
|
Desired dtype. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
**kwargs
|
Additional keyword args. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A zero-filled tensor. |
Source code in python/utils/hostruntime/tensor_class.py
full
classmethod
¶
Returns a tensor of shape size filled with fill_value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int or tuple/list of int
|
Shape of the returned tensor. |
required |
fill_value
|
scalar
|
Value to fill the tensor with. |
required |
out
|
Tensor
|
Optional output tensor to write into. |
None
|
dtype
|
dtype
|
Desired dtype. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
**kwargs
|
Additional keyword args. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A tensor filled with |
Source code in python/utils/hostruntime/tensor_class.py
randint
classmethod
¶
Returns a tensor filled with random integers uniformly sampled from [low, high).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
int
|
Lowest integer to be drawn (inclusive). |
required |
high
|
int
|
One above the highest integer to be drawn (exclusive). |
required |
size
|
tuple
|
Shape of the returned tensor. |
required |
out
|
Tensor
|
Optional tensor to write the result into. |
None
|
dtype
|
dtype
|
Data type. Defaults to np.int64. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
generator
|
Generator
|
Source RNG for reproducibility. If None, uses np.random module-level state. |
None
|
**kwargs
|
Additional arguments passed to the constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A tensor with random integers. |
Source code in python/utils/hostruntime/tensor_class.py
rand
classmethod
¶
Returns a tensor filled with random numbers from a uniform distribution on [0, 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*size
|
int...
|
Variable number of integers or a single tuple defining the shape. |
()
|
out
|
Tensor
|
Output tensor to write into. |
None
|
dtype
|
dtype
|
Desired data type. Defaults to np.float32. |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
generator
|
Generator
|
Source RNG for reproducibility. If None, uses np.random module-level state. |
None
|
**kwargs
|
Additional arguments passed to constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A tensor with random values in [0, 1). |
Source code in python/utils/hostruntime/tensor_class.py
arange
classmethod
¶
Returns a tensor with values from the interval [start, end) with spacing step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
number
|
Start of interval. Defaults to 0. |
0
|
end
|
number
|
End of interval (exclusive). Required if only one argument is given. |
None
|
step
|
number
|
Gap between elements. Defaults to 1. |
1
|
shape
|
tuple
|
If given, reshape the 1-D sequence to this shape.
|
None
|
dtype
|
dtype
|
Desired output data type. Inferred if not provided. |
None
|
out
|
Tensor
|
Optional tensor to write output to (must match shape and dtype). |
None
|
device
|
str
|
Target device. Defaults to 'npu'. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor containing the sequence (1-D by default, or |
Source code in python/utils/hostruntime/tensor_class.py
zeros_like
classmethod
¶
Creates a new tensor with the same shape as other, filled with zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Tensor
|
The reference tensor to copy shape from. |
required |
dtype
|
dtype
|
Data type of the new tensor. Defaults to other's dtype. |
None
|
device
|
str
|
Target device. Defaults to other's device. |
None
|
**kwargs
|
Additional keyword arguments forwarded to the constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
A new zero-filled tensor with the same shape. |
Source code in python/utils/hostruntime/tensor_class.py
Shared CLI/argument helpers¶
Reusable argparse flag groups and the standard design CLI dispatcher used by
the programming examples.
Reusable argparse flag groups shared by the programming examples.
Almost every basic/ design repeats the same handful of CLI flags:
-d/--dev (device selector), --xclbin-path/--insts-path
(compile-only output paths), optionally --elf-path (xrt::elf
testbench) and --emit-mlir (aiecc / vck5000 path), and the
benchmark/trace pair -w/--warmup / -i/--iters / -t/--trace_size.
The helpers in this module mutate an existing argparse.ArgumentParser
(so each design can keep its own design-specific flags and prog).
add_compile_args ¶
add_compile_args(
parser: ArgumentParser,
*,
with_dev: bool = True,
dev_choices: tuple[str, ...] = DEFAULT_DEV_CHOICES,
default_dev: str | None = None,
short_dev: str | None = "-d",
with_elf: bool = False,
with_full_elf: bool = False,
with_pdi: bool = False,
with_emit_mlir: bool = False
) -> None
Add the standard compile-mode flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
ArgumentParser
|
Parser to mutate. |
required |
with_dev
|
bool
|
If True (default), add |
True
|
dev_choices
|
tuple[str, ...]
|
Allowed device-name strings (default
|
DEFAULT_DEV_CHOICES
|
default_dev
|
str | None
|
Fixed target used when |
None
|
short_dev
|
str | None
|
Short-option for |
'-d'
|
with_elf
|
bool
|
When True, also add |
False
|
with_full_elf
|
bool
|
When True, also add |
False
|
with_pdi
|
bool
|
When True, also add |
False
|
with_emit_mlir
|
bool
|
When True, also add |
False
|
Source code in python/utils/hostruntime/argparse.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
add_benchmark_args ¶
add_benchmark_args(
parser: ArgumentParser,
*,
default_warmup: int = 2,
default_iters: int = 5
) -> None
Add the standard benchmark flags: -w/--warmup and -i/--iters.
Source code in python/utils/hostruntime/argparse.py
add_trace_arg ¶
Add the standard --trace_size flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
ArgumentParser
|
Parser to mutate. |
required |
with_short
|
bool
|
When True (default), exposes |
True
|
default
|
int
|
Default trace size in bytes ( |
0
|
Source code in python/utils/hostruntime/argparse.py
add_runtime_args ¶
add_runtime_args(
parser: ArgumentParser,
*,
with_io_sizes: bool = False,
with_benchmark: bool = False
) -> None
Add the standard runtime / test-harness flags.
Pairs with :func:add_compile_args: add_compile_args covers the
write-side flags (--xclbin-path, --insts-path) used by JIT
designs, while this helper covers the read-side flags (--xclbin,
--instr) used by test.py-style host harnesses that load a
pre-compiled xclbin and run it on the NPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
ArgumentParser
|
Parser to mutate. |
required |
with_io_sizes
|
bool
|
When True, adds |
False
|
with_benchmark
|
bool
|
When True, also calls :func: |
False
|
Adds (always): --xclbin, --instr, -k/--kernel,
-v/--verbosity, --verify/--no-verify, --trace-file,
--ddr-id, --enable-ctrl-pkts; and via :func:add_trace_arg,
-t/--trace_size.
Source code in python/utils/hostruntime/argparse.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
device_from_args ¶
Resolve a parsed-argparse namespace to a :class:aie.iron.device.Device.
Collapses the boilerplate variants the example suite used to repeat across ~30 sites::
from_name(args.dev, n_cols=None) # "use all cols"
from_name(args.dev, n_cols=1) # "single col"
into one helper with an explicit n_cols parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
An |
required | |
dev_attr
|
str
|
Name of the device-string attribute on |
'dev'
|
n_cols
|
'int | None | str'
|
Column-count selector. |
'auto'
|
Returns:
| Type | Description |
|---|---|
|
Device | None: The selected device, or |
|
|
device selection to the runtime. |
Source code in python/utils/hostruntime/argparse.py
run_design_cli — standard 3-mode dispatcher for IRON design CLIs.
Almost every basic/ design's main() is the same skeleton:
def main():
opts = _make_argparser().parse_args()
# optional: _validate(opts)
if opts.emit_mlir:
print(design.specialize(**_compile_kwargs(opts)).as_mlir()); return
if opts.xclbin_path:
_compile_only(opts)
return
_run_and_verify(opts)
…where _compile_only always does the same --insts-path check +
device binding + design.specialize(**kw).compile(
xclbin_path=opts.xclbin_path, inst_path=opts.insts_path
[, elf_path=opts.elf_path][, pdi_path=opts.pdi_path]).
This module wraps that skeleton so each design just declares the two
pieces it actually owns (compile kwargs, the verify body) and lets the
dispatcher do the branching + the boilerplate around it. An optional
emit_mlir= callback covers the rare design whose generator needs
real iron.tensor instances at MLIR-gen time.
run_design_cli ¶
run_design_cli(
design,
opts,
*,
compile_kwargs: (
Mapping[str, Any]
| Callable[[Any], Mapping[str, Any]]
),
run_and_verify: Callable[[Any], None] | None = None,
device: _DeviceArg | None = None,
emit_mlir: Callable[[Any], None] | None = None,
validate: Callable[[Any], None] | None = None
) -> None
Standard 3-mode CLI dispatcher for basic/ designs.
The standard branch tree (in order):
- Bind an explicit
--devtarget or concretedeviceargument, or detect the attached runtime device for run and local compile-only modes. - If
validateis given, callvalidate(opts). -
If
opts.emit_mliris True, then:- If an
emit_mlircallback was supplied, callemit_mlir(opts). - Otherwise print
design.specialize(**compile_kwargs).as_mlir()— the right answer for almost every design (no tensor args needed when shapes come fromCompileTime[T]params).
Then return.
- If an
-
If
opts.xclbin_pathoropts.full_elf_pathis set (compile-only):- For
full_elf_path: calldesign.specialize(**compile_kwargs) .compile(full_elf_path=opts.full_elf_path)— a single self-contained ELF, no xclbin/insts. - Otherwise refuse if
opts.insts_pathis unset (sys.exitwith the standard message), then calldesign.specialize(**compile_kwargs).compile( xclbin_path=opts.xclbin_path, inst_path=opts.insts_path, [elf_path=opts.elf_path]).
- For
-
Otherwise, call
run_and_verify(opts).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
design
|
The |
required | |
opts
|
Parsed |
required | |
compile_kwargs
|
Mapping[str, Any] | Callable[[Any], Mapping[str, Any]]
|
Either a dict OR a callable that takes |
required |
run_and_verify
|
Callable[[Any], None] | None
|
Callable invoked in the default (NPU
run + numpy verify) branch. Takes |
None
|
device
|
_DeviceArg | None
|
Optional iron |
None
|
emit_mlir
|
Callable[[Any], None] | None
|
Optional callable for the |
None
|
validate
|
Callable[[Any], None] | None
|
Optional callable invoked after target selection — e.g. for shape / arg consistency checks that should fire in all modes. |
None
|
Source code in python/utils/hostruntime/cli.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
XRT runtime implementation¶
Concrete XRT-backed runtime, tensor, and scratchpad types. These modules import
pyxrt, so they are only importable in environments where XRT / PyXRT is
installed. MkDocs CI does not install XRT, so these symbols are summarized here
(same approach as the non-importable JIT helpers on the IRON page) with links to
source. On a machine with PyXRT available they can also be rendered with
mkdocstrings, for example:
| Symbol | Module | Summary |
|---|---|---|
XRTKernelHandle |
xrtruntime.hostruntime |
Handle for a loaded XRT kernel. |
XRTKernelResult |
xrtruntime.hostruntime |
Result wrapper for a PyXRT kernel run. |
XRTHostRuntime |
xrtruntime.hostruntime |
Singleton manager for AIE XRT resources. |
CachedXRTKernelHandle |
xrtruntime.hostruntime |
Cached handle for a loaded XRT kernel. |
CachedXRTRuntime |
xrtruntime.hostruntime |
Cached XRTHostRuntime that reuses contexts for the same xclbin. |
XRTTensor |
xrtruntime.tensor |
Tensor backed by NPU/CPU-accessible memory managed with PyXRT. |
ParameterScratchpad |
xrtruntime.parameter_scratchpad |
Write named runtime parameters to the NPU scratchpad buffer. |