Skip to content

Commit b97c148

Browse files
committed
bug-fix in tokenization
1 parent f094b95 commit b97c148

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

cpp/include/culda/cuda_lda_kernels.cuh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ float Digamma(float x) {
2828
__global__ void EstepKernel(
2929
const int* cols, const int* indptr, const bool* vali,
3030
const int num_cols, const int num_indptr,
31-
const int num_words, const int num_topics, const int num_iters,
31+
const int num_topics, const int num_iters,
3232
float* gamma, float* new_gamma, float* phi,
3333
float* alpha, float* beta,
3434
float* grad_alpha, float* new_beta, float* train_losses, float* vali_losses) {
@@ -39,10 +39,8 @@ __global__ void EstepKernel(
3939
float* _phi = phi + num_topics * blockIdx.x;
4040
float* _grad_alpha = grad_alpha + num_topics * blockIdx.x;
4141

42-
4342
for (int i = blockIdx.x; i < num_indptr; i += gridDim.x) {
4443
int beg = indptr[i], end = indptr[i + 1];
45-
4644
// initialize gamma
4745
for (int j = threadIdx.x; j < num_topics; j += blockDim.x)
4846
_gamma[j] = alpha[j] + (end - beg) / num_topics;
@@ -59,7 +57,6 @@ __global__ void EstepKernel(
5957
for (int k = beg; k < end; ++k) {
6058
const int w = cols[k];
6159
const bool _vali = vali[k];
62-
6360
// compute phi
6461
if (not _vali or j + 1 == num_iters) {
6562
for (int l = threadIdx.x; l < num_topics; l += blockDim.x)

cpp/src/culda/culda.cu

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool CuLDA::Init(std::string opt_path) {
3939
void CuLDA::LoadModel(float* alpha, float* beta,
4040
float* grad_alpha, float* new_beta, int num_words) {
4141
num_words_ = num_words;
42-
DEBUG("copy model({} x {})", num_topics_, num_words_);
42+
DEBUG("copy model({} x {})", num_words_, num_topics_);
4343
dev_alpha_.resize(num_topics_);
4444
dev_beta_.resize(num_topics_ * num_words_);
4545
thrust::copy(alpha, alpha + num_topics_, dev_alpha_.begin());
@@ -49,7 +49,7 @@ void CuLDA::LoadModel(float* alpha, float* beta,
4949
// resize device vector
5050
grad_alpha_ = grad_alpha;
5151
new_beta_ = new_beta;
52-
dev_grad_alpha_.resize(block_cnt_ * num_topics_);
52+
dev_grad_alpha_.resize(num_topics_ * block_cnt_);
5353
dev_new_beta_.resize(num_topics_ * num_words_);
5454

5555
// copy to device
@@ -74,15 +74,15 @@ std::pair<float, float> CuLDA::FeedData(
7474
thrust::copy(cols, cols + num_cols, dev_cols.begin());
7575
thrust::copy(indptr, indptr + num_indptr + 1, dev_indptr.begin());
7676
thrust::copy(vali, vali + num_cols, dev_vali.begin());
77-
7877
CHECK_CUDA(cudaDeviceSynchronize());
78+
DEBUG0("copy feed data to GPU memory");
7979

8080
// run E step in GPU
8181
EstepKernel<<<block_cnt_, block_dim_>>>(
8282
thrust::raw_pointer_cast(dev_cols.data()),
8383
thrust::raw_pointer_cast(dev_indptr.data()),
8484
thrust::raw_pointer_cast(dev_vali.data()),
85-
num_cols, num_indptr, num_words_, num_topics_, num_iters,
85+
num_cols, num_indptr, num_topics_, num_iters,
8686
thrust::raw_pointer_cast(dev_gamma_.data()),
8787
thrust::raw_pointer_cast(dev_new_gamma_.data()),
8888
thrust::raw_pointer_cast(dev_phi_.data()),
@@ -92,14 +92,15 @@ std::pair<float, float> CuLDA::FeedData(
9292
thrust::raw_pointer_cast(dev_new_beta_.data()),
9393
thrust::raw_pointer_cast(dev_train_losses.data()),
9494
thrust::raw_pointer_cast(dev_vali_losses.data()));
95-
9695
CHECK_CUDA(cudaDeviceSynchronize());
96+
DEBUG0("run E step in GPU");
9797

9898
// pull loss
9999
std::vector<float> train_losses(block_cnt_), vali_losses(block_cnt_);
100100
thrust::copy(dev_train_losses.begin(), dev_train_losses.end(), train_losses.begin());
101101
thrust::copy(dev_vali_losses.begin(), dev_vali_losses.end(), vali_losses.begin());
102102
CHECK_CUDA(cudaDeviceSynchronize());
103+
DEBUG0("pull loss values");
103104

104105
// accumulate
105106
float train_loss = std::accumulate(train_losses.begin(), train_losses.end(), 0.0f);

cpp/src/utils/ioutils.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ std::pair<int, int> IoUtils::TokenizeStream(int num_lines, int num_threads) {
9090

9191
// tokenize
9292
for (auto& word: line_vec) {
93-
if (not word_count_.count(word)) continue;
94-
cols_[i].push_back(word_count_[word]);
93+
if (not word_idmap_.count(word)) continue;
94+
cols_[i].push_back(word_idmap_[word]);
9595
}
9696
}
9797
}
@@ -155,6 +155,7 @@ std::pair<int, int> IoUtils::ReadStreamForVocab(int num_lines, int num_threads)
155155

156156
void IoUtils::GetWordVocab(int min_count, std::string keys_path) {
157157
INFO("number of raw words: {}", word_count_.size());
158+
word_idmap_.clear(); word_list_.clear();
158159
for (auto& it: word_count_) {
159160
if (it.second >= min_count) {
160161
word_idmap_[it.first] = word_idmap_.size();

cusim/culda/bindings.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CuLDABind {
4747
throw std::runtime_error("invalid grad_alpha or new_beta");
4848
}
4949

50-
int num_words = beta_buffer.shape[1];
50+
int num_words = beta_buffer.shape[0];
5151

5252
return obj_.LoadModel(_alpha.mutable_data(0),
5353
_beta.mutable_data(0),
@@ -67,7 +67,7 @@ class CuLDABind {
6767
throw std::runtime_error("invalid cols or indptr");
6868
}
6969
int num_cols = cols_buffer.shape[0];
70-
int num_indptr = indptr_buffer.shape[0];
70+
int num_indptr = indptr_buffer.shape[0] - 1;
7171
return obj_.FeedData(_cols.data(0), _indptr.data(0), _vali.data(0),
7272
num_cols, num_indptr, num_iters);
7373
}

cusim/culda/pyculda.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from cusim.culda.culda_bind import CuLDABind
2020
from cusim.config_pb2 import CuLDAConfigProto
2121

22+
EPS = 1e-10
23+
2224
class CuLDA:
2325
def __init__(self, opt=None):
2426
self.opt = aux.get_opt_as_proto(opt or {}, CuLDAConfigProto)
@@ -77,6 +79,8 @@ def init_model(self):
7779
self.grad_alpha = np.zeros(shape=(block_cnt, self.opt.num_topics),
7880
dtype=np.float32)
7981
self.new_beta = np.zeros(shape=self.beta.shape, dtype=np.float32)
82+
self.logger.info("grad alpha %s, new beta %s initialized",
83+
self.grad_alpha.shape, self.new_beta.shape)
8084

8185
# push it to gpu
8286
self.obj.load_model(self.alpha, self.beta, self.grad_alpha, self.new_beta)
@@ -118,10 +122,10 @@ def _train_e_step(self, h5f):
118122
vali_loss_nume -= vali_loss
119123
vali_cnt = np.count_nonzero(vali)
120124
train_cnt = len(vali) - vali_cnt
121-
train_loss_nume += train_cnt
122-
vali_loss_nume += train_cnt
123-
train_loss = train_loss_nume / train_loss_deno
124-
vali_loss = vali_loss_nume / vali_loss_deno
125+
train_loss_deno += train_cnt
126+
vali_loss_deno += vali_cnt
127+
train_loss = train_loss_nume / (train_loss_deno + EPS)
128+
vali_loss = vali_loss_nume / (vali_loss_deno + EPS)
125129

126130
# update progress bar
127131
pbar.update(end, values=[("train_loss", train_loss),

examples/example1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def run_lda():
4242
opt = {
4343
"data_path": DATA_PATH,
4444
"data_dir": DATA_PATH2,
45+
# "skip_preprocess": True,
46+
"c_log_level": 3,
4547
}
4648
lda = CuLDA(opt)
4749
lda.train_model()

0 commit comments

Comments
 (0)