ONE - On-device Neural Engine
Loading...
Searching...
No Matches
q_implant::QImplant Class Referencefinal

#include <QImplant.h>

Public Member Functions

 QImplant (const std::string &path)
 
void write (loco::Graph *g)
 

Detailed Description

Definition at line 27 of file QImplant.h.

Constructor & Destructor Documentation

◆ QImplant()

q_implant::QImplant::QImplant ( const std::string &  path)
inline

Definition at line 30 of file QImplant.h.

30: _path(path) {}

Member Function Documentation

◆ write()

void QImplant::write ( loco::Graph g)

Definition at line 205 of file QImplant.cpp.

206{
207 const auto root = load_json(_path);
208 const auto dir_path = directory_path(_path);
209
210 std::unordered_map<std::string, luci::CircleNode *> name_to_node;
211 for (auto node : loco::all_nodes(g))
212 {
213 auto circle_node = loco::must_cast<luci::CircleNode *>(node);
214 if (circle_node->opcode() == luci::CircleOpcode::CIRCLEOUTPUT)
215 {
216 continue;
217 }
218
219 name_to_node[circle_node->name()] = circle_node;
220 }
221
222 THROW_UNLESS(root.isObject());
223
224 for (const auto tensor_name : root.getMemberNames())
225 {
226 const auto tensor = root[tensor_name];
227
228 verify_tensor(tensor);
229
230 const auto scale_path = dir_path + '/' + tensor["scale"].asString();
231 const auto zerop_path = dir_path + '/' + tensor["zerop"].asString();
232 const auto quantized_dimension = tensor["quantized_dimension"].asUInt();
233 const auto dtype = str_to_dtype(tensor["dtype"].asString());
234
235 auto node = name_to_node.at(tensor_name);
236
237 // Node must be fp32
238 THROW_UNLESS(node->dtype() == loco::DataType::FLOAT32);
239
240 node->quantparam(std::make_unique<luci::CircleQuantParam>());
241
242 set_dtype(node, dtype);
243 set_scale(node, scale_path);
244 set_zerop(node, zerop_path);
245 set_quantized_dimension(node, quantized_dimension);
246
247 if (tensor.isMember("value"))
248 {
249 auto const_node = loco::must_cast<luci::CircleConst *>(node);
250 const auto value_path = dir_path + '/' + tensor["value"].asString();
251
252 set_value(const_node, value_path, dtype);
253 }
254 }
255
256 forward_qparam(g);
257
258 // Update output nodes
259 auto graph_outputs = g->outputs();
260 assert(graph_outputs); // FIX_CALLER_UNLESS
261 for (auto node : loco::output_nodes(g))
262 {
263 auto out_node = loco::must_cast<luci::CircleOutput *>(node);
264 auto from_node = loco::must_cast<luci::CircleNode *>(out_node->from());
265
266 THROW_UNLESS(from_node->quantparam());
267
268 out_node->quantparam(std::make_unique<luci::CircleQuantParam>());
269 out_node->quantparam()->scale = from_node->quantparam()->scale;
270 out_node->quantparam()->zerop = from_node->quantparam()->zerop;
271 out_node->quantparam()->quantized_dimension = from_node->quantparam()->quantized_dimension;
272 out_node->dtype(from_node->dtype());
273
274 auto graph_output = graph_outputs->at(out_node->index());
275 graph_output->dtype(out_node->dtype());
276 }
277
278 // Verify quantized model
279 for (auto node : loco::active_nodes(loco::output_nodes(g)))
280 {
281 auto circle_node = loco::must_cast<luci::CircleNode *>(node);
282
283 // Throw an exception if dtype is not float32
284 // TODO Operator-level verification (ex: using QuantizedModelVerifier)
285 THROW_UNLESS(circle_node->dtype() != loco::DataType::FLOAT32);
286 }
287}
#define THROW_UNLESS(COND, MSG)
Op * root(Op *)
Return the root Op from a given Op node.
Definition Op.cpp:144
std::set< Node * > all_nodes(Graph *)
Enumerate all the nodes in a given graph.
Definition Graph.cpp:59
std::set< loco::Node * > active_nodes(const std::vector< loco::Node * > &roots)
Enumerate all the nodes required to compute "roots".
std::vector< Node * > output_nodes(Graph *)
Definition Graph.cpp:101
const char * tensor_name(const circle::Tensor *tensor)
loco::DataType str_to_dtype(const std::string &str)
Definition Strings.cpp:47

References loco::active_nodes(), loco::all_nodes(), loco::output_nodes(), and THROW_UNLESS.

Referenced by entry().


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