/
word2vec_inner.pyx
executable file
·214 lines (178 loc) · 7.5 KB
/
word2vec_inner.pyx
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
#!/usr/bin/env cython
# cython: boundscheck=False
# cython: wraparound=False
# cython: cdivision=True
# coding: utf-8
#
# Copyright (C) 2013 Radim Rehurek <me@radimrehurek.com>
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html
#define IS_PY3K
import cython
import numpy as np
cimport numpy as np
from libc.math cimport exp
from libc.string cimport memset
from cpython cimport PyCapsule_GetPointer
from scipy.linalg.blas import fblas
REAL = np.float32
ctypedef np.float32_t REAL_t
ctypedef void (*scopy_ptr) (const int *N, const float *X, const int *incX, float *Y, const int *incY) nogil
ctypedef void (*saxpy_ptr) (const int *N, const float *alpha, const float *X, const int *incX, float *Y, const int *incY) nogil
ctypedef float (*sdot_ptr) (const int *N, const float *X, const int *incX, const float *Y, const int *incY) nogil
ctypedef double (*dsdot_ptr) (const int *N, const float *X, const int *incX, const float *Y, const int *incY) nogil
ctypedef double (*snrm2_ptr) (const int *N, const float *X, const int *incX) nogil
ctypedef void (*fast_sentence_ptr) (
const np.uint32_t *word_point, const np.uint8_t *word_code, const int codelen,
REAL_t *syn0, REAL_t *syn1, const int size,
const np.uint32_t word2_index, const REAL_t alpha, REAL_t *work) nogil
cdef scopy_ptr scopy=<scopy_ptr>PyCapsule_GetPointer(fblas.scopy._cpointer, NULL) # y = x
cdef saxpy_ptr saxpy=<saxpy_ptr>PyCapsule_GetPointer(fblas.saxpy._cpointer, NULL) # y += alpha * x
cdef sdot_ptr sdot=<sdot_ptr>PyCapsule_GetPointer(fblas.sdot._cpointer, NULL) # float = dot(x, y)
cdef dsdot_ptr dsdot=<dsdot_ptr>PyCapsule_GetPointer(fblas.sdot._cpointer, NULL) # double = dot(x, y)
cdef snrm2_ptr snrm2=<snrm2_ptr>PyCapsule_GetPointer(fblas.snrm2._cpointer, NULL) # sqrt(x^2)
cdef fast_sentence_ptr fast_sentence
DEF EXP_TABLE_SIZE = 1000
DEF MAX_EXP = 6
cdef REAL_t[EXP_TABLE_SIZE] EXP_TABLE
cdef int ONE = 1
cdef REAL_t ONEF = <REAL_t>1.0
cdef void fast_sentence0(
const np.uint32_t *word_point, const np.uint8_t *word_code, const int codelen,
REAL_t *syn0, REAL_t *syn1, const int size,
const np.uint32_t word2_index, const REAL_t alpha, REAL_t *work) nogil:
cdef long long a, b
cdef long long row1 = word2_index * size, row2
cdef REAL_t f, g
memset(work, 0, size * cython.sizeof(REAL_t))
for b in range(codelen):
row2 = word_point[b] * size
f = <REAL_t>dsdot(&size, &syn0[row1], &ONE, &syn1[row2], &ONE)
if f <= -MAX_EXP or f >= MAX_EXP:
continue
f = EXP_TABLE[<int>((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]
g = (1 - word_code[b] - f) * alpha
saxpy(&size, &g, &syn1[row2], &ONE, work, &ONE)
saxpy(&size, &g, &syn0[row1], &ONE, &syn1[row2], &ONE)
saxpy(&size, &ONEF, work, &ONE, &syn0[row1], &ONE)
cdef void fast_sentence1(
const np.uint32_t *word_point, const np.uint8_t *word_code, const int codelen,
REAL_t *syn0, REAL_t *syn1, const int size,
const np.uint32_t word2_index, const REAL_t alpha, REAL_t *work) nogil:
cdef long long a, b
cdef long long row1 = word2_index * size, row2
cdef REAL_t f, g
memset(work, 0, size * cython.sizeof(REAL_t))
for b in range(codelen):
row2 = word_point[b] * size
f = <REAL_t>sdot(&size, &syn0[row1], &ONE, &syn1[row2], &ONE)
if f <= -MAX_EXP or f >= MAX_EXP:
continue
f = EXP_TABLE[<int>((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]
g = (1 - word_code[b] - f) * alpha
saxpy(&size, &g, &syn1[row2], &ONE, work, &ONE)
saxpy(&size, &g, &syn0[row1], &ONE, &syn1[row2], &ONE)
saxpy(&size, &ONEF, work, &ONE, &syn0[row1], &ONE)
cdef void fast_sentence2(
const np.uint32_t *word_point, const np.uint8_t *word_code, const int codelen,
REAL_t *syn0, REAL_t *syn1, const int size,
const np.uint32_t word2_index, const REAL_t alpha, REAL_t *work) nogil:
cdef long long a, b
cdef long long row1 = word2_index * size, row2
cdef REAL_t f, g
for a in range(size):
work[a] = <REAL_t>0.0
for b in range(codelen):
row2 = word_point[b] * size
f = <REAL_t>0.0
for a in range(size):
f += syn0[row1 + a] * syn1[row2 + a]
if f <= -MAX_EXP or f >= MAX_EXP:
continue
f = EXP_TABLE[<int>((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]
g = (1 - word_code[b] - f) * alpha
for a in range(size):
work[a] += g * syn1[row2 + a]
for a in range(size):
syn1[row2 + a] += g * syn0[row1 + a]
for a in range(size):
syn0[row1 + a] += work[a]
DEF MAX_SENTENCE_LEN = 1000
def train_sentence(model, sentence, alpha, _work):
cdef REAL_t *syn0 = <REAL_t *>(np.PyArray_DATA(model.syn0))
cdef REAL_t *syn1 = <REAL_t *>(np.PyArray_DATA(model.syn1))
cdef REAL_t *work
cdef np.uint32_t word2_index
cdef REAL_t _alpha = alpha
cdef int size = model.layer1_size
cdef np.uint32_t *points[MAX_SENTENCE_LEN]
cdef np.uint8_t *codes[MAX_SENTENCE_LEN]
cdef int codelens[MAX_SENTENCE_LEN]
cdef np.uint32_t indexes[MAX_SENTENCE_LEN]
cdef np.uint32_t reduced_windows[MAX_SENTENCE_LEN]
cdef int sentence_len
cdef int window = model.window
cdef int i, j, k
cdef long result = 0
# convert Python structures to primitive types, so we can release the GIL
work = <REAL_t *>np.PyArray_DATA(_work)
sentence_len = <int>min(MAX_SENTENCE_LEN, len(sentence))
for i in range(sentence_len):
word = sentence[i]
if word is None:
codelens[i] = 0
else:
indexes[i] = word.index
codelens[i] = <int>len(word.code)
codes[i] = <np.uint8_t *>np.PyArray_DATA(word.code)
points[i] = <np.uint32_t *>np.PyArray_DATA(word.point)
reduced_windows[i] = np.random.randint(window)
result += 1
# release GIL & train on the sentence
with nogil:
for i in range(sentence_len):
if codelens[i] == 0:
continue
j = i - window + reduced_windows[i]
if j < 0:
j = 0
k = i + window + 1 - reduced_windows[i]
if k > sentence_len:
k = sentence_len
for j in range(j, k):
if j == i or codelens[j] == 0:
continue
fast_sentence(points[i], codes[i], codelens[i], syn0, syn1, size, indexes[j], _alpha, work)
return result
def init():
"""
Precompute function `sigmoid(x) = 1 / (1 + exp(-x))`, for x values discretized
into table EXP_TABLE.
"""
global fast_sentence
cdef int i
cdef float *x = [<float>10.0]
cdef float *y = [<float>0.01]
cdef float expected = <float>0.1
cdef int size = 1
cdef double d_res
cdef float *p_res
# build the sigmoid table
for i in range(EXP_TABLE_SIZE):
EXP_TABLE[i] = <REAL_t>exp((i / <REAL_t>EXP_TABLE_SIZE * 2 - 1) * MAX_EXP)
EXP_TABLE[i] = <REAL_t>(EXP_TABLE[i] / (EXP_TABLE[i] + 1))
# check whether sdot returns double or float
d_res = dsdot(&size, x, &ONE, y, &ONE)
p_res = <float *>&d_res
if (abs(d_res - expected) < 0.0001):
fast_sentence = fast_sentence0
return 0 # double
elif (abs(p_res[0] - expected) < 0.0001):
fast_sentence = fast_sentence1
return 1 # float
else:
# neither => use cython loops, no BLAS
# actually, BLAted above and
# never even get here
fast_sentence = fast_sentence2
return 2
FAST_VERSION = init() # initialize the module