【发布时间】:2023-12-06 06:55:01
【问题描述】:
在related question solution 之后,我创建了 docker 容器,它在 docker 容器中加载 GoogleNews-vectors-negative300 KeyedVector 并将其全部加载到内存中
KeyedVectors.load(model_path, mmap='r')
word_vectors.most_similar('stuff')
我还有另一个 Docker 容器,它提供了 REST API,它用
加载这个模型KeyedVectors.load(model_path, mmap='r')
我观察到满载的容器占用超过 5GB 的内存,每个 gunicorn worker 占用 1.7GB 的内存。
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
acbfd080ab50 vectorizer_model_loader_1 0.00% 5.141GiB / 15.55GiB 33.07% 24.9kB / 0B 32.9MB / 0B 15
1a9ad3dfdb8d vectorizer_vectorizer_1 0.94% 1.771GiB / 15.55GiB 11.39% 26.6kB / 0B 277MB / 0B 17
但是,我希望所有这些进程为 KeyedVector 共享相同的内存,因此所有容器之间只需要共享 5.4 GB。
有人尝试过实现这一目标并取得成功吗?
编辑: 我尝试了以下代码 sn-p,它确实在不同的容器之间共享相同的内存。
import mmap
from threading import Semaphore
with open("data/GoogleNews-vectors-negative300.bin", "rb") as f:
# memory-map the file, size 0 means whole file
fileno = f.fileno()
mm = mmap.mmap(fileno, 0, access=mmap.ACCESS_READ)
# read whole content
mm.read()
Semaphore(0).acquire()
# close the map
mm.close()
所以KeyedVectors.load(model_path, mmap='r')不共享内存的问题
编辑2:
研究gensim的源代码我看到np.load(subname(fname, attrib), mmap_mode=mmap)被调用来打开memmaped文件。以下代码示例跨多个容器共享内存。
from threading import Semaphore
import numpy as np
data = np.load('data/native_format.bin.vectors.npy', mmap_mode='r')
print(data.shape)
# load whole file to memory
print(data.mean())
Semaphore(0).acquire()
【问题讨论】:
标签: python mmap gensim word2vec