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

#include <MinMaxRecorder.h>

Collaboration diagram for onert::exec::MinMaxRecorder:

Public Member Functions

 MinMaxRecorder (const std::string &workspace_dir, const ir::Graph &graph, const backend::BackendContexts &backend_contexts)
 
void handleJobBegin (IExecutor *, ir::SubgraphIndex, ir::OperationIndex, const backend::Backend *) override
 
void handleJobEnd (IExecutor *, ir::SubgraphIndex, ir::OperationIndex, const backend::Backend *) override
 
void handleSubgraphBegin (ir::SubgraphIndex) override
 Invoked just before model (not individual operation) execution begins.
 
void handleSubgraphEnd (ir::SubgraphIndex) override
 Invoked just after model (not individual operation) execution ends.
 
ObserverType type () const override
 
- Public Member Functions inherited from onert::exec::IExecutionObserver
virtual ~IExecutionObserver ()=default
 

Detailed Description

Definition at line 29 of file MinMaxRecorder.h.

Constructor & Destructor Documentation

◆ MinMaxRecorder()

onert::exec::MinMaxRecorder::MinMaxRecorder ( const std::string &  workspace_dir,
const ir::Graph graph,
const backend::BackendContexts backend_contexts 
)

Definition at line 27 of file MinMaxRecorder.cc.

29 : _graph{graph}, _backend_contexts{backend_contexts}, _workspace_dir(workspace_dir)
30{
31 // DO NOTHING
32}

Member Function Documentation

◆ handleJobBegin()

void onert::exec::MinMaxRecorder::handleJobBegin ( IExecutor ,
ir::SubgraphIndex  ,
ir::OperationIndex  ,
const backend::Backend  
)
inlineoverridevirtual

Implements onert::exec::IExecutionObserver.

Definition at line 34 of file MinMaxRecorder.h.

36 {
37 return;
38 }

◆ handleJobEnd()

void onert::exec::MinMaxRecorder::handleJobEnd ( IExecutor ,
ir::SubgraphIndex  subg_idx,
ir::OperationIndex  op_idx,
const backend::Backend backend 
)
overridevirtual

Implements onert::exec::IExecutionObserver.

Definition at line 67 of file MinMaxRecorder.cc.

69{
70 const auto &tensor_reg = _backend_contexts.at(backend)->tensor_registry;
71 const auto &op = _graph.operations().at(op_idx);
72 const auto &outputs = op.getOutputs();
73 // TODO: Support multiple output
74 if (outputs.size() != 1)
75 throw std::runtime_error("Only 1 output operator is supported for recording minmax.");
76
77 auto tensor = tensor_reg->getITensor(outputs.at(0));
78
79 // Logic copied from MinMaxObserver.cpp.
80
81 // Filter Ops
82 if (tensor->is_constant())
83 return;
84
85 if (tensor->data_type() != ir::DataType::FLOAT32)
86 return;
87
88 switch (op.opcode())
89 {
90 // Operators with multiple outputs
91 case ir::OpCode::If:
92 case ir::OpCode::Split:
93 case ir::OpCode::SplitV:
94 case ir::OpCode::TopKV2:
95 case ir::OpCode::Unpack:
96 case ir::OpCode::While:
97 return;
98 // NOTE: Sin, Cos, Tanh's output is in [-1, 1]
99 // We may not need to dump those operators.
100 default:; // Do Nothing
101 }
102
103 // Otherwise, dump!
104 assert(tensor->data_type() == ir::DataType::FLOAT32);
105 auto [min, max] = minmaxFrom(tensor);
106 _op_minmax.append({subg_idx, op_idx}, min, max);
107}
const Operations & operations() const override
Definition Graph.h:112
void append(N node, float min, float max)
Definition MinMaxMap.h:34
const Object & at(const Index &index) const
Get the object that is associated with the given index.
std::pair< float, float > minmaxFrom(const backend::ITensor *tensor)

References onert::util::MinMaxMap< N, Hash >::append(), onert::util::ObjectManager< Index, Object >::at(), onert::exec::minmaxFrom(), and onert::ir::Graph::operations().

◆ handleSubgraphBegin()

void onert::exec::MinMaxRecorder::handleSubgraphBegin ( ir::SubgraphIndex  )
overridevirtual

Invoked just before model (not individual operation) execution begins.

Reimplemented from onert::exec::IExecutionObserver.

Definition at line 109 of file MinMaxRecorder.cc.

110{
111 // Make sure there is only cpu backend except for builtin backend
112 std::set<std::string> backend_names;
113 backend::ITensorRegistry *tensor_reg = nullptr;
114 for (const auto &[backend, bctx] : _backend_contexts)
115 {
116 backend_names.insert(backend->config()->id());
117 if (backend->config()->id() == "cpu")
118 {
119 tensor_reg = bctx->tensor_registry.get();
120 }
121 }
122 if (backend_names != std::set<std::string>{"builtin", "cpu"})
123 throw std::runtime_error("MinMaxRecorder must have cpu backend only.");
124
125 const auto &inputs = _graph.getInputs(); //.at(op_idx);
126 for (uint32_t i = 0; i < inputs.size(); ++i)
127 {
128 auto input_idx = inputs.at(i);
129 auto tensor = tensor_reg->getITensor(input_idx);
130
131 if (tensor->is_constant())
132 return;
133 if (tensor->data_type() != ir::DataType::FLOAT32)
134 return;
135
136 auto [min, max] = minmaxFrom(tensor);
137 _input_minmax.append({subg_idx, ir::IOIndex{i}}, min, max);
138 }
139}
const OperandIndexSequence & getInputs() const override
Definition Graph.h:104
::onert::util::Index< uint32_t, IOIndexTag > IOIndex
Definition Index.h:36

References onert::util::MinMaxMap< N, Hash >::append(), onert::ir::Graph::getInputs(), onert::backend::ITensorRegistry::getITensor(), and onert::exec::minmaxFrom().

◆ handleSubgraphEnd()

void onert::exec::MinMaxRecorder::handleSubgraphEnd ( ir::SubgraphIndex  )
overridevirtual

Invoked just after model (not individual operation) execution ends.

Reimplemented from onert::exec::IExecutionObserver.

Definition at line 141 of file MinMaxRecorder.cc.

142{
143 // It would be better to dump at the end of model execution, not subgraph
144 // But it requires more changes than subgraph.
145 auto raw_dumper = RawMinMaxDumper(_workspace_dir + "/minmax.bin");
146 raw_dumper.dump(_input_minmax, _op_minmax);
147}

◆ type()

ObserverType onert::exec::MinMaxRecorder::type ( ) const
inlineoverridevirtual

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