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/coreml/builders/hardswish_op_builder.h"
16
17 #include "tensorflow/lite/c/builtin_op_data.h"
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/delegates/coreml/builders/add_op_builder.h"
20 #include "tensorflow/lite/delegates/coreml/builders/mul_op_builder.h"
21 #include "tensorflow/lite/delegates/coreml/builders/op_factory.h"
22
23 namespace tflite {
24 namespace delegates {
25 namespace coreml {
DebugName()26 const std::string& HardSwishOpBuilder::DebugName() {
27 if (debug_name_.empty()) SetDebugName("HardSwishOpBuilder", node_id_);
28 return debug_name_;
29 }
30
Build()31 CoreML::Specification::NeuralNetworkLayer* HardSwishOpBuilder::Build() {
32 layer_->set_name(DebugName());
33 layer_->mutable_multiply()->set_alpha(1.0f / 6.0f);
34
35 return layer_.release();
36 }
37
PopulateSubgraph(TfLiteContext * context)38 TfLiteStatus HardSwishOpBuilder::PopulateSubgraph(TfLiteContext* context) {
39 // hswish(x) = (x/6) * ReLU6(x+3). main layer_ contains the first part, x/6.
40 // ReLU6(x +3) constructed as add op with fused ReLU6 activation.
41 AddOpBuilder* add_builder = reinterpret_cast<AddOpBuilder*>(
42 graph_builder_->AddBuilder(CreateAddOpBuilder, nullptr));
43 TfLiteAddParams add_param{kTfLiteActRelu6};
44 add_builder->SetBuiltinData(&add_param);
45 add_builder->SetAlpha(3.0f);
46 add_builder->AddInput(layer_->input(0));
47 add_builder->PopulateSubgraph(context);
48
49 // multiplies (x/6) from main layer_ and ReLU6(x+3) from the above code.
50 MulOpBuilder* mul_builder = reinterpret_cast<MulOpBuilder*>(
51 graph_builder_->AddBuilder(CreateMulOpBuilder, nullptr));
52 mul_builder->AddInput(AddOutput());
53 mul_builder->AddInput(add_builder->GetOutput(context));
54 builder_output_ = mul_builder->AddOutput();
55 return kTfLiteOk;
56 }
57
RegisterInputs(const TfLiteIntArray * inputs,TfLiteContext * context)58 TfLiteStatus HardSwishOpBuilder::RegisterInputs(const TfLiteIntArray* inputs,
59 TfLiteContext* context) {
60 if (inputs->size != 1) {
61 TF_LITE_KERNEL_LOG(context, "Wrong # of inputs to hardswish!.");
62 return kTfLiteError;
63 }
64 AddInput(inputs->data[0]);
65 return kTfLiteOk;
66 }
67
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)68 TfLiteStatus HardSwishOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
69 TfLiteContext* context) {
70 if (outputs->size != 1) {
71 TF_LITE_KERNEL_LOG(context, "Wrong # of outputs to hardswish!.");
72 return kTfLiteError;
73 }
74 TensorID output_tensor = GetOutput(context);
75 if (output_tensor.NodeID() == -1) {
76 TF_LITE_KERNEL_LOG(context, "Failed to build output tensor.");
77 return kTfLiteError;
78 }
79 graph_builder_->AddTensorWithID(outputs->data[0], output_tensor);
80 return kTfLiteOk;
81 }
82
CreateHardSwishOpBuilder(GraphBuilder * graph_builder)83 OpBuilder* CreateHardSwishOpBuilder(GraphBuilder* graph_builder) {
84 return new HardSwishOpBuilder(graph_builder);
85 }
86 } // namespace coreml
87 } // namespace delegates
88 } // namespace tflite
89