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