ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mpqsolver::core::Quantizer Class Reference

#include <Quantizer.h>

Data Structures

struct  Context
 

Public Member Functions

 Quantizer (const Context &ctx)
 
void setHook (const QuantizerHook *callback)
 set hook on the end of quantization event
 
bool quantize (luci::Module *module, const std::string &quant_dtype, LayerParams &layer_params)
 quantize recorded module (min/max initialized) with specified parameters returns true on success
 
bool quantize (luci::Module *module, LayerParams &layer_params)
 quantize recorded module (min/max initialized) with specified parameters returns true on success
 
bool fakeQuantize (luci::Module *module, const std::string &quant_dtype, LayerParams &layer_params)
 fake_quantize recorded module (min/max initialized) with specified parameters returns true on success
 
const ContextgetContext () const
 

Detailed Description

Definition at line 44 of file Quantizer.h.

Constructor & Destructor Documentation

◆ Quantizer()

mpqsolver::core::Quantizer::Quantizer ( const Context ctx)
inline

Definition at line 59 of file Quantizer.h.

59: _ctx(ctx) {}

Member Function Documentation

◆ fakeQuantize()

bool Quantizer::fakeQuantize ( luci::Module module,
const std::string &  quant_dtype,
LayerParams layer_params 
)

fake_quantize recorded module (min/max initialized) with specified parameters returns true on success

Definition at line 132 of file Quantizer.cpp.

134{
135 if (!quantize(module, quant_dtype, layer_params))
136 return false;
137
138 if (!make_model_fake_quantized(module))
139 return false;
140
141 return true;
142}
bool quantize(luci::Module *module, const std::string &quant_dtype, LayerParams &layer_params)
quantize recorded module (min/max initialized) with specified parameters returns true on success
Definition Quantizer.cpp:58

References quantize().

◆ getContext()

const Context & mpqsolver::core::Quantizer::getContext ( ) const
inline

Definition at line 85 of file Quantizer.h.

85{ return _ctx; }

◆ quantize() [1/2]

bool Quantizer::quantize ( luci::Module module,
const std::string &  quant_dtype,
LayerParams layer_params 
)

quantize recorded module (min/max initialized) with specified parameters returns true on success

Definition at line 58 of file Quantizer.cpp.

60{
61 if (!module)
62 return false;
63
64 static const std::string default_dtype = "float32";
65
66 luci::CircleQuantizer quantizer;
67
68 auto options = quantizer.options();
69 options->enable(Algorithms::QuantizeWithMinMax);
70
71 options->param(AlgorithmParameters::Quantize_input_model_dtype, default_dtype);
72 options->param(AlgorithmParameters::Quantize_output_model_dtype, quant_dtype);
73 // Only channel-wise quantization is supported for int16
74 // TODO Fix this if this assumption breaks
75 if (quant_dtype == "int16")
76 options->param(AlgorithmParameters::Quantize_granularity, "channel");
77 else
78 options->param(AlgorithmParameters::Quantize_granularity, _ctx.granularity);
79
80 options->param(AlgorithmParameters::Quantize_input_type, _ctx.input_type);
81 options->param(AlgorithmParameters::Quantize_output_type, _ctx.output_type);
82 options->param(AlgorithmParameters::Quantize_TF_style_maxpool,
83 _ctx.TF_style_maxpool ? "True" : "False");
84 options->param(AlgorithmParameters::Quantize_save_min_max, _ctx.save_min_max ? "True" : "False");
85
86 if (!layer_params.empty())
87 {
88 try
89 {
90 options->layer_params(AlgorithmParameters::Quantize_layer_params, layer_params);
91 }
92 catch (const std::runtime_error &e)
93 {
94 std::cerr << e.what() << '\n';
95 return false;
96 }
97 }
98
99 for (size_t idx = 0; idx < module->size(); ++idx)
100 {
101 auto graph = module->graph(idx);
102 // quantize the graph
103 quantizer.quantize(graph);
104 if (!luci::validate(graph))
105 {
106 std::cerr << "ERROR: Quantized graph is invalid" << std::endl;
107 return false;
108 }
109 }
110
111 if (_hook)
112 {
113 _hook->onQuantized(module);
114 }
115
116 return true;
117}
void quantize(loco::Graph *) const
bool validate(luci::PartitionTable &partition)
virtual void onQuantized(luci::Module *module) const =0
called on successfull quantization

References mpqsolver::core::Quantizer::Context::granularity, mpqsolver::core::Quantizer::Context::input_type, mpqsolver::core::QuantizerHook::onQuantized(), luci::CircleQuantizer::options(), mpqsolver::core::Quantizer::Context::output_type, luci::CircleQuantizer::quantize(), mpqsolver::core::Quantizer::Context::save_min_max, mpqsolver::core::Quantizer::Context::TF_style_maxpool, and luci::validate().

Referenced by fakeQuantize(), and quantize().

◆ quantize() [2/2]

bool Quantizer::quantize ( luci::Module module,
LayerParams layer_params 
)

quantize recorded module (min/max initialized) with specified parameters returns true on success

Definition at line 123 of file Quantizer.cpp.

124{
125 return quantize(module, _ctx.output_model_dtype, layer_params);
126}

References mpqsolver::core::Quantizer::Context::output_model_dtype, and quantize().

◆ setHook()

void Quantizer::setHook ( const QuantizerHook callback)

set hook on the end of quantization event

Definition at line 52 of file Quantizer.cpp.

52{ _hook = hook; }

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