| 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 "tile.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | Tile::Tile() |
| 20 | { |
| 21 | one_blob_only = true; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int Tile::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | axis = pd.get(0, 0); |
| 28 | tiles = pd.get(1, 1); |
| 29 | repeats = pd.get(2, Mat()); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | int Tile::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 35 | { |
| 36 | int dims = bottom_blob.dims; |
| 37 | int repeat_w = 1; |
| 38 | int repeat_h = 1; |
| 39 | int repeat_d = 1; |
| 40 | int repeat_c = 1; |
| 41 | |
| 42 | const int repeats_num = repeats.w; |
| 43 | |
| 44 | if (repeats.empty()) |
| 45 | { |
| 46 | if (dims == 1) // axis == 0 |
| 47 | { |
| 48 | repeat_w = tiles; |
| 49 | } |
| 50 | else if (dims == 2) |
| 51 | { |
| 52 | if (axis == 0) repeat_h = tiles; |
| 53 | if (axis == 1) repeat_w = tiles; |
| 54 | } |
| 55 | else if (dims == 3) |
| 56 | { |
| 57 | if (axis == 0) repeat_c = tiles; |
| 58 | if (axis == 1) repeat_h = tiles; |
| 59 | if (axis == 2) repeat_w = tiles; |
| 60 | } |
| 61 | else if (dims == 4) |
| 62 | { |
| 63 | if (axis == 0) repeat_c = tiles; |
| 64 | if (axis == 1) repeat_d = tiles; |
| 65 | if (axis == 2) repeat_h = tiles; |
| 66 | if (axis == 3) repeat_w = tiles; |
| 67 | } |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | // numpy style tile |
| 72 | const int* repeats_ptr = repeats; |
| 73 | |
| 74 | if (repeats_num == 1) |
| 75 | { |
| 76 | repeat_w = repeats_ptr[0]; |
| 77 | } |
| 78 | if (repeats_num == 2) |
| 79 | { |
| 80 | repeat_h = repeats_ptr[0]; |
| 81 | repeat_w = repeats_ptr[1]; |
| 82 | } |
| 83 | if (repeats_num == 3) |
| 84 | { |
| 85 | if (dims == 4) |
| 86 | { |
| 87 | repeat_d = repeats_ptr[0]; |
| 88 | repeat_h = repeats_ptr[1]; |
| 89 | repeat_w = repeats_ptr[2]; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | repeat_c = repeats_ptr[0]; |
| 94 | repeat_h = repeats_ptr[1]; |
| 95 | repeat_w = repeats_ptr[2]; |
| 96 | } |
| 97 | } |
| 98 | if (repeats_num == 4) |
| 99 | { |
| 100 | repeat_c = repeats_ptr[0]; |
| 101 | repeat_d = repeats_ptr[1]; |
| 102 | repeat_h = repeats_ptr[2]; |
| 103 | repeat_w = repeats_ptr[3]; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int w = bottom_blob.w; |
| 108 | int h = bottom_blob.h; |
| 109 | int d = bottom_blob.d; |
| 110 | int channels = bottom_blob.c; |
| 111 | size_t elemsize = bottom_blob.elemsize; |
| 112 | |
| 113 | const int outdims = std::max(dims, repeats_num); |
| 114 | if (repeat_w != 1 && repeat_h == 1 && repeat_d == 1 && repeat_c == 1) |
| 115 | { |
| 116 | if (outdims == 1) |
| 117 | top_blob.create(w * repeat_w, elemsize, opt.blob_allocator); |
| 118 | if (outdims == 2) |
| 119 | top_blob.create(w * repeat_w, h, elemsize, opt.blob_allocator); |
| 120 | if (outdims == 3) |
| 121 | top_blob.create(w * repeat_w, h, channels, elemsize, opt.blob_allocator); |
| 122 | if (outdims == 4) |
| 123 | top_blob.create(w * repeat_w, h, d, channels, elemsize, opt.blob_allocator); |
| 124 | } |
| 125 | else if (repeat_h != 1 && repeat_d == 1 && repeat_c == 1) |
| 126 | { |
| 127 | if (outdims == 2) |
| 128 | top_blob.create(w * repeat_w, h * repeat_h, elemsize, opt.blob_allocator); |
| 129 | if (outdims == 3) |
| 130 | top_blob.create(w * repeat_w, h * repeat_h, channels, elemsize, opt.blob_allocator); |
| 131 | if (outdims == 4) |
| 132 | top_blob.create(w * repeat_w, h * repeat_h, d, channels, elemsize, opt.blob_allocator); |
| 133 | } |
| 134 | else if (repeat_d == 1 && repeat_c != 1) |
| 135 | { |
| 136 | if (outdims == 3) |
| 137 | top_blob.create(w * repeat_w, h * repeat_h, channels * repeat_c, elemsize, opt.blob_allocator); |
| 138 | if (outdims == 4) |
| 139 | top_blob.create(w * repeat_w, h * repeat_h, d, channels * repeat_c, elemsize, opt.blob_allocator); |
| 140 | } |
| 141 | else if (repeat_d != 1 && repeat_c != 1) |
| 142 | { |
| 143 | if (outdims == 4) |
| 144 | top_blob.create(w * repeat_w, h * repeat_h, d * repeat_d, channels * repeat_c, elemsize, opt.blob_allocator); |
| 145 | } |
| 146 | else // all ones |
| 147 | { |
| 148 | if (repeats_num == 0 || dims == repeats_num) |
| 149 | { |
| 150 | top_blob = bottom_blob; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | if (outdims == 2) |
| 155 | top_blob.create(w * repeat_w, h * repeat_h, elemsize, opt.blob_allocator); |
| 156 | if (outdims == 3) |
| 157 | top_blob.create(w * repeat_w, h * repeat_h, channels * repeat_c, elemsize, opt.blob_allocator); |
| 158 | if (outdims == 4) |
| 159 | top_blob.create(w * repeat_w, h * repeat_h, d * repeat_d, channels * repeat_c, elemsize, opt.blob_allocator); |
| 160 | } |
| 161 | if (top_blob.empty()) |
| 162 | return -100; |
| 163 | |
| 164 | #pragma omp parallel for num_threads(opt.num_threads) |
| 165 | for (int q = 0; q < channels; q++) |
| 166 | { |
| 167 | // repeat 0-w |
| 168 | for (int z = 0; z < d; z++) |
| 169 | { |
| 170 | for (int y = 0; y < h; y++) |
| 171 | { |
| 172 | const float* ptr = bottom_blob.channel(q).depth(z).row(y); |
| 173 | float* outptr = top_blob.channel(q).depth(z).row(y); |
| 174 | |
| 175 | for (int p = 0; p < repeat_w; p++) |
| 176 | { |
| 177 | memcpy(outptr, ptr, w * sizeof(float)); |
| 178 | outptr += w; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // repeat 1-h |
| 184 | for (int z = 0; z < d; z++) |
| 185 | { |
| 186 | const float* ptr = top_blob.channel(q).depth(z); |
| 187 | float* outptr = top_blob.channel(q).depth(z).row(h); |
| 188 | |
| 189 | const int size = w * repeat_w * h; |
| 190 | for (int p = 1; p < repeat_h; p++) |
| 191 | { |
| 192 | memcpy(outptr, ptr, size * sizeof(float)); |
| 193 | outptr += size; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // repeat 1-d |
| 198 | { |
| 199 | const float* ptr = top_blob.channel(q); |
| 200 | float* outptr = top_blob.channel(q).depth(d); |
| 201 | |
| 202 | const int size = w * repeat_w * h * repeat_h * d; |
| 203 | for (int p = 1; p < repeat_d; p++) |
| 204 | { |
| 205 | memcpy(outptr, ptr, size * sizeof(float)); |
| 206 | outptr += size; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // repeat 1-c |
| 212 | #pragma omp parallel for num_threads(opt.num_threads) |
| 213 | for (int p = 1; p < repeat_c; p++) |
| 214 | { |
| 215 | const float* ptr = top_blob.channel_range(0, channels); |
| 216 | float* outptr = top_blob.channel_range(p * channels, channels); |
| 217 | |
| 218 | memcpy(outptr, ptr, top_blob.cstep * channels * sizeof(float)); |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | } // namespace ncnn |