xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/hexagon/builders/min_max_builder.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/min_max_builder.h"
16 
17 #include "tensorflow/lite/c/common.h"
18 
19 namespace tflite {
20 namespace delegates {
21 namespace hexagon {
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)22 TfLiteStatus MinMaxOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
23                                                const TfLiteIntArray* outputs,
24                                                TfLiteContext* context) {
25   // Input tensors a and b.
26   int a_tensor_id = inputs->data[0];
27   int b_tensor_id = inputs->data[1];
28   const auto& a_tensor = context->tensors[a_tensor_id];
29   const auto& b_tensor = context->tensors[b_tensor_id];
30   AddInput(graph_builder_->GetHexagonTensorId(a_tensor_id));
31   AddInput(graph_builder_->GetHexagonTensorId(b_tensor_id));
32 
33   // Add Inputs A & B min/max
34   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, a_tensor));
35   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, b_tensor));
36 
37   // Add output min/max
38   const int output_tensor_id = outputs->data[0];
39   const auto& output_tensor = context->tensors[output_tensor_id];
40   TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, output_tensor));
41 
42   // Add outputs.
43   int output_batch_size, output_height_size, output_width_size,
44       output_depth_size;
45   GetDims(&output_batch_size, &output_height_size, &output_width_size,
46           &output_depth_size, context->tensors[outputs->data[0]].dims);
47   node_output_ = AddOutput(sizeof(uint8_t), 4,
48                            {output_batch_size, output_height_size,
49                             output_width_size, output_depth_size});
50   AddOutput(sizeof(float), 4, kScalarShape);
51   AddOutput(sizeof(float), 4, kScalarShape);
52 
53   return kTfLiteOk;
54 }
55 
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)56 TfLiteStatus MinMaxOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
57                                               TfLiteContext* context) {
58   // Should be only 1 output.
59   graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
60                                   node_output_.second);
61 
62   return kTfLiteOk;
63 }
64 
CreateMinMaxBuilder(GraphBuilder * graph_builder,int op_type)65 OpBuilder* CreateMinMaxBuilder(GraphBuilder* graph_builder, int op_type) {
66   return new MinMaxOpBuilder(graph_builder, op_type);
67 }
68 
69 }  // namespace hexagon
70 }  // namespace delegates
71 }  // namespace tflite
72