ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci_interpreter::Interpreter Class Reference

#include <Interpreter.h>

Public Member Functions

 Interpreter (const luci::Module *module)
 
 Interpreter (const luci::Module *module, IMemoryManager *memory_manager)
 
 ~Interpreter ()
 
void writeInputTensor (const luci::CircleInput *input_node, const void *data, size_t data_size)
 
void readOutputTensor (const luci::CircleOutput *output_node, void *data, size_t data_size)
 
size_t getOutputTensorSize (const luci::CircleOutput *output_node)
 
void interpret ()
 
void attachObserver (ExecutionObserver *observer)
 
const TensorgetTensor (const loco::Node *node)
 
 Interpreter (const char *model_data_raw, bool dealloc_input)
 
 ~Interpreter ()
 
void allocateAndWriteInputTensor (int32_t input_tensor_index, const uint8_t *data, size_t data_size)
 
uint8_t * allocateInputTensor (int32_t input_tensor_index)
 
uint8_t * readOutputTensor (int32_t output_tensor_index)
 
int32_t getInputDataSizeByIndex (int32_t input_tensor_index)
 
int32_t getOutputDataSizeByIndex (int32_t output_tensor_index)
 
void interpret ()
 

Detailed Description

Definition at line 50 of file Interpreter.h.

Constructor & Destructor Documentation

◆ Interpreter() [1/3]

luci_interpreter::Interpreter::Interpreter ( const luci::Module module)
explicit

Definition at line 73 of file Interpreter.cpp.

74{
75 _runtime_to_ir = std::make_unique<RuntimeToIR>();
76 _event_notifier = std::make_unique<EventNotifierImpl>(*_runtime_to_ir, _observers);
77 _runtime_module = std::make_unique<RuntimeModule>(_event_notifier.get());
78
79 _default_memory_manager = std::make_unique<SimpleMemoryManager>();
80
81 ModuleLoader loader(module, _runtime_module.get(), *_runtime_to_ir, _node_to_tensor,
82 _default_memory_manager.get());
83 loader.load();
84}

References luci_interpreter::ModuleLoader::load().

◆ Interpreter() [2/3]

luci_interpreter::Interpreter::Interpreter ( const luci::Module module,
luci_interpreter::IMemoryManager memory_manager 
)
explicit

Definition at line 86 of file Interpreter.cpp.

88{
89 assert(memory_manager && "Use Interpreter::Interpreter(module) constructor instead");
90
91 _runtime_to_ir = std::make_unique<RuntimeToIR>();
92 _event_notifier = std::make_unique<EventNotifierImpl>(*_runtime_to_ir, _observers);
93 _runtime_module = std::make_unique<RuntimeModule>(_event_notifier.get());
94
95 ModuleLoader loader(module, _runtime_module.get(), *_runtime_to_ir, _node_to_tensor,
96 memory_manager);
97 loader.load();
98}

References luci_interpreter::ModuleLoader::load().

◆ ~Interpreter() [1/2]

luci_interpreter::Interpreter::~Interpreter ( )
default

◆ Interpreter() [3/3]

luci_interpreter::Interpreter::Interpreter ( const char *  model_data_raw,
bool  dealloc_input 
)
explicit

Definition at line 58 of file Interpreter.cpp.

59{
60 ModuleLoader::load(&_runtime_module, &_memory_manager, model_data_raw, dealloc_input);
61}

References luci_interpreter::ModuleLoader::load().

◆ ~Interpreter() [2/2]

luci_interpreter::Interpreter::~Interpreter ( )

Member Function Documentation

◆ allocateAndWriteInputTensor()

void luci_interpreter::Interpreter::allocateAndWriteInputTensor ( int32_t  input_tensor_index,
const uint8_t *  data,
size_t  data_size 
)

Definition at line 83 of file Interpreter.cpp.

85{
86 assert(data_size > 0);
87 assert(data != nullptr);
88 assert(input_tensor_index >= 0);
89 auto *runtime_graph = _runtime_module.getMainGraph();
90 auto tensor_data = runtime_graph->configureGraphInput(input_tensor_index);
91
92 std::memcpy(tensor_data, data, data_size);
93}
const T * data(const std::vector< T, Alloc > &v)

References flatbuffers::data().

◆ allocateInputTensor()

uint8_t * luci_interpreter::Interpreter::allocateInputTensor ( int32_t  input_tensor_index)

Definition at line 95 of file Interpreter.cpp.

96{
97 assert(input_tensor_index >= 0);
98
99 auto *runtime_graph = _runtime_module.getMainGraph();
100
101 return runtime_graph->configureGraphInput(input_tensor_index);
102}

◆ attachObserver()

void luci_interpreter::Interpreter::attachObserver ( ExecutionObserver observer)

Definition at line 144 of file Interpreter.cpp.

145{
146 if (std::find(_observers.cbegin(), _observers.cend(), observer) != _observers.cend())
147 throw std::runtime_error("Observer is already attached.");
148 _observers.push_back(observer);
149}

◆ getInputDataSizeByIndex()

int32_t luci_interpreter::Interpreter::getInputDataSizeByIndex ( int32_t  input_tensor_index)

Definition at line 69 of file Interpreter.cpp.

70{
71 auto *runtime_graph = _runtime_module.getMainGraph();
72
73 return runtime_graph->getInputDataSizeByIndex(input_tensor_index);
74}

◆ getOutputDataSizeByIndex()

int32_t luci_interpreter::Interpreter::getOutputDataSizeByIndex ( int32_t  output_tensor_index)

Definition at line 76 of file Interpreter.cpp.

77{
78 auto *runtime_graph = _runtime_module.getMainGraph();
79
80 return runtime_graph->getOutputDataSizeByIndex(output_tensor_index);
81}

◆ getOutputTensorSize()

size_t luci_interpreter::Interpreter::getOutputTensorSize ( const luci::CircleOutput output_node)

Definition at line 128 of file Interpreter.cpp.

129{
130 Tensor *tensor = _runtime_module->getOutputTensors()[output_node->index()];
131 if (tensor == nullptr)
132 {
133 const std::string &name = output_node->name();
134 throw std::runtime_error("Cannot find tensor size for output node named \"" + name + "\".");
135 }
136
137 size_t tensor_size = luci_interpreter::getDataTypeSize(tensor->element_type());
138 tensor_size *= tensor->shape().num_elements();
139 return tensor_size;
140}
void index(const loco::GraphOutputIndex &index)
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
CircleOutput * output_node(loco::Graph *g, const loco::GraphOutputIndex &index)
Find a CircleOutput node with a given output index.
NodeName name(void) const

References luci_interpreter::getDataTypeSize(), luci::CircleOutput::index(), luci::CircleNode::name(), and luci::output_node().

◆ getTensor()

const Tensor * luci_interpreter::Interpreter::getTensor ( const loco::Node node)
inline

Definition at line 69 of file Interpreter.h.

69{ return _node_to_tensor[node]; }

◆ interpret() [1/2]

void luci_interpreter::Interpreter::interpret ( )

Definition at line 142 of file Interpreter.cpp.

142{ _runtime_module->execute(); }

Referenced by InterpreterWrapper::interpret(), and record_minmax::RecordMinMax::profileData().

◆ interpret() [2/2]

void luci_interpreter::Interpreter::interpret ( )

◆ readOutputTensor() [1/2]

void luci_interpreter::Interpreter::readOutputTensor ( const luci::CircleOutput output_node,
void *  data,
size_t  data_size 
)

Definition at line 115 of file Interpreter.cpp.

117{
118 Tensor *tensor = _runtime_module->getOutputTensors()[output_node->index()];
119 if (tensor == nullptr)
120 {
121 const std::string &name = output_node->name();
122 throw std::runtime_error("Cannot find tensor for output node named \"" + name + "\".");
123 }
124 if (data != nullptr)
125 tensor->readData(data, data_size);
126}

References flatbuffers::data(), luci::CircleOutput::index(), luci::CircleNode::name(), and luci::output_node().

Referenced by InterpreterWrapper::readOutputTensor().

◆ readOutputTensor() [2/2]

uint8_t * luci_interpreter::Interpreter::readOutputTensor ( int32_t  output_tensor_index)

Definition at line 104 of file Interpreter.cpp.

105{
106 auto *runtime_graph = _runtime_module.getMainGraph();
107
108 return runtime_graph->getOutputDataByIndex(output_tensor_index);
109}

◆ writeInputTensor()

void luci_interpreter::Interpreter::writeInputTensor ( const luci::CircleInput input_node,
const void *  data,
size_t  data_size 
)

Definition at line 102 of file Interpreter.cpp.

104{
105 Tensor *tensor = _runtime_module->getInputTensors()[input_node->index()];
106 if (tensor == nullptr)
107 {
108 const std::string &name = input_node->name();
109 throw std::runtime_error("Cannot find tensor for input node named \"" + name + "\".");
110 }
111 if (data != nullptr)
112 tensor->writeData(data, data_size);
113}
void index(const loco::GraphInputIndex &index)
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.

References flatbuffers::data(), luci::CircleInput::index(), luci::input_node(), and luci::CircleNode::name().

Referenced by record_minmax::RecordMinMax::profileData(), and InterpreterWrapper::writeInputTensor().


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