-
Notifications
You must be signed in to change notification settings - Fork 31
/
cython_utils.pyx
472 lines (390 loc) · 14.7 KB
/
cython_utils.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Author: Mathurin Massias <mathurin.massias@gmail.com>
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
# Joseph Salmon <joseph.salmon@telecom-paristech.fr>
# License: BSD 3 clause
cimport cython
cimport numpy as np
from scipy.linalg.cython_blas cimport ddot, dasum, daxpy, dnrm2, dcopy, dscal
from scipy.linalg.cython_blas cimport sdot, sasum, saxpy, snrm2, scopy, sscal
from scipy.linalg.cython_lapack cimport sposv, dposv
from libc.math cimport fabs, log, exp, sqrt
from numpy.math cimport INFINITY
from cython cimport floating
cdef:
int LASSO = 0
int LOGREG = 1
int GRPLASSO = 2
int inc = 1
cdef floating fdot(int * n, floating * x, int * inc1, floating * y,
int * inc2) nogil:
if floating is double:
return ddot(n, x, inc1, y, inc2)
else:
return sdot(n, x, inc1, y, inc2)
cdef floating fasum(int * n, floating * x, int * inc) nogil:
if floating is double:
return dasum(n, x, inc)
else:
return sasum(n, x, inc)
cdef void faxpy(int * n, floating * alpha, floating * x, int * incx,
floating * y, int * incy) nogil:
if floating is double:
daxpy(n, alpha, x, incx, y, incy)
else:
saxpy(n, alpha, x, incx, y, incy)
cdef floating fnrm2(int * n, floating * x, int * inc) nogil:
if floating is double:
return dnrm2(n, x, inc)
else:
return snrm2(n, x, inc)
cdef void fcopy(int * n, floating * x, int * incx, floating * y,
int * incy) nogil:
if floating is double:
dcopy(n, x, incx, y, incy)
else:
scopy(n, x, incx, y, incy)
cdef void fscal(int * n, floating * alpha, floating * x,
int * incx) nogil:
if floating is double:
dscal(n, alpha, x, incx)
else:
sscal(n, alpha, x, incx)
cdef void fposv(char * uplo, int * n, int * nrhs, floating * a,
int * lda, floating * b, int * ldb, int * info) nogil:
if floating is double:
dposv(uplo, n, nrhs, a, lda, b, ldb, info)
else:
sposv(uplo, n, nrhs, a, lda, b, ldb, info)
cdef inline floating ST(floating x, floating u) nogil:
if x > u:
return x - u
elif x < - u:
return x + u
else:
return 0
cdef floating log_1pexp(floating x) nogil:
"""Compute log(1. + exp(x)) while avoiding over/underflow."""
if x < - 18:
return exp(x)
elif x > 18:
return x
else:
return log(1. + exp(x))
cdef inline floating xlogx(floating x) nogil:
if x < 1e-10:
return 0.
else:
return x * log(x)
cdef inline floating Nh(floating x) nogil:
"""Negative entropy of scalar x."""
if 0. <= x <= 1.:
return xlogx(x) + xlogx(1. - x)
else:
return INFINITY # not - INFINITY
@cython.boundscheck(False)
@cython.wraparound(False)
cdef floating fweighted_norm_w2(floating[:] w, floating[:] weights) nogil:
cdef floating weighted_norm = 0.
cdef int n_features = w.shape[0]
cdef int j
for j in range(n_features):
if weights[j] == INFINITY:
continue
weighted_norm += weights[j] * w[j] ** 2
return weighted_norm
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef inline floating sigmoid(floating x) nogil:
return 1. / (1. + exp(- x))
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef floating primal_logreg(
floating alpha, floating[:] Xw, floating[:] y, floating[:] w,
floating[:] weights) nogil:
cdef int inc = 1
cdef int n_samples = Xw.shape[0]
cdef int n_features = w.shape[0]
cdef floating p_obj = 0.
cdef int i, j
for i in range(n_samples):
p_obj += log_1pexp(- y[i] * Xw[i])
for j in range(n_features):
# avoid nan when weights[j] is INFINITY
if w[j]:
p_obj += alpha * weights[j] * fabs(w[j])
return p_obj
# todo check normalization by 1 / n_samples everywhere
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef floating primal_lasso(
floating alpha, floating l1_ratio, floating[:] R, floating[:] w,
floating[:] weights) nogil:
cdef int n_samples = R.shape[0]
cdef int n_features = w.shape[0]
cdef int inc = 1
cdef int j
cdef floating p_obj = 0.
p_obj = fdot(&n_samples, &R[0], &inc, &R[0], &inc) / (2. * n_samples)
for j in range(n_features):
# avoid nan when weights[j] is INFINITY
if w[j]:
p_obj += alpha * weights[j] * (
l1_ratio * fabs(w[j]) +
0.5 * (1. - l1_ratio) * w[j] ** 2)
return p_obj
cdef floating primal(
int pb, floating alpha, floating l1_ratio, floating[:] R, floating[:] y,
floating[:] w, floating[:] weights) nogil:
if pb == LASSO:
return primal_lasso(alpha, l1_ratio, R, w, weights)
else:
return primal_logreg(alpha, R, y, w, weights)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef floating dual_enet(int n_samples, floating alpha, floating l1_ratio,
floating norm_y2, floating norm_w2, floating * theta,
floating * y) nogil:
"""Theta must be feasible"""
cdef int i
cdef floating d_obj = 0.
for i in range(n_samples):
d_obj -= (y[i] - n_samples * theta[i]) ** 2
d_obj *= 0.5 / n_samples
d_obj += norm_y2 / (2. * n_samples)
if l1_ratio != 1.0:
d_obj -= 0.5 * alpha * (1 - l1_ratio) * norm_w2
return d_obj
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef floating dual_logreg(int n_samples, floating * theta,
floating * y) nogil:
"""Compute dual objective value at theta, which must be feasible."""
cdef int i
cdef floating d_obj = 0.
for i in range(n_samples):
d_obj -= Nh(y[i] * theta[i])
return d_obj
cdef floating dual(int pb, int n_samples, floating alpha, floating l1_ratio,
floating norm_y2, floating norm_w2, floating * theta, floating * y) nogil:
if pb == LASSO:
return dual_enet(n_samples, alpha, l1_ratio, norm_y2, norm_w2, &theta[0], &y[0])
else:
return dual_logreg(n_samples, &theta[0], &y[0])
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef void create_dual_pt(
int pb, int n_samples, floating * out,
floating * R, floating * y) nogil:
cdef floating tmp = 1.
if pb == LASSO: # out = R / n_samples
tmp = 1. / n_samples
fcopy(&n_samples, &R[0], &inc, &out[0], &inc)
else: # out = y * sigmoid(-y * Xw)
for i in range(n_samples):
out[i] = y[i] * sigmoid(-y[i] * R[i])
fscal(&n_samples, &tmp, &out[0], &inc)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef int create_accel_pt(
int pb, int n_samples, int epoch, int gap_freq,
floating * R, floating * out, floating * last_K_R, floating[:, :] U,
floating[:, :] UtU, floating[:] onesK, floating[:] y):
# solving linear system in cython
# doc at https://software.intel.com/en-us/node/468894
# cdef int n_samples = y.shape[0] cannot use this for MTL
cdef int K = U.shape[0] + 1
cdef char * char_U = 'U'
cdef int one = 1
cdef int Kminus1 = K - 1
cdef int inc = 1
cdef floating sum_z
cdef int info_dposv
cdef int i, j, k
# warning: this is wrong (n_samples) for MTL, it is handled outside
cdef floating tmp = 1. if pb == LOGREG else 1. / n_samples
if epoch // gap_freq < K:
# last_K_R[it // f_gap] = R:
fcopy(&n_samples, R, &inc,
&last_K_R[(epoch // gap_freq) * n_samples], &inc)
else:
for k in range(K - 1):
fcopy(&n_samples, &last_K_R[(k + 1) * n_samples], &inc,
&last_K_R[k * n_samples], &inc)
fcopy(&n_samples, R, &inc, &last_K_R[(K - 1) * n_samples], &inc)
for k in range(K - 1):
for i in range(n_samples):
U[k, i] = last_K_R[(k + 1) * n_samples + i] - \
last_K_R[k * n_samples + i]
for k in range(K - 1):
for j in range(k, K - 1):
UtU[k, j] = fdot(&n_samples, &U[k, 0], &inc, &U[j, 0], &inc)
UtU[j, k] = UtU[k, j]
# refill onesK with ones because it has been overwritten
# by dposv
for k in range(K - 1):
onesK[k] = 1.
fposv(char_U, &Kminus1, &one, &UtU[0, 0], &Kminus1,
&onesK[0], &Kminus1, &info_dposv)
# onesK now holds the solution in x to UtU dot x = onesK
if info_dposv != 0:
# don't use accel for this iteration
for k in range(K - 2):
onesK[k] = 0
onesK[K - 2] = 1
sum_z = 0.
for k in range(K - 1):
sum_z += onesK[k]
for k in range(K - 1):
onesK[k] /= sum_z
for i in range(n_samples):
out[i] = 0.
for k in range(K - 1):
for i in range(n_samples):
out[i] += onesK[k] * last_K_R[k * n_samples + i]
if pb == LOGREG:
for i in range(n_samples):
out[i] = y[i] * sigmoid(- y[i] * out[i])
fscal(&n_samples, &tmp, &out[0], &inc)
# out now holds the extrapolated dual point:
# LASSO: (y - Xw) / n_samples
# LOGREG: y * sigmoid(-y * Xw)
return info_dposv
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef void compute_norms_X_col(
bint is_sparse, floating[:] norms_X_col, int n_samples,
floating[::1, :] X, floating[:] X_data, int[:] X_indices,
int[:] X_indptr, floating[:] X_mean):
cdef int j, startptr, endptr
cdef floating tmp, X_mean_j
cdef int n_features = norms_X_col.shape[0]
for j in range(n_features):
if is_sparse:
startptr = X_indptr[j]
endptr = X_indptr[j + 1]
X_mean_j = X_mean[j]
tmp = 0.
for i in range(startptr, endptr):
tmp += (X_data[i] - X_mean_j) ** 2
tmp += (n_samples - endptr + startptr) * X_mean_j ** 2
norms_X_col[j] = sqrt(tmp)
else:
norms_X_col[j] = fnrm2(&n_samples, &X[0, j], &inc)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef void compute_Xw(
bint is_sparse, int pb, floating[:] R, floating[:] w,
floating[:] y, bint center, floating[::1, :] X, floating[:] X_data,
int[:] X_indices, int[:] X_indptr, floating[:] X_mean):
# R holds residuals if LASSO, Xw for LOGREG
cdef int i, j, startptr, endptr
cdef floating tmp, X_mean_j
cdef int inc = 1
cdef int n_samples = y.shape[0]
cdef int n_features = w.shape[0]
for j in range(n_features):
if w[j] != 0:
if is_sparse:
startptr, endptr = X_indptr[j], X_indptr[j + 1]
for i in range(startptr, endptr):
R[X_indices[i]] += w[j] * X_data[i]
if center:
X_mean_j = X_mean[j]
for i in range(n_samples):
R[i] -= X_mean_j * w[j]
else:
tmp = w[j]
faxpy(&n_samples, &tmp, &X[0, j], &inc, &R[0], &inc)
# currently R = X @ w, update for LASSO/GRPLASSO:
if pb in (LASSO, GRPLASSO):
for i in range(n_samples):
R[i] = y[i] - R[i]
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef floating dnorm_enet(
bint is_sparse, floating[:] theta, floating[:] w, floating[::1, :] X,
floating[:] X_data, int[:] X_indices, int[:] X_indptr, int[:] skip,
floating[:] X_mean, floating[:] weights, bint center,
bint positive, floating alpha, floating l1_ratio) nogil:
"""compute norm(X[:, ~skip].T.dot(theta), ord=inf)"""
cdef int n_samples = theta.shape[0]
cdef int n_features = skip.shape[0]
cdef floating Xj_theta
cdef floating scal = 0.
cdef floating theta_sum = 0.
cdef int i, j, Cj, startptr, endptr
if is_sparse:
# TODO by design theta_sum should always be 0 when center
if center:
for i in range(n_samples):
theta_sum += theta[i]
# max over feature for which skip[j] == False
for j in range(n_features):
if skip[j] or weights[j] == INFINITY:
continue
if is_sparse:
startptr = X_indptr[j]
endptr = X_indptr[j + 1]
Xj_theta = 0.
for i in range(startptr, endptr):
Xj_theta += X_data[i] * theta[X_indices[i]]
if center:
Xj_theta -= theta_sum * X_mean[j]
else:
Xj_theta = fdot(&n_samples, &theta[0], &inc, &X[0, j], &inc)
# minus sign to consider the choice theta = y - Xw and not theta = Xw -y
if l1_ratio != 1:
Xj_theta -= alpha * (1 - l1_ratio) * weights[j] * w[j]
if not positive:
Xj_theta = fabs(Xj_theta)
scal = max(scal, Xj_theta / weights[j])
return scal
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef void set_prios(
bint is_sparse, floating[:] theta, floating[:] w, floating alpha, floating l1_ratio,
floating[::1, :] X, floating[:] X_data, int[:] X_indices, int[:] X_indptr,
floating[:] norms_X_col, floating[:] weights, floating[:] prios,
int[:] screened, floating radius, int * n_screened, bint positive) nogil:
cdef int i, j, startptr, endptr
cdef floating Xj_theta
cdef int n_samples = theta.shape[0]
cdef int n_features = prios.shape[0]
cdef floating norms_X_col_j = 0.
# TODO we do not substract theta_sum, which seems to indicate that theta
# is always centered...
for j in range(n_features):
if screened[j] or norms_X_col[j] == 0. or weights[j] == 0.:
prios[j] = INFINITY
continue
if is_sparse:
Xj_theta = 0
startptr = X_indptr[j]
endptr = X_indptr[j + 1]
for i in range(startptr, endptr):
Xj_theta += theta[X_indices[i]] * X_data[i]
else:
Xj_theta = fdot(&n_samples, &theta[0], &inc, &X[0, j], &inc)
norms_X_col_j = norms_X_col[j]
if l1_ratio != 1:
Xj_theta -= alpha * (1 - l1_ratio) * weights[j] * w[j]
norms_X_col_j = norms_X_col_j ** 2
norms_X_col_j += sqrt(norms_X_col_j + alpha * (1 - l1_ratio) * weights[j])
if positive:
prios[j] = fabs(Xj_theta - alpha * l1_ratio * weights[j]) / norms_X_col_j
else:
prios[j] = (alpha * l1_ratio * weights[j] - fabs(Xj_theta)) / norms_X_col_j
if prios[j] > radius:
screened[j] = True
n_screened[0] += 1