| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "copyto.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | CopyTo::CopyTo() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int CopyTo::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | woffset = pd.get(0, 0); |
| 28 | hoffset = pd.get(1, 0); |
| 29 | doffset = pd.get(13, 0); |
| 30 | coffset = pd.get(2, 0); |
| 31 | |
| 32 | starts = pd.get(9, Mat()); |
| 33 | axes = pd.get(11, Mat()); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | template<typename T> |
| 39 | static void copy_to_image(const Mat& src, Mat& self, int top, int left) |
| 40 | { |
| 41 | int w = src.w; |
| 42 | int h = src.h; |
| 43 | |
| 44 | const T* ptr = src; |
| 45 | T* outptr = self.row<T>(top) + left; |
| 46 | |
| 47 | for (int y = 0; y < h; y++) |
| 48 | { |
| 49 | memcpy(outptr, ptr, w * sizeof(T)); |
| 50 | ptr += w; |
| 51 | outptr += self.w; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | int CopyTo::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 56 | { |
| 57 | const Mat& self_blob = bottom_blobs[0]; |
| 58 | const Mat& src_blob = bottom_blobs[1]; |
| 59 | Mat& top_blob = top_blobs[0]; |
| 60 | |
| 61 | int w = self_blob.w; |
| 62 | int h = self_blob.h; |
| 63 | int d = self_blob.d; |
| 64 | int channels = self_blob.c; |
| 65 | int dims = self_blob.dims; |
| 66 | size_t elemsize = self_blob.elemsize; |
| 67 | |
| 68 | if (src_blob.dims == dims && src_blob.w == w && src_blob.h == h && src_blob.d == d && src_blob.c == channels) |
| 69 | { |
| 70 | top_blob = src_blob; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | top_blob = self_blob.clone(opt.blob_allocator); |
| 75 | if (top_blob.empty()) |
| 76 | return -100; |
| 77 | |
| 78 | int _woffset, _hoffset, _doffset, _coffset; |
| 79 | resolve_copyto_offset(self_blob.shape(), _woffset, _hoffset, _doffset, _coffset); |
| 80 | |
| 81 | if (dims == 1) |
| 82 | { |
| 83 | if (elemsize == 1) |
| 84 | copy_to_image<signed char>(src_blob, top_blob, 0, _woffset); |
| 85 | if (elemsize == 2) |
| 86 | copy_to_image<unsigned short>(src_blob, top_blob, 0, _woffset); |
| 87 | if (elemsize == 4) |
| 88 | copy_to_image<float>(src_blob, top_blob, 0, _woffset); |
| 89 | } |
| 90 | |
| 91 | if (dims == 2) |
| 92 | { |
| 93 | if (elemsize == 1) |
| 94 | copy_to_image<signed char>(src_blob, top_blob, _hoffset, _woffset); |
| 95 | if (elemsize == 2) |
| 96 | copy_to_image<unsigned short>(src_blob, top_blob, _hoffset, _woffset); |
| 97 | if (elemsize == 4) |
| 98 | copy_to_image<float>(src_blob, top_blob, _hoffset, _woffset); |
| 99 | } |
| 100 | |
| 101 | if (dims == 3) |
| 102 | { |
| 103 | #pragma omp parallel for num_threads(opt.num_threads) |
| 104 | for (int q = 0; q < src_blob.c; q++) |
| 105 | { |
| 106 | const Mat roim = src_blob.channel(q); |
| 107 | Mat m = top_blob.channel(q + _coffset); |
| 108 | |
| 109 | if (elemsize == 1) |
| 110 | copy_to_image<signed char>(roim, m, _hoffset, _woffset); |
| 111 | if (elemsize == 2) |
| 112 | copy_to_image<unsigned short>(roim, m, _hoffset, _woffset); |
| 113 | if (elemsize == 4) |
| 114 | copy_to_image<float>(roim, m, _hoffset, _woffset); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (dims == 4) |
| 119 | { |
| 120 | #pragma omp parallel for num_threads(opt.num_threads) |
| 121 | for (int q = 0; q < src_blob.c; q++) |
| 122 | { |
| 123 | for (int z = 0; z < src_blob.d; z++) |
| 124 | { |
| 125 | const Mat roim = src_blob.channel(q).depth(z); |
| 126 | Mat m = top_blob.channel(q + _coffset).depth(z + _doffset); |
| 127 | |
| 128 | if (elemsize == 1) |
| 129 | copy_to_image<signed char>(roim, m, _hoffset, _woffset); |
| 130 | if (elemsize == 2) |
| 131 | copy_to_image<unsigned short>(roim, m, _hoffset, _woffset); |
| 132 | if (elemsize == 4) |
| 133 | copy_to_image<float>(roim, m, _hoffset, _woffset); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | void CopyTo::resolve_copyto_offset(const Mat& self_blob, int& _woffset, int& _hoffset, int& _doffset, int& _coffset) const |
| 142 | { |
| 143 | int w = self_blob.w; |
| 144 | int h = self_blob.h; |
| 145 | int d = self_blob.d; |
| 146 | int channels = self_blob.c; |
| 147 | int dims = self_blob.dims; |
| 148 | |
| 149 | bool numpy_style_slice = !starts.empty(); |
| 150 | if (numpy_style_slice) |
| 151 | { |
| 152 | _woffset = 0; |
| 153 | _hoffset = 0; |
| 154 | _doffset = 0; |
| 155 | _coffset = 0; |
| 156 | |
| 157 | const int* starts_ptr = starts; |
| 158 | const int* axes_ptr = axes; |
| 159 | |
| 160 | int _axes[4] = {0, 1, 2, 3}; |
| 161 | int num_axis = axes.w; |
| 162 | if (num_axis == 0) |
| 163 | { |
| 164 | num_axis = dims; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | for (int i = 0; i < num_axis; i++) |
| 169 | { |
| 170 | int axis = axes_ptr[i]; |
| 171 | if (axis < 0) |
| 172 | axis = dims + axis; |
| 173 | _axes[i] = axis; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | for (int i = 0; i < num_axis; i++) |
| 178 | { |
| 179 | int axis = _axes[i]; |
| 180 | int start = starts_ptr[i]; |
| 181 | |
| 182 | if (dims == 1) // axis == 0 |
| 183 | { |
| 184 | if (start == -233) start = 0; |
| 185 | _woffset = start >= 0 ? start : w + start; |
| 186 | } |
| 187 | if (dims == 2) |
| 188 | { |
| 189 | if (axis == 0) |
| 190 | { |
| 191 | if (start == -233) start = 0; |
| 192 | _hoffset = start >= 0 ? start : h + start; |
| 193 | } |
| 194 | if (axis == 1) |
| 195 | { |
| 196 | if (start == -233) start = 0; |
| 197 | _woffset = start >= 0 ? start : w + start; |
| 198 | } |
| 199 | } |
| 200 | if (dims == 3) |
| 201 | { |
| 202 | if (axis == 0) |
| 203 | { |
| 204 | if (start == -233) start = 0; |
| 205 | _coffset = start >= 0 ? start : channels + start; |
| 206 | } |
| 207 | if (axis == 1) |
| 208 | { |
| 209 | if (start == -233) start = 0; |
| 210 | _hoffset = start >= 0 ? start : h + start; |
| 211 | } |
| 212 | if (axis == 2) |
| 213 | { |
| 214 | if (start == -233) start = 0; |
| 215 | _woffset = start >= 0 ? start : w + start; |
| 216 | } |
| 217 | } |
| 218 | if (dims == 4) |
| 219 | { |
| 220 | if (axis == 0) |
| 221 | { |
| 222 | if (start == -233) start = 0; |
| 223 | _coffset = start >= 0 ? start : channels + start; |
| 224 | } |
| 225 | if (axis == 1) |
| 226 | { |
| 227 | if (start == -233) start = 0; |
| 228 | _doffset = start >= 0 ? start : d + start; |
| 229 | } |
| 230 | if (axis == 2) |
| 231 | { |
| 232 | if (start == -233) start = 0; |
| 233 | _hoffset = start >= 0 ? start : h + start; |
| 234 | } |
| 235 | if (axis == 3) |
| 236 | { |
| 237 | if (start == -233) start = 0; |
| 238 | _woffset = start >= 0 ? start : w + start; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | _woffset = woffset; |
| 246 | _hoffset = hoffset; |
| 247 | _doffset = doffset; |
| 248 | _coffset = coffset; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | } // namespace ncnn |