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/arg_min_max_builder.h"
16
17 #include <limits>
18
19 #include "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/util.h"
21
22 namespace tflite {
23 namespace delegates {
24 namespace hexagon {
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)25 TfLiteStatus ArgMinMaxOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
26 const TfLiteIntArray* outputs,
27 TfLiteContext* context) {
28 if (inputs->size != 2) {
29 context->ReportError(context, "Expecting 2 inputs %d != 2\n", inputs->size);
30 return kTfLiteError;
31 }
32
33 // Input data tensor.
34 int input_tensor_id = inputs->data[0];
35 const auto& input_tensor = context->tensors[input_tensor_id];
36 AddInput(graph_builder_->GetHexagonTensorId(input_tensor_id));
37
38 // Axis tensor.
39 const int axis_tensor_id = inputs->data[1];
40 const auto& axis = context->tensors[axis_tensor_id];
41 if (axis.allocation_type != kTfLiteMmapRo) {
42 context->ReportError(context,
43 "Axis tensor doesn't have correct allocation type: %s",
44 axis.name);
45 return kTfLiteError;
46 }
47
48 int axis_value = axis.data.i32[0];
49 if (axis_value < 0) {
50 axis_value += input_tensor.dims->size;
51 }
52 auto* input_axis_const = graph_builder_->AddConstNodeWithData(
53 kScalarShape, reinterpret_cast<char*>(&axis_value), sizeof(int));
54 AddInput(TensorID(input_axis_const->GetID(), 0));
55
56 // Compute Min/Max
57 TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input_tensor));
58
59 // Output Node
60 int output_batch_size, output_height_size, output_width_size,
61 output_depth_size;
62 size_t output_element_size = 0;
63 TF_LITE_ENSURE_STATUS(GetSizeOfType(
64 context, context->tensors[outputs->data[0]].type, &output_element_size));
65 GetDims(&output_batch_size, &output_height_size, &output_width_size,
66 &output_depth_size, context->tensors[outputs->data[0]].dims);
67 node_output_ = AddOutput(output_element_size, 4,
68 {output_batch_size, output_height_size,
69 output_width_size, output_depth_size});
70
71 return kTfLiteOk;
72 }
73
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)74 TfLiteStatus ArgMinMaxOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
75 TfLiteContext* context) {
76 // Should be only 1 output.
77 graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
78 node_output_.second);
79 return kTfLiteOk;
80 }
81
~ArgMinMaxOpBuilder()82 ArgMinMaxOpBuilder::~ArgMinMaxOpBuilder() {}
83
CreateArgMinMaxOpBuilder(GraphBuilder * graph_builder,int op_type)84 OpBuilder* CreateArgMinMaxOpBuilder(GraphBuilder* graph_builder, int op_type) {
85 return new ArgMinMaxOpBuilder(graph_builder, op_type);
86 }
87
88 } // namespace hexagon
89 } // namespace delegates
90 } // namespace tflite
91