forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_block.cpp
More file actions
482 lines (425 loc) · 14.5 KB
/
model_block.cpp
File metadata and controls
482 lines (425 loc) · 14.5 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <fstream>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <limits>
#include "utils.hpp"
#include <stdlib.h>
#include "model_block.h"
#include "lda_document.h"
namespace lda
{
using namespace std;
int64_t upper_bound(int64_t x)
{
if (x == 0)
{
return 0;
}
int64_t shift = 0;
int64_t y = 1;
x--;
while (x)
{
x = x >> 1;
y = y << 1;
++shift;
}
return y;
}
int32_t align64(int32_t size)
{
if (size % 64 == 0)
{
return size;
}
else
{
size = 64 * (size / 64) + 64;
return size;
}
}
LDAModelBlock::LDAModelBlock()
: dict_(nullptr),
num_vocabs_(0),
mem_block_size_(0),
mem_block_(nullptr),
alias_mem_block_size_(0),
alias_mem_block_(nullptr)
{
}
LDAModelBlock::~LDAModelBlock()
{
Clear();
}
void LDAModelBlock::Clear()
{
if (dict_)
{
delete[]dict_;
dict_ = nullptr;
}
if (mem_block_)
{
delete[]mem_block_;
mem_block_ = nullptr;
}
if (alias_mem_block_)
{
delete[]alias_mem_block_;
alias_mem_block_ = nullptr;
}
num_vocabs_ = -1;
num_topics_ = -1;
mem_block_size_ = 0;
alias_mem_block_size_ = 0;
}
void LDAModelBlock::Init(int32_t num_vocabs, int32_t num_topics, int64_t nonzero_num)
{
num_vocabs_ = num_vocabs;
num_topics_ = num_topics;
dict_ = new WordEntry[num_vocabs_];
for (int i = 0; i < num_vocabs_; ++i)
{
dict_[i].is_dense_ = 0;
dict_[i].is_alias_dense_ = 0;
}
uint64_t size = 2 * upper_bound(load_factor_ * nonzero_num);
if (nonzero_num < 0 || size > numeric_limits<size_t>::max())
throw bad_alloc();
mem_block_size_ = static_cast<size_t>(size);
size = nonzero_num * 3;
if (static_cast<uint64_t>(nonzero_num) > numeric_limits<size_t>::max() / 3)
throw bad_alloc();
alias_mem_block_size_ = static_cast<size_t>(size);
mem_block_ = new int32_t[mem_block_size_](); // NOTE: force to initialize the values to be zero
alias_mem_block_ = new int32_t[alias_mem_block_size_](); // NOTE: force to initialize the values to be zero
}
void LDAModelBlock::Init(int32_t num_vocabs, int32_t num_topics, int64_t mem_block_size, int64_t alias_mem_block_size)
{
num_vocabs_ = num_vocabs;
num_topics_ = num_topics;
dict_ = new WordEntry[num_vocabs_];
for (int i = 0; i < num_vocabs_; ++i)
{
dict_[i].is_dense_ = 0;
dict_[i].is_alias_dense_ = 0;
}
if (mem_block_size < 0 || static_cast<uint64_t>(mem_block_size) > numeric_limits<size_t>::max())
throw bad_alloc();
mem_block_size_ = static_cast<size_t>(mem_block_size);
mem_block_ = new int32_t[mem_block_size_](); // NOTE : force to initialize the values to be zero
if (mem_block_size < 0 || static_cast<uint64_t>(alias_mem_block_size) > numeric_limits<size_t>::max())
throw bad_alloc();
alias_mem_block_size_ = static_cast<size_t>(alias_mem_block_size);
alias_mem_block_ = new int32_t[alias_mem_block_size_](); //NOTE: force to initialize the values to be zero
cout << "mem_block_size = " << sizeof(mem_block_size_) << endl;
cout << "alias_mem_block_size = " << sizeof(alias_mem_block_size_)<< endl;
offset_ = 0;
alias_offset_ = 0;
}
void LDAModelBlock::Init(int32_t num_vocabs, int32_t num_topics)
{
num_vocabs_ = num_vocabs;
num_topics_ = num_topics;
dict_ = new WordEntry[num_vocabs_];
for (int i = 0; i < num_vocabs_; ++i)
{
// This warning is a false positive caused by an old bug in PREfast. It is fixed in VS 2015.
#pragma warning(suppress: 6386)
dict_[i].tf = 0;
dict_[i].is_dense_ = 0;
dict_[i].is_alias_dense_ = 0;
}
}
void LDAModelBlock::SetWordInfo(int word_id, int32_t nonzero_num, bool fullSparse)
{
dict_[word_id].word_id_ = word_id;
dict_[word_id].tf = nonzero_num;
int32_t hot_thresh;
if (fullSparse)
{
// use a very large threshold to ensure every row of word-topic-table using a sparse representation
hot_thresh = numeric_limits<int32_t>::max();
}
else
{
hot_thresh = num_topics_ / (2 * load_factor_); //hybrid
}
int32_t alias_hot_thresh;
if (fullSparse)
{
// use a very large threshold to ensure every row of alias table using a sparse representation
alias_hot_thresh = numeric_limits<int32_t>::max();
}
else
{
alias_hot_thresh = (num_topics_ * 2) / 3;
}
int32_t capacity = 0;
int32_t row_size = 0;
int32_t alias_capacity = 0;
int32_t alias_row_size = 0;
if (dict_[word_id].tf >= hot_thresh)
{
dict_[word_id].is_dense_ = 1;
capacity = num_topics_;
row_size = capacity;
}
else if (dict_[word_id].tf > 0)
{
dict_[word_id].is_dense_ = 0;
int capacity_lower_bound = load_factor_ * dict_[word_id].tf;
capacity = (int32_t)upper_bound(capacity_lower_bound);
row_size = capacity * 2;
}
else
{
dict_[word_id].is_dense_ = 1;
row_size = 0;
capacity = 0;
}
dict_[word_id].offset_ = offset_;
dict_[word_id].end_offset_ = offset_ + row_size;
dict_[word_id].capacity_ = capacity;
offset_ += row_size;
if (dict_[word_id].tf >= alias_hot_thresh)
{
alias_capacity = num_topics_;
alias_row_size = 2 * num_topics_;
dict_[word_id].is_alias_dense_ = 1;
}
else if (dict_[word_id].tf > 0)
{
alias_capacity = dict_[word_id].tf;
alias_row_size = 3 * dict_[word_id].tf;
dict_[word_id].is_alias_dense_ = 0;
}
else
{
alias_capacity = 0;
alias_row_size = 0;
dict_[word_id].is_alias_dense_ = 1;
}
dict_[word_id].alias_capacity_ = alias_capacity;
dict_[word_id].alias_offset_ = alias_offset_;
dict_[word_id].alias_end_offset_ = alias_offset_ + alias_row_size;
alias_offset_ += alias_row_size;
}
// NOTE: sometimes, we use totally sparse representation (in testing phase), fullSparse == true
// in other times, we use hybrid structure (in training phase), fullSparse == false
void LDAModelBlock::InitModelBlockByTFS(bool fullSparse)
{
const int32_t max_tf_thresh = numeric_limits<int32_t>::max();
int32_t hot_thresh;
if (fullSparse)
{
// totally sparse
// use a very large threshold to ensure every row of word-topic-table using a sparse representation
hot_thresh = numeric_limits<int32_t>::max();
}
else
{
// hybrid
hot_thresh = num_topics_ / (2 * load_factor_);
}
int32_t alias_hot_thresh;
if (fullSparse)
{
// use a very large threshold to ensure every row of alias table using a sparse representation
alias_hot_thresh = numeric_limits<int32_t>::max();
}
else
{
alias_hot_thresh = (num_topics_ * 2) / 3;
}
int32_t word_id;
int32_t capacity = 0;
int32_t row_size = 0;
int32_t alias_capacity = 0;
int32_t alias_row_size = 0;
int64_t offset = 0;
int64_t alias_offset = 0;
for (word_id = 0; word_id < num_vocabs_; ++word_id)
{
int32_t tf = dict_[word_id].tf;
dict_[word_id].word_id_ = word_id;
dict_[word_id].tf = tf;
if (tf >= hot_thresh)
{
dict_[word_id].is_dense_ = 1;
capacity = num_topics_;
row_size = capacity;
}
else if (tf > 0)
{
dict_[word_id].is_dense_ = 0;
int capacity_lower_bound = load_factor_ * tf;
capacity = (int32_t)upper_bound(capacity_lower_bound);
row_size = capacity * 2;
}
else
{
dict_[word_id].is_dense_ = 1;
capacity = 0;
row_size = 0;
}
dict_[word_id].offset_ = offset;
dict_[word_id].end_offset_ = offset + row_size;
dict_[word_id].capacity_ = capacity;
offset += row_size;
if (tf >= alias_hot_thresh)
{
alias_capacity = num_topics_;
alias_row_size = 2 * num_topics_;
dict_[word_id].is_alias_dense_ = 1;
}
else if (tf > 0)
{
alias_capacity = tf;
alias_row_size = 3 * tf;
dict_[word_id].is_alias_dense_ = 0;
}
else
{
alias_capacity = 0;
alias_row_size = 0;
dict_[word_id].is_alias_dense_ = 1;
}
dict_[word_id].alias_capacity_ = alias_capacity;
dict_[word_id].alias_offset_ = alias_offset;
dict_[word_id].alias_end_offset_ = alias_offset + alias_row_size;
alias_offset += alias_row_size;
}
uint64_t size = dict_[num_vocabs_ - 1].end_offset_;
if (size > numeric_limits<size_t>::max())
throw bad_alloc();
mem_block_size_ = static_cast<size_t>(size);
mem_block_ = new int32_t[mem_block_size_](); // NOTE: force to initialize the values to be zero
size = dict_[num_vocabs_ - 1].alias_end_offset_;
if (size > numeric_limits<size_t>::max())
throw bad_alloc();
alias_mem_block_size_ = static_cast<size_t>(size);
alias_mem_block_ = new int32_t[alias_mem_block_size_](); //NOTE: force to initialize the values to be zero
cout << "mem_block_size = " << sizeof(mem_block_size_) << endl;
cout << "alias_mem_block_size = " << sizeof(alias_mem_block_size_) << endl;
}
void LDAModelBlock::InitFromDataBlock(const LDADataBlock &data_block, int32_t num_vocabs, int32_t num_topics)
{
num_vocabs_ = num_vocabs;
num_topics_ = num_topics;
int32_t doc_num = data_block.num_documents();
dict_ = new WordEntry[num_vocabs_];
for (int i = 0; i < num_vocabs_; ++i)
{
dict_[i].tf = 0;
}
for (int i = 0; i < doc_num; ++i)
{
shared_ptr<LDADocument> doc = data_block.GetOneDoc(i);
int32_t doc_size = doc->size();
for (int j = 0; j < doc_size; ++j)
{
int32_t w = doc->Word(j);
dict_[w].tf++;
}
}
InitModelBlockByTFS(false);
}
// Count the number of nonzero values in each row
void LDAModelBlock::CountNonZero(vector<int32_t> &tfs)
{
for (int i = 0; i < num_vocabs_; ++i)
{
hybrid_map row(mem_block_ + dict_[i].offset_,
dict_[i].is_dense_,
dict_[i].capacity_,
0,
nullptr);
tfs[i] = row.nonzero_num();
}
}
void LDAModelBlock::GetModelSizeByTFS(bool fullSparse, vector<int32_t> &tfs, int64_t &mem_block_size, int64_t &alias_mem_block_size)
{
const int32_t max_tf_thresh = numeric_limits<int32_t>::max();
int32_t hot_thresh;
if (fullSparse)
{
// totally sparse
// use a very large threshold to ensure every row of word-topic-table using a sparse representation
hot_thresh = numeric_limits<int32_t>::max();
}
else
{
// hybrid
hot_thresh = num_topics_ / (2 * load_factor_);
}
// hot_thresh = 0; // totally dense
int32_t alias_hot_thresh;
if (fullSparse)
{
// use a very large threshold to ensure every row of alias table using a sparse representation
alias_hot_thresh = numeric_limits<int32_t>::max();
}
else
{
alias_hot_thresh = (num_topics_ * 2) / 3;
}
int32_t word_id;
int32_t capacity = 0;
int32_t alias_capacity = 0;
int32_t row_size = 0;
int32_t alias_row_size = 0;
mem_block_size = 0;
alias_mem_block_size = 0;
for (word_id = 0; word_id < num_vocabs_; ++word_id)
{
int32_t tf = tfs[word_id];
if (tf >= hot_thresh)
{
capacity = num_topics_;
row_size = capacity;
}
else if (tf > 0)
{
int capacity_lower_bound = load_factor_ * tf;
capacity = (int32_t)upper_bound(capacity_lower_bound);
row_size = capacity * 2;
}
else
{
capacity = 0;
row_size = 0;
}
mem_block_size += row_size;
if (tf >= alias_hot_thresh)
{
alias_capacity = num_topics_;
alias_row_size = 2 * num_topics_;
}
else if (tf > 0)
{
alias_capacity = tf;
alias_row_size = 3 * tf;
}
else
{
alias_capacity = 0;
alias_row_size = 0;
}
alias_mem_block_size += alias_row_size;
}
}
// NOTE: we can re-use the dict_ variable here, but we deliberately not use it.
// This function should not change the internal state of model_block_
void LDAModelBlock::GetModelStat(int64_t &mem_block_size, int64_t &alias_mem_block_size)
{
vector<int32_t> tfs(num_vocabs_, 0);
CountNonZero(tfs);
// calculate the mem_block_size, alias_mem_block_size
GetModelSizeByTFS(true, tfs, mem_block_size, alias_mem_block_size);
}
}