-
Notifications
You must be signed in to change notification settings - Fork 73
/
word2vec_kernels.cc
407 lines (373 loc) · 14.3 KB
/
word2vec_kernels.cc
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Copyright 2015 The TensorFlow Authors. 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.
==============================================================================*/
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/gtl/map_util.h"
#include "tensorflow/core/lib/random/distribution_sampler.h"
#include "tensorflow/core/lib/random/philox_random.h"
#include "tensorflow/core/lib/random/simple_philox.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/thread_annotations.h"
#include "tensorflow/core/util/guarded_philox_random.h"
namespace tensorflow {
// Number of examples to precalculate.
const int kPrecalc = 3000;
// Number of words to read into a sentence before processing.
const int kSentenceSize = 1000;
namespace {
bool ScanWord(StringPiece* input, string* word) {
str_util::RemoveLeadingWhitespace(input);
StringPiece tmp;
if (str_util::ConsumeNonWhitespace(input, &tmp)) {
word->assign(tmp.data(), tmp.size());
return true;
} else {
return false;
}
}
} // end namespace
class SkipgramWord2vecOp : public OpKernel {
public:
explicit SkipgramWord2vecOp(OpKernelConstruction* ctx)
: OpKernel(ctx), rng_(&philox_) {
string filename;
OP_REQUIRES_OK(ctx, ctx->GetAttr("filename", &filename));
OP_REQUIRES_OK(ctx, ctx->GetAttr("batch_size", &batch_size_));
OP_REQUIRES_OK(ctx, ctx->GetAttr("window_size", &window_size_));
OP_REQUIRES_OK(ctx, ctx->GetAttr("min_count", &min_count_));
OP_REQUIRES_OK(ctx, ctx->GetAttr("subsample", &subsample_));
OP_REQUIRES_OK(ctx, Init(ctx->env(), filename));
// Calling init here. Init needs to signal OK
//LOG(INFO) << "This should run without segfault";
//LOG(INFO) << "After calling Init";
mutex_lock l(mu_);
example_pos_ = corpus_size_;
label_pos_ = corpus_size_;
label_limit_ = corpus_size_;
sentence_index_ = kSentenceSize;
//LOG(INFO) << "Precalc Loop";
for (int i = 0; i < kPrecalc; ++i) {
NextExample(&precalc_examples_[i].input, &precalc_examples_[i].label);
}
//LOG(INFO) << "After Precalc Loop";
}
void Compute(OpKernelContext* ctx) override {
//LOG(INFO) << "Beginning of Compute";
//LOG(INFO) << ".";
Tensor words_per_epoch(DT_INT64, TensorShape({}));
Tensor current_epoch(DT_INT32, TensorShape({}));
Tensor total_words_processed(DT_INT64, TensorShape({}));
Tensor examples(DT_INT32, TensorShape({batch_size_}));
auto Texamples = examples.flat<int32>();
Tensor labels(DT_INT32, TensorShape({batch_size_}));
auto Tlabels = labels.flat<int32>();
{
mutex_lock l(mu_);
//LOG(INFO) << "starting loop compute";
for (int i = 0; i < batch_size_; ++i) {
Texamples(i) = precalc_examples_[precalc_index_].input;
Tlabels(i) = precalc_examples_[precalc_index_].label;
precalc_index_++;
if (precalc_index_ >= kPrecalc) {
precalc_index_ = 0;
for (int j = 0; j < kPrecalc; ++j) {
NextExample(&precalc_examples_[j].input,
&precalc_examples_[j].label);
}
}
}
//LOG(INFO) << "Ending loop compute";
words_per_epoch.scalar<int64>()() = corpus_size_;
current_epoch.scalar<int32>()() = current_epoch_;
total_words_processed.scalar<int64>()() = total_words_processed_;
}
//LOG(INFO) << "Setting output";
ctx->set_output(0, word_);
ctx->set_output(1, freq_);
ctx->set_output(2, words_per_epoch);
ctx->set_output(3, current_epoch);
ctx->set_output(4, total_words_processed);
ctx->set_output(5, examples);
ctx->set_output(6, labels);
}
private:
struct Example {
int32 input;
int32 label;
};
int32 batch_size_ = 0;
int32 window_size_ = 5;
float subsample_ = 1e-3;
int min_count_ = 50;
int32 vocab_size_ = 0;
Tensor word_;
Tensor freq_;
int64 corpus_size_ = 0;
// Ben A: changed this to int64 on Nov 11
std::vector<int64> corpus_;
std::vector<Example> precalc_examples_;
int precalc_index_ = 0;
// Ben A: changed this to int64 on Nov 11
std::vector<int64> sentence_;
int sentence_index_ = 0;
mutex mu_;
random::PhiloxRandom philox_ GUARDED_BY(mu_);
random::SimplePhilox rng_ GUARDED_BY(mu_);
int32 current_epoch_ GUARDED_BY(mu_) = -1;
int64 total_words_processed_ GUARDED_BY(mu_) = 0;
// Ben A: changed this to int64 on Nov 11
int64 example_pos_ GUARDED_BY(mu_);
int64 label_pos_ GUARDED_BY(mu_);
int64 label_limit_ GUARDED_BY(mu_);
// {example_pos_, label_pos_} is the cursor for the next example.
// example_pos_ wraps around at the end of corpus_. For each
// example, we randomly generate [label_pos_, label_limit) for
// labels.
void NextExample(int32* example, int32* label) EXCLUSIVE_LOCKS_REQUIRED(mu_) {
while (true) {
if (label_pos_ >= label_limit_) {
++total_words_processed_;
++sentence_index_;
if (sentence_index_ >= kSentenceSize) {
sentence_index_ = 0;
for (int i = 0; i < kSentenceSize; ++i, ++example_pos_) {
if (example_pos_ >= corpus_size_) {
++current_epoch_;
example_pos_ = 0;
}
if (subsample_ > 0) {
// Ben A: changed to 64 here
//LOG(INFO) << "before word_freq";
//LOG(INFO) << "corpus_[example_pos_] =";
//LOG(INFO) << corpus_[example_pos_];
int64 word_freq = freq_.flat<int64>()(corpus_[example_pos_]);
// See Eq. 5 in http://arxiv.org/abs/1310.4546
//LOG(INFO) << "After word freq";
float keep_prob =
(std::sqrt(word_freq / (subsample_ * corpus_size_)) + 1) *
(subsample_ * corpus_size_) / word_freq;
if (rng_.RandFloat() > keep_prob) {
i--;
continue;
}
}
//LOG(INFO) << "loc 2";
sentence_[i] = corpus_[example_pos_];
}
}
//LOG(INFO) << "loc 3";
const int32 skip = 1 + rng_.Uniform(window_size_);
label_pos_ = std::max<int64>(0, sentence_index_ - skip);
label_limit_ =
std::min<int64>(kSentenceSize, sentence_index_ + skip + 1);
//LOG(INFO) << "loc 3";
}
if (sentence_index_ != label_pos_) {
break;
}
++label_pos_;
}
//LOG(INFO) << "loc 4";
*example = sentence_[sentence_index_];
//LOG(INFO) << "loc 5";
*label = sentence_[label_pos_++];
//LOG(INFO) << "loc 6";
}
Status Init(Env* env, const string& filename) {
//LOG(INFO) << "At beginning of Init";
string data;
TF_RETURN_IF_ERROR(ReadFileToString(env, filename, &data));
StringPiece input = data;
string w;
corpus_size_ = 0;
// Ben A: changed here
std::unordered_map<string, int64> word_freq;
while (ScanWord(&input, &w)) {
++(word_freq[w]);
++corpus_size_;
}
if (corpus_size_ < window_size_ * 10) {
return errors::InvalidArgument("The text file ", filename,
" contains too little data: boo!!!!",
corpus_size_, " words");
}
// Ben A: changed here
typedef std::pair<string, int64> WordFreq;
std::vector<WordFreq> ordered;
for (const auto& p : word_freq) {
if (p.second >= min_count_) ordered.push_back(p);
}
LOG(INFO) << "Data file: " << filename << " contains " << data.size()
<< " bytes, " << corpus_size_ << " words, " << word_freq.size()
<< " unique words, " << ordered.size()
<< " unique frequent words.";
word_freq.clear();
std::sort(ordered.begin(), ordered.end(),
[](const WordFreq& x, const WordFreq& y) {
return x.second > y.second;
});
vocab_size_ = static_cast<int32>(1 + ordered.size());
//LOG(INFO) << "(1)";
Tensor word(DT_STRING, TensorShape({vocab_size_}));
//LOG(INFO) << "(2)";
// Ben A: I changed this to 64
Tensor freq(DT_INT64, TensorShape({vocab_size_}));
//LOG(INFO) << "(3)";
word.flat<string>()(0) = "UNK";
//LOG(INFO) << "(4)";
static const int32 kUnkId = 0;
//LOG(INFO) << "(5)";
// Ben A: changed here
std::unordered_map<string, int64> word_id;
//LOG(INFO) << "(6)";
//LOG(INFO) << "changed";
int64 total_counted = 0;
for (std::size_t i = 0; i < ordered.size(); ++i) {
//LOG(INFO) << "(6.1";
const auto& w = ordered[i].first;
//LOG(INFO) << "(6.2";
auto id = i + 1;
//LOG(INFO) << "(6.3";
word.flat<string>()(id) = w;
//LOG(INFO) << "(6.4";
auto word_count = ordered[i].second;
//LOG(INFO) << "(6.5";
// Ben A: changed here
freq.flat<int64>()(id) = word_count;
//LOG(INFO) << "(6.6";
total_counted += word_count;
//LOG(INFO) << "(6.7";
word_id[w] = id;
//LOG(INFO) << "(6.8";
}
//LOG(INFO) << "(7)";
// Ben A: changed here
freq.flat<int64>()(kUnkId) = corpus_size_ - total_counted;
//LOG(INFO) << "(8)";
word_ = word;
freq_ = freq;
corpus_.reserve(corpus_size_);
//LOG(INFO) << "(9)";
input = data;
while (ScanWord(&input, &w)) {
corpus_.push_back(gtl::FindWithDefault(word_id, w, kUnkId));
}
//LOG(INFO) << "(10)";
precalc_examples_.resize(kPrecalc);
//LOG(INFO) << "(11)";
//LOG(INFO) << kSentenceSize;
sentence_.resize(kSentenceSize);
//LOG(INFO) << "(12)";
return Status::OK();
}
};
REGISTER_KERNEL_BUILDER(Name("SkipgramWord2vec").Device(DEVICE_CPU), SkipgramWord2vecOp);
class NegTrainWord2vecOp : public OpKernel {
public:
explicit NegTrainWord2vecOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
base_.Init(0, 0);
OP_REQUIRES_OK(ctx, ctx->GetAttr("num_negative_samples", &num_samples_));
std::vector<int32> vocab_count;
OP_REQUIRES_OK(ctx, ctx->GetAttr("vocab_count", &vocab_count));
std::vector<float> vocab_weights;
vocab_weights.reserve(vocab_count.size());
for (const auto& f : vocab_count) {
float r = std::pow(static_cast<float>(f), 0.75f);
vocab_weights.push_back(r);
}
sampler_ = new random::DistributionSampler(vocab_weights);
}
~NegTrainWord2vecOp() { delete sampler_; }
void Compute(OpKernelContext* ctx) override {
Tensor w_in = ctx->mutable_input(0, false);
OP_REQUIRES(ctx, TensorShapeUtils::IsMatrix(w_in.shape()),
errors::InvalidArgument("Must be a matrix"));
Tensor w_out = ctx->mutable_input(1, false);
OP_REQUIRES(ctx, w_in.shape() == w_out.shape(),
errors::InvalidArgument("w_in.shape == w_out.shape"));
const Tensor& examples = ctx->input(2);
OP_REQUIRES(ctx, TensorShapeUtils::IsVector(examples.shape()),
errors::InvalidArgument("Must be a vector"));
const Tensor& labels = ctx->input(3);
OP_REQUIRES(ctx, examples.shape() == labels.shape(),
errors::InvalidArgument("examples.shape == labels.shape"));
const Tensor& learning_rate = ctx->input(4);
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(learning_rate.shape()),
errors::InvalidArgument("Must be a scalar"));
auto Tw_in = w_in.matrix<float>();
auto Tw_out = w_out.matrix<float>();
auto Texamples = examples.flat<int32>();
auto Tlabels = labels.flat<int32>();
auto lr = learning_rate.scalar<float>()();
const int64 vocab_size = w_in.dim_size(0);
const int64 dims = w_in.dim_size(1);
const int64 batch_size = examples.dim_size(0);
OP_REQUIRES(ctx, vocab_size == sampler_->num(),
errors::InvalidArgument("vocab_size mismatches: ", vocab_size,
" vs. ", sampler_->num()));
// Gradient accumulator for v_in.
Tensor buf(DT_FLOAT, TensorShape({dims}));
auto Tbuf = buf.flat<float>();
// Scalar buffer to hold sigmoid(+/- dot).
Tensor g_buf(DT_FLOAT, TensorShape({}));
auto g = g_buf.scalar<float>();
// The following loop needs 2 random 32-bit values per negative
// sample. We reserve 8 values per sample just in case the
// underlying implementation changes.
auto rnd = base_.ReserveSamples32(batch_size * num_samples_ * 8);
random::SimplePhilox srnd(&rnd);
for (int64 i = 0; i < batch_size; ++i) {
const int32 example = Texamples(i);
DCHECK(0 <= example && example < vocab_size) << example;
const int32 label = Tlabels(i);
DCHECK(0 <= label && label < vocab_size) << label;
auto v_in = Tw_in.chip<0>(example);
// Positive: example predicts label.
// forward: x = v_in' * v_out
// l = log(sigmoid(x))
// backward: dl/dx = g = sigmoid(-x)
// dl/d(v_in) = g * v_out'
// dl/d(v_out) = v_in' * g
{
auto v_out = Tw_out.chip<0>(label);
auto dot = (v_in * v_out).sum();
g = (dot.exp() + 1.f).inverse();
Tbuf = v_out * (g() * lr);
v_out += v_in * (g() * lr);
}
// Negative samples:
// forward: x = v_in' * v_sample
// l = log(sigmoid(-x))
// backward: dl/dx = g = -sigmoid(x)
// dl/d(v_in) = g * v_out'
// dl/d(v_out) = v_in' * g
for (int j = 0; j < num_samples_; ++j) {
const int sample = sampler_->Sample(&srnd);
if (sample == label) continue; // Skip.
auto v_sample = Tw_out.chip<0>(sample);
auto dot = (v_in * v_sample).sum();
g = -((-dot).exp() + 1.f).inverse();
Tbuf += v_sample * (g() * lr);
v_sample += v_in * (g() * lr);
}
// Applies the gradient on v_in.
v_in += Tbuf;
}
}
private:
int32 num_samples_ = 0;
random::DistributionSampler* sampler_ = nullptr;
GuardedPhiloxRandom base_;
};
REGISTER_KERNEL_BUILDER(Name("NegTrainWord2vec").Device(DEVICE_CPU), NegTrainWord2vecOp);
} // end namespace tensorflow