ncnn

Форк
0
/
softmax_reduce_max.comp 
192 строки · 5.8 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2019 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
#endif
20
#if NCNN_fp16_arithmetic
21
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
22
#endif
23

24
layout (constant_id = 0) const int axis = 0;
25

26
#define shape_constant_id_offset 1
27
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
28
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
29
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
30
layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
31
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
32

33
layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
34
layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
35
layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
36
layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
37
layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
38

39
#if NCNN_image_shader
40
layout (binding = 0) uniform unfp sampler3D bottom_top_blob_3d;
41
layout (binding = 1, imfmtc1) writeonly uniform unfp image3D max_workspace_3d;
42
#else
43
layout (binding = 0) readonly buffer bottom_top_blob { sfp bottom_top_blob_data[]; };
44
layout (binding = 1) writeonly buffer max_workspace { sfp max_workspace_data[]; };
45
#endif
46

47
layout (push_constant) uniform parameter
48
{
49
    int dims;
50
    int w;
51
    int h;
52
    int c;
53
    int cstep;
54

55
    int outdims;
56
    int outw;
57
    int outh;
58
    int outc;
59
    int outcstep;
60
} p;
61

62
void main()
63
{
64
    int gx = int(gl_GlobalInvocationID.x);
65
    int gy = int(gl_GlobalInvocationID.y);
66
    int gz = int(gl_GlobalInvocationID.z);
67

68
    if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
69
        return;
70

71
    int positive_axis = axis < 0 ? psc(dims) + axis : axis;
72
    afp max_value = afp(-99999999.f);
73

74
    if (psc(dims) == 1) // positive_axis == 0
75
    {
76
        for (int i = 0; i < psc(w); i++)
77
        {
78
#if NCNN_image_shader
79
            afp v = image3d_ld1(bottom_top_blob_3d, ivec3(i, 0, 0));
80
#else
81
            afp v = buffer_ld1(bottom_top_blob_data, i);
82
#endif
83
            max_value = max(max_value, v);
84
        }
85
#if NCNN_image_shader
86
        image3d_st1(max_workspace_3d, ivec3(0, 0, 0), max_value);
87
#else
88
        buffer_st1(max_workspace_data, 0, max_value);
89
#endif
90
        return;
91
    }
92

93
    if (psc(dims) == 2 && positive_axis == 0)
94
    {
95
        for (int i = 0; i < psc(h); i++)
96
        {
97
#if NCNN_image_shader
98
            afp v = image3d_ld1(bottom_top_blob_3d, ivec3(gx, i, 0));
99
#else
100
            int v_offset = i * psc(w) + gx;
101
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
102
#endif
103
            max_value = max(max_value, v);
104
        }
105
#if NCNN_image_shader
106
        image3d_st1(max_workspace_3d, ivec3(gx, 0, 0), max_value);
107
#else
108
        buffer_st1(max_workspace_data, gx, max_value);
109
#endif
110
        return;
111
    }
112

113
    if (psc(dims) == 2 && positive_axis == 1)
114
    {
115
        for (int i = 0; i < psc(w); i++)
116
        {
117
#if NCNN_image_shader
118
            afp v = image3d_ld1(bottom_top_blob_3d, ivec3(i, gx, 0));
119
#else
120
            int v_offset = gx * psc(w) + i;
121
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
122
#endif
123
            max_value = max(max_value, v);
124
        }
125
#if NCNN_image_shader
126
        image3d_st1(max_workspace_3d, ivec3(gx, 0, 0), max_value);
127
#else
128
        buffer_st1(max_workspace_data, gx, max_value);
129
#endif
130
        return;
131
    }
132

133
    if (psc(dims) == 3 && positive_axis == 0)
134
    {
135
        for (int i = 0; i < psc(c); i++)
136
        {
137
#if NCNN_image_shader
138
            afp v = image3d_ld1(bottom_top_blob_3d, ivec3(gx, gy, i));
139
#else
140
            int v_offset = i * psc(cstep) + gy * psc(w) + gx;
141
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
142
#endif
143
            max_value = max(max_value, v);
144
        }
145
#if NCNN_image_shader
146
        image3d_st1(max_workspace_3d, ivec3(gx, gy, 0), max_value);
147
#else
148
        buffer_st1(max_workspace_data, gy * psc(w) + gx, max_value);
149
#endif
150
        return;
151
    }
152

153
    if (psc(dims) == 3 && positive_axis == 1)
154
    {
155
        for (int i = 0; i < psc(h); i++)
156
        {
157
#if NCNN_image_shader
158
            afp v = image3d_ld1(bottom_top_blob_3d, ivec3(gx, i, gy));
159
#else
160
            int v_offset = gy * psc(cstep) + i * psc(w) + gx;
161
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
162
#endif
163
            max_value = max(max_value, v);
164
        }
165
#if NCNN_image_shader
166
        image3d_st1(max_workspace_3d, ivec3(gx, gy, 0), max_value);
167
#else
168
        buffer_st1(max_workspace_data, gy * psc(w) + gx, max_value);
169
#endif
170
        return;
171
    }
172

173
    if (psc(dims) == 3 && positive_axis == 2)
174
    {
175
        for (int i = 0; i < psc(w); i++)
176
        {
177
#if NCNN_image_shader
178
            afp v = image3d_ld1(bottom_top_blob_3d, ivec3(i, gx, gy));
179
#else
180
            int v_offset = gy * psc(cstep) + gx * psc(w) + i;
181
            afp v = buffer_ld1(bottom_top_blob_data, v_offset);
182
#endif
183
            max_value = max(max_value, v);
184
        }
185
#if NCNN_image_shader
186
        image3d_st1(max_workspace_3d, ivec3(gx, gy, 0), max_value);
187
#else
188
        buffer_st1(max_workspace_data, gy * psc(h) + gx, max_value);
189
#endif
190
        return;
191
    }
192
}
193

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

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

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

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