Set a LRU cache for word embeddings for a decrease of 20% of inference time #1084
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Set an embedding LRU cache of Tensor of word embeddings.
This approach avoid to convert most used Gensim embeddings to Pytorch Tensor and even more important to avoid to transfer from computer RAM to GPU Ram (this is a slow operation).
Because of zipf law, the effect of such caching approach are magnified.
LRU cache is set to 10000 because it s very small and still provide most of the performance boost compared to loading all embeddings in GPU Ram. A 1000 embedding LRU cache is slightly less performant on my own dataset.
A second little optimization is to replace a call of
unsqueeze
on each token followed by acat
by a single call ofstack
.Time to process 100 French documents decreased from 33s to 26s with this PR.
For what it worths, Spacy takes exactly the same time (26s) on my dataset with much lower accuracy on some tricky entities (and same accuracy on easiest to recognize entities).
Another change is on Ner HTML viewer with the introduction of a HTML title parameter.