ONE - On-device Neural Engine
Loading...
Searching...
No Matches
locomotiv::Session Class Referencefinal

Session for loco graph inference. More...

#include <Session.h>

Public Member Functions

 Session ()=delete
 
 Session (loco::Graph *g)
 Make Session for graph with graph outputs themselves.
 
 Session (loco::Graph *g, const std::vector< loco::Node * > &custom_outputs)
 Make Session for graph with selective custom outputs. Only subgraph to calculate given outputs would be executed.
 
template<typename InputIt >
 Session (loco::Graph *g, InputIt begin, InputIt end)
 Make Session by range.
 
 ~Session ()
 Free all node annotations of the graph assigned by this Session.
 
uint32_t input_size () const
 Get number of graph inputs held by this Session.
 
void set_input (uint32_t index, std::unique_ptr< NodeData > &&data)
 Set graph input at specific index by NodeData.
 
void infer ()
 Do inference for this session and graph.
 
uint32_t output_size () const
 Get number of graph outputs held by this Session.
 
const NodeDataget_output (uint32_t index)
 Get output of graph as NodeData.
 
const loco::Nodeget_output_node (uint32_t index)
 

Detailed Description

Session for loco graph inference.

Definition at line 33 of file Session.h.

Constructor & Destructor Documentation

◆ Session() [1/4]

locomotiv::Session::Session ( )
delete

◆ Session() [2/4]

locomotiv::Session::Session ( loco::Graph g)
inline

Make Session for graph with graph outputs themselves.

Definition at line 39 of file Session.h.

40 {
41 // DO NOTHING
42 }
std::vector< Node * > output_nodes(Graph *)
Definition Graph.cpp:101

◆ Session() [3/4]

locomotiv::Session::Session ( loco::Graph g,
const std::vector< loco::Node * > &  custom_outputs 
)
inline

Make Session for graph with selective custom outputs. Only subgraph to calculate given outputs would be executed.

Note
Set required inputs for given outputs, or inference may fail.
custom_outputs don't need to be graph output, but can be any nodes in the middle of the graph. @warn This approach may fail in case of graph with control flow

Definition at line 53 of file Session.h.

54 : _graph(g), _outputs(custom_outputs)
55 {
56 // DO NOTHING
57 }

◆ Session() [4/4]

template<typename InputIt >
locomotiv::Session::Session ( loco::Graph g,
InputIt  begin,
InputIt  end 
)
inline

Make Session by range.

Definition at line 61 of file Session.h.

61 : _graph(g), _outputs(begin, end)
62 {
63 // DO NOTHING
64 }
int32_t begin[5]
Definition Slice.cpp:33

◆ ~Session()

locomotiv::Session::~Session ( )

Free all node annotations of the graph assigned by this Session.

Definition at line 30 of file Session.cpp.

31{
32 for (uint32_t i = 0; i < _graph->nodes()->size(); ++i)
33 {
34 auto node = _graph->nodes()->at(i);
35 erase_user_data(node);
36 erase_annot_data(node);
38 }
39}
NodeContext * nodes(void)
Definition Graph.h:218
T * at(uint32_t n) const
Access N-th object.
Definition ObjectPool.h:41
void erase_annot_domain(loco::Node *node)
Erase already annotated node domain.
int32_t size[5]
Definition Slice.cpp:35

References loco::ObjectPool< T >::at(), locomotiv::erase_annot_domain(), loco::Graph::nodes(), and size.

Member Function Documentation

◆ get_output()

const NodeData * locomotiv::Session::get_output ( uint32_t  index)

Get output of graph as NodeData.

Note
May return nullptr, for example, when graph output not yet calculated

Definition at line 85 of file Session.cpp.

86{
87 assert(index < output_size());
88
89 auto output_node = _outputs.at(index);
90 return annot_data(output_node);
91}
uint32_t output_size() const
Get number of graph outputs held by this Session.
Definition Session.h:90
CircleOutput * output_node(loco::Graph *g, const loco::GraphOutputIndex &index)
Find a CircleOutput node with a given output index.

References output_size().

Referenced by nnkit::support::moco::tf::OutputTensorContext::getConstFloatTensor().

◆ get_output_node()

const loco::Node * locomotiv::Session::get_output_node ( uint32_t  index)
inline

Definition at line 99 of file Session.h.

99{ return _outputs.at(index); }

◆ infer()

void locomotiv::Session::infer ( )

Do inference for this session and graph.

Note
Multiple run is possible. Abort program when inputs are not fully set or invalid calculation found in the middle.

Definition at line 75 of file Session.cpp.

76{
77 auto schedules = loco::postorder_traversal(_outputs);
78
79 for (auto node : schedules)
80 {
81 NodeExecution::get().run(node);
82 }
83}
static NodeExecution & get()
void run(loco::Node *node)
Run calculation for one unspecified Node.
std::vector< loco::Node * > postorder_traversal(const std::vector< loco::Node * > &roots)
Generate postorder traversal sequence starting from "roots".
Definition Algorithm.cpp:53

References locomotiv::NodeExecution::get(), loco::postorder_traversal(), and locomotiv::NodeExecution::run().

◆ input_size()

uint32_t locomotiv::Session::input_size ( ) const
inline

Get number of graph inputs held by this Session.

Definition at line 70 of file Session.h.

70{ return _graph->inputs()->size(); }
InputContext * inputs(void)
Definition Graph.h:220
uint32_t size(void) const
Return the number of objects.
Definition ObjectPool.h:38

References loco::Graph::inputs(), and loco::ObjectPool< T >::size().

Referenced by set_input().

◆ output_size()

uint32_t locomotiv::Session::output_size ( ) const
inline

Get number of graph outputs held by this Session.

Definition at line 90 of file Session.h.

90{ return _outputs.size(); }

Referenced by get_output().

◆ set_input()

void locomotiv::Session::set_input ( uint32_t  index,
std::unique_ptr< NodeData > &&  data 
)

Set graph input at specific index by NodeData.

Exceptions
runtime_errorIn case when another NodeData already annotated for the input, and when given data type or shape are not congruent with loco node information.

Definition at line 41 of file Session.cpp.

42{
43 assert(index < input_size());
44
45 // Check whether already annotated
46 auto pull = loco::pull_node(_graph, index);
47 if (user_data(pull))
48 {
49 throw std::runtime_error("Graph input already has NodeData");
50 }
51
52 // Check data type match
53 if (pull->dtype() != data->dtype())
54 {
55 throw std::runtime_error("Data type mismatch");
56 }
57
58 // Check shape match
59 auto shape = data->shape();
60 if (pull->rank() != shape->rank())
61 {
62 throw std::runtime_error("Shape rank mismatch");
63 }
64 for (uint32_t i = 0; i < pull->rank(); ++i)
65 {
66 if (pull->dim(i).known() && pull->dim(i).value() != shape->dim(i))
67 {
68 throw std::runtime_error("Shape dimension mismatch");
69 }
70 }
71
72 user_data(pull, std::move(data));
73}
uint32_t input_size() const
Get number of graph inputs held by this Session.
Definition Session.h:70
Pull * pull_node(Graph *g, const GraphInputIndex &index)
Find a Pull node with a given input index.
Definition Nodes.cpp:162

References input_size(), and loco::pull_node().

Referenced by package.infer.session::set_inputs().


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