Skip to content

Commit

Permalink
fix indexing error in word2vec_inner.pyx (#3136)
Browse files Browse the repository at this point in the history
* Add explicit typecasting for the W2V cython code

* Add explicit typecasting for the W2V cython code

* Unifying the types

* Fix minor code style issues

* Code refactoring

* Update CHANGELOG.md

Co-authored-by: Jinhyuk Yun <jhyun@ssu>
Co-authored-by: Michael Penkov <m@penkov.dev>
  • Loading branch information
3 people authored May 13, 2021
1 parent 52281eb commit ec95fe4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
Empty file added 16
Empty file.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changes
- Improve & unify docs for dirichlet priors (PR [#3125](https://github.com/RaRe-Technologies/gensim/pull/3125), [@jonaschn](https://github.com/jonaschn))
- Materialize and copy the corpus passed to SoftCosineSimilarity (PR [#3128](https://github.com/RaRe-Technologies/gensim/pull/3128), [@Witiko](https://github.com/Witiko))
- [#3115](https://github.com/RaRe-Technologies/gensim/pull/3115): Make LSI dispatcher CLI param for number of jobs optional, by [@robguinness](https://github.com/robguinness))
- [#3136](https://github.com/RaRe-Technologies/gensim/pull/3136): fix indexing error in word2vec_inner.pyx, by [@bluekura](https://github.com/bluekura)
- Update link to Hoffman paper (online VB LDA) (PR [#3133](https://github.com/RaRe-Technologies/gensim/pull/3133), [@jonaschn](https://github.com/jonaschn))
- fix bug when loading saved Phrases model (PR [#3116](https://github.com/RaRe-Technologies/gensim/pull/3116), [@aloknayak29](https://github.com/aloknayak29))
- Optimize performance of Author-Topic model (PR [#2978](https://github.com/RaRe-Technologies/gensim/pull/2978), [@horpto](https://github.com/horpto))
Expand Down
32 changes: 18 additions & 14 deletions gensim/models/word2vec_inner.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ cdef void our_saxpy_noblas(const int *N, const float *alpha, const float *X, con
for i from 0 <= i < N[0] by 1:
Y[i * (incY[0])] = (alpha[0]) * X[i * (incX[0])] + Y[i * (incY[0])]

cdef long long _mul(const np.uint32_t a, const int b) nogil:
"""Safe multiplication of ints with explict typecasting"""
return <long long>a * <long long>b

cdef void w2v_fast_sentence_sg_hs(
const np.uint32_t *word_point, const np.uint8_t *word_code, const int codelen,
REAL_t *syn0, REAL_t *syn1, const int size,
Expand Down Expand Up @@ -112,12 +116,12 @@ cdef void w2v_fast_sentence_sg_hs(
"""

cdef long long a, b
cdef long long row1 = word2_index * size, row2, sgn
cdef long long row1 = _mul(word2_index, size), row2, sgn
cdef REAL_t f, g, f_dot, lprob

memset(work, 0, size * cython.sizeof(REAL_t))
for b in range(codelen):
row2 = word_point[b] * size
row2 = _mul(word_point[b], size)
f_dot = our_dot(&size, &syn0[row1], &ONE, &syn1[row2], &ONE)
if f_dot <= -MAX_EXP or f_dot >= MAX_EXP:
continue
Expand Down Expand Up @@ -206,7 +210,7 @@ cdef unsigned long long w2v_fast_sentence_sg_neg(
"""
cdef long long a
cdef long long row1 = word2_index * size, row2
cdef long long row1 = _mul(word2_index, size), row2
cdef unsigned long long modulo = 281474976710655ULL
cdef REAL_t f, g, label, f_dot, log_e_f_dot
cdef np.uint32_t target_index
Expand All @@ -225,7 +229,7 @@ cdef unsigned long long w2v_fast_sentence_sg_neg(
continue
label = <REAL_t>0.0

row2 = target_index * size
row2 = _mul(target_index, size)
f_dot = our_dot(&size, &syn0[row1], &ONE, &syn1neg[row2], &ONE)
if f_dot <= -MAX_EXP or f_dot >= MAX_EXP:
continue
Expand Down Expand Up @@ -309,15 +313,15 @@ cdef void w2v_fast_sentence_cbow_hs(
continue
else:
count += ONEF
our_saxpy(&size, &ONEF, &syn0[indexes[m] * size], &ONE, neu1, &ONE)
our_saxpy(&size, &ONEF, &syn0[_mul(indexes[m], size)], &ONE, neu1, &ONE)
if count > (<REAL_t>0.5):
inv_count = ONEF/count
if cbow_mean:
sscal(&size, &inv_count, neu1, &ONE) # (does this need BLAS-variants like saxpy?)

memset(work, 0, size * cython.sizeof(REAL_t))
for b in range(codelens[i]):
row2 = word_point[b] * size
row2 = _mul(word_point[b], size)
f_dot = our_dot(&size, neu1, &ONE, &syn1[row2], &ONE)
if f_dot <= -MAX_EXP or f_dot >= MAX_EXP:
continue
Expand All @@ -342,7 +346,7 @@ cdef void w2v_fast_sentence_cbow_hs(
if m == i:
continue
else:
our_saxpy(&size, &words_lockf[indexes[m] % lockf_len], work, &ONE, &syn0[indexes[m] * size], &ONE)
our_saxpy(&size, &words_lockf[indexes[m] % lockf_len], work, &ONE, &syn0[_mul(indexes[m],size)], &ONE)


cdef unsigned long long w2v_fast_sentence_cbow_neg(
Expand Down Expand Up @@ -416,7 +420,7 @@ cdef unsigned long long w2v_fast_sentence_cbow_neg(
continue
else:
count += ONEF
our_saxpy(&size, &ONEF, &syn0[indexes[m] * size], &ONE, neu1, &ONE)
our_saxpy(&size, &ONEF, &syn0[_mul(indexes[m], size)], &ONE, neu1, &ONE)
if count > (<REAL_t>0.5):
inv_count = ONEF/count
if cbow_mean:
Expand All @@ -435,7 +439,7 @@ cdef unsigned long long w2v_fast_sentence_cbow_neg(
continue
label = <REAL_t>0.0

row2 = target_index * size
row2 = _mul(target_index, size)
f_dot = our_dot(&size, neu1, &ONE, &syn1neg[row2], &ONE)
if f_dot <= -MAX_EXP or f_dot >= MAX_EXP:
continue
Expand All @@ -459,7 +463,7 @@ cdef unsigned long long w2v_fast_sentence_cbow_neg(
if m == i:
continue
else:
our_saxpy(&size, &words_lockf[indexes[m] % lockf_len], work, &ONE, &syn0[indexes[m]*size], &ONE)
our_saxpy(&size, &words_lockf[indexes[m] % lockf_len], work, &ONE, &syn0[_mul(indexes[m], size)], &ONE)

return next_random

Expand Down Expand Up @@ -784,11 +788,11 @@ cdef void score_pair_sg_hs(
const np.uint32_t word2_index, REAL_t *work) nogil:

cdef long long b
cdef long long row1 = word2_index * size, row2, sgn
cdef long long row1 = _mul(word2_index, size), row2, sgn
cdef REAL_t f

for b in range(codelen):
row2 = word_point[b] * size
row2 = _mul(word_point[b], size)
f = our_dot(&size, &syn0[row1], &ONE, &syn1[row2], &ONE)
sgn = (-1)**word_code[b] # ch function: 0-> 1, 1 -> -1
f *= sgn
Expand Down Expand Up @@ -889,14 +893,14 @@ cdef void score_pair_cbow_hs(
continue
else:
count += ONEF
our_saxpy(&size, &ONEF, &syn0[indexes[m] * size], &ONE, neu1, &ONE)
our_saxpy(&size, &ONEF, &syn0[_mul(indexes[m], size)], &ONE, neu1, &ONE)
if count > (<REAL_t>0.5):
inv_count = ONEF/count
if cbow_mean:
sscal(&size, &inv_count, neu1, &ONE)

for b in range(codelens[i]):
row2 = word_point[b] * size
row2 = _mul(word_point[b], size)
f = our_dot(&size, neu1, &ONE, &syn1[row2], &ONE)
sgn = (-1)**word_code[b] # ch function: 0-> 1, 1 -> -1
f *= sgn
Expand Down

0 comments on commit ec95fe4

Please sign in to comment.