ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 31 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 29 of file MinMaxRecorder.cc.

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

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 36 of file MinMaxRecorder.h.

38 {
39 return;
40 }

◆ 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 69 of file MinMaxRecorder.cc.

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

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

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 143 of file MinMaxRecorder.cc.

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

◆ type()

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

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