1 /* Copyright 2019 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/l2_normalization_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 L2NormalizationOpBuilder::PopulateSubGraph(
29     const TfLiteIntArray* inputs, const TfLiteIntArray* outputs,
30     TfLiteContext* context) {
31   // Input data tensor.
32   int tensor_id = inputs->data[0];
33   const auto& input_tensor = context->tensors[tensor_id];
34   AddInput(graph_builder_->GetHexagonTensorId(tensor_id));
35   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
36 
37   // Hexagon outputs for this node.
38   int output_batch_size, output_height_size, output_width_size,
39       output_depth_size;
40   GetDims(&output_batch_size, &output_height_size, &output_width_size,
41           &output_depth_size, context->tensors[outputs->data[0]].dims);
42   node_output_ = AddOutput(sizeof(uint8_t), 4,
43                            {output_batch_size, output_height_size,
44                             output_width_size, output_depth_size});
45   AddOutput(sizeof(float), 4, kScalarShape);
46   AddOutput(sizeof(float), 4, kScalarShape);
47 
48   return kTfLiteOk;
49 }
50 
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)51 TfLiteStatus L2NormalizationOpBuilder::RegisterOutputs(
52     const TfLiteIntArray* outputs, TfLiteContext* context) {
53   // Should be only 1 output.
54   graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
55                                   node_output_.second);
56 
57   return kTfLiteOk;
58 }
59 
~L2NormalizationOpBuilder()60 L2NormalizationOpBuilder::~L2NormalizationOpBuilder() {}
61 
CreateL2NormalizationBuilder(GraphBuilder * graph_builder,int op_type)62 OpBuilder* CreateL2NormalizationBuilder(GraphBuilder* graph_builder,
63                                         int op_type) {
64   return new L2NormalizationOpBuilder(graph_builder, op_type);
65 }
66 
67 }  // namespace hexagon
68 }  // namespace delegates
69 }  // namespace tflite
70