ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mir::Graph Class Reference

#include <Graph.h>

Public Member Functions

 Graph ()=default
 
virtual ~Graph ()
 
template<typename T , typename... Args>
Operationcreate (Args &&...args)
 
OperationcopyOpWithInputs (Operation *old_op, const std::vector< Operation::Output * > &inputs)
 Copies old_op with new inputs and registers it into graph.
 
void accept (IVisitor *visitor)
 
std::unordered_set< Operation * > getNodes () const
 Returns all graph nodes.
 
std::vector< ops::InputOp * > getInputs () const
 Returns all graph input nodes.
 
std::vector< ops::OutputOp * > getOutputs () const
 Returns all graph output nodes.
 
void removeNode (Operation *op)
 remove node from graph, along with its links in other nodes
 
void replaceNode (Operation *op, Operation *with)
 Subsitude node in graph with another keeping all edges.
 

Detailed Description

Definition at line 34 of file Graph.h.

Constructor & Destructor Documentation

◆ Graph()

mir::Graph::Graph ( )
explicitdefault

◆ ~Graph()

mir::Graph::~Graph ( )
virtual

Definition at line 92 of file Graph.cpp.

93{
94 for (auto &node : _ops)
95 {
96 delete node;
97 }
98}

Member Function Documentation

◆ accept()

void mir::Graph::accept ( IVisitor visitor)

Definition at line 84 of file Graph.cpp.

85{
86 for (Operation *node : getSortedNodes(this))
87 {
88 node->accept(visitor);
89 }
90}
std::vector< Operation * > getSortedNodes(Graph *graph)
Returns nodes of the graph sorted topologically.
Definition Graph.cpp:42

References mir::Operation::accept(), and mir::getSortedNodes().

Referenced by mir2loco::Transformer::transform().

◆ copyOpWithInputs()

Operation * mir::Graph::copyOpWithInputs ( Operation old_op,
const std::vector< Operation::Output * > &  inputs 
)
inline

Copies old_op with new inputs and registers it into graph.

Definition at line 52 of file Graph.h.

53 {
54 assert(inputs.size() == old_op->getNumInputs());
55 auto op = old_op->copyWithInputs(inputs);
56 op->setId(_last_node_id++);
57 registerOp(op);
58 return op;
59 }

References mir::Operation::copyWithInputs(), mir::Operation::getNumInputs(), and mir::Operation::setId().

◆ create()

template<typename T , typename... Args>
Operation * mir::Graph::create ( Args &&...  args)
inline

Definition at line 41 of file Graph.h.

42 {
43 auto op = new T(std::forward<Args>(args)...);
44 op->setId(_last_node_id++);
45 registerOp(op);
46 return op;
47 }

◆ getInputs()

std::vector< ops::InputOp * > mir::Graph::getInputs ( ) const
inline

Returns all graph input nodes.

Returns
vector containing all graph input nodes

Definition at line 73 of file Graph.h.

73{ return _inputs; }

◆ getNodes()

std::unordered_set< Operation * > mir::Graph::getNodes ( ) const
inline

Returns all graph nodes.

Returns
vector containing all graph nodes

Definition at line 67 of file Graph.h.

67{ return _ops; }

Referenced by mir::GraphPatternMatcher::matchEdge(), mir::GraphPatternMatcher::matchUpBush(), and nnc::DataFormatSwitcher::run().

◆ getOutputs()

std::vector< ops::OutputOp * > mir::Graph::getOutputs ( ) const
inline

Returns all graph output nodes.

Returns
vector containing all graph output nodes

Definition at line 79 of file Graph.h.

79{ return _outputs; }

◆ removeNode()

void mir::Graph::removeNode ( Operation op)

remove node from graph, along with its links in other nodes

Parameters
opnode to be removed

Definition at line 117 of file Graph.cpp.

118{
119#ifndef NDEBUG
120 for (const auto &output : op->getOutputs())
121 {
122 assert(output.getUses().empty() && "Trying to remove a node that has uses.");
123 }
124#endif
125
126 for (std::size_t i = 0; i < op->getNumInputs(); ++i)
127 {
128 op->getInput(i)->removeUse(Operation::Use(op, i));
129 }
130
131 if (op->getType() == Operation::Type::input)
132 _inputs.erase(
133 std::remove(_inputs.begin(), _inputs.end(), op)); // NOLINT(bugprone-inaccurate-erase)
134
135 if (op->getType() == Operation::Type::output)
136 _outputs.erase(
137 std::remove(_outputs.begin(), _outputs.end(), op)); // NOLINT(bugprone-inaccurate-erase)
138
139 _ops.erase(op);
140 delete op;
141}
std::vector< ops::OutputOp * > getOutputs() const
Returns all graph output nodes.
Definition Graph.h:79

References mir::Operation::getInput(), mir::Operation::getNumInputs(), mir::Operation::getOutputs(), mir::Operation::getType(), and mir::Operation::Output::removeUse().

Referenced by replaceNode().

◆ replaceNode()

void mir::Graph::replaceNode ( Operation op,
Operation with 
)

Subsitude node in graph with another keeping all edges.

Parameters
opNode to subsitude
withNode to place instead

Definition at line 111 of file Graph.cpp.

112{
113 replaceUsages(op, with);
114 removeNode(op);
115}
void removeNode(Operation *op)
remove node from graph, along with its links in other nodes
Definition Graph.cpp:117

References removeNode().


The documentation for this class was generated from the following files: