Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Split.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef LUCI_INTERPRETER_KERNELS_SPLIT_IMPL_H
18 #define LUCI_INTERPRETER_KERNELS_SPLIT_IMPL_H
19
20 #include "Builders.h"
21 #include "Utils.h"
22
23 namespace luci_interpreter
24 {
25
26 template <typename T>
27 void splitImpl(const circle::Operator *cur_op, const circle::Tensor *input, int axis_value,
28                BaseRuntimeGraph *runtime_graph)
29 {
30   const int output_count = cur_op->outputs()->size();
31
32   const auto output0_index = cur_op->outputs()->operator[](0);
33   assert(output0_index != -1);
34
35   const auto output0 = runtime_graph->getCircleTensorByIndex(output0_index);
36   assert(output0 != nullptr);
37
38   const int split_dimensions = Tensor::num_dims(input);
39
40   assert(axis_value < split_dimensions);
41   assert(Tensor::num_dims(output0) == split_dimensions);
42
43   int64_t outer_size = 1;
44   for (int i = 0; i < axis_value; ++i)
45   {
46     outer_size *= Tensor::dim(input, i);
47   }
48
49   int64_t base_inner_size = 1;
50   for (int i = axis_value + 1; i < split_dimensions; ++i)
51   {
52     base_inner_size *= Tensor::dim(input, i);
53   }
54
55   const T *input_ptr = kernels::getTensorData<T>(runtime_graph->getDataByTensor(input));
56   assert(input_ptr != nullptr);
57   for (int k = 0; k < outer_size; ++k)
58   {
59     for (int i = 0; i < output_count; ++i)
60     {
61       const auto output_index = cur_op->outputs()->operator[](i);
62       assert(output_index != -1);
63
64       const auto output = runtime_graph->getCircleTensorByIndex(output_index);
65       assert(output != nullptr);
66
67       T *output_data = kernels::getTensorData<T>(runtime_graph->getDataByTensor(output));
68       assert(output_data != nullptr);
69       const int copy_size = Tensor::dim(output, axis_value) * base_inner_size;
70       T *output_ptr = output_data + k * copy_size;
71       assert(output_ptr != nullptr);
72       for (int j = 0; j < copy_size; ++j)
73         output_ptr[j] = input_ptr[j];
74       input_ptr += copy_size;
75     }
76   }
77 }
78
79 } // namespace luci_interpreter
80
81 #endif // LUCI_INTERPRETER_KERNELS_SPLIT_IMPL_H