File size: 6,701 Bytes
be903e2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | // 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.
#version 450
#if NCNN_fp16_storage
#extension GL_EXT_shader_16bit_storage: require
struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
#endif
#if NCNN_fp16_arithmetic
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
#endif
#extension GL_GOOGLE_include_directive: enable
#include "vulkan_activation.comp"
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;
#define shape_constant_id_offset 10
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;
#if NCNN_image_shader
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;
#else
layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
layout (binding = 3) readonly buffer bias_blob { sfpvec8 bias_data[]; };
#endif
layout (push_constant) uniform parameter
{
int w;
int h;
int c;
int cstep;
int outw;
int outh;
int outc;
int outcstep;
} p;
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;
afpvec8 sum0;
afpvec8 sum1;
afpvec8 sum2;
afpvec8 sum3;
if (bias_term == 1)
{
#if NCNN_image_shader
sum0 = image3d_ld8(bias_blob, ivec3(gy, 0, 0));
#else
sum0 = buffer_ld8(bias_data, gy);
#endif
sum1 = sum0;
sum2 = sum0;
sum3 = sum0;
}
else
{
sum0 = afpvec8(afpvec4(0.f), afpvec4(0.f));
sum1 = afpvec8(afpvec4(0.f), afpvec4(0.f));
sum2 = afpvec8(afpvec4(0.f), afpvec4(0.f));
sum3 = afpvec8(afpvec4(0.f), 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;
#if NCNN_image_shader
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));
afpvec8 k = image3d_ld8(weight_blob, ivec3(z, gy, 0));
// sum += v * k;
sum0[0] += v0 * k[0];
sum0[1] += v0 * k[1];
sum1[0] += v1 * k[0];
sum1[1] += v1 * k[1];
sum2[0] += v2 * k[0];
sum2[1] += v2 * k[1];
sum3[0] += v3 * k[0];
sum3[1] += v3 * k[1];
}
#else
int w_offset = gy * N;
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);
afpvec8 k = buffer_ld8(weight_data, w_offset);
// sum += v * k;
sum0[0] += v0 * k[0];
sum0[1] += v0 * k[1];
sum1[0] += v1 * k[0];
sum1[1] += v1 * k[1];
sum2[0] += v2 * k[0];
sum2[1] += v2 * k[1];
sum3[0] += v3 * k[0];
sum3[1] += v3 * k[1];
w_offset += 1;
}
#endif
sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1);
sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1);
sum2 = activation_afpvec8(sum2, activation_type, activation_param_0, activation_param_1);
sum3 = activation_afpvec8(sum3, activation_type, activation_param_0, activation_param_1);
#if NCNN_image_shader
image3d_st8(top_blob, ivec3(sx4.r, sy4.r, gy), sum0);
image3d_st8(top_blob, ivec3(sx4.g, sy4.g, gy), sum1);
image3d_st8(top_blob, ivec3(sx4.b, sy4.b, gy), sum2);
image3d_st8(top_blob, ivec3(sx4.a, sy4.a, gy), sum3);
#else
const int gi = gy * psc(outcstep) + gx;
buffer_st8(top_blob_data, gi, sum0);
if (gx + 1 < outsize) buffer_st8(top_blob_data, gi + 1, sum1);
if (gx + 2 < outsize) buffer_st8(top_blob_data, gi + 2, sum2);
if (gx + 3 < outsize) buffer_st8(top_blob_data, gi + 3, sum3);
#endif
}
|