• 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/slice_builder.h"
16 
17 #include <vector>
18 
19 #include "tensorflow/lite/kernels/internal/tensor.h"
20 
21 namespace tflite {
22 namespace delegates {
23 namespace hexagon {
24 namespace {
25 template <typename T>
GetBeginAndSizeVectors(int dimensions,const TfLiteTensor * begin,const TfLiteTensor * size,std::vector<int> * begins,std::vector<int> * sizes)26 void GetBeginAndSizeVectors(int dimensions, const TfLiteTensor* begin,
27                             const TfLiteTensor* size, std::vector<int>* begins,
28                             std::vector<int>* sizes) {
29   for (int i = 0; i < dimensions; ++i) {
30     begins->push_back(GetTensorData<T>(begin)[i]);
31     sizes->push_back(GetTensorData<T>(size)[i]);
32   }
33 }
34 }  // namespace
35 
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)36 TfLiteStatus SliceOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
37                                               const TfLiteIntArray* outputs,
38                                               TfLiteContext* context) {
39   // Input data tensor.
40   const int tensor_id = inputs->data[0];
41   const auto& input_tensor = context->tensors[tensor_id];
42   AddInput(graph_builder_->GetHexagonTensorId(tensor_id));
43   // Start / Size
44   const auto& begin_tensor = context->tensors[inputs->data[1]];
45   const auto& size_tensor = context->tensors[inputs->data[2]];
46   std::vector<int> begins, sizes;
47   if (begin_tensor.type == kTfLiteInt32) {
48     GetBeginAndSizeVectors<int>(input_tensor.dims->size, &begin_tensor,
49                                 &size_tensor, &begins, &sizes);
50   } else if (begin_tensor.type == kTfLiteInt64) {
51     GetBeginAndSizeVectors<int64_t>(input_tensor.dims->size, &begin_tensor,
52                                     &size_tensor, &begins, &sizes);
53   } else {
54     return kTfLiteError;
55   }
56   const int begins_shape[] = {1, 1, 1, static_cast<int>(begins.size())};
57   auto begins_node = graph_builder_->AddConstNodeWithData(
58       begins_shape, reinterpret_cast<char*>(begins.data()),
59       sizeof(int) * begins.size());
60   auto sizes_node = graph_builder_->AddConstNodeWithData(
61       begins_shape, reinterpret_cast<char*>(sizes.data()),
62       sizeof(int) * begins.size());
63   AddInput(TensorID(begins_node->GetID(), 0));
64   AddInput(TensorID(sizes_node->GetID(), 0));
65 
66   // Input min/max
67   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
68 
69   // Outputs
70   int output_batch_size, output_height_size, output_width_size,
71       output_depth_size;
72   GetDims(&output_batch_size, &output_height_size, &output_width_size,
73           &output_depth_size, context->tensors[outputs->data[0]].dims);
74   node_output_ = AddOutput(sizeof(uint8_t), 4,
75                            {output_batch_size, output_height_size,
76                             output_width_size, output_depth_size});
77   AddOutput(sizeof(float), 4, kScalarShape);
78   AddOutput(sizeof(float), 4, kScalarShape);
79   return kTfLiteOk;
80 }
81 
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)82 TfLiteStatus SliceOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
83                                              TfLiteContext* context) {
84   // Should be only 1 output.
85   graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
86                                   node_output_.second);
87   return kTfLiteOk;
88 }
89 
CreateSliceOpBuilder(GraphBuilder * graph_builder,int op_type)90 OpBuilder* CreateSliceOpBuilder(GraphBuilder* graph_builder, int op_type) {
91   return new SliceOpBuilder(graph_builder, op_type);
92 }
93 }  // namespace hexagon
94 }  // namespace delegates
95 }  // namespace tflite
96