1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13 ==============================================================================*/
14 #include "tensorflow/lite/delegates/hexagon/builders/hardswish_builder.h"
15
16 #include <stdint.h>
17
18 #include <limits>
19
20 #include "tensorflow/lite/c/builtin_op_data.h"
21 #include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn.h"
22 #include "tensorflow/lite/kernels/kernel_util.h"
23
24 namespace tflite {
25 namespace delegates {
26 namespace hexagon {
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)27 TfLiteStatus HardSwishOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
28 const TfLiteIntArray* outputs,
29 TfLiteContext* context) {
30 // input data tensor.
31 int tensor_id = inputs->data[0];
32 const auto& input1_tensor = context->tensors[tensor_id];
33 AddInput(graph_builder_->GetHexagonTensorId(tensor_id));
34 TF_LITE_ENSURE_STATUS(ComputeAndAddMinAndMax(context, input1_tensor));
35
36 // Output min/max
37 TF_LITE_ENSURE_STATUS(
38 ComputeAndAddMinAndMax(context, context->tensors[outputs->data[0]]));
39
40 int output_batch_size, output_height_size, output_width_size,
41 output_depth_size;
42 GetDims(&output_batch_size, &output_height_size, &output_width_size,
43 &output_depth_size, context->tensors[outputs->data[0]].dims);
44
45 node_output_ = AddOutput(sizeof(uint8_t), 4,
46 {output_batch_size, output_height_size,
47 output_width_size, output_depth_size});
48 AddOutput(sizeof(float), 4, kScalarShape);
49 AddOutput(sizeof(float), 4, kScalarShape);
50
51 return kTfLiteOk;
52 }
53
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)54 TfLiteStatus HardSwishOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
55 TfLiteContext* context) {
56 // Should be only 1 output.
57 graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
58 node_output_.second);
59 return kTfLiteOk;
60 }
61
~HardSwishOpBuilder()62 HardSwishOpBuilder::~HardSwishOpBuilder() {}
63
CreateHardSwishBuilder(GraphBuilder * graph_builder,int op_type)64 OpBuilder* CreateHardSwishBuilder(GraphBuilder* graph_builder, int op_type) {
65 return new HardSwishOpBuilder(graph_builder, op_type);
66 }
67
68 } // namespace hexagon
69 } // namespace delegates
70 } // namespace tflite
71