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/softmax_op_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 {
DebugName()25 const std::string& SoftmaxOpBuilder::DebugName() {
26 if (debug_name_.empty()) SetDebugName("SoftmaxOpBuilder", node_id_);
27 return debug_name_;
28 }
29
Build()30 CoreML::Specification::NeuralNetworkLayer* SoftmaxOpBuilder::Build() {
31 if (layer_ == nullptr) {
32 layer_ = std::make_unique<CoreML::Specification::NeuralNetworkLayer>();
33 }
34 layer_->set_name(DebugName());
35 layer_->mutable_softmax();
36
37 return layer_.release();
38 }
39
RegisterInputs(const TfLiteIntArray * inputs,TfLiteContext * context)40 TfLiteStatus SoftmaxOpBuilder::RegisterInputs(const TfLiteIntArray* inputs,
41 TfLiteContext* context) {
42 if (inputs->size != 1) {
43 TF_LITE_KERNEL_LOG(context, "Wrong # of inputs to softmax!.");
44 return kTfLiteError;
45 }
46 AddInput(inputs->data[0]);
47 return kTfLiteOk;
48 }
49
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)50 TfLiteStatus SoftmaxOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
51 TfLiteContext* context) {
52 if (outputs->size != 1) {
53 TF_LITE_KERNEL_LOG(context, "Wrong # of outputs to softmax!.");
54 return kTfLiteError;
55 }
56 TensorID output_tensor = GetOutput(context);
57 if (output_tensor.NodeID() == -1) {
58 TF_LITE_KERNEL_LOG(context, "Failed to build output tensor.");
59 return kTfLiteError;
60 }
61 graph_builder_->AddTensorWithID(outputs->data[0], output_tensor);
62 return kTfLiteOk;
63 }
64
CreateSoftmaxOpBuilder(GraphBuilder * graph_builder)65 OpBuilder* CreateSoftmaxOpBuilder(GraphBuilder* graph_builder) {
66 return new SoftmaxOpBuilder(graph_builder);
67 }
68 } // namespace coreml
69 } // namespace delegates
70 } // namespace tflite
71