ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::odc::Quantizer Class Reference

#include <Quantizer.h>

Collaboration diagram for onert::odc::Quantizer:

Public Member Functions

 Quantizer ()=default
 
 ~Quantizer ()=default
 
int quantize (const char *in, const char *out, QuantizeType qtype) override
 
void setMinMaxRecordsThreshold (uint32_t value)
 Set the number of minmax records enough for quantization.
 
bool readyForQuantize () override
 Checking the number of minmax records enough for quantization (comparison with threshold)
 
bool deleteMinMaxFile () override
 Delete minmax file.
 
- Public Member Functions inherited from onert::odc::IQuantizer
virtual ~IQuantizer ()=default
 

Detailed Description

Definition at line 27 of file Quantizer.h.

Constructor & Destructor Documentation

◆ Quantizer()

onert::odc::Quantizer::Quantizer ( )
default

◆ ~Quantizer()

onert::odc::Quantizer::~Quantizer ( )
default

Member Function Documentation

◆ deleteMinMaxFile()

bool onert::odc::Quantizer::deleteMinMaxFile ( )
overridevirtual

Delete minmax file.

Returns
True if there were no errors, False otherwise

Implements onert::odc::IQuantizer.

Definition at line 174 of file Quantizer.cc.

175{
176 int result = 0;
177
178 std::string minmax_path = getMinMaxFilePath();
179 result = std::remove(minmax_path.c_str());
180
181 if (!result)
182 {
183 return true;
184 }
185 else
186 return false;
187}
result
Definition infer.py:103

◆ quantize()

int onert::odc::Quantizer::quantize ( const char *  in,
const char *  out,
QuantizeType  qtype 
)
overridevirtual

Implements onert::odc::IQuantizer.

Definition at line 87 of file Quantizer.cc.

88{
89 if (not in || not out)
90 return 1;
91
92 bool full_quantize = false;
93 if (qtype == QuantizeType::ODC_QTYPE_U8_ASYM || qtype == QuantizeType::ODC_QTYPE_I16_SYM)
94 full_quantize = true;
95
96 // Load model from the file
97 luci::ImporterEx importerex;
98 auto module = importerex.importVerifyModule(std::string(in));
99 if (module.get() == nullptr)
100 return 1;
101
102 // Additional phase for full quantization
103 if (full_quantize)
104 {
105 luci::CircleQuantizer quantizer;
106 auto options = quantizer.options();
107 fillQuantizeOptionParam(options, qtype);
108
109 // Fake quantization
111 for (size_t idx = 0; idx < module->size(); ++idx)
112 {
113 auto graph = module->graph(idx);
114
115 // quantize the graph
116 quantizer.quantize(graph);
117 }
118
119 // Record minmax by minmax-embedder
120 auto minmax_path = getMinMaxFilePath();
121 Embedder().embed(module.get(), minmax_path, {1.f, 99.f});
122 }
123
124 luci::CircleQuantizer quantizer;
125 auto options = quantizer.options();
126 fillQuantizeOptionParam(options, qtype);
127
128 if (full_quantize)
130 else
132
133 for (size_t idx = 0; idx < module->size(); ++idx)
134 {
135 auto graph = module->graph(idx);
136
137 // quantize the graph
138 quantizer.quantize(graph);
139
140 // Skip validate
141 // TODO Validate if needed
142#if 0
143 if (!luci::validate(graph))
144 {
145 std::cerr << "ERROR: Quantized graph is invalid" << std::endl;
146 return 1;
147 }
148#endif
149 }
150
151 // Export to output Circle file
152 luci::CircleExporter exporter;
153 luci::CircleFileExpContract contract(module.get(), std::string(out));
154
155 if (!exporter.invoke(&contract))
156 return 1;
157
158 return 0;
159}
bool invoke(Contract *) const
void quantize(loco::Graph *) const
bool validate(luci::PartitionTable &partition)

References onert::odc::Embedder::embed(), luci::CircleExporter::invoke(), luci::CircleQuantizer::options(), luci::CircleQuantizer::quantize(), luci::CircleQuantizer::Options::QuantizeDequantizeWeights, luci::CircleQuantizer::Options::QuantizeWeights, luci::CircleQuantizer::Options::QuantizeWithMinMax, and luci::validate().

◆ readyForQuantize()

bool onert::odc::Quantizer::readyForQuantize ( )
overridevirtual

Checking the number of minmax records enough for quantization (comparison with threshold)

Returns
True if ready, False otherwise

Implements onert::odc::IQuantizer.

Definition at line 161 of file Quantizer.cc.

162{
163 std::string minmax_path = getMinMaxFilePath();
164
165 MinMaxReader mmr{minmax_path};
166
167 uint32_t numRuns = mmr.readNumRuns();
168 if (_minmax_threshold != 0 && numRuns >= _minmax_threshold)
169 return true;
170 else
171 return false;
172}

References onert::odc::MinMaxReader::readNumRuns().

◆ setMinMaxRecordsThreshold()

void onert::odc::Quantizer::setMinMaxRecordsThreshold ( uint32_t  value)
inlinevirtual

Set the number of minmax records enough for quantization.

Implements onert::odc::IQuantizer.

Definition at line 38 of file Quantizer.h.

38{ _minmax_threshold = value; };

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