CTranslate2 将转换后的model.bin分割成多个.bin文件,

zpqajqem  于 2个月前  发布在  其他
关注(0)|答案(2)|浏览(40)

对于LLama-2-70b、Bloom等模型,可能会经常遇到的问题是:如何将model.bin分割成多个子文件?

例如,在PyTorch中,状态字典会被分割。
model.bin = model-00001-of-00002.bin + model-00002-of-00002.bin

在我的具体情况下,即使LLama-70b的转换尚未实现,因为它们的注意力机制发生了变化,但在int8之后的转换大小约为65GB-70GB。由于这个较大的文件大小,许多服务(如Cloudflare、Huggingface)拒绝将模型作为单个文件上传。

这也将提高网络上的并行化/下载速度,例如,在Huggingface的Transformers库中,小于50GB的模型也会被分割成9GB的部分。

wlp8pajw

wlp8pajw1#

顺便问一下,你是否成功地使用转换CT2脚本将较小的Llama2模型进行了转换?
编辑:在已关闭的问题中找到了讨论:#1351

roqulrg3

roqulrg32#

同时,我认为你可以在CTranslate2之外完成这个操作。
在上传之前,将model.bin分成多个部分,例如:

split -d -b 9GB model.bin model.bin.
rm model.bin

下载后,从多个部分重建model.bin。你可以使用Python来实现这个功能,例如:

import os
import glob
import shutil

model_bin = os.path.join(model_dir, "model.bin")

if not os.path.exists(model_bin):
    shards = glob.glob("%s.*" % model_bin)
    shards = sorted(shards, key=lambda path: int(path.split(".")[-1]))
    with open(model_bin, "wb") as model_bin_file:
        for shard in shards:
            with open(shard, "rb") as shard_file:
                shutil.copyfileobj(shard_file, model_bin_file)
            os.remove(shard)

我没有测试过这段代码,但你应该能理解这个想法。

相关问题