| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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 "reorg_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | Reorg_vulkan::Reorg_vulkan() |
| 22 | { |
| 23 | support_vulkan = true; |
| 24 | support_image_storage = true; |
| 25 | |
| 26 | pipeline_reorg = 0; |
| 27 | pipeline_reorg_pack4 = 0; |
| 28 | pipeline_reorg_pack1to4 = 0; |
| 29 | pipeline_reorg_pack8 = 0; |
| 30 | pipeline_reorg_pack1to8 = 0; |
| 31 | pipeline_reorg_pack4to8 = 0; |
| 32 | } |
| 33 | |
| 34 | int Reorg_vulkan::create_pipeline(const Option& _opt) |
| 35 | { |
| 36 | Option opt = _opt; |
| 37 | const Mat& shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0]; |
| 38 | const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| 39 | |
| 40 | int elempack = 1; |
| 41 | if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1; |
| 42 | if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1; |
| 43 | if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1; |
| 44 | |
| 45 | int out_elempack = 1; |
| 46 | if (out_shape.dims == 1) out_elempack = opt.use_shader_pack8 && out_shape.w % 8 == 0 ? 8 : out_shape.w % 4 == 0 ? 4 : 1; |
| 47 | if (out_shape.dims == 2) out_elempack = opt.use_shader_pack8 && out_shape.h % 8 == 0 ? 8 : out_shape.h % 4 == 0 ? 4 : 1; |
| 48 | if (out_shape.dims == 3) out_elempack = opt.use_shader_pack8 && out_shape.c % 8 == 0 ? 8 : out_shape.c % 4 == 0 ? 4 : 1; |
| 49 | |
| 50 | size_t elemsize; |
| 51 | size_t out_elemsize; |
| 52 | if (opt.use_fp16_storage) |
| 53 | { |
| 54 | elemsize = elempack * 2u; |
| 55 | out_elemsize = out_elempack * 2u; |
| 56 | } |
| 57 | else if (opt.use_fp16_packed) |
| 58 | { |
| 59 | elemsize = elempack == 1 ? 4u : elempack * 2u; |
| 60 | out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | elemsize = elempack * 4u; |
| 65 | out_elemsize = out_elempack * 4u; |
| 66 | } |
| 67 | |
| 68 | Mat shape_packed; |
| 69 | if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack); |
| 70 | if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack); |
| 71 | if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack); |
| 72 | |
| 73 | Mat out_shape_packed; |
| 74 | if (out_shape.dims == 1) out_shape_packed = Mat(out_shape.w / out_elempack, (void*)0, out_elemsize, out_elempack); |
| 75 | if (out_shape.dims == 2) out_shape_packed = Mat(out_shape.w, out_shape.h / out_elempack, (void*)0, out_elemsize, out_elempack); |
| 76 | if (out_shape.dims == 3) out_shape_packed = Mat(out_shape.w, out_shape.h, out_shape.c / out_elempack, (void*)0, out_elemsize, out_elempack); |
| 77 | |
| 78 | // check blob shape |
| 79 | if (!vkdev->shape_support_image_storage(shape_packed) || !vkdev->shape_support_image_storage(out_shape_packed)) |
| 80 | { |
| 81 | support_image_storage = false; |
| 82 | opt.use_image_storage = false; |
| 83 | } |
| 84 | |
| 85 | std::vector<vk_specialization_type> specializations(2 + 10); |
| 86 | specializations[0].i = stride; |
| 87 | specializations[1].i = mode; |
| 88 | specializations[2 + 0].i = shape_packed.dims; |
| 89 | specializations[2 + 1].i = shape_packed.w; |
| 90 | specializations[2 + 2].i = shape_packed.h; |
| 91 | specializations[2 + 3].i = shape_packed.c; |
| 92 | specializations[2 + 4].i = shape_packed.cstep; |
| 93 | specializations[2 + 5].i = out_shape_packed.dims; |
| 94 | specializations[2 + 6].i = out_shape_packed.w; |
| 95 | specializations[2 + 7].i = out_shape_packed.h; |
| 96 | specializations[2 + 8].i = out_shape_packed.c; |
| 97 | specializations[2 + 9].i = out_shape_packed.cstep; |
| 98 | |
| 99 | Mat local_size_xyz; |
| 100 | if (out_shape_packed.dims != 0) |
| 101 | { |
| 102 | local_size_xyz.w = std::min(4, out_shape_packed.w); |
| 103 | local_size_xyz.h = std::min(4, out_shape_packed.h); |
| 104 | local_size_xyz.c = std::min(4, out_shape_packed.c); |
| 105 | } |
| 106 | |
| 107 | // pack1 |
| 108 | if (shape.dims == 0 || (elempack == 1 && out_elempack == 1)) |
| 109 | { |
| 110 | pipeline_reorg = new Pipeline(vkdev); |
| 111 | pipeline_reorg->set_optimal_local_size_xyz(local_size_xyz); |
| 112 | pipeline_reorg->create(LayerShaderType::reorg, opt, specializations); |
| 113 | } |
| 114 | |
| 115 | // pack4 |
| 116 | if (shape.dims == 0 || (elempack == 4 && out_elempack == 4)) |
| 117 | { |
| 118 | pipeline_reorg_pack4 = new Pipeline(vkdev); |
| 119 | pipeline_reorg_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 120 | pipeline_reorg_pack4->create(LayerShaderType::reorg_pack4, opt, specializations); |
| 121 | } |
| 122 | |
| 123 | // pack1to4 |
| 124 | if (shape.dims == 0 || (elempack == 1 && out_elempack == 4)) |
| 125 | { |
| 126 | pipeline_reorg_pack1to4 = new Pipeline(vkdev); |
| 127 | pipeline_reorg_pack1to4->set_optimal_local_size_xyz(local_size_xyz); |
| 128 | pipeline_reorg_pack1to4->create(LayerShaderType::reorg_pack1to4, opt, specializations); |
| 129 | } |
| 130 | |
| 131 | // pack8 |
| 132 | if ((opt.use_shader_pack8 && shape.dims == 0) || (elempack == 8 && out_elempack == 8)) |
| 133 | { |
| 134 | pipeline_reorg_pack8 = new Pipeline(vkdev); |
| 135 | pipeline_reorg_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 136 | pipeline_reorg_pack8->create(LayerShaderType::reorg_pack8, opt, specializations); |
| 137 | } |
| 138 | |
| 139 | // pack1to8 |
| 140 | if ((opt.use_shader_pack8 && shape.dims == 0) || (elempack == 1 && out_elempack == 8)) |
| 141 | { |
| 142 | pipeline_reorg_pack1to8 = new Pipeline(vkdev); |
| 143 | pipeline_reorg_pack1to8->set_optimal_local_size_xyz(local_size_xyz); |
| 144 | pipeline_reorg_pack1to8->create(LayerShaderType::reorg_pack1to8, opt, specializations); |
| 145 | } |
| 146 | |
| 147 | // pack4to8 |
| 148 | if ((opt.use_shader_pack8 && shape.dims == 0) || (elempack == 4 && out_elempack == 8)) |
| 149 | { |
| 150 | pipeline_reorg_pack4to8 = new Pipeline(vkdev); |
| 151 | pipeline_reorg_pack4to8->set_optimal_local_size_xyz(local_size_xyz); |
| 152 | pipeline_reorg_pack4to8->create(LayerShaderType::reorg_pack4to8, opt, specializations); |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int Reorg_vulkan::destroy_pipeline(const Option& /*opt*/) |
| 159 | { |
| 160 | delete pipeline_reorg; |
| 161 | pipeline_reorg = 0; |
| 162 | |
| 163 | delete pipeline_reorg_pack4; |
| 164 | pipeline_reorg_pack4 = 0; |
| 165 | |
| 166 | delete pipeline_reorg_pack1to4; |
| 167 | pipeline_reorg_pack1to4 = 0; |
| 168 | |
| 169 | delete pipeline_reorg_pack8; |
| 170 | pipeline_reorg_pack8 = 0; |
| 171 | |
| 172 | delete pipeline_reorg_pack1to8; |
| 173 | pipeline_reorg_pack1to8 = 0; |
| 174 | |
| 175 | delete pipeline_reorg_pack4to8; |
| 176 | pipeline_reorg_pack4to8 = 0; |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int Reorg_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 182 | { |
| 183 | int w = bottom_blob.w; |
| 184 | int h = bottom_blob.h; |
| 185 | int channels = bottom_blob.c; |
| 186 | size_t elemsize = bottom_blob.elemsize; |
| 187 | int elempack = bottom_blob.elempack; |
| 188 | |
| 189 | int outw = w / stride; |
| 190 | int outh = h / stride; |
| 191 | int outc = channels * elempack * stride * stride; |
| 192 | |
| 193 | int out_elempack = opt.use_shader_pack8 && outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1; |
| 194 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 195 | |
| 196 | if (opt.use_fp16_packed && !opt.use_fp16_storage) |
| 197 | { |
| 198 | if (out_elempack == 8) out_elemsize = 8 * 2u; |
| 199 | if (out_elempack == 4) out_elemsize = 4 * 2u; |
| 200 | if (out_elempack == 1) out_elemsize = 4u; |
| 201 | } |
| 202 | |
| 203 | top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 204 | if (top_blob.empty()) |
| 205 | return -100; |
| 206 | |
| 207 | std::vector<VkMat> bindings(2); |
| 208 | bindings[0] = bottom_blob; |
| 209 | bindings[1] = top_blob; |
| 210 | |
| 211 | std::vector<vk_constant_type> constants(10); |
| 212 | constants[0].i = bottom_blob.dims; |
| 213 | constants[1].i = bottom_blob.w; |
| 214 | constants[2].i = bottom_blob.h; |
| 215 | constants[3].i = bottom_blob.c; |
| 216 | constants[4].i = bottom_blob.cstep; |
| 217 | constants[5].i = top_blob.dims; |
| 218 | constants[6].i = top_blob.w; |
| 219 | constants[7].i = top_blob.h; |
| 220 | constants[8].i = top_blob.c; |
| 221 | constants[9].i = top_blob.cstep; |
| 222 | |
| 223 | const Pipeline* pipeline = 0; |
| 224 | if (elempack == 1 && out_elempack == 1) |
| 225 | { |
| 226 | pipeline = pipeline_reorg; |
| 227 | } |
| 228 | else if (elempack == 4 && out_elempack == 4) |
| 229 | { |
| 230 | pipeline = pipeline_reorg_pack4; |
| 231 | } |
| 232 | else if (elempack == 1 && out_elempack == 4) |
| 233 | { |
| 234 | pipeline = pipeline_reorg_pack1to4; |
| 235 | } |
| 236 | else if (elempack == 8) // assert out_elempack == 8 |
| 237 | { |
| 238 | pipeline = pipeline_reorg_pack8; |
| 239 | } |
| 240 | else if (elempack == 1 && out_elempack == 8) |
| 241 | { |
| 242 | pipeline = pipeline_reorg_pack1to8; |
| 243 | } |
| 244 | else if (elempack == 4 && out_elempack == 8) |
| 245 | { |
| 246 | pipeline = pipeline_reorg_pack4to8; |
| 247 | } |
| 248 | |
| 249 | cmd.record_pipeline(pipeline, bindings, constants, top_blob); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | int Reorg_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 255 | { |
| 256 | int w = bottom_blob.w; |
| 257 | int h = bottom_blob.h; |
| 258 | int channels = bottom_blob.c; |
| 259 | size_t elemsize = bottom_blob.elemsize; |
| 260 | int elempack = bottom_blob.elempack; |
| 261 | |
| 262 | int outw = w / stride; |
| 263 | int outh = h / stride; |
| 264 | int outc = channels * elempack * stride * stride; |
| 265 | |
| 266 | int out_elempack = opt.use_shader_pack8 && outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1; |
| 267 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 268 | |
| 269 | if (opt.use_fp16_packed && !opt.use_fp16_storage) |
| 270 | { |
| 271 | if (out_elempack == 8) out_elemsize = 8 * 2u; |
| 272 | if (out_elempack == 4) out_elemsize = 4 * 2u; |
| 273 | if (out_elempack == 1) out_elemsize = 4u; |
| 274 | } |
| 275 | |
| 276 | top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 277 | if (top_blob.empty()) |
| 278 | return -100; |
| 279 | |
| 280 | std::vector<VkImageMat> bindings(2); |
| 281 | bindings[0] = bottom_blob; |
| 282 | bindings[1] = top_blob; |
| 283 | |
| 284 | std::vector<vk_constant_type> constants(10); |
| 285 | constants[0].i = bottom_blob.dims; |
| 286 | constants[1].i = bottom_blob.w; |
| 287 | constants[2].i = bottom_blob.h; |
| 288 | constants[3].i = bottom_blob.c; |
| 289 | constants[4].i = 0; //bottom_blob.cstep; |
| 290 | constants[5].i = top_blob.dims; |
| 291 | constants[6].i = top_blob.w; |
| 292 | constants[7].i = top_blob.h; |
| 293 | constants[8].i = top_blob.c; |
| 294 | constants[9].i = 0; //top_blob.cstep; |
| 295 | |
| 296 | const Pipeline* pipeline = 0; |
| 297 | if (elempack == 1 && out_elempack == 1) |
| 298 | { |
| 299 | pipeline = pipeline_reorg; |
| 300 | } |
| 301 | else if (elempack == 4 && out_elempack == 4) |
| 302 | { |
| 303 | pipeline = pipeline_reorg_pack4; |
| 304 | } |
| 305 | else if (elempack == 1 && out_elempack == 4) |
| 306 | { |
| 307 | pipeline = pipeline_reorg_pack1to4; |
| 308 | } |
| 309 | else if (elempack == 8) // assert out_elempack == 8 |
| 310 | { |
| 311 | pipeline = pipeline_reorg_pack8; |
| 312 | } |
| 313 | else if (elempack == 1 && out_elempack == 8) |
| 314 | { |
| 315 | pipeline = pipeline_reorg_pack1to8; |
| 316 | } |
| 317 | else if (elempack == 4 && out_elempack == 8) |
| 318 | { |
| 319 | pipeline = pipeline_reorg_pack4to8; |
| 320 | } |
| 321 | |
| 322 | cmd.record_pipeline(pipeline, bindings, constants, top_blob); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | } // namespace ncnn |