Export#
Export lib#
- class aidge_core.export_utils.ExportLib(operator)#
Aidge export library that manages a registry for operators and static files.
This class provides a structure for registering different operator types and export nodes, facilitating access and management of these elements.
- Variables:
_name – The name of the export library, used for namespacing.
static_files – A dictionary mapping paths of static files to their target locations relative to the export root.
- __init__(self: aidge_core.aidge_core.OperatorImpl, arg0: Aidge::Operator, arg1: str) None #
- backend(self: aidge_core.aidge_core.OperatorImpl) str #
- backward(self: aidge_core.aidge_core.OperatorImpl) None #
- forward(self: aidge_core.aidge_core.OperatorImpl) None #
- get_adaptation(self: aidge_core.aidge_core.OperatorImpl, arg0: aidge_core.aidge_core.ImplSpec, arg1: aidge_core.aidge_core.ImplSpec) aidge_core.aidge_core.Node #
- get_available_impl_specs() List[ImplSpec] #
Override the virtual OperatorImpl method, in order to provide available implementation specifications.
- Returns:
List of implementation specification available for the type of operator.
- Return type:
List[aidge_core.ImplSpec]
- get_best_adaptation(self: aidge_core.aidge_core.OperatorImpl, arg0: aidge_core.aidge_core.ImplSpec) aidge_core.aidge_core.Node #
- get_best_match(self: aidge_core.aidge_core.OperatorImpl, arg0: aidge_core.aidge_core.ImplSpec) aidge_core.aidge_core.ImplSpec #
- get_export_node(spec: ImplSpec) ExportNode #
Given an
aidge_core.ImplSpec
, return the ExportNode that is the closest match.- Parameters:
spec (:py:class:`aidge_core.ImplSpec) – Implementation specification to match
- Returns:
The class ExportNode that is the closest match
- Return type:
- get_operator(self: aidge_core.aidge_core.OperatorImpl) Aidge::Operator #
- get_prod_conso(self: aidge_core.aidge_core.OperatorImpl) Aidge::ProdConso #
- get_required_spec(self: aidge_core.aidge_core.OperatorImpl) aidge_core.aidge_core.ImplSpec #
- prod_conso(self: aidge_core.aidge_core.OperatorImpl) Aidge::ProdConso #
- classmethod register(op_type, spec)#
Decorator to register an operator implementation for a specified operator type.
Registers an operator under a given operator type and specification, adding it to the export library registry. This method supports both single operator types (str) and lists of types (List[str]).
- Parameters:
op_type (Union[str, List[str]]) – The operator type(s) to register.
spec (:py:class:
aidge_core.ImplSpec
) – Implementation specification for the operator.
- Returns:
A wrapper class that initializes the registered operator.
- Return type:
Callable
- classmethod register_generic(op_type, spec)#
Decorator to register a GenericOperator with the export library.
Registers a GenericOperator under a given operator type and specification. This decorator is intended for operator types that are grouped as meta operators.
- Parameters:
op_type (Union[str, List[str]]) – Operator type(s) to register as a
GenericOperator
.spec (aidge_core.ImplSpec) – Implementation specification for the GenericOperator.
- Returns:
A wrapper class that initializes the registered GenericOperator.
- Return type:
Callable
- classmethod register_metaop(op_type, spec)#
Decorator to register a MetaOperator with the export library.
Registers a MetaOperator under a given operator type and specification. This decorator is intended for operator types that are grouped as meta operators.
- Parameters:
op_type (Union[str, List[str]]) – Operator type(s) to register as a
MetaOperator
.spec (aidge_core.ImplSpec) – Implementation specification for the MetaOperator.
- Returns:
A wrapper class that initializes the registered MetaOperator.
- Return type:
Callable
Export node#
- class aidge_core.export_utils.ExportNode(aidge_node: Node, mem_info: List[dict] | None = None)#
Abstract class to interface node with export generation.
This class exposes a dictionary,
attributes
, which contains all the information required to generate an export for a given node, including input/output names, dimensions, types, and optional memory information.All the attributes of the Aidge Operator are automatically fetch, the key to get an attribute is the attribute name in python format, example
no_bias
node (aidge_core.Node): The Aidge Node instance associated with this ExportNode.
name (str): Name of the node, typically set via the Aidge node.
nb_in (int): Number of input connections for the node.
nb_out (int): Number of output connections for the node.
in_name (list[str]): Unique name for each input connection.
- Format:
If no input node:
{node_name}_input_{in_id}
If there is a parent node:
{parent_name}_output_{out_id}
in_dims (list[list[int]]): Dimensions of each input.
in_node (list[aidge_core.Node]): List of associated input nodes.
in_size (list[int]): Size of each input.
in_chan (list[int]): Channels in each input, based on data format.
in_height (list[int]): Height of each input, based on data format.
in_width (list[int]): Width of each input, based on data format.
in_dtype (list[
aidge_core.dtype
]): Data type for each input (Aidge format).in_cdtype (list[str]): Data type for each input (C/C++ format).
out_name (list[str]): Unique name for each output, formatted as
{name}_output_{out_id}
.out_node (list[list[aidge_core.Node]]): Associated output nodes for each output.
out_dims (list[list[int]]): Dimensions of each output.
out_size (list[int]): Size of each output.
out_chan (list[int]): Channels in each output, based on data format.
out_height (list[int]): Height of each output, based on data format.
out_width (list[int]): Width of each output, based on data format.
out_dtype (list[
aidge_core.dtype
]): Data type for each output (Aidge format).out_cdtype (list[str]): Data type for each output (C/C++ format).
mem_info (bool): True if mem_info is available for this node.
mem_info_size (list[int]): Memory size for each output, if applicable.
mem_info_offset (list[int]): Offset to access each output, if applicable.
mem_info_stride (list[int]): Stride for accessing each output.
mem_info_length (list[int]): Length of each output.
mem_info_cont_size (list[int]): Continuous size for each output.
mem_info_cont_offset (list[int]): Continuous offset for each output.
mem_info_wrap_offset (list[int]): Wrap offset for each output.
mem_info_wrap_size (list[int]): Wrap size for each output.
- class aidge_core.export_utils.ExportNodeCpp(aidge_node: Node, mem_info: List[dict] | None = None)#
Class for exporting Aidge nodes with C++ code generation.
This subclass of
ExportNode
defines specific templates, configuration paths, and other attributes required to generate C++ code for Aidge nodes, including header and source files for node definitions and forward passes.- Variables:
config_template (str) – Path to the template defining how to export the node definition. This template is required for exporting; if undefined, raises an error, if no config template is required set this to an empty string.
forward_template (str) – Path to the template for generating code to perform a forward pass of the node. Required for exporting the forward pass; raises an error if undefined.
include_list (list[str]) – List of include paths (e.g., “include/toto.hpp”) to be added to the generated export files. Must be defined before export; raises an error if undefined.
kernels_to_copy (list[str]) – List of paths to kernel files that should be copied during export. The kernels are copied to
kernels_path
, and are automatically added to the include list.kernels_path (str) – Path where all kernels are stored in the export, prefixed by the export_root. Defaults to “include/kernels”.
config_path (str) – Path of the configuration folder where node definitions are exported. Defaults to “include/layers”.
config_extension (str) – File extension for the configuration files, typically for header files. Defaults to “h”.
- abstract __init__(aidge_node: Node, mem_info: List[dict] | None = None) None #
Create ExportNode and retrieve attributes from
aidge_node
:
- export(export_folder: str)#
Defines how to export the node definition.
This method checks that config_template, include_list, and kernels_to_copy are defined, then copies each kernel to the export folder, appends their paths to the include list, and generates the configuration file based on the config_template.
- Parameters:
export_folder (str) – Folder path where the files are exported.
- Returns:
List of include paths with the paths for kernels and configuration files.
- Return type:
list[str]
- forward()#
Generates code for a forward pass using the forward_template.
Export scheduler#
- aidge_core.export_utils.scheduler_export(scheduler, export_folder_path: str, export_lib: ExportLib | None = None, memory_manager=None, memory_manager_args=None, test_mode=False) None #
Exports an aidge_core.Scheduler to C++ code.
This function generates files for a given computation graph, including forward-pass functions, configuration headers, and the main API entry point for the exported model. It requires a memory manager to allocate resources, and optionally an ExportLib instance to handle backend configurations for node operators.
- Export Preparation:
Initializes export and DNN folders, checking that required memory management functions are defined.
Retrieves peak memory usage and memory details for each node using the memory_manager.
- Configuration Generation:
Iterates over nodes scheduled by scheduler, configuring backends if export_lib is specified.
- Exports configuration headers and forward-pass actions for each node by invoking op.export()
and op.forward(), appending these to list_configs and list_actions, respectively.
Collects information on input and output nodes, including their names, data types, and sizes.
- Code Generation:
Defines the forward-pass function, model_forward, with inputs and outputs based on node attributes.
Generates the following files:
forward.cpp: Implements the model forward pass using templates, applying configurations
and actions for each node.
forward.hpp: Exports the forward API, defining inputs and outputs.
main.cpp: Main entry file, serving as the model’s forward-pass interface.
- Static File Export (Optional):
If export_lib is specified, static files are copied to the export folder based on export_lib
specifications.
- Parameters:
scheduler (aidge_core.Scheduler) – Scheduler instance managing the computation graph. Uses graph_view and get_static_scheduling methods to retrieve the computation graph layout and ordered nodes.
export_folder_path (str) – Path to the folder where the generated export files will be saved. Creates this folder, along with subdirectories for model and source files.
export_lib (ExportLib, optional) – Library providing the backend implementation for node operators. Defaults to None. If provided, each node’s backend is set to the library’s name.
memory_manager (callable) – Required function for managing memory allocation. It should take scheduler and optional memory_manager_args as parameters, returning peak_mem (peak memory usage) and mem_info (memory details for each node).
memory_manager_args (dict, optional) – Additional arguments passed to memory_manager. Defaults to an empty dictionary.
test_mode (bool, optional) – Additional argument which may be used during forward generation.