ModelScope模型下载脚本
分享一个自己写的下载ModelScope大模型文件的python脚本,支持多线程下载,支持多个模型一起下载。
1. 安装modelscope模块
下载客户端已经安装python3和pip,执行如下命令安装modelscope
:
pip install modelscope -i https://mirrors.aliyun.com/pypi/simple/
2. 运行下载脚本
创建python脚本文件modelscope_spdownload.py
,内容如下:
from modelscope.hub.snapshot_download import snapshot_download
import time
from concurrent.futures import ThreadPoolExecutor
def download_snapshot(*, model_id):
print('Downloading snapshot {}'.format(model_id))
snapshot_download(f'{model_id}', cache_dir='/tmp')
def main():
with ThreadPoolExecutor(max_workers=4) as executor:
model_ids = ['qwen/Qwen-7B', 'qwen/Qwen-7B-Chat', 'qwen/Qwen-7B-Chat-Int4']
start = time.time()
for model_id in model_ids:
executor.submit(download_snapshot, model_id=model_id)
end = time.time()
print('Total elapsed time: {}'.format(end - start))
if __name__ == '__main__':
main()
2.1 修改下载位置
根据下载的客户端操作系统,修改模型下载位置:
- windows:cache_dir=‘C:\tmp’,将模型下载到C盘tmp目录;
- linux: cache_dir=‘/tmp’,将模型下载到/tmp目录;
2.2 填写下载的model_ids
根据需求更新下载的model_ids,model_id可以在modelscope官网进入具体的模型文件界面查询。例如上述脚本会下载千问7B的三个model:
model_ids = ['qwen/Qwen-7B', 'qwen/Qwen-7B-Chat', 'qwen/Qwen-7B-Chat-Int4']
2.3 运行脚本
根据客户端配置修改下载的线程数,上述脚本为4线程,max_workers=4
。
运行python modelscope_spdownload.py
执行下载。