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/strided_slice_builder.h"
16
17 #include <vector>
18
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/kernels/internal/tensor.h"
21
22 namespace tflite {
23 namespace delegates {
24 namespace hexagon {
25 namespace {} // namespace
26
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)27 TfLiteStatus StridedSliceOpBuilder::PopulateSubGraph(
28 const TfLiteIntArray* inputs, const TfLiteIntArray* outputs,
29 TfLiteContext* context) {
30 // Input data tensor.
31 const auto& input_tensor = context->tensors[inputs->data[0]];
32 AddInput(graph_builder_->GetHexagonTensorId(inputs->data[0]));
33 // Begin/End/Step.
34 const auto& begin_tensor = context->tensors[inputs->data[1]];
35 const auto& end_tensor = context->tensors[inputs->data[2]];
36 const auto& step_tensor = context->tensors[inputs->data[3]];
37 auto begins_node =
38 graph_builder_->AddConstNodeWithData(inputs->data[1], begin_tensor);
39 auto ends_node =
40 graph_builder_->AddConstNodeWithData(inputs->data[2], end_tensor);
41 auto steps_node =
42 graph_builder_->AddConstNodeWithData(inputs->data[3], step_tensor);
43 AddInput(TensorID(begins_node->GetID(), 0));
44 AddInput(TensorID(ends_node->GetID(), 0));
45 AddInput(TensorID(steps_node->GetID(), 0));
46 // Begin/End/Shrink-Axis masks.
47 // Hexagon's op always expects bits at 0, 1, 2 & 3 to correspond to BHWD.
48 // So we have to left-shift the mask by (4 - begins.size()).
49 const TfLiteStridedSliceParams* params =
50 reinterpret_cast<const TfLiteStridedSliceParams*>(builtin_data_);
51 int begin_mask = params->begin_mask;
52 int end_mask = params->end_mask;
53 int shrink_axis_mask = params->shrink_axis_mask;
54 int original_mask_size = input_tensor.dims->size;
55 begin_mask = begin_mask << (4 - original_mask_size);
56 end_mask = end_mask << (4 - original_mask_size);
57 shrink_axis_mask = shrink_axis_mask << (4 - original_mask_size);
58 auto* begin_mask_const = graph_builder_->AddConstNodeWithData(
59 kScalarShape, reinterpret_cast<char*>(&begin_mask), sizeof(begin_mask));
60 AddInput(TensorID(begin_mask_const->GetID(), 0));
61 auto* end_mask_const = graph_builder_->AddConstNodeWithData(
62 kScalarShape, reinterpret_cast<char*>(&end_mask), sizeof(end_mask));
63 AddInput(TensorID(end_mask_const->GetID(), 0));
64 auto* shrink_axis_mask_const = graph_builder_->AddConstNodeWithData(
65 kScalarShape, reinterpret_cast<char*>(&shrink_axis_mask),
66 sizeof(shrink_axis_mask));
67 AddInput(TensorID(shrink_axis_mask_const->GetID(), 0));
68
69 // Input min/max
70 TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
71
72 // Slice outputs.
73 int output_batch_size, output_height_size, output_width_size,
74 output_depth_size;
75 GetDims(&output_batch_size, &output_height_size, &output_width_size,
76 &output_depth_size, context->tensors[outputs->data[0]].dims);
77 node_output_ = AddOutput(sizeof(uint8_t), 4,
78 {output_batch_size, output_height_size,
79 output_width_size, output_depth_size});
80 AddOutput(sizeof(float), 4, kScalarShape);
81 AddOutput(sizeof(float), 4, kScalarShape);
82
83 return kTfLiteOk;
84 }
85
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)86 TfLiteStatus StridedSliceOpBuilder::RegisterOutputs(
87 const TfLiteIntArray* outputs, TfLiteContext* context) {
88 // Should be only 1 output.
89 graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
90 node_output_.second);
91 return kTfLiteOk;
92 }
93
CreateStridedSliceBuilder(GraphBuilder * graph_builder,int op_type)94 OpBuilder* CreateStridedSliceBuilder(GraphBuilder* graph_builder, int op_type) {
95 return new StridedSliceOpBuilder(graph_builder, op_type);
96 }
97 } // namespace hexagon
98 } // namespace delegates
99 } // namespace tflite
100