| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 "slice.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | Slice::Slice() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | int Slice::load_param(const ParamDict& pd) |
| 24 | { |
| 25 | slices = pd.get(0, Mat()); |
| 26 | axis = pd.get(1, 0); |
| 27 | indices = pd.get(2, Mat()); |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int Slice::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 33 | { |
| 34 | const Mat& bottom_blob = bottom_blobs[0]; |
| 35 | int dims = bottom_blob.dims; |
| 36 | size_t elemsize = bottom_blob.elemsize; |
| 37 | const int* slices_ptr = slices; |
| 38 | const int* indices_ptr = indices; |
| 39 | int positive_axis = axis < 0 ? dims + axis : axis; |
| 40 | |
| 41 | if (dims == 1) // positive_axis == 0 |
| 42 | { |
| 43 | int w = bottom_blob.w; |
| 44 | |
| 45 | int q = 0; |
| 46 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 47 | { |
| 48 | int slice; |
| 49 | if (indices_ptr) |
| 50 | { |
| 51 | if (i == top_blobs.size() - 1) |
| 52 | { |
| 53 | slice = w - q; |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | int indice = indices_ptr[i]; |
| 58 | int positive_indice = indice < 0 ? w + indice : indice; |
| 59 | slice = positive_indice - q; |
| 60 | } |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | slice = slices_ptr[i]; |
| 65 | if (slice == -233) |
| 66 | { |
| 67 | slice = static_cast<int>((w - q) / (top_blobs.size() - i)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Mat& top_blob = top_blobs[i]; |
| 72 | top_blob.create(slice, elemsize, opt.blob_allocator); |
| 73 | if (top_blob.empty()) |
| 74 | return -100; |
| 75 | |
| 76 | const unsigned char* ptr = (const unsigned char*)bottom_blob + q * elemsize; |
| 77 | unsigned char* outptr = top_blob; |
| 78 | memcpy(outptr, ptr, slice * elemsize); |
| 79 | |
| 80 | q += slice; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | if (dims == 2 && positive_axis == 0) |
| 87 | { |
| 88 | int w = bottom_blob.w; |
| 89 | int h = bottom_blob.h; |
| 90 | |
| 91 | int q = 0; |
| 92 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 93 | { |
| 94 | int slice; |
| 95 | if (indices_ptr) |
| 96 | { |
| 97 | if (i == top_blobs.size() - 1) |
| 98 | { |
| 99 | slice = h - q; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | int indice = indices_ptr[i]; |
| 104 | int positive_indice = indice < 0 ? h + indice : indice; |
| 105 | slice = positive_indice - q; |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | slice = slices_ptr[i]; |
| 111 | if (slice == -233) |
| 112 | { |
| 113 | slice = static_cast<int>((h - q) / (top_blobs.size() - i)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | Mat& top_blob = top_blobs[i]; |
| 118 | top_blob.create(w, slice, elemsize, opt.blob_allocator); |
| 119 | if (top_blob.empty()) |
| 120 | return -100; |
| 121 | |
| 122 | int size = w * slice; |
| 123 | |
| 124 | const unsigned char* ptr = bottom_blob.row<const unsigned char>(q); |
| 125 | unsigned char* outptr = top_blob; |
| 126 | memcpy(outptr, ptr, size * elemsize); |
| 127 | |
| 128 | q += slice; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | if (dims == 2 && positive_axis == 1) |
| 135 | { |
| 136 | int w = bottom_blob.w; |
| 137 | int h = bottom_blob.h; |
| 138 | |
| 139 | int q = 0; |
| 140 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 141 | { |
| 142 | int slice; |
| 143 | if (indices_ptr) |
| 144 | { |
| 145 | if (i == top_blobs.size() - 1) |
| 146 | { |
| 147 | slice = w - q; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | int indice = indices_ptr[i]; |
| 152 | int positive_indice = indice < 0 ? w + indice : indice; |
| 153 | slice = positive_indice - q; |
| 154 | } |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | slice = slices_ptr[i]; |
| 159 | if (slice == -233) |
| 160 | { |
| 161 | slice = static_cast<int>((w - q) / (top_blobs.size() - i)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | Mat& top_blob = top_blobs[i]; |
| 166 | top_blob.create(slice, h, elemsize, opt.blob_allocator); |
| 167 | if (top_blob.empty()) |
| 168 | return -100; |
| 169 | |
| 170 | #pragma omp parallel for num_threads(opt.num_threads) |
| 171 | for (int j = 0; j < h; j++) |
| 172 | { |
| 173 | unsigned char* outptr = top_blob.row<unsigned char>(j); |
| 174 | const unsigned char* ptr = bottom_blob.row<const unsigned char>(j) + q * elemsize; |
| 175 | memcpy(outptr, ptr, slice * elemsize); |
| 176 | } |
| 177 | |
| 178 | q += slice; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | if ((dims == 3 || dims == 4) && positive_axis == 0) |
| 185 | { |
| 186 | int w = bottom_blob.w; |
| 187 | int h = bottom_blob.h; |
| 188 | int d = bottom_blob.d; |
| 189 | int channels = bottom_blob.c; |
| 190 | |
| 191 | int q = 0; |
| 192 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 193 | { |
| 194 | int slice; |
| 195 | if (indices_ptr) |
| 196 | { |
| 197 | if (i == top_blobs.size() - 1) |
| 198 | { |
| 199 | slice = channels - q; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | int indice = indices_ptr[i]; |
| 204 | int positive_indice = indice < 0 ? channels + indice : indice; |
| 205 | slice = positive_indice - q; |
| 206 | } |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | slice = slices_ptr[i]; |
| 211 | if (slice == -233) |
| 212 | { |
| 213 | slice = static_cast<int>((channels - q) / (top_blobs.size() - i)); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | Mat& top_blob = top_blobs[i]; |
| 218 | top_blob.create(w, h, d, slice, elemsize, opt.blob_allocator); |
| 219 | if (top_blob.empty()) |
| 220 | return -100; |
| 221 | |
| 222 | top_blob.dims = dims; |
| 223 | |
| 224 | int size = static_cast<int>(bottom_blob.cstep * slice); |
| 225 | |
| 226 | const unsigned char* ptr = bottom_blob.channel(q); |
| 227 | unsigned char* outptr = top_blob; |
| 228 | memcpy(outptr, ptr, size * elemsize); |
| 229 | |
| 230 | q += slice; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | if ((dims == 3 && positive_axis == 1) || (dims == 4 && positive_axis == 2)) |
| 237 | { |
| 238 | int w = bottom_blob.w; |
| 239 | int h = bottom_blob.h; |
| 240 | int d = bottom_blob.d; |
| 241 | int channels = bottom_blob.c; |
| 242 | |
| 243 | int q = 0; |
| 244 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 245 | { |
| 246 | int slice; |
| 247 | if (indices_ptr) |
| 248 | { |
| 249 | if (i == top_blobs.size() - 1) |
| 250 | { |
| 251 | slice = h - q; |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | int indice = indices_ptr[i]; |
| 256 | int positive_indice = indice < 0 ? h + indice : indice; |
| 257 | slice = positive_indice - q; |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | slice = slices_ptr[i]; |
| 263 | if (slice == -233) |
| 264 | { |
| 265 | slice = static_cast<int>((h - q) / (top_blobs.size() - i)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | Mat& top_blob = top_blobs[i]; |
| 270 | top_blob.create(w, slice, d, channels, elemsize, opt.blob_allocator); |
| 271 | if (top_blob.empty()) |
| 272 | return -100; |
| 273 | |
| 274 | top_blob.dims = dims; |
| 275 | |
| 276 | #pragma omp parallel for num_threads(opt.num_threads) |
| 277 | for (int p = 0; p < channels; p++) |
| 278 | { |
| 279 | for (int j = 0; j < d; j++) |
| 280 | { |
| 281 | int size = w * slice; |
| 282 | |
| 283 | unsigned char* outptr = top_blob.channel(p).depth(j); |
| 284 | const unsigned char* ptr = bottom_blob.channel(p).depth(j).row<const unsigned char>(q); |
| 285 | memcpy(outptr, ptr, size * elemsize); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | q += slice; |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | if ((dims == 3 && positive_axis == 2) || (dims == 4 && positive_axis == 3)) |
| 296 | { |
| 297 | int w = bottom_blob.w; |
| 298 | int h = bottom_blob.h; |
| 299 | int d = bottom_blob.d; |
| 300 | int channels = bottom_blob.c; |
| 301 | |
| 302 | int q = 0; |
| 303 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 304 | { |
| 305 | int slice; |
| 306 | if (indices_ptr) |
| 307 | { |
| 308 | if (i == top_blobs.size() - 1) |
| 309 | { |
| 310 | slice = w - q; |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | int indice = indices_ptr[i]; |
| 315 | int positive_indice = indice < 0 ? w + indice : indice; |
| 316 | slice = positive_indice - q; |
| 317 | } |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | slice = slices_ptr[i]; |
| 322 | if (slice == -233) |
| 323 | { |
| 324 | slice = static_cast<int>((w - q) / (top_blobs.size() - i)); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | Mat& top_blob = top_blobs[i]; |
| 329 | top_blob.create(slice, h, d, channels, elemsize, opt.blob_allocator); |
| 330 | if (top_blob.empty()) |
| 331 | return -100; |
| 332 | |
| 333 | top_blob.dims = dims; |
| 334 | |
| 335 | #pragma omp parallel for num_threads(opt.num_threads) |
| 336 | for (int p = 0; p < channels; p++) |
| 337 | { |
| 338 | unsigned char* outptr = top_blob.channel(p); |
| 339 | const Mat m = bottom_blob.channel(p); |
| 340 | |
| 341 | for (int j = 0; j < d; j++) |
| 342 | { |
| 343 | for (int k = 0; k < h; k++) |
| 344 | { |
| 345 | const unsigned char* ptr = m.depth(j).row<const unsigned char>(k) + q * elemsize; |
| 346 | memcpy(outptr, ptr, slice * elemsize); |
| 347 | |
| 348 | outptr += slice * elemsize; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | q += slice; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | if (dims == 4 && positive_axis == 1) |
| 360 | { |
| 361 | int w = bottom_blob.w; |
| 362 | int h = bottom_blob.h; |
| 363 | int d = bottom_blob.d; |
| 364 | int channels = bottom_blob.c; |
| 365 | |
| 366 | int q = 0; |
| 367 | for (size_t i = 0; i < top_blobs.size(); i++) |
| 368 | { |
| 369 | int slice; |
| 370 | if (indices_ptr) |
| 371 | { |
| 372 | if (i == top_blobs.size() - 1) |
| 373 | { |
| 374 | slice = d - q; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | int indice = indices_ptr[i]; |
| 379 | int positive_indice = indice < 0 ? d + indice : indice; |
| 380 | slice = positive_indice - q; |
| 381 | } |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | slice = slices_ptr[i]; |
| 386 | if (slice == -233) |
| 387 | { |
| 388 | slice = static_cast<int>((d - q) / (top_blobs.size() - i)); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | Mat& top_blob = top_blobs[i]; |
| 393 | top_blob.create(w, h, slice, channels, elemsize, opt.blob_allocator); |
| 394 | if (top_blob.empty()) |
| 395 | return -100; |
| 396 | |
| 397 | #pragma omp parallel for num_threads(opt.num_threads) |
| 398 | for (int p = 0; p < channels; p++) |
| 399 | { |
| 400 | int size = w * h * slice; |
| 401 | |
| 402 | unsigned char* outptr = top_blob.channel(p); |
| 403 | const unsigned char* ptr = bottom_blob.channel(p).depth(q); |
| 404 | memcpy(outptr, ptr, size * elemsize); |
| 405 | } |
| 406 | |
| 407 | q += slice; |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | } // namespace ncnn |