Graph#
Node#
The Node class is used to form a node in a graph. In Aidge, there is no “edge” object per se, as the references to parent and child nodes are contained in the node. There is no graph container in Aidge: connected nodes intrinsically form a graph.
- class aidge_core.Node#
- __init__(self: aidge_core.aidge_core.Node, op: Aidge::Operator, name: str = '') None #
- add_child(*args, **kwargs)#
Overloaded function.
add_child(self: aidge_core.aidge_core.Node, other_node: aidge_core.aidge_core.Node, out_id: int = 0, other_in_id: int = 65535) -> None
Link another Node to an output of the current Node.
- param other_node:
Pointer to the other Node.
- type other_node:
- py:class:
Node
- param out_id:
ID of the output of the current Node to connect to the other Node. (If Node has 1 output max ID is 0). Default to 0.
- type out_id:
int
- param other_in_id:
ID of the input of the other Node to connect to the current Node (If the node is a Mul op it has 2 input then Max ID is 1).Default to the first available data input.
- type other_in_id:
int
add_child(self: aidge_core.aidge_core.Node, other_graph: Aidge::GraphView, out_id: int = 0, other_in_id: object = None) -> None
Link a Node from a specific GraphView to the current Node.
- param other_view:
Pointer to the GraphView whose content should be linked to the current Node.
- type other_view:
- py:class:
GraphView
- param out_id:
ID of the current Node output to connect to the other Node. Default to 0.
- type out_id:
int
- param other_in_id:
Pair of Node and input connection ID for specifying the connection. If the GraphView whose content is linked has only one input Node, then it defaults to the first available data input ID of this Node.
- type other_in_id:
tuple[:py:class: Node, int]
- attributes(self: aidge_core.aidge_core.Node) aidge_core.aidge_core.DynamicAttributes #
Get attributes.
- clone(self: aidge_core.aidge_core.Node) aidge_core.aidge_core.Node #
- create_unique_name(self: aidge_core.aidge_core.Node, base_name: str) str #
Given a base name, generate a new name which is unique in all the GraphViews containing this node.
- Parameters:
base_name (str) – proposed name for the node.
- Return type:
str
- get_children(self: aidge_core.aidge_core.Node) Set[aidge_core.aidge_core.Node] #
Get children.
- get_nb_inputs(self: aidge_core.aidge_core.Node) int #
Number of inputs.
- Return type:
int
- get_nb_outputs(self: aidge_core.aidge_core.Node) int #
Number of outputs.
- Return type:
int
- get_operator(self: aidge_core.aidge_core.Node) Aidge::Operator #
Get the Operator object of the Node.
- get_ordered_children(self: aidge_core.aidge_core.Node) List[List[aidge_core.aidge_core.Node]] #
Get ordered children.
- get_parent(self: aidge_core.aidge_core.Node, in_id: int) aidge_core.aidge_core.Node #
- get_parents(self: aidge_core.aidge_core.Node) List[aidge_core.aidge_core.Node] #
Get parents.
- input(self: aidge_core.aidge_core.Node, in_id: int) Tuple[aidge_core.aidge_core.Node, int] #
Get the parent Node and the associated output index connected to the i-th input of the current Node.
- Parameters:
in_id (int) – input index of the current Node object.
- Returns:
i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
- Return type:
tuple[Node, int]
- input_category(self: aidge_core.aidge_core.Node, idx: int) Aidge::InputCategory #
Category of a specific input (Data or Param, optional or not). Data inputs exclude inputs expecting parameters (weights or bias).
- Return type:
InputCategory
- inputs(self: aidge_core.aidge_core.Node) List[Tuple[aidge_core.aidge_core.Node, int]] #
Get ordered list of parent Node and the associated output index connected to the current Node’s inputs.
- Returns:
List of connections. When an input is not linked to any parent, the default value is (None, default_index)
- Return type:
list[tuple[Node, int]]
- name(self: aidge_core.aidge_core.Node) str #
Name of the Node.
- output(self: aidge_core.aidge_core.Node, out_id: int) List[Tuple[aidge_core.aidge_core.Node, int]] #
Get a list of the children Node for a specific output and the associated input index connected to it.
- Parameters:
out_id (int) – input index of the current Node object.
- Returns:
i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
- Return type:
list[tuple[Node, int]]
- outputs(self: aidge_core.aidge_core.Node) List[List[Tuple[aidge_core.aidge_core.Node, int]]] #
Get, for each output of the Node, a list of the children Node and the associated input index connected to it.
- Returns:
List of a list of connections. When an output is not linked to any child, its list a empty.
- Return type:
list[list[tuple[Node, int]]]
- set_name(self: aidge_core.aidge_core.Node, name: str) None #
Set the Node name.
- Parameters:
name (str) – New name for the node.
- Return type:
str
- type(self: aidge_core.aidge_core.Node) str #
Type of the node.
-
class Node : public std::enable_shared_from_this<Node>#
Object carrying the topological information of the computational graph.
Public Functions
-
Node() = delete#
-
virtual ~Node()#
-
inline void addBeforeForward(std::function<bool()> func)#
-
inline void addAfterForward(std::function<bool()> func)#
-
inline void addBeforeBackward(std::function<bool()> func)#
-
inline void addAfterBackward(std::function<bool()> func)#
-
Connector operator()(const std::vector<Connector> &ctors)#
Functional operator for user-friendly connection interface using an ordered set of Connectors.
-
inline std::shared_ptr<DynamicAttributes> attributes() const#
-
void setName(const std::string &name)#
Set the Node name.
Warning
Undefined behavior when several Nodes have the same name.
- Parameters:
name – New name for the node.
-
std::string createUniqueName(std::string name)#
Given the parameter name generate a new name which is unique in all the GraphView which contains this node. To generate the new name the method is called recursively and append the character
_
. If no duplicate return name, this is the exit condition.- Parameters:
name – Base name to make unique.
- Returns:
A unique name in all the GraphView which contains this one.
-
inline std::string type() const#
Type of the node.
- Returns:
std::string
-
void backward()#
Run backward() function of the associated Operator.
-
inline std::shared_ptr<Operator> getOperator() const#
Get the Operator object of the Node.
- Returns:
std::shared_ptr<Operator>
-
bool valid() const#
Whether or not every input of the Node is linked to a Parent. If true then the Node is ready to be executed.
- Returns:
true
- Returns:
false
-
std::vector<std::pair<NodePtr, IOIndex_t>> dataInputs() const#
List of pair <Parent, ID of the data input>. When an input is not linked to any Parent, the pair is <nullptr, gk_IODefaultIndex>. Data inputs exclude inputs expecting parameters (weights or bias).
- Returns:
std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>>
-
std::vector<std::pair<NodePtr, IOIndex_t>> inputs() const#
List of pair <Parent, ID of the parent output>. When an input is not linked to any Parent, the pair is <nullptr, gk_IODefaultIndex>.
- Returns:
std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>>
-
inline std::pair<NodePtr, IOIndex_t> input(const IOIndex_t inID) const#
Parent and its output Tensor ID linked to the inID-th input Tensor. If the input is not linked to any Parent, the pair is <nullptr, gk_IODefaultIndex>.
- Parameters:
inID –
- Returns:
std::pair<std::shared_ptr<Node>, IOIndex_t>
-
inline IOIndex_t getFirstFreeDataInput() const#
Get the lowest index in the InputData Parent list equal to the nullptr. Data inputs exclude inputs expecting parameters (weights or bias).
- Returns:
std::size_t
-
IOIndex_t getNbFreeDataInputs() const#
-
std::vector<std::vector<std::pair<NodePtr, IOIndex_t>>> outputs() const#
List input ids of children linked to outputs of the node. The vector size is guaranteed to match the number of outputs of the node. If there is no connection to a given output, the corresponding sub-vector will be empty.
- Returns:
std::vector<std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>>>
-
std::vector<std::pair<NodePtr, IOIndex_t>> output(IOIndex_t outId) const#
Children and their input Tensor ID linked to the outId-th output Tensor.
- Parameters:
outId –
- Returns:
std::vector<std::pair<std::shared_ptr<Node>, IOIndex_t>>
-
inline IOIndex_t nbInputs() const noexcept#
Number of inputs, including both data and learnable parameters.
[data, data, weight, bias] => 4
- Returns:
IOIndex_t
-
inline InputCategory inputCategory(IOIndex_t idx) const#
Category of a specific input (Data or Param, optional or not). Data inputs exclude inputs expecting parameters (weights or bias).
- Returns:
InputCategory
-
inline bool parentIsBackEdge(IOIndex_t idx) const#
Returns whether the given node parent index is a back edge A back edge is defined by the operator and node parent index correspond to operator input index.
- Returns:
true if the operator defines it as a back edge
-
IOIndex_t nbValidInputs() const#
Number of inputs linked to a Parent’s output.
- Returns:
IOIndex_t
-
inline IOIndex_t nbOutputs() const noexcept#
Getter for the number of Output Tensors of the Node.
- Returns:
IOIndex_t
-
IOIndex_t nbValidOutputs() const#
-
inline std::set<std::shared_ptr<GraphView>> views() const noexcept#
Set of pointers to each GraphView containing this Node.
- Returns:
std::set<GraphView>
Add a GraphView pointer to the list of GraphView containing the current Node. This feature allows transparent GraphViews.
- Parameters:
graphPtr – Pointer to GraphView to add to the list.
-
void addChild(NodePtr otherNode, const IOIndex_t outId = IOIndex_t(0), IOIndex_t otherInId = gk_IODefaultIndex)#
Link a Node from a specific GraphView to the current Node.
- Parameters:
otherView – Pointer to the GraphView whose content should be linked to the current Node.
outId – ID of the output Tensor to connect to the other Node. Default to 0.
otherInId – Pair of pointer to Node and Tensor ID for specifying the connection. If the GraphView whose content is linked has only one input Node, then it defaults to the first available data input Tensor of this Node.
-
std::vector<NodePtr> getParents() const#
Get the list of parent Nodes. As an input is linked to a unique Node, if none is linked then the parent is a nullptr.
- Returns:
std::vector<std::shared_ptr<Node>>
-
inline NodePtr &getParent(const IOIndex_t inId)#
Get the pointer to parent of the specified input index. This pointer is nullptr if no parent is linked.
- Parameters:
inId – Input index.
- Returns:
std::shared_ptr<Node>&
-
NodePtr popParent(const IOIndex_t inId)#
Unlink the parent Node at the specified input index and return its pointer. Return a nullptr is no parent was linked.
- Parameters:
inId – Input index.
- Returns:
std::shared_ptr<Node>
-
bool removeParent(const IOIndex_t inId)#
-
std::set<NodePtr> getChildren() const#
Get the set of pointers to children Nodes linked to the current Node.object.
The returned set does not include any nullptr as an output maybe linked to an undifined number of Nodes. It does not change the computation of its associated Operator.
- Returns:
std::set<std::shared_ptr<Node>>>
-
std::vector<std::vector<NodePtr>> getOrderedChildren() const#
-
std::vector<NodePtr> getChildren(const IOIndex_t outId) const#
Get the list of children Nodes linked to the output at specified index.
- Parameters:
outId – Output index.
- Returns:
std::vector<std::shared_ptr<Node>>
-
bool removeChild(const NodePtr nodePtr, const IOIndex_t outId = 0)#
Remove registered child from children list of specified output if possible. If so, also remove current Node from child Node from parent.
- Parameters:
std::shared_ptr<Node> – Node to remove.
outId – Output index. Default 0.
- Returns:
true Child found and removed for given output index.
- Returns:
false Child not found at given index. Nothing removed.
-
void resetConnections(bool includeLearnableParam = false)#
Remove every link of surrounding nodes to it and conversely.
Clone the current Node. The Operator attribute of the new Node is not copied but shared with the current Node. The new node has no connection.
- Returns:
NodePtr
Clone the Node. Every attribute is copied, even Operator pointer except for Producers for which it is shared. The new Node has no connection.
- Returns:
NodePtr
-
NodePtr clone() const#
Clone the Node and its Operator. The new Node has no connection.
- Returns:
NodePtr
-
std::set<NodePtr> getNodeDelta(int delta, std::set<Aidge::NodePtr> nodeSee)#
Get the set of pointers to connected node at a distance of a delta.
the recution are cut Return a nullptr is nofing found.
- Parameters:
delta – Input delta.
- Returns:
std::shared_ptr<Node>
Public Static Functions
Callback function to clone the Node keeping the same Operator object instance. The new Node has no connection.
- Parameters:
node – Node to clone.
- Returns:
NodePtr
-
Node() = delete#
GraphView#
As the name suggests, a GraphView is not a graph container, but merely a “view” of some nodes in a graph. There can be multiple overlapping views of nodes in a graph. Nodes in a view do not have to form a single connected graph.
A GraphView is however more than just a set of nodes. It has the following features:
It keeps track of all its inputs and outputs (if the parent of a node is not in the GraphView, the node is an input node for the GraphView). Furthermore, the GraphView inputs/outputs are ordered, and the order is stable during insertion or deletion of nodes in the GraphView;
There is a root node in the GraphView (by default, the first node added to it), and nodes inside a GraphView can be ranked with a unique and deterministic order if the nodes form a single connected graph;
Some methods can be applied globally to the GraphView, like
setBackend()
.
- class aidge_core.GraphView#
- __init__(self: aidge_core.aidge_core.GraphView) None #
- add(*args, **kwargs)#
Overloaded function.
add(self: aidge_core.aidge_core.GraphView, other_node: aidge_core.aidge_core.Node, include_learnable_parameters: bool = True) -> None
Include a Node to the current GraphView object.
- param other_node:
Node to add
- type other_node:
Node
- param include_learnable_parameters:
include non-data inputs, like weights and biases, default True.
- type include_learnable_parameters:
bool, optional
add(self: aidge_core.aidge_core.GraphView, other_graph: aidge_core.aidge_core.GraphView, include_learnable_parameters: bool = True) -> bool
Include a GraphView to the current GraphView object.
- param other_graph:
GraphView to add
- type other_graph:
GraphView
- param include_learnable_parameters:
include non-data inputs, like weights and biases, default True.
- type include_learnable_parameters:
bool, optional
- add_child(self: aidge_core.aidge_core.GraphView, to_other_node: aidge_core.aidge_core.Node, from_out_node: aidge_core.aidge_core.Node = None, from_tensor: int = 0, to_tensor: int = 65535) None #
Include a Node to the current GraphView object.
- Parameters:
to_other_node (Node) – Node to add
from_out_node (Node) – Node inside the GraphView the new Node will be linked to (it will become a parent of the new Node). If the GraphView only has one output Node, then default to this Node.
from_tensor (int) – Output Tensor ID of the already included Node. Default to 0.
to_tensor (int) – Input Tensor ID of the new Node. Default to gk_IODefaultIndex, meaning first available data input for the Node.
- compile(self: aidge_core.aidge_core.GraphView, backend: str, datatype: aidge_core.aidge_core.dtype, device: int = 0, dims: List[List[int]] = []) None #
- forward_dims(self: aidge_core.aidge_core.GraphView, dims: List[List[int]] = [], allow_data_dependency: bool = False) bool #
- get_input_nodes(self: aidge_core.aidge_core.GraphView) Set[aidge_core.aidge_core.Node] #
Get set of input Nodes.
- Return type:
list[Node]
- get_node(self: aidge_core.aidge_core.GraphView, node_name: str) aidge_core.aidge_core.Node #
- get_nodes(self: aidge_core.aidge_core.GraphView) Set[aidge_core.aidge_core.Node] #
- get_ordered_inputs(self: aidge_core.aidge_core.GraphView) List[Tuple[aidge_core.aidge_core.Node, int]] #
- get_ordered_nodes(self: aidge_core.aidge_core.GraphView, reversed: bool = False) List[aidge_core.aidge_core.Node] #
Get ordered nodes for the graph view
- get_ordered_outputs(self: aidge_core.aidge_core.GraphView) List[Tuple[aidge_core.aidge_core.Node, int]] #
- get_output_nodes(self: aidge_core.aidge_core.GraphView) Set[aidge_core.aidge_core.Node] #
Get set of output Nodes.
- Return type:
list[Node]
- get_ranked_nodes(self: aidge_core.aidge_core.GraphView) Tuple[List[aidge_core.aidge_core.Node], int] #
- get_ranked_nodes_name(self: aidge_core.aidge_core.GraphView, format: str, mark_non_unicity: bool = True) Dict[aidge_core.aidge_core.Node, str] #
- in_view(*args, **kwargs)#
Overloaded function.
in_view(self: aidge_core.aidge_core.GraphView, arg0: aidge_core.aidge_core.Node) -> bool
in_view(self: aidge_core.aidge_core.GraphView, arg0: str) -> bool
- inputs(self: aidge_core.aidge_core.GraphView) List[Tuple[aidge_core.aidge_core.Node, int]] #
- log_outputs(self: aidge_core.aidge_core.GraphView, path: str) None #
- outputs(self: aidge_core.aidge_core.GraphView) List[List[Tuple[aidge_core.aidge_core.Node, int]]] #
- static replace(*args, **kwargs)#
Overloaded function.
replace(old_graph: aidge_core.aidge_core.GraphView, new_graph: aidge_core.aidge_core.GraphView) -> bool
Replace the old set of Nodes in a GraphView with the new set of given Nodes in a GraphView if possible in every GraphView.
- param old_graph:
GraphView of Nodes actually connected in GraphViews.
- type old_graph:
GraphView
- param new_graph:
GraphView of Nodes with inner connections already taken care of.
- type new_graph:
GraphView
- return:
Whether any replacement has been made.
- rtype:
bool
replace(old_nodes: Set[aidge_core.aidge_core.Node], new_nodes: Set[aidge_core.aidge_core.Node]) -> bool
Replace the old set of Nodes with the new set of given Nodes if possible in every GraphView.
- param old_nodes:
Nodes actually connected in GraphViews.
- type old_nodes:
Node
- param new_nodes:
Nodes with inner connections already taken care of.
- type new_nodes:
Node
- return:
Whether any replacement has been made.
- rtype:
bool
- root_node(self: aidge_core.aidge_core.GraphView) aidge_core.aidge_core.Node #
- save(self: aidge_core.aidge_core.GraphView, path: str, verbose: bool = False, show_producers: bool = True) None #
Save the GraphView as a Mermaid graph in a .md file at the specified location.
- Parameters:
path (str) – save location
- set_backend(self: aidge_core.aidge_core.GraphView, backend: str, device: int = 0) None #
- set_dataformat(self: aidge_core.aidge_core.GraphView, dataformat: aidge_core.aidge_core.dformat) None #
- set_datatype(self: aidge_core.aidge_core.GraphView, datatype: aidge_core.aidge_core.dtype) None #
- set_ordered_inputs(self: aidge_core.aidge_core.GraphView, inputs: List[Tuple[aidge_core.aidge_core.Node, int]]) None #
- set_ordered_outputs(self: aidge_core.aidge_core.GraphView, outputs: List[Tuple[aidge_core.aidge_core.Node, int]]) None #
- set_root_node(self: aidge_core.aidge_core.GraphView, node: aidge_core.aidge_core.Node) None #
-
class GraphView : public std::enable_shared_from_this<GraphView>#
Groupement of Nodes forming a computational graph on which properties and functions can easily and safely be applied or run.
Public Functions
-
inline GraphView(const std::string &name = "")#
-
const NodePtr operator[](const std::string &nodeName) const#
-
inline std::string name() const noexcept#
Name of the node.
- Returns:
std::string
-
inline void setName(const std::string &name)#
Set the node name.
Warning
Undefined behavior when several Nodes have the same name.
- Parameters:
name – New name for the node.
-
void setNodesName() const#
Set the name of every Node based on the current GraphView name in following form: “name_type#type-id”.
-
void save(const std::string &path, bool verbose = false, bool showProducers = true) const#
Save the GraphView as a Mermaid graph in a .md file at the specified location.
- Parameters:
path –
-
void logOutputs(const std::string &dirName) const#
-
inline NodePtr rootNode() const noexcept#
-
void setRootNode(NodePtr node)#
-
std::set<NodePtr> inputNodes() const#
Get reference to the set of input Nodes.
-
std::set<NodePtr> outputNodes() const#
Get reference to the set of output Nodes.
-
bool isInputNode(const NodePtr &nodePtr) const#
Assess if the given Node is an input Node of the GraphView object.
-
bool isOutputNode(const NodePtr &nodePtr) const#
Assess if the given Node is an output Node of the GraphView object.
-
void setOrderedInputs(const std::vector<std::pair<NodePtr, IOIndex_t>> &inputs)#
-
void setOrderedOutputs(const std::vector<std::pair<NodePtr, IOIndex_t>> &outputs)#
-
std::vector<Aidge::NodePtr> getOrderedNodes(bool reversed = false) const#
Get a topological node order for an acyclic walk of the graph Graph cycles are broken on operator back edges such that resolution on single level lattice can be done in a single pass as it is the case generally for static resolution of Tensor shapes/datatypes. When reversed is true, gets a topological order on the reversed graph which is equivalent to a post-dfs order of the graph. The returned order is deterministic given the graph node set and the graph ordered output nodes. The output nodes connectivity must cover all nodes of the graph, otherwise a runtime exception is thrown. The returned order is biased toward left-to-right child order both for topological and post-dfs order.
- Parameters:
reversed – returns a topological order of the reversed graph
- Returns:
the ordered list of nodes
-
inline const std::vector<std::pair<NodePtr, IOIndex_t>> &getOrderedInputs() const noexcept#
Get inputs of the current GraphView with their associated id. The rank of the nodes are their rank in the vector.
- Returns:
const std::vector<std::pair<NodePtr, IOIndex_t>>&
-
inline const std::vector<std::pair<NodePtr, IOIndex_t>> &getOrderedOutputs() const noexcept#
Get outputs of the current GraphView with their associated id. The rank of the nodes are their rank in the vector.
- Returns:
const std::vector<std::pair<NodePtr, IOIndex_t>>&
-
std::vector<std::pair<NodePtr, IOIndex_t>> dataInputs() const#
List outside data input connections of the GraphView. Data inputs exclude inputs expecting parameters (weights or bias). The vector size is guaranteed to match the number of outside data inputs of the GraphView. If there is no external connection to a given input, a pair of nullptr and gk_IODefaultIndex is returned.
- Returns:
std::vector<std::pair<NodePtr, IOIndex_t>>
-
inline auto dataInputs(const std::string name) const#
List all dataInput connections (within and outside) of the specified GraphView node named “name”. Data inputs exclude inputs expecting parameters (weights or bias).
- Parameters:
name – Name of the Node.
- Returns:
std::vector<std::pair<NodePtr, IOIndex_t>>
-
std::vector<std::pair<NodePtr, IOIndex_t>> inputs() const#
List outside input connections of the GraphView. The vector size is guaranteed to match the number of outside inputs of the GraphView. If there is no external connection to a given input, a pair of nullptr and gk_IODefaultIndex is returned.
- Returns:
std::vector<std::pair<NodePtr, IOIndex_t>>
-
std::vector<std::pair<NodePtr, IOIndex_t>> inputs(const std::string &name) const#
List all input connections (within and outside) of the specified GraphView node named “name”.
- Returns:
std::vector<std::pair<NodePtr, IOIndex_t>>
-
std::vector<std::vector<std::pair<NodePtr, IOIndex_t>>> outputs() const#
List outside output connections of the GraphView. The vector size is guaranteed to match the number of outputs of the GraphView. If there is no connection to a given output, the corresponding sub-vector will be empty.
- Returns:
std::vector<std::pair<NodePtr, IOIndex_t>>
-
std::vector<std::vector<std::pair<NodePtr, IOIndex_t>>> outputs(const std::string &nodeName) const#
List all output connections (within and outside) of the specified GraphView node named “name”.
- Parameters:
nodeName – Name of the Node of which to show the output.
- Returns:
std::vector<std::vector<std::pair<NodePtr, IOIndex_t>>>
-
void compile(const std::string &backend = "cpu", const Aidge::DataType datatype = DataType::Float32, DeviceIdx_t device = 0, const std::vector<std::vector<DimSize_t>> dims = {})#
Assert Datatype, Backend, data format and dimensions along the GraphView are coherent. If not, apply the required transformations.
Sets the GraphView ready for computation in four steps: 1 - Assert input Tensors’ datatype is compatible with each Operator’s datatype. If not, a conversion Operator is inserted. 2 - Assert input Tensors’ backend is compatible with each Operator’s backend. If not, add a Transmitter Operator. 3 - Assert data format (NCHW, NHWC, …) of each Operator’s input Tensor is compatible with the selected kernel. If not, add a Transpose Operator. 4 - Propagate Tensor dimensions through the consecutive Operators.
-
bool forwardDims(const std::vector<std::vector<DimSize_t>> &dims = {}, bool allowDataDependency = false)#
Compute dimensions of input/output Tensors for each Operator of the GraphView object’s Nodes, by calling Node::forwardDims(). This function verifies the following conditions:
Every node will forwardDims() regardless of if dims were previously forwarded or not;
forwadDims() calls are made in node dependencies order, because if dims have changed at any point in the graph, it must de propagated correctly to all succeeding nodes;
It handles cyclic dependencies correctly (currently only induced by the Memorize_Op).
-
void setBackend(const std::string &backend, const DeviceIdx_t device = 0) const#
Set the same backend for each Operator of the GraphView object’s Nodes.
-
void setDataType(const DataType &datatype) const#
Set the same data type for each Operator of the GraphView object’s Nodes.
-
void setDataFormat(const DataFormat &dataformat) const#
Set the same data format for each Operator of the GraphView object’s Nodes.
-
std::set<NodePtr> getParents() const#
Get the parents Nodes of inputNodes.
- Returns:
std::set<NodePtr>
-
std::vector<NodePtr> getParents(const std::string nodeName) const#
Get parents Nodes of the specified Node.
- Parameters:
nodeName – Name of the Node.
- Returns:
std::vector<NodePtr>
-
std::vector<std::vector<NodePtr>> getOrderedParents() const#
-
std::set<NodePtr> getChildren() const#
Get the children Nodes of outputNodes.
- Returns:
std::set<NodePtr>
-
std::vector<std::vector<NodePtr>> getChildren(const std::string nodeName) const#
Get children Nodes of the specified Node.
- Parameters:
nodeName – Name of the Node.
- Returns:
std::vector<std::vector<NodePtr>>
-
std::set<NodePtr> getChildren(const NodePtr otherNode) const#
-
inline const std::set<NodePtr> &getNodes() const noexcept#
Get the Nodes pointed to by the GraphView object.
- Returns:
std::set<NodePtr>
-
NodePtr getNode(const std::string &nodeName) const#
Get the operator with the corresponding name if it is in the GraphView.
- Parameters:
nodeName – Name of the node.
- Returns:
NodePtr returns a nullptr if the one asked for was not found.
-
std::pair<std::vector<NodePtr>, size_t> getRankedNodes() const#
Get the ranked list of nodes in the GraphView. Node ranking if performed the following:
The root node is put in the ranked list first (rank 1);
Then, its childs (in order of outputs) are added in the ranked list;
Then, its parents (in order of inputs) are added in the ranked list;
The childs and parents of the next node in the ranked list are then added to the list, and so on.
Any remaining nodes have no path to the root node and are added in arbitrary order. In this case, the ranking is not guaranteed to be unique.
If the ranking cannot be guaranteed to be unique, the second item indicates the rank from which unicity cannot be guaranteed.
- Returns:
std::pair<std::vector<NodePtr>, size_t> Pair with the list of ranked nodes and the size of the ranked sub-list where unicity is guaranteed.
-
std::map<NodePtr, std::string> getRankedNodesName(const std::string &format, bool markNonUnicity = true) const#
Get the nodes name according to the GraphView nodes ranking.
- Parameters:
format – The formatting string to be used with fmt::format(). The usable positional arguments are the following: {0} node name, {1} node type, {2} rank, {3} type rank
markNonUnicity – If true, non unique ranking is prefixed with “?”
- Returns:
std::map<NodePtr, std::string> A map with the corresponding names
-
void remove(NodePtr nodePtr, bool includeLearnableParam = true)#
Remove a Node from the current GraphView scope without affecting its connections.
- Parameters:
nodePtr – Node to remove
includeLearnableParam – Whether learnable parameters should also be removed. Default true.
-
void setInputId(IOIndex_t inID, IOIndex_t newNodeOutID)#
-
void add(NodePtr otherNode, bool includeLearnableParam = true)#
-
bool add(std::set<NodePtr> otherNodes, bool includeLearnableParam = true)#
Include a set of Nodes to the current GraphView object.
- Parameters:
otherNodes –
includeLearnableParam –
- Returns:
true if graph ordering is unique (meaning inputs/outputs order is well defined).
-
bool add(std::pair<NodePtr, std::set<NodePtr>> otherNodes, bool includeLearnableParam = true)#
Include a set of Nodes to the current GraphView object. The first element of the otherNodes pair is the start node and the second is the remaining nodes to add.
- Parameters:
otherNodes –
includeLearnableParam –
- Returns:
true if graph ordering is unique (meaning inputs/outputs order is well defined).
Include every Node inside another GraphView to the current GraphView.
- Parameters:
other_graph – GraphView containing the Nodes to include.
- Returns:
true if graph ordering is unique (meaning inputs/outputs order is well defined).
-
void addChild(NodePtr toOtherNode, NodePtr fromOutNode = nullptr, const IOIndex_t fromTensor = IOIndex_t(0), IOIndex_t toTensor = gk_IODefaultIndex)#
Include a Node in the current GraphView and link it to another already contained Node.
- Parameters:
toOtherNode – Pointer to the Node to add.
fromOutNode – Pointer to the already included Node the new Node will be linked to (it will become a parent of the new Node). If the GraphView only has one output Node, then default to this Node.
fromTensor – Output Tensor ID of the already included Node. Default to 0.
toTensor – Input Tensor ID of the new Node. Default to gk_IODefaultIndex, meaning first available data input for the Node.
-
inline void addChild(NodePtr toOtherNode, const std::string &fromOutNodeName, const IOIndex_t fromTensor = IOIndex_t(0), IOIndex_t toTensor = gk_IODefaultIndex)#
Include a Node in the current GraphView and link it to another already contained Node.
- Parameters:
toOtherNode – Pointer to the Node to add.
fromOutNodeName – Name of the already included Node the new Node will be linked to (it will become a parent of the new Node). As a name is optional, ensure such Node is in the GraphView or it will send back an error message.
fromTensor – Output Tensor ID of the already included Node. Default to 0.
toTensor – Input Tensor ID of the new Node. Default to gk_IODefaultIndex, meaning first available data input for the Node.
Include a GraphView content in the current GraphView and link the two sets by linking one Node from each GraphView.
- Parameters:
toOtherView – Pointer to the GraphView whose content should be added.
fromOutNode – Pair of pointer to Node and Tensor ID for specifying the connection. If the GraphView including the other one has only one output Node, then it defaults to the first output Tensor of this Node.
toNode – Pair of pointer to Node and Tensor ID for specifying the connection. If the GraphView whose content is included has only one input Node, then it defaults to the first available data input Tensor of this Node.
-
bool swap(Node &node, Node &otherNode)#
Swap two Node instances if possible.
- Parameters:
node –
otherNode –
- Returns:
true
- Returns:
false
-
void link(const std::string &name1_inID, const std::string &name2_outID)#
-
void insertParent(NodePtr childNode, NodePtr newParentNode, IOIndex_t childInputTensorIdx, IOIndex_t newParentInputTensorIdx, IOIndex_t newParentOutputTensorIdx)#
Insert a node (newParentNode) as a parent of the passed node (childNode).
- Parameters:
childNode – Node that gets a new parent.
newParentNode – Inserted Node.
childInputTensorIdx – Index of the input Tensor for the childNode linked to the inserted Node output.
newParentInputTensorIdx – Index of the input Tensor for the newParentNode linked to the former parent of childNode.
newParentOutputTensorIdx – Index of the output Tensor for the newParentNode linked to the childNode’s input Tensor.
Clone the GraphView with shared Operators. It is a new GraphView, with cloned Nodes, but the new Nodes refer to the same Operators as the original ones.
- Returns:
std::shared_ptr<GraphView>
Clone the GraphView with shared Producers. All the other Operators are copied.
- Returns:
std::shared_ptr<GraphView>
-
inline std::shared_ptr<GraphView> clone() const#
Clone the GraphView. Everything is cloned: Nodes and Operators.
- Returns:
std::shared_ptr<GraphView>
-
std::shared_ptr<GraphView> cloneCallback(NodePtr (*cloneNode)(NodePtr)) const#
Clone the current GraphView using a callback function for the Node cloning, allowing to specify how each Node should be cloned or replaced by another Node type, or removed (i.e. replaced by identity). When a Node is removed, the clone() method automatically finds the next valid parent in line, going backward in the graph and connects it if that makes sense without ambiguity (effectively treating the removed Node as an identity operation).
- Parameters:
cloneNode – Callback function to clone a node
- Returns:
std::shared_ptr<GraphView>
Public Static Functions
-
static bool replace(const std::set<NodePtr> &oldNodes, const std::set<NodePtr> &newNodes)#
Replace a set of Nodes in every available GraphView with a new set of Nodes if possible. Both sets should include all the necessary Producers.
There are 3 cases of replacement: Case 1: same number of input/output connections for oldNodes and newNodes sets.
input/output connections are replicated according to their IDs. Case 2: different number of input/output connections for oldNodes and newNodes sets.
only a single parent/child node for the newNodes set, every input/output is connected to it.
several parents/children nodes for newNodes set => impossible to know, return false Case 3: newNodes set is empty
same number of input/output connections in oldNodes, parents and children are linked according to these connections IDs
different number of input/output connections in oldNodes => return false
- Parameters:
oldNodes –
newNodes –
- Returns:
true replacement has been performed
- Returns:
false no replacement has been performed
-
inline GraphView(const std::string &name = "")#
It is possible to automatically generate a GraphView that encompasses all the connected nodes with a path to a given node with the getConnectedGraphView
function:
- aidge_core.get_connected_graph_view(arg0: aidge_core.aidge_core.Node) aidge_core.aidge_core.GraphView #
Graph helpers#
Graph helpers are pseudo-containers that allow you to quickly build a graph without explicit node connections, like the Sequential
container in PyTorch. One important difference with PyTorch is that they are only temporary builders, and do not form a structure in the returned graph. They return a GraphView.
- aidge_core.sequential(inputs: List[aidge_core.aidge_core.OpArgs], name: str = '') aidge_core.aidge_core.GraphView #
Usage example:
input_data = np.array([0, 1, 2, 3]).astype(np.float32)
input_tensor = aidge_core.Tensor(input_data)
graph_view = aidge_core.sequential([
aidge_core.Producer(input_tensor, "IN"),
aidge_core.FC(1, 50, name='FC0'),
aidge_core.FC(50, 50, name='FC1'),
aidge_core.FC(50, 10, name='FC2'),
])
-
std::shared_ptr<GraphView> Aidge::Sequential(std::vector<OpArgs> inputs, std::string name = "")#
Create a GraphView by linking every input with the next one in a sequential way. Nodes linked with the Sequential graph generation instructions must have a single output. Sequential(A, B, C) returns A–>B–>C.
Usage example:
auto input_tensor = std::make_shared<Tensor>(Array1D<float,4>{{0, 1, 2, 3}});
auto graph_view = Sequential({
Producer(input_tensor, "IN"),
FC(1, 50, false, "FC0"),
FC(50, 50, false, "FC1"),
FC(50, 10, false, "FC2")
})
- aidge_core.parallel(inputs: List[aidge_core.aidge_core.OpArgs], name: str = '') aidge_core.aidge_core.GraphView #
Usage example:
input_data = np.array([0, 1, 2, 3]).astype(np.float32)
input_tensor = aidge_core.Tensor(input_data)
graph_view = aidge_core.sequential([
aidge_core.Producer(input_tensor, "X"),
aidge_core.FC(1, 50, name='FC0'),
aidge_core.parallel([
aidge_core.FC(50, 50, name='FC1'),
aidge_core.FC(50, 50, name='FC3')
]),
aidge_core.Add(2, name='FC2'),
])
-
std::shared_ptr<GraphView> Aidge::Parallel(std::vector<OpArgs> inputs, std::string name = "")#
Creates a GraphView with provided Nodes without linking them.
Usage example:
auto g = Sequential({
Conv(1, 3, {3, 3}, "inputConv"),
Parallel({
Conv(3, 3, {1, 1}, "conv1.1"),
Conv(3, 3, {1, 1}, "conv1.2"),
Conv(3, 3, {1, 1}, "conv1.3")
}),
Add(3, "add1"),
Conv(3, 2, {1, 1}, "conv2"),
FC(18, 5, false, "out")
});
- aidge_core.residual(inputs: List[aidge_core.aidge_core.OpArgs], name: str = '') aidge_core.aidge_core.GraphView #
-
std::shared_ptr<GraphView> Aidge::Residual(std::vector<OpArgs> inputs, std::string name = "")#
Create a GraphView by linking every input with the next one in a sequential way. Finally the first element output is used as another input for the last element. Nodes linked with the Recursive graph generation instructions must have a single output. Recursive(A, B, C) returns A–>B–>C , A–>C.
Connector#
The Connector object enables you to build a graph in a functional style like you would often do in other DL frameworks. However, please note that doing so is just a way of building a graph, and does not result in any “eager” execution. Using an explicit Connector object is here to make that intent clear.
Additionally, it is possible to mix both graph helpers and functional Connector-based styles.
-
class Connector#
Object meant for simpler and more instrinctive user API.
example: Connector x(); x = Conv(…)(x); Connector y = Split(3)(x[0]); // Error! Cannot slice a Connector with one output only Connector y = Split(3)(x); CustomLayer cl(…); Connector z = cl(y) // Error! y has multiple outputs, must specify which one to use Connector z1 = cl(y[0]); Connector z2 = cl(y[1]); Connector z3 = cl(y[2]); x = Sum(…)(z1, z2, z3); GraphView g = x.generateGraph();
Usage example:
auto x = Connector();
auto y = Connector();
x = (*GenericOperator("Producer", 0, 0, 1))({});
y = (*GenericOperator("Producer", 0, 0, 1))({});
// Connect 5 successive convolution
for (int i = 0; i < 5; ++i) {
x = (*GenericOperator("Conv", 1, 0, 1))({x});
}
y = (*GenericOperator("ElemWise", 2, 0, 1))({y, x});
// Create a GraphView
auto g = generateGraph({y});
A GraphView can be obtained directly from the last Connector with the generateGraph
function.
- aidge_core.generate_graph(output_connectors: List[aidge_core.aidge_core.Connector]) aidge_core.aidge_core.GraphView #