为什么并行处理需要比通常的代码更长的时间?

问题描述

该想法是使用不同的新语料集集来更新特定的预训练word2vec模型。我有以下

<indicator:Title>MalicIoUs FQDN Indicator</indicator:Title>
<indicator:Type xsi:type="stixVocabs:IndicatorTypeVocab-1.1">Domain Watchlist</indicator:Type>
<indicator:Observable id="CISA:Observable-1a97193f-7206-4bda-aadb-a9876943629b">
<cyBox:Object id="CISA:Object-8ee878fd-147c-4438-be3a-ad61470eec80">
<cyBox:Properties xsi:type="DomainNameObj:DomainNameObjectType" type="FQDN">
<DomainNameObj:Value condition="Equals">jp-ssl[.]work</DomainNameObj:Value>
</cyBox:Properties>
</cyBox:Object>
</indicator:Observable>
<indicator:Sightings sightings_count="1">
<indicator:Sighting timestamp="2018-05-01T00:00:00"/>
</indicator:Sightings>
</stix:Indicator>
<stix:Indicator id="CISA:Indicator-90e0da37-5c97-49f1-8fe5-5a4db57513fa" timestamp="2020-10-23T18:29:40.163791+00:00" xsi:type="indicator:IndicatorType">
<indicator:Title>MalicIoUs FQDN Indicator</indicator:Title>
<indicator:Type xsi:type="stixVocabs:IndicatorTypeVocab-1.1">Domain Watchlist</indicator:Type>
<indicator:Observable id="CISA:Observable-40f4bf4d-db81-4560-bbaa-73ce4a9ead99">
<cyBox:Object id="CISA:Object-da253d57-6148-435a-ae55-a4a9cd1d4661">
<cyBox:Properties xsi:type="DomainNameObj:DomainNameObjectType" type="FQDN">
<DomainNameObj:Value condition="Equals">intemet[.]work</DomainNameObj:Value>
</cyBox:Properties>
</cyBox:Object>
</indicator:Observable>
<indicator:Sightings sightings_count="1">
<indicator:Sighting timestamp="2018-05-01T00:00:00"/>
</indicator:Sightings>
</stix:Indicator>

大约需要13分钟才能运行。但是非并行版本(在# c1,c2 are each a list of 100 files filelist = [c1,c2,c3,c4,c5,c6,c7,c8,c9,c10] def update_model(files): # loading a pre-trained model trained_model = gensim.models.Word2Vec.load("model_both_100") # Document Feeder is an iterable docs = DocumentFeeder(files) trained_model.build_vocab(docs,update=True) trained_model.train(docs,total_examples=trained_model.corpus_count,epochs=trained_model.epochs) with Pool(processes=10) as P: P.map(update_model,filelist) 上循环)大约需要11分钟。为什么会这样呢?在12核cpu上运行。

解决方法

Gensim的Word2Vec培训已经使用了多个线程–取决于模型创建时的workers参数。 (默认为使用workers=3,但您的模型可能已初始化为使用更多的模型。)

因此,您要启动10个(重量级)流程,每个流程分别加载一个全尺寸模型。这很容易触发大量内存使用,从而引发虚拟内存交换。

然后,这些模型中的每一个都会进行自己的(单线程)词汇扩展,然后进行训练(一个管理器线程和3个或更多工作线程)。如果他们都同时接受培训,则意味着在12个核心处理器上的10个OS进程中有40个线程处于活动状态。在这种情况下,没有理由必然会提速,而且线程多于内核的争用以及所有争夺访问完全不同的加载模型内存范围的竞争都可以轻松地解释这种情况。

您是否真的要创建10个单独的增量更新模型? (在更新培训之后,它们是否会重新保存为10个不同的文件名?)