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