-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathvisualization.cuh
More file actions
244 lines (221 loc) · 9.49 KB
/
Copy pathvisualization.cuh
File metadata and controls
244 lines (221 loc) · 9.49 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* Copyright 2019 MilaGraph. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Zhaocheng Zhu
*/
#pragma once
#include "base/memory.h"
#include "core/optimizer.h"
#include "util/gpu.cuh"
namespace graphvite {
namespace gpu {
namespace visualization {
const float kSmoothTerm = 0.1;
/**
* @brief Train visualization with 0-moment optimizers
* @tparam Vector vector type of embeddings
* @tparam Index integral type of indexes
* @tparam Model embedding model
* @tparam optimizer_type type of optimizer
*/
template<class Vector, class Index, template<class> class Model, OptimizerType optimizer_type>
__global__ void train(Memory<Vector, Index> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float negative_weight) {
typedef typename Vector::Float Float;
const int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
const int lane_id = thread_id % kWarpSize;
const int num_thread = gridDim.x * blockDim.x;
const int batch_size = batch.count / 2;
const int num_negative = negative_batch.count / batch_size;
Model<Vector> model;
__shared__ Vector buffer[kThreadPerBlock / kWarpSize];
Vector &head_buffer = buffer[threadIdx.x / kWarpSize];
for (int sample_id = thread_id / kWarpSize; sample_id < batch_size; sample_id += num_thread / kWarpSize) {
// elements in std::tuple are stored in reverse order
// each positive sample is {tail, head}
Index head_id = batch[sample_id * 2 + 1];
Vector &head = head_embeddings[head_id];
head_buffer = head;
Float sample_loss = 0;
for (int s = 0; s <= num_negative; s++) {
Index tail_id;
int label;
if (s < num_negative) {
tail_id = negative_batch[sample_id * num_negative + s];
label = 0;
} else {
tail_id = batch[sample_id * 2];
label = 1;
}
Vector &tail = tail_embeddings[tail_id];
// Forward
Float x;
model.forward(head, tail, x);
Float prob = 1 / (1 + x);
// Backward
Float gradient, weight;
if (label) {
gradient = 2 * prob;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = -2 * prob / (x + kSmoothTerm);
weight = negative_weight;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(head, tail, gradient, optimizer, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / (1 + num_negative * negative_weight);
head = head_buffer;
}
}
/**
* @brief Train visualization with 1-moment optimizers
* @tparam Vector vector type of embeddings
* @tparam Index integral type of indexes
* @tparam Model embedding model
* @tparam optimizer_type type of optimizer
*/
template<class Vector, class Index, template<class> class Model, OptimizerType optimizer_type>
__global__ void train_1_moment(Memory<Vector, Index> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Vector, Index> head_moment1s, Memory<Vector, Index> tail_moment1s,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float negative_weight) {
typedef typename Vector::Float Float;
const int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
const int lane_id = thread_id % kWarpSize;
const int num_thread = gridDim.x * blockDim.x;
const int batch_size = batch.count / 2;
const int num_negative = negative_batch.count / batch_size;
Model<Vector> model;
__shared__ Vector buffer[kThreadPerBlock / kWarpSize];
Vector &head_buffer = buffer[threadIdx.x / kWarpSize];
for (int sample_id = thread_id / kWarpSize; sample_id < batch_size; sample_id += num_thread / kWarpSize) {
// elements in std::tuple are stored in reverse order
// each positive sample is {tail, head}
Index head_id = batch[sample_id * 2 + 1];
Vector &head = head_embeddings[head_id];
Vector &head_moment1 = head_moment1s[head_id];
head_buffer = head;
Float sample_loss = 0;
for (int s = 0; s <= num_negative; s++) {
Index tail_id;
int label;
if (s < num_negative) {
tail_id = negative_batch[sample_id * num_negative + s];
label = 0;
} else {
tail_id = batch[sample_id * 2];
label = 1;
}
Vector &tail = tail_embeddings[tail_id];
Vector &tail_moment1 = tail_moment1s[tail_id];
// Forward
Float x;
model.forward(head, tail, x);
Float prob = 1 / (1 + x);
// Backward
Float gradient, weight;
if (label) {
gradient = 2 * prob;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = -2 * prob / (x + kSmoothTerm);
weight = negative_weight;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(head, tail, head_moment1, tail_moment1, gradient, optimizer, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / (1 + num_negative * negative_weight);
head = head_buffer;
}
}
/**
* @brief Train visualization with 2-moment optimizers
* @tparam Vector vector type of embeddings
* @tparam Index integral type of indexes
* @tparam Model embedding model
* @tparam optimizer_type type of optimizer
*/
template<class Vector, class Index, template<class> class Model, OptimizerType optimizer_type>
__global__ void train_2_moment(Memory<Vector, Index> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Vector, Index> head_moment1s, Memory<Vector, Index> tail_moment1s,
Memory<Vector, Index> head_moment2s, Memory<Vector, Index> tail_moment2s,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float negative_weight) {
typedef typename Vector::Float Float;
const int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
const int lane_id = thread_id % kWarpSize;
const int num_thread = gridDim.x * blockDim.x;
const int batch_size = batch.count / 2;
const int num_negative = negative_batch.count / batch_size;
Model<Vector> model;
__shared__ Vector buffer[kThreadPerBlock / kWarpSize];
Vector &head_buffer = buffer[threadIdx.x / kWarpSize];
for (int sample_id = thread_id / kWarpSize; sample_id < batch_size; sample_id += num_thread / kWarpSize) {
// elements in std::tuple are stored in reverse order
// each positive sample is {tail, head}
Index head_id = batch[sample_id * 2 + 1];
Vector &head = head_embeddings[head_id];
Vector &head_moment1 = head_moment1s[head_id];
Vector &head_moment2 = head_moment2s[head_id];
head_buffer = head;
Float sample_loss = 0;
for (int s = 0; s <= num_negative; s++) {
Index tail_id;
int label;
if (s < num_negative) {
tail_id = negative_batch[sample_id * num_negative + s];
label = 0;
} else {
tail_id = batch[sample_id * 2];
label = 1;
}
Vector &tail = tail_embeddings[tail_id];
Vector &tail_moment1 = tail_moment1s[tail_id];
Vector &tail_moment2 = tail_moment2s[tail_id];
// Forward
Float x;
model.forward(head, tail, x);
Float prob = 1 / (1 + x);
// Backward
Float gradient, weight;
if (label) {
gradient = 2 * prob;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = -2 * prob / (x + kSmoothTerm);
weight = negative_weight;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(head, tail, head_moment1, tail_moment1, head_moment2, tail_moment2,
gradient, optimizer, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / (1 + num_negative * negative_weight);
head = head_buffer;
}
}
} // namespace visualization
} // namespace gpu
} // namespace graphvite