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/coreml/builders/add_op_builder.h"
16
17 #include <memory>
18 #include <string>
19
20 #include "tensorflow/lite/c/builtin_op_data.h"
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/delegates/coreml/builders/activation_layer_builder.h"
23 #include "tensorflow/lite/delegates/coreml/builders/op_factory.h"
24 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
25 #include "tensorflow/lite/kernels/kernel_util.h"
26
27 namespace tflite {
28 namespace delegates {
29 namespace coreml {
DebugName()30 const std::string& AddOpBuilder::DebugName() {
31 if (debug_name_.empty()) SetDebugName("AddOpBuilder", node_id_);
32 return debug_name_;
33 }
34
Build()35 CoreML::Specification::NeuralNetworkLayer* AddOpBuilder::Build() {
36 if (layer_ == nullptr) {
37 layer_ = std::make_unique<CoreML::Specification::NeuralNetworkLayer>();
38 }
39 layer_->set_name(DebugName());
40 layer_->mutable_add();
41 if (alpha_ != 0.0f) {
42 layer_->mutable_add()->set_alpha(alpha_);
43 }
44
45 return layer_.release();
46 }
47
PopulateSubgraph(TfLiteContext * context)48 TfLiteStatus AddOpBuilder::PopulateSubgraph(TfLiteContext* context) {
49 TfLiteAddParams* params = reinterpret_cast<TfLiteAddParams*>(builtin_data_);
50
51 TfLiteFusedActivation activation = params->activation;
52 if (activation == kTfLiteActNone) {
53 builder_output_ = AddOutput();
54 } else {
55 ActivationLayerBuilder* activation_builder =
56 reinterpret_cast<ActivationLayerBuilder*>(
57 graph_builder_->AddBuilder(CreateActivationLayerBuilder, nullptr));
58 activation_builder->SetActivation(activation);
59 activation_builder->AddInput(AddOutput());
60 activation_builder->PopulateSubgraph(context);
61 builder_output_ = activation_builder->GetOutput(context);
62 }
63 return kTfLiteOk;
64 }
65
RegisterInputs(const TfLiteIntArray * inputs,TfLiteContext * context)66 TfLiteStatus AddOpBuilder::RegisterInputs(const TfLiteIntArray* inputs,
67 TfLiteContext* context) {
68 // TODO(taeheej): support 1 input case if necessary. TFL add needs 2 inputs.
69 if (inputs->size != 2) {
70 TF_LITE_KERNEL_LOG(context, "Wrong # of inputs to add!.");
71 return kTfLiteError;
72 }
73 const auto* input_0 = &context->tensors[inputs->data[0]];
74 const auto* input_1 = &context->tensors[inputs->data[1]];
75 // store constant, scalar value into MultiplyLayerParams directly.
76 if (IsConstantTensor(input_0) && NumElements(input_0) == 1) {
77 AddInput(inputs->data[1]);
78 SetAlpha(GetTensorData<float>(input_0)[0]);
79 } else if (IsConstantTensor(input_1) && NumElements(input_1) == 1) {
80 AddInput(inputs->data[0]);
81 SetAlpha(GetTensorData<float>(input_1)[0]);
82 } else {
83 AddInput(inputs->data[0]);
84 AddInput(inputs->data[1]);
85 }
86 return kTfLiteOk;
87 }
88
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)89 TfLiteStatus AddOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
90 TfLiteContext* context) {
91 if (outputs->size != 1) {
92 TF_LITE_KERNEL_LOG(context, "Wrong # of outputs to add!.");
93 return kTfLiteError;
94 }
95 TensorID output_tensor = GetOutput(context);
96 if (output_tensor.NodeID() == -1) {
97 TF_LITE_KERNEL_LOG(context, "Failed to build output tensor.");
98 return kTfLiteError;
99 }
100 graph_builder_->AddTensorWithID(outputs->data[0], output_tensor);
101 return kTfLiteOk;
102 }
103
SetAlpha(float alpha)104 void AddOpBuilder::SetAlpha(float alpha) { alpha_ = alpha; }
105
CreateAddOpBuilder(GraphBuilder * graph_builder)106 OpBuilder* CreateAddOpBuilder(GraphBuilder* graph_builder) {
107 return new AddOpBuilder(graph_builder);
108 }
109 } // namespace coreml
110 } // namespace delegates
111 } // namespace tflite
112