| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 | #version 450 |
| 16 | |
| 17 | #if NCNN_fp16_storage |
| 18 | #extension GL_EXT_shader_16bit_storage: require |
| 19 | struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; }; |
| 20 | #endif |
| 21 | #if NCNN_fp16_arithmetic |
| 22 | #extension GL_EXT_shader_explicit_arithmetic_types_float16: require |
| 23 | #endif |
| 24 | |
| 25 | #extension GL_GOOGLE_include_directive: enable |
| 26 | #include "vulkan_activation.comp" |
| 27 | |
| 28 | layout (constant_id = 0) const int bias_term = 0; |
| 29 | layout (constant_id = 1) const int activation_type = 0; |
| 30 | layout (constant_id = 2) const float activation_param_0 = 0; |
| 31 | layout (constant_id = 3) const float activation_param_1 = 0; |
| 32 | |
| 33 | #define shape_constant_id_offset 4 |
| 34 | layout (constant_id = shape_constant_id_offset + 0) const int w = 0; |
| 35 | layout (constant_id = shape_constant_id_offset + 1) const int h = 0; |
| 36 | layout (constant_id = shape_constant_id_offset + 2) const int c = 0; |
| 37 | layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0; |
| 38 | |
| 39 | layout (constant_id = shape_constant_id_offset + 4) const int outw = 0; |
| 40 | layout (constant_id = shape_constant_id_offset + 5) const int outh = 0; |
| 41 | layout (constant_id = shape_constant_id_offset + 6) const int outc = 0; |
| 42 | layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0; |
| 43 | |
| 44 | #if NCNN_image_shader |
| 45 | layout (binding = 0) uniform unfp sampler3D bottom_blob; |
| 46 | layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob; |
| 47 | layout (binding = 2) uniform unfp sampler3D weight_blob; |
| 48 | layout (binding = 3) uniform unfp sampler3D bias_blob; |
| 49 | #else |
| 50 | layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; }; |
| 51 | layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; }; |
| 52 | layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; }; |
| 53 | layout (binding = 3) readonly buffer bias_blob { sfpvec8 bias_data[]; }; |
| 54 | #endif |
| 55 | |
| 56 | layout (push_constant) uniform parameter |
| 57 | { |
| 58 | int w; |
| 59 | int h; |
| 60 | int c; |
| 61 | int cstep; |
| 62 | |
| 63 | int outw; |
| 64 | int outh; |
| 65 | int outc; |
| 66 | int outcstep; |
| 67 | } p; |
| 68 | |
| 69 | void main() |
| 70 | { |
| 71 | int gx = int(gl_GlobalInvocationID.x) * 4; |
| 72 | int gy = int(gl_GlobalInvocationID.y); |
| 73 | |
| 74 | #if NCNN_image_shader |
| 75 | if (gx >= psc(outw) * psc(outh) || gy >= psc(outc)) |
| 76 | return; |
| 77 | #else |
| 78 | if (gx >= psc(outcstep) || gy >= psc(outc)) |
| 79 | return; |
| 80 | #endif |
| 81 | |
| 82 | afpvec8 sum0; |
| 83 | afpvec8 sum1; |
| 84 | afpvec8 sum2; |
| 85 | afpvec8 sum3; |
| 86 | |
| 87 | if (bias_term == 1) |
| 88 | { |
| 89 | #if NCNN_image_shader |
| 90 | afpvec8 b = image3d_ld8(bias_blob, ivec3(gy, 0, 0)); |
| 91 | #else |
| 92 | afpvec8 b = buffer_ld8(bias_data, gy); |
| 93 | #endif |
| 94 | sum0 = b; |
| 95 | sum1 = b; |
| 96 | sum2 = b; |
| 97 | sum3 = b; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | sum0 = afpvec8(afpvec4(0.f), afpvec4(0.f)); |
| 102 | sum1 = afpvec8(afpvec4(0.f), afpvec4(0.f)); |
| 103 | sum2 = afpvec8(afpvec4(0.f), afpvec4(0.f)); |
| 104 | sum3 = afpvec8(afpvec4(0.f), afpvec4(0.f)); |
| 105 | } |
| 106 | |
| 107 | #if NCNN_image_shader |
| 108 | ivec4 gx4 = gx + ivec4(0, 1, 2, 3); |
| 109 | |
| 110 | ivec4 sy4 = gx4 / psc(w); |
| 111 | ivec4 sx4 = gx4 % psc(w); |
| 112 | |
| 113 | for (int z = 0; z < psc(c); z++) |
| 114 | { |
| 115 | afp v0 = image3d_ld1(bottom_blob, ivec3(sx4.r, sy4.r, z)); |
| 116 | afp v1 = image3d_ld1(bottom_blob, ivec3(sx4.g, sy4.g, z)); |
| 117 | afp v2 = image3d_ld1(bottom_blob, ivec3(sx4.b, sy4.b, z)); |
| 118 | afp v3 = image3d_ld1(bottom_blob, ivec3(sx4.a, sy4.a, z)); |
| 119 | |
| 120 | afpvec8 k = image3d_ld8(weight_blob, ivec3(0, z, gy)); |
| 121 | |
| 122 | // sum += v * k; |
| 123 | sum0[0] += v0 * k[0]; |
| 124 | sum0[1] += v0 * k[1]; |
| 125 | |
| 126 | sum1[0] += v1 * k[0]; |
| 127 | sum1[1] += v1 * k[1]; |
| 128 | |
| 129 | sum2[0] += v2 * k[0]; |
| 130 | sum2[1] += v2 * k[1]; |
| 131 | |
| 132 | sum3[0] += v3 * k[0]; |
| 133 | sum3[1] += v3 * k[1]; |
| 134 | } |
| 135 | #else |
| 136 | int w_offset = gy * psc(c); |
| 137 | int v_offset = gx; |
| 138 | |
| 139 | for (int z = 0; z < psc(c); z++) |
| 140 | { |
| 141 | afp v0 = buffer_ld1(bottom_blob_data, v_offset + 0); |
| 142 | afp v1 = buffer_ld1(bottom_blob_data, v_offset + 1); |
| 143 | afp v2 = buffer_ld1(bottom_blob_data, v_offset + 2); |
| 144 | afp v3 = buffer_ld1(bottom_blob_data, v_offset + 3); |
| 145 | |
| 146 | afpvec8 k = buffer_ld8(weight_data, w_offset); |
| 147 | |
| 148 | // sum += v * k; |
| 149 | sum0[0] += v0 * k[0]; |
| 150 | sum0[1] += v0 * k[1]; |
| 151 | |
| 152 | sum1[0] += v1 * k[0]; |
| 153 | sum1[1] += v1 * k[1]; |
| 154 | |
| 155 | sum2[0] += v2 * k[0]; |
| 156 | sum2[1] += v2 * k[1]; |
| 157 | |
| 158 | sum3[0] += v3 * k[0]; |
| 159 | sum3[1] += v3 * k[1]; |
| 160 | |
| 161 | w_offset += 1; |
| 162 | v_offset += psc(cstep); |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1); |
| 167 | sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1); |
| 168 | sum2 = activation_afpvec8(sum2, activation_type, activation_param_0, activation_param_1); |
| 169 | sum3 = activation_afpvec8(sum3, activation_type, activation_param_0, activation_param_1); |
| 170 | |
| 171 | #if NCNN_image_shader |
| 172 | image3d_st8(top_blob, ivec3(sx4.r, sy4.r, gy), sum0); |
| 173 | image3d_st8(top_blob, ivec3(sx4.g, sy4.g, gy), sum1); |
| 174 | image3d_st8(top_blob, ivec3(sx4.b, sy4.b, gy), sum2); |
| 175 | image3d_st8(top_blob, ivec3(sx4.a, sy4.a, gy), sum3); |
| 176 | #else |
| 177 | int gi = gy * psc(outcstep) + gx; |
| 178 | |
| 179 | buffer_st8(top_blob_data, gi + 0, sum0); |
| 180 | if (gx + 1 < psc(outcstep)) buffer_st8(top_blob_data, gi + 1, sum1); |
| 181 | if (gx + 2 < psc(outcstep)) buffer_st8(top_blob_data, gi + 2, sum2); |
| 182 | if (gx + 3 < psc(outcstep)) buffer_st8(top_blob_data, gi + 3, sum3); |
| 183 | #endif |
| 184 | } |