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/mirror_pad_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/c/common.h"
23 #include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn.h"
24 #include "tensorflow/lite/kernels/kernel_util.h"
25
26 namespace tflite {
27 namespace delegates {
28 namespace hexagon {
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)29 TfLiteStatus MirrorPadOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
30 const TfLiteIntArray* outputs,
31 TfLiteContext* context) {
32 // Input data tensor.
33 int tensor_id = inputs->data[0];
34 const auto& input_tensor = context->tensors[tensor_id];
35 AddInput(graph_builder_->GetHexagonTensorId(tensor_id));
36
37 // Padding tensor.
38 // Should be a constant.
39 tensor_id = inputs->data[1];
40 const auto& padding_tensor = context->tensors[tensor_id];
41 if (padding_tensor.dims->size != 2 || padding_tensor.dims->data[0] > 4 ||
42 padding_tensor.dims->data[1] != 2) {
43 TF_LITE_KERNEL_LOG(context, "Invalid padding tensor shape");
44 return kTfLiteError;
45 }
46 paddings_shape_ = {1, 1, 4, 2};
47 std::vector<int> padding_data(8, 0);
48 // Hexagon always expects padding data for each dimension in order {b, h, w,
49 // d}. This start value ensures we pad the non-relevant dimensions with 0.
50 int padding_data_start = 8 - padding_tensor.dims->data[0] * 2;
51 for (int i = 0; i < padding_tensor.dims->data[0] * 2; ++i) {
52 padding_data[padding_data_start + i] = padding_tensor.data.i32[i];
53 }
54 auto* const_padding_node = graph_builder_->AddConstNodeWithData(
55 paddings_shape_.data(), reinterpret_cast<char*>(padding_data.data()),
56 padding_data.size() * sizeof(padding_data[0]));
57 AddInput(TensorID(const_padding_node->GetID(), 0));
58 // Padding type.
59 const TfLiteMirrorPaddingParams* params =
60 reinterpret_cast<const TfLiteMirrorPaddingParams*>(builtin_data_);
61 if (params->mode == kTfLiteMirrorPaddingReflect) {
62 SetPaddingType(NN_PAD_MIRROR_REFLECT);
63 } else if (params->mode == kTfLiteMirrorPaddingSymmetric) {
64 SetPaddingType(NN_PAD_MIRROR_SYMMETRIC);
65 }
66
67 // Min/max values for input tensor.
68 TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
69
70 // Hexagon outputs for this node.
71 int output_batch_size, output_height_size, output_width_size,
72 output_depth_size;
73 GetDims(&output_batch_size, &output_height_size, &output_width_size,
74 &output_depth_size, context->tensors[outputs->data[0]].dims);
75 node_output_ = AddOutput(sizeof(uint8_t), 4,
76 {output_batch_size, output_height_size,
77 output_width_size, output_depth_size});
78 AddOutput(sizeof(float), 4, kScalarShape);
79 AddOutput(sizeof(float), 4, kScalarShape);
80
81 return kTfLiteOk;
82 }
83
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)84 TfLiteStatus MirrorPadOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
85 TfLiteContext* context) {
86 // Should be only 1 output.
87 graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
88 node_output_.second);
89 return kTfLiteOk;
90 }
91
~MirrorPadOpBuilder()92 MirrorPadOpBuilder::~MirrorPadOpBuilder() {}
93
CreateMirrorPadBuilder(GraphBuilder * graph_builder,int op_type)94 OpBuilder* CreateMirrorPadBuilder(GraphBuilder* graph_builder, int op_type) {
95 return new MirrorPadOpBuilder(graph_builder, op_type);
96 }
97
98 } // namespace hexagon
99 } // namespace delegates
100 } // namespace tflite
101