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/concatenation_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/op_validator.h"
20
21 namespace tflite {
22 namespace delegates {
23 namespace coreml {
24
Build()25 CoreML::Specification::NeuralNetworkLayer* ConcatenationOpBuilder::Build() {
26 if (layer_ == nullptr) {
27 layer_.reset(new CoreML::Specification::NeuralNetworkLayer);
28 }
29 layer_->set_name(DebugName());
30 layer_->mutable_concat()->set_sequenceconcat(false);
31 return layer_.release();
32 }
33
RegisterInputs(const TfLiteIntArray * inputs,TfLiteContext * context)34 TfLiteStatus ConcatenationOpBuilder::RegisterInputs(
35 const TfLiteIntArray* inputs, TfLiteContext* context) {
36 if (inputs->size < 2) {
37 TF_LITE_KERNEL_LOG(
38 context, "ConcatenationOpBuidler: at least 2 inputs are required.");
39 return kTfLiteError;
40 }
41 for (int i = 0; i < inputs->size; ++i) {
42 AddInput(inputs->data[i]);
43 }
44 return kTfLiteOk;
45 }
46
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)47 TfLiteStatus ConcatenationOpBuilder::RegisterOutputs(
48 const TfLiteIntArray* outputs, TfLiteContext* context) {
49 if (outputs->size != 1) {
50 TF_LITE_KERNEL_LOG(context, "Wrong # of outputs to Concat!.");
51 return kTfLiteError;
52 }
53 graph_builder_->AddTensorWithID(outputs->data[0], GetOutput(context));
54 return kTfLiteOk;
55 }
56
CreateConcatenationOpBuilder(GraphBuilder * graph_builder)57 OpBuilder* CreateConcatenationOpBuilder(GraphBuilder* graph_builder) {
58 return new ConcatenationOpBuilder(graph_builder);
59 }
60
IsConcatenationOpSupported(const TfLiteRegistration * registration,const TfLiteNode * node,TfLiteContext * context)61 bool IsConcatenationOpSupported(const TfLiteRegistration* registration,
62 const TfLiteNode* node,
63 TfLiteContext* context) {
64 if (node->builtin_data == nullptr) return false;
65 auto params =
66 reinterpret_cast<TfLiteConcatenationParams*>(node->builtin_data);
67 int input_dims = context->tensors[node->inputs->data[0]].dims->size;
68
69 // Not supported in TfLite kernel.
70 if (params->activation != kTfLiteActNone) return false;
71 if (node->inputs->size < 2) return false;
72
73 // Only supports concatenation by channel. Core ML concatenation supports
74 // concatenation by channel and by sequence (axis -5) only.
75 // TODO(b/145642128): support stack layer here with Core ML 3 support.
76 if (input_dims == 3) return params->axis == 2 || params->axis == -1;
77 if (input_dims == 4) return params->axis == 3 || params->axis == -1;
78 return false;
79 }
80
81 } // namespace coreml
82 } // namespace delegates
83 } // namespace tflite
84