Recipes#
Recipes are relatively generic, built-in functionnalities for manipulating a compute graph in Aidge. Some are built with Aidge’s graph matching engine, do not hesitate to have a look at their source code to understand how they work and build similar functionnalities!
🚧 The list of recipes is still growing!
Adapt to backend#
Adapt a graph to the available kernels of a backend. The following transformations
can be performed at the inputs and/or the outputs of operators:
- Cast: change of data type;
- Transpose: change of data format.
- aidge_core.adapt_to_backend(graph_view: aidge_core.aidge_core.GraphView) None#
Adapt the graph to a specific backend.
- Parameters:
graph_view (
aidge_core.GraphView) – Graph view on which we want to apply the recipe
Adapt a graph to the available kernels of a backend.
- Parameters:
graph – Graph to manipulate
Constant folding#
Fold constant operators (like ONNX Simplifier).
Retrieve part of the graph that can be pre-computed and replace them by a Producer.
- Parameters:
graph – Graph to fold the constant
constant_shape – If true Shape operators are considered to be constant
- Returns:
bool True if the graph has been modified
Convert Conv to MatMul#
Convert Conv operators to Unfold (im2col operation) + MatMul + Reshape.
Transform Conv layers with MatMul.
- Parameters:
graph – Graph to manipulate
- Returns:
size_t Number of replacement
Input graph:
Output graph:
Expand meta operators#
Expand meta operators, replacing them with their inner graph (flatten the graph).
- aidge_core.expand_metaops(graph_view: aidge_core.aidge_core.GraphView, recursive: bool = False, name_format: str = '{0}', unique_name: bool = False) None#
Flatten the graph by replacing the meta operators by their micro graph.
- Parameters:
graph_view (
aidge_core.GraphView) – Graph view on which we want to apply the reciperecursive (bool) – If true, recursively replace meta operators until there is no more meta operator in the graph.
name_format (str) – The formatting string to be used with fmt::format() for naming the nodes from the meta-op (inner nodes) in the expanded graph. The usable positional arguments are the following: {0} inner node name, {1} inner node type, {2} meta-node name, {3} meta-node type. Default is {0} (inner node name).
unique_name (bool) – If True, ensure that the expanded nodes name are unique in the expanded graph.
Flatten the graph by replacing the meta operators by their micro graph.
The usable positional arguments are the following: {0} inner node name, {1} inner node type, {2} meta-node name, {3} meta-node type Default is {0} (inner node name).
- Parameters:
recursive – If true, recursively replace meta operators until there is no more meta operator in the graph.
string – nameFormat: The formatting string to be used with fmt::format() for naming the nodes from the meta-op (inner nodes) in the expanded graph.
bool – uniqueName: If true, ensure that the expanded nodes name are unique in the expanded graph.
Explicit Cast Move#
Insert Cast and Move operators where needed (thus removing all implicit data type conversion and backend change data movement).
Add Cast and Move operators where needed to ensure no conversion needs to be done at the Operator level.
Explicit Transpose#
Insert Transpose operators where needed to ensure no transposition needs to be done at the Operator level (thus removing all implicit data format conversion).
Add Transpose operators where needed to ensure no transposition needs to be done at the Operator level.
Fuse BatchNorm#
Fuse batch normalization with the preceding Conv or FC operator, if possible.
- aidge_core.fuse_batchnorm(graph_view: aidge_core.aidge_core.GraphView) None#
Recipe to remove a flatten operator.
- Parameters:
graph_view (
aidge_core.GraphView) – Graph view on which we want to apply the recipe
Fuse :cpp:function:
Aidge::BatchNormwith :cpp:function:Aidge::Convor :cpp:function:Aidge::FCNodes. Ref: https://nenadmarkus.com/p/fusing-batchnorm-and-conv/.- Parameters:
graphView – Graph view to use graph matching on, in order to apply transformations.
Fuse MatMul and Add to FC#
Fuse MatMul optionnally followed by Add operator into a FC operator.
- aidge_core.matmul_to_fc(graph_view: aidge_core.aidge_core.GraphView) None#
Recipe to Fuse MatMul and Add operators into an
aidge_core.FCoperator.- Parameters:
graph_view (
aidge_core.GraphView) – Graph view on which we want to apply the recipe
Fuse to meta operator#
Fuse each sub-graph matching a query in a Meta Operator.
- aidge_core.fuse_to_metaops(*args, **kwargs)#
Overloaded function.
fuse_to_metaops(gm: aidge_core.aidge_core.SinglePassGraphMatching, query: str, type: str = ‘’, graph_func: collections.abc.Callable[[aidge_core.aidge_core.GraphView], None] = None) -> int
Fuse each sub-graph matching a query in a Meta Operator.
- param gm:
SinglePassGraphMatching containing the graph to manipulate
- type gm:
- param query:
Sub-graph matching query
- type query:
str
- param type:
Type name of the resulting meta operators
- type type:
str, optional
- param graph_func:
Function to apply to the matched graph before building the meta-op
- type graph_func:
function, optional
- return:
Number of sub-graph actually fused in a Meta Operator.
- rtype:
int
fuse_to_metaops(graph_view: aidge_core.aidge_core.GraphView, query: str, type: str = ‘’, graph_func: collections.abc.Callable[[aidge_core.aidge_core.GraphView], None] = None) -> int
Fuse each sub-graph matching a query in a Meta Operator.
- param graph_view:
Graph view on which we want to apply the recipe
- type graph_view:
- param query:
Sub-graph matching query
- type query:
str
- param type:
Type name of the resulting meta operators
- type type:
str, optional
- param graph_func:
Function to apply to the matched graph before building the meta-op
- type graph_func:
function, optional
- return:
Number of sub-graph actually fused in a Meta Operator.
- rtype:
int
Fuse each sub-graph matching a query in a Meta Operator.
- Parameters:
gm – SinglePassGraphMatching containing the graph to manipulate
query – Sub-graph matching query
type – Type name of the resulting meta operators
- Returns:
size_t Number of replacement
MatMul tiling#
Tile any MatMul operator to several fixed size matrix multiplications.
For instance, for a MatMul of size 80x80 and a tiling of 16x16, this will tile
the MatMul operator to 25 (5 by 5) MatMul operators of size 16x16, with Slice
operators inserted at the inputs and Concat operators inserted at the outputs.
This is especially useful when matrix multiplication must be mapped to fixed
maximum size hardware TPU (Tensor Processing Unit) or MMA (Matrix Multiplication
Accelerator). This recipe can be combined with the convToMatMul recipe in
order to convert convolutions to matrix multiplication beforehand, and
constantFolding recipe to fold sliced constant tensors.
-
void Aidge::matMulTiling(NodePtr matMul, const std::vector<DimSize_t> &maxDims)#
Tile any :cpp:function:
Aidge::MatMuloperator to several fixed size matrix multiplications. For instance, for a MatMul of size 80x80 and a tiling of 16x16, this will tile the MatMul operator to 25 (5 by 5) MatMul operators of size 16x16, with Slice operators inserted at the inputs and Concat operators inserted at the outputs.This is especially useful when matrix multiplication must be mapped to fixed maximum size hardware TPU (Tensor Processing Unit) or MMA (Matrix Multiplication Accelerator). This recipe can be combined with the :cpp:function:
Aidge::convToMatMulrecipe in order to convert convolutions to matrix multiplication beforehand, and :cpp:function:Aidge::constantFoldingrecipe to fold sliced constant tensors.- Parameters:
matMul – MatMul operator to be tiled.
maxDims – Maximum output dimensions of the tiled MatMul operators.
Initial graph:
Graph generated by a single step of the matMulTiling recipe (after the very first matrix multiplication split):
Remove Dropout#
Remove Dropout operators.
Remove
DropoutNode.- Parameters:
graphView – Graph view to use graph matching on, in order to apply transfomrations.
- Returns:
size_t Number of identity nodes removed
Remove Flatten#
Remove Flatten operators.
- aidge_core.remove_flatten(graph_view: aidge_core.aidge_core.GraphView) None#
Recipe to remove a Flatten operator if it is followed by a FC or a MatMul. The recipe can remove multiple Flatten operator if they are one after the other.
- Parameters:
graph_view (
aidge_core.GraphView) – Graph view on which we want to apply the recipe.
Remove
Flattenbefore :cpp:function:Aidge::FCNode.- Parameters:
graphView – Graph view to use graph matching on, in order to apply transformations.