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/threshold_layer_builder.h"
16
17 #include <memory>
18 #include <string>
19
20 #include "tensorflow/lite/c/common.h"
21
22 namespace tflite {
23 namespace delegates {
24 namespace coreml {
25
DebugName()26 const std::string& ThresholdLayerBuilder::DebugName() {
27 if (debug_name_.empty()) SetDebugName("ThresholdLayerBuilder", node_id_);
28 return debug_name_;
29 }
30
Build()31 CoreML::Specification::NeuralNetworkLayer* ThresholdLayerBuilder::Build() {
32 if (layer_ == nullptr) {
33 layer_ = std::make_unique<CoreML::Specification::NeuralNetworkLayer>();
34 }
35 layer_->set_name(DebugName());
36 layer_->mutable_unary()->set_alpha(alpha_);
37 layer_->mutable_unary()->set_scale(scale_);
38 layer_->mutable_unary()->set_type(
39 CoreML::Specification::UnaryFunctionLayerParams::THRESHOLD);
40 return layer_.release();
41 }
42
RegisterInputs(const TfLiteIntArray * inputs,TfLiteContext * context)43 TfLiteStatus ThresholdLayerBuilder::RegisterInputs(const TfLiteIntArray* inputs,
44 TfLiteContext* context) {
45 if (inputs->size != 1) {
46 TF_LITE_KERNEL_LOG(context, "Threshold: Wrong # of inputs!.");
47 return kTfLiteError;
48 }
49 AddInput(inputs->data[0]);
50 return kTfLiteOk;
51 }
52
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)53 TfLiteStatus ThresholdLayerBuilder::RegisterOutputs(
54 const TfLiteIntArray* outputs, TfLiteContext* context) {
55 if (outputs->size != 1) {
56 TF_LITE_KERNEL_LOG(context, "Threshold: Wrong # of outputs!.");
57 return kTfLiteError;
58 }
59 graph_builder_->AddTensorWithID(outputs->data[0], GetOutput(context));
60 return kTfLiteOk;
61 }
62
CreateThresholdLayerBuilder(GraphBuilder * graph_builder)63 OpBuilder* CreateThresholdLayerBuilder(GraphBuilder* graph_builder) {
64 return new ThresholdLayerBuilder(graph_builder);
65 }
66
67 } // namespace coreml
68 } // namespace delegates
69 } // namespace tflite
70