-
Notifications
You must be signed in to change notification settings - Fork 37
/
SoftProposalGenerator.cu
178 lines (154 loc) · 4.92 KB
/
SoftProposalGenerator.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
#ifndef THC_GENERIC_FILE
#define THC_GENERIC_FILE "generic/SoftProposalGenerator.cu"
#else
int cuspn_(SP_InitDistanceMetric)(
THCTensor *distanceMetric,
const float factor,
const uint16 mW,
const uint16 mH,
const uint16 N)
{
const uint32 count = N * N;
THCTensor_(resize2d)(state, distanceMetric, N, N);
real *distanceMetric_data = THCTensor_(data)(state, distanceMetric);
kernel_(InitDistanceMetric)(
THCState_getCurrentStream(state),
count,
distanceMetric_data,
factor,
mW,
mH,
N);
THCudaCheck(cudaGetLastError());
return 1;
}
int cuspn_(SP_Generate)(
THCTensor *input,
THCTensor *distanceMetric,
THCTensor *transferMatrix,
THCTensor *proposal,
THCTensor *proposalBuffer,
const float tolerance,
const uint32 maxIteration)
{
uint8 ndim = input->nDimension;
THArgCheck(ndim == 4, 1, "only supports batch mode.");
const uint16 nBatch = input->size[0];
const uint16 nChannel = input->size[1];
const uint16 mW = input->size[2];
const uint16 mH = input->size[3];
const uint16 N = mW * mH;
const uint32 nEntry = nChannel * N;
THCUNN_assertSameGPU(state, 5, input, distanceMetric, transferMatrix, proposal, proposalBuffer);
THCTensor_(resizeAs)(state, transferMatrix, distanceMetric);
THCTensor_(resize3d)(state, proposal, nBatch, mW, mH);
THCTensor_(resize2d)(state, proposalBuffer, mW, mH);
// (nBatch, nChannel, mH, mW)
real *input_data = THCTensor_(data)(state, input);
// (mH*mW, mH*mW)
real *distanceMetric_data = THCTensor_(data)(state, distanceMetric);
// (mH*mW, mH*mW)
real *transferMatrix_data = THCTensor_(data)(state, transferMatrix);
// (nBatch, mH, mW)
real *proposal_data = THCTensor_(data)(state, proposal);
// (mH, mW)
real *proposalBuffer_data = THCTensor_(data)(state, proposalBuffer);
const float avg = 1.0f / N;
float sumOver;
uint32 count;
uint32 i, j;
THCTensor_(fill)(state, proposal, avg);
for (i = 0; i < nBatch; i++) {
/* init transfer matrix */
count = N * N;
kernel_(InitTransferMatrix)(
THCState_getCurrentStream(state),
count,
input_data + i * nEntry,
distanceMetric_data,
transferMatrix_data,
nChannel,
mW,
mH,
N);
THCudaCheck(cudaGetLastError());
count = N;
kernel_(NormTransferMatrix)(
THCState_getCurrentStream(state),
count,
transferMatrix_data,
N);
THCudaCheck(cudaGetLastError());
/* generate soft proposal for each sample */
// init buffer
THCTensor_(fill)(state, proposalBuffer, avg);
for (j = 0; j < maxIteration; j++) {
// calculate diffs
THCudaBlas_Sgemv(
state,
't',
N,
N,
1.0f,
transferMatrix_data,
N,
proposal_data,
1,
-1.0f,
proposalBuffer_data,
1);
float normDiff = THCTensor_(normall)(state, proposalBuffer, 2);
if (normDiff < tolerance) break;
// add diffs
kernel_(UpdateProposal)(
THCState_getCurrentStream(state),
count,
proposal_data,
proposalBuffer_data,
proposalBuffer_data);
THCudaCheck(cudaGetLastError());
sumOver = THCTensor_(sumall)(state, proposalBuffer);
if (sumOver < 0) break;
// norm proposal
kernel_(NormProposal)(
THCState_getCurrentStream(state),
count,
proposalBuffer_data,
proposal_data,
1.0f / sumOver);
THCudaCheck(cudaGetLastError());
}
proposal_data += N;
}
return 1;
}
int cuspn_(SP_Couple)(
THCTensor *input,
THCTensor *proposal,
THCTensor *output)
{
uint8 ndim = input->nDimension;
THArgCheck(ndim == 4, 1, "only supports batch mode.");
THCUNN_assertSameGPU(state, 3, input, proposal, output);
const uint16 nBatch = input->size[0];
const uint16 nChannel = input->size[1];
const uint16 mW = input->size[2];
const uint16 mH = input->size[3];
const uint16 N = mW * mH;
real *input_data = THCTensor_(data)(state, input);
// (nBatch, mH, mW)
real *proposal_data = THCTensor_(data)(state, proposal);
real *output_data = THCTensor_(data)(state, output);
const uint32 count = nBatch * nChannel * N;
kernel_(Couple)(
THCState_getCurrentStream(state),
count,
input_data,
proposal_data,
output_data,
nChannel,
N);
THCudaCheck(cudaGetLastError());
return 1;
}
#endif