-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathknowledge_graph.cuh
More file actions
371 lines (344 loc) · 16.1 KB
/
Copy pathknowledge_graph.cuh
File metadata and controls
371 lines (344 loc) · 16.1 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* 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"
#include "util/math.h"
namespace graphvite {
namespace gpu {
namespace knowledge_graph {
/**
* @brief Train knowledge graph 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> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Vector, Index> relation_embeddings, Memory<Index, int> batch,
Memory<Index, int> negative_batch, Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float relation_lr_multiplier, float margin_or_l3,
float adversarial_temperature) {
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 num_head = head_embeddings.count;
const int batch_size = batch.count / 3;
const int num_negative = negative_batch.count / batch_size;
Model<Vector> model;
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 {relation, tail, head}
Index relation_id = batch[sample_id * 3];
Vector &relation = relation_embeddings[relation_id];
// compute normalizer
Float bias, normalizer = 0;
if (adversarial_temperature > kEpsilon)
for (int s = 0; s < num_negative; s++) {
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
Index negative_id = negative_batch[sample_id * num_negative + s];
if (negative_id < num_head)
head_id = negative_id;
else
tail_id = negative_id - num_head;
Vector &head = head_embeddings[head_id];
Vector &tail = tail_embeddings[tail_id];
// Forward
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
if (s == 0)
bias = logit;
normalizer += safe_exp((logit - bias) / adversarial_temperature);
}
Float sample_loss = 0;
for (int s = 0; s <= num_negative; s++) {
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
int label = 1;
if (s < num_negative) {
Index negative_id = negative_batch[sample_id * num_negative + s];
if (negative_id < num_head)
head_id = negative_id;
else
tail_id = negative_id - num_head;
label = 0;
}
Vector &head = head_embeddings[head_id];
Vector &tail = tail_embeddings[tail_id];
// Forward
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
Float prob = sigmoid(logit);
// Backward
Float gradient, weight;
if (label) {
gradient = prob - 1;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = prob;
if (adversarial_temperature > kEpsilon) {
weight = safe_exp((logit - bias) / adversarial_temperature) / normalizer;
// the normalizer may be out of date in ASGD
// so we need to clip the weight
weight = min(weight, Float(1));
}
else
weight = 1.0 / num_negative;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(head, tail, relation,
margin_or_l3, gradient, optimizer, relation_lr_multiplier, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / 2;
}
}
/**
* @brief Train knowledge graph 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> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Vector, Index> relation_embeddings, Memory<Vector, Index> head_moment1s,
Memory<Vector, Index> tail_moment1s, Memory<Vector, Index> relation_moment1s,
Memory<Index, int> batch, Memory<Index, int> negative_batch,
Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float relation_lr_multiplier, float margin_or_l3,
float adversarial_temperature) {
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 num_head = head_embeddings.count;
const int batch_size = batch.count / 3;
const int num_negative = negative_batch.count / batch_size;
Model<Vector> model;
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 {relation, tail, head}
Index relation_id = batch[sample_id * 3];
Vector &relation = relation_embeddings[relation_id];
Vector &relation_moment1 = relation_moment1s[relation_id];
// compute normalizer
Float bias, normalizer = 0;
if (adversarial_temperature > kEpsilon)
for (int s = 0; s < num_negative; s++) {
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
Index negative_id = negative_batch[sample_id * num_negative + s];
if (negative_id < num_head)
head_id = negative_id;
else
tail_id = negative_id - num_head;
Vector &head = head_embeddings[head_id];
Vector &tail = tail_embeddings[tail_id];
// Forward
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
if (s == 0)
bias = logit;
normalizer += safe_exp((logit - bias) / adversarial_temperature);
}
Float sample_loss = 0;
for (int s = 0; s <= num_negative; s++) {
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
int label = 1;
if (s < num_negative) {
Index negative_id = negative_batch[sample_id * num_negative + s];
if (negative_id < num_head)
head_id = negative_id;
else
tail_id = negative_id - num_head;
label = 0;
}
Vector &head = head_embeddings[head_id];
Vector &head_moment1 = head_moment1s[head_id];
Vector &tail = tail_embeddings[tail_id];
Vector &tail_moment1 = tail_moment1s[tail_id];
// Forward
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
Float prob = sigmoid(logit);
// Backward
Float gradient, weight;
if (label) {
gradient = prob - 1;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = prob;
if (adversarial_temperature > kEpsilon) {
weight = safe_exp((logit - bias) / adversarial_temperature) / normalizer;
// the normalizer may be out of date in ASGD
// so we need to clip the weight
weight = min(weight, Float(1));
}
else
weight = 1.0 / num_negative;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(head, tail, relation, head_moment1, tail_moment1, relation_moment1,
margin_or_l3, gradient, optimizer, relation_lr_multiplier, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / 2;
}
}
/**
* @brief Train knowledge graph 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> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Vector, Index> relation_embeddings, Memory<Vector, Index> head_moment1s,
Memory<Vector, Index> tail_moment1s, Memory<Vector, Index> relation_moment1s,
Memory<Vector, Index> head_moment2s, Memory<Vector, Index> tail_moment2s,
Memory<Vector, Index> relation_moment2s, Memory<Index, int> batch,
Memory<Index, int> negative_batch, Memory<typename Vector::Float, int> loss,
Optimizer optimizer, float relation_lr_multiplier, float margin_or_l3,
float adversarial_temperature) {
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 num_head = head_embeddings.count;
const int batch_size = batch.count / 3;
const int num_negative = negative_batch.count / batch_size;
Model<Vector> model;
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 {relation, tail, head}
Index relation_id = batch[sample_id * 3];
Vector &relation = relation_embeddings[relation_id];
Vector &relation_moment1 = relation_moment1s[relation_id];
Vector &relation_moment2 = relation_moment2s[relation_id];
// compute normalizer
Float bias, normalizer = 0;
if (adversarial_temperature > kEpsilon)
for (int s = 0; s < num_negative; s++) {
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
Index negative_id = negative_batch[sample_id * num_negative + s];
if (negative_id < num_head)
head_id = negative_id;
else
tail_id = negative_id - num_head;
Vector &head = head_embeddings[head_id];
Vector &tail = tail_embeddings[tail_id];
// Forward
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
if (s == 0)
bias = logit;
normalizer += safe_exp((logit - bias) / adversarial_temperature);
}
Float sample_loss = 0;
for (int s = 0; s <= num_negative; s++) {
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
int label = 1;
if (s < num_negative) {
Index negative_id = negative_batch[sample_id * num_negative + s];
if (negative_id < num_head)
head_id = negative_id;
else
tail_id = negative_id - num_head;
label = 0;
}
Vector &head = head_embeddings[head_id];
Vector &head_moment1 = head_moment1s[head_id];
Vector &head_moment2 = head_moment2s[head_id];
Vector &tail = tail_embeddings[tail_id];
Vector &tail_moment1 = tail_moment1s[tail_id];
Vector &tail_moment2 = tail_moment2s[tail_id];
// Forward
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
Float prob = sigmoid(logit);
// Backward
Float gradient, weight;
if (label) {
gradient = prob - 1;
weight = 1;
sample_loss += weight * -log(prob + kEpsilon);
} else {
gradient = prob;
if (adversarial_temperature > kEpsilon) {
weight = safe_exp((logit - bias) / adversarial_temperature) / normalizer;
// the normalizer may be out of date in ASGD
// so we need to clip the weight
weight = min(weight, Float(1));
}
else
weight = 1.0 / num_negative;
sample_loss += weight * -log(1 - prob + kEpsilon);
}
model.backward<optimizer_type>(head, tail, relation, head_moment1, tail_moment1, relation_moment1,
head_moment2, tail_moment2, relation_moment2,
margin_or_l3, gradient, optimizer, relation_lr_multiplier, weight);
}
if (lane_id == 0)
loss[sample_id] = sample_loss / 2;
}
}
/**
* @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> head_embeddings, Memory<Vector, Index> tail_embeddings,
Memory<Vector, Index> relation_embeddings, Memory<Index, int> batch,
Memory<typename Vector::Float, int> logits, float margin_or_l3) {
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 / 3;
Model<Vector> model;
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 {relation, tail, head}
Index head_id = batch[sample_id * 3 + 2];
Index tail_id = batch[sample_id * 3 + 1];
Index relation_id = batch[sample_id * 3];
Vector &head = head_embeddings[head_id];
Vector &tail = tail_embeddings[tail_id];
Vector &relation = relation_embeddings[relation_id];
Float logit;
model.forward(head, tail, relation, logit, margin_or_l3);
if (lane_id == 0)
logits[sample_id] = logit;
}
}
} // namespace knowledge graph
} // namespace gpu
} // namespace graphvite