Compute Library
 21.02
CpuConcatenate.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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  */
25 
30 
33 
34 #include "arm_compute/core/Error.h"
37 #include "arm_compute/core/Types.h"
40 
41 namespace arm_compute
42 {
43 namespace cpu
44 {
46  : _concat_kernels(), _num_srcs(0), _axis(0)
47 {
48 }
49 
50 void CpuConcatenate::configure(const std::vector<const ITensorInfo *> &srcs_vector, ITensorInfo *dst, size_t axis)
51 {
52  ARM_COMPUTE_ERROR_ON(dst == nullptr);
53 
54  _axis = axis;
55  _num_srcs = srcs_vector.size();
56 
58 
59  // Output auto inizialitation if not yet initialized
60  auto_init_if_empty(*dst, dst_shape, 1, srcs_vector[0]->data_type());
62 
63  unsigned int offset = 0;
64 
65  for(unsigned int i = 0; i < _num_srcs; ++i)
66  {
67  switch(axis)
68  {
69  case Window::DimX:
70  {
71  auto kernel = std::make_unique<kernels::CpuConcatenateWidthKernel>();
72  kernel->configure(srcs_vector.at(i), offset, dst);
73  _concat_kernels.emplace_back(std::move(kernel));
74  break;
75  }
76  case Window::DimY:
77  {
78  auto kernel = std::make_unique<kernels::CpuConcatenateHeightKernel>();
79  kernel->configure(srcs_vector.at(i), offset, dst);
80  _concat_kernels.emplace_back(std::move(kernel));
81  break;
82  }
83  case Window::DimZ:
84  {
85  auto kernel = std::make_unique<kernels::CpuConcatenateDepthKernel>();
86  kernel->configure(srcs_vector.at(i), offset, dst);
87  _concat_kernels.emplace_back(std::move(kernel));
88  break;
89  }
90  case 3:
91  {
92  auto kernel = std::make_unique<kernels::CpuConcatenateBatchKernel>();
93  kernel->configure(srcs_vector.at(i), offset, dst);
94  _concat_kernels.emplace_back(std::move(kernel));
95  break;
96  }
97  default:
98  ARM_COMPUTE_ERROR("Axis not supported");
99  }
100  offset += srcs_vector.at(i)->dimension(axis);
101  }
102 }
103 
104 Status CpuConcatenate::validate(const std::vector<const ITensorInfo *> &srcs_vector, const ITensorInfo *dst, size_t axis)
105 {
107  ARM_COMPUTE_RETURN_ERROR_ON(srcs_vector.size() < 2);
108 
109  unsigned int offset = 0;
110  for(const auto &src : srcs_vector)
111  {
113  switch(axis)
114  {
115  case Window::DimX:
116  {
118  break;
119  }
120  case Window::DimY:
121  {
123  break;
124  }
125  case Window::DimZ:
126  {
128  break;
129  }
130  case 3:
131  {
133  break;
134  }
135  default:
136  ARM_COMPUTE_ERROR("Axis not supported");
137  }
138  offset += src->dimension(axis);
139  }
140 
141  if(dst->total_size() != 0)
142  {
145  }
146 
147  return Status{};
148 }
149 
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_srcs))
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;
168  NEScheduler::get().schedule_op(k.get(), Window::DimY, k->window(), pack);
169  ++i;
170  }
171 }
172 } // namespace cpu
173 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
void configure(const std::vector< const ITensorInfo *> &srcs_vector, ITensorInfo *dst, size_t axis)
Configure operator for a given list of arguments.
Shape of a tensor.
Definition: TensorShape.h:39
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:61
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual void schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)=0
Runs the kernel in the same thread as the caller synchronously.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
const DataType data_type
Definition: Im2Col.cpp:150
static Status validate(const ITensorInfo *src, unsigned int width_offset, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuConcatenateWidthKerne...
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:40
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:172
static Status validate(const std::vector< const ITensorInfo *> &srcs_vector, const ITensorInfo *dst, size_t axis)
Static function to check if given info will lead to a valid configuration of NEConcatenateLayer.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
size_t size() const
Pack size accessor.
Definition: ITensorPack.cpp:56
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:50
static Status validate(const ITensorInfo *src, unsigned int height_offset, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuConcatenateHeightKern...
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
static Status validate(const ITensorInfo *src, unsigned int batch_offset, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuConcatenateBatchKerne...
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Tensor packing service.
Definition: ITensorPack.h:37
static Status validate(const ITensorInfo *src, unsigned int depth_offset, const ITensorInfo *dst)
Static function to check if given info will lead to a valid configuration of CpuConcatenateDepthKerne...
TensorShape calculate_concatenate_shape(const std::vector< T *> &input, size_t axis)
Calculate the concatenate output shape of the concatenate operation along a single axis...
void add_tensor(int id, ITensor *tensor)
Add tensor to the pack.
Definition: ITensorPack.cpp:30
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94