| 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 "concat.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | Concat::Concat() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int Concat::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | axis = pd.get(0, 0); |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int Concat::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 33 | { |
| 34 | int dims = bottom_blobs[0].dims; |
| 35 | size_t elemsize = bottom_blobs[0].elemsize; |
| 36 | int positive_axis = axis < 0 ? dims + axis : axis; |
| 37 | |
| 38 | if (dims == 1) // positive_axis == 0 |
| 39 | { |
| 40 | // concat vector |
| 41 | // total length |
| 42 | int top_w = 0; |
| 43 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 44 | { |
| 45 | const Mat& bottom_blob = bottom_blobs[b]; |
| 46 | top_w += bottom_blob.w; |
| 47 | } |
| 48 | |
| 49 | Mat& top_blob = top_blobs[0]; |
| 50 | top_blob.create(top_w, elemsize, opt.blob_allocator); |
| 51 | if (top_blob.empty()) |
| 52 | return -100; |
| 53 | |
| 54 | unsigned char* outptr = top_blob; |
| 55 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 56 | { |
| 57 | const Mat& bottom_blob = bottom_blobs[b]; |
| 58 | |
| 59 | int w = bottom_blob.w; |
| 60 | |
| 61 | const unsigned char* ptr = bottom_blob; |
| 62 | memcpy(outptr, ptr, w * elemsize); |
| 63 | |
| 64 | outptr += w * elemsize; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (dims == 2 && positive_axis == 0) |
| 69 | { |
| 70 | // concat image |
| 71 | int w = bottom_blobs[0].w; |
| 72 | |
| 73 | // total height |
| 74 | int top_h = 0; |
| 75 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 76 | { |
| 77 | const Mat& bottom_blob = bottom_blobs[b]; |
| 78 | top_h += bottom_blob.h; |
| 79 | } |
| 80 | |
| 81 | Mat& top_blob = top_blobs[0]; |
| 82 | top_blob.create(w, top_h, elemsize, opt.blob_allocator); |
| 83 | if (top_blob.empty()) |
| 84 | return -100; |
| 85 | |
| 86 | unsigned char* outptr = top_blob; |
| 87 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 88 | { |
| 89 | const Mat& bottom_blob = bottom_blobs[b]; |
| 90 | |
| 91 | int size = w * bottom_blob.h; |
| 92 | |
| 93 | const unsigned char* ptr = bottom_blob; |
| 94 | memcpy(outptr, ptr, size * elemsize); |
| 95 | |
| 96 | outptr += size * elemsize; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (dims == 2 && positive_axis == 1) |
| 101 | { |
| 102 | // interleave image row |
| 103 | int h = bottom_blobs[0].h; |
| 104 | |
| 105 | // total width |
| 106 | int top_w = 0; |
| 107 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 108 | { |
| 109 | const Mat& bottom_blob = bottom_blobs[b]; |
| 110 | top_w += bottom_blob.w; |
| 111 | } |
| 112 | |
| 113 | Mat& top_blob = top_blobs[0]; |
| 114 | top_blob.create(top_w, h, elemsize, opt.blob_allocator); |
| 115 | if (top_blob.empty()) |
| 116 | return -100; |
| 117 | |
| 118 | #pragma omp parallel for num_threads(opt.num_threads) |
| 119 | for (int i = 0; i < h; i++) |
| 120 | { |
| 121 | unsigned char* outptr = top_blob.row<unsigned char>(i); |
| 122 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 123 | { |
| 124 | const Mat& bottom_blob = bottom_blobs[b]; |
| 125 | |
| 126 | const unsigned char* ptr = bottom_blob.row<const unsigned char>(i); |
| 127 | memcpy(outptr, ptr, bottom_blob.w * elemsize); |
| 128 | |
| 129 | outptr += bottom_blob.w * elemsize; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if ((dims == 3 || dims == 4) && positive_axis == 0) |
| 135 | { |
| 136 | // concat dim |
| 137 | int w = bottom_blobs[0].w; |
| 138 | int h = bottom_blobs[0].h; |
| 139 | int d = bottom_blobs[0].d; |
| 140 | |
| 141 | // total channels |
| 142 | int top_channels = 0; |
| 143 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 144 | { |
| 145 | const Mat& bottom_blob = bottom_blobs[b]; |
| 146 | top_channels += bottom_blob.c; |
| 147 | } |
| 148 | |
| 149 | Mat& top_blob = top_blobs[0]; |
| 150 | top_blob.create(w, h, d, top_channels, elemsize, opt.blob_allocator); |
| 151 | if (top_blob.empty()) |
| 152 | return -100; |
| 153 | |
| 154 | top_blob.dims = dims; |
| 155 | |
| 156 | int q = 0; |
| 157 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 158 | { |
| 159 | const Mat& bottom_blob = bottom_blobs[b]; |
| 160 | |
| 161 | int channels = bottom_blob.c; |
| 162 | size_t size = bottom_blob.cstep * channels; |
| 163 | |
| 164 | const unsigned char* ptr = bottom_blob; |
| 165 | unsigned char* outptr = top_blob.channel(q); |
| 166 | memcpy(outptr, ptr, size * elemsize); |
| 167 | |
| 168 | q += channels; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if ((dims == 3 && positive_axis == 1) || (dims == 4 && positive_axis == 2)) |
| 173 | { |
| 174 | // interleave dim height |
| 175 | int w = bottom_blobs[0].w; |
| 176 | int d = bottom_blobs[0].d; |
| 177 | int channels = bottom_blobs[0].c; |
| 178 | |
| 179 | // total height |
| 180 | int top_h = 0; |
| 181 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 182 | { |
| 183 | const Mat& bottom_blob = bottom_blobs[b]; |
| 184 | top_h += bottom_blob.h; |
| 185 | } |
| 186 | |
| 187 | Mat& top_blob = top_blobs[0]; |
| 188 | top_blob.create(w, top_h, d, channels, elemsize, opt.blob_allocator); |
| 189 | if (top_blob.empty()) |
| 190 | return -100; |
| 191 | |
| 192 | top_blob.dims = dims; |
| 193 | |
| 194 | #pragma omp parallel for num_threads(opt.num_threads) |
| 195 | for (int q = 0; q < channels; q++) |
| 196 | { |
| 197 | unsigned char* outptr = top_blob.channel(q); |
| 198 | |
| 199 | for (int i = 0; i < d; i++) |
| 200 | { |
| 201 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 202 | { |
| 203 | const Mat& bottom_blob = bottom_blobs[b]; |
| 204 | |
| 205 | int size = bottom_blob.w * bottom_blob.h; |
| 206 | |
| 207 | const unsigned char* ptr = bottom_blob.channel(q).depth(i); |
| 208 | memcpy(outptr, ptr, size * elemsize); |
| 209 | |
| 210 | outptr += size * elemsize; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if ((dims == 3 && positive_axis == 2) || (dims == 4 && positive_axis == 3)) |
| 217 | { |
| 218 | // interleave dim width |
| 219 | int h = bottom_blobs[0].h; |
| 220 | int d = bottom_blobs[0].d; |
| 221 | int channels = bottom_blobs[0].c; |
| 222 | |
| 223 | // total width |
| 224 | int top_w = 0; |
| 225 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 226 | { |
| 227 | const Mat& bottom_blob = bottom_blobs[b]; |
| 228 | top_w += bottom_blob.w; |
| 229 | } |
| 230 | |
| 231 | Mat& top_blob = top_blobs[0]; |
| 232 | top_blob.create(top_w, h, d, channels, elemsize, opt.blob_allocator); |
| 233 | if (top_blob.empty()) |
| 234 | return -100; |
| 235 | |
| 236 | top_blob.dims = dims; |
| 237 | |
| 238 | #pragma omp parallel for num_threads(opt.num_threads) |
| 239 | for (int q = 0; q < channels; q++) |
| 240 | { |
| 241 | unsigned char* outptr = top_blob.channel(q); |
| 242 | |
| 243 | for (int i = 0; i < d; i++) |
| 244 | { |
| 245 | for (int j = 0; j < h; j++) |
| 246 | { |
| 247 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 248 | { |
| 249 | const Mat& bottom_blob = bottom_blobs[b]; |
| 250 | |
| 251 | const unsigned char* ptr = bottom_blob.channel(q).depth(i).row<const unsigned char>(j); |
| 252 | memcpy(outptr, ptr, bottom_blob.w * elemsize); |
| 253 | |
| 254 | outptr += bottom_blob.w * elemsize; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (dims == 4 && positive_axis == 1) |
| 262 | { |
| 263 | // interleave dim depth |
| 264 | int w = bottom_blobs[0].w; |
| 265 | int h = bottom_blobs[0].h; |
| 266 | int channels = bottom_blobs[0].c; |
| 267 | |
| 268 | // total depth |
| 269 | int top_d = 0; |
| 270 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 271 | { |
| 272 | const Mat& bottom_blob = bottom_blobs[b]; |
| 273 | top_d += bottom_blob.d; |
| 274 | } |
| 275 | |
| 276 | Mat& top_blob = top_blobs[0]; |
| 277 | top_blob.create(w, h, top_d, channels, elemsize, opt.blob_allocator); |
| 278 | if (top_blob.empty()) |
| 279 | return -100; |
| 280 | |
| 281 | #pragma omp parallel for num_threads(opt.num_threads) |
| 282 | for (int q = 0; q < channels; q++) |
| 283 | { |
| 284 | unsigned char* outptr = top_blob.channel(q); |
| 285 | |
| 286 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 287 | { |
| 288 | const Mat& bottom_blob = bottom_blobs[b]; |
| 289 | |
| 290 | int size = bottom_blob.w * bottom_blob.h * bottom_blob.d; |
| 291 | |
| 292 | const unsigned char* ptr = bottom_blob.channel(q); |
| 293 | memcpy(outptr, ptr, size * elemsize); |
| 294 | |
| 295 | outptr += size * elemsize; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | } // namespace ncnn |