ncnn

Форк
0
/
convolution1d_pack8to4.comp 
208 строк · 8.0 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2023 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 kernel_w = 1;
29
layout (constant_id = 1) const int dilation_w = 1;
30
layout (constant_id = 2) const int stride_w = 1;
31
layout (constant_id = 3) const int bias_term = 0;
32
layout (constant_id = 4) const int activation_type = 0;
33
layout (constant_id = 5) const float activation_param_0 = 0;
34
layout (constant_id = 6) const float activation_param_1 = 0;
35

36
#define shape_constant_id_offset 7
37
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
38
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
39

40
layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
41
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
42

43
#if NCNN_image_shader
44
layout (binding = 0) uniform unfp sampler3D bottom_blob;
45
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
46
layout (binding = 2) uniform unfp sampler3D weight_blob;
47
layout (binding = 3) uniform unfp sampler3D bias_blob;
48
#else
49
layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
50
layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
51
layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
52
layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
53
#endif
54

55
layout (push_constant) uniform parameter
56
{
57
    int w;
58
    int h;
59

60
    int outw;
61
    int outh;
62
} p;
63

64
void main()
65
{
66
    int gx = int(gl_GlobalInvocationID.x) * 2;
67
    int gy = int(gl_GlobalInvocationID.y) * 2;
68

69
    if (gx >= psc(outw) || gy >= psc(outh))
70
        return;
71

72
    const ivec2 gx2 = gx + ivec2(0, 1);
73
    const ivec2 gy2 = gy + ivec2(0, 1);
74

75
    afpvec4 sum0 = afpvec4(0.0f);
76
    afpvec4 sum1 = afpvec4(0.0f);
77
    afpvec4 sum2 = afpvec4(0.0f);
78
    afpvec4 sum3 = afpvec4(0.0f);
79

80
    if (bias_term == 1)
81
    {
82
#if NCNN_image_shader
83
        sum0 = image3d_ld4(bias_blob, ivec3(gy2.x, 0, 0));
84
        sum2 = image3d_ld4(bias_blob, ivec3(gy2.y, 0, 0));
85
#else
86
        sum0 = buffer_ld4(bias_data, gy2.x);
87
        sum2 = buffer_ld4(bias_data, gy2.y);
88
#endif
89
        sum1 = sum0;
90
        sum3 = sum2;
91
    }
92

93
#if NCNN_image_shader
94

95
    ivec2 v_offset = gx2 * stride_w;
96

97
    for (int y = 0; y < psc(h); y++)
98
    {
99
        int wx = 0;
100

101
        for (int x = 0; x < kernel_w; x++)
102
        {
103
            afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(v_offset.x + x * dilation_w, y, 0));
104
            afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(v_offset.y + x * dilation_w, y, 0));
105
            
106
            afpvec8 k0 = image3d_ld8(weight_blob, ivec3(wx + 0, y, gy2.x));
107
            afpvec8 k1 = image3d_ld8(weight_blob, ivec3(wx + 1, y, gy2.x));
108
            afpvec8 k2 = image3d_ld8(weight_blob, ivec3(wx + 2, y, gy2.x));
109
            afpvec8 k3 = image3d_ld8(weight_blob, ivec3(wx + 3, y, gy2.x));
110
            afpvec8 k4 = image3d_ld8(weight_blob, ivec3(wx + 0, y, gy2.y));
111
            afpvec8 k5 = image3d_ld8(weight_blob, ivec3(wx + 1, y, gy2.y));
112
            afpvec8 k6 = image3d_ld8(weight_blob, ivec3(wx + 2, y, gy2.y));
113
            afpvec8 k7 = image3d_ld8(weight_blob, ivec3(wx + 3, y, gy2.y));
114

115
            sum0.r += dot(v0[0], k0[0]) + dot(v0[1], k0[1]);
116
            sum0.g += dot(v0[0], k1[0]) + dot(v0[1], k1[1]);
117
            sum0.b += dot(v0[0], k2[0]) + dot(v0[1], k2[1]);
118
            sum0.a += dot(v0[0], k3[0]) + dot(v0[1], k3[1]);
119

120
            sum1.r += dot(v1[0], k0[0]) + dot(v1[1], k0[1]);
121
            sum1.g += dot(v1[0], k1[0]) + dot(v1[1], k1[1]);
122
            sum1.b += dot(v1[0], k2[0]) + dot(v1[1], k2[1]);
123
            sum1.a += dot(v1[0], k3[0]) + dot(v1[1], k3[1]);
124

125
            sum2.r += dot(v0[0], k4[0]) + dot(v0[1], k4[1]);
126
            sum2.g += dot(v0[0], k5[0]) + dot(v0[1], k5[1]);
127
            sum2.b += dot(v0[0], k6[0]) + dot(v0[1], k6[1]);
128
            sum2.a += dot(v0[0], k7[0]) + dot(v0[1], k7[1]);
129

130
            sum3.r += dot(v1[0], k4[0]) + dot(v1[1], k4[1]);
131
            sum3.g += dot(v1[0], k5[0]) + dot(v1[1], k5[1]);
132
            sum3.b += dot(v1[0], k6[0]) + dot(v1[1], k6[1]);
133
            sum3.a += dot(v1[0], k7[0]) + dot(v1[1], k7[1]);
134

135
            wx += 4;
136
        }
137
    }
138
    
139
#else
140

141
    ivec2 v_offset = gx2 * stride_w;
142
    ivec2 w_offset = gy2 * psc(h) * kernel_w;
143
    
144
    for (int y = 0; y < psc(h); y++)
145
    {    
146
        for (int x = 0; x < kernel_w; x++)
147
        {
148
            afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset.x + x * dilation_w);
149
            afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset.y + x * dilation_w);
150
            
151
            afpvec8 k0 = buffer_ld8(weight_data, (w_offset.x + x) * 4 + 0);
152
            afpvec8 k1 = buffer_ld8(weight_data, (w_offset.x + x) * 4 + 1);
153
            afpvec8 k2 = buffer_ld8(weight_data, (w_offset.x + x) * 4 + 2);
154
            afpvec8 k3 = buffer_ld8(weight_data, (w_offset.x + x) * 4 + 3);
155
            afpvec8 k4 = buffer_ld8(weight_data, (w_offset.y + x) * 4 + 0);
156
            afpvec8 k5 = buffer_ld8(weight_data, (w_offset.y + x) * 4 + 1);
157
            afpvec8 k6 = buffer_ld8(weight_data, (w_offset.y + x) * 4 + 2);
158
            afpvec8 k7 = buffer_ld8(weight_data, (w_offset.y + x) * 4 + 3);
159

160
            sum0.r += dot(v0[0], k0[0]) + dot(v0[1], k0[1]);
161
            sum0.g += dot(v0[0], k1[0]) + dot(v0[1], k1[1]);
162
            sum0.b += dot(v0[0], k2[0]) + dot(v0[1], k2[1]);
163
            sum0.a += dot(v0[0], k3[0]) + dot(v0[1], k3[1]);
164

165
            sum1.r += dot(v1[0], k0[0]) + dot(v1[1], k0[1]);
166
            sum1.g += dot(v1[0], k1[0]) + dot(v1[1], k1[1]);
167
            sum1.b += dot(v1[0], k2[0]) + dot(v1[1], k2[1]);
168
            sum1.a += dot(v1[0], k3[0]) + dot(v1[1], k3[1]);
169

170
            sum2.r += dot(v0[0], k4[0]) + dot(v0[1], k4[1]);
171
            sum2.g += dot(v0[0], k5[0]) + dot(v0[1], k5[1]);
172
            sum2.b += dot(v0[0], k6[0]) + dot(v0[1], k6[1]);
173
            sum2.a += dot(v0[0], k7[0]) + dot(v0[1], k7[1]);
174

175
            sum3.r += dot(v1[0], k4[0]) + dot(v1[1], k4[1]);
176
            sum3.g += dot(v1[0], k5[0]) + dot(v1[1], k5[1]);
177
            sum3.b += dot(v1[0], k6[0]) + dot(v1[1], k6[1]);
178
            sum3.a += dot(v1[0], k7[0]) + dot(v1[1], k7[1]);
179
        }       
180
        v_offset += psc(w);
181
        w_offset += kernel_w;
182
    }
183
    
184
#endif
185

186
    sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
187
    sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
188
    sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1);
189
    sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1);
190
    
191
#if NCNN_image_shader
192

193
    image3d_st4(top_blob, ivec3(gx2.x, gy2.x, 0), sum0);
194
    image3d_st4(top_blob, ivec3(gx2.y, gy2.x, 0), sum1);
195
    image3d_st4(top_blob, ivec3(gx2.x, gy2.y, 0), sum2);
196
    image3d_st4(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);
197
    
198
#else
199

200
    const int gi = gy * psc(outw) + gx;
201

202
    buffer_st4(top_blob_data, gi, sum0);
203
    if (gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi + 1, sum1);
204
    if (gy + 1 < psc(outh)) buffer_st4(top_blob_data, gi + psc(outw), sum2);
205
    if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi + psc(outw) + 1, sum3);
206
    
207
#endif
208
}
209

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.