Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/softmax.cl
16337 views
1
/*************************************************************************************
2
* Copyright (c) 2015, Advanced Micro Devices, Inc.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without modification,
6
* are permitted provided that the following conditions are met:
7
*
8
* 1. Redistributions of source code must retain the above copyright notice, this
9
* list of conditions and the following disclaimer.
10
*
11
* 2. Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation and/or
13
* other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
* POSSIBILITY OF SUCH DAMAGE.
25
**************************************************************************************/
26
27
#if defined(cl_khr_fp16)
28
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
29
#endif
30
31
__kernel void kernel_channel_max(const int num, const int channels,
32
const int spatial_dim, __global const T* data, __global T* out) {
33
int index = get_global_id(0);
34
if(index < num * spatial_dim) {
35
int n = index / spatial_dim;
36
int s = index % spatial_dim;
37
T maxval = -FLT_MAX;
38
for (int c = 0; c < channels; ++c) {
39
maxval = max(data[(n * channels + c) * spatial_dim + s], maxval);
40
}
41
out[index] = maxval;
42
}
43
}
44
45
__kernel void kernel_channel_subtract(const int count,
46
const int num, const int channels,
47
const int spatial_dim, __global const T* channel_max, __global const T* src, __global T* data) {
48
int index = get_global_id(0);
49
if(index < count) {
50
int n = index / channels / spatial_dim;
51
int s = index % spatial_dim;
52
data[index] = exp(src[index] - channel_max[n * spatial_dim + s]);
53
}
54
}
55
56
__kernel void kernel_channel_sum(const int num, const int channels,
57
const int spatial_dim, __global const T* data, __global T* channel_sum) {
58
int index = get_global_id(0);
59
if(index < num * spatial_dim) {
60
int n = index / spatial_dim;
61
int s = index % spatial_dim;
62
T sum = 0;
63
for (int c = 0; c < channels; ++c) {
64
sum += data[(n * channels + c) * spatial_dim + s];
65
}
66
channel_sum[index] = sum;
67
}
68
}
69
70
__kernel void kernel_channel_div(const int count,
71
const int num, const int channels,
72
const int spatial_dim, __global const T* channel_sum, __global T* data) {
73
int index = get_global_id(0);
74
if(index < count) {
75
int n = index / channels / spatial_dim;
76
int s = index % spatial_dim;
77
T v = data[index] / channel_sum[n * spatial_dim + s];
78
#ifdef LOG_SOFTMAX
79
v = log(v);
80
#endif
81
data[index] = v;
82
}
83
}
84
85