xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuCopyKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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  */
24 #include "src/cpu/kernels/CpuCopyKernel.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/core/Validate.h"
31 #include "arm_compute/core/Window.h"
32 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 
36 namespace arm_compute
37 {
38 namespace cpu
39 {
40 namespace kernels
41 {
42 namespace
43 {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const PaddingList & padding=PaddingList ())44 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PaddingList &padding = PaddingList())
45 {
46     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
47     ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
48     ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4);
49 
50     // Validate destination if initialized
51     if(dst->total_size() != 0)
52     {
53         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(src->tensor_shape(), padding), dst->tensor_shape());
54         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
55     }
56 
57     return Status{};
58 }
59 
validate_and_configure_window(const ITensorInfo * src,ITensorInfo * dst)60 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
61 {
62     // Destination auto inizialitation if not yet initialized
63     auto_init_if_empty(*dst, *src);
64     return std::make_pair(Status{}, calculate_max_window(*dst));
65 }
66 
validate_and_configure_window_with_padding(const ITensorInfo * src,ITensorInfo * dst,const PaddingList & padding)67 std::pair<Status, Window> validate_and_configure_window_with_padding(const ITensorInfo *src, ITensorInfo *dst, const PaddingList &padding)
68 {
69     const TensorShape src_shape    = src->tensor_shape();
70     const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(src_shape, padding);
71     auto_init_if_empty(*dst, src->clone()->set_tensor_shape(padded_shape));
72     // Configure window
73     const Window win = calculate_max_window(*dst, dst->dimension(0));
74     return std::make_pair(Status{}, win);
75 }
76 
77 } // namespace
78 
configure(const ITensorInfo * src,ITensorInfo * dst,const PaddingList & padding)79 void CpuCopyKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const PaddingList &padding)
80 {
81     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
82     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, padding));
83 
84     _padding = padding;
85 
86     std::pair<Status, Window> win_config;
87     if(padding.empty())
88     {
89         win_config = validate_and_configure_window(src, dst);
90     }
91     else
92     {
93         win_config = validate_and_configure_window_with_padding(src, dst, padding);
94     }
95 
96     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
97     ICpuKernel::configure(win_config.second);
98 }
99 
validate(const arm_compute::ITensorInfo * src,const arm_compute::ITensorInfo * dst,const PaddingList & padding)100 Status CpuCopyKernel::validate(const arm_compute::ITensorInfo *src, const arm_compute::ITensorInfo *dst, const PaddingList &padding)
101 {
102     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, padding));
103 
104     if(padding.empty())
105     {
106         ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get()).first);
107     }
108     else
109     {
110         ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(src->clone().get(), dst->clone().get(), padding).first);
111     }
112 
113     return Status{};
114 }
115 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)116 void CpuCopyKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
117 {
118     ARM_COMPUTE_UNUSED(info);
119     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
121 
122     const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
123     auto       dst = tensors.get_tensor(TensorType::ACL_DST);
124 
125     if(_padding.empty())
126     {
127         Window dst_window{ window };
128         dst_window.set(Window::DimX, Window::Dimension(dst_window.x().start(), dst_window.x().end(), src->info()->dimension(0)));
129         Window out_slice = dst_window.first_slice_window_1D();
130         do
131         {
132             Iterator src_it(src, out_slice);
133             Iterator dst_it(dst, out_slice);
134 
135             execute_window_loop(out_slice, [&](const Coordinates &)
136             {
137                 memcpy(dst_it.ptr(), src_it.ptr(), dst->info()->dimension(0) * dst->info()->element_size());
138             },
139             src_it, dst_it);
140         }
141         while(dst_window.slide_window_slice_1D(out_slice));
142     }
143     else
144     {
145         Window src_window{ window };
146         src_window.set(Window::DimX, Window::Dimension(0, window.x().end() - _padding[0].first, src->info()->dimension(0)));
147 
148         Iterator     src_it(src, src_window);
149         Iterator     dst_it(dst, window);
150         const size_t row_size_in_bytes = src->info()->dimension(0) * src->info()->element_size();
151         execute_window_loop(window, [&](const Coordinates &)
152         {
153             auto dst_ptr = dst_it.ptr() + _padding[0].first * dst->info()->element_size();
154             std::memcpy(dst_ptr, src_it.ptr(), row_size_in_bytes);
155         },
156         src_it, dst_it);
157     }
158 }
159 
name() const160 const char *CpuCopyKernel::name() const
161 {
162     return "CpuCopyKernel";
163 }
164 } // namespace kernels
165 } // namespace cpu
166 } // namespace arm_compute
167