ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci::QuantizeActivation Struct Referencefinal

Quantize non-const activation using recorded min/max values. More...

#include <QuantizeActivation.h>

Collaboration diagram for luci::QuantizeActivation:

Public Member Functions

 QuantizeActivation (loco::DataType output)
 
void visit (luci::CircleNode *node)
 Default fallback.
 
- Public Member Functions inherited from luci::CircleNodeMutableVisitor< void >
virtual ~CircleNodeMutableVisitor ()=default
 
- Public Member Functions inherited from luci::CircleNodeMutableVisitorBase< T >
virtual ~CircleNodeMutableVisitorBase ()=default
 

Data Fields

loco::DataType output_type
 

Detailed Description

Quantize non-const activation using recorded min/max values.

Definition at line 28 of file QuantizeActivation.h.

Constructor & Destructor Documentation

◆ QuantizeActivation()

luci::QuantizeActivation::QuantizeActivation ( loco::DataType  output)
inline

Definition at line 30 of file QuantizeActivation.h.

30: output_type(output) {}

Member Function Documentation

◆ visit()

void luci::QuantizeActivation::visit ( luci::CircleNode )
virtual

Default fallback.

Reimplemented from luci::CircleNodeMutableVisitor< void >.

Definition at line 42 of file QuantizeActivation.cpp.

43{
44 LOGGER(l);
45 INFO(l) << "QuantizeActivation visit node: " << node->name() << std::endl;
46
47 // Check if node is fp32
48 if (not is_fp32(node))
49 return;
50
51 // Check if this is const (const activation is handled by QuantizeConstInputActivation)
52 // NOTE QuantizePreChecker guarantees weights/bias are const.
53 // Update this code when we accept non-const weights/bias.
54 if (node->opcode() == luci::CircleOpcode::CIRCLECONST)
55 return;
56
57 // Check if this is activation
58 // We assume min/max are recorded only for activations
59 if (has_min_max(node))
60 {
61 // Quantize using recorded min/max
62 auto quantparam = node->quantparam();
63 assert(quantparam);
64 assert(quantparam->min.size() == 1); // only support layer-wise quant
65 assert(quantparam->max.size() == 1); // only support layer-wise quant
66 auto min = quantparam->min[0];
67 auto max = quantparam->max[0];
68
69 float scaling_factor{0};
70 int64_t zp{0};
71 float nudged_min{0};
72 float nudged_max{0};
73
74 if (output_type == loco::DataType::U8)
75 {
76 compute_asym_scale_zp(min, max, scaling_factor, zp, nudged_min, nudged_max);
77 node->dtype(loco::DataType::U8);
78 }
79 else
80 {
81 compute_sym_scale(min, max, scaling_factor, nudged_min, nudged_max);
82 node->dtype(loco::DataType::S16);
83 }
84
85 node->quantparam()->scale.push_back(scaling_factor);
86 node->quantparam()->zerop.push_back(zp);
87 }
88 // Fix special attributes
89 if (node->opcode() == luci::CircleOpcode::CAST)
90 {
91 auto *cast = loco::must_cast<luci::CircleCast *>(node);
92 auto *cast_input = loco::must_cast<luci::CircleNode *>(cast->x());
93
94 // make sure that cast_input is already quantized
95 assert(cast_input->dtype() != loco::DataType::FLOAT32);
96 cast->in_data_type(cast_input->dtype());
97 cast->out_data_type(cast->dtype());
98 }
99}
#define LOGGER(name)
Definition Log.h:65
#define INFO(name)
Definition Log.h:68
T * cast(Object *)
Cast a generic object as a specific one.
void compute_asym_scale_zp(float min, float max, float &scaling_factor, int64_t &zp, float &nudged_min, float &nudged_max)
void compute_sym_scale(float min, float max, float &scaling_factor, float &nudged_min, float &nudged_max, loco::DataType out_type)
bool is_fp32(const CircleNode *node)

References luci::compute_asym_scale_zp(), luci::compute_sym_scale(), INFO, luci::is_fp32(), LOGGER, luci::CircleNode::name(), luci::CircleNode::opcode(), output_type, luci::CircleNode::quantparam(), luci::CircleQuantParam::scale, and luci::CircleQuantParam::zerop.

Field Documentation

◆ output_type

loco::DataType luci::QuantizeActivation::output_type

Definition at line 32 of file QuantizeActivation.h.

Referenced by visit().


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