| 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 "lrn_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | LRN_vulkan::LRN_vulkan() |
| 22 | { |
| 23 | support_vulkan = true; |
| 24 | support_image_storage = true; |
| 25 | |
| 26 | pipeline_lrn_square_pad = 0; |
| 27 | pipeline_lrn_norm = 0; |
| 28 | pipeline_lrn_square_pad_across_channel_pack4 = 0; |
| 29 | pipeline_lrn_norm_across_channel_pack4 = 0; |
| 30 | pipeline_lrn_square_pad_within_channel_pack4 = 0; |
| 31 | pipeline_lrn_norm_within_channel_pack4 = 0; |
| 32 | pipeline_lrn_square_pad_across_channel_pack8 = 0; |
| 33 | pipeline_lrn_norm_across_channel_pack8 = 0; |
| 34 | pipeline_lrn_square_pad_within_channel_pack8 = 0; |
| 35 | pipeline_lrn_norm_within_channel_pack8 = 0; |
| 36 | } |
| 37 | |
| 38 | int LRN_vulkan::create_pipeline(const Option& opt) |
| 39 | { |
| 40 | const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| 41 | |
| 42 | int elempack = 1; |
| 43 | if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1; |
| 44 | if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1; |
| 45 | if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1; |
| 46 | |
| 47 | size_t elemsize; |
| 48 | if (opt.use_fp16_storage) |
| 49 | { |
| 50 | elemsize = elempack * 2u; |
| 51 | } |
| 52 | else if (opt.use_fp16_packed) |
| 53 | { |
| 54 | elemsize = elempack == 1 ? 4u : elempack * 2u; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | elemsize = elempack * 4u; |
| 59 | } |
| 60 | |
| 61 | Mat shape_packed; |
| 62 | if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack); |
| 63 | if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack); |
| 64 | if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack); |
| 65 | |
| 66 | Mat workspace_shape_packed; |
| 67 | if (shape.dims != 0) |
| 68 | { |
| 69 | if (region_type == NormRegion_ACROSS_CHANNELS) |
| 70 | { |
| 71 | workspace_shape_packed = Mat(shape.w, shape.h, shape.c + local_size - 1, (void*)0); |
| 72 | } |
| 73 | else if (region_type == NormRegion_WITHIN_CHANNEL) |
| 74 | { |
| 75 | workspace_shape_packed = Mat(shape.w + local_size - 1, shape.h + local_size - 1, shape.c / elempack, (void*)0, elempack * 4u, elempack); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | int pad = local_size / 2; |
| 81 | |
| 82 | std::vector<vk_specialization_type> specializations(3 + 10); |
| 83 | specializations[0].i = region_type; |
| 84 | specializations[1].i = pad; |
| 85 | specializations[2].i = local_size - pad - 1; |
| 86 | specializations[3 + 0].i = shape_packed.dims; |
| 87 | specializations[3 + 1].i = shape_packed.w; |
| 88 | specializations[3 + 2].i = shape_packed.h; |
| 89 | specializations[3 + 3].i = shape_packed.c; |
| 90 | specializations[3 + 4].i = shape_packed.cstep; |
| 91 | specializations[3 + 5].i = workspace_shape_packed.dims; |
| 92 | specializations[3 + 6].i = workspace_shape_packed.w; |
| 93 | specializations[3 + 7].i = workspace_shape_packed.h; |
| 94 | specializations[3 + 8].i = workspace_shape_packed.c; |
| 95 | specializations[3 + 9].i = workspace_shape_packed.cstep; |
| 96 | |
| 97 | Mat local_size_xyz; |
| 98 | if (workspace_shape_packed.dims != 0) |
| 99 | { |
| 100 | local_size_xyz.w = std::min(4, workspace_shape_packed.w); |
| 101 | local_size_xyz.h = std::min(4, workspace_shape_packed.h); |
| 102 | local_size_xyz.c = std::min(4, workspace_shape_packed.c); |
| 103 | } |
| 104 | |
| 105 | // pack1 |
| 106 | if (shape.dims == 0 || elempack == 1) |
| 107 | { |
| 108 | pipeline_lrn_square_pad = new Pipeline(vkdev); |
| 109 | pipeline_lrn_square_pad->set_optimal_local_size_xyz(local_size_xyz); |
| 110 | pipeline_lrn_square_pad->create(LayerShaderType::lrn_square_pad, opt, specializations); |
| 111 | } |
| 112 | |
| 113 | // pack4 |
| 114 | if (region_type == 0 && (shape.dims == 0 || elempack == 4)) |
| 115 | { |
| 116 | pipeline_lrn_square_pad_across_channel_pack4 = new Pipeline(vkdev); |
| 117 | pipeline_lrn_square_pad_across_channel_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 118 | pipeline_lrn_square_pad_across_channel_pack4->create(LayerShaderType::lrn_square_pad_across_channel_pack4, opt, specializations); |
| 119 | } |
| 120 | if (region_type == 1 && (shape.dims == 0 || elempack == 4)) |
| 121 | { |
| 122 | pipeline_lrn_square_pad_within_channel_pack4 = new Pipeline(vkdev); |
| 123 | pipeline_lrn_square_pad_within_channel_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 124 | pipeline_lrn_square_pad_within_channel_pack4->create(LayerShaderType::lrn_square_pad_within_channel_pack4, opt, specializations); |
| 125 | } |
| 126 | |
| 127 | // pack8 |
| 128 | if (region_type == 0 && ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)) |
| 129 | { |
| 130 | pipeline_lrn_square_pad_across_channel_pack8 = new Pipeline(vkdev); |
| 131 | pipeline_lrn_square_pad_across_channel_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 132 | pipeline_lrn_square_pad_across_channel_pack8->create(LayerShaderType::lrn_square_pad_across_channel_pack8, opt, specializations); |
| 133 | } |
| 134 | if (region_type == 1 && ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)) |
| 135 | { |
| 136 | pipeline_lrn_square_pad_within_channel_pack8 = new Pipeline(vkdev); |
| 137 | pipeline_lrn_square_pad_within_channel_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 138 | pipeline_lrn_square_pad_within_channel_pack8->create(LayerShaderType::lrn_square_pad_within_channel_pack8, opt, specializations); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | std::vector<vk_specialization_type> specializations(5 + 10); |
| 144 | specializations[0].i = region_type; |
| 145 | specializations[1].i = local_size; |
| 146 | specializations[2].f = alpha; |
| 147 | specializations[3].f = beta; |
| 148 | specializations[4].f = bias; |
| 149 | specializations[5 + 0].i = workspace_shape_packed.dims; |
| 150 | specializations[5 + 1].i = workspace_shape_packed.w; |
| 151 | specializations[5 + 2].i = workspace_shape_packed.h; |
| 152 | specializations[5 + 3].i = workspace_shape_packed.c; |
| 153 | specializations[5 + 4].i = workspace_shape_packed.cstep; |
| 154 | specializations[5 + 5].i = shape_packed.dims; |
| 155 | specializations[5 + 6].i = shape_packed.w; |
| 156 | specializations[5 + 7].i = shape_packed.h; |
| 157 | specializations[5 + 8].i = shape_packed.c; |
| 158 | specializations[5 + 9].i = shape_packed.cstep; |
| 159 | |
| 160 | Mat local_size_xyz; |
| 161 | if (shape_packed.dims != 0) |
| 162 | { |
| 163 | local_size_xyz.w = std::min(4, shape_packed.w); |
| 164 | local_size_xyz.h = std::min(4, shape_packed.h); |
| 165 | local_size_xyz.c = std::min(4, shape_packed.c); |
| 166 | } |
| 167 | |
| 168 | // pack1 |
| 169 | if (shape.dims == 0 || elempack == 1) |
| 170 | { |
| 171 | pipeline_lrn_norm = new Pipeline(vkdev); |
| 172 | pipeline_lrn_norm->set_optimal_local_size_xyz(local_size_xyz); |
| 173 | pipeline_lrn_norm->create(LayerShaderType::lrn_norm, opt, specializations); |
| 174 | } |
| 175 | |
| 176 | // pack4 |
| 177 | if (region_type == 0 && (shape.dims == 0 || elempack == 4)) |
| 178 | { |
| 179 | pipeline_lrn_norm_across_channel_pack4 = new Pipeline(vkdev); |
| 180 | pipeline_lrn_norm_across_channel_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 181 | pipeline_lrn_norm_across_channel_pack4->create(LayerShaderType::lrn_norm_across_channel_pack4, opt, specializations); |
| 182 | } |
| 183 | if (region_type == 1 && (shape.dims == 0 || elempack == 4)) |
| 184 | { |
| 185 | pipeline_lrn_norm_within_channel_pack4 = new Pipeline(vkdev); |
| 186 | pipeline_lrn_norm_within_channel_pack4->set_optimal_local_size_xyz(local_size_xyz); |
| 187 | pipeline_lrn_norm_within_channel_pack4->create(LayerShaderType::lrn_norm_within_channel_pack4, opt, specializations); |
| 188 | } |
| 189 | |
| 190 | // pack8 |
| 191 | if (region_type == 0 && ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)) |
| 192 | { |
| 193 | pipeline_lrn_norm_across_channel_pack8 = new Pipeline(vkdev); |
| 194 | pipeline_lrn_norm_across_channel_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 195 | pipeline_lrn_norm_across_channel_pack8->create(LayerShaderType::lrn_norm_across_channel_pack8, opt, specializations); |
| 196 | } |
| 197 | if (region_type == 1 && ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)) |
| 198 | { |
| 199 | pipeline_lrn_norm_within_channel_pack8 = new Pipeline(vkdev); |
| 200 | pipeline_lrn_norm_within_channel_pack8->set_optimal_local_size_xyz(local_size_xyz); |
| 201 | pipeline_lrn_norm_within_channel_pack8->create(LayerShaderType::lrn_norm_within_channel_pack8, opt, specializations); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int LRN_vulkan::destroy_pipeline(const Option& /*opt*/) |
| 209 | { |
| 210 | delete pipeline_lrn_square_pad; |
| 211 | pipeline_lrn_square_pad = 0; |
| 212 | |
| 213 | delete pipeline_lrn_norm; |
| 214 | pipeline_lrn_norm = 0; |
| 215 | |
| 216 | delete pipeline_lrn_square_pad_across_channel_pack4; |
| 217 | pipeline_lrn_square_pad_across_channel_pack4 = 0; |
| 218 | |
| 219 | delete pipeline_lrn_norm_across_channel_pack4; |
| 220 | pipeline_lrn_norm_across_channel_pack4 = 0; |
| 221 | |
| 222 | delete pipeline_lrn_square_pad_within_channel_pack4; |
| 223 | pipeline_lrn_square_pad_within_channel_pack4 = 0; |
| 224 | |
| 225 | delete pipeline_lrn_norm_within_channel_pack4; |
| 226 | pipeline_lrn_norm_within_channel_pack4 = 0; |
| 227 | |
| 228 | delete pipeline_lrn_square_pad_across_channel_pack8; |
| 229 | pipeline_lrn_square_pad_across_channel_pack8 = 0; |
| 230 | |
| 231 | delete pipeline_lrn_norm_across_channel_pack8; |
| 232 | pipeline_lrn_norm_across_channel_pack8 = 0; |
| 233 | |
| 234 | delete pipeline_lrn_square_pad_within_channel_pack8; |
| 235 | pipeline_lrn_square_pad_within_channel_pack8 = 0; |
| 236 | |
| 237 | delete pipeline_lrn_norm_within_channel_pack8; |
| 238 | pipeline_lrn_norm_within_channel_pack8 = 0; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int LRN_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const |
| 244 | { |
| 245 | int w = bottom_top_blob.w; |
| 246 | int h = bottom_top_blob.h; |
| 247 | int channels = bottom_top_blob.c; |
| 248 | int elempack = bottom_top_blob.elempack; |
| 249 | |
| 250 | VkMat square_workspace; |
| 251 | |
| 252 | if (region_type == NormRegion_ACROSS_CHANNELS) |
| 253 | { |
| 254 | // always create scalar square workspace blob for norm across channel |
| 255 | square_workspace.create(w, h, channels * elempack + local_size - 1, 4u, 1, opt.workspace_vkallocator); |
| 256 | } |
| 257 | else if (region_type == NormRegion_WITHIN_CHANNEL) |
| 258 | { |
| 259 | square_workspace.create(w + local_size - 1, h + local_size - 1, channels, elempack * 4u, elempack, opt.workspace_vkallocator); |
| 260 | } |
| 261 | |
| 262 | // square pad |
| 263 | { |
| 264 | std::vector<VkMat> bindings(2); |
| 265 | bindings[0] = bottom_top_blob; |
| 266 | bindings[1] = square_workspace; |
| 267 | |
| 268 | std::vector<vk_constant_type> constants(10); |
| 269 | constants[0].i = bottom_top_blob.dims; |
| 270 | constants[1].i = bottom_top_blob.w; |
| 271 | constants[2].i = bottom_top_blob.h; |
| 272 | constants[3].i = bottom_top_blob.c; |
| 273 | constants[4].i = bottom_top_blob.cstep; |
| 274 | constants[5].i = square_workspace.dims; |
| 275 | constants[6].i = square_workspace.w; |
| 276 | constants[7].i = square_workspace.h; |
| 277 | constants[8].i = square_workspace.c; |
| 278 | constants[9].i = square_workspace.cstep; |
| 279 | |
| 280 | const Pipeline* pipeline = 0; |
| 281 | if (elempack == 8) |
| 282 | { |
| 283 | if (region_type == 0) pipeline = pipeline_lrn_square_pad_across_channel_pack8; |
| 284 | if (region_type == 1) pipeline = pipeline_lrn_square_pad_within_channel_pack8; |
| 285 | } |
| 286 | else if (elempack == 4) |
| 287 | { |
| 288 | if (region_type == 0) pipeline = pipeline_lrn_square_pad_across_channel_pack4; |
| 289 | if (region_type == 1) pipeline = pipeline_lrn_square_pad_within_channel_pack4; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | pipeline = pipeline_lrn_square_pad; |
| 294 | } |
| 295 | |
| 296 | cmd.record_pipeline(pipeline, bindings, constants, square_workspace); |
| 297 | } |
| 298 | |
| 299 | // norm |
| 300 | { |
| 301 | std::vector<VkMat> bindings(2); |
| 302 | bindings[0] = square_workspace; |
| 303 | bindings[1] = bottom_top_blob; |
| 304 | |
| 305 | std::vector<vk_constant_type> constants(10); |
| 306 | constants[0].i = square_workspace.dims; |
| 307 | constants[1].i = square_workspace.w; |
| 308 | constants[2].i = square_workspace.h; |
| 309 | constants[3].i = square_workspace.c; |
| 310 | constants[4].i = square_workspace.cstep; |
| 311 | constants[5].i = bottom_top_blob.dims; |
| 312 | constants[6].i = bottom_top_blob.w; |
| 313 | constants[7].i = bottom_top_blob.h; |
| 314 | constants[8].i = bottom_top_blob.c; |
| 315 | constants[9].i = bottom_top_blob.cstep; |
| 316 | |
| 317 | const Pipeline* pipeline = 0; |
| 318 | if (elempack == 8) |
| 319 | { |
| 320 | if (region_type == 0) pipeline = pipeline_lrn_norm_across_channel_pack8; |
| 321 | if (region_type == 1) pipeline = pipeline_lrn_norm_within_channel_pack8; |
| 322 | } |
| 323 | else if (elempack == 4) |
| 324 | { |
| 325 | if (region_type == 0) pipeline = pipeline_lrn_norm_across_channel_pack4; |
| 326 | if (region_type == 1) pipeline = pipeline_lrn_norm_within_channel_pack4; |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | pipeline = pipeline_lrn_norm; |
| 331 | } |
| 332 | |
| 333 | cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob); |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | int LRN_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const |
| 340 | { |
| 341 | int w = bottom_top_blob.w; |
| 342 | int h = bottom_top_blob.h; |
| 343 | int channels = bottom_top_blob.c; |
| 344 | int elempack = bottom_top_blob.elempack; |
| 345 | |
| 346 | VkImageMat square_workspace; |
| 347 | |
| 348 | if (region_type == NormRegion_ACROSS_CHANNELS) |
| 349 | { |
| 350 | // always create scalar square workspace blob for norm across channel |
| 351 | square_workspace.create(w, h, channels * elempack + local_size - 1, 4u, 1, opt.workspace_vkallocator); |
| 352 | } |
| 353 | else if (region_type == NormRegion_WITHIN_CHANNEL) |
| 354 | { |
| 355 | square_workspace.create(w + local_size - 1, h + local_size - 1, channels, elempack * 4u, elempack, opt.workspace_vkallocator); |
| 356 | } |
| 357 | |
| 358 | // square pad |
| 359 | { |
| 360 | std::vector<VkImageMat> bindings(2); |
| 361 | bindings[0] = bottom_top_blob; |
| 362 | bindings[1] = square_workspace; |
| 363 | |
| 364 | std::vector<vk_constant_type> constants(10); |
| 365 | constants[0].i = bottom_top_blob.dims; |
| 366 | constants[1].i = bottom_top_blob.w; |
| 367 | constants[2].i = bottom_top_blob.h; |
| 368 | constants[3].i = bottom_top_blob.c; |
| 369 | constants[4].i = 0; //bottom_top_blob.cstep; |
| 370 | constants[5].i = square_workspace.dims; |
| 371 | constants[6].i = square_workspace.w; |
| 372 | constants[7].i = square_workspace.h; |
| 373 | constants[8].i = square_workspace.c; |
| 374 | constants[9].i = 0; //square_workspace.cstep; |
| 375 | |
| 376 | const Pipeline* pipeline = 0; |
| 377 | if (elempack == 8) |
| 378 | { |
| 379 | if (region_type == 0) pipeline = pipeline_lrn_square_pad_across_channel_pack8; |
| 380 | if (region_type == 1) pipeline = pipeline_lrn_square_pad_within_channel_pack8; |
| 381 | } |
| 382 | else if (elempack == 4) |
| 383 | { |
| 384 | if (region_type == 0) pipeline = pipeline_lrn_square_pad_across_channel_pack4; |
| 385 | if (region_type == 1) pipeline = pipeline_lrn_square_pad_within_channel_pack4; |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | pipeline = pipeline_lrn_square_pad; |
| 390 | } |
| 391 | |
| 392 | cmd.record_pipeline(pipeline, bindings, constants, square_workspace); |
| 393 | } |
| 394 | |
| 395 | // norm |
| 396 | { |
| 397 | std::vector<VkImageMat> bindings(3); |
| 398 | bindings[0] = square_workspace; |
| 399 | bindings[1] = bottom_top_blob; |
| 400 | bindings[2] = bottom_top_blob; |
| 401 | |
| 402 | std::vector<vk_constant_type> constants(10); |
| 403 | constants[0].i = square_workspace.dims; |
| 404 | constants[1].i = square_workspace.w; |
| 405 | constants[2].i = square_workspace.h; |
| 406 | constants[3].i = square_workspace.c; |
| 407 | constants[4].i = 0; //square_workspace.cstep; |
| 408 | constants[5].i = bottom_top_blob.dims; |
| 409 | constants[6].i = bottom_top_blob.w; |
| 410 | constants[7].i = bottom_top_blob.h; |
| 411 | constants[8].i = bottom_top_blob.c; |
| 412 | constants[9].i = 0; //bottom_top_blob.cstep; |
| 413 | |
| 414 | const Pipeline* pipeline = 0; |
| 415 | if (elempack == 8) |
| 416 | { |
| 417 | if (region_type == 0) pipeline = pipeline_lrn_norm_across_channel_pack8; |
| 418 | if (region_type == 1) pipeline = pipeline_lrn_norm_within_channel_pack8; |
| 419 | } |
| 420 | else if (elempack == 4) |
| 421 | { |
| 422 | if (region_type == 0) pipeline = pipeline_lrn_norm_across_channel_pack4; |
| 423 | if (region_type == 1) pipeline = pipeline_lrn_norm_within_channel_pack4; |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | pipeline = pipeline_lrn_norm; |
| 428 | } |
| 429 | |
| 430 | cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob); |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | } // namespace ncnn |