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