ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 33 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 123 of file Graph.cc.

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

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 35 of file Graph.cc.

36{
37 return _operands.emplace(shape, type);
38}
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 40 of file Graph.cc.

41{
42 return _operands.push(std::move(operand), index);
43}
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 80 of file Graph.cc.

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

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

◆ addOperation() [2/2]

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

Definition at line 69 of file Graph.cc.

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

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 130 of file Graph.cc.

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

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 117 of file Graph.cc.

118{
119 assert(_operands.exist(ind));
120 _operands.at(ind).info().shape(new_shape);
121}
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 137 of file Graph.cc.

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

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

◆ getInputs() [1/2]

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

Definition at line 107 of file Graph.h.

107{ return _inputs; }

◆ getInputs() [2/2]

◆ getOutputIndex()

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

Implements onert::ir::IGraph.

Definition at line 143 of file Graph.cc.

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

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

◆ getOutputs() [1/2]

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

Definition at line 109 of file Graph.h.

109{ return _outputs; }

◆ getOutputs() [2/2]

◆ operands() [1/2]

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

Definition at line 113 of file Graph.h.

113{ 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 112 of file Graph.h.

112{ 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 115 of file Graph.h.

115{ return _operations; }

◆ operations() [2/2]

◆ removeOperand()

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

Definition at line 94 of file Graph.h.

94{ _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 94 of file Graph.cc.

96{
97 const IOperation &op_ref = *operation;
98 if (!checkOperandsForOperation(op_ref) || !_operations.exist(index))
99 return OperationIndex{};
100
101 // Check the new operation has the same inputs/outputs as the existing operation
102 const auto &old_op = _operations.at(index);
103 if (!(old_op.getInputs() == op_ref.getInputs() && old_op.getOutputs() == op_ref.getOutputs()))
104 {
105 return OperationIndex{};
106 }
107
108 return _operations.set(index, std::move(operation));
109}
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 111 of file Graph.cc.

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

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 184 of file Graph.cc.

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

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

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: