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/resize_nearest_neighbor_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 ResizeNearestNeighborOpBuilder::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
36 // Output dimensions tensor.
37 tensor_id = inputs->data[1];
38 const auto& output_dim_tensor = context->tensors[tensor_id];
39 if (output_dim_tensor.allocation_type == kTfLiteMmapRo) {
40 // If the output dimensions input is a constant, bake it into the Hexagon
41 // graph as a Const node.
42 auto* const_output_dim_node =
43 graph_builder_->AddConstNodeWithData(tensor_id, output_dim_tensor);
44 AddInput(TensorID(const_output_dim_node->GetID(), 0));
45 } else {
46 AddInput(graph_builder_->GetHexagonTensorId(tensor_id));
47 }
48
49 // Min/max values for input tensor.
50 TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
51
52 // Align corners.
53 const TfLiteResizeNearestNeighborParams* params =
54 reinterpret_cast<const TfLiteResizeNearestNeighborParams*>(builtin_data_);
55 int align_corners = params->align_corners ? 1 : 0;
56 auto* align_corners_const = graph_builder_->AddConstNodeWithData(
57 kScalarShape, reinterpret_cast<char*>(&align_corners),
58 sizeof(align_corners));
59 AddInput(TensorID(align_corners_const->GetID(), 0));
60 int half_pixel_centers = params->half_pixel_centers ? 1 : 0;
61 auto* half_pixel_centers_const = graph_builder_->AddConstNodeWithData(
62 kScalarShape, reinterpret_cast<char*>(&half_pixel_centers),
63 sizeof(half_pixel_centers));
64 AddInput(TensorID(half_pixel_centers_const->GetID(), 0));
65
66 // Hexagon outputs for this node.
67 int output_batch_size, output_height_size, output_width_size,
68 output_depth_size;
69 GetDims(&output_batch_size, &output_height_size, &output_width_size,
70 &output_depth_size, context->tensors[outputs->data[0]].dims);
71 node_output_ = AddOutput(sizeof(uint8_t), 4,
72 {output_batch_size, output_height_size,
73 output_width_size, output_depth_size});
74 AddOutput(sizeof(float), 4, kScalarShape);
75 AddOutput(sizeof(float), 4, kScalarShape);
76
77 return kTfLiteOk;
78 }
79
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)80 TfLiteStatus ResizeNearestNeighborOpBuilder::RegisterOutputs(
81 const TfLiteIntArray* outputs, TfLiteContext* context) {
82 // Should be only 1 output.
83 graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
84 node_output_.second);
85 return kTfLiteOk;
86 }
87
~ResizeNearestNeighborOpBuilder()88 ResizeNearestNeighborOpBuilder::~ResizeNearestNeighborOpBuilder() {}
89
CreateResizeNearestNeighborBuilder(GraphBuilder * graph_builder,int op_type)90 OpBuilder* CreateResizeNearestNeighborBuilder(GraphBuilder* graph_builder,
91 int op_type) {
92 return new ResizeNearestNeighborOpBuilder(graph_builder, op_type);
93 }
94
95 } // namespace hexagon
96 } // namespace delegates
97 } // namespace tflite
98