| 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 "pooling_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | #include "layer_type.h" |
| 19 | |
| 20 | #include <float.h> |
| 21 | |
| 22 | namespace ncnn { |
| 23 | |
| 24 | Pooling_vulkan::Pooling_vulkan() |
| 25 | { |
| 26 | support_vulkan = true; |
| 27 | support_image_storage = true; |
| 28 | |
| 29 | padding = 0; |
| 30 | pipeline_pooling = 0; |
| 31 | pipeline_pooling_pack4 = 0; |
| 32 | pipeline_pooling_pack8 = 0; |
| 33 | |
| 34 | pipeline_pooling_adaptive = 0; |
| 35 | pipeline_pooling_adaptive_pack4 = 0; |
| 36 | pipeline_pooling_adaptive_pack8 = 0; |
| 37 | |
| 38 | pipeline_pooling_global_reduce_first = 0; |
| 39 | pipeline_pooling_global_reduce_first_pack4 = 0; |
| 40 | pipeline_pooling_global_reduce_first_pack8 = 0; |
| 41 | pipeline_pooling_global_reduce = 0; |
| 42 | pipeline_pooling_global_reduce_pack4 = 0; |
| 43 | pipeline_pooling_global_reduce_pack8 = 0; |
| 44 | pipeline_pooling_global_reduce_last = 0; |
| 45 | pipeline_pooling_global_reduce_last_pack4 = 0; |
| 46 | pipeline_pooling_global_reduce_last_pack8 = 0; |
| 47 | } |
| 48 | |
| 49 | int Pooling_vulkan::create_pipeline(const Option& _opt) |
| 50 | { |
| 51 | Option opt = _opt; |
| 52 | const Mat& shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0]; |
| 53 | const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| 54 | |
| 55 | // the shape after padding |
| 56 | Mat shape_bordered; |
| 57 | if (shape.dims != 0) |
| 58 | { |
| 59 | if (pad_mode == 0) |
| 60 | { |
| 61 | int wtail = (shape.w + pad_left + pad_right - kernel_w) % stride_w; |
| 62 | int htail = (shape.h + pad_top + pad_bottom - kernel_h) % stride_h; |
| 63 | |
| 64 | int wtailpad = 0; |
| 65 | int htailpad = 0; |
| 66 | if (wtail != 0) |
| 67 | wtailpad = stride_w - wtail; |
| 68 | if (htail != 0) |
| 69 | htailpad = stride_h - htail; |
| 70 | |
| 71 | shape_bordered = Mat(shape.w + pad_left + pad_right + wtailpad, shape.h + pad_top + pad_bottom + htailpad, shape.c, (void*)0); |
| 72 | } |
| 73 | else if (pad_mode == 1) |
| 74 | { |
| 75 | shape_bordered = Mat(shape.w + pad_left + pad_right, shape.h + pad_top + pad_bottom, shape.c, (void*)0); |
| 76 | } |
| 77 | else if (pad_mode == 2 || pad_mode == 3) |
| 78 | { |
| 79 | int wpad = kernel_w + (shape.w - 1) / stride_w * stride_w - shape.w; |
| 80 | int hpad = kernel_h + (shape.h - 1) / stride_h * stride_h - shape.h; |
| 81 | if (wpad > 0 || hpad > 0) |
| 82 | { |
| 83 | shape_bordered = Mat(shape.w + wpad, shape.h + hpad, shape.c, (void*)0); |
| 84 | } |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | shape_bordered = shape; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | int elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1; |
| 93 | int out_elempack = opt.use_shader_pack8 && out_shape.c % 8 == 0 ? 8 : out_shape.c % 4 == 0 ? 4 : 1; |
| 94 | |
| 95 | size_t elemsize; |
| 96 | size_t out_elemsize; |
| 97 | if (opt.use_fp16_storage) |
| 98 | { |
| 99 | elemsize = elempack * 2u; |
| 100 | out_elemsize = out_elempack * 2u; |
| 101 | } |
| 102 | else if (opt.use_fp16_packed) |
| 103 | { |
| 104 | elemsize = elempack == 1 ? 4u : elempack * 2u; |
| 105 | out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | elemsize = elempack * 4u; |
| 110 | out_elemsize = out_elempack * 4u; |
| 111 | } |
| 112 | |
| 113 | Mat shape_bordered_packed; |
| 114 | if (shape_bordered.dims == 1) shape_bordered_packed = Mat(shape_bordered.w / elempack, (void*)0, elemsize, elempack); |
| 115 | if (shape_bordered.dims == 2) shape_bordered_packed = Mat(shape_bordered.w, shape_bordered.h / elempack, (void*)0, elemsize, elempack); |
| 116 | if (shape_bordered.dims == 3) shape_bordered_packed = Mat(shape_bordered.w, shape_bordered.h, shape_bordered.c / elempack, (void*)0, elemsize, elempack); |
| 117 | |
| 118 | Mat out_shape_packed; |
| 119 | if (out_shape.dims == 1) out_shape_packed = Mat(out_shape.w / out_elempack, (void*)0, out_elemsize, out_elempack); |
| 120 | if (out_shape.dims == 2) out_shape_packed = Mat(out_shape.w, out_shape.h / out_elempack, (void*)0, out_elemsize, out_elempack); |
| 121 | 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); |
| 122 | |
| 123 | // check blob shape |
| 124 | if (!vkdev->shape_support_image_storage(shape_bordered_packed) || !vkdev->shape_support_image_storage(out_shape_packed)) |
| 125 | { |
| 126 | support_image_storage = false; |
| 127 | opt.use_image_storage = false; |
| 128 | } |
| 129 | |
| 130 | { |
| 131 | padding = ncnn::create_layer_vulkan(ncnn::LayerType::Padding); |
| 132 | padding->vkdev = vkdev; |
| 133 | |
| 134 | padding->bottom_shapes.resize(1); |
| 135 | padding->bottom_shapes[0] = shape; |
| 136 | padding->top_shapes.resize(1); |
| 137 | padding->top_shapes[0] = shape_bordered; |
| 138 | |
| 139 | ncnn::ParamDict pd; |
| 140 | pd.set(0, pad_top); |
| 141 | pd.set(1, pad_bottom); |
| 142 | pd.set(2, pad_left); |
| 143 | pd.set(3, pad_right); |
| 144 | pd.set(4, 0); |
| 145 | |
| 146 | if (pooling_type == PoolMethod_MAX) |
| 147 | { |
| 148 | // FLT_MAX becomes NaN during fp16 conversion in shader with swiftshader |
| 149 | // use a proper fp16-representable max as workaround --- nihui |
| 150 | if (opt.use_fp16_packed || opt.use_fp16_storage || opt.use_fp16_arithmetic) |
| 151 | pd.set(5, -65000.f); |
| 152 | else |
| 153 | pd.set(5, -FLT_MAX); |
| 154 | } |
| 155 | else if (pooling_type == PoolMethod_AVE) |
| 156 | { |
| 157 | pd.set(5, 0.f); |
| 158 | } |
| 159 | |
| 160 | padding->load_param(pd); |
| 161 | |
| 162 | padding->create_pipeline(opt); |
| 163 | } |
| 164 | |
| 165 | if (global_pooling) |
| 166 | { |
| 167 | // reduce first |
| 168 | { |
| 169 | std::vector<vk_specialization_type> specializations(6); |
| 170 | specializations[0].i = shape_bordered_packed.w; |
| 171 | specializations[1].i = shape_bordered_packed.h; |
| 172 | specializations[2].i = shape_bordered_packed.c; |
| 173 | specializations[3].i = shape_bordered_packed.cstep; |
| 174 | specializations[4].i = 0; |
| 175 | specializations[5].i = 0; |
| 176 | |
| 177 | Mat local_size_xyz(64, 1, 1, (void*)0); |
| 178 | |
| 179 | // pack1 |
| 180 | if (shape.dims == 0 || elempack == 1) |
| 181 | { |
| 182 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_first : LayerShaderType::pooling_global_reduce_sum_first; |
| 183 | |
| 184 | pipeline_pooling_global_reduce_first = new Pipeline(vkdev); |
| 185 | pipeline_pooling_global_reduce_first->set_optimal_local_size_xyz(local_size_xyz); |
| 186 | pipeline_pooling_global_reduce_first->create(layer_shader_type, opt, specializations); |
| 187 | } |
| 188 | |
| 189 | // pack4 |
| 190 | if (shape.dims == 0 || elempack == 4) |
| 191 | { |
| 192 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_first_pack4 : LayerShaderType::pooling_global_reduce_sum_first_pack4; |
| 193 | |
| 194 | pipeline_pooling_global_reduce_first_pack4 = new Pipeline(vkdev); |
| 195 | pipeline_pooling_global_reduce_first_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 196 | pipeline_pooling_global_reduce_first_pack4->create(layer_shader_type, opt, specializations); |
| 197 | } |
| 198 | |
| 199 | // pack8 |
| 200 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 201 | { |
| 202 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_first_pack8 : LayerShaderType::pooling_global_reduce_sum_first_pack8; |
| 203 | |
| 204 | pipeline_pooling_global_reduce_first_pack8 = new Pipeline(vkdev); |
| 205 | pipeline_pooling_global_reduce_first_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 206 | pipeline_pooling_global_reduce_first_pack8->create(layer_shader_type, opt, specializations); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // reduce more |
| 211 | { |
| 212 | std::vector<vk_specialization_type> specializations(5); |
| 213 | specializations[0].i = 0; |
| 214 | specializations[1].i = shape_bordered_packed.c; |
| 215 | specializations[2].i = 0; |
| 216 | specializations[3].i = 0; |
| 217 | specializations[4].i = 0; |
| 218 | |
| 219 | Mat local_size_xyz(64, 1, 1, (void*)0); |
| 220 | |
| 221 | // pack1 |
| 222 | if (shape.dims == 0 || elempack == 1) |
| 223 | { |
| 224 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max : LayerShaderType::pooling_global_reduce_sum; |
| 225 | |
| 226 | pipeline_pooling_global_reduce = new Pipeline(vkdev); |
| 227 | pipeline_pooling_global_reduce->set_optimal_local_size_xyz(local_size_xyz); |
| 228 | pipeline_pooling_global_reduce->create(layer_shader_type, opt, specializations); |
| 229 | } |
| 230 | |
| 231 | // pack4 |
| 232 | if (shape.dims == 0 || elempack == 4) |
| 233 | { |
| 234 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_pack4 : LayerShaderType::pooling_global_reduce_sum_pack4; |
| 235 | |
| 236 | pipeline_pooling_global_reduce_pack4 = new Pipeline(vkdev); |
| 237 | pipeline_pooling_global_reduce_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 238 | pipeline_pooling_global_reduce_pack4->create(layer_shader_type, opt, specializations); |
| 239 | } |
| 240 | |
| 241 | // pack8 |
| 242 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 243 | { |
| 244 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_pack8 : LayerShaderType::pooling_global_reduce_sum_pack8; |
| 245 | |
| 246 | pipeline_pooling_global_reduce_pack8 = new Pipeline(vkdev); |
| 247 | pipeline_pooling_global_reduce_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 248 | pipeline_pooling_global_reduce_pack8->create(layer_shader_type, opt, specializations); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // reduce last |
| 253 | { |
| 254 | std::vector<vk_specialization_type> specializations(3); |
| 255 | specializations[0].i = 0; |
| 256 | specializations[1].i = shape_bordered_packed.c; |
| 257 | specializations[2].i = 0; |
| 258 | |
| 259 | Mat local_size_xyz(1, 1, 64, (void*)0); |
| 260 | |
| 261 | // pack1 |
| 262 | if (shape.dims == 0 || elempack == 1) |
| 263 | { |
| 264 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_last : LayerShaderType::pooling_global_reduce_sum_last; |
| 265 | |
| 266 | pipeline_pooling_global_reduce_last = new Pipeline(vkdev); |
| 267 | pipeline_pooling_global_reduce_last->set_optimal_local_size_xyz(local_size_xyz); |
| 268 | pipeline_pooling_global_reduce_last->create(layer_shader_type, opt, specializations); |
| 269 | } |
| 270 | |
| 271 | // pack4 |
| 272 | if (shape.dims == 0 || elempack == 4) |
| 273 | { |
| 274 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_last_pack4 : LayerShaderType::pooling_global_reduce_sum_last_pack4; |
| 275 | |
| 276 | pipeline_pooling_global_reduce_last_pack4 = new Pipeline(vkdev); |
| 277 | pipeline_pooling_global_reduce_last_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 278 | pipeline_pooling_global_reduce_last_pack4->create(layer_shader_type, opt, specializations); |
| 279 | } |
| 280 | |
| 281 | // pack8 |
| 282 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 283 | { |
| 284 | int layer_shader_type = pooling_type == 0 ? LayerShaderType::pooling_global_reduce_max_last_pack8 : LayerShaderType::pooling_global_reduce_sum_last_pack8; |
| 285 | |
| 286 | pipeline_pooling_global_reduce_last_pack8 = new Pipeline(vkdev); |
| 287 | pipeline_pooling_global_reduce_last_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 288 | pipeline_pooling_global_reduce_last_pack8->create(layer_shader_type, opt, specializations); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | else if (adaptive_pooling) |
| 293 | { |
| 294 | std::vector<vk_specialization_type> specializations(1 + 10); |
| 295 | specializations[0].i = pooling_type; |
| 296 | specializations[1 + 0].i = shape_bordered_packed.dims; |
| 297 | specializations[1 + 1].i = shape_bordered_packed.w; |
| 298 | specializations[1 + 2].i = shape_bordered_packed.h; |
| 299 | specializations[1 + 3].i = shape_bordered_packed.c; |
| 300 | specializations[1 + 4].i = shape_bordered_packed.cstep; |
| 301 | specializations[1 + 5].i = out_shape_packed.dims; |
| 302 | specializations[1 + 6].i = out_shape_packed.w; |
| 303 | specializations[1 + 7].i = out_shape_packed.h; |
| 304 | specializations[1 + 8].i = out_shape_packed.c; |
| 305 | specializations[1 + 9].i = out_shape_packed.cstep; |
| 306 | |
| 307 | Mat local_size_xyz; |
| 308 | if (out_shape_packed.dims != 0) |
| 309 | { |
| 310 | local_size_xyz.w = std::min(4, out_shape_packed.w); |
| 311 | local_size_xyz.h = std::min(4, out_shape_packed.h); |
| 312 | local_size_xyz.c = std::min(4, out_shape_packed.c); |
| 313 | } |
| 314 | |
| 315 | // pack1 |
| 316 | if (shape.dims == 0 || elempack == 1) |
| 317 | { |
| 318 | pipeline_pooling_adaptive = new Pipeline(vkdev); |
| 319 | pipeline_pooling_adaptive->set_optimal_local_size_xyz(local_size_xyz); |
| 320 | pipeline_pooling_adaptive->create(LayerShaderType::pooling_adaptive, opt, specializations); |
| 321 | } |
| 322 | |
| 323 | // pack4 |
| 324 | if (shape.dims == 0 || elempack == 4) |
| 325 | { |
| 326 | pipeline_pooling_adaptive_pack4 = new Pipeline(vkdev); |
| 327 | pipeline_pooling_adaptive_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 328 | pipeline_pooling_adaptive_pack4->create(LayerShaderType::pooling_adaptive_pack4, opt, specializations); |
| 329 | } |
| 330 | |
| 331 | // pack8 |
| 332 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 333 | { |
| 334 | pipeline_pooling_adaptive_pack8 = new Pipeline(vkdev); |
| 335 | pipeline_pooling_adaptive_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 336 | pipeline_pooling_adaptive_pack8->create(LayerShaderType::pooling_adaptive_pack8, opt, specializations); |
| 337 | } |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | std::vector<vk_specialization_type> specializations(12 + 10); |
| 342 | specializations[0].i = pooling_type; |
| 343 | specializations[1].i = kernel_w; |
| 344 | specializations[2].i = kernel_h; |
| 345 | specializations[3].i = stride_w; |
| 346 | specializations[4].i = stride_h; |
| 347 | specializations[5].i = pad_left; |
| 348 | specializations[6].i = pad_right; |
| 349 | specializations[7].i = pad_top; |
| 350 | specializations[8].i = pad_bottom; |
| 351 | specializations[9].i = global_pooling; |
| 352 | specializations[10].i = pad_mode; |
| 353 | specializations[11].i = avgpool_count_include_pad; |
| 354 | specializations[12 + 0].i = shape_bordered_packed.dims; |
| 355 | specializations[12 + 1].i = shape_bordered_packed.w; |
| 356 | specializations[12 + 2].i = shape_bordered_packed.h; |
| 357 | specializations[12 + 3].i = shape_bordered_packed.c; |
| 358 | specializations[12 + 4].i = shape_bordered_packed.cstep; |
| 359 | specializations[12 + 5].i = out_shape_packed.dims; |
| 360 | specializations[12 + 6].i = out_shape_packed.w; |
| 361 | specializations[12 + 7].i = out_shape_packed.h; |
| 362 | specializations[12 + 8].i = out_shape_packed.c; |
| 363 | specializations[12 + 9].i = out_shape_packed.cstep; |
| 364 | |
| 365 | Mat local_size_xyz; |
| 366 | if (out_shape_packed.dims != 0) |
| 367 | { |
| 368 | local_size_xyz.w = std::min(4, out_shape_packed.w); |
| 369 | local_size_xyz.h = std::min(4, out_shape_packed.h); |
| 370 | local_size_xyz.c = std::min(4, out_shape_packed.c); |
| 371 | } |
| 372 | |
| 373 | // pack1 |
| 374 | if (shape.dims == 0 || elempack == 1) |
| 375 | { |
| 376 | pipeline_pooling = new Pipeline(vkdev); |
| 377 | pipeline_pooling->set_optimal_local_size_xyz(local_size_xyz); |
| 378 | pipeline_pooling->create(LayerShaderType::pooling, opt, specializations); |
| 379 | } |
| 380 | |
| 381 | // pack4 |
| 382 | if (shape.dims == 0 || elempack == 4) |
| 383 | { |
| 384 | pipeline_pooling_pack4 = new Pipeline(vkdev); |
| 385 | pipeline_pooling_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 386 | pipeline_pooling_pack4->create(LayerShaderType::pooling_pack4, opt, specializations); |
| 387 | } |
| 388 | |
| 389 | // pack8 |
| 390 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 391 | { |
| 392 | pipeline_pooling_pack8 = new Pipeline(vkdev); |
| 393 | pipeline_pooling_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 394 | pipeline_pooling_pack8->create(LayerShaderType::pooling_pack8, opt, specializations); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | int Pooling_vulkan::destroy_pipeline(const Option& _opt) |
| 402 | { |
| 403 | Option opt = _opt; |
| 404 | opt.use_image_storage = support_image_storage; |
| 405 | |
| 406 | if (padding) |
| 407 | { |
| 408 | padding->destroy_pipeline(opt); |
| 409 | delete padding; |
| 410 | padding = 0; |
| 411 | } |
| 412 | |
| 413 | delete pipeline_pooling; |
| 414 | pipeline_pooling = 0; |
| 415 | |
| 416 | delete pipeline_pooling_pack4; |
| 417 | pipeline_pooling_pack4 = 0; |
| 418 | |
| 419 | delete pipeline_pooling_pack8; |
| 420 | pipeline_pooling_pack8 = 0; |
| 421 | |
| 422 | delete pipeline_pooling_adaptive; |
| 423 | pipeline_pooling_adaptive = 0; |
| 424 | |
| 425 | delete pipeline_pooling_adaptive_pack4; |
| 426 | pipeline_pooling_adaptive_pack4 = 0; |
| 427 | |
| 428 | delete pipeline_pooling_adaptive_pack8; |
| 429 | pipeline_pooling_adaptive_pack8 = 0; |
| 430 | |
| 431 | delete pipeline_pooling_global_reduce_first; |
| 432 | pipeline_pooling_global_reduce_first = 0; |
| 433 | |
| 434 | delete pipeline_pooling_global_reduce_first_pack4; |
| 435 | pipeline_pooling_global_reduce_first_pack4 = 0; |
| 436 | |
| 437 | delete pipeline_pooling_global_reduce_first_pack8; |
| 438 | pipeline_pooling_global_reduce_first_pack8 = 0; |
| 439 | |
| 440 | delete pipeline_pooling_global_reduce; |
| 441 | pipeline_pooling_global_reduce = 0; |
| 442 | |
| 443 | delete pipeline_pooling_global_reduce_pack4; |
| 444 | pipeline_pooling_global_reduce_pack4 = 0; |
| 445 | |
| 446 | delete pipeline_pooling_global_reduce_pack8; |
| 447 | pipeline_pooling_global_reduce_pack8 = 0; |
| 448 | |
| 449 | delete pipeline_pooling_global_reduce_last; |
| 450 | pipeline_pooling_global_reduce_last = 0; |
| 451 | |
| 452 | delete pipeline_pooling_global_reduce_last_pack4; |
| 453 | pipeline_pooling_global_reduce_last_pack4 = 0; |
| 454 | |
| 455 | delete pipeline_pooling_global_reduce_last_pack8; |
| 456 | pipeline_pooling_global_reduce_last_pack8 = 0; |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | int Pooling_vulkan::upload_model(VkTransfer& cmd, const Option& opt) |
| 462 | { |
| 463 | if (padding) |
| 464 | { |
| 465 | padding->upload_model(cmd, opt); |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int Pooling_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 472 | { |
| 473 | int w = bottom_blob.w; |
| 474 | int h = bottom_blob.h; |
| 475 | int channels = bottom_blob.c; |
| 476 | size_t elemsize = bottom_blob.elemsize; |
| 477 | int elempack = bottom_blob.elempack; |
| 478 | |
| 479 | if (global_pooling) |
| 480 | { |
| 481 | // reduce first |
| 482 | VkMat reduced_blob; |
| 483 | { |
| 484 | int reduced_size = (w * h + 7) / 8; |
| 485 | size_t reduced_elemsize = pooling_type == 0 ? elemsize : 4u * elempack; |
| 486 | reduced_blob.create(reduced_size, 1, channels, reduced_elemsize, elempack, opt.workspace_vkallocator); |
| 487 | if (reduced_blob.empty()) |
| 488 | return -100; |
| 489 | |
| 490 | std::vector<VkMat> bindings(2); |
| 491 | bindings[0] = bottom_blob; |
| 492 | bindings[1] = reduced_blob; |
| 493 | |
| 494 | std::vector<vk_constant_type> constants(6); |
| 495 | constants[0].i = bottom_blob.w; |
| 496 | constants[1].i = bottom_blob.h; |
| 497 | constants[2].i = bottom_blob.c; |
| 498 | constants[3].i = bottom_blob.cstep; |
| 499 | constants[4].i = reduced_blob.w; |
| 500 | constants[5].i = reduced_blob.cstep; |
| 501 | |
| 502 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_global_reduce_first_pack8 |
| 503 | : elempack == 4 ? pipeline_pooling_global_reduce_first_pack4 |
| 504 | : pipeline_pooling_global_reduce_first; |
| 505 | |
| 506 | VkMat dispatcher; |
| 507 | dispatcher.w = reduced_blob.w; |
| 508 | dispatcher.h = 1; |
| 509 | dispatcher.c = bottom_blob.c; |
| 510 | |
| 511 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 512 | } |
| 513 | |
| 514 | // reduce more |
| 515 | while (reduced_blob.w > 32) |
| 516 | { |
| 517 | int reduced_size = (reduced_blob.w + 7) / 8; |
| 518 | size_t reduced_elemsize = pooling_type == 0 ? elemsize : 4u * elempack; |
| 519 | VkMat reduced_blob2; |
| 520 | reduced_blob2.create(reduced_size, 1, channels, reduced_elemsize, elempack, opt.workspace_vkallocator); |
| 521 | if (reduced_blob2.empty()) |
| 522 | return -100; |
| 523 | |
| 524 | std::vector<VkMat> bindings(2); |
| 525 | bindings[0] = reduced_blob; |
| 526 | bindings[1] = reduced_blob2; |
| 527 | |
| 528 | std::vector<vk_constant_type> constants(5); |
| 529 | constants[0].i = reduced_blob.w; |
| 530 | constants[1].i = reduced_blob.c; |
| 531 | constants[2].i = reduced_blob.cstep; |
| 532 | constants[3].i = reduced_blob2.w; |
| 533 | constants[4].i = reduced_blob2.cstep; |
| 534 | |
| 535 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_global_reduce_pack8 |
| 536 | : elempack == 4 ? pipeline_pooling_global_reduce_pack4 |
| 537 | : pipeline_pooling_global_reduce; |
| 538 | |
| 539 | VkMat dispatcher; |
| 540 | dispatcher.w = reduced_blob2.w; |
| 541 | dispatcher.h = 1; |
| 542 | dispatcher.c = reduced_blob2.c; |
| 543 | |
| 544 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 545 | |
| 546 | reduced_blob = reduced_blob2; |
| 547 | } |
| 548 | |
| 549 | // reduce last |
| 550 | { |
| 551 | top_blob.create(channels, elemsize, elempack, opt.blob_vkallocator); |
| 552 | if (top_blob.empty()) |
| 553 | return -100; |
| 554 | |
| 555 | std::vector<VkMat> bindings(2); |
| 556 | bindings[0] = reduced_blob; |
| 557 | bindings[1] = top_blob; |
| 558 | |
| 559 | std::vector<vk_constant_type> constants(4); |
| 560 | constants[0].i = reduced_blob.w; |
| 561 | constants[1].i = reduced_blob.c; |
| 562 | constants[2].i = reduced_blob.cstep; |
| 563 | constants[3].i = w * h; |
| 564 | |
| 565 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_global_reduce_last_pack8 |
| 566 | : elempack == 4 ? pipeline_pooling_global_reduce_last_pack4 |
| 567 | : pipeline_pooling_global_reduce_last; |
| 568 | |
| 569 | VkMat dispatcher; |
| 570 | dispatcher.w = 1; |
| 571 | dispatcher.h = 1; |
| 572 | dispatcher.c = top_blob.w; |
| 573 | |
| 574 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 575 | } |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | if (adaptive_pooling) |
| 581 | { |
| 582 | int _out_w = out_w == -233 ? w : out_w; |
| 583 | int _out_h = out_h == -233 ? h : out_h; |
| 584 | |
| 585 | if (_out_w == w && _out_h == h) |
| 586 | { |
| 587 | top_blob = bottom_blob; |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | top_blob.create(_out_w, _out_h, channels, elemsize, elempack, opt.blob_vkallocator); |
| 592 | if (top_blob.empty()) |
| 593 | return -100; |
| 594 | |
| 595 | std::vector<VkMat> bindings(2); |
| 596 | bindings[0] = bottom_blob; |
| 597 | bindings[1] = top_blob; |
| 598 | |
| 599 | std::vector<vk_constant_type> constants(10); |
| 600 | constants[0].i = bottom_blob.dims; |
| 601 | constants[1].i = bottom_blob.w; |
| 602 | constants[2].i = bottom_blob.h; |
| 603 | constants[3].i = bottom_blob.c; |
| 604 | constants[4].i = bottom_blob.cstep; |
| 605 | constants[5].i = top_blob.dims; |
| 606 | constants[6].i = top_blob.w; |
| 607 | constants[7].i = top_blob.h; |
| 608 | constants[8].i = top_blob.c; |
| 609 | constants[9].i = top_blob.cstep; |
| 610 | |
| 611 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_adaptive_pack8 |
| 612 | : elempack == 4 ? pipeline_pooling_adaptive_pack4 |
| 613 | : pipeline_pooling_adaptive; |
| 614 | |
| 615 | cmd.record_pipeline(pipeline, bindings, constants, top_blob); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | VkMat bottom_blob_bordered = bottom_blob; |
| 621 | |
| 622 | int wtailpad = 0; |
| 623 | int htailpad = 0; |
| 624 | |
| 625 | if (pad_mode == 0) // full padding |
| 626 | { |
| 627 | int wtail = (w + pad_left + pad_right - kernel_w) % stride_w; |
| 628 | int htail = (h + pad_top + pad_bottom - kernel_h) % stride_h; |
| 629 | |
| 630 | if (wtail != 0) |
| 631 | wtailpad = stride_w - wtail; |
| 632 | if (htail != 0) |
| 633 | htailpad = stride_h - htail; |
| 634 | |
| 635 | Option opt_pad = opt; |
| 636 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 637 | |
| 638 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 639 | int* padding_params = padding_param_blob.mapped(); |
| 640 | |
| 641 | padding_params[0] = pad_top; |
| 642 | padding_params[1] = pad_bottom + htailpad; |
| 643 | padding_params[2] = pad_left; |
| 644 | padding_params[3] = pad_right + wtailpad; |
| 645 | padding_params[4] = 0; |
| 646 | padding_params[5] = 0; |
| 647 | |
| 648 | std::vector<VkMat> padding_inputs(2); |
| 649 | padding_inputs[0] = bottom_blob; |
| 650 | padding_inputs[1] = padding_param_blob; |
| 651 | |
| 652 | std::vector<VkMat> padding_outputs(1); |
| 653 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 654 | bottom_blob_bordered = padding_outputs[0]; |
| 655 | } |
| 656 | else if (pad_mode == 1) // valid padding |
| 657 | { |
| 658 | Option opt_pad = opt; |
| 659 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 660 | |
| 661 | padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad); |
| 662 | } |
| 663 | else if (pad_mode == 2) // tensorflow padding=SAME or onnx padding=SAME_UPPER |
| 664 | { |
| 665 | int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; |
| 666 | int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; |
| 667 | if (wpad > 0 || hpad > 0) |
| 668 | { |
| 669 | Option opt_pad = opt; |
| 670 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 671 | |
| 672 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 673 | int* padding_params = padding_param_blob.mapped(); |
| 674 | |
| 675 | padding_params[0] = hpad / 2; |
| 676 | padding_params[1] = hpad - hpad / 2; |
| 677 | padding_params[2] = wpad / 2; |
| 678 | padding_params[3] = wpad - wpad / 2; |
| 679 | padding_params[4] = 0; |
| 680 | padding_params[5] = 0; |
| 681 | |
| 682 | std::vector<VkMat> padding_inputs(2); |
| 683 | padding_inputs[0] = bottom_blob; |
| 684 | padding_inputs[1] = padding_param_blob; |
| 685 | |
| 686 | std::vector<VkMat> padding_outputs(1); |
| 687 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 688 | bottom_blob_bordered = padding_outputs[0]; |
| 689 | } |
| 690 | } |
| 691 | else if (pad_mode == 3) // onnx padding=SAME_LOWER |
| 692 | { |
| 693 | int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; |
| 694 | int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; |
| 695 | if (wpad > 0 || hpad > 0) |
| 696 | { |
| 697 | Option opt_pad = opt; |
| 698 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 699 | |
| 700 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 701 | int* padding_params = padding_param_blob.mapped(); |
| 702 | |
| 703 | padding_params[0] = hpad - hpad / 2; |
| 704 | padding_params[1] = hpad / 2; |
| 705 | padding_params[2] = wpad - wpad / 2; |
| 706 | padding_params[3] = wpad / 2; |
| 707 | padding_params[4] = 0; |
| 708 | padding_params[5] = 0; |
| 709 | |
| 710 | std::vector<VkMat> padding_inputs(2); |
| 711 | padding_inputs[0] = bottom_blob; |
| 712 | padding_inputs[1] = padding_param_blob; |
| 713 | |
| 714 | std::vector<VkMat> padding_outputs(1); |
| 715 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 716 | bottom_blob_bordered = padding_outputs[0]; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | w = bottom_blob_bordered.w; |
| 721 | h = bottom_blob_bordered.h; |
| 722 | |
| 723 | int outw = (w - kernel_w) / stride_w + 1; |
| 724 | int outh = (h - kernel_h) / stride_h + 1; |
| 725 | |
| 726 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_vkallocator); |
| 727 | if (top_blob.empty()) |
| 728 | return -100; |
| 729 | |
| 730 | std::vector<VkMat> bindings(2); |
| 731 | bindings[0] = bottom_blob_bordered; |
| 732 | bindings[1] = top_blob; |
| 733 | |
| 734 | std::vector<vk_constant_type> constants(12); |
| 735 | constants[0].i = bottom_blob_bordered.dims; |
| 736 | constants[1].i = bottom_blob_bordered.w; |
| 737 | constants[2].i = bottom_blob_bordered.h; |
| 738 | constants[3].i = bottom_blob_bordered.c; |
| 739 | constants[4].i = bottom_blob_bordered.cstep; |
| 740 | constants[5].i = top_blob.dims; |
| 741 | constants[6].i = top_blob.w; |
| 742 | constants[7].i = top_blob.h; |
| 743 | constants[8].i = top_blob.c; |
| 744 | constants[9].i = top_blob.cstep; |
| 745 | constants[10].i = wtailpad; |
| 746 | constants[11].i = htailpad; |
| 747 | |
| 748 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_pack8 |
| 749 | : elempack == 4 ? pipeline_pooling_pack4 |
| 750 | : pipeline_pooling; |
| 751 | |
| 752 | cmd.record_pipeline(pipeline, bindings, constants, top_blob); |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | int Pooling_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 758 | { |
| 759 | int w = bottom_blob.w; |
| 760 | int h = bottom_blob.h; |
| 761 | int channels = bottom_blob.c; |
| 762 | size_t elemsize = bottom_blob.elemsize; |
| 763 | int elempack = bottom_blob.elempack; |
| 764 | |
| 765 | if (global_pooling) |
| 766 | { |
| 767 | // reduce first |
| 768 | VkImageMat reduced_blob; |
| 769 | { |
| 770 | int reduced_size = (w * h + 7) / 8; |
| 771 | size_t reduced_elemsize = pooling_type == 0 ? elemsize : 4u * elempack; |
| 772 | reduced_blob.create(reduced_size, 1, channels, reduced_elemsize, elempack, opt.workspace_vkallocator); |
| 773 | if (reduced_blob.empty()) |
| 774 | return -100; |
| 775 | |
| 776 | std::vector<VkImageMat> bindings(2); |
| 777 | bindings[0] = bottom_blob; |
| 778 | bindings[1] = reduced_blob; |
| 779 | |
| 780 | std::vector<vk_constant_type> constants(6); |
| 781 | constants[0].i = bottom_blob.w; |
| 782 | constants[1].i = bottom_blob.h; |
| 783 | constants[2].i = bottom_blob.c; |
| 784 | constants[3].i = 0; //bottom_blob.cstep; |
| 785 | constants[4].i = reduced_blob.w; |
| 786 | constants[5].i = 0; //reduced_blob.cstep; |
| 787 | |
| 788 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_global_reduce_first_pack8 |
| 789 | : elempack == 4 ? pipeline_pooling_global_reduce_first_pack4 |
| 790 | : pipeline_pooling_global_reduce_first; |
| 791 | |
| 792 | VkImageMat dispatcher; |
| 793 | dispatcher.w = reduced_blob.w; |
| 794 | dispatcher.h = 1; |
| 795 | dispatcher.c = bottom_blob.c; |
| 796 | |
| 797 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 798 | } |
| 799 | |
| 800 | // reduce more |
| 801 | while (reduced_blob.w > 32) |
| 802 | { |
| 803 | int reduced_size = (reduced_blob.w + 7) / 8; |
| 804 | size_t reduced_elemsize = pooling_type == 0 ? elemsize : 4u * elempack; |
| 805 | VkImageMat reduced_blob2; |
| 806 | reduced_blob2.create(reduced_size, 1, channels, reduced_elemsize, elempack, opt.workspace_vkallocator); |
| 807 | if (reduced_blob2.empty()) |
| 808 | return -100; |
| 809 | |
| 810 | std::vector<VkImageMat> bindings(2); |
| 811 | bindings[0] = reduced_blob; |
| 812 | bindings[1] = reduced_blob2; |
| 813 | |
| 814 | std::vector<vk_constant_type> constants(5); |
| 815 | constants[0].i = reduced_blob.w; |
| 816 | constants[1].i = reduced_blob.c; |
| 817 | constants[2].i = 0; //reduced_blob.cstep; |
| 818 | constants[3].i = reduced_blob2.w; |
| 819 | constants[4].i = 0; //reduced_blob2.cstep; |
| 820 | |
| 821 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_global_reduce_pack8 |
| 822 | : elempack == 4 ? pipeline_pooling_global_reduce_pack4 |
| 823 | : pipeline_pooling_global_reduce; |
| 824 | |
| 825 | VkImageMat dispatcher; |
| 826 | dispatcher.w = reduced_blob2.w; |
| 827 | dispatcher.h = 1; |
| 828 | dispatcher.c = reduced_blob2.c; |
| 829 | |
| 830 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 831 | |
| 832 | reduced_blob = reduced_blob2; |
| 833 | } |
| 834 | |
| 835 | // reduce last |
| 836 | { |
| 837 | top_blob.create(channels, elemsize, elempack, opt.blob_vkallocator); |
| 838 | if (top_blob.empty()) |
| 839 | return -100; |
| 840 | |
| 841 | std::vector<VkImageMat> bindings(2); |
| 842 | bindings[0] = reduced_blob; |
| 843 | bindings[1] = top_blob; |
| 844 | |
| 845 | std::vector<vk_constant_type> constants(4); |
| 846 | constants[0].i = reduced_blob.w; |
| 847 | constants[1].i = reduced_blob.c; |
| 848 | constants[2].i = 0; //reduced_blob.cstep; |
| 849 | constants[3].i = w * h; |
| 850 | |
| 851 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_global_reduce_last_pack8 |
| 852 | : elempack == 4 ? pipeline_pooling_global_reduce_last_pack4 |
| 853 | : pipeline_pooling_global_reduce_last; |
| 854 | |
| 855 | VkImageMat dispatcher; |
| 856 | dispatcher.w = 1; |
| 857 | dispatcher.h = 1; |
| 858 | dispatcher.c = top_blob.w; |
| 859 | |
| 860 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | if (adaptive_pooling) |
| 867 | { |
| 868 | int _out_w = out_w == -233 ? w : out_w; |
| 869 | int _out_h = out_h == -233 ? h : out_h; |
| 870 | |
| 871 | if (_out_w == w && _out_h == h) |
| 872 | { |
| 873 | top_blob = bottom_blob; |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | top_blob.create(_out_w, _out_h, channels, elemsize, elempack, opt.blob_vkallocator); |
| 878 | if (top_blob.empty()) |
| 879 | return -100; |
| 880 | |
| 881 | std::vector<VkImageMat> bindings(2); |
| 882 | bindings[0] = bottom_blob; |
| 883 | bindings[1] = top_blob; |
| 884 | |
| 885 | std::vector<vk_constant_type> constants(10); |
| 886 | constants[0].i = bottom_blob.dims; |
| 887 | constants[1].i = bottom_blob.w; |
| 888 | constants[2].i = bottom_blob.h; |
| 889 | constants[3].i = bottom_blob.c; |
| 890 | constants[4].i = 0; //bottom_blob.cstep; |
| 891 | constants[5].i = top_blob.dims; |
| 892 | constants[6].i = top_blob.w; |
| 893 | constants[7].i = top_blob.h; |
| 894 | constants[8].i = top_blob.c; |
| 895 | constants[9].i = 0; //top_blob.cstep; |
| 896 | |
| 897 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_adaptive_pack8 |
| 898 | : elempack == 4 ? pipeline_pooling_adaptive_pack4 |
| 899 | : pipeline_pooling_adaptive; |
| 900 | |
| 901 | cmd.record_pipeline(pipeline, bindings, constants, top_blob); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | VkImageMat bottom_blob_bordered = bottom_blob; |
| 907 | |
| 908 | int wtailpad = 0; |
| 909 | int htailpad = 0; |
| 910 | |
| 911 | if (pad_mode == 0) // full padding |
| 912 | { |
| 913 | int wtail = (w + pad_left + pad_right - kernel_w) % stride_w; |
| 914 | int htail = (h + pad_top + pad_bottom - kernel_h) % stride_h; |
| 915 | |
| 916 | if (wtail != 0) |
| 917 | wtailpad = stride_w - wtail; |
| 918 | if (htail != 0) |
| 919 | htailpad = stride_h - htail; |
| 920 | |
| 921 | Option opt_pad = opt; |
| 922 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 923 | |
| 924 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 925 | int* padding_params = padding_param_blob.mapped(); |
| 926 | |
| 927 | padding_params[0] = pad_top; |
| 928 | padding_params[1] = pad_bottom + htailpad; |
| 929 | padding_params[2] = pad_left; |
| 930 | padding_params[3] = pad_right + wtailpad; |
| 931 | padding_params[4] = 0; |
| 932 | padding_params[5] = 0; |
| 933 | |
| 934 | std::vector<VkImageMat> padding_inputs(2); |
| 935 | padding_inputs[0] = bottom_blob; |
| 936 | padding_inputs[1] = padding_param_blob; |
| 937 | |
| 938 | std::vector<VkImageMat> padding_outputs(1); |
| 939 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 940 | bottom_blob_bordered = padding_outputs[0]; |
| 941 | } |
| 942 | else if (pad_mode == 1) // valid padding |
| 943 | { |
| 944 | Option opt_pad = opt; |
| 945 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 946 | |
| 947 | padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad); |
| 948 | } |
| 949 | else if (pad_mode == 2) // tensorflow padding=SAME or onnx padding=SAME_UPPER |
| 950 | { |
| 951 | int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; |
| 952 | int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; |
| 953 | if (wpad > 0 || hpad > 0) |
| 954 | { |
| 955 | Option opt_pad = opt; |
| 956 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 957 | |
| 958 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 959 | int* padding_params = padding_param_blob.mapped(); |
| 960 | |
| 961 | padding_params[0] = hpad / 2; |
| 962 | padding_params[1] = hpad - hpad / 2; |
| 963 | padding_params[2] = wpad / 2; |
| 964 | padding_params[3] = wpad - wpad / 2; |
| 965 | padding_params[4] = 0; |
| 966 | padding_params[5] = 0; |
| 967 | |
| 968 | std::vector<VkImageMat> padding_inputs(2); |
| 969 | padding_inputs[0] = bottom_blob; |
| 970 | padding_inputs[1] = padding_param_blob; |
| 971 | |
| 972 | std::vector<VkImageMat> padding_outputs(1); |
| 973 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 974 | bottom_blob_bordered = padding_outputs[0]; |
| 975 | } |
| 976 | } |
| 977 | else if (pad_mode == 3) // onnx padding=SAME_LOWER |
| 978 | { |
| 979 | int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; |
| 980 | int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; |
| 981 | if (wpad > 0 || hpad > 0) |
| 982 | { |
| 983 | Option opt_pad = opt; |
| 984 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 985 | |
| 986 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 987 | int* padding_params = padding_param_blob.mapped(); |
| 988 | |
| 989 | padding_params[0] = hpad - hpad / 2; |
| 990 | padding_params[1] = hpad / 2; |
| 991 | padding_params[2] = wpad - wpad / 2; |
| 992 | padding_params[3] = wpad / 2; |
| 993 | padding_params[4] = 0; |
| 994 | padding_params[5] = 0; |
| 995 | |
| 996 | std::vector<VkImageMat> padding_inputs(2); |
| 997 | padding_inputs[0] = bottom_blob; |
| 998 | padding_inputs[1] = padding_param_blob; |
| 999 | |
| 1000 | std::vector<VkImageMat> padding_outputs(1); |
| 1001 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 1002 | bottom_blob_bordered = padding_outputs[0]; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | w = bottom_blob_bordered.w; |
| 1007 | h = bottom_blob_bordered.h; |
| 1008 | |
| 1009 | int outw = (w - kernel_w) / stride_w + 1; |
| 1010 | int outh = (h - kernel_h) / stride_h + 1; |
| 1011 | |
| 1012 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_vkallocator); |
| 1013 | if (top_blob.empty()) |
| 1014 | return -100; |
| 1015 | |
| 1016 | std::vector<VkImageMat> bindings(2); |
| 1017 | bindings[0] = bottom_blob_bordered; |
| 1018 | bindings[1] = top_blob; |
| 1019 | |
| 1020 | std::vector<vk_constant_type> constants(12); |
| 1021 | constants[0].i = bottom_blob_bordered.dims; |
| 1022 | constants[1].i = bottom_blob_bordered.w; |
| 1023 | constants[2].i = bottom_blob_bordered.h; |
| 1024 | constants[3].i = bottom_blob_bordered.c; |
| 1025 | constants[4].i = 0; //bottom_blob_bordered.cstep; |
| 1026 | constants[5].i = top_blob.dims; |
| 1027 | constants[6].i = top_blob.w; |
| 1028 | constants[7].i = top_blob.h; |
| 1029 | constants[8].i = top_blob.c; |
| 1030 | constants[9].i = 0; //top_blob.cstep; |
| 1031 | constants[10].i = wtailpad; |
| 1032 | constants[11].i = htailpad; |
| 1033 | |
| 1034 | const Pipeline* pipeline = elempack == 8 ? pipeline_pooling_pack8 |
| 1035 | : elempack == 4 ? pipeline_pooling_pack4 |
| 1036 | : pipeline_pooling; |
| 1037 | |
| 1038 | cmd.record_pipeline(pipeline, bindings, constants, top_blob); |
| 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | } // namespace ncnn |