Setting missing quantization info (#3330)
authorShubham Gupta/System SW /SRI-Bangalore/Engineer/삼성전자 <shub98.gupta@samsung.com>
Thu, 25 Oct 2018 04:25:44 +0000 (09:55 +0530)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 25 Oct 2018 04:25:44 +0000 (13:25 +0900)
This patch will pass scale and offset so as to set quantization info.
If required, this quantization info can be used by the operations

Signed-off-by: shubham <shub98.gupta@samsung.com>
runtimes/pure_arm_compute/src/compilation.cc

index 7206815..2fcdd8b 100644 (file)
@@ -2207,10 +2207,12 @@ void Planner::visit(const ::internal::tflite::op::ResizeBilinear::Node &node)
 
   // TODO Should move to the place where the operand is handled, if it is possible.
   // Set Shape Constraints
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   struct Param
   {
@@ -2447,14 +2449,18 @@ void Planner::visit(const ::internal::tflite::op::StridedSlice::Node &node)
   assert(_ctx.at(startData_index).shape().rank() == 1);
   assert(_ctx.at(endData_index).shape().rank() == 1);
   assert(_ctx.at(stridesData_index).shape().rank() == 1);
-  _builder.addShapeConstr(startData_index,
-                          asTensorInfo(asTensorShape(_ctx.at(startData_index).shape()),
-                                       _ctx.at(startData_index).type()));
+  _builder.addShapeConstr(
+      startData_index,
+      asTensorInfo(asTensorShape(_ctx.at(startData_index).shape()), _ctx.at(startData_index).type(),
+                   _ctx.at(startData_index).scale(), _ctx.at(startData_index).zeroPoint()));
   _builder.addShapeConstr(endData_index, asTensorInfo(asTensorShape(_ctx.at(endData_index).shape()),
-                                                      _ctx.at(endData_index).type()));
-  _builder.addShapeConstr(stridesData_index,
-                          asTensorInfo(asTensorShape(_ctx.at(endData_index).shape()),
-                                       _ctx.at(stridesData_index).type()));
+                                                      _ctx.at(endData_index).type(),
+                                                      _ctx.at(endData_index).scale(),
+                                                      _ctx.at(endData_index).zeroPoint()));
+  _builder.addShapeConstr(
+      stridesData_index,
+      asTensorInfo(asTensorShape(_ctx.at(endData_index).shape()), _ctx.at(stridesData_index).type(),
+                   _ctx.at(stridesData_index).scale(), _ctx.at(stridesData_index).zeroPoint()));
 
   // Set initializers for indices data such as order of inputData
   {
@@ -2580,10 +2586,12 @@ void Planner::visit(const ::internal::tflite::op::ReduceMax::Node &node)
     }
   }
 
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   std::vector<uint32_t> axis;
   {
@@ -3019,10 +3027,12 @@ void Planner::visit(const ::internal::tflite::op::Tanh::Node &node)
   const ::internal::tflite::operand::Index ifm_index{node.param().ifm_index};
 
   // Set shape constraints
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
 
   struct Param
   {
@@ -3168,8 +3178,10 @@ void Planner::visit(const ::internal::tflite::op::Mean::Node &node)
   _builder.addShapeConstr(
       ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
                               _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
-  _builder.addShapeConstr(axis_index, asTensorInfo(asTensorShape(_ctx.at(axis_index).shape()),
-                                                   _ctx.at(axis_index).type()));
+  _builder.addShapeConstr(axis_index,
+                          asTensorInfo(asTensorShape(_ctx.at(axis_index).shape()),
+                                       _ctx.at(axis_index).type(), _ctx.at(axis_index).scale(),
+                                       _ctx.at(axis_index).zeroPoint()));
 
   // TODO keep_dims==0
   assert(keep_dims != 0);
@@ -3286,23 +3298,37 @@ void Planner::visit(const ::internal::tflite::op::RNN::Node &node)
          num_units == _ctx.at(hidden_state_out_index).shape().dim(1));
 
   // Set Shape Constraints and TensorInfo
-  _builder.addShapeConstr(output_index, asTensorInfo(asTensorShape(_ctx.at(output_index).shape()),
-                                                     _ctx.at(output_index).type()));
+  _builder.addShapeConstr(output_index,
+                          asTensorInfo(asTensorShape(_ctx.at(output_index).shape()),
+                                       _ctx.at(output_index).type(), _ctx.at(output_index).scale(),
+                                       _ctx.at(output_index).zeroPoint()));
   _builder.addShapeConstr(hidden_state_out_index,
                           asTensorInfo(asTensorShape(_ctx.at(hidden_state_out_index).shape()),
-                                       _ctx.at(hidden_state_out_index).type()));
-  _builder.addShapeConstr(input_index, asTensorInfo(asTensorShape(_ctx.at(input_index).shape()),
-                                                    _ctx.at(input_index).type()));
+                                       _ctx.at(hidden_state_out_index).type(),
+                                       _ctx.at(hidden_state_out_index).scale(),
+                                       _ctx.at(hidden_state_out_index).zeroPoint()));
+  _builder.addShapeConstr(input_index,
+                          asTensorInfo(asTensorShape(_ctx.at(input_index).shape()),
+                                       _ctx.at(input_index).type(), _ctx.at(input_index).scale(),
+                                       _ctx.at(input_index).zeroPoint()));
   _builder.addShapeConstr(weights_index, asTensorInfo(asTensorShape(_ctx.at(weights_index).shape()),
-                                                      _ctx.at(weights_index).type()));
+                                                      _ctx.at(weights_index).type(),
+                                                      _ctx.at(weights_index).scale(),
+                                                      _ctx.at(weights_index).zeroPoint()));
   _builder.addShapeConstr(recurrent_weights_index,
                           asTensorInfo(asTensorShape(_ctx.at(recurrent_weights_index).shape()),
-                                       _ctx.at(recurrent_weights_index).type()));
-  _builder.addShapeConstr(bias_index, asTensorInfo(asTensorShape(_ctx.at(bias_index).shape()),
-                                                   _ctx.at(bias_index).type()));
+                                       _ctx.at(recurrent_weights_index).type(),
+                                       _ctx.at(recurrent_weights_index).scale(),
+                                       _ctx.at(recurrent_weights_index).zeroPoint()));
+  _builder.addShapeConstr(bias_index,
+                          asTensorInfo(asTensorShape(_ctx.at(bias_index).shape()),
+                                       _ctx.at(bias_index).type(), _ctx.at(bias_index).scale(),
+                                       _ctx.at(bias_index).zeroPoint()));
   _builder.addShapeConstr(hidden_state_in_index,
                           asTensorInfo(asTensorShape(_ctx.at(hidden_state_in_index).shape()),
-                                       _ctx.at(hidden_state_in_index).type()));
+                                       _ctx.at(hidden_state_in_index).type(),
+                                       _ctx.at(hidden_state_in_index).scale(),
+                                       _ctx.at(hidden_state_in_index).zeroPoint()));
 
   // Construct operation parameters
   struct Param
@@ -3428,10 +3454,12 @@ void Planner::visit(const ::internal::tflite::op::Floor::Node &node)
   const ::internal::tflite::operand::Index ifm_index{node.param().input_index};
 
   // Set shape constraints
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   struct Param
   {
@@ -3477,10 +3505,14 @@ void Planner::visit(const ::internal::tflite::op::RSQRT::Node &node)
   const ::internal::tflite::operand::Index input_index{node.param().input_index};
 
   // Set shape constraints
-  _builder.addShapeConstr(output_index, asTensorInfo(asTensorShape(_ctx.at(output_index).shape()),
-                                                     _ctx.at(output_index).type()));
-  _builder.addShapeConstr(input_index, asTensorInfo(asTensorShape(_ctx.at(input_index).shape()),
-                                                    _ctx.at(input_index).type()));
+  _builder.addShapeConstr(output_index,
+                          asTensorInfo(asTensorShape(_ctx.at(output_index).shape()),
+                                       _ctx.at(output_index).type(), _ctx.at(output_index).scale(),
+                                       _ctx.at(output_index).zeroPoint()));
+  _builder.addShapeConstr(input_index,
+                          asTensorInfo(asTensorShape(_ctx.at(input_index).shape()),
+                                       _ctx.at(input_index).type(), _ctx.at(input_index).scale(),
+                                       _ctx.at(input_index).zeroPoint()));
 
   struct Param
   {
@@ -4095,10 +4127,12 @@ void Planner::visit(const ::internal::tflite::op::L2Pool2D::Implicit::Node &node
   assert((ANEURALNETWORKS_PADDING_SAME == padding_type) ||
          (ANEURALNETWORKS_PADDING_VALID == padding_type));
 
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   struct Param
   {
@@ -4190,10 +4224,12 @@ void Planner::visit(const ::internal::tflite::op::L2Pool2D::Explicit::Node &node
   const int32_t padding_top = _ctx.at(padding_top_index).asScalar<int32_t>();
   const int32_t padding_bottom = _ctx.at(padding_bottom_index).asScalar<int32_t>();
 
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   // Construct operation parameters
   struct Param
@@ -4639,10 +4675,12 @@ void Planner::visit(const ::internal::tflite::op::Exp::Node &node)
   const ::internal::tflite::operand::Index ifm_index{node.param().ifm_index};
 
   // Set shape constraints
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   struct Param
   {
@@ -4724,10 +4762,12 @@ void Planner::visit(const ::internal::tflite::op::ReduceSum::Node &node)
   }
 
   // Set shape constraints
-  _builder.addShapeConstr(ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()),
-                                                  _ctx.at(ofm_index).type()));
-  _builder.addShapeConstr(ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()),
-                                                  _ctx.at(ifm_index).type()));
+  _builder.addShapeConstr(
+      ofm_index, asTensorInfo(asTensorShape(_ctx.at(ofm_index).shape()), _ctx.at(ofm_index).type(),
+                              _ctx.at(ofm_index).scale(), _ctx.at(ofm_index).zeroPoint()));
+  _builder.addShapeConstr(
+      ifm_index, asTensorInfo(asTensorShape(_ctx.at(ifm_index).shape()), _ctx.at(ifm_index).type(),
+                              _ctx.at(ifm_index).scale(), _ctx.at(ifm_index).zeroPoint()));
 
   uint32_t input_rank = ifm_shape.rank();
   std::vector<uint32_t> axis;