Python API¶
RBLN compile API¶
RBLNCompiledModel ¶
A class that holds the compiled binaries.
An instance is returned by the compile_from_* functions, or loaded from a saved .rbln file by passing its path to the constructor.
__init__(compiled_model) ¶
Load a compiled model from a .rbln file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compiled_model | PathLike or str | Path to the | required |
Raises:
| Type | Description |
|---|---|
TypeError | If |
save(path) ¶
Serialize and save the model to the disk, .rbln formatted file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | PathLike | Path to save serialized data | required |
create_runtime(*, device=0, tensor_type='np') ¶
Create a runtime from this compiled model. Note that this function is exclusive to create_async_runtime. Once you create a runtime by create_runtime with the instance, you can't call create_async_runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device | int | The device ID of the NPU to use for execution. Defaults to 0. | 0 |
tensor_type | str | The object type of the tensor used in the
Defaults to "np". | 'np' |
Returns:
| Name | Type | Description |
|---|---|---|
Runtime | Runtime | Runtime object that can be run on the RBLN NPU. |
Raises:
| Type | Description |
|---|---|
RBLNRuntimeError | If the requested NPU is missing or does not match the compiled model. |
create_async_runtime(*, device=0, tensor_type='np', parallel=None) ¶
Create an asynchronous runtime from this compiled model. Note that this function is exclusive to create_runtime. Once you create an asynchronous runtime by create_async_runtime with the instance, you can't call create_runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device | int | The device ID of the NPU to use for execution. Defaults to 0. | 0 |
tensor_type | str | The object type of the tensor used in the
Defaults to "np". | 'np' |
parallel | int | The number of threads used to prepare and queue input data for the NPU's input buffer. The NPU supports double buffering, allowing it to process one input while the next is being prepared. Possible values are:
This parameter helps manage the NPU's input buffer more efficiently, ensuring that the next input is ready as soon as the current computation finishes. Only applicable to AsyncRuntime due to its non-blocking nature. Note: Use parallel = 2 with caution, as it may not work on some models (e.g. LLM). It's recommended to benchmark your specific use case to determine if it provides a performance benefit. | None |
Returns:
| Name | Type | Description |
|---|---|---|
AsyncRuntime | AsyncRuntime | Asynchronous runtime object that can be run on the RBLN NPU. |
Raises:
| Type | Description |
|---|---|
ValueError | If |
get_total_device_alloc(parallel=1) ¶
Retrieve the total device memory allocation (in bytes) required for the compiled graph across all NPUs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parallel | int | If parallel > 1, the returned allocation size accounts for additional buffer requirements when operating in non-blocking (asynchronous) mode. Defaults to 1. | 1 |
Returns:
| Name | Type | Description |
|---|---|---|
int | int | The total device memory allocation (in bytes) required for the compiled graph. |
get_alloc_per_node(parallel=1) ¶
Retrieve the device memory allocation (in bytes) required for the compiled graph on each individual NPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parallel | int | If parallel > 1, the returned allocation size accounts for additional buffer requirements when operating in non-blocking (asynchronous) mode. Defaults to 1. | 1 |
Returns:
| Type | Description |
|---|---|
List[int] | List[int]: A list containing the device memory allocation (in bytes) for each NPU. The length of the list equals the |
inspect(path) classmethod ¶
Return metadata about the compiled model without loading it into host memory.
This method retrieves essential details such as:
- Estimated memory usage per device
- Compiler version used for compilation
- I/O Tensor's name, shape, and dtype
- Name of the compiled model
- Target NPU type
- Number of required devices (
num_devices) - Unique identifier (UUID) for the compiled model
- Reasons for graph breaks (If device_func_count == 1, it means no graph breaks occurred)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | PathLike | Path to the compiled RBLN model file. | required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any] | Dict[str, Any]: Dictionary containing metadata information about the compiled model. |
Example
Output
compile_from_torch(mod, input_info=None, example_inputs=None, *, npu=None, num_devices=None, model_trace_method=None) ¶
Compile a model from torch.nn.Module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mod | Module | A PyTorch model ( | required |
input_info | List[Tuple[str, List[int], DType]] | Input configuration information. Supports two formats: Single Input Configuration: A list of input information entries, each described as a (name, shape, dtype) tuple.
Bucketing (Multiple Input Configurations): A list of input configurations, where each configuration is itself a list of input information. This enables the compiled model to support multiple input shapes efficiently within a single runtime, allowing automatic bucket selection based on input shape at inference time. For detailed bucketing examples and best practices, see bucketing tutorial. | None |
example_inputs | List[Tensor] | A list of example input torch tensors that can be used for tracing. If None, tracing may use default dummy inputs obtained from input_info. | None |
npu | str | The identifier of the target NPU for compilation. If None, the function will use the NPU installed on the host machine. If an NPU is not installed on the host machine, an error will be raised. Defaults to None. | None |
num_devices | int | The number of NPU devices to distribute the model across. The parallelization strategy (tensor / pipeline / etc.) is decided by the compiler. If None, the model is compiled for a single device. Defaults to None. ( | None |
model_trace_method | str | The method used to trace the given model. If unspecified, the function will first try to trace the model with torch.export.export non-strict mode. If it fails, it will try to trace the model with torch.jit.trace. If 'export' is given, the model is traced by torch.export.export non-strict mode. If 'export_strict' is given, the model is traced by torch.export.export strict mode. If 'jittrace' is given, the model is traced by torch.jit.trace. | None |
Returns:
| Name | Type | Description |
|---|---|---|
RBLNCompiledModel | RBLNCompiledModel | Compiled model that can be run on the RBLN NPU. |
compile_from_torchscript(mod, input_names=None, *, npu=None, num_devices=None) ¶
Compile a model from torch.jit.ScriptModule, a result of torch.jit.trace function.
Note that the input shape and dtype information must be kept to compile a TorchScript model. When a TorchScript model is loaded with torch.jit.load, this information is not restored by default. Pass _restore_shapes=True to restore it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mod | TorchScript | A PyTorch jit-traced model. | required |
input_names | Optional[List[Optional[str]]] | A list of input names. If | None |
npu | str | The identifier of the target NPU for compilation. If None, the function will use the NPU installed on the host machine. If an NPU is not installed on the host machine, an error will be raised. Defaults to None. | None |
num_devices | int | The number of NPU devices to distribute the model across. The parallelization strategy (tensor / pipeline / etc.) is decided by the compiler. If None, the model is compiled for a single device. Defaults to None. ( | None |
Returns:
| Name | Type | Description |
|---|---|---|
RBLNCompiledModel | RBLNCompiledModel | Compiled model that can be run on the RBLN NPU. |
compile_from_tf_function(func, input_info, outputs=None, layout='NHWC', *, npu=None) ¶
Compile a model from tf.function. Note that the input function should not be concretized by get_concrete_function method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func | GenericFunction | A TensorFlow function | required |
input_info | List[Tuple[str, List[int], DType]] | A list of input information entries, each described as a (name, shape, dtype) tuple. If the dtype is specified as | required |
outputs | Optional[Union[str, List[str]]] | A string or list of the name of output node(s) (Optional). If not specified, then the last node is assumed to be the graph output. This may be useful when the graph has multiple outputs. | None |
layout | str | Layout of the tensor used internally in the model. One of "NHWC" or "NCHW" | 'NHWC' |
npu | str | The identifier of the target NPU for compilation. If None, the function will use the NPU installed on the host machine. If an NPU is not installed on the host machine, an error will be raised. Defaults to None. | None |
Returns:
| Name | Type | Description |
|---|---|---|
RBLNCompiledModel | RBLNCompiledModel | Compiled model that can be run on the RBLN NPU. |
compile_from_tf_graph_def(graph_def, outputs=None, layout='NHWC', *, npu=None) ¶
Compile a model from TensorFlow GraphDef. This function allows you to compile TensorFlow V1.x legacy models. If you are using TensorFlow V2 as a default, we recommend compiling the model using compile_from_tf_function in its function form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_def | GraphDef | A TensorFlow graph definition in the form of a protocol buffer | required |
outputs | Optional[Union[str, List[str]]] | A string or list of the name of output node(s) (Optional). If not specified, then the last node is assumed to be the graph output. This may be useful when the graph has multiple outputs. | None |
layout | str | Layout of the tensor used internally in the model. One of "NHWC" or "NCHW" | 'NHWC' |
npu | str | The identifier of the target NPU for compilation. If None, the function will use the NPU installed on the host machine. If an NPU is not installed on the host machine, an error will be raised. Defaults to None. | None |
Returns:
| Name | Type | Description |
|---|---|---|
RBLNCompiledModel | RBLNCompiledModel | Compiled model that can be run on the RBLN NPU. |
torch.compile API¶
compile(model=None, *, dynamic=None, backend='inductor', options=None) ¶
Optimize the given model/function using TorchDynamo with the RBLN backend for execution on RBLN NPUs.
This function compiles the input model to run efficiently on RBLN NPUs. It leverages TorchDynamo for tracing and the RBLN backend for generating optimized code tailored to RBLN NPUs. To use this backend, run import rebel before calling torch.compile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | Callable | The module or function to optimize. | None |
dynamic | bool or None | Use dynamic shape tracing. The RBLN backend does not support general dynamic shapes. Set this to | None |
backend | str or Callable | The backend to use.
| 'inductor' |
options | dict | A dictionary of options to pass to the backend. Notable options for the RBLN backend are:
| None |
runtime API¶
RuntimeBase ¶
A base class for runtime, providing common functionalities for both synchronous and asynchronous runtime.
The RuntimeBase class serves as a foundational class for the Runtime and AsyncRuntime classes. It encapsulates shared methods and attributes that manage the execution of the model.
num_threads property writable ¶
The current number of threads used by the system.
Returns:
| Name | Type | Description |
|---|---|---|
int | int | The current number of threads. |
model_description() ¶
Return a description of the model currently loaded in the runtime.
This method provides a summary of the model's architecture, including details about its inputs, outputs, and memory usage on the RBLN NPU.
Returns:
| Name | Type | Description |
|---|---|---|
str | str | A string containing the model's description. |
flush_reports() ¶
Fetch and discard all pending reports from the runtime.
This method streams all available reports using _stream_reports() and then clears the internal _reports list.
get_reports() ¶
Retrieve all pending reports from the runtime.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]] | List[str]: A list of report dictionaries in string format. |
This method fetches all available reports and returns their contents while preserving their order.
Note
Timer reports are only available when the environment variable RBLN_RUNTIME_TIMER=1 is set. Without this setting, timer reports will not be generated.
Example
get_elapsed_times() ¶
Calculate the average total execution time of operations in microseconds.
Returns:
| Name | Type | Description |
|---|---|---|
float | float | Average execution time in microseconds, or 0.0 if no timer reports are available. |
This method iterates over the available reports, sums the total values for reports, and computes the average across all such reports.
Note
Timer reports are only available when the environment variable RBLN_RUNTIME_TIMER=1 is set. Without this setting, timer reports will not be generated.
get_elapsed_device_times() ¶
Calculate the average device-side execution time of operations in microseconds.
Returns:
| Name | Type | Description |
|---|---|---|
float | float | Average device execution time in microseconds, or 0.0 if no timer reports are available. |
This method iterates over the available reports, sums the total_device values for reports, and computes the average across all such reports.
Note
Timer reports are only available when the environment variable RBLN_RUNTIME_TIMER=1 is set. Without this setting, timer reports will not be generated.
Runtime ¶
A Runtime object for executing a compiled neural network on an NPU.
__init__(compiled_model, *, device=0, tensor_type='np', activate_profiler=False, timeout=None) ¶
Initialize a Runtime object for executing a compiled neural network on an NPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compiled_model | str, PathLike, or RBLNCompiledModel | The path to the compiled RBLN model file ( | required |
device | int | The device ID of the NPU to use for execution. Defaults to 0. | 0 |
tensor_type | str | The object type of the tensor used in the
Defaults to "np". | 'np' |
activate_profiler | bool | Whether to activate profiling for this runtime instance. If set to | False |
timeout | int | The timeout for the runtime in seconds. If the runtime exceeds this timeout, it will be terminated. If not provided, the runtime will use the default timeout value (60 seconds). | None |
Raises:
| Type | Description |
|---|---|
TypeError | If |
run(*input_args, out=None, **input_kwargs) ¶
Run the compiled neural network with the given input tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*input_args | Union[ndarray, Tensor] | Variable length argument list of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | () |
out | Optional[List[Union[ndarray, Tensor]]] | An optional list or tensor to store the output tensors. If provided, it must contain pre-allocated tensors with shapes matching the network's output shapes. If not provided or set to | None |
**input_kwargs | Union[ndarray, Tensor] | Arbitrary keyword arguments of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | {} |
Returns:
| Type | Description |
|---|---|
Union[ndarray, Tensor, List[Union[ndarray, Tensor]]] | The output tensor(s) of the neural network. The return depends on the network's architecture and can be either a single tensor or a list of tensors. The tensor type (numpy.ndarray or torch.Tensor) is determined by the tensor_type provided during the Runtime object's initialization. |
forward(*input_args, out=None, **input_kwargs) ¶
An alias for the run method.
This method is provided for compatibility with PyTorch's naming convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*input_args | Union[ndarray, Tensor] | Variable length argument list of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | () |
out | Optional[List[Union[ndarray, Tensor]]] | An optional list or tensor to store the output tensors. If provided, it must contain pre-allocated tensors with shapes matching the network's output shapes. If not provided or set to | None |
**input_kwargs | Union[ndarray, Tensor] | Arbitrary keyword arguments of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | {} |
Returns:
| Type | Description |
|---|---|
Union[ndarray, Tensor, List[Union[ndarray, Tensor]]] | The output tensor(s) of the neural network, as returned by the |
__call__(*input_args, out=None, **input_kwargs) ¶
Allow the Runtime object to be called as a function.
This method is provided for convenience and compatibility with common neural network frameworks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*input_args | Union[ndarray, Tensor] | Variable length argument list of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | () |
out | Optional[List[Union[ndarray, Tensor]]] | An optional list or tensor to store the output tensors. If provided, it must contain pre-allocated tensors with shapes matching the network's output shapes. If not provided or set to | None |
**input_kwargs | Union[ndarray, Tensor] | Arbitrary keyword arguments of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | {} |
Returns:
| Type | Description |
|---|---|
Union[ndarray, Tensor, List[Union[ndarray, Tensor]]] | The output tensor(s) of the neural network, as returned by the |
AsyncRuntime ¶
An AsyncRuntime object for executing a compiled neural network asynchronously on an NPU.
__init__(compiled_model, *, device=0, tensor_type='np', parallel=None, timeout=None) ¶
Initialize an AsyncRuntime object for executing a compiled neural network asynchronously on an NPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compiled_model | str, PathLike, or RBLNCompiledModel | The path to the compiled RBLN model file ( | required |
device | int | The device ID of the NPU to use for execution. Defaults to 0. | 0 |
tensor_type | str | The object type of the tensor used in the
Defaults to "np". | 'np' |
parallel | int | The number of threads used to prepare and queue input data for the NPU's input buffer. The NPU supports double buffering, allowing it to process one input while the next is being prepared. Possible values are:
This parameter helps manage the NPU's input buffer more efficiently, ensuring that the next input is ready as soon as the current computation finishes. Only applicable to AsyncRuntime due to its non-blocking nature. Note: Use parallel = 2 with caution, as it may not work on some models (e.g. LLM). It's recommended to benchmark your specific use case to determine if it provides a performance benefit. | None |
timeout | int | The timeout for the runtime in seconds. If the runtime exceeds this timeout, it will be terminated. If not provided, the runtime will use the default timeout value (60 seconds). | None |
Raises:
| Type | Description |
|---|---|
ValueError | If |
run(*input_args, **input_kwargs) ¶
Run the compiled neural network asynchronously with the given input tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*input_args | Union[ndarray, Tensor] | Variable length argument list of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | () |
**input_kwargs | Union[ndarray, Tensor] | Arbitrary keyword arguments of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | {} |
Returns:
| Name | Type | Description |
|---|---|---|
AsyncTask | AsyncTask | An asynchronous task object representing the neural network execution. Call its |
async_run(*input_args, **input_kwargs) async ¶
Run the compiled neural network asynchronously and returns the result awaitably.
This method is a coroutine that can be used with the await keyword to asynchronously run the neural network and retrieve the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*input_args | Union[ndarray, Tensor] | Variable length argument list of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | () |
**input_kwargs | Union[ndarray, Tensor] | Arbitrary keyword arguments of input tensors. Each argument should be either a numpy.ndarray or a torch.Tensor. | {} |
Returns:
| Type | Description |
|---|---|
Union[ndarray, Tensor, List[Union[ndarray, Tensor]]] | The output tensor(s) of the neural network. The return type depends on the network's architecture and can be either a single tensor or a list of tensors. The tensor type (numpy.ndarray or torch.Tensor) is determined by the tensor_type provided during the AsyncRuntime object's initialization. |
AsyncTask ¶
A handle for one asynchronous execution, returned by AsyncRuntime.run.
wait(timeout=None) ¶
Wait for the asynchronous task to complete and returns the result.
This method blocks the calling thread until the task is completed or the specified timeout is reached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout | float | The maximum amount of time (in seconds) to wait for the task to complete. If None, the method will wait indefinitely until the task is completed. Defaults to None. | None |
Returns:
| Type | Description |
|---|---|
Union[ndarray, Tensor, List[Union[ndarray, Tensor]]] | The output tensor(s) of the neural network. The return depends on the network's architecture and can be either a single tensor or a list of tensors. The tensor type (numpy.ndarray or torch.Tensor) is determined by the tensor_type provided during the AsyncRuntime object's initialization. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If the task has already returned its result. |
utility API¶
DeviceProperties ¶
Static properties of an NPU, as returned by get_device_properties.
Attributes:
| Name | Type | Description |
|---|---|---|
name | str | Name of the NPU, e.g. "RBLN-CA25". |
total_memory | int | Total device memory in bytes ( |
memory_per_chiplet | int | Memory of one chiplet in bytes. |
num_chiplet | int | Number of chiplets in the NPU. |
ChipletMemoryInfo ¶
User DRAM pool of one chiplet, in bytes.
Each chiplet allocates from its own pool, so a physically contiguous buffer is bounded by one chiplet's largest_free, not by the device-wide free total.
Attributes:
| Name | Type | Description |
|---|---|---|
total | int | Size of the pool. |
used | int | Bytes in use. |
free | int | Bytes free. |
largest_free | int | Largest free contiguous region. |
largest_free_huge | int | Largest free contiguous region at the huge granule, or 0 where the part has none. |
DeviceMemoryInfo ¶
Device-wide user DRAM usage, in bytes, as returned by get_device_memory_info.
Attributes:
| Name | Type | Description |
|---|---|---|
total | int | Total allocatable memory, summed over |
used | int | Bytes in use, summed over |
free | int | Bytes free, summed over |
granularity | int | Allocation granule in bytes. |
huge_granularity | int | Huge allocation granule in bytes. |
chiplets | List[ChipletMemoryInfo] | Per-chiplet usage. |
npu_is_available(device_id=0) ¶
Return a bool indicating whether the RBLN NPU is currently available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_id | int | Index of the NPU. Defaults to 0. | 0 |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | Whether the RBLN NPU is currently available. |
get_npu_name(device_id=0) ¶
Return the name of the RBLN NPU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_id | int | Index of the NPU. Defaults to 0. | 0 |
Returns:
| Name | Type | Description |
|---|---|---|
str | str | The name of the NPU, e.g. "RBLN-CA12". |
get_num_threads() ¶
Return the number of host threads the runtime uses.
Returns:
| Name | Type | Description |
|---|---|---|
int | int | The current thread count. |
set_num_threads(num_threads) ¶
Set the number of host threads the runtime uses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_threads | int | The thread count to use. | required |
search_num_threads(runtime, max_iterations=200) ¶
Benchmark different thread counts and prints the average execution time for each configuration.
This function tests various thread counts by adjusting the num_threads property of the given runtime instance. For each configuration, it measures the average host execution time over a specified number of iterations and prints the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
runtime | Runtime | The runtime instance whose thread count will be tested. | required |
max_iterations | int | The number of iterations to run for each thread count. A higher value provides more accurate results. Defaults to 200. | 200 |
Example
device_count() ¶
Return the number of NPUs available.
Returns:
| Name | Type | Description |
|---|---|---|
int | int | The number of NPUs available. |
get_device_properties(device_id=0) ¶
Return the static properties the NPU reports about itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_id | int | Index of the NPU. Defaults to 0. | 0 |
Returns:
| Type | Description |
|---|---|
Optional[DeviceProperties] | DeviceProperties or None: The properties of the NPU, or None when no NPU is found at that index. |
get_device_memory_info(device_id=0) ¶
Return the device-wide DRAM usage of the NPU, per chiplet and in total.
This needs no runtime and can be called before a model is loaded. The result is a reading, not a reservation: another process may allocate between this call and yours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_id | int | Index of the NPU. Defaults to 0. | 0 |
Returns:
| Type | Description |
|---|---|
Optional[DeviceMemoryInfo] | DeviceMemoryInfo or None: The memory usage of the NPU, or None when the NPU is not available or the installed driver does not provide the query. |
manual_seed(seed) ¶
Set the manual seed of the RBLN Runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed | int | The seed to set. | required |