Free pointers in parser activations (#4486)

* Free pointers in ActivationsC

* Restructure alloc/free for parser activations

* Rewrite/restructure to have allocation and free in parallel functions
in `_parser_model` rather than partially in `_parseC()` in `Parser`.

* Remove `resize_activations` from `_parser_model.pxd`.
This commit is contained in:
adrianeboyd 2019-10-22 15:06:44 +02:00 committed by Matthew Honnibal
parent 388ea03065
commit 3dfc764577
3 changed files with 22 additions and 4 deletions

View File

@ -36,7 +36,9 @@ cdef WeightsC get_c_weights(model) except *
cdef SizesC get_c_sizes(model, int batch_size) except * cdef SizesC get_c_sizes(model, int batch_size) except *
cdef void resize_activations(ActivationsC* A, SizesC n) nogil cdef ActivationsC alloc_activations(SizesC n) nogil
cdef void free_activations(const ActivationsC* A) nogil
cdef void predict_states(ActivationsC* A, StateC** states, cdef void predict_states(ActivationsC* A, StateC** states,
const WeightsC* W, SizesC n) nogil const WeightsC* W, SizesC n) nogil

View File

@ -62,6 +62,21 @@ cdef SizesC get_c_sizes(model, int batch_size) except *:
return output return output
cdef ActivationsC alloc_activations(SizesC n) nogil:
cdef ActivationsC A
memset(&A, 0, sizeof(A))
resize_activations(&A, n)
return A
cdef void free_activations(const ActivationsC* A) nogil:
free(A.token_ids)
free(A.scores)
free(A.unmaxed)
free(A.hiddens)
free(A.is_valid)
cdef void resize_activations(ActivationsC* A, SizesC n) nogil: cdef void resize_activations(ActivationsC* A, SizesC n) nogil:
if n.states <= A._max_size: if n.states <= A._max_size:
A._curr_size = n.states A._curr_size = n.states

View File

@ -27,7 +27,8 @@ from thinc.neural.util import get_array_module
from thinc.linalg cimport Vec, VecVec from thinc.linalg cimport Vec, VecVec
import srsly import srsly
from ._parser_model cimport resize_activations, predict_states, arg_max_if_valid from ._parser_model cimport alloc_activations, free_activations
from ._parser_model cimport predict_states, arg_max_if_valid
from ._parser_model cimport WeightsC, ActivationsC, SizesC, cpu_log_loss from ._parser_model cimport WeightsC, ActivationsC, SizesC, cpu_log_loss
from ._parser_model cimport get_c_weights, get_c_sizes from ._parser_model cimport get_c_weights, get_c_sizes
from ._parser_model import ParserModel from ._parser_model import ParserModel
@ -312,8 +313,7 @@ cdef class Parser:
WeightsC weights, SizesC sizes) nogil: WeightsC weights, SizesC sizes) nogil:
cdef int i, j cdef int i, j
cdef vector[StateC*] unfinished cdef vector[StateC*] unfinished
cdef ActivationsC activations cdef ActivationsC activations = alloc_activations(sizes)
memset(&activations, 0, sizeof(activations))
while sizes.states >= 1: while sizes.states >= 1:
predict_states(&activations, predict_states(&activations,
states, &weights, sizes) states, &weights, sizes)
@ -327,6 +327,7 @@ cdef class Parser:
states[i] = unfinished[i] states[i] = unfinished[i]
sizes.states = unfinished.size() sizes.states = unfinished.size()
unfinished.clear() unfinished.clear()
free_activations(&activations)
def set_annotations(self, docs, states_or_beams, tensors=None): def set_annotations(self, docs, states_or_beams, tensors=None):
cdef StateClass state cdef StateClass state