Download src/layer/vulkan/shader/convolution_pack1to4_gemm.comp from camenduru/ncnn: direct link, hf CLI and curl.
- Browser
- Download file 9.22 kB
-
https://huggingface.co/camenduru/ncnn/resolve/main/src/layer/vulkan/shader/convolution_pack1to4_gemm.comp
- Command line
-
hf download hf://camenduru/ncnn/src/layer/vulkan/shader/convolution_pack1to4_gemm.comp
-
curl -L -o convolution_pack1to4_gemm.comp https://huggingface.co/camenduru/ncnn/resolve/main/src/layer/vulkan/shader/convolution_pack1to4_gemm.comp
9.22 kB
| // Tencent is pleased to support the open source community by making ncnn available. | |
| // | |
| // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. | |
| // | |
| // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | |
| // in compliance with the License. You may obtain a copy of the License at | |
| // | |
| // https://opensource.org/licenses/BSD-3-Clause | |
| // | |
| // Unless required by applicable law or agreed to in writing, software distributed | |
| // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
| // CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| // specific language governing permissions and limitations under the License. | |
| layout (constant_id = 0) const int kernel_w = 1; | |
| layout (constant_id = 1) const int kernel_h = 1; | |
| layout (constant_id = 2) const int dilation_w = 1; | |
| layout (constant_id = 3) const int dilation_h = 1; | |
| layout (constant_id = 4) const int stride_w = 1; | |
| layout (constant_id = 5) const int stride_h = 1; | |
| layout (constant_id = 6) const int bias_term = 0; | |
| layout (constant_id = 7) const int activation_type = 0; | |
| layout (constant_id = 8) const float activation_param_0 = 0; | |
| layout (constant_id = 9) const float activation_param_1 = 0; | |
| layout (constant_id = shape_constant_id_offset + 0) const int w = 0; | |
| layout (constant_id = shape_constant_id_offset + 1) const int h = 0; | |
| layout (constant_id = shape_constant_id_offset + 2) const int c = 0; | |
| layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0; | |
| layout (constant_id = shape_constant_id_offset + 4) const int outw = 0; | |
| layout (constant_id = shape_constant_id_offset + 5) const int outh = 0; | |
| layout (constant_id = shape_constant_id_offset + 6) const int outc = 0; | |
| layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0; | |
| layout (binding = 0) uniform unfp sampler3D bottom_blob; | |
| layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob; | |
| layout (binding = 2) uniform unfp sampler3D weight_blob; | |
| layout (binding = 3) uniform unfp sampler3D bias_blob; | |
| layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; }; | |
| layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; }; | |
| layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; }; | |
| layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; }; | |
| layout (push_constant) uniform parameter | |
| { | |
| int w; | |
| int h; | |
| int c; | |
| int cstep; | |
| int outw; | |
| int outh; | |
| int outc; | |
| int outcstep; | |
| } p; | |
| shared lfp tmp_v[8][LOCAL_MEMORY_UNROLL_INCH][4]; | |
| shared lfpvec4 tmp_k[8][LOCAL_MEMORY_UNROLL_INCH]; | |
| void main() | |
| { | |
| int gx = int(gl_GlobalInvocationID.x) * 4; | |
| int gy = int(gl_GlobalInvocationID.y); | |
| const int outsize = psc(outw) * psc(outh); | |
| if (gx >= outsize || gy >= psc(outc)) | |
| return; | |
| afpvec4 sum0; | |
| afpvec4 sum1; | |
| afpvec4 sum2; | |
| afpvec4 sum3; | |
| if (bias_term == 1) | |
| { | |
| sum0 = image3d_ld4(bias_blob, ivec3(gy, 0, 0)); | |
| sum0 = buffer_ld4(bias_data, gy); | |
| sum1 = sum0; | |
| sum2 = sum0; | |
| sum3 = sum0; | |
| } | |
| else | |
| { | |
| sum0 = afpvec4(0.f); | |
| sum1 = afpvec4(0.f); | |
| sum2 = afpvec4(0.f); | |
| sum3 = afpvec4(0.f); | |
| } | |
| const int maxk = kernel_w * kernel_h; | |
| const int N = psc(c) * maxk; | |
| const ivec4 gx4 = gx + ivec4(0, 1, 2, 3); | |
| const ivec4 sy4 = gx4 / psc(outw); | |
| const ivec4 sx4 = gx4 % psc(outw); | |
| const ivec4 sxs4 = sx4 * stride_w; | |
| const ivec4 sys4 = sy4 * stride_h; | |
| for (int z = 0; z < N; z++) | |
| { | |
| const int sz = z / maxk; | |
| const int kk = z % maxk; | |
| const int ky = kk / kernel_w; | |
| const int kx = kk % kernel_w; | |
| const ivec4 x4 = sxs4 + kx * dilation_w; | |
| const ivec4 y4 = sys4 + ky * dilation_h; | |
| afp v0 = image3d_ld1(bottom_blob, ivec3(x4.r, y4.r, sz)); | |
| afp v1 = image3d_ld1(bottom_blob, ivec3(x4.g, y4.g, sz)); | |
| afp v2 = image3d_ld1(bottom_blob, ivec3(x4.b, y4.b, sz)); | |
| afp v3 = image3d_ld1(bottom_blob, ivec3(x4.a, y4.a, sz)); | |
| afpvec4 k = image3d_ld4(weight_blob, ivec3(z, gy, 0)); | |
| sum0 += v0 * k; | |
| sum1 += v1 * k; | |
| sum2 += v2 * k; | |
| sum3 += v3 * k; | |
| } | |
| int w_offset = gy * N; | |
| const int lx = int(gl_LocalInvocationID.x); | |
| const int ly = int(gl_LocalInvocationID.y); | |
| int z = 0; | |
| for (; z + (LOCAL_MEMORY_UNROLL_INCH - 1) < N; z += LOCAL_MEMORY_UNROLL_INCH) | |
| { | |
| if (ly < 4) | |
| { | |
| for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) | |
| { | |
| const int sz = (z + z4) / maxk; | |
| const int k = (z + z4) % maxk; | |
| const int ky = k / kernel_w; | |
| const int kx = k % kernel_w; | |
| const int v_offset = sz * psc(cstep) + (sys4[ly] + ky * dilation_h) * psc(w) + sxs4[ly] + kx * dilation_w; | |
| tmp_v[lx][z4][ly] = sfp2lfp(bottom_blob_data[v_offset]); | |
| } | |
| } | |
| if (lx == 0) | |
| { | |
| for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) | |
| { | |
| tmp_k[ly][z4] = sfp2lfpvec4(weight_data[w_offset + z4]); | |
| } | |
| } | |
| barrier(); | |
| for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++) | |
| { | |
| afp v0 = lfp2afp(tmp_v[lx][z4][0]); | |
| afp v1 = lfp2afp(tmp_v[lx][z4][1]); | |
| afp v2 = lfp2afp(tmp_v[lx][z4][2]); | |
| afp v3 = lfp2afp(tmp_v[lx][z4][3]); | |
| afpvec4 k = lfp2afpvec4(tmp_k[ly][z4]); | |
| sum0 += v0 * k; | |
| sum1 += v1 * k; | |
| sum2 += v2 * k; | |
| sum3 += v3 * k; | |
| } | |
| w_offset += LOCAL_MEMORY_UNROLL_INCH; | |
| barrier(); | |
| } | |
| if (z < N) | |
| { | |
| const int remain = N - z; | |
| if (ly < 4) | |
| { | |
| for (int z4 = 0; z4 < remain; z4++) | |
| { | |
| const int sz = (z + z4) / maxk; | |
| const int k = (z + z4) % maxk; | |
| const int ky = k / kernel_w; | |
| const int kx = k % kernel_w; | |
| const int v_offset = sz * psc(cstep) + (sys4[ly] + ky * dilation_h) * psc(w) + sxs4[ly] + kx * dilation_w; | |
| tmp_v[lx][z4][ly] = sfp2lfp(bottom_blob_data[v_offset]); | |
| } | |
| } | |
| if (lx == 0) | |
| { | |
| for (int z4 = 0; z4 < remain; z4++) | |
| { | |
| tmp_k[ly][z4] = sfp2lfpvec4(weight_data[w_offset + z4]); | |
| } | |
| } | |
| barrier(); | |
| for (int z4 = 0; z4 < remain; z4++) | |
| { | |
| afp v0 = lfp2afp(tmp_v[lx][z4][0]); | |
| afp v1 = lfp2afp(tmp_v[lx][z4][1]); | |
| afp v2 = lfp2afp(tmp_v[lx][z4][2]); | |
| afp v3 = lfp2afp(tmp_v[lx][z4][3]); | |
| afpvec4 k = lfp2afpvec4(tmp_k[ly][z4]); | |
| sum0 += v0 * k; | |
| sum1 += v1 * k; | |
| sum2 += v2 * k; | |
| sum3 += v3 * k; | |
| } | |
| } | |
| for (int z = 0; z < N; z++) | |
| { | |
| const int sz = z / maxk; | |
| const int kk = z % maxk; | |
| const int ky = kk / kernel_w; | |
| const int kx = kk % kernel_w; | |
| const ivec4 v_offset = sz * psc(cstep) + (sys4 + ky * dilation_h) * psc(w) + sxs4 + kx * dilation_w; | |
| afp v0 = buffer_ld1(bottom_blob_data, v_offset.r); | |
| afp v1 = buffer_ld1(bottom_blob_data, v_offset.g); | |
| afp v2 = buffer_ld1(bottom_blob_data, v_offset.b); | |
| afp v3 = buffer_ld1(bottom_blob_data, v_offset.a); | |
| afpvec4 k = buffer_ld4(weight_data, w_offset); | |
| sum0 += v0 * k; | |
| sum1 += v1 * k; | |
| sum2 += v2 * k; | |
| sum3 += v3 * k; | |
| w_offset += 1; | |
| } | |
| if (gx >= outsize || gy >= psc(outc)) | |
| return; | |
| sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1); | |
| sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1); | |
| sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1); | |
| sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1); | |
| image3d_st4(top_blob, ivec3(sx4.r, sy4.r, gy), sum0); | |
| image3d_st4(top_blob, ivec3(sx4.g, sy4.g, gy), sum1); | |
| image3d_st4(top_blob, ivec3(sx4.b, sy4.b, gy), sum2); | |
| image3d_st4(top_blob, ivec3(sx4.a, sy4.a, gy), sum3); | |
| const int gi = gy * psc(outcstep) + gx; | |
| buffer_st4(top_blob_data, gi, sum0); | |
| if (gx + 1 < outsize) buffer_st4(top_blob_data, gi + 1, sum1); | |
| if (gx + 2 < outsize) buffer_st4(top_blob_data, gi + 2, sum2); | |
| if (gx + 3 < outsize) buffer_st4(top_blob_data, gi + 3, sum3); | |
| } | |