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/mul_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/delegates/coreml/builders/op_validator.h"
25 #include "tensorflow/lite/delegates/coreml/builders/util.h"
26 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
27 #include "tensorflow/lite/kernels/internal/types.h"
28 #include "tensorflow/lite/kernels/kernel_util.h"
29
30 namespace tflite {
31 namespace delegates {
32 namespace coreml {
DebugName()33 const std::string& MulOpBuilder::DebugName() {
34 if (debug_name_.empty()) SetDebugName("MulOpBuilder", node_id_);
35 return debug_name_;
36 }
37
Build()38 CoreML::Specification::NeuralNetworkLayer* MulOpBuilder::Build() {
39 if (layer_ == nullptr) {
40 layer_ = std::make_unique<CoreML::Specification::NeuralNetworkLayer>();
41 }
42 // MultiplyLayerParams only has limited broadcasting support. For example:
43 // [B, 1, 1, 1], [B, C, 1, 1], [B, 1, H, W], [B, C, H, W]. other shapes
44 // will make broadcasting fail.
45 layer_->set_name(DebugName());
46 layer_->mutable_multiply();
47 if (alpha_ != 1.0f) {
48 layer_->mutable_multiply()->set_alpha(alpha_);
49 }
50
51 return layer_.release();
52 }
53
PopulateSubgraph(TfLiteContext * context)54 TfLiteStatus MulOpBuilder::PopulateSubgraph(TfLiteContext* context) {
55 TfLiteMulParams* params = reinterpret_cast<TfLiteMulParams*>(builtin_data_);
56
57 TfLiteFusedActivation activation = params->activation;
58 if (activation == kTfLiteActNone) {
59 builder_output_ = AddOutput();
60 } else {
61 ActivationLayerBuilder* activation_builder =
62 reinterpret_cast<ActivationLayerBuilder*>(
63 graph_builder_->AddBuilder(CreateActivationLayerBuilder, nullptr));
64 activation_builder->SetActivation(activation);
65 activation_builder->AddInput(AddOutput());
66 activation_builder->PopulateSubgraph(context);
67 builder_output_ = activation_builder->GetOutput(context);
68 }
69 return kTfLiteOk;
70 }
71
RegisterInputs(const TfLiteIntArray * inputs,TfLiteContext * context)72 TfLiteStatus MulOpBuilder::RegisterInputs(const TfLiteIntArray* inputs,
73 TfLiteContext* context) {
74 // TFL MUL op always has 2 inputs.
75 if (inputs->size != 2) {
76 TF_LITE_KERNEL_LOG(context, "Wrong # of inputs to mul!.");
77 return kTfLiteError;
78 }
79 const auto* input_0 = &context->tensors[inputs->data[0]];
80 const auto* input_1 = &context->tensors[inputs->data[1]];
81 // store constant, scalar value into MultiplyLayerParams directly.
82 if (IsConstantTensor(input_0) && NumElements(input_0) == 1) {
83 AddInput(inputs->data[1]);
84 SetAlpha(GetTensorData<float>(input_0)[0]);
85 } else if (IsConstantTensor(input_1) && NumElements(input_1) == 1) {
86 AddInput(inputs->data[0]);
87 SetAlpha(GetTensorData<float>(input_1)[0]);
88 } else {
89 AddInput(inputs->data[0]);
90 AddInput(inputs->data[1]);
91 }
92 return kTfLiteOk;
93 }
94
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)95 TfLiteStatus MulOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
96 TfLiteContext* context) {
97 if (outputs->size != 1) {
98 TF_LITE_KERNEL_LOG(context, "Wrong # of outputs to mul!.");
99 return kTfLiteError;
100 }
101 TensorID output_tensor = GetOutput(context);
102 if (output_tensor.NodeID() == -1) {
103 TF_LITE_KERNEL_LOG(context, "Failed to build output tensor.");
104 return kTfLiteError;
105 }
106 graph_builder_->AddTensorWithID(outputs->data[0], output_tensor);
107 return kTfLiteOk;
108 }
109
SetAlpha(float alpha)110 void MulOpBuilder::SetAlpha(float alpha) { alpha_ = alpha; }
111
CreateMulOpBuilder(GraphBuilder * graph_builder)112 OpBuilder* CreateMulOpBuilder(GraphBuilder* graph_builder) {
113 return new MulOpBuilder(graph_builder);
114 }
115
116 } // namespace coreml
117 } // namespace delegates
118 } // namespace tflite
119