You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convolutiondepthwise_group_pack4.comp 3.8 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #version 450
  15. #if NCNN_fp16_storage
  16. #extension GL_EXT_shader_16bit_storage: require
  17. #endif
  18. #if NCNN_fp16_arithmetic
  19. #extension GL_AMD_gpu_shader_half_float: require
  20. #endif
  21. layout (constant_id = 0) const int kernel_w = 1;
  22. layout (constant_id = 1) const int kernel_h = 1;
  23. layout (constant_id = 2) const int dilation_w = 1;
  24. layout (constant_id = 3) const int dilation_h = 1;
  25. layout (constant_id = 4) const int stride_w = 1;
  26. layout (constant_id = 5) const int stride_h = 1;
  27. layout (constant_id = 6) const int bias_term = 0;
  28. layout (constant_id = 7) const int group = 1;
  29. layout (local_size_x_id = 233) in;
  30. layout (local_size_y_id = 234) in;
  31. layout (local_size_z_id = 235) in;
  32. layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
  33. layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
  34. #if NCNN_fp16_storage && !NCNN_fp16_arithmetic
  35. // GL_EXT_shader_16bit_storage does not define f16mat4 type :(
  36. layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
  37. #else
  38. layout (binding = 2) readonly buffer weight_blob { sfpmat4 weight_data[]; };
  39. #endif
  40. layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
  41. layout (push_constant) uniform parameter
  42. {
  43. int dims;
  44. int w;
  45. int h;
  46. int c;
  47. int cstep;
  48. int outdims;
  49. int outw;
  50. int outh;
  51. int outc;
  52. int outcstep;
  53. } p;
  54. void main()
  55. {
  56. int gx = int(gl_GlobalInvocationID.x);
  57. int gy = int(gl_GlobalInvocationID.y);
  58. int gz = int(gl_GlobalInvocationID.z);
  59. if (gx >= p.outw || gy >= p.outh || gz >= p.outc)
  60. return;
  61. afpvec4 sum;
  62. if (bias_term == 1)
  63. {
  64. sum = afpvec4(bias_data[gz]);
  65. }
  66. else
  67. {
  68. sum = afpvec4(0.f);
  69. }
  70. // group convolution
  71. const int channels_g = p.c / group;
  72. const int num_output_g = p.outc / group;
  73. // group id
  74. const int gg = gz / num_output_g;
  75. int w_offset = gz * channels_g * kernel_w * kernel_h;
  76. int v_offset_0 = gg * channels_g * p.cstep;
  77. for (int z = 0; z < channels_g; z++)
  78. {
  79. int v_offset = v_offset_0 + gy * stride_h * p.w + gx * stride_w;
  80. for (int y = 0; y < kernel_h; y++)
  81. {
  82. for (int x = 0; x < kernel_w; x++)
  83. {
  84. afpvec4 v = afpvec4(bottom_blob_data[v_offset + x * dilation_w]);
  85. #if NCNN_fp16_storage && !NCNN_fp16_arithmetic
  86. // GL_EXT_shader_16bit_storage does not define f16mat4 type :(
  87. afpmat4 k = afpmat4(
  88. afpvec4(weight_data[(w_offset + x) * 4 + 0]),
  89. afpvec4(weight_data[(w_offset + x) * 4 + 1]),
  90. afpvec4(weight_data[(w_offset + x) * 4 + 2]),
  91. afpvec4(weight_data[(w_offset + x) * 4 + 3])
  92. );
  93. #else
  94. afpmat4 k = weight_data[w_offset + x];
  95. #endif
  96. sum += v * k;
  97. }
  98. v_offset += dilation_h * p.w;
  99. w_offset += kernel_w;
  100. }
  101. v_offset_0 += p.cstep;
  102. }
  103. top_blob_data[gz * p.outcstep + gy * p.outw + gx] = sfpvec4(sum);
  104. }