-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathgraph.cuh
More file actions
283 lines (255 loc) · 11.2 KB
/
Copy pathgraph.cuh
File metadata and controls
283 lines (255 loc) · 11.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* 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, Shizhen Xu
*/
#pragma once
#include "base/memory.h"
#include "core/optimizer.h"
#include "util/gpu.cuh"
namespace graphvite {
namespace gpu {
namespace graph {
/**
* @brief Train node embedding 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> vertex_embeddings, Memory<Vector, Index> context_embeddings,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float negative_weight) {
static const size_t dim = Vector::dim;
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 &vertex_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 &vertex = vertex_embeddings[head_id];
vertex_buffer = vertex;
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 &context = context_embeddings[tail_id];
// Forward
Float logit;
model.forward(vertex_buffer, context, logit);
Float prob = sigmoid(logit);
// Backward
Float gradient, weight;
if (label) {
gradient = prob - 1;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = prob;
weight = negative_weight;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(vertex_buffer, context, gradient, optimizer, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / (1 + num_negative * negative_weight);
vertex = vertex_buffer;
}
}
/**
* @brief Train node embedding 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> vertex_embeddings, Memory <Vector, Index> context_embeddings,
Memory<Vector, Index> vertex_moment1s, Memory<Vector, Index> context_moment1s,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float negative_weight) {
static const size_t dim = Vector::dim;
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 &vertex_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 &vertex = vertex_embeddings[head_id];
Vector &vertex_moment1 = vertex_moment1s[head_id];
vertex_buffer = vertex;
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 &context = context_embeddings[tail_id];
Vector &context_moment1 = context_moment1s[tail_id];
// Forward
Float logit;
model.forward(vertex_buffer, context, logit);
Float prob = sigmoid(logit);
// Backward
Float gradient, weight;
if (label) {
gradient = prob - 1;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = prob;
weight = negative_weight;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(vertex_buffer, context, vertex_moment1, context_moment1,
gradient, optimizer, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / (1 + num_negative * negative_weight);
vertex = vertex_buffer;
}
}
/**
* @brief Train node embedding 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> vertex_embeddings, Memory <Vector, Index> context_embeddings,
Memory<Vector, Index> vertex_moment1s, Memory<Vector, Index> context_moment1s,
Memory<Vector, Index> vertex_moment2s, Memory<Vector, Index> context_moment2s,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float negative_weight) {
static const size_t dim = Vector::dim;
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 &vertex_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 &vertex = vertex_embeddings[head_id];
Vector &vertex_moment1 = vertex_moment1s[head_id];
Vector &vertex_moment2 = vertex_moment2s[head_id];
vertex_buffer = vertex;
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 &context = context_embeddings[tail_id];
Vector &context_moment1 = context_moment1s[tail_id];
Vector &context_moment2 = context_moment2s[tail_id];
// Forward
Float logit;
model.forward(vertex_buffer, context, logit);
Float prob = sigmoid(logit);
// Backward
Float gradient, weight;
if (label) {
gradient = prob - 1;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = prob;
weight = negative_weight;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(vertex_buffer, context, vertex_moment1, context_moment1,
vertex_moment2, context_moment2, gradient, optimizer, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / (1 + num_negative * negative_weight);
vertex = vertex_buffer;
}
}
/**
* @brief Predict logits for batch samples
* @tparam Vector vector type of embeddings
* @tparam Index integral type of indexes
* @tparam Model embedding model
*/
template<class Vector, class Index, template<class> class Model>
__global__ void predict(Memory<Vector, Index> vertex_embeddings, Memory<Vector, Index> context_embeddings,
Memory<Index, int> batch, Memory<typename Vector::Float, int> logits) {
static const size_t dim = Vector::dim;
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;
Model<Vector> model;
__shared__ Vector buffer[kThreadPerBlock / kWarpSize];
Vector &vertex_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];
Index tail_id = batch[sample_id * 2];
Vector &vertex = vertex_embeddings[head_id];
Vector &context = context_embeddings[tail_id];
Float logit;
model.forward(vertex, context, logit);
if (lane_id == 0)
logits[sample_id] = logit;
}
}
} // namespace graph
} // namespace gpu
} // namespace graphvite