• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h"
25 
26 #include "src/core/NEON/kernels/NEBatchConcatenateLayerKernel.h"
27 #include "src/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
28 #include "src/core/NEON/kernels/NEHeightConcatenateLayerKernel.h"
29 #include "src/core/NEON/kernels/NEWidthConcatenateLayerKernel.h"
30 
31 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
32 #include "arm_compute/runtime/NEON/NEScheduler.h"
33 
34 #include "arm_compute/core/Error.h"
35 #include "arm_compute/core/ITensor.h"
36 #include "arm_compute/core/TensorInfo.h"
37 #include "arm_compute/core/Types.h"
38 #include "src/core/helpers/AutoConfiguration.h"
39 #include "support/MemorySupport.h"
40 
41 namespace arm_compute
42 {
43 namespace experimental
44 {
NEConcatenation()45 NEConcatenation::NEConcatenation()
46     : _concat_kernels(), _num_inputs(0), _axis(0)
47 {
48 }
49 
configure(const std::vector<const ITensorInfo * > & inputs_vector,ITensorInfo * output,size_t axis)50 void NEConcatenation::configure(const std::vector<const ITensorInfo *> &inputs_vector, ITensorInfo *output, size_t axis)
51 {
52     ARM_COMPUTE_ERROR_ON(output == nullptr);
53 
54     _axis       = axis;
55     _num_inputs = inputs_vector.size();
56 
57     TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
58 
59     // Output auto inizialitation if not yet initialized
60     auto_init_if_empty(*output, output_shape, 1, inputs_vector[0]->data_type());
61     ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector, output, axis));
62 
63     unsigned int offset = 0;
64 
65     for(unsigned int i = 0; i < _num_inputs; ++i)
66     {
67         switch(axis)
68         {
69             case Window::DimX:
70             {
71                 auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>();
72                 kernel->configure(inputs_vector.at(i), offset, output);
73                 _concat_kernels.emplace_back(std::move(kernel));
74                 break;
75             }
76             case Window::DimY:
77             {
78                 auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>();
79                 kernel->configure(inputs_vector.at(i), offset, output);
80                 _concat_kernels.emplace_back(std::move(kernel));
81                 break;
82             }
83             case Window::DimZ:
84             {
85                 auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>();
86                 kernel->configure(inputs_vector.at(i), offset, output);
87                 _concat_kernels.emplace_back(std::move(kernel));
88                 break;
89             }
90             case 3:
91             {
92                 auto kernel = support::cpp14::make_unique<NEBatchConcatenateLayerKernel>();
93                 kernel->configure(inputs_vector.at(i), offset, output);
94                 _concat_kernels.emplace_back(std::move(kernel));
95                 break;
96             }
97             default:
98                 ARM_COMPUTE_ERROR("Axis not supported");
99         }
100         offset += inputs_vector.at(i)->dimension(axis);
101     }
102 }
103 
validate(const std::vector<const ITensorInfo * > & inputs_vector,const ITensorInfo * output,size_t axis)104 Status NEConcatenation::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
105 {
106     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
107     ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
108 
109     unsigned int offset = 0;
110     for(const auto &input : inputs_vector)
111     {
112         ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
113         switch(axis)
114         {
115             case Window::DimX:
116             {
117                 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output));
118                 break;
119             }
120             case Window::DimY:
121             {
122                 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output));
123                 break;
124             }
125             case Window::DimZ:
126             {
127                 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output));
128                 break;
129             }
130             case 3:
131             {
132                 ARM_COMPUTE_RETURN_ON_ERROR(NEBatchConcatenateLayerKernel::validate(input, offset, output));
133                 break;
134             }
135             default:
136                 ARM_COMPUTE_ERROR("Axis not supported");
137         }
138         offset += input->dimension(axis);
139     }
140 
141     if(output->total_size() != 0)
142     {
143         TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
144         ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
145     }
146 
147     return Status{};
148 }
149 
run(ITensorPack & tensors)150 void NEConcatenation::run(ITensorPack &tensors)
151 {
152     if(tensors.empty())
153     {
154         ARM_COMPUTE_ERROR("No inputs provided");
155     }
156 
157     if(static_cast<int>(tensors.size() - 1) != static_cast<int>(_num_inputs))
158     {
159         ARM_COMPUTE_ERROR("Configured with different number of inputs");
160     }
161 
162     int i = 0;
163     for(auto &k : _concat_kernels)
164     {
165         ITensorPack pack;
166         pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i));
167         pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST));
168         NEScheduler::get().schedule_op(k.get(), Window::DimY, pack);
169         ++i;
170     }
171 }
172 } // namespace experimental
173 
174 struct NEConcatenateLayer::Impl
175 {
176     std::vector<const ITensor *>                   srcs{};
177     ITensor                                       *dst{ nullptr };
178     unsigned int                                   num_inputs{ 0 };
179     unsigned int                                   axis{ 0 };
180     std::unique_ptr<experimental::NEConcatenation> op{ nullptr };
181 };
182 
NEConcatenateLayer()183 NEConcatenateLayer::NEConcatenateLayer()
184     : _impl(support::cpp14::make_unique<Impl>())
185 {
186 }
187 
188 NEConcatenateLayer::NEConcatenateLayer(NEConcatenateLayer &&) = default;
189 
190 NEConcatenateLayer &NEConcatenateLayer::operator=(NEConcatenateLayer &&) = default;
191 
192 NEConcatenateLayer::~NEConcatenateLayer() = default;
193 
configure(std::vector<const ITensor * > inputs_vector,ITensor * output,size_t axis)194 void NEConcatenateLayer::configure(std::vector<const ITensor *> inputs_vector, ITensor *output, size_t axis)
195 {
196     ARM_COMPUTE_ERROR_ON(output == nullptr);
197 
198     _impl->srcs       = inputs_vector;
199     _impl->dst        = output;
200     _impl->axis       = axis;
201     _impl->num_inputs = inputs_vector.size();
202     _impl->op         = arm_compute::support::cpp14::make_unique<experimental::NEConcatenation>();
203 
204     std::vector<const ITensorInfo *> inputs_vector_info;
205     for(unsigned int i = 0; i < inputs_vector.size(); ++i)
206     {
207         ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
208         inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
209     }
210     _impl->op->configure(inputs_vector_info, _impl->dst->info(), axis);
211 }
212 
validate(const std::vector<const ITensorInfo * > & inputs_vector,const ITensorInfo * output,size_t axis)213 Status NEConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
214 {
215     return experimental::NEConcatenation::validate(inputs_vector, output, axis);
216 }
217 
run()218 void NEConcatenateLayer::run()
219 {
220     ITensorPack pack;
221     for(unsigned i = 0; i < _impl->num_inputs; ++i)
222     {
223         pack.add_tensor(TensorType::ACL_SRC_VEC + i, _impl->srcs.at(i));
224     }
225     pack.add_tensor(TensorType::ACL_DST, _impl->dst);
226 
227     _impl->op->run(pack);
228 }
229 } // namespace arm_compute
230