ncnn

Форк
0
/
binaryop_broadcast_pack8.comp 
263 строки · 6.9 Кб
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
layout (constant_id = 0) const int op_type = 0;
26

27
#define shape_constant_id_offset 1
28
layout (constant_id = shape_constant_id_offset + 0) const int adims = 0;
29
layout (constant_id = shape_constant_id_offset + 1) const int aw = 0;
30
layout (constant_id = shape_constant_id_offset + 2) const int ah = 0;
31
layout (constant_id = shape_constant_id_offset + 3) const int ad = 0;
32
layout (constant_id = shape_constant_id_offset + 4) const int ac = 0;
33
layout (constant_id = shape_constant_id_offset + 5) const int acstep = 0;
34

35
layout (constant_id = shape_constant_id_offset + 6) const int bdims = 0;
36
layout (constant_id = shape_constant_id_offset + 7) const int bw = 0;
37
layout (constant_id = shape_constant_id_offset + 8) const int bh = 0;
38
layout (constant_id = shape_constant_id_offset + 9) const int bd = 0;
39
layout (constant_id = shape_constant_id_offset + 10) const int bc = 0;
40
layout (constant_id = shape_constant_id_offset + 11) const int bcstep = 0;
41

42
layout (constant_id = shape_constant_id_offset + 12) const int outdims = 0;
43
layout (constant_id = shape_constant_id_offset + 13) const int outw = 0;
44
layout (constant_id = shape_constant_id_offset + 14) const int outh = 0;
45
layout (constant_id = shape_constant_id_offset + 15) const int outd = 0;
46
layout (constant_id = shape_constant_id_offset + 16) const int outc = 0;
47
layout (constant_id = shape_constant_id_offset + 17) const int outcstep = 0;
48

49
#if NCNN_image_shader
50
layout (binding = 0) uniform unfp sampler3D a_blob_3d;
51
layout (binding = 1) uniform unfp sampler3D b_blob_3d;
52
layout (binding = 2, imfmtc4) writeonly uniform unfp image3D top_blob_3d;
53
#else
54
layout (binding = 0) readonly buffer a_blob { sfpvec8 a_blob_data[]; };
55
layout (binding = 1) readonly buffer b_blob { sfpvec8 b_blob_data[]; };
56
layout (binding = 2) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
57
#endif
58

59
layout (push_constant) uniform parameter
60
{
61
    int adims;
62
    int aw;
63
    int ah;
64
    int ad;
65
    int ac;
66
    int acstep;
67

68
    int bdims;
69
    int bw;
70
    int bh;
71
    int bd;
72
    int bc;
73
    int bcstep;
74

75
    int outdims;
76
    int outw;
77
    int outh;
78
    int outd;
79
    int outc;
80
    int outcstep;
81
} p;
82

83
void main()
84
{
85
    int gx = int(gl_GlobalInvocationID.x);
86
    int gy = int(gl_GlobalInvocationID.y);
87
    int gz = int(gl_GlobalInvocationID.z);
88

89
    if (gx >= psc(outw) || gy >= psc(outh) * psc(outd) || gz >= psc(outc))
90
        return;
91

92
    int yd = gy / psc(outh);
93
    int yh = gy % psc(outh);
94

95
    int ax = gx;
96
    int ay = gy;
97
    int az = gz;
98

99
    int bx = gx;
100
    int by = gy;
101
    int bz = gz;
102

103
    if (psc(adims) == psc(bdims))
104
    {
105
        // explicit broadcast
106
        ax = min(gx, psc(aw) - 1);
107
        ay = min(yd, psc(ad) - 1) * psc(ah) + min(yh, psc(ah) - 1);
108
        az = min(gz, psc(ac) - 1);
109

110
        bx = min(gx, psc(bw) - 1);
111
        by = min(yd, psc(bd) - 1) * psc(bh) + min(yh, psc(bh) - 1);
112
        bz = min(gz, psc(bc) - 1);
113
    }
114
    else
115
    {
116
        // implicit broadcast
117
        if (psc(adims) == 4)
118
        {
119
            if (psc(bdims) == 3)
120
            {
121
                // type 13
122
                bx = yh;
123
                by = yd;
124
                bz = gz;
125
            }
126

127
            if (psc(bdims) == 2)
128
            {
129
                // type 12
130
                bx = yd;
131
                by = gz;
132
                bz = 0;
133
            }
134

135
            if (psc(bdims) == 1)
136
            {
137
                // type 11
138
                bx = gz;
139
                by = 0;
140
                bz = 0;
141
            }
142
        }
143
        else if (psc(adims) == 3)
144
        {
145
            if (psc(bdims) == 2)
146
            {
147
                // type 10
148
                bx = gy;
149
                by = gz;
150
                bz = 0;
151
            }
152

153
            if (psc(bdims) == 1)
154
            {
155
                // type 9
156
                bx = gz;
157
                by = 0;
158
                bz = 0;
159
            }
160
        }
161
        else // if (psc(adims) == 2)
162
        {
163
            // if (psc(bdims) == 1)
164
            {
165
                // type 8
166
                bx = gy;
167
                by = 0;
168
                bz = 0;
169
            }
170
        }
171
    }
172

173
#if NCNN_image_shader
174
    afpvec8 v1 = image3d_ld8(a_blob_3d, ivec3(ax, ay, az));
175
    afpvec8 v2 = image3d_ld8(b_blob_3d, ivec3(bx, by, bz));
176
#else
177
    int ai = az * psc(acstep) + ay * psc(aw) + ax;
178
    int bi = bz * psc(bcstep) + by * psc(bw) + bx;
179

180
    afpvec8 v1 = buffer_ld8(a_blob_data, ai);
181
    afpvec8 v2 = buffer_ld8(b_blob_data, bi);
182
#endif
183

184
    afpvec8 res;
185

186
    if (op_type == 0)
187
    {
188
        res[0] = v1[0] + v2[0];
189
        res[1] = v1[1] + v2[1];
190
    }
191
    if (op_type == 1)
192
    {
193
        res[0] = v1[0] - v2[0];
194
        res[1] = v1[1] - v2[1];
195
    }
196
    if (op_type == 2)
197
    {
198
        res[0] = v1[0] * v2[0];
199
        res[1] = v1[1] * v2[1];
200
    }
201
    if (op_type == 3)
202
    {
203
        res[0] = v1[0] / v2[0];
204
        res[1] = v1[1] / v2[1];
205
    }
206
    if (op_type == 4)
207
    {
208
        res[0] = max(v1[0], v2[0]);
209
        res[1] = max(v1[1], v2[1]);
210
    }
211
    if (op_type == 5)
212
    {
213
        res[0] = min(v1[0], v2[0]);
214
        res[1] = min(v1[1], v2[1]);
215
    }
216
    if (op_type == 6)
217
    {
218
        res[0] = pow(v1[0], v2[0]);
219
        res[1] = pow(v1[1], v2[1]);
220
    }
221
    if (op_type == 7)
222
    {
223
        res[0] = v2[0] - v1[0];
224
        res[1] = v2[1] - v1[1];
225
    }
226
    if (op_type == 8)
227
    {
228
        res[0] = v2[0] / v1[0];
229
        res[1] = v2[1] / v1[1];
230
    }
231
    if (op_type == 9)
232
    {
233
        res[0] = pow(v2[0], v1[0]);
234
        res[1] = pow(v2[1], v1[1]);
235
    }
236
    if (op_type == 10)
237
    {
238
#if NCNN_moltenvk
239
        res[0] = afpvec4(atan(vec4(v1[0]), vec4(v2[0])));
240
        res[1] = afpvec4(atan(vec4(v1[1]), vec4(v2[1])));
241
#else
242
        res[0] = atan(v1[0], v2[0]);
243
        res[1] = atan(v1[1], v2[1]);
244
#endif
245
    }
246
    if (op_type == 11)
247
    {
248
#if NCNN_moltenvk
249
        res[0] = afpvec4(atan(vec4(v2[0]), vec4(v1[0])));
250
        res[1] = afpvec4(atan(vec4(v2[1]), vec4(v1[1])));
251
#else
252
        res[0] = atan(v2[0], v1[0]);
253
        res[1] = atan(v2[1], v1[1]);
254
#endif
255
    }
256

257
#if NCNN_image_shader
258
    image3d_st8(top_blob_3d, ivec3(gx, gy, gz), res);
259
#else
260
    int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
261
    buffer_st8(top_blob_data, gi, res);
262
#endif
263
}
264

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

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

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

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