Skip to content

Commit

Permalink
replace _mul function with explicit casts (#3143)
Browse files Browse the repository at this point in the history
* replace _mul function with explicit casts

* git rm 16
  • Loading branch information
mpenkov authored Jun 1, 2021
1 parent 383fc48 commit f3f6d2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Empty file removed 16
Empty file.
32 changes: 14 additions & 18 deletions gensim/models/word2vec_inner.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ 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 @@ -116,12 +112,12 @@ cdef void w2v_fast_sentence_sg_hs(
"""

cdef long long a, b
cdef long long row1 = _mul(word2_index, size), row2, sgn
cdef long long row1 = <long long>word2_index * <long long>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 = _mul(word_point[b], size)
row2 = <long long>word_point[b] * <long long>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 @@ -210,7 +206,7 @@ cdef unsigned long long w2v_fast_sentence_sg_neg(
"""
cdef long long a
cdef long long row1 = _mul(word2_index, size), row2
cdef long long row1 = <long long>word2_index * <long long>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 @@ -229,7 +225,7 @@ cdef unsigned long long w2v_fast_sentence_sg_neg(
continue
label = <REAL_t>0.0

row2 = _mul(target_index, size)
row2 = <long long>target_index * <long long>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 @@ -313,15 +309,15 @@ cdef void w2v_fast_sentence_cbow_hs(
continue
else:
count += ONEF
our_saxpy(&size, &ONEF, &syn0[_mul(indexes[m], size)], &ONE, neu1, &ONE)
our_saxpy(&size, &ONEF, &syn0[<long long>indexes[m] * <long long>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 = _mul(word_point[b], size)
row2 = <long long>word_point[b] * <long long>size
f_dot = our_dot(&size, neu1, &ONE, &syn1[row2], &ONE)
if f_dot <= -MAX_EXP or f_dot >= MAX_EXP:
continue
Expand All @@ -346,7 +342,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[_mul(indexes[m],size)], &ONE)
our_saxpy(&size, &words_lockf[indexes[m] % lockf_len], work, &ONE, &syn0[<long long>indexes[m] * <long long>size], &ONE)


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

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

return next_random

Expand Down Expand Up @@ -788,11 +784,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 = _mul(word2_index, size), row2, sgn
cdef long long row1 = <long long>word2_index * <long long>size, row2, sgn
cdef REAL_t f

for b in range(codelen):
row2 = _mul(word_point[b], size)
row2 = <long long>word_point[b] * <long long>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 @@ -893,14 +889,14 @@ cdef void score_pair_cbow_hs(
continue
else:
count += ONEF
our_saxpy(&size, &ONEF, &syn0[_mul(indexes[m], size)], &ONE, neu1, &ONE)
our_saxpy(&size, &ONEF, &syn0[<long long>indexes[m] * <long long>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 = _mul(word_point[b], size)
row2 = <long long>word_point[b] * <long long>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 f3f6d2d

Please sign in to comment.