def load_memory_map_dir(directory: str) -> Embeddings:
"""
Loads embeddings from a memory map directory to allow lazy loading (and reduce the memory usage).
Args:
directory: a file prefix. This function loads two files in the directory: a meta json file with shape information
and the vocabulary, and the actual memory map file.
Returns:
Embeddings object with a lookup matrix that is backed by a memory map.
"""
meta_file = os.path.join(directory, "meta.json")
mem_map_file = os.path.join(directory, "memory_map")
with open(meta_file, "r") as f:
meta = json.load(f)
shape = tuple(meta['shape'])
vocab = meta['vocab']
mem_map = np.memmap(mem_map_file, dtype='float32', mode='r+', shape=shape)
result = Embeddings(vocab, mem_map, filename=directory, emb_format="memory_map_dir")
return result
评论列表
文章目录