• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/delegates/hexagon/builders/quantize_builder.h"
16 
17 #include <stdint.h>
18 
19 #include <limits>
20 
21 #include "tensorflow/lite/c/builtin_op_data.h"
22 #include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn.h"
23 #include "tensorflow/lite/kernels/kernel_util.h"
24 
25 namespace tflite {
26 namespace delegates {
27 namespace hexagon {
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)28 TfLiteStatus QuantizeOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
29                                                  const TfLiteIntArray* outputs,
30                                                  TfLiteContext* context) {
31   const auto& input_tensor = context->tensors[inputs->data[0]];
32   const auto& output_tensor = context->tensors[outputs->data[0]];
33   int output_batch_size, output_height_size, output_width_size,
34       output_depth_size;
35   GetDims(&output_batch_size, &output_height_size, &output_width_size,
36           &output_depth_size, output_tensor.dims);
37 
38   AddInput(graph_builder_->GetHexagonTensorId(inputs->data[0]));
39   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
40   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, output_tensor));
41 
42   // Hexagon outputs for this node.
43   node_output_ = AddOutput(sizeof(uint8_t), 4,
44                            {output_batch_size, output_height_size,
45                             output_width_size, output_depth_size});
46   AddOutput(sizeof(float), 4, kScalarShape);
47   AddOutput(sizeof(float), 4, kScalarShape);
48 
49   return kTfLiteOk;
50 }
51 
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)52 TfLiteStatus QuantizeOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
53                                                 TfLiteContext* context) {
54   // Should be only 1 output.
55   graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
56                                   node_output_.second);
57 
58   return kTfLiteOk;
59 }
60 
~QuantizeOpBuilder()61 QuantizeOpBuilder::~QuantizeOpBuilder() {}
62 
CreateQuantizeBuilder(GraphBuilder * graph_builder,int op_type)63 OpBuilder* CreateQuantizeBuilder(GraphBuilder* graph_builder, int op_type) {
64   return new QuantizeOpBuilder(graph_builder, op_type);
65 }
66 
67 }  // namespace hexagon
68 }  // namespace delegates
69 }  // namespace tflite
70