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

Fix speed problem with top_k>1 on CPU in edit tree lemmatizer #12017

Merged
merged 10 commits into from
Jan 20, 2023
23 changes: 7 additions & 16 deletions spacy/pipeline/edit_tree_lemmatizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,18 @@ def predict(self, docs: Iterable[Doc]) -> List[Ints2d]:
def _scores2guesses(self, docs, scores):
guesses = []
for doc, doc_scores in zip(docs, scores):
if self.top_k == 1:
doc_guesses = doc_scores.argmax(axis=1).reshape(-1, 1)
else:
doc_guesses = np.argsort(doc_scores)[..., : -self.top_k - 1 : -1]

if not isinstance(doc_guesses, np.ndarray):
doc_guesses = doc_guesses.get()

doc_compat_guesses = []
for token, candidates in zip(doc, doc_guesses):
tree_id = -1
for candidate in candidates:
for i, token in enumerate(doc):
for _ in range(self.top_k):
richardpaulhudson marked this conversation as resolved.
Show resolved Hide resolved
candidate = doc_scores[i].argmax()
richardpaulhudson marked this conversation as resolved.
Show resolved Hide resolved
candidate_tree_id = self.cfg["labels"][candidate]

if self.trees.apply(candidate_tree_id, token.text) is not None:
tree_id = candidate_tree_id
doc_compat_guesses.append(candidate_tree_id)
break
doc_compat_guesses.append(tree_id)

doc_scores[i, candidate] = -1
richardpaulhudson marked this conversation as resolved.
Show resolved Hide resolved
else:
doc_compat_guesses.append(-1)
guesses.append(np.array(doc_compat_guesses))

return guesses

def set_annotations(self, docs: Iterable[Doc], batch_tree_ids):
Expand Down