ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::ir::Graph Class Reference

#include <Graph.h>

Collaboration diagram for onert::ir::Graph:

Public Member Functions

 Graph (void)
 
 Graph (const Graph &)
 
 ~Graph (void)
 
OperandIndex addOperand (const Shape &shape, const TypeInfo &type)
 
OperandIndex addOperand (OperandIndex index, std::unique_ptr< Operand > &&operand)
 Add an operand to the graph with the given index and object.
 
OperationIndex addOperation (std::unique_ptr< IOperation > &&node)
 
OperationIndex addOperation (OperationIndex index, std::unique_ptr< IOperation > &&operation)
 Add an operation to the graph with the given index and object.
 
OperationIndex replaceOperation (OperationIndex index, std::unique_ptr< IOperation > &&operation)
 Replace an operation which the graph already has.
 
void setOperandValue (const OperandIndex &ind, std::shared_ptr< Data > data)
 
void changeShape (const OperandIndex &ind, const ir::Shape &new_shape) override
 
void addInput (const OperandIndex &ind, const std::string &name="")
 
void addOutput (const OperandIndex &ind, const std::string &name="")
 
void verify (void) const
 
void removeOperand (const OperandIndex &ind)
 
const OperandIndexSequencegetInputs () const override
 
OperandIndexSequencegetInputs ()
 
const OperandIndexSequencegetOutputs () const override
 
OperandIndexSequencegetOutputs ()
 
IOIndex getInputIndex (const std::string &name) const override
 
IOIndex getOutputIndex (const std::string &name) const override
 
const Operandsoperands () const override
 
Operandsoperands ()
 
const Operationsoperations () const override
 
Operationsoperations ()
 
std::vector< ir::OperationIndextopolSortOperations () const
 
- Public Member Functions inherited from onert::ir::IGraph
virtual ~IGraph ()=default
 

Detailed Description

Definition at line 31 of file Graph.h.

Constructor & Destructor Documentation

◆ Graph() [1/2]

onert::ir::Graph::Graph ( void  )
explicitdefault

◆ Graph() [2/2]

onert::ir::Graph::Graph ( const Graph )
explicitdefault

◆ ~Graph()

onert::ir::Graph::~Graph ( void  )
default

Member Function Documentation

◆ addInput()

void onert::ir::Graph::addInput ( const OperandIndex ind,
const std::string &  name = "" 
)

Definition at line 121 of file Graph.cc.

122{
123 if (!name.empty())
124 _name_to_input.emplace(name, IOIndex{_inputs.size()});
125 _inputs.append(ind);
126}
void append(const OperandIndex &index)
::onert::util::Index< uint32_t, IOIndexTag > IOIndex
Definition Index.h:36

References onert::ir::OperandIndexSequence::append(), and onert::ir::OperandIndexSequence::size().

Referenced by onert::ir::train::TrainableGraph::addInput().

◆ addOperand() [1/2]

OperandIndex onert::ir::Graph::addOperand ( const Shape shape,
const TypeInfo type 
)

Definition at line 33 of file Graph.cc.

34{
35 return _operands.emplace(shape, type);
36}
Index emplace(Args &&...args)
Create an object with args and put it in the container with a newly assigned Index.

References onert::util::ObjectManager< Index, Object >::emplace().

Referenced by onert::ir::train::TrainableGraph::addOperand(), onert::ir::train::TrainableGraph::addOperand(), onert::compiler::pass::ConstantOutputPass::callback(), and onert::loader::BaseLoader< LoaderDomain >::loadOperand().

◆ addOperand() [2/2]

OperandIndex onert::ir::Graph::addOperand ( OperandIndex  index,
std::unique_ptr< Operand > &&  operand 
)

Add an operand to the graph with the given index and object.

If the given index is available, it succeeds. And operand is moved which invalidates the caller's pointer. If the given index is already taken, it fails. And operand will not be moved so the caller's pointer will be still valid.

Parameters
[in]indexIndex to be added
[in]operandOperand to be added
Returns
OperandIndex index if successful, Undefined otherwise

Definition at line 38 of file Graph.cc.

39{
40 return _operands.push(std::move(operand), index);
41}
Index push(std::unique_ptr< Object > &&object, Index index)
Put the object in the container with given index.

References onert::util::ObjectManager< Index, Object >::push().

◆ addOperation() [1/2]

OperationIndex onert::ir::Graph::addOperation ( OperationIndex  index,
std::unique_ptr< IOperation > &&  operation 
)

Add an operation to the graph with the given index and object.

If the given index is available, it succeeds. And operation is moved which invalidates the caller's pointer. If the given index is already taken, it fails. And operation will not be moved so the caller's pointer will be still valid.

Parameters
indexIndex to be added
operationIOperation to be added
Returns
OperandIndex index if successful, Undefined otherwise

Definition at line 78 of file Graph.cc.

79{
80 const IOperation &op_ref = *operation;
81 if (!checkOperandsForOperation(op_ref))
82 return OperationIndex{};
83 auto ind_gen = _operations.push(std::move(operation), index);
84 if (ind_gen.valid())
85 {
86 assert(ind_gen == index);
87 linkOperandToOperation(index, op_ref);
88 }
89 return index;
90}
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54
::onert::util::Index< uint32_t, OperationIndexTag > OperationIndex
Definition Index.h:30

References onert::util::ObjectManager< Index, Object >::push().

◆ addOperation() [2/2]

OperationIndex onert::ir::Graph::addOperation ( std::unique_ptr< IOperation > &&  node)

Definition at line 67 of file Graph.cc.

68{
69 const IOperation &op_ref = *operation;
70 if (!checkOperandsForOperation(op_ref))
71 return OperationIndex{};
72 auto ind = _operations.push(std::move(operation));
73 if (ind.valid())
74 linkOperandToOperation(ind, op_ref);
75 return ind;
76}

References onert::util::ObjectManager< Index, Object >::push().

Referenced by onert::ir::train::TrainableGraph::addOperation().

◆ addOutput()

void onert::ir::Graph::addOutput ( const OperandIndex ind,
const std::string &  name = "" 
)

Definition at line 128 of file Graph.cc.

129{
130 if (!name.empty())
131 _name_to_output.emplace(name, IOIndex{_outputs.size()});
132 _outputs.append(ind);
133}

References onert::ir::OperandIndexSequence::append(), and onert::ir::OperandIndexSequence::size().

Referenced by onert::ir::train::TrainableGraph::addOutput().

◆ changeShape()

void onert::ir::Graph::changeShape ( const OperandIndex ind,
const ir::Shape new_shape 
)
overridevirtual

Implements onert::ir::IGraph.

Definition at line 115 of file Graph.cc.

116{
117 assert(_operands.exist(ind));
118 _operands.at(ind).info().shape(new_shape);
119}
const Object & at(const Index &index) const
Get the object that is associated with the given index.
bool exist(const Index &index) const
Get the object that is associated with the given index.

References onert::util::ObjectManager< Index, Object >::at(), and onert::util::ObjectManager< Index, Object >::exist().

Referenced by onert::ir::train::TrainableGraph::changeShape().

◆ getInputIndex()

IOIndex onert::ir::Graph::getInputIndex ( const std::string &  name) const
overridevirtual

Implements onert::ir::IGraph.

Definition at line 135 of file Graph.cc.

136{
137 auto itr = _name_to_input.find(name);
138 return (itr == _name_to_input.end()) ? IOIndex{} : itr->second;
139}

Referenced by onert::ir::train::TrainableGraph::getInputIndex().

◆ getInputs() [1/2]

OperandIndexSequence & onert::ir::Graph::getInputs ( )
inline

Definition at line 105 of file Graph.h.

105{ return _inputs; }

◆ getInputs() [2/2]

◆ getOutputIndex()

IOIndex onert::ir::Graph::getOutputIndex ( const std::string &  name) const
overridevirtual

Implements onert::ir::IGraph.

Definition at line 141 of file Graph.cc.

142{
143 auto itr = _name_to_output.find(name);
144 return (itr == _name_to_output.end()) ? IOIndex{} : itr->second;
145}

Referenced by onert::ir::train::TrainableGraph::getOutputIndex().

◆ getOutputs() [1/2]

OperandIndexSequence & onert::ir::Graph::getOutputs ( )
inline

Definition at line 107 of file Graph.h.

107{ return _outputs; }

◆ getOutputs() [2/2]

◆ operands() [1/2]

Operands & onert::ir::Graph::operands ( )
inline

Definition at line 111 of file Graph.h.

111{ return _operands; } // TODO Remove this non-const accessor

◆ operands() [2/2]

const Operands & onert::ir::Graph::operands ( ) const
inlineoverridevirtual

Implements onert::ir::IGraph.

Definition at line 110 of file Graph.h.

110{ return _operands; }

Referenced by onert::compiler::pass::ConstantOutputPass::callback(), onert::compiler::pass::PermutationInsertionPass::callback(), onert::compiler::pass::ConstantInsertionPass::callback(), onert::compiler::pass::ConstantLoweringPass::callback(), onert::compiler::train::pass::TrainableConstantInsertionPass::callback(), onert::compiler::StaticShapeInferer::dump(), onert::backend::acl_common::AclBackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator, T_Optimizer >::genTensors(), onert::exec::ProfileObserver::handleJobEnd(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::initConsts(), onert::backend::basic::initConsts(), onert::loader::BaseLoader< LoaderDomain >::loadOperand(), onert::ir::train::TrainableGraph::operands(), onert::ir::train::TrainableGraph::operands(), onert::backend::cl_common::BackendContext< T_TensorBuilder, T_ConstantInitializer, T_KernelGenerator >::planTensors(), onert::compiler::pass::UnusedOperandEliminationPass::run(), onert::compiler::pass::OddOutputPass::run(), onert::compiler::pass::OperandPass::run(), topolSortOperations(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::backend::acl_common::AclSubTensorAnalyzer::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), onert::compiler::ShapeValidator::visit(), and onert::compiler::ShapeValidator::visit().

◆ operations() [1/2]

Operations & onert::ir::Graph::operations ( )
inline

Definition at line 113 of file Graph.h.

113{ return _operations; }

◆ operations() [2/2]

◆ removeOperand()

void onert::ir::Graph::removeOperand ( const OperandIndex ind)
inline

Definition at line 92 of file Graph.h.

92{ _operands.remove(ind); }
void remove(const Index &index)
Remove the object that is associated with the given index.

References onert::util::ObjectManager< Index, Object >::remove().

Referenced by onert::ir::train::TrainableGraph::removeOperand().

◆ replaceOperation()

OperationIndex onert::ir::Graph::replaceOperation ( OperationIndex  index,
std::unique_ptr< IOperation > &&  operation 
)

Replace an operation which the graph already has.

If the given index is available, it succeeds. And operation is moved which invalidates the caller's pointer. If the given operation has at least one invalid operand index, it fails. And operation will not be moved so the caller's pointer will be still valid.

No information in the graph is changed except for replacing an operation.

Parameters
operationOperation to be added
Returns
OperationIndex index if successful, UNDEFINED otherwise

Definition at line 92 of file Graph.cc.

94{
95 const IOperation &op_ref = *operation;
96 if (!checkOperandsForOperation(op_ref) || !_operations.exist(index))
97 return OperationIndex{};
98
99 // Check the new operation has the same inputs/outputs as the existing operation
100 const auto &old_op = _operations.at(index);
101 if (!(old_op.getInputs() == op_ref.getInputs() && old_op.getOutputs() == op_ref.getOutputs()))
102 {
103 return OperationIndex{};
104 }
105
106 return _operations.set(index, std::move(operation));
107}
Index set(Index index, std::unique_ptr< Object > &&object)
Set the object in the container with given index.

References onert::util::ObjectManager< Index, Object >::at(), onert::util::ObjectManager< Index, Object >::exist(), onert::ir::IOperation::getInputs(), onert::ir::IOperation::getOutputs(), and onert::util::ObjectManager< Index, Object >::set().

Referenced by onert::ir::train::TrainableGraph::replaceOperation().

◆ setOperandValue()

void onert::ir::Graph::setOperandValue ( const OperandIndex ind,
std::shared_ptr< Data data 
)

Definition at line 109 of file Graph.cc.

110{
111 assert(_operands.exist(ind));
112 _operands.at(ind).data(std::move(data));
113}

References onert::util::ObjectManager< Index, Object >::at(), and onert::util::ObjectManager< Index, Object >::exist().

Referenced by onert::loader::BaseLoader< LoaderDomain >::loadOperand().

◆ topolSortOperations()

std::vector< ir::OperationIndex > onert::ir::Graph::topolSortOperations ( ) const

Definition at line 182 of file Graph.cc.

183{
184 std::vector<ir::OperationIndex> ret;
185 util::Set<ir::OperationIndex> unvisited;
187 [&](const ir::OperationIndex &index, const ir::IOperation &) { unvisited.add(index); });
188
189 std::function<void(const ir::OperationIndex &, const ir::IOperation &)> dfs =
190 [&](const ir::OperationIndex &index, const ir::IOperation &op) -> void {
191 if (!unvisited.contains(index))
192 return;
193 unvisited.remove(index);
194
195 for (const auto &output : op.getOutputs() | ir::Remove::DUPLICATED | ir::Remove::UNDEFINED)
196 {
197 const auto &operand = operands().at(output);
198 for (const auto &use : operand.getUses())
199 {
200 dfs(use, operations().at(use));
201 }
202 }
203 ret.push_back(index);
204 };
205 operations().iterate(dfs);
206
207 assert(unvisited.empty()); // All of the nodes must have been visited
208 // Reversing Postorder DFS result to make it sorted in topoligical order
209 std::reverse(ret.begin(), ret.end());
210 return ret;
211}
const Operands & operands() const override
Definition Graph.h:110
const Operations & operations() const override
Definition Graph.h:112
const OperandIndexSequence & getOutputs() const override
Definition Graph.h:106
void iterate(const std::function< void(const Index &, const Object &)> &fn) const
Iterate over the container with given function.

References onert::util::Set< Element >::add(), onert::util::ObjectManager< Index, Object >::at(), onert::util::Set< Element >::contains(), onert::ir::DUPLICATED, onert::util::Set< Element >::empty(), onert::util::ObjectManager< Index, Object >::iterate(), operands(), operations(), onert::util::Set< Element >::remove(), and onert::ir::UNDEFINED.

Referenced by onert::compiler::StaticShapeInferer::infer(), onert::compiler::train::StaticBackwardShapeInferer::infer(), onert::compiler::Linear::linearize(), and onert::ir::train::TrainableGraph::topolSortOperations().

◆ verify()

void onert::ir::Graph::verify ( void  ) const

Definition at line 147 of file Graph.cc.

148{
149 // Call graph verifications for the MODEL phase
150 {
151 // Except for edge consistency, the user might have been given a bad model
152 // so here it throws an execption rather than assertion.
153 if (!verifier::InputOutputChecker().verify(*this))
154 throw std::runtime_error{"One of model input and output operands does not exist."};
155 if (!verifier::DAGChecker().verify(*this))
156 throw std::runtime_error{"The graph is cyclic."};
157 assert(verifier::EdgeChecker().verify(*this));
158 }
159
160 // Check shape independent operation feature
161 // - Operand type
162 // - Shape independent parameter
163 OperationValidator{*this}();
164}
void verify(void) const
Definition Graph.cc:147

References verify().

Referenced by onert::ir::train::TrainableGraph::updateGraphDependency(), onert::ir::train::TrainableGraph::verify(), and verify().


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