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