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_pack4.comp 3.5 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 (constant_id = 8) const int activation_type = 0;
  30. layout (constant_id = 9) const float activation_param_0 = 0;
  31. layout (constant_id = 10) const float activation_param_1 = 0;
  32. layout (local_size_x_id = 233) in;
  33. layout (local_size_y_id = 234) in;
  34. layout (local_size_z_id = 235) in;
  35. layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
  36. layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
  37. layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
  38. layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
  39. layout (push_constant) uniform parameter
  40. {
  41. int dims;
  42. int w;
  43. int h;
  44. int c;
  45. int cstep;
  46. int outdims;
  47. int outw;
  48. int outh;
  49. int outc;
  50. int outcstep;
  51. } p;
  52. void main()
  53. {
  54. int gx = int(gl_GlobalInvocationID.x);
  55. int gy = int(gl_GlobalInvocationID.y);
  56. int gz = int(gl_GlobalInvocationID.z);
  57. if (gx >= p.outw || gy >= p.outh || gz >= p.outc)
  58. return;
  59. afpvec4 sum;
  60. if (bias_term == 1)
  61. {
  62. sum = afpvec4(bias_data[gz]);
  63. }
  64. else
  65. {
  66. sum = afpvec4(0.f);
  67. }
  68. // depth-wise convolution
  69. int w_offset = gz * kernel_w * kernel_h;
  70. int v_offset = gz * p.cstep + gy * stride_h * p.w + gx * stride_w;
  71. for (int y = 0; y < kernel_h; y++)
  72. {
  73. for (int x = 0; x < kernel_w; x++)
  74. {
  75. afpvec4 v = afpvec4(bottom_blob_data[v_offset + x * dilation_w]);
  76. afpvec4 k = afpvec4(weight_data[w_offset + x]);
  77. sum += v * k;
  78. }
  79. v_offset += dilation_h * p.w;
  80. w_offset += kernel_w;
  81. }
  82. if (activation_type == 1)
  83. {
  84. sum = max(sum, afp(0.f));
  85. }
  86. if (activation_type == 2)
  87. {
  88. const afp slope = afp(activation_param_0);
  89. sum = mix(sum, sum * afp(slope), lessThan(sum, afpvec4(0.f)));
  90. }
  91. if (activation_type == 3)
  92. {
  93. const afp const_min = afp(activation_param_0);
  94. const afp const_max = afp(activation_param_1);
  95. sum = clamp(sum, const_min, const_max);
  96. }
  97. if (activation_type == 4)
  98. {
  99. sum = afp(1.f) / (afp(1.f) + exp(-sum));
  100. }
  101. top_blob_data[gz * p.outcstep + gy * p.outw + gx] = sfpvec4(sum);
  102. }