Terms Trains Crashing

{
“lang”:“en”,
“pipeline”:[
“tagger”,
“parser”,
“ner”
],
“accuracy”:{
“token_acc”:99.8698372794,
“ents_p”:84.9664503965,
“ents_r”:85.6312524451,
“uas”:91.7237657538,
“tags_acc”:97.0403350292,
“ents_f”:85.2975560875,
“las”:89.800872413
},
“name”:“core_web_sm”,
“license”:“CC BY-SA 3.0”,
“author”:“Explosion AI”,
“url”:“https://explosion.ai”,
“vectors”:{
“width”:200,
“vectors”:4644099,
“keys”:4087446
},
“sources”:[
“OntoNotes 5”,
“Common Crawl”
],
“version”:“2.0.0”,
“spacy_version”:">=2.0.0a18",
“parent_package”:“spacy”,
“speed”:{
“gpu”:null,
“nwords”:291344,
“cpu”:5122.3040471407
},
“email”:“contact@explosion.ai”,
“description”:“English multi-task CNN trained on OntoNotes, with GloVe vectors trained on Common Crawl. Assigns word vectors, context-specific token vectors, POS tags, dependency parse and named entities.”
}

Thanks.

Surely something to do with the custom vectors. If you run without your vectors by just using spacy.load('en_core_web_sm'), can you confirm there’s no leak?

I’ll open an issue for this on the spaCy tracker, probably later today. Hopefully I’ll have a runtime workaround, but if not I can upload a development build for you to pip install.

This does not cause any memory leak. I ran it for an hour and it did not lead to memory spike.

Thanks! Closing in on this…

Now add:

from prodigy.components import preprocess
nlp.add_pipe(preprocess.merge_entities, name='merge_entities')
nlp.add_pipe(preprocess.merge_noun_chunks, name='merge_noun_chunks')

This should be added after the call to spacy.load(). If this leaks, the problem is in spaCy’s doc.merge(). If it doesn’t leak, the problem is in Prodigy, somewhere innocuous – possibly the text loader or something.

Even without merge_entities and merge_noun_chunks we had memory leak. Should i still try this ?

As a sanity check I think it would be useful.

I wrote simple code with blank model and it gives me memory leak.

from future import unicode_literals
import plac
import numpy
from gensim.models.word2vec import Word2Vec
from gensim.models import KeyedVectors
import spacy
from prodigy.components import preprocess
from prodigy.components.loaders import get_stream

nlp = spacy.blank(“en”)
nlp.add_pipe(nlp.create_pipe(‘sentencizer’))

#doc = nlp(“testing is good habit. I am doing great”)

stream = get_stream("/Users/philips/Development/BigData/RS/word2vecreport/report/allReporttext.txt", loader=None, input_key=‘text’)
sentences = []
count = 0

for doc in nlp.pipe((eg[‘text’] for eg in stream)):
count = count + 1
for sent in doc.sents:
sentences.append([w.text for w in doc])
print(count)

w2v = Word2Vec(sentences, size=300, window=5, min_count=10,
sample=1e-5, iter=1, workers=6,
negative=5)

nlp.vocab.reset_vectors(width=300)
output_model = “/Users/philips/Development/BigData/RS/word2vecreport/report/pmcmodel”
for word in w2v.wv.vocab:
nlp.vocab.set_vector(word, w2v.wv.word_vec(word))

nlp.to_disk("/Users/philips/Development/BigData/RS/word2vecreport/report/pmcmodel")
print(‘Trained Word2Vec model’, output_model.resolve())

Also what I don’t understand is we are iterating through sentences but not using the sent object. This might be the root cause of the issue.

For example I had sample file with below text:
testing the sentences. there might be a memory leak. This will repeat three times for each sentences.

the sentence array contained the same tokens repeated 3 times as there were three sentences.

[
[‘testing’, ‘the’, ‘sentences’, ‘.’, ‘there’, ‘might’, ‘be’, ‘a’, ‘memory’, ‘leak’, ‘.’, ‘This’, ‘will’, ‘repeat’, ‘there’, ‘times’, ‘for’, ‘each’, ‘sentences’, ‘.’],

[‘testing’, ‘the’, ‘sentences’, ‘.’, ‘there’, ‘might’, ‘be’, ‘a’, ‘memory’, ‘leak’, ‘.’, ‘This’, ‘will’, ‘repeat’, ‘there’, ‘times’, ‘for’, ‘each’, ‘sentences’, ‘.’],

[‘testing’, ‘the’, ‘sentences’, ‘.’, ‘there’, ‘might’, ‘be’, ‘a’, ‘memory’, ‘leak’, ‘.’, ‘This’, ‘will’, ‘repeat’, ‘there’, ‘times’, ‘for’, ‘each’, ‘sentences’, ‘.’]
]

This might be the root cause of the memory issue.

That’s definitely a bug! It should be [w.text for w in sent]!

I don’t understand why my linter didn’t pike that up — usually we run frosted, which identified unused variables.

You can’t use two sets of pre-trained word vectors in the same model. If you have vectors from your PMC model, you can use those — or, alternatively, you can train new vectors which you would use instead.

This applies to both spaCy and Gensim: you can only use two sets of vectors separately, not together, because they don’t map the words to any consistent space. It’s not a software limitation; that’s just how these things work.

Another OOM issue. The document size is around 4million and around 27GB. Machine configuration is 60GB and 16cores. I am unable to proceed further. Any workaround.

File “/home/madhujahagirdar/bionlp-gpu/venv/lib/python3.5/site-packages/thinc/api.py”, line 55, in predict
X = layer(X)
File “/home/madhujahagirdar/bionlp-gpu/venv/lib/python3.5/site-packages/thinc/neural/_classes/model.py”, line 161, in call
return self.predict(x)
File “/home/madhujahagirdar/bionlp-gpu/venv/lib/python3.5/site-packages/thinc/neural/_classes/convolution.py”, line 25, in predict
return self.ops.seq2col(X, self.nW)
File “ops.pyx”, line 462, in thinc.neural.ops.NumpyOps.seq2col
File “cymem/cymem.pyx”, line 42, in cymem.cymem.Pool.alloc (cymem/cymem.cpp:1091)
MemoryError: Error assigning 18446744072542178688 bytesError assigning 18446744072542178688 bytesError assigning 18446744072542178688 bytesError assigning 18446744072542178688 bytes
3166000

I have split the documents into Max 100 lines now, still I am running to OOM issue.

@ines, @honnibal. Spacy and Prodigy are both awesome tools and I would love to use it. With the issues what I am having to train and use it in production, I am unable to proceed further due to OOM issues. I would love some more support. Let me know if there are other ways to get support on this (like enterprise license etc.,)

@madhujahagirdar That error is coming from the convolutional layers, trying to assign a really enormous block of memory. Are you still running terms.train-vectors? If you’ve emptied the pipeline there should be no reason you’d be calling into that function.

More importantly, why is the allocation so large? What’s the maximum length of the documents you’re working with, in number of words?

Unfortunately we can’t offer a support contract, as we need to prioritise any bugs or feature requests based on their impact, no matter who reports them.

I have around 4 million documents, and each document size is 100 lines (roughly around 8-10kb) and total corpus size is 27GB. I am trying to create word2vec model using spacy and terms.train. After processing 300k documents it consumes all 60GB and runs out of memory.

I have disabled all the pipes, except sentencizer: (in terms.py)

nlp = spacy.load(spacy_model)
nlp.disable_pipes(*nlp.pipe_names)
nlp.add_pipe(nlp.create_pipe(‘sentencizer’))

and also reduced the batch size

for doc in nlp.pipe((eg[‘text’] for eg in stream),batch_size=25,n_threads=-1):

and fixed the loop which was causing potential mem leak: (refered above)
for sent in doc.sents:
sentences.append([w.text for w in sent])

Even with all these, after 300k it consumes all 60GB and causes OOM.

The error above indicates that the pipeline is somehow not disabled — the convolutional layers are being applied, which means that there’s some model being used.

Your corpus is quite large. Do you run out of memory if you just train the word vectors with Gensim directly? RaRe, the makers of Gensim, do offer support — you could contact them here: https://rare-technologies.com/contact/

The error above is not after disabling the pipelines, it was before. The kernel kills the python process now as it consumes all the Memory.

I can train word2vec using gensim, without any memory issues, however, once I build in gensim, I have another issue in batch train ( i have reported this as well ) which crashes due to OOM as well if I use model from gensim. https://support.prodi.gy/t/batch-train-buffer-full/373/12

It’s a Pub-Med corpus and publically available data. If you can give me repo, I can upload the corups.

I’m confused — so if you do disable the pipeline, does the training work?

Also can you try just training with Gensim but setting the dimensionality quite small (e.g. 64 dimensions), and a high frequency threshold? Then you should be able to get a smaller vector model, to keep working. It shouldn’t affect accuracy that much to have a word vector model that’s like 300mb.

OK, Will switch to gensim for word2vec, but after I build word2vec with gensim I cannot use prodigy to do text classification due to another issue with batch.train OOM https://support.prodi.gy/t/batch-train-buffer-full/373/12

You can still use Prodigy for text classification. If you use a smaller vector model this issue with the serialization won’t occur, preventing the problem you were initially trying to work around. If necessary you can just not use the word vectors — they’ll improve accuracy, but it’s still definitely possible to train without them.