Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove linalg backend as per #13701 #13702

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions spacy/ml/parser_model.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ from libc.math cimport exp
from libc.stdlib cimport calloc, free, realloc
from libc.string cimport memcpy, memset
from thinc.backends.cblas cimport saxpy, sgemm
from thinc.backends.linalg cimport Vec, VecVec

import numpy
import numpy.random
Expand Down Expand Up @@ -116,14 +115,10 @@ cdef void predict_states(
n.hiddens * n.pieces
)
for i in range(n.states):
VecVec.add_i(
&A.unmaxed[i*n.hiddens*n.pieces],
W.feat_bias, 1.,
n.hiddens * n.pieces
)
saxpy(cblas)(n.hiddens * n.pieces, 1., W.feat_bias, 1, &A.unmaxed[i*n.hiddens*n.pieces], 1)
for j in range(n.hiddens):
index = i * n.hiddens * n.pieces + j * n.pieces
which = Vec.arg_max(&A.unmaxed[index], n.pieces)
which = _arg_max(&A.unmaxed[index], n.pieces)
A.hiddens[i*n.hiddens + j] = A.unmaxed[index + which]
memset(A.scores, 0, n.states * n.classes * sizeof(float))
if W.hidden_weights == NULL:
Expand All @@ -138,7 +133,7 @@ cdef void predict_states(
)
# Add bias
for i in range(n.states):
VecVec.add_i(&A.scores[i*n.classes], W.hidden_bias, 1., n.classes)
saxpy(cblas)(n.classes, 1., W.hidden_bias, 1, &A.scores[i*n.classes], 1)
# Set unseen classes to minimum value
i = 0
min_ = A.scores[0]
Expand Down Expand Up @@ -187,7 +182,7 @@ cdef void cpu_log_loss(
"""Do multi-label log loss"""
cdef double max_, gmax, Z, gZ
best = arg_max_if_gold(scores, costs, is_valid, O)
guess = Vec.arg_max(scores, O)
guess = _arg_max(scores, O)
if best == -1 or guess == -1:
# These shouldn't happen, but if they do, we want to make sure we don't
# cause an OOB access.
Expand Down Expand Up @@ -232,6 +227,17 @@ cdef int arg_max_if_valid(const weight_t* scores, const int* is_valid, int n) no
best = i
return best

cdef int _arg_max(const float* scores, const int n_classes) nogil:
cdef int i
cdef int best = 0
cdef float mode = scores[0]

for i in range(1, n_classes):
if scores[i] > mode:
mode = scores[i]
best = i

return best

class ParserStepModel(Model):
def __init__(
Expand Down Expand Up @@ -531,3 +537,4 @@ cdef class precompute_hiddens:
return d_best.reshape((d_best.shape + (1,)))

return state_vector, backprop_relu