-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpi_estc_lda.cc
359 lines (320 loc) · 11.7 KB
/
mpi_estc_lda.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
// Copyright 2008 Google Inc.
//
// 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.
/*
An example running of this program:
mpiexec -n 2 ./mpi_lda \
--num_topics 2 \
--alpha 0.1 \
--beta 0.01 \
--training_data_file ./testdata/test_data.txt \
--model_file /tmp/lda_model.txt \
--burn_in_iterations 100 \
--total_iterations 150
*/
#include "mpi.h"
#include <algorithm>
#include <fstream>
#include <set>
#include <vector>
#include <sstream>
#include <string>
#include "common.h"
#include "document.h"
#include "model.h"
#include "accumulative_model.h"
#include "sampler.h"
#include "cmd_flags.h"
using std::ifstream;
using std::ofstream;
using std::istringstream;
using std::set;
using std::vector;
using std::list;
using std::map;
using std::sort;
using std::string;
using learning_lda::LDADocument;
namespace learning_lda {
using std::ifstream;
using std::ofstream;
using std::ostream;
using std::istringstream;
using std::set;
using std::map;
// A wrapper of MPI_Allreduce. If the vector is over 32M, we allreduce part
// after part. This will save temporary memory needed.
void AllReduceTopicDistribution(int64* buf, int count) {
static int kMaxDataCount = 1 << 22;
static int datatype_size = sizeof(*buf);
if (count > kMaxDataCount) {
char* tmp_buf = new char[datatype_size * kMaxDataCount];
for (int i = 0; i < count / kMaxDataCount; ++i) {
MPI_Allreduce(reinterpret_cast<char*>(buf) +
datatype_size * kMaxDataCount * i,
tmp_buf,
kMaxDataCount, MPI_LONG_LONG, MPI_SUM, MPI_COMM_WORLD);
memcpy(reinterpret_cast<char*>(buf) +
datatype_size * kMaxDataCount * i, tmp_buf,
kMaxDataCount * datatype_size);
}
// If count is not divisible by kMaxDataCount, there are some elements left
// to be reduced.
if (count % kMaxDataCount > 0) {
MPI_Allreduce(reinterpret_cast<char*>(buf)
+ datatype_size * kMaxDataCount * (count / kMaxDataCount),
tmp_buf,
count - kMaxDataCount * (count / kMaxDataCount), MPI_LONG_LONG, MPI_SUM,
MPI_COMM_WORLD);
memcpy(reinterpret_cast<char*>(buf)
+ datatype_size * kMaxDataCount * (count / kMaxDataCount),
tmp_buf,
(count - kMaxDataCount * (count / kMaxDataCount)) * datatype_size);
}
delete[] tmp_buf;
} else {
char* tmp_buf = new char[datatype_size * count];
MPI_Allreduce(buf, tmp_buf, count, MPI_LONG_LONG, MPI_SUM, MPI_COMM_WORLD);
memcpy(buf, tmp_buf, datatype_size * count);
delete[] tmp_buf;
}
}
class ParallelLDAModel : public LDAModel {
public:
ParallelLDAModel(int num_topic, const map<string, int>& word_index_map)
: LDAModel(num_topic, word_index_map) {
}
void ComputeAndAllReduce(const LDACorpus& corpus) {
for (list<LDADocument*>::const_iterator iter = corpus.begin();
iter != corpus.end();
++iter) {
LDADocument* document = *iter;
for (LDADocument::WordOccurrenceIterator iter2(document);
!iter2.Done(); iter2.Next()) {
IncrementTopic(iter2.Word(), iter2.Topic(), 1);
}
}
AllReduceTopicDistribution(&memory_alloc_[0], memory_alloc_.size());
}
};
int DistributelyLoadAndInitTrainingCorpus(
const string& corpus_file,
int file_type,
int num_topics,
int myid, int pnum, LDACorpus* corpus, map<string, int>* word_index_map) {
corpus->clear();
ifstream fin(corpus_file.c_str());
string line;
int index = 0;
while (getline(fin, line)) { // Each line is a training document.
if (line.size() > 0 && // Skip empty lines.
line[0] != '\r' && // Skip empty lines.
line[0] != '\n' && // Skip empty lines.
line[0] != '#') { // Skip comment lines.
istringstream ss(line);
if ((myid == 0) && (index % 500000 == 0))
{
std::cout << "Loading... "<< index << "\n";;
}
if (index % pnum == myid) {
// This is a document that I need to store in local memory.
string doc_name;
ss >> doc_name;
// printf("%s",doc_name.c_str());
DocumentWordTopicsPB document;
string word;
int count;
set<string> words_in_document;
while (ss >> word ) { // Load and init a document.
if (0==file_type)
{
ss >> count;
}
else
{
count = 1;
}
vector<int32> topics;
for (int i = 0; i < count; ++i) {
topics.push_back(RandInt(num_topics));
}
int word_index;
map<string, int>::const_iterator iter = word_index_map->find(word);
if (iter == word_index_map->end()) {
// word_index = word_index_map->size();
// (*word_index_map)[word] = word_index;
continue;
} else {
word_index = iter->second;
}
document.add_wordtopics(word, word_index, topics);
words_in_document.insert(word);
}
if (words_in_document.size() > 0) {
corpus->push_back(new LDADocument(doc_name, document, num_topics));
}
}
index++;
}
}
return corpus->size();
}
void OutputAssignments(LDACorpus* corpus,vector<string>& index_word_map,string file_out1,string file_out2) {
ofstream out1(file_out1.c_str());
ofstream out2(file_out2.c_str());
for (list<LDADocument*>::iterator iter = corpus->begin();
iter != corpus->end();
++iter) {
const vector<int64> topic_distributions_vec = (*iter)->topic_distribution();
out1 << (*iter)->document_name();
for (int topic = 0; topic < topic_distributions_vec.size(); ++topic) {
out1 << " " << topic_distributions_vec[topic];
}
out1 << "\n";
out2 << (*iter)->document_name();
vector<pair<int,int> > word_assign;
(*iter)->word_assignments(word_assign);
for (int index = 0; index < word_assign.size(); ++index) {
out2 << " " << index_word_map[word_assign[index].first]<< ":" << word_assign[index].second;
}
out2 << "\n";
}
}
void FreeCorpus(LDACorpus* corpus) {
for (list<LDADocument*>::iterator iter = corpus->begin();
iter != corpus->end();
++iter) {
if (*iter != NULL) {
delete *iter;
*iter = NULL;
}
}
}
} // namespace learning_lda
int main(int argc, char** argv) {
using learning_lda::LDACorpus;
using learning_lda::LDAModel;
using learning_lda::ParallelLDAModel;
using learning_lda::LDASampler;
using learning_lda::LDATopicSplitRule;
using learning_lda::LoadWordIndex;
using learning_lda::LoadWordLex;
using learning_lda::LoadWordSet;
using learning_lda::DistributelyLoadAndInitTrainingCorpus;
using learning_lda::OutputAssignments;
using learning_lda::generate_model_name;
using learning_lda::LDACmdLineFlags;
using std::ifstream;
using std::ofstream;
int myid, pnum;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &pnum);
LDACmdLineFlags flags;
flags.ParseCmdFlags(argc, argv);
srand(time(NULL));
LDACorpus corpus;
map<string, int> word_index_map;
ifstream model_fin(flags.model_file_.c_str());
LDAModel model(model_fin, &word_index_map);
set<int> new_words;
if (flags.new_words_file_.length() > 0 )
{
ifstream newwords_fin(flags.new_words_file_.c_str());
LoadWordSet(newwords_fin,word_index_map,new_words);
}
vector<string> index_word_map(word_index_map.size());
CHECK_GT(LoadWordLex(word_index_map,index_word_map), 0);
LDATopicSplitRule rule;
ifstream rule_fin(flags.rule_file_.c_str());
rule.LoadSplitRule(rule_fin ,word_index_map);
CHECK_GT(DistributelyLoadAndInitTrainingCorpus(flags.training_data_file_,
flags.file_type_,
flags.num_topics_,
myid, pnum, &corpus, &word_index_map), 0);
std::cout << "Training data loaded" << std::endl;
//// Make vocabulary words sorted and give each word an int index.
//vector<string> sorted_words;
//map<string, int> word_index_map;
//for (set<string>::const_iterator iter = allwords.begin();
// iter != allwords.end(); ++iter) {
// sorted_words.push_back(*iter);
//}
//sort(sorted_words.begin(), sorted_words.end());
//for (int i = 0; i < sorted_words.size(); ++i) {
// word_index_map[sorted_words[i]] = i;
//}
//for (LDACorpus::iterator iter = corpus.begin(); iter != corpus.end();
// ++iter) {
// (*iter)->ResetWordIndex(word_index_map);
//}
LDASampler sampler(flags.alpha_, flags.beta_, &model, NULL);
for (int iter = 0; iter < flags.burn_in_iterations_; ++iter) {
if (myid == 0) {
std::cout << "Iteration " << iter << " ...\n";
}
ParallelLDAModel model(flags.num_topics_, word_index_map);
model.ComputeAndAllReduce(corpus);
sampler.DoIteration(&corpus, false, iter < flags.burn_in_iterations_);
}
sampler.AdjustCorpusWithRule(corpus,flags.num_topics_ ,new_words,rule);
for (int iter = 0; iter < flags.total_iterations_; ++iter) {
if (myid == 0) {
std::cout << "Iteration " << iter << " ...\n";
}
ParallelLDAModel new_model(flags.num_topics_, word_index_map);
new_model.ComputeAndAllReduce(corpus);
if ((flags.save_step_ > 0) && (iter % flags.save_step_ == 0)) {
// saving the model
if (myid == 0) {
printf("Saving the Assignments File at iteration %d ...\n", iter);
string file_out = generate_model_name(flags.new_model_file_, myid, iter);
std::ofstream fout(file_out.c_str());
new_model.AppendAsString(fout);
}
string file_out1 = generate_model_name(flags.topic_distribution_file_, myid, iter);
string file_out2 = generate_model_name(flags.topic_assignments_file_, myid, iter);
OutputAssignments(&corpus,index_word_map,file_out1,file_out2);
}
LDASampler new_sampler(flags.alpha_, flags.beta_, &new_model, NULL);
if (flags.compute_likelihood_ == "true") {
double loglikelihood_local = 0;
double loglikelihood_global = 0;
for (list<LDADocument*>::const_iterator iter = corpus.begin();
iter != corpus.end();
++iter) {
printf("%s\n",(*iter)->document_name().c_str());
loglikelihood_local += new_sampler.LogLikelihood(*iter);
}
MPI_Allreduce(&loglikelihood_local, &loglikelihood_global, 1, MPI_DOUBLE,
MPI_SUM, MPI_COMM_WORLD);
if (myid == 0) {
std::cout << "Loglikelihood: " << loglikelihood_global << std::endl;
}
}
new_sampler.DoIteration2(&corpus, true, false);
}
ParallelLDAModel new_model(flags.num_topics_, word_index_map);
new_model.ComputeAndAllReduce(corpus);
if (myid == 0) {
string file_out = generate_model_name(flags.new_model_file_, myid, -1);
std::ofstream fout(file_out.c_str());
new_model.AppendAsString(fout);
}
string file_out1 = generate_model_name(flags.topic_distribution_file_, myid, -1);
string file_out2 = generate_model_name(flags.topic_assignments_file_, myid, -1);
OutputAssignments(&corpus,index_word_map,file_out1,file_out2);
FreeCorpus(&corpus);
MPI_Finalize();
return 0;
}