| 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 "normalize_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | Normalize_vulkan::Normalize_vulkan() |
| 22 | { |
| 23 | support_vulkan = true; |
| 24 | support_image_storage = true; |
| 25 | |
| 26 | pipeline_normalize_reduce_sum4_fp16_to_fp32 = 0; |
| 27 | pipeline_normalize_reduce_sum4_fp32[0] = 0; |
| 28 | pipeline_normalize_reduce_sum4_fp32[1] = 0; |
| 29 | pipeline_normalize_coeffs = 0; |
| 30 | pipeline_normalize_norm = 0; |
| 31 | |
| 32 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 = 0; |
| 33 | pipeline_normalize_reduce_sum4_fp32_pack4[0] = 0; |
| 34 | pipeline_normalize_reduce_sum4_fp32_pack4[1] = 0; |
| 35 | pipeline_normalize_coeffs_pack4 = 0; |
| 36 | pipeline_normalize_norm_pack4 = 0; |
| 37 | |
| 38 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 = 0; |
| 39 | pipeline_normalize_reduce_sum4_fp32_pack8[0] = 0; |
| 40 | pipeline_normalize_reduce_sum4_fp32_pack8[1] = 0; |
| 41 | pipeline_normalize_coeffs_pack8 = 0; |
| 42 | pipeline_normalize_norm_pack8 = 0; |
| 43 | } |
| 44 | |
| 45 | int Normalize_vulkan::create_pipeline(const Option& opt) |
| 46 | { |
| 47 | const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| 48 | |
| 49 | int elempack = 1; |
| 50 | if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1; |
| 51 | if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1; |
| 52 | if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1; |
| 53 | |
| 54 | size_t elemsize; |
| 55 | if (opt.use_fp16_storage) |
| 56 | { |
| 57 | elemsize = elempack * 2u; |
| 58 | } |
| 59 | else if (opt.use_fp16_packed) |
| 60 | { |
| 61 | elemsize = elempack == 1 ? 4u : elempack * 2u; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | elemsize = 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 | { |
| 74 | std::vector<vk_specialization_type> specializations(2); |
| 75 | specializations[0].i = across_spatial; |
| 76 | specializations[1].i = across_channel; |
| 77 | |
| 78 | Mat local_size_xyz; // TODO select by across_channel / across_spatial |
| 79 | |
| 80 | // pack1 |
| 81 | if (shape.dims == 0 || elempack == 1) |
| 82 | { |
| 83 | pipeline_normalize_reduce_sum4_fp16_to_fp32 = new Pipeline(vkdev); |
| 84 | pipeline_normalize_reduce_sum4_fp16_to_fp32->set_optimal_local_size_xyz(local_size_xyz); |
| 85 | pipeline_normalize_reduce_sum4_fp16_to_fp32->create(LayerShaderType::normalize_reduce_sum4_fp16_to_fp32, opt, specializations); |
| 86 | |
| 87 | pipeline_normalize_reduce_sum4_fp32[0] = new Pipeline(vkdev); |
| 88 | pipeline_normalize_reduce_sum4_fp32[0]->set_optimal_local_size_xyz(local_size_xyz); |
| 89 | pipeline_normalize_reduce_sum4_fp32[0]->create(LayerShaderType::normalize_reduce_sum4_fp32, opt, specializations); |
| 90 | pipeline_normalize_reduce_sum4_fp32[1] = new Pipeline(vkdev); |
| 91 | pipeline_normalize_reduce_sum4_fp32[1]->set_optimal_local_size_xyz(local_size_xyz); |
| 92 | pipeline_normalize_reduce_sum4_fp32[1]->create(LayerShaderType::normalize_reduce_sum4_fp32, opt, specializations); |
| 93 | } |
| 94 | |
| 95 | // pack4 |
| 96 | if (shape.dims == 0 || elempack == 4) |
| 97 | { |
| 98 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 = new Pipeline(vkdev); |
| 99 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 100 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4->create(LayerShaderType::normalize_reduce_sum4_fp16_to_fp32_pack4, opt, specializations); |
| 101 | |
| 102 | pipeline_normalize_reduce_sum4_fp32_pack4[0] = new Pipeline(vkdev); |
| 103 | pipeline_normalize_reduce_sum4_fp32_pack4[0]->set_optimal_local_size_xyz(local_size_xyz); |
| 104 | pipeline_normalize_reduce_sum4_fp32_pack4[0]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack4, opt, specializations); |
| 105 | pipeline_normalize_reduce_sum4_fp32_pack4[1] = new Pipeline(vkdev); |
| 106 | pipeline_normalize_reduce_sum4_fp32_pack4[1]->set_optimal_local_size_xyz(local_size_xyz); |
| 107 | pipeline_normalize_reduce_sum4_fp32_pack4[1]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack4, opt, specializations); |
| 108 | } |
| 109 | |
| 110 | // pack8 |
| 111 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 112 | { |
| 113 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 = new Pipeline(vkdev); |
| 114 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 115 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8->create(LayerShaderType::normalize_reduce_sum4_fp16_to_fp32_pack8, opt, specializations); |
| 116 | |
| 117 | pipeline_normalize_reduce_sum4_fp32_pack8[0] = new Pipeline(vkdev); |
| 118 | pipeline_normalize_reduce_sum4_fp32_pack8[0]->set_optimal_local_size_xyz(local_size_xyz); |
| 119 | pipeline_normalize_reduce_sum4_fp32_pack8[0]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack8, opt, specializations); |
| 120 | pipeline_normalize_reduce_sum4_fp32_pack8[1] = new Pipeline(vkdev); |
| 121 | pipeline_normalize_reduce_sum4_fp32_pack8[1]->set_optimal_local_size_xyz(local_size_xyz); |
| 122 | pipeline_normalize_reduce_sum4_fp32_pack8[1]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack8, opt, specializations); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | { |
| 127 | std::vector<vk_specialization_type> specializations(4); |
| 128 | specializations[0].i = across_spatial; |
| 129 | specializations[1].i = across_channel; |
| 130 | specializations[2].f = eps; |
| 131 | specializations[3].i = eps_mode; |
| 132 | |
| 133 | Mat local_size_xyz; // TODO resolve sqsum_workspace shape |
| 134 | |
| 135 | if (shape.dims == 0 || elempack == 1) |
| 136 | { |
| 137 | pipeline_normalize_coeffs = new Pipeline(vkdev); |
| 138 | pipeline_normalize_coeffs->set_optimal_local_size_xyz(local_size_xyz); |
| 139 | pipeline_normalize_coeffs->create(LayerShaderType::normalize_coeffs, opt, specializations); |
| 140 | } |
| 141 | |
| 142 | if (shape.dims == 0 || elempack == 4) |
| 143 | { |
| 144 | pipeline_normalize_coeffs_pack4 = new Pipeline(vkdev); |
| 145 | pipeline_normalize_coeffs_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 146 | pipeline_normalize_coeffs_pack4->create(LayerShaderType::normalize_coeffs_pack4, opt, specializations); |
| 147 | } |
| 148 | |
| 149 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 150 | { |
| 151 | pipeline_normalize_coeffs_pack8 = new Pipeline(vkdev); |
| 152 | pipeline_normalize_coeffs_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 153 | pipeline_normalize_coeffs_pack8->create(LayerShaderType::normalize_coeffs_pack8, opt, specializations); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | { |
| 158 | std::vector<vk_specialization_type> specializations(5 + 5); |
| 159 | specializations[0].i = across_spatial; |
| 160 | specializations[1].i = across_channel; |
| 161 | specializations[2].i = channel_shared; |
| 162 | specializations[3].i = (scale_data_size == 1 && scale_data[0] == 1.f) ? 0 : 1; |
| 163 | specializations[4].f = channel_shared ? scale_data[0] : 1.f; |
| 164 | specializations[5 + 0].i = shape_packed.dims; |
| 165 | specializations[5 + 1].i = shape_packed.w; |
| 166 | specializations[5 + 2].i = shape_packed.h; |
| 167 | specializations[5 + 3].i = shape_packed.c; |
| 168 | specializations[5 + 4].i = shape_packed.cstep; |
| 169 | |
| 170 | Mat local_size_xyz; |
| 171 | if (shape_packed.dims != 0) |
| 172 | { |
| 173 | local_size_xyz.w = std::min(4, shape_packed.w); |
| 174 | local_size_xyz.h = std::min(4, shape_packed.h); |
| 175 | local_size_xyz.c = std::min(4, shape_packed.c); |
| 176 | } |
| 177 | |
| 178 | if (shape.dims == 0 || elempack == 1) |
| 179 | { |
| 180 | pipeline_normalize_norm = new Pipeline(vkdev); |
| 181 | pipeline_normalize_norm->set_optimal_local_size_xyz(local_size_xyz); |
| 182 | pipeline_normalize_norm->create(LayerShaderType::normalize_norm, opt, specializations); |
| 183 | } |
| 184 | |
| 185 | if (shape.dims == 0 || elempack == 4) |
| 186 | { |
| 187 | pipeline_normalize_norm_pack4 = new Pipeline(vkdev); |
| 188 | pipeline_normalize_norm_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 189 | pipeline_normalize_norm_pack4->create(LayerShaderType::normalize_norm_pack4, opt, specializations); |
| 190 | } |
| 191 | |
| 192 | if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8) |
| 193 | { |
| 194 | pipeline_normalize_norm_pack8 = new Pipeline(vkdev); |
| 195 | pipeline_normalize_norm_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 196 | pipeline_normalize_norm_pack8->create(LayerShaderType::normalize_norm_pack8, opt, specializations); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | int Normalize_vulkan::destroy_pipeline(const Option& /*opt*/) |
| 204 | { |
| 205 | delete pipeline_normalize_reduce_sum4_fp16_to_fp32; |
| 206 | pipeline_normalize_reduce_sum4_fp16_to_fp32 = 0; |
| 207 | |
| 208 | delete pipeline_normalize_reduce_sum4_fp32[0]; |
| 209 | delete pipeline_normalize_reduce_sum4_fp32[1]; |
| 210 | pipeline_normalize_reduce_sum4_fp32[0] = 0; |
| 211 | pipeline_normalize_reduce_sum4_fp32[1] = 0; |
| 212 | |
| 213 | delete pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4; |
| 214 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 = 0; |
| 215 | |
| 216 | delete pipeline_normalize_reduce_sum4_fp32_pack4[0]; |
| 217 | delete pipeline_normalize_reduce_sum4_fp32_pack4[1]; |
| 218 | pipeline_normalize_reduce_sum4_fp32_pack4[0] = 0; |
| 219 | pipeline_normalize_reduce_sum4_fp32_pack4[1] = 0; |
| 220 | |
| 221 | delete pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8; |
| 222 | pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 = 0; |
| 223 | |
| 224 | delete pipeline_normalize_reduce_sum4_fp32_pack8[0]; |
| 225 | delete pipeline_normalize_reduce_sum4_fp32_pack8[1]; |
| 226 | pipeline_normalize_reduce_sum4_fp32_pack8[0] = 0; |
| 227 | pipeline_normalize_reduce_sum4_fp32_pack8[1] = 0; |
| 228 | |
| 229 | delete pipeline_normalize_coeffs; |
| 230 | pipeline_normalize_coeffs = 0; |
| 231 | |
| 232 | delete pipeline_normalize_coeffs_pack4; |
| 233 | pipeline_normalize_coeffs_pack4 = 0; |
| 234 | |
| 235 | delete pipeline_normalize_coeffs_pack8; |
| 236 | pipeline_normalize_coeffs_pack8 = 0; |
| 237 | |
| 238 | delete pipeline_normalize_norm; |
| 239 | pipeline_normalize_norm = 0; |
| 240 | |
| 241 | delete pipeline_normalize_norm_pack4; |
| 242 | pipeline_normalize_norm_pack4 = 0; |
| 243 | |
| 244 | delete pipeline_normalize_norm_pack8; |
| 245 | pipeline_normalize_norm_pack8 = 0; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int Normalize_vulkan::upload_model(VkTransfer& cmd, const Option& opt) |
| 251 | { |
| 252 | if (!channel_shared && !(scale_data_size == 1 && scale_data[0] == 1.f)) |
| 253 | { |
| 254 | int elempack = opt.use_shader_pack8 && scale_data_size % 8 == 0 ? 8 : scale_data_size % 4 == 0 ? 4 : 1; |
| 255 | |
| 256 | Mat scale_data_packed; |
| 257 | convert_packing(scale_data, scale_data_packed, elempack, opt); |
| 258 | |
| 259 | if (opt.use_image_storage) |
| 260 | { |
| 261 | cmd.record_upload(scale_data_packed, scale_data_gpu_image, opt); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | cmd.record_upload(scale_data_packed, scale_data_gpu, opt); |
| 266 | } |
| 267 | |
| 268 | if (opt.lightmode) |
| 269 | scale_data.release(); |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | int Normalize_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const |
| 276 | { |
| 277 | // int w = bottom_top_blob.w; |
| 278 | // int h = bottom_top_blob.h; |
| 279 | // int size = w * h; |
| 280 | size_t elemsize = bottom_top_blob.elemsize; |
| 281 | int elempack = bottom_top_blob.elempack; |
| 282 | |
| 283 | // reduce square sum |
| 284 | VkMat sqsum_workspace; |
| 285 | { |
| 286 | { |
| 287 | int reduced_w; |
| 288 | int reduced_h; |
| 289 | int reduced_c; |
| 290 | |
| 291 | if (across_spatial && across_channel) |
| 292 | { |
| 293 | reduced_w = (bottom_top_blob.w * bottom_top_blob.h + 1) / 2; |
| 294 | reduced_h = 1; |
| 295 | reduced_c = (bottom_top_blob.c + 1) / 2; |
| 296 | } |
| 297 | else if (across_spatial && !across_channel) |
| 298 | { |
| 299 | reduced_w = (bottom_top_blob.w * bottom_top_blob.h + 3) / 4; |
| 300 | reduced_h = 1; |
| 301 | reduced_c = bottom_top_blob.c; |
| 302 | } |
| 303 | else // if (!across_spatial && across_channel) |
| 304 | { |
| 305 | reduced_w = bottom_top_blob.w * bottom_top_blob.h; |
| 306 | reduced_h = 1; |
| 307 | reduced_c = (bottom_top_blob.c + 3) / 4; |
| 308 | } |
| 309 | |
| 310 | sqsum_workspace.create(reduced_w, reduced_h, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator); |
| 311 | { |
| 312 | std::vector<VkMat> bindings(2); |
| 313 | bindings[0] = bottom_top_blob; |
| 314 | bindings[1] = sqsum_workspace; |
| 315 | |
| 316 | std::vector<vk_constant_type> constants(8); |
| 317 | constants[0].i = bottom_top_blob.w * bottom_top_blob.h; |
| 318 | constants[1].i = 1; |
| 319 | constants[2].i = bottom_top_blob.c; |
| 320 | constants[3].i = bottom_top_blob.cstep; |
| 321 | constants[4].i = sqsum_workspace.w; |
| 322 | constants[5].i = 1; |
| 323 | constants[6].i = sqsum_workspace.c; |
| 324 | constants[7].i = sqsum_workspace.cstep; |
| 325 | |
| 326 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 |
| 327 | : elempack == 4 ? pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 |
| 328 | : pipeline_normalize_reduce_sum4_fp16_to_fp32; |
| 329 | |
| 330 | cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | int pb = 0; |
| 335 | while ((across_spatial && sqsum_workspace.w > 1) || (across_channel && sqsum_workspace.c > 1)) |
| 336 | { |
| 337 | int reduced_w; |
| 338 | int reduced_h; |
| 339 | int reduced_c; |
| 340 | |
| 341 | if (across_spatial && across_channel) |
| 342 | { |
| 343 | reduced_w = (sqsum_workspace.w + 1) / 2; |
| 344 | reduced_h = 1; |
| 345 | reduced_c = (sqsum_workspace.c + 1) / 2; |
| 346 | } |
| 347 | else if (across_spatial && !across_channel) |
| 348 | { |
| 349 | reduced_w = (sqsum_workspace.w + 3) / 4; |
| 350 | reduced_h = 1; |
| 351 | reduced_c = sqsum_workspace.c; |
| 352 | } |
| 353 | else // if (!across_spatial && across_channel) |
| 354 | { |
| 355 | reduced_w = sqsum_workspace.w; |
| 356 | reduced_h = 1; |
| 357 | reduced_c = (sqsum_workspace.c + 3) / 4; |
| 358 | } |
| 359 | |
| 360 | VkMat sqsum_workspace_reduced; |
| 361 | sqsum_workspace_reduced.create(reduced_w, reduced_h, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator); |
| 362 | |
| 363 | { |
| 364 | std::vector<VkMat> bindings(2); |
| 365 | bindings[0] = sqsum_workspace; |
| 366 | bindings[1] = sqsum_workspace_reduced; |
| 367 | |
| 368 | std::vector<vk_constant_type> constants(8); |
| 369 | constants[0].i = sqsum_workspace.w; |
| 370 | constants[1].i = 1; |
| 371 | constants[2].i = sqsum_workspace.c; |
| 372 | constants[3].i = sqsum_workspace.cstep; |
| 373 | constants[4].i = sqsum_workspace_reduced.w; |
| 374 | constants[5].i = 1; |
| 375 | constants[6].i = sqsum_workspace_reduced.c; |
| 376 | constants[7].i = sqsum_workspace_reduced.cstep; |
| 377 | |
| 378 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_reduce_sum4_fp32_pack8[pb % 2] |
| 379 | : elempack == 4 ? pipeline_normalize_reduce_sum4_fp32_pack4[pb % 2] |
| 380 | : pipeline_normalize_reduce_sum4_fp32[pb % 2]; |
| 381 | |
| 382 | cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace_reduced); |
| 383 | |
| 384 | pb++; |
| 385 | } |
| 386 | |
| 387 | sqsum_workspace = sqsum_workspace_reduced; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // coeffs |
| 392 | VkMat coeffs_workspace; |
| 393 | coeffs_workspace.create(sqsum_workspace.w * sqsum_workspace.h * sqsum_workspace.c, elemsize, elempack, opt.workspace_vkallocator); |
| 394 | { |
| 395 | std::vector<VkMat> bindings(2); |
| 396 | bindings[0] = sqsum_workspace; |
| 397 | bindings[1] = coeffs_workspace; |
| 398 | |
| 399 | std::vector<vk_constant_type> constants(4); |
| 400 | constants[0].i = sqsum_workspace.w; |
| 401 | constants[1].i = sqsum_workspace.h; |
| 402 | constants[2].i = sqsum_workspace.c; |
| 403 | constants[3].i = sqsum_workspace.cstep; |
| 404 | |
| 405 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_coeffs_pack8 |
| 406 | : elempack == 4 ? pipeline_normalize_coeffs_pack4 |
| 407 | : pipeline_normalize_coeffs; |
| 408 | |
| 409 | cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace); |
| 410 | } |
| 411 | |
| 412 | // norm |
| 413 | { |
| 414 | std::vector<VkMat> bindings(3); |
| 415 | bindings[0] = bottom_top_blob; |
| 416 | bindings[1] = coeffs_workspace; |
| 417 | bindings[2] = scale_data_gpu; |
| 418 | |
| 419 | std::vector<vk_constant_type> constants(5); |
| 420 | constants[0].i = bottom_top_blob.dims; |
| 421 | constants[1].i = bottom_top_blob.w; |
| 422 | constants[2].i = bottom_top_blob.h; |
| 423 | constants[3].i = bottom_top_blob.c; |
| 424 | constants[4].i = bottom_top_blob.cstep; |
| 425 | |
| 426 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_norm_pack8 |
| 427 | : elempack == 4 ? pipeline_normalize_norm_pack4 |
| 428 | : pipeline_normalize_norm; |
| 429 | |
| 430 | cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob); |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | int Normalize_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const |
| 437 | { |
| 438 | // int w = bottom_top_blob.w; |
| 439 | // int h = bottom_top_blob.h; |
| 440 | // int size = w * h; |
| 441 | size_t elemsize = bottom_top_blob.elemsize; |
| 442 | int elempack = bottom_top_blob.elempack; |
| 443 | |
| 444 | // reduce square sum |
| 445 | VkImageMat sqsum_workspace; |
| 446 | { |
| 447 | { |
| 448 | int reduced_w; |
| 449 | int reduced_h; |
| 450 | int reduced_c; |
| 451 | |
| 452 | if (across_spatial && across_channel) |
| 453 | { |
| 454 | reduced_w = (bottom_top_blob.w + 1) / 2; |
| 455 | reduced_h = (bottom_top_blob.h + 1) / 2; |
| 456 | reduced_c = (bottom_top_blob.c + 1) / 2; |
| 457 | } |
| 458 | else if (across_spatial && !across_channel) |
| 459 | { |
| 460 | reduced_w = (bottom_top_blob.w + 1) / 2; |
| 461 | reduced_h = (bottom_top_blob.h + 1) / 2; |
| 462 | reduced_c = bottom_top_blob.c; |
| 463 | } |
| 464 | else // if (!across_spatial && across_channel) |
| 465 | { |
| 466 | reduced_w = bottom_top_blob.w; |
| 467 | reduced_h = bottom_top_blob.h; |
| 468 | reduced_c = (bottom_top_blob.c + 3) / 4; |
| 469 | } |
| 470 | |
| 471 | sqsum_workspace.create(reduced_w, reduced_h, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator); |
| 472 | { |
| 473 | std::vector<VkImageMat> bindings(2); |
| 474 | bindings[0] = bottom_top_blob; |
| 475 | bindings[1] = sqsum_workspace; |
| 476 | |
| 477 | std::vector<vk_constant_type> constants(8); |
| 478 | constants[0].i = bottom_top_blob.w; |
| 479 | constants[1].i = bottom_top_blob.h; |
| 480 | constants[2].i = bottom_top_blob.c; |
| 481 | constants[3].i = 0; //bottom_top_blob.cstep; |
| 482 | constants[4].i = sqsum_workspace.w; |
| 483 | constants[5].i = sqsum_workspace.h; |
| 484 | constants[6].i = sqsum_workspace.c; |
| 485 | constants[7].i = 0; //sqsum_workspace.cstep; |
| 486 | |
| 487 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 |
| 488 | : elempack == 4 ? pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 |
| 489 | : pipeline_normalize_reduce_sum4_fp16_to_fp32; |
| 490 | |
| 491 | cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | int pb = 0; |
| 496 | while ((across_spatial && sqsum_workspace.w * sqsum_workspace.h > 1) || (across_channel && sqsum_workspace.c > 1)) |
| 497 | { |
| 498 | int reduced_w; |
| 499 | int reduced_h; |
| 500 | int reduced_c; |
| 501 | |
| 502 | if (across_spatial && across_channel) |
| 503 | { |
| 504 | reduced_w = (sqsum_workspace.w + 1) / 2; |
| 505 | reduced_h = (sqsum_workspace.h + 1) / 2; |
| 506 | reduced_c = (sqsum_workspace.c + 1) / 2; |
| 507 | } |
| 508 | else if (across_spatial && !across_channel) |
| 509 | { |
| 510 | reduced_w = (sqsum_workspace.w + 1) / 2; |
| 511 | reduced_h = (sqsum_workspace.h + 1) / 2; |
| 512 | reduced_c = sqsum_workspace.c; |
| 513 | } |
| 514 | else // if (!across_spatial && across_channel) |
| 515 | { |
| 516 | reduced_w = sqsum_workspace.w; |
| 517 | reduced_h = sqsum_workspace.h; |
| 518 | reduced_c = (sqsum_workspace.c + 3) / 4; |
| 519 | } |
| 520 | |
| 521 | VkImageMat sqsum_workspace_reduced; |
| 522 | sqsum_workspace_reduced.create(reduced_w, reduced_h, reduced_c, 4u * elempack, elempack, opt.workspace_vkallocator); |
| 523 | |
| 524 | { |
| 525 | std::vector<VkImageMat> bindings(2); |
| 526 | bindings[0] = sqsum_workspace; |
| 527 | bindings[1] = sqsum_workspace_reduced; |
| 528 | |
| 529 | std::vector<vk_constant_type> constants(8); |
| 530 | constants[0].i = sqsum_workspace.w; |
| 531 | constants[1].i = sqsum_workspace.h; |
| 532 | constants[2].i = sqsum_workspace.c; |
| 533 | constants[3].i = 0; //sqsum_workspace.cstep; |
| 534 | constants[4].i = sqsum_workspace_reduced.w; |
| 535 | constants[5].i = sqsum_workspace_reduced.h; |
| 536 | constants[6].i = sqsum_workspace_reduced.c; |
| 537 | constants[7].i = 0; //sqsum_workspace_reduced.cstep; |
| 538 | |
| 539 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_reduce_sum4_fp32_pack8[pb % 2] |
| 540 | : elempack == 4 ? pipeline_normalize_reduce_sum4_fp32_pack4[pb % 2] |
| 541 | : pipeline_normalize_reduce_sum4_fp32[pb % 2]; |
| 542 | |
| 543 | cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace_reduced); |
| 544 | |
| 545 | pb++; |
| 546 | } |
| 547 | |
| 548 | sqsum_workspace = sqsum_workspace_reduced; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // coeffs |
| 553 | VkImageMat coeffs_workspace; |
| 554 | coeffs_workspace.create(sqsum_workspace.w * sqsum_workspace.h * sqsum_workspace.c, elemsize, elempack, opt.workspace_vkallocator); |
| 555 | { |
| 556 | std::vector<VkImageMat> bindings(2); |
| 557 | bindings[0] = sqsum_workspace; |
| 558 | bindings[1] = coeffs_workspace; |
| 559 | |
| 560 | std::vector<vk_constant_type> constants(4); |
| 561 | constants[0].i = sqsum_workspace.w; |
| 562 | constants[1].i = sqsum_workspace.h; |
| 563 | constants[2].i = sqsum_workspace.c; |
| 564 | constants[3].i = 0; //sqsum_workspace.cstep; |
| 565 | |
| 566 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_coeffs_pack8 |
| 567 | : elempack == 4 ? pipeline_normalize_coeffs_pack4 |
| 568 | : pipeline_normalize_coeffs; |
| 569 | |
| 570 | cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace); |
| 571 | } |
| 572 | |
| 573 | // norm |
| 574 | { |
| 575 | std::vector<VkImageMat> bindings(4); |
| 576 | bindings[0] = bottom_top_blob; |
| 577 | bindings[1] = bottom_top_blob; |
| 578 | bindings[2] = coeffs_workspace; |
| 579 | bindings[3] = scale_data_gpu_image; |
| 580 | |
| 581 | std::vector<vk_constant_type> constants(5); |
| 582 | constants[0].i = bottom_top_blob.dims; |
| 583 | constants[1].i = bottom_top_blob.w; |
| 584 | constants[2].i = bottom_top_blob.h; |
| 585 | constants[3].i = bottom_top_blob.c; |
| 586 | constants[4].i = 0; //bottom_top_blob.cstep; |
| 587 | |
| 588 | const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_norm_pack8 |
| 589 | : elempack == 4 ? pipeline_normalize_norm_pack4 |
| 590 | : pipeline_normalize_norm; |
| 591 | |
| 592 | cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob); |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | } // namespace ncnn |