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/split_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 SplitOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
29 const TfLiteIntArray* outputs,
30 TfLiteContext* context) {
31 const int input_tensor_id = inputs->data[1];
32 const auto& input_tensor = context->tensors[input_tensor_id];
33
34 // Axis tensor.
35 const int axis_tensor_id = inputs->data[0];
36 const auto& axis = context->tensors[axis_tensor_id];
37 if (axis.allocation_type != kTfLiteMmapRo) {
38 TF_LITE_KERNEL_LOG(context,
39 "Axis tensor doesn't have correct allocation type: %s",
40 axis.name);
41 return kTfLiteError;
42 }
43 int axis_value = axis.data.i32[0];
44 if (axis_value < 0) axis_value += input_tensor.dims->size;
45 // We pad Hexagon tensor dimensions with 1 if dims.size < 4.
46 // (4 - input_tensor.dims->size) helps maps the input axis value in such
47 // cases.
48 axis_value += (4 - input_tensor.dims->size);
49 auto* input_axis_const = graph_builder_->AddConstNodeWithData(
50 kScalarShape, reinterpret_cast<char*>(&axis_value), sizeof(int));
51 AddInput(TensorID(input_axis_const->GetID(), 0));
52
53 // Input data tensor & min/max.
54 AddInput(graph_builder_->GetHexagonTensorId(input_tensor_id));
55 TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
56
57 // Output data tensors.
58 for (int i = 0; i < outputs->size; ++i) {
59 int output_batch_size, output_height_size, output_width_size,
60 output_depth_size;
61 GetDims(&output_batch_size, &output_height_size, &output_width_size,
62 &output_depth_size, context->tensors[outputs->data[i]].dims);
63 TensorID output = AddOutput(sizeof(uint8_t), 4,
64 {output_batch_size, output_height_size,
65 output_width_size, output_depth_size});
66 node_outputs_.push_back(output);
67 }
68 // For Hexagon output min/max.
69 AddOutput(sizeof(float), 4, kScalarShape);
70 AddOutput(sizeof(float), 4, kScalarShape);
71
72 return kTfLiteOk;
73 }
74
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)75 TfLiteStatus SplitOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
76 TfLiteContext* context) {
77 for (int i = 0; i < node_outputs_.size(); ++i) {
78 graph_builder_->AddTensorWithID(outputs->data[i], node_outputs_[i].first,
79 node_outputs_[i].second);
80 }
81 return kTfLiteOk;
82 }
83
~SplitOpBuilder()84 SplitOpBuilder::~SplitOpBuilder() {}
85
CreateSplitBuilder(GraphBuilder * graph_builder,int op_type)86 OpBuilder* CreateSplitBuilder(GraphBuilder* graph_builder, int op_type) {
87 return new SplitOpBuilder(graph_builder, op_type);
88 }
89
90 } // namespace hexagon
91 } // namespace delegates
92 } // namespace tflite
93