-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknnloss.cu
186 lines (150 loc) · 5.73 KB
/
knnloss.cu
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
/*!
* Copyright (c) 2017 by Contributors
* \file knnloss.cu
* \brief Knnloss
* \author deepearthgo
*/
#include "./knnloss-inl.h"
#include <math.h>
#include <thrust/scan.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
namespace mshadow {
namespace cuda {
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
MSHADOW_XINLINE int LSPowOfMO(const int k) {
return 1 - ((k&0x01) << 1);
}
template<typename DType>
__global__ void SimilarityMatrixKernel(const Tensor<gpu, 2, DType> x,
Tensor<gpu, 2, DType> sbm) {
const int n = x.size(0);
unsigned int i = blockIdx.y*blockDim.y + threadIdx.y;
unsigned int j = blockIdx.x*blockDim.x + threadIdx.x;
if (i<n && j <n){
float sum = 0;
for (int k=0; k <n; ++k){
sum += pow (x[i]-x[j],2);
}
sbm[i][j] = sqrt(sum);
}
}
template<typename DType>
__global__ void Sort_Keykernel(const Tensor<gpu, 2, DType> sbm,
Tensor<gpu, 2, DType> ssbm) {
const int n = sbm.size(0);
CUDA_KERNEL_LOOP(i, n) {
for (int j=0; j<k_num; ++j){
ssbm[i][j]=j;
}
thrust::sort_by_key{sbm[i],sbm[i]+n,ssbm[i]};
}
}
template<typename DType>
__global__ void KnnlossForwardKernel(const Tensor<gpu, 2, DType> x,
const Tensor<gpu, 2, DType> sbm,
const Tensor<gpu, 2, DType> ssbm,
Tensor<gpu, 2, DType> out,
Tensor<gpu, 2, DType> dls,
Tensor<gpu, 1, DType> knc,
Tensor<gpu, 1, DType> kls,
const int k_num,
const int batch_size) {
const int n = x.size(0);
const int feature_dim = x.size(1);
CUDA_KERNEL_LOOP(i, n) {
float sum1[feature_dim] ={0};
float sum2 = 0;
for (int j=0; j<k_num; ++j){
sum1 += (x[i][ssbm[i][j]]-x[i])/k_num;
sum2 += dls[i][j];
}
kls += sum2;
knc[i] = sum1;
dls[i] = x[i]-knx[i];
out += kls;
}
}
template<typename DType>
inline void KnnlossForward(const Tensor<gpu, 2, DType> &x,
const Tensor<gpu, 2, DType> &sbm,
const Tensor<gpu, 2, DType> &ssbm,
const Tensor<gpu, 2, DType> &out,
const Tensor<gpu, 2, DType> &dls,
const Tensor<gpu, 1, DType> &knc,
const Tensor<gpu, 2, DType> &kls,
const int k_num,
const int batch_size) {
const int n = x.size(0);
dim3 dimBlock(kBaseThreadNum);
dim3 dimGrid((n + kBaseThreadNum - 1) / kBaseThreadNum);
dimGrid.x = ((n + kBaseThreadNum - 1) / kBaseThreadNum);
SimilarityMatrixKernel<<<dimGrid, dimBlock>>>(x, sbm);
dimGrid.x = ((n + kBaseThreadNum - 1) / kBaseThreadNum);
Sort_Keykernal<<<dimGrid, dimBlock>>>(sbm, ssbm);
dimGrid.x = ((n + kBaseThreadNum - 1) / kBaseThreadNum);
KnnlossForwardKernel<<<dimGrid, dimBlock>>>(x,sbm, ssbm,out,dls,knc,kls,k_num,batch_size);
}
template<typename DType>
__global__ void KnnlossBackwardGradKernel(const Tensor<gpu, 2, DType> dls,
const Tensor<gpu, 2, DType> o_grad,
Tensor<gpu, 2, DType> x_grad,
const float lamna) {
const int n = dls.size(0);
CUDA_KERNEL_LOOP(i,n) {
x_grad[i]= static_cast<float>(lamna) * dls[i];
}
}
template<typename DType>
inline void KnnlossBackward(const Tensor<gpu, 2, DType> &x,
const Tensor<gpu, 2, DType> &out,
const Tensor<gpu, 2, DType> &o_grad,
const Tensor<gpu, 2, DType> &x_grad,
const Tensor<gpu, 2, DType> &dls,
const int batch_size,
const float lamna) {
const int x = diff.size(0);
dim3 dimBlock(kBaseThreadNum);
dim3 dimGrid((n + kBaseThreadNum - 1) / kBaseThreadNum);
dimGrid.x = ((n * feature_dim + kBaseThreadNum - 1) / kBaseThreadNum);
knnlossBackwardGradKernel<<<dimGrid, dimBlock>>>(dls, o_grad, x_grad, lamna);
}
} // namespace cuda
template<typename DType>
inline void KnnlossForward(const Tensor<gpu, 2, DType> &x,
const Tensor<gpu, 2, DType> &sbm,
const Tensor<gpu, 2, DType> &ssbm,
const Tensor<gpu, 2, DType> &out,
const Tensor<cpu, 2, DType> &dls,
const Tensor<cpu, 2, DType> &knc,
const Tensor<cpu, 2, DType> &kls,
const int k_num,
const int batch_size) {
cuda::KnnlossForward(x, sbm, ssbm, out, dls,knc, kls, k_num, batch_size);
}
template<typename DType>
inline void KnnlossBackward(const Tensor<cpu, 2, DType> &x,
const Tensor<cpu, 2, DType> &out,
const Tensor<gpu, 2, DType> &o_grad,
const Tensor<gpu, 2, DType> &x_grad,
const Tensor<gpu, 2, DType> &dls,
const int batch_size,
const float lamna) {
cuda::KnnlossBackward(x,out,o_grad,x_grad,dls,batch_size,lamna);
}
} // namespace mshadow
namespace mxnet {
namespace op {
template<>
Operator *CreateOp<gpu>(KnnlossParam param, int dtype) {
Operator *op = NULL;
MSHADOW_REAL_TYPE_SWITCH(dtype, DType, {
op = new KnnlossOp<gpu, DType>(param);
})
return op;
}
} // namespace op
} // namespace mxnet