| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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 "gemm_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | Gemm_vulkan::Gemm_vulkan() |
| 22 | { |
| 23 | support_vulkan = true; |
| 24 | support_image_storage = true; |
| 25 | |
| 26 | pipeline_gemm = 0; |
| 27 | } |
| 28 | |
| 29 | int Gemm_vulkan::load_param(const ParamDict& pd) |
| 30 | { |
| 31 | int ret = Gemm::load_param(pd); |
| 32 | |
| 33 | if (int8_scale_term) |
| 34 | { |
| 35 | support_vulkan = false; |
| 36 | support_image_storage = false; |
| 37 | } |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | int Gemm_vulkan::create_pipeline(const Option& opt) |
| 43 | { |
| 44 | // const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| 45 | |
| 46 | // int elempack = 1; |
| 47 | // if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1; |
| 48 | |
| 49 | // size_t elemsize; |
| 50 | // if (opt.use_fp16_storage) |
| 51 | // { |
| 52 | // elemsize = elempack * 2u; |
| 53 | // } |
| 54 | // else if (opt.use_fp16_packed) |
| 55 | // { |
| 56 | // elemsize = elempack == 1 ? 4u : elempack * 2u; |
| 57 | // } |
| 58 | // else |
| 59 | // { |
| 60 | // elemsize = elempack * 4u; |
| 61 | // } |
| 62 | |
| 63 | // Mat shape_packed; |
| 64 | // if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack); |
| 65 | |
| 66 | if (constantA) |
| 67 | { |
| 68 | A_data_packed = transA ? A_data.reshape(constantM, constantK) : A_data.reshape(constantK, constantM); |
| 69 | } |
| 70 | if (constantB) |
| 71 | { |
| 72 | B_data_packed = transB ? B_data.reshape(constantK, constantN) : B_data.reshape(constantN, constantK); |
| 73 | } |
| 74 | if (constantC) |
| 75 | { |
| 76 | C_data_packed = C_data; |
| 77 | } |
| 78 | |
| 79 | std::vector<vk_specialization_type> specializations(15); |
| 80 | specializations[0].f = alpha; |
| 81 | specializations[1].f = beta; |
| 82 | specializations[2].i = transA; |
| 83 | specializations[3].i = transB; |
| 84 | specializations[4].i = constantA; |
| 85 | specializations[5].i = constantB; |
| 86 | specializations[6].i = constantC; |
| 87 | specializations[7].i = constantM; |
| 88 | specializations[8].i = constantN; |
| 89 | specializations[9].i = constantK; |
| 90 | specializations[10].i = constant_broadcast_type_C; |
| 91 | specializations[11].i = output_N1M; |
| 92 | specializations[12].i = output_elempack; |
| 93 | specializations[13].i = output_elemtype; |
| 94 | specializations[14].i = output_transpose; |
| 95 | |
| 96 | Mat local_size_xyz; |
| 97 | // if (shape_packed.dims == 2) |
| 98 | // { |
| 99 | // local_size_xyz.w = std::min(8, shape_packed.w); |
| 100 | // local_size_xyz.h = std::min(8, shape_packed.h); |
| 101 | // local_size_xyz.c = 1; |
| 102 | // } |
| 103 | |
| 104 | // pack1 |
| 105 | // if (shape.dims == 0 || elempack == 1) |
| 106 | { |
| 107 | pipeline_gemm = new Pipeline(vkdev); |
| 108 | pipeline_gemm->set_optimal_local_size_xyz(local_size_xyz); |
| 109 | if (opt.use_shader_local_memory) |
| 110 | { |
| 111 | pipeline_gemm->set_local_size_xyz(8, 8, 1); |
| 112 | } |
| 113 | pipeline_gemm->create(LayerShaderType::gemm, opt, specializations); |
| 114 | } |
| 115 | |
| 116 | if (opt.lightmode) |
| 117 | { |
| 118 | A_data.release(); |
| 119 | B_data.release(); |
| 120 | C_data.release(); |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int Gemm_vulkan::destroy_pipeline(const Option& /*opt*/) |
| 127 | { |
| 128 | delete pipeline_gemm; |
| 129 | pipeline_gemm = 0; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int Gemm_vulkan::upload_model(VkTransfer& cmd, const Option& opt) |
| 135 | { |
| 136 | if (constantA) |
| 137 | { |
| 138 | if (support_image_storage && opt.use_image_storage) |
| 139 | { |
| 140 | cmd.record_upload(A_data_packed, A_data_gpu_image, opt); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | cmd.record_upload(A_data_packed, A_data_gpu, opt); |
| 145 | } |
| 146 | |
| 147 | A_data_packed.release(); |
| 148 | } |
| 149 | |
| 150 | if (constantB) |
| 151 | { |
| 152 | if (support_image_storage && opt.use_image_storage) |
| 153 | { |
| 154 | cmd.record_upload(B_data_packed, B_data_gpu_image, opt); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | cmd.record_upload(B_data_packed, B_data_gpu, opt); |
| 159 | } |
| 160 | |
| 161 | B_data_packed.release(); |
| 162 | } |
| 163 | |
| 164 | if (constantC) |
| 165 | { |
| 166 | if (support_image_storage && opt.use_image_storage) |
| 167 | { |
| 168 | cmd.record_upload(C_data_packed, C_data_gpu_image, opt); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | cmd.record_upload(C_data_packed, C_data_gpu, opt); |
| 173 | } |
| 174 | |
| 175 | C_data_packed.release(); |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int Gemm_vulkan::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const |
| 182 | { |
| 183 | const VkMat& A0 = constantA ? A_data_gpu : bottom_blobs[0]; |
| 184 | const VkMat& B0 = constantB ? B_data_gpu : constantA ? bottom_blobs[0] : bottom_blobs[1]; |
| 185 | |
| 186 | VkMat A; |
| 187 | VkMat B; |
| 188 | vkdev->convert_packing(A0, A, 1, cmd, opt); |
| 189 | vkdev->convert_packing(B0, B, 1, cmd, opt); |
| 190 | |
| 191 | const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h); |
| 192 | const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w; |
| 193 | const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w; |
| 194 | |
| 195 | VkMat C; |
| 196 | int broadcast_type_C = -1; |
| 197 | if (constantC) |
| 198 | { |
| 199 | vkdev->convert_packing(C_data_gpu, C, 1, cmd, opt); |
| 200 | broadcast_type_C = constant_broadcast_type_C; |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | VkMat C0; |
| 205 | if (constantA && constantB) |
| 206 | { |
| 207 | C0 = bottom_blobs.size() == 1 ? bottom_blobs[0] : VkMat(); |
| 208 | } |
| 209 | else if (constantA) |
| 210 | { |
| 211 | C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkMat(); |
| 212 | } |
| 213 | else if (constantB) |
| 214 | { |
| 215 | C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkMat(); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | C0 = bottom_blobs.size() == 3 ? bottom_blobs[2] : VkMat(); |
| 220 | } |
| 221 | |
| 222 | if (!C0.empty()) |
| 223 | { |
| 224 | vkdev->convert_packing(C0, C, 1, cmd, opt); |
| 225 | |
| 226 | if (C.dims == 1 && C.w == 1) |
| 227 | { |
| 228 | // scalar |
| 229 | broadcast_type_C = 0; |
| 230 | } |
| 231 | if (C.dims == 1 && C.w == M) |
| 232 | { |
| 233 | // M |
| 234 | // auto broadcast from h to w is the ncnn-style convention |
| 235 | broadcast_type_C = 1; |
| 236 | } |
| 237 | if (C.dims == 1 && C.w == N) |
| 238 | { |
| 239 | // N |
| 240 | broadcast_type_C = 4; |
| 241 | } |
| 242 | if (C.dims == 2 && C.w == 1 && C.h == M) |
| 243 | { |
| 244 | // Mx1 |
| 245 | broadcast_type_C = 2; |
| 246 | } |
| 247 | if (C.dims == 2 && C.w == N && C.h == M) |
| 248 | { |
| 249 | // MxN |
| 250 | broadcast_type_C = 3; |
| 251 | } |
| 252 | if (C.dims == 2 && C.w == N && C.h == 1) |
| 253 | { |
| 254 | // 1xN |
| 255 | broadcast_type_C = 4; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | int elempack = A.elempack; |
| 261 | size_t elemsize = A.elemsize; |
| 262 | |
| 263 | VkMat& top_blob = top_blobs[0]; |
| 264 | if (output_transpose) |
| 265 | { |
| 266 | if (output_N1M) |
| 267 | top_blob.create(M, 1, N, elemsize, opt.blob_vkallocator); |
| 268 | else |
| 269 | top_blob.create(M, N, elemsize, opt.blob_vkallocator); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | if (output_N1M) |
| 274 | top_blob.create(N, 1, M, elemsize, opt.blob_vkallocator); |
| 275 | else |
| 276 | top_blob.create(N, M, elemsize, opt.blob_vkallocator); |
| 277 | } |
| 278 | if (top_blob.empty()) |
| 279 | return -100; |
| 280 | |
| 281 | std::vector<VkMat> bindings(4); |
| 282 | bindings[0] = top_blob; |
| 283 | bindings[1] = A; |
| 284 | bindings[2] = B; |
| 285 | bindings[3] = C; |
| 286 | |
| 287 | std::vector<vk_constant_type> constants(10); |
| 288 | constants[0].i = M; |
| 289 | constants[1].i = N; |
| 290 | constants[2].i = K; |
| 291 | constants[3].i = broadcast_type_C; |
| 292 | constants[4].i = A.dims; |
| 293 | constants[5].i = A.dims == 3 ? A.cstep : transA ? M : K; |
| 294 | constants[6].i = B.dims; |
| 295 | constants[7].i = B.dims == 3 ? B.cstep : transB ? K : N; |
| 296 | constants[8].i = top_blob.dims; |
| 297 | constants[9].i = top_blob.dims == 3 ? top_blob.cstep : top_blob.w; |
| 298 | |
| 299 | const Pipeline* pipeline = pipeline_gemm; |
| 300 | |
| 301 | VkMat dispatcher; |
| 302 | dispatcher.w = (N + 1) / 2; |
| 303 | dispatcher.h = (M + 1) / 2; |
| 304 | dispatcher.c = 1; |
| 305 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 306 | |
| 307 | int out_elempack = 1; |
| 308 | { |
| 309 | int outh = output_transpose ? N : M; |
| 310 | out_elempack = opt.use_shader_pack8 && outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1; |
| 311 | } |
| 312 | if (output_elempack) |
| 313 | out_elempack = output_elempack; |
| 314 | |
| 315 | if (out_elempack != 1) |
| 316 | { |
| 317 | VkMat top_blob0; |
| 318 | vkdev->convert_packing(top_blob, top_blob0, out_elempack, cmd, opt); |
| 319 | top_blobs[0] = top_blob0; |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int Gemm_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 326 | { |
| 327 | std::vector<VkMat> bottom_blobs(1); |
| 328 | std::vector<VkMat> top_blobs(1); |
| 329 | bottom_blobs[0] = bottom_blob; |
| 330 | int ret = forward(bottom_blobs, top_blobs, cmd, opt); |
| 331 | top_blob = top_blobs[0]; |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | int Gemm_vulkan::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const |
| 336 | { |
| 337 | const VkImageMat& A0 = constantA ? A_data_gpu_image : bottom_blobs[0]; |
| 338 | const VkImageMat& B0 = constantB ? B_data_gpu_image : constantA ? bottom_blobs[0] : bottom_blobs[1]; |
| 339 | |
| 340 | VkImageMat A; |
| 341 | VkImageMat B; |
| 342 | vkdev->convert_packing(A0, A, 1, cmd, opt); |
| 343 | vkdev->convert_packing(B0, B, 1, cmd, opt); |
| 344 | |
| 345 | const int M = constantM ? constantM : transA ? A.w : (A.dims == 3 ? A.c : A.h); |
| 346 | const int K = constantK ? constantK : transA ? (A.dims == 3 ? A.c : A.h) : A.w; |
| 347 | const int N = constantN ? constantN : transB ? (B.dims == 3 ? B.c : B.h) : B.w; |
| 348 | |
| 349 | VkImageMat C; |
| 350 | int broadcast_type_C = -1; |
| 351 | if (constantC) |
| 352 | { |
| 353 | vkdev->convert_packing(C_data_gpu_image, C, 1, cmd, opt); |
| 354 | broadcast_type_C = constant_broadcast_type_C; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | VkImageMat C0; |
| 359 | if (constantA && constantB) |
| 360 | { |
| 361 | C0 = bottom_blobs.size() == 1 ? bottom_blobs[0] : VkImageMat(); |
| 362 | } |
| 363 | else if (constantA) |
| 364 | { |
| 365 | C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkImageMat(); |
| 366 | } |
| 367 | else if (constantB) |
| 368 | { |
| 369 | C0 = bottom_blobs.size() == 2 ? bottom_blobs[1] : VkImageMat(); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | C0 = bottom_blobs.size() == 3 ? bottom_blobs[2] : VkImageMat(); |
| 374 | } |
| 375 | |
| 376 | if (!C0.empty()) |
| 377 | { |
| 378 | vkdev->convert_packing(C0, C, 1, cmd, opt); |
| 379 | |
| 380 | if (C.dims == 1 && C.w == 1) |
| 381 | { |
| 382 | // scalar |
| 383 | broadcast_type_C = 0; |
| 384 | } |
| 385 | if (C.dims == 1 && C.w == M) |
| 386 | { |
| 387 | // M |
| 388 | // auto broadcast from h to w is the ncnn-style convention |
| 389 | broadcast_type_C = 1; |
| 390 | } |
| 391 | if (C.dims == 1 && C.w == N) |
| 392 | { |
| 393 | // N |
| 394 | broadcast_type_C = 4; |
| 395 | } |
| 396 | if (C.dims == 2 && C.w == 1 && C.h == M) |
| 397 | { |
| 398 | // Mx1 |
| 399 | broadcast_type_C = 2; |
| 400 | } |
| 401 | if (C.dims == 2 && C.w == N && C.h == M) |
| 402 | { |
| 403 | // MxN |
| 404 | broadcast_type_C = 3; |
| 405 | } |
| 406 | if (C.dims == 2 && C.w == N && C.h == 1) |
| 407 | { |
| 408 | // 1xN |
| 409 | broadcast_type_C = 4; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | int elempack = A.elempack; |
| 415 | size_t elemsize = A.elemsize; |
| 416 | |
| 417 | VkImageMat& top_blob = top_blobs[0]; |
| 418 | if (output_transpose) |
| 419 | { |
| 420 | if (output_N1M) |
| 421 | top_blob.create(M, 1, N, elemsize, opt.blob_vkallocator); |
| 422 | else |
| 423 | top_blob.create(M, N, elemsize, opt.blob_vkallocator); |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | if (output_N1M) |
| 428 | top_blob.create(N, 1, M, elemsize, opt.blob_vkallocator); |
| 429 | else |
| 430 | top_blob.create(N, M, elemsize, opt.blob_vkallocator); |
| 431 | } |
| 432 | if (top_blob.empty()) |
| 433 | return -100; |
| 434 | |
| 435 | std::vector<VkImageMat> bindings(4); |
| 436 | bindings[0] = top_blob; |
| 437 | bindings[1] = A; |
| 438 | bindings[2] = B; |
| 439 | bindings[3] = C; |
| 440 | |
| 441 | std::vector<vk_constant_type> constants(10); |
| 442 | constants[0].i = M; |
| 443 | constants[1].i = N; |
| 444 | constants[2].i = K; |
| 445 | constants[3].i = broadcast_type_C; |
| 446 | constants[4].i = A.dims; |
| 447 | constants[5].i = 0; //A.w; |
| 448 | constants[6].i = B.dims; |
| 449 | constants[7].i = 0; //B.w; |
| 450 | constants[8].i = top_blob.dims; |
| 451 | constants[9].i = 0; //top_blob.w; |
| 452 | |
| 453 | const Pipeline* pipeline = pipeline_gemm; |
| 454 | |
| 455 | VkImageMat dispatcher; |
| 456 | dispatcher.w = (N + 1) / 2; |
| 457 | dispatcher.h = (M + 1) / 2; |
| 458 | dispatcher.c = 1; |
| 459 | cmd.record_pipeline(pipeline, bindings, constants, dispatcher); |
| 460 | |
| 461 | int out_elempack = 1; |
| 462 | { |
| 463 | int outh = output_transpose ? N : M; |
| 464 | out_elempack = opt.use_shader_pack8 && outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1; |
| 465 | } |
| 466 | if (output_elempack) |
| 467 | out_elempack = output_elempack; |
| 468 | |
| 469 | if (out_elempack != 1) |
| 470 | { |
| 471 | VkImageMat top_blob0; |
| 472 | vkdev->convert_packing(top_blob, top_blob0, out_elempack, cmd, opt); |
| 473 | top_blobs[0] = top_blob0; |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | int Gemm_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 480 | { |
| 481 | std::vector<VkImageMat> bottom_blobs(1); |
| 482 | std::vector<VkImageMat> top_blobs(1); |
| 483 | bottom_blobs[0] = bottom_blob; |
| 484 | int ret = forward(bottom_blobs, top_blobs, cmd, opt); |
| 485 | top_blob = top_blobs[0]; |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | } // namespace ncnn |