From f3f6d2dae351094f9cdcd939c13e242d1888ac8e Mon Sep 17 00:00:00 2001 From: Michael Penkov Date: Tue, 1 Jun 2021 10:45:07 +0900 Subject: [PATCH] replace _mul function with explicit casts (#3143) * replace _mul function with explicit casts * git rm 16 --- 16 | 0 gensim/models/word2vec_inner.pyx | 32 ++++++++++++++------------------ 2 files changed, 14 insertions(+), 18 deletions(-) delete mode 100644 16 diff --git a/16 b/16 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/gensim/models/word2vec_inner.pyx b/gensim/models/word2vec_inner.pyx index 9dc47fb040..81d140e131 100755 --- a/gensim/models/word2vec_inner.pyx +++ b/gensim/models/word2vec_inner.pyx @@ -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 a * 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, @@ -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 = 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 = _mul(word_point[b], size) + row2 = 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 @@ -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 = 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 @@ -229,7 +225,7 @@ cdef unsigned long long w2v_fast_sentence_sg_neg( continue label = 0.0 - row2 = _mul(target_index, size) + row2 = 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 @@ -313,7 +309,7 @@ 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[indexes[m] * size], &ONE, neu1, &ONE) if count > (0.5): inv_count = ONEF/count if cbow_mean: @@ -321,7 +317,7 @@ cdef void w2v_fast_sentence_cbow_hs( memset(work, 0, size * cython.sizeof(REAL_t)) for b in range(codelens[i]): - row2 = _mul(word_point[b], size) + row2 = 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 @@ -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[indexes[m] * size], &ONE) cdef unsigned long long w2v_fast_sentence_cbow_neg( @@ -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[indexes[m] * size], &ONE, neu1, &ONE) if count > (0.5): inv_count = ONEF/count if cbow_mean: @@ -439,7 +435,7 @@ cdef unsigned long long w2v_fast_sentence_cbow_neg( continue label = 0.0 - row2 = _mul(target_index, size) + row2 = target_index * size f_dot = our_dot(&size, neu1, &ONE, &syn1neg[row2], &ONE) if f_dot <= -MAX_EXP or f_dot >= MAX_EXP: continue @@ -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[indexes[m] * size], &ONE) return next_random @@ -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 = word2_index * size, row2, sgn cdef REAL_t f for b in range(codelen): - row2 = _mul(word_point[b], size) + row2 = 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 @@ -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[indexes[m] * size], &ONE, neu1, &ONE) if count > (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 = 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