2016-10-27 16:08:13 +00:00
|
|
|
# cython: infer_types=True
|
2023-09-12 06:49:41 +00:00
|
|
|
# cython: profile=False
|
2020-06-26 17:34:12 +00:00
|
|
|
from __future__ import print_function
|
2023-06-14 15:48:41 +00:00
|
|
|
|
2015-02-21 15:38:18 +00:00
|
|
|
from cymem.cymem cimport Pool
|
2020-03-02 10:48:10 +00:00
|
|
|
|
2019-12-22 00:53:56 +00:00
|
|
|
from collections import Counter
|
2023-06-14 15:48:41 +00:00
|
|
|
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
import srsly
|
2015-02-21 15:38:18 +00:00
|
|
|
|
2020-07-30 21:30:54 +00:00
|
|
|
from ...structs cimport TokenC
|
2023-06-14 15:48:41 +00:00
|
|
|
from ...typedefs cimport attr_t, weight_t
|
2015-06-09 19:20:14 +00:00
|
|
|
from .stateclass cimport StateClass
|
2020-03-02 10:48:10 +00:00
|
|
|
|
2020-07-30 21:30:54 +00:00
|
|
|
from ... import util
|
2023-06-14 15:48:41 +00:00
|
|
|
from ...errors import Errors
|
2015-06-09 19:20:14 +00:00
|
|
|
|
2015-02-21 15:38:18 +00:00
|
|
|
|
|
|
|
cdef weight_t MIN_SCORE = -90000
|
|
|
|
|
|
|
|
|
2015-02-22 05:32:07 +00:00
|
|
|
class OracleError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-03-11 17:12:01 +00:00
|
|
|
cdef void* _init_state(Pool mem, int length, void* tokens) except NULL:
|
2017-11-14 01:11:40 +00:00
|
|
|
cdef StateC* st = new StateC(<const TokenC*>tokens, length)
|
2017-03-11 17:12:01 +00:00
|
|
|
return <void*>st
|
|
|
|
|
|
|
|
|
2019-12-10 12:23:27 +00:00
|
|
|
cdef int _del_state(Pool mem, void* state, void* x) except -1:
|
|
|
|
cdef StateC* st = <StateC*>state
|
|
|
|
del st
|
|
|
|
|
|
|
|
|
2015-02-21 15:38:18 +00:00
|
|
|
cdef class TransitionSystem:
|
2021-06-17 07:33:00 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
StringStore string_table,
|
|
|
|
labels_by_action=None,
|
|
|
|
min_freq=None,
|
|
|
|
incorrect_spans_key=None
|
|
|
|
):
|
|
|
|
self.cfg = {"neg_key": incorrect_spans_key}
|
2015-02-21 15:38:18 +00:00
|
|
|
self.mem = Pool()
|
2015-03-14 15:06:35 +00:00
|
|
|
self.strings = string_table
|
2016-01-19 18:09:33 +00:00
|
|
|
self.n_moves = 0
|
|
|
|
self._size = 100
|
2017-03-11 17:12:01 +00:00
|
|
|
|
2016-01-19 18:09:33 +00:00
|
|
|
self.c = <Transition*>self.mem.alloc(self._size, sizeof(Transition))
|
2017-03-11 17:12:01 +00:00
|
|
|
|
2018-03-27 17:23:02 +00:00
|
|
|
self.labels = {}
|
|
|
|
if labels_by_action:
|
|
|
|
self.initialize_actions(labels_by_action, min_freq=min_freq)
|
2017-05-28 16:09:27 +00:00
|
|
|
self.root_label = self.strings.add('ROOT')
|
2020-12-13 01:08:32 +00:00
|
|
|
self.init_beam_state = _init_state
|
|
|
|
self.del_beam_state = _del_state
|
2015-02-21 15:38:18 +00:00
|
|
|
|
2015-10-12 08:33:11 +00:00
|
|
|
def __reduce__(self):
|
2021-06-17 07:33:00 +00:00
|
|
|
# TODO: This loses the 'cfg'
|
2018-03-27 17:23:02 +00:00
|
|
|
return (self.__class__, (self.strings, self.labels), None, None)
|
2015-10-12 08:33:11 +00:00
|
|
|
|
2021-06-17 07:33:00 +00:00
|
|
|
@property
|
|
|
|
def neg_key(self):
|
|
|
|
return self.cfg.get("neg_key")
|
|
|
|
|
2017-05-15 19:46:08 +00:00
|
|
|
def init_batch(self, docs):
|
|
|
|
cdef StateClass state
|
|
|
|
states = []
|
|
|
|
offset = 0
|
|
|
|
for doc in docs:
|
|
|
|
state = StateClass(doc, offset=offset)
|
|
|
|
states.append(state)
|
|
|
|
offset += len(doc)
|
|
|
|
return states
|
2020-07-06 11:06:25 +00:00
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
def get_oracle_sequence(self, Example example, _debug=False):
|
2020-06-30 09:50:48 +00:00
|
|
|
states, golds, _ = self.init_gold_batch([example])
|
|
|
|
if not states:
|
|
|
|
return []
|
|
|
|
state = states[0]
|
|
|
|
gold = golds[0]
|
|
|
|
if _debug:
|
|
|
|
return self.get_oracle_sequence_from_state(state, gold, _debug=example)
|
|
|
|
else:
|
|
|
|
return self.get_oracle_sequence_from_state(state, gold)
|
|
|
|
|
|
|
|
def get_oracle_sequence_from_state(self, StateClass state, gold, _debug=None):
|
2017-05-26 16:31:23 +00:00
|
|
|
cdef Pool mem = Pool()
|
2019-10-22 14:54:33 +00:00
|
|
|
# n_moves should not be zero at this point, but make sure to avoid zero-length mem alloc
|
|
|
|
assert self.n_moves > 0
|
2017-05-26 16:31:23 +00:00
|
|
|
costs = <float*>mem.alloc(self.n_moves, sizeof(float))
|
|
|
|
is_valid = <int*>mem.alloc(self.n_moves, sizeof(int))
|
|
|
|
|
|
|
|
history = []
|
2020-06-26 17:34:12 +00:00
|
|
|
debug_log = []
|
2017-05-26 16:31:23 +00:00
|
|
|
while not state.is_final():
|
2020-12-13 01:08:32 +00:00
|
|
|
self.set_costs(is_valid, costs, state.c, gold)
|
2017-05-26 16:31:23 +00:00
|
|
|
for i in range(self.n_moves):
|
|
|
|
if is_valid[i] and costs[i] <= 0:
|
|
|
|
action = self.c[i]
|
|
|
|
history.append(i)
|
2020-06-26 17:34:12 +00:00
|
|
|
if _debug:
|
2020-06-30 23:03:20 +00:00
|
|
|
s0 = state.S(0)
|
|
|
|
b0 = state.B(0)
|
2020-06-30 09:50:48 +00:00
|
|
|
example = _debug
|
2020-06-26 17:34:12 +00:00
|
|
|
debug_log.append(" ".join((
|
|
|
|
self.get_class_name(i),
|
|
|
|
"S0=", (example.x[s0].text if s0 >= 0 else "__"),
|
|
|
|
"B0=", (example.x[b0].text if b0 >= 0 else "__"),
|
|
|
|
"S0 head?", str(state.has_head(state.S(0))),
|
|
|
|
)))
|
2017-05-26 16:31:23 +00:00
|
|
|
action.do(state.c, action.label)
|
|
|
|
break
|
2017-05-27 20:52:20 +00:00
|
|
|
else:
|
2020-06-26 17:34:12 +00:00
|
|
|
if _debug:
|
2020-06-30 09:50:48 +00:00
|
|
|
example = _debug
|
2020-06-26 17:34:12 +00:00
|
|
|
print("Actions")
|
|
|
|
for i in range(self.n_moves):
|
|
|
|
print(self.get_class_name(i))
|
|
|
|
print("Gold")
|
|
|
|
for token in example.y:
|
|
|
|
print(token.text, token.dep_, token.head.text)
|
|
|
|
s0 = state.S(0)
|
|
|
|
b0 = state.B(0)
|
|
|
|
debug_log.append(" ".join((
|
|
|
|
"?",
|
|
|
|
"S0=", (example.x[s0].text if s0 >= 0 else "-"),
|
|
|
|
"B0=", (example.x[b0].text if b0 >= 0 else "-"),
|
|
|
|
"S0 head?", str(state.has_head(state.S(0))),
|
|
|
|
)))
|
|
|
|
print("\n".join(debug_log))
|
2018-04-03 13:50:31 +00:00
|
|
|
raise ValueError(Errors.E024)
|
2017-05-26 16:31:23 +00:00
|
|
|
return history
|
|
|
|
|
2019-03-10 12:00:00 +00:00
|
|
|
def apply_transition(self, StateClass state, name):
|
|
|
|
if not self.is_valid(state, name):
|
2019-09-21 12:37:06 +00:00
|
|
|
raise ValueError(Errors.E170.format(name=name))
|
2019-03-10 12:00:00 +00:00
|
|
|
action = self.lookup_transition(name)
|
|
|
|
action.do(state.c, action.label)
|
|
|
|
|
2015-03-09 11:06:01 +00:00
|
|
|
cdef Transition lookup_transition(self, object name) except *:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-05-28 12:06:40 +00:00
|
|
|
cdef Transition init_transition(self, int clas, int move, attr_t label) except *:
|
2015-02-21 15:38:18 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-08-08 21:36:18 +00:00
|
|
|
def is_valid(self, StateClass stcls, move_name):
|
|
|
|
action = self.lookup_transition(move_name)
|
2016-02-01 01:58:14 +00:00
|
|
|
return action.is_valid(stcls.c, action.label)
|
2015-08-08 21:36:18 +00:00
|
|
|
|
2016-02-01 02:00:15 +00:00
|
|
|
cdef int set_valid(self, int* is_valid, const StateC* st) nogil:
|
2015-06-04 17:32:32 +00:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.n_moves):
|
2016-02-01 02:00:15 +00:00
|
|
|
is_valid[i] = self.c[i].is_valid(st, self.c[i].label)
|
2015-06-04 17:32:32 +00:00
|
|
|
|
2016-01-30 13:31:12 +00:00
|
|
|
cdef int set_costs(self, int* is_valid, weight_t* costs,
|
2020-12-13 01:08:32 +00:00
|
|
|
const StateC* state, gold) except -1:
|
2020-06-26 17:34:12 +00:00
|
|
|
raise NotImplementedError
|
2017-05-28 16:09:27 +00:00
|
|
|
|
2017-07-20 13:02:55 +00:00
|
|
|
def get_class_name(self, int clas):
|
|
|
|
act = self.c[clas]
|
|
|
|
return self.move_name(act.move, act.label)
|
|
|
|
|
2018-03-27 17:23:02 +00:00
|
|
|
def initialize_actions(self, labels_by_action, min_freq=None):
|
|
|
|
self.labels = {}
|
|
|
|
self.n_moves = 0
|
2019-02-24 15:36:29 +00:00
|
|
|
added_labels = []
|
|
|
|
added_actions = {}
|
2018-03-27 17:23:02 +00:00
|
|
|
for action, label_freqs in sorted(labels_by_action.items()):
|
|
|
|
action = int(action)
|
|
|
|
# Make sure we take a copy here, and that we get a Counter
|
|
|
|
self.labels[action] = Counter()
|
|
|
|
# Have to be careful here: Sorting must be stable, or our model
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
# won't be read back in correctly.
|
2018-03-27 17:23:02 +00:00
|
|
|
sorted_labels = [(f, L) for L, f in label_freqs.items()]
|
|
|
|
sorted_labels.sort()
|
|
|
|
sorted_labels.reverse()
|
|
|
|
for freq, label_str in sorted_labels:
|
2019-02-24 15:36:29 +00:00
|
|
|
if freq < 0:
|
|
|
|
added_labels.append((freq, label_str))
|
|
|
|
added_actions.setdefault(label_str, []).append(action)
|
|
|
|
else:
|
|
|
|
self.add_action(int(action), label_str)
|
|
|
|
self.labels[action][label_str] = freq
|
|
|
|
added_labels.sort(reverse=True)
|
|
|
|
for freq, label_str in added_labels:
|
|
|
|
for action in added_actions[label_str]:
|
2018-03-27 17:23:02 +00:00
|
|
|
self.add_action(int(action), label_str)
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
self.labels[action][label_str] = freq
|
2018-03-27 17:23:02 +00:00
|
|
|
|
2017-05-28 16:09:27 +00:00
|
|
|
def add_action(self, int action, label_name):
|
|
|
|
cdef attr_t label_id
|
2017-10-18 19:48:00 +00:00
|
|
|
if not isinstance(label_name, int) and \
|
2017-10-27 17:45:57 +00:00
|
|
|
not isinstance(label_name, long):
|
2017-05-28 16:09:27 +00:00
|
|
|
label_id = self.strings.add(label_name)
|
|
|
|
else:
|
|
|
|
label_id = label_name
|
2016-01-19 19:10:04 +00:00
|
|
|
# Check we're not creating a move we already have, so that this is
|
|
|
|
# idempotent
|
|
|
|
for trans in self.c[:self.n_moves]:
|
2017-05-28 16:09:27 +00:00
|
|
|
if trans.move == action and trans.label == label_id:
|
2016-01-19 19:10:04 +00:00
|
|
|
return 0
|
2016-01-19 18:09:33 +00:00
|
|
|
if self.n_moves >= self._size:
|
|
|
|
self._size *= 2
|
|
|
|
self.c = <Transition*>self.mem.realloc(self.c, self._size * sizeof(self.c[0]))
|
2017-05-28 16:09:27 +00:00
|
|
|
self.c[self.n_moves] = self.init_transition(self.n_moves, action, label_id)
|
2016-01-19 18:09:33 +00:00
|
|
|
self.n_moves += 1
|
2018-08-15 13:37:24 +00:00
|
|
|
# Add the new (action, label) pair, making up a frequency for it if
|
|
|
|
# necessary. To preserve sort order, the frequency needs to be lower
|
|
|
|
# than previous frequencies.
|
2018-03-27 17:23:02 +00:00
|
|
|
if self.labels.get(action, []):
|
|
|
|
new_freq = min(self.labels[action].values())
|
|
|
|
else:
|
|
|
|
self.labels[action] = Counter()
|
|
|
|
new_freq = -1
|
|
|
|
if new_freq > 0:
|
|
|
|
new_freq = 0
|
|
|
|
self.labels[action][label_name] = new_freq-1
|
2016-01-19 19:10:04 +00:00
|
|
|
return 1
|
2017-05-29 09:45:45 +00:00
|
|
|
|
2019-03-10 18:16:45 +00:00
|
|
|
def to_disk(self, path, **kwargs):
|
2017-05-31 11:44:00 +00:00
|
|
|
with path.open('wb') as file_:
|
2019-03-10 18:16:45 +00:00
|
|
|
file_.write(self.to_bytes(**kwargs))
|
2017-05-29 09:45:45 +00:00
|
|
|
|
2019-03-10 18:16:45 +00:00
|
|
|
def from_disk(self, path, **kwargs):
|
2017-05-31 11:44:00 +00:00
|
|
|
with path.open('rb') as file_:
|
|
|
|
byte_data = file_.read()
|
2019-03-10 18:16:45 +00:00
|
|
|
self.from_bytes(byte_data, **kwargs)
|
2017-05-29 09:45:45 +00:00
|
|
|
return self
|
|
|
|
|
2020-07-06 11:06:25 +00:00
|
|
|
def to_bytes(self, exclude=tuple()):
|
2017-05-29 09:45:45 +00:00
|
|
|
serializers = {
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
'moves': lambda: srsly.json_dumps(self.labels),
|
2021-06-17 07:33:00 +00:00
|
|
|
'strings': lambda: self.strings.to_bytes(),
|
|
|
|
'cfg': lambda: self.cfg
|
2017-05-29 09:45:45 +00:00
|
|
|
}
|
|
|
|
return util.to_bytes(serializers, exclude)
|
|
|
|
|
2020-07-06 11:06:25 +00:00
|
|
|
def from_bytes(self, bytes_data, exclude=tuple()):
|
2021-06-17 07:33:00 +00:00
|
|
|
# We're adding a new field, 'cfg', here and we don't want to break
|
|
|
|
# previous models that don't have it.
|
|
|
|
msg = srsly.msgpack_loads(bytes_data)
|
2018-03-27 17:23:02 +00:00
|
|
|
labels = {}
|
2021-06-17 07:33:00 +00:00
|
|
|
if 'moves' not in exclude:
|
|
|
|
labels.update(srsly.json_loads(msg['moves']))
|
|
|
|
if 'strings' not in exclude:
|
|
|
|
self.strings.from_bytes(msg['strings'])
|
|
|
|
if 'cfg' not in exclude and 'cfg' in msg:
|
|
|
|
self.cfg.update(msg['cfg'])
|
2018-03-27 17:23:02 +00:00
|
|
|
self.initialize_actions(labels)
|
2017-05-29 09:45:45 +00:00
|
|
|
return self
|